Add an icon to a program

Linux specific forum
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Add an icon to a program

Post by Armoured »

Hi :)
How I can add an icon to a Linux PureBasic program?
In Linux the icons are external files?


Thanks
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

yes, the are external files...
add an icon with:

Code: Select all

gtk_window_set_icon_name_(*window, stockitem$);
if it's a stock item
(*window a pointer to your Window
stockitem$ is a Gtk-stockItem)
or

Code: Select all

gtk_window_set_icon_(*window,*icon);
*window a pointer to your Window
*icon must be a pointer to a GdkPixbuf
or

Code: Select all

gtk_window_set_icon_from_file_(*window, *filename, *err)
*window a pointer to your Window
*filename ..... well the path to your icon
*err a variable for which contais the error (if any occurs)

I guess the function you'll need is the third one.... (don't know it exactely but I think you an use every image-format GTK+ knows... and that are a lot.... :D )
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

hi walker
gtk_window_set_icon_from_file_(*window, *filename, *err) doesn't work any more with purebasic 4.10 is a bug?
This problems come up when I try to compile an unicode executable.
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

no bug... gtk expects utf-8 formated strings.... the filename you pass isn't... that's something I had to learn too :wink:

see http://www.purebasic.fr/english/viewtopic.php?t=29302 and try that with gtk_window_set_icon_from_file_() it will work 8)
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

walker wrote:no bug... gtk expects utf-8 formated strings.... the filename you pass isn't... that's something I had to learn too :wink:

see http://www.purebasic.fr/english/viewtopic.php?t=29302 and try that with gtk_window_set_icon_from_file_() it will work 8)
Ok!
I try this:

Code: Select all

iconpath.s = GetPathPart(ProgramFilename()) + "Icon.png"
iconerror.l = 0
pathutf8.s = Space(255)
PokeS(@pathutf8,iconpath,#PB_UTF8)
gtk_window_set_icon_from_file_(Wpointer, @pathutf8, @iconerror)
where is the error?

Thanks
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

first, if you use the flags parameter at PokeS, you have to add the length (or -1 for the whole length) otherwise the flag is treaten as length.....
second, import the needed function to pass the pseudo variable type p-utf8 and let PB translate the string into the desired format (see pseudotypes section in the manual)

here a working example:

Code: Select all

ImportC "/usr/lib/libgtk-x11-2.0.so"
  gtk_window_set_icon_from_file(*window,icon.p-utf8,*error)
EndImport

iconpath.s = GetPathPart(ProgramFilename()) + "Icon.png"
iconerror.l = 0
pathutf8.s = Space(255)
PokeS(@pathutf8,iconpath,-1,#PB_UTF8)
Wpointer=OpenWindow(#PB_Any ,0,0,200,100,"Icon-Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
gtk_window_set_icon_from_file(WindowID(Wpointer), PeekS(@iconpath), iconerror)
Repeat

Until WaitWindowEvent(1)=#PB_Event_CloseWindow
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Thanks walker!
only one thing the import part is useless :)
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

well.. not if you want to create an exe with unicode enabled..... :wink: but you can delete the line PokeS(...) in this example
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

walker wrote:well.. not if you want to create an exe with unicode enabled..... :wink: but you can delete the line PokeS(...) in this example
Hi walker
Here work well without the include part. Please try yourself.

Thanks again!
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Thanks for this code snippet! Works well here.

Is there a way to include the image with "IncludeBinary" and use it for the icon? No big deal if not, just curious.
-Beach
walker
Enthusiast
Enthusiast
Posts: 634
Joined: Wed May 05, 2004 4:04 pm
Location: Germany

Post by walker »

ohhhh... yesssss.. now I see what you mean..

Code: Select all

 iconpath.s = GetPathPart(ProgramFilename()) + "Icon.png"
iconerror.l = 0
pathutf8.s = Space(255)
PokeS(@pathutf8,iconpath,-1,#PB_UTF8)

Wpointer=OpenWindow(#PB_Any ,0,0,200,100,"Icon-Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
gtk_window_set_icon_from_file_(WindowID(Wpointer), PeekS(@pathutf8), iconerror) 

Repeat

Until WaitWindowEvent(1)=#PB_Event_CloseWindow
this way it will work without the Import....

@beach: yes, that would be possible.. but needs a little work as you can pass a GDKPixbuf to

Code: Select all

gtk_window_set_icon_(*window,*icon); 
if you include a png or whatever, you first have to create the PixBuf from it... I'd never tried this... but I will have a look how it would work...
User avatar
bembulak
Enthusiast
Enthusiast
Posts: 576
Joined: Mon Mar 06, 2006 3:53 pm
Location: Austria

Post by bembulak »

Maybe this helps:
http://www.purebasic.fr/english/viewtop ... highlight=

I asked for this some time ago and Freak posted a working example. It's great, though I'd like to load the icon from a file instead of using a data section.

I you'll get it working, please let us know. Thanks.
cheers,

bembulak
Post Reply