Page 1 of 1
Setting the program's icon in Linux
Posted: Sun Jun 08, 2014 7:31 pm
by heartbone
The code below works in 64 bit, but only when a small .png file present in the run directory.
I am using a 48 pixel square image.
I thought that after I compile the code into an executable,
then the .png data is available at runtime just as if the local file were there.
But after creating an executable, I can only get it to work properly with the png file in the same folder.
Code: Select all
ImportC ""
gtk_window_set_icon_from_file(a.l, b.l, c.l)
EndImport
DataSection
LINUXICON:
IncludeBinary "S48.png"
EndDataSection
Procedure SETLINUXICON(iWindow.i)
Protected.l lError
gtk_window_set_icon_from_file_(WindowID(iWindow),"S48.png",lError)
EndProcedure
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SETLINUXICON(0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
Replacing the call in the code snippet above with the following line:
gtk_window_set_icon_from_file_(WindowID(iWindow),?LINUXICON,lError)
and then running yields the error: Failed to open file (random garbage)PNG
(garbage) No such file or directory.
The documentation at
https://developer.gnome.org/ states:
gtk_window_set_icon_from_file (GtkWindow *window,
const gchar *filename,
GError **err);
Sets the icon for window . Warns on failure if err is NULL.
This function is equivalent to calling gtk_window_set_icon() with a pixbuf created by loading the image from filename .
Parameters:
window - a GtkWindow
filename - location of icon file. [type filename]
err - location to store error, or NULL.[allow-none]
Does anyone know how to accomplish setting the GTK icon, without having the file actually existing in a folder?
Re: Setting the program's icon in Linux
Posted: Sun Jun 08, 2014 7:45 pm
by luis
Try this, I'm on Win so not tested.
Code: Select all
UsePNGImageDecoder()
DataSection
LINUXICON:
IncludeBinary "S48.png"
EndDataSection
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
icon = CatchImage(#PB_Any, ?LINUXICON)
gtk_window_set_icon_(WindowID(0), ImageID(icon))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
Re: Setting the program's icon in Linux
Posted: Sun Jun 08, 2014 7:48 pm
by heartbone
luis wrote:Try this, I'm on Win so not tested.
Code: Select all
UsePNGImageDecoder()
DataSection
LINUXICON:
IncludeBinary "S48.png"
EndDataSection
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
icon = CatchImage(#PB_Any, ?LINUXICON)
gtk_window_set_icon_(WindowID(0), ImageID(icon))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
Thanks
luis, it worked like a charm!
Now I just have to grok the use of CatchImage in this context.
This make my little game project almost finished.
All I really need now is for my kids to finish their artwork and for me to register with a file hosting outfit.
And of course to flesh out the comments while the stuff is still fresh in my mind.
luis you in particular, and this forum in general, are a much needed and appreciated PB programmer's resource.
-------------------
The first snippet above will only work on my 32 bit system if I name a local file "S".
It almost seems as if only the first character of the filename is used by the x86 version of this function.
Re: Setting the program's icon in Linux
Posted: Sun Jun 08, 2014 8:03 pm
by luis
heartbone wrote:
The first snippet above will only work on my 32 bit system if I name a local file "S".
Try that one with ascii and see if it works, you are using unicode right ?
Maybe there is something wrong the way pb imports it ?
Try this
Code: Select all
ImportC ""
gtk_window_set_icon_from_file_(*win, file.p-utf8, *err) As "gtk_window_set_icon_from_file"
EndImport
Procedure SETLINUXICON(iWindow)
Protected iError
gtk_window_set_icon_from_file_(WindowID(iWindow), "s48.png", @iError)
EndProcedure
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SETLINUXICON(0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
This should override PB import and force the conversion of the string to UTF8, so it will work in ascii and unicode
See pseudotypes in the help for more info (a very nice feature).
Re: Setting the program's icon in Linux
Posted: Sun Jun 08, 2014 8:18 pm
by heartbone
luis wrote:Try that one with ascii and see if it works, you are using unicode right ?
I'm running the defaults there and I see two seperate seemingly independently places in the IDE to toggle the compile.
FILE > Preferences > Compiler > Defaults > Create unicode executable [checkbox]
and
Compiler > Compiler Options... > Create unicode executable [checkbox]
Maybe there is something wrong the way pb imports it ?
Try this
Code: Select all
ImportC ""
gtk_window_set_icon_from_file_(*win, file.p-utf8, *err) As "gtk_window_set_icon_from_file"
EndImport
Procedure SETLINUXICON(iWindow)
Protected iError
gtk_window_set_icon_from_file_(WindowID(iWindow), "s48.png", @iError)
EndProcedure
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SETLINUXICON(0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
This should override PB import and declare the string as UTF8, see pseudotypes in the help for more info.
I will look into it sir, but since your correction above allows the IncludeBinary to function the way that I wanted,
I'll delay that investigation until after I finish my immediate tasks.
I suppose that need to learn about the effects of unicode on programs.
Re: Setting the program's icon in Linux
Posted: Mon Aug 04, 2014 3:25 pm
by heartbone
luis wrote:heartbone wrote:
The first snippet above will only work on my 32 bit system if I name a local file "S".
Try that one with ascii and see if it works, you are using unicode right ?
Maybe there is something wrong the way pb imports it ?
Try this
Code: Select all
ImportC ""
gtk_window_set_icon_from_file_(*win, file.p-utf8, *err) As "gtk_window_set_icon_from_file"
EndImport
Procedure SETLINUXICON(iWindow)
Protected iError
gtk_window_set_icon_from_file_(WindowID(iWindow), "s48.png", @iError)
EndProcedure
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SETLINUXICON(0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
This should override PB import and force the conversion of the string to UTF8, so it will work in ascii and unicode
See pseudotypes in the help for more info (a very nice feature).
I apologize for being negligent in getting back to testing this.
I tested it in
UBUNTU x86 and the unicode executable compiles and runs without error.
However it doesn't set the icon when using either single (S.png) or multi character (S48.png) filenames, the grey question mark remains.
Because the following solution seems to work well, implementing this is not an issue.
Code: Select all
;;; SETUP LINUX ICON CREATION
UsePNGImageDecoder() ;;;
ImportC "" ;;;
gtk_window_set_icon(a.l,b.l,c.l) ;;;
EndImport ;;;
InitSprite()
OpenWindow(0,0,0,800,600,"SOFTWARE",#PB_Window_ScreenCentered)
ICON= CatchImage(#PB_Any,?LINUXICON) ;;;
gtk_window_set_icon_(WindowID(0),ImageID(ICON)) ;;;
Delay(5000)
End
LINUXICON: : IncludeBinary "linux.png" ;;;
Re: Setting the program's icon in Linux
Posted: Mon Nov 03, 2014 1:10 pm
by Vera
Hello,
after extensive searching and testing many codes, I finally was lucky to have found a way to get a program icon showing for good.
With all examples from here or from
[Linux] Window icon creator I always experience the same issue:
- the tray-icon always shows
- the title-icon only shows once in a while in the actual IDE-compilation, but never if I'd run the pb-compilation.out nor ever in any compiled executable.
If anybody has an idea why that is, please let me know.
[Maybe it's Suse 11.1 missing a lib or alike. But even years ago nothing had worked out.]
Now, in case someone else has similar issues, I'd like to share my solution using "
gtk_window_set_default_icon_from_file". And benefitting from the codes around me, I managed to make it work for both ASCII and Unicode compilations.
Code: Select all
; **********
; Linux - Program Icon via "...default_icon_from_file"
; ASCII and Unicode compilation
; **********
EnableExplicit
Enumeration
#MainWin = 0
#Tex1 = 0
EndEnumeration
Global *err, compil, IconPath.s
; Adjust your icon path here ...
IconPath.s = #PB_Compiler_Home + "examples/sources/Data/world.png"
CompilerIf #PB_Compiler_Unicode
compil = 1
ImportC ""
gtk_window_set_default_icon_from_file(file.p-utf8, *err) As "gtk_window_set_default_icon_from_file"
EndImport
; gtk_window_set_default_icon_from_file("/home/path/to.png", @*err)
gtk_window_set_default_icon_from_file(IconPath, @*err)
CompilerElse
; gtk_window_set_default_icon_from_file_(@"/home/path/to.png", @*err)
gtk_window_set_default_icon_from_file_(@IconPath, @*err)
CompilerEndIf
If OpenWindow(#MainWin, 300, 200, 200, 100, "show prog icon", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TextGadget(#Tex1, 50, 30, 140, 25, "")
If compil = 1 : SetGadgetText(#Tex1, "unicode")
Else :: SetGadgetText(#Tex1, "ASCII") : EndIf
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
thanks

nd greetings ~ Vera
Re: Setting the program's icon in Linux
Posted: Fri Nov 07, 2014 10:36 pm
by marcoagpinto
Why can't Freddy code an automatic icon adding in Linux such as the one for Windows?
Re: Setting the program's icon in Linux
Posted: Sat Nov 08, 2014 12:17 am
by Vera
marcoagpinto wrote:Why can't Freddy code an automatic icon adding in Linux such as the one for Windows?
I think that's because there couldn't be a single solution for all Unix based OSs but countless for all the distributions.
please see freak's comment
Still it would be nice if the PB-help gave some hints here and there where to look to find such specific solutions.
Luckily a lot is shared on the forums

Re: Setting the program's icon in Linux
Posted: Tue Jan 08, 2019 10:40 am
by collectordave
I think this is the same problem. I wanted to create a desktop icon on linux to launch my programme.
Eventually wrote this only of use in LINUX
Code: Select all
CreateFile(0,GetUserDirectory(#PB_Directory_Desktop) + "World Stamp Collector.desktop")
WriteString(0,"[Desktop Entry]")
WriteString(0,"Type=Application")
WriteString(0,"Name=World Stamp Collector")
WriteString(0,"Terminal=false")
WriteString(0,"Exec=" + #DQUOTE$ + "/home/davemach/Programmes/World Stamp Collector/World Stamp Collector" + #DQUOTE$)
WriteString(0,"Icon=/home/davemach/Programmes/World Stamp Collector/Collection.png")
CloseFile(0)
This creates a file in the users desktop with it's own desktop icon and when clicked lauches that application.
I enclose this in if compiler is linux then ask user create a desktop icon?
Change the E$XEC line for your own application and the Icon line to point at your own png file for the icon.
Just need to crack setting permissions on file to Run as application now without user intervention.
Any ideas.
Hope this helps someone.
CD
Re: Setting the program's icon in Linux
Posted: Tue Jan 08, 2019 2:03 pm
by collectordave
I was just looking at the first post and thought of this.
Code: Select all
UsePNGImageDecoder()
ImportC ""
gtk_window_set_icon_from_file(a.l, b.l, c.l)
gtk_window_set_icon(a.l,b.l)
EndImport
DataSection
LINUXICON:
IncludeBinary "Collection.png"
EndDataSection
CatchImage(0,?LINUXICON)
Procedure SETLINUXICON(iWindow.i)
Protected.l lError
; gtk_window_set_icon_from_file_(WindowID(iWindow),ImageID(0),lError)
gtk_window_set_icon(WindowID(iWindow), ImageID(0));
EndProcedure
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
SETLINUXICON(0)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
icon in exe?
Re: Setting the program's icon in Linux
Posted: Tue Jan 08, 2019 4:34 pm
by deeproot
Here's what I do to embed the icon in the exe. Very simple if you want the same icon in every window of the program. Just a variation on the above post really.
Code: Select all
ImportC ""
gtk_window_set_default_icon(a.l)
EndImport
CatchImage(#Image_Icon,?ProgIcon)
gtk_window_set_default_icon(ImageID(#Image_Icon))
DataSection
ProgIcon: : IncludeBinary "some_icon.png"
EndDataSection
My understanding about creating desktop launcher icons is that there are different ways for different Desktop Environments (but I'm no expert

). The Linux edition of my application isn't generally released yet and I don't do anything for the launcher icon, but I just tell my beta testers to look up the appropriate method for their installation. On some systems it appears to be a simple symbolic link, dragged to the desktop. For others it's a file, similar to the CreateFile post above. I do supply the icon .png in case it's needed. On my own XFCE desktop I just use the "Create Launcher" menu which is pretty easy.
Re: Setting the program's icon in Linux
Posted: Thu Jan 17, 2019 1:03 pm
by collectordave
Just found one drawback
After exploring all the ways i could find to create a desktop shortcut ( they all worked sort of) I found my programme would run but would find no records in the database.
Traced this to the currentdirectory() when run from a desktop shortcut it is set to the desktop. I had one area where I relied on g
GetCurrentDirectory() and some where I was opening a local file in the programme which then did not work as it had the wrong directory.
Getting there with linux.
Regards
CD
Re: Setting the program's icon in Linux
Posted: Sat Aug 17, 2024 6:08 pm
by Borstensohn
heartbone wrote: Sun Jun 08, 2014 7:48 pm
luis wrote:Try this, I'm on Win so not tested.
Code: Select all
UsePNGImageDecoder()
DataSection
LINUXICON:
IncludeBinary "S48.png"
EndDataSection
OpenWindow(0,100,100,300,200,"SOFTWARE",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
icon = CatchImage(#PB_Any, ?LINUXICON)
gtk_window_set_icon_(WindowID(0), ImageID(icon))
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
End
Thanks
luis, it worked like a charm!
It works for me, too, thank you so much!
I was looking for a solution to add an icon to my programs for days and days …