GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Oct 21, 2014 6:25 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: anything better the absolute path for image from file?
PostPosted: Wed Sep 05, 2012 4:53 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Dear friends,
in my GTK(3) app, I am using icons as
Code:
gtk_window_set_icon(GTK_WINDOW(window),
create_pixbuf("images/icon.svg"));

or
Code:
GtkWidget *ast4im = gtk_image_new_from_file ("images/mkb3.png");


The problem is, when I am running the app away from the source
directory, those images are broken, obviously due to the fact that the
path is missing. One solution is to put absolute path, but then the code
will not be portable.

Is there any better way of doing it?


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Wed Sep 05, 2012 5:43 am 
Offline
Familiar Face

Joined: Wed Sep 05, 2012 2:02 am
Posts: 7
On windows/ms-dos O.S. the path start with "\" but on linux start with "/". So to avoid this, I do not put path in my programs to make reference to a file and I avoid these symbols (inside path context) in a favor to portability.
The bad thing about this is that the file need be on the same path of executable. If the file are in other path, so I create a code to deal with this.
Including the files inside executable you can avoid this. The bad thing is if you like to change only one icon, so, recompile again is necessary.

Not sure if these words give you some light, the intention is to help.


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Wed Sep 05, 2012 6:12 am 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
mineiro wrote:
Including the files inside executable you can avoid this.

Can you kindly show me how I can do this?
I tried:

Code:
objcopy --input binary images/icon.png icon.o

and then make:
Code:
$(exe):$(obj)
   $(cc) $(obj) icon.o -o $(exe) `$(pconf1)`

which gives error:
Code:
$ make
gcc assist.o help.o mkbib.o update_file.o caution.o icon.o -o mkbib `pkg-config --cflags --libs gtk+-3.0`
icon.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
make: *** [mkbib] Error 1


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Wed Sep 05, 2012 8:25 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hi,

objcopy is not what you are looking for as it is for copying object files and doing transformations on them and not for graphic files.

You are better off saving your graphic files in XPM format which is C compilable or using the utility application gdk-pixbuf-csource then converting these in your application using gdk_pixbuf_new_from_xpm_data () or gdk_pixbuf_new_from_inline ().
Code:
gdk-pixbuf-csource --build-list img1 images/img1.png img2 images/img2.png img3 images/img3.png > my_images.c
Code:
{
...
    pixbuf = gdk_pixbuf_new_from_inline (-1, img1, FALSE, NULL);
...
}


Note if your images are fairly large then this can become a bit of a memory hog.

The other option is to use absolute paths for your images. These should be set to where the images will be installed or configurable in some way. As noted by a previous poster avoid using the path separator '/' or '\' and use the Glib functions to build paths for you or provide two versions of the path. One for Unix based systems and one for Windows based systems.

_________________
E.


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Wed Sep 05, 2012 11:12 am 
Offline
Familiar Face

Joined: Wed Sep 05, 2012 2:02 am
Posts: 7
Like Sr errol have said;
I have used command below because I'm coding in other language.
Code:
gdk-pixbuf-pixdata mypicture.png myoutput

After, I have made a hexdump in myoutput and after changed it a bit.
Code:
cat myoutput | hexdump -v -e '"," 1/1 "%02X" " "' > veryclose

The generated file veryclose can be now inserted in my source code like a raw hex string.
Code:
mypixd db 47h ,64h ,6Bh ,50h ....


And the pseudo-code is like this, pixbuf and aloalo are in this example, global variables, and "offset" means a pointer to that data:
Code:
   gdk_pixbuf_new_from_inline,-1,offset mypixd,0,0
   mov pixbuf,rax
   gtk_image_new_from_pixbuf,pixbuf
   mov aloalo,rax
   gtk_container_add,window,aloalo


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Wed Sep 05, 2012 12:03 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Looking at the original posters code it looks like he is using C. Using pseudo assembly code is very hard to read and understand by the majority of people so it would be best to stay with using C when giving examples. Also C tends to be much more portable than assembly.

_________________
E.


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Fri Sep 07, 2012 12:33 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
file locations should never be embedded directly in code. You are right that it is not portable. It is also bad practice. At some point later you (or someone else) may rearrange where you are storing your files and things will break. The structure of your source package and the programming should be kept separate so that someone who tries to debug one doesn't require a full working knowledge of the other.
File locations should be handled by macros in the makefile. It will resolve OS dependent paths and provide checks the file exists where it should be at compile time (and provide fallback options if it doesn't). If you haven't used the autotools before then now is a good time to learn as this issue is one that they are designed to resolve and they do it very well.


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Sun Sep 30, 2012 9:50 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
Paul Childs wrote:
File locations should be handled by macros in the makefile.

Can you kindly be more elaborate? About the specific Macro I need to use?


Top
 Profile  
 
 Post subject: Re: anything better the absolute path for image from file?
PostPosted: Mon Oct 01, 2012 12:33 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
e.g. if using the autotools in configure.ac you'd put something like:
Code:
...
AC_CONFIG_HEADERS([config.h])
case "$host_os" in
   mingw*)
      AC_DEFINE([DLMT],["\r\n"],[End Of Line delimiter])
      ;;
   *)
      AC_DEFINE([DLMT],["\n"],[End Of Line delimiter])
      ;;
esac
...

which defines DLMT in config.h which if linked in can then allow you to use the DLMT macro throughout your program. In this eample I've used the different end of line characters for windows & unix likes, but you can define a variety of different macros this way. Tricky thing with absolute paths is that your program can install in different places and this needs to be taken into account (e.g. in linux it can often be either /usr/share/pixmaps or /usr/local/share/pixmaps).
In this case you'd use the autotools to define a macro to the installation directory and then in your program put it together in a portable way with glib functions. E.g. in one program I have:
Code:
   fout=g_build_filename(g_get_user_config_dir(), PACKAGE, "session.conf", NULL);

which builds the path from a config.h maco and the particular filename (though it is a bit different as I've used another glib function within it).

However, for icons I've always used the gnome packagename.desktop file to identify icons for programs (I haven't tested this as to how it goes when ported to windows etc). It is set up in the Makefile.am with:
Code:
Applicationsdir=$(datadir)/applications
Applications_DATA=packagename.desktop
pixmapdir=$(datadir)/pixmaps
pixmap_DATA=pixmaps/icon.svg
EXTRA_DIST=$(pixmap_DATA) $(Applications_DATA)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group