Setting the program's icon in Linux

Just starting out? Need help? Post your questions and find answers here.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Setting the program's icon in Linux

Post 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?
Keep it BASIC.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Setting the program's icon in Linux

Post 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
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Setting the program's icon in Linux

Post 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.
Last edited by heartbone on Sun Jun 08, 2014 8:05 pm, edited 1 time in total.
Keep it BASIC.
User avatar
luis
Addict
Addict
Posts: 3876
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Setting the program's icon in Linux

Post 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).
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Setting the program's icon in Linux

Post 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.
Keep it BASIC.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Setting the program's icon in Linux

Post 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"   ;;;
Keep it BASIC.
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Setting the program's icon in Linux

Post 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. :D

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
User avatar
marcoagpinto
Addict
Addict
Posts: 940
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Setting the program's icon in Linux

Post by marcoagpinto »

Why can't Freddy code an automatic icon adding in Linux such as the one for Windows?
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Setting the program's icon in Linux

Post 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 :-)
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Setting the program's icon in Linux

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Setting the program's icon in Linux

Post 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?
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
deeproot
Enthusiast
Enthusiast
Posts: 269
Joined: Thu Dec 17, 2009 12:00 pm
Location: Llangadog, Wales, UK
Contact:

Re: Setting the program's icon in Linux

Post 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.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Setting the program's icon in Linux

Post 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
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply