Page 1 of 1

Add an icon to a program

Posted: Fri Sep 21, 2007 1:38 am
by Armoured
Hi :)
How I can add an icon to a Linux PureBasic program?
In Linux the icons are external files?


Thanks

Posted: Tue Oct 09, 2007 1:45 am
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 )

Posted: Sun Nov 11, 2007 12:13 am
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.

Posted: Sun Nov 11, 2007 9:26 am
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)

Posted: Sun Nov 11, 2007 10:42 am
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

Posted: Sun Nov 11, 2007 11:57 am
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

Posted: Sun Nov 11, 2007 1:33 pm
by Armoured
Thanks walker!
only one thing the import part is useless :)

Posted: Sun Nov 11, 2007 1:49 pm
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

Posted: Sun Nov 11, 2007 8:34 pm
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!

Posted: Sun Nov 11, 2007 8:50 pm
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.

Posted: Sun Nov 11, 2007 9:43 pm
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...

Posted: Mon Nov 12, 2007 11:37 am
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.