Icon on title bar?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

A short example which runs in Windows and Linux and uses a CDPlayer icon which is contained in each PB Windows and Linux installation:

Code: Select all

IconPath.S = #PB_Compiler_Home + "examples/sources/Data/CdPlayer.ico"

OpenWindow(0, 0, 0, 300, 100, "<-- Changed titlebar icon", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_window_set_icon_from_file_(WindowID(0), PeekS(@IconPath), IconError)
CompilerElse
  IconPath = ReplaceString(IconPath, "/", "\")

  If LoadImage(0, IconPath)
    SendMessage_(WindowID(0), #WM_SETICON, 0, ImageID(0))
  EndIf
CompilerEndIf

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

This should be added to feature request as a built-in command SetWindowIcon - like SetWindowTitle.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
gedumer
User
User
Posts: 48
Joined: Wed Jun 03, 2009 9:04 pm

Post by gedumer »

Shardik wrote:A short example which runs in Windows and Linux and uses a CDPlayer icon which is contained in each PB Windows and Linux installation:

Code: Select all

IconPath.S = #PB_Compiler_Home + "examples/sources/Data/CdPlayer.ico"

OpenWindow(0, 0, 0, 300, 100, "<-- Changed titlebar icon", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
    gtk_window_set_icon_from_file_(WindowID(0), PeekS(@IconPath), IconError)
CompilerElse
  IconPath = ReplaceString(IconPath, "/", "")

  If LoadImage(0, IconPath)
    SendMessage_(WindowID(0), #WM_SETICON, 0, ImageID(0))
  EndIf
CompilerEndIf

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
This works well and I can even use CatchImage in the Windows version so I don't have to load it from a file. Is it possible to do the same thing with the GTK API? I looked at the docs but I don't see any possibility... of course I've never looked at GTK docs before so I'm probably missing something.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Icon on title bar?

Post by Vera »

Hello Shardik,

today I've come to share your help ~ Image
but it wouldn't work for me on Linux (Suse 11.1) with all pb-versions down to 4.31. (???) until I exchanged the WindowID(0) by an enumerated constant.
Strange this makes a difference ~ but this way the icon shines from the title to me ;)

thanks ~ Vera
cas
Enthusiast
Enthusiast
Posts: 597
Joined: Mon Nov 03, 2008 9:56 pm

Re: Icon on title bar?

Post by cas »

Shardik wrote:

Code: Select all

gtk_window_set_icon_from_file_(WindowID(0), PeekS(@IconPath), IconError)
Why PeekS()? IconPath is already string variable.
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Icon on title bar?

Post by Shardik »

Vera wrote:but it wouldn't work for me on Linux (Suse 11.1) with all pb-versions down to 4.31. (???)
Dear Vera,

I have tested my above example on OpenSUSE 11.1 with 4 freshly installed PB versions
(4.20, 4.31, 4.41 and 4.51) and discovered that only 4.20 doesn't display the CD icon.
This is caused by a rename in the distributed PB tgz archive of the sub folder /data
from 4.20 to /Data in 4.31 and subsequent versions. Since Linux is case sensitive, this
change of one character from lowercase to uppercase had the effect that the icon file
couldn't be found. Therefore everyone should improve my code example by testing the
return value of the GTK function:

Code: Select all

If gtk_window_set_icon_from_file_(WindowID(0), @IconPath, IconError) = #False
  MessageRequester("Error", "Icon file couldn't be found!")
EndIf
Your described effect with not being able to use WindowID(0) I can't confirm nor explain...


@cas,

you are right, the PeekS(@IconPath) is not necessary. Just using "@IconPath" is sufficient.
Thank you for your hint.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Icon on title bar?

Post by freak »

If you use PB 4.40 or newer, you can just load the image normally with PB commands, and then do:

Code: Select all

gtk_window_set_icon_(WindowID(#YourWindow), ImageID(#YourImage))
Or, if you want it to apply to all new windows, do:

Code: Select all

gtk_window_set_default_icon_(ImageID(#YourImage))
Just keep in mind that PB/Linux doesn't load *.ico files. The best is to use png's with alpha channel (don't forget UsePNGImageDecoder() ;))
quidquid Latine dictum sit altum videtur
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Icon on title bar?

Post by Vera »

Hello Shardik,

thanks for regarding my hint. As for the path - that was the first I checked, but only for the first version.
I tested it all again (several 100 times) and it's a case of driving nuts, as this time it wouldn't work with the constant until I switched back to the first code. There again it only worked once then 10 times not, then again .... the same with the world.png. But today I saw that in all cases the image is always displayed on the taskbarbutton [standart hidden on my OS] but not necessarily on the titlebar.

I have no clue why it behaves like this. I made another 10 passes of 60 compilations (devided by minimal changes) and it wouldn't even show up in each circle (1 time each in 3 passes, 2 times in 2 passes and 4 times in 1 pass).

As the application where I use your method only seldom lacks to display the icon on compilation it might be due to the shortness of the code snippet. Maybe the window is already final build up before the image can be supplied?

I would conclude it works fine (except for this special incidence) and have an eye on it when I'll apply it in coming usages.

greetings ~ Vera
Post Reply