GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Nov 24, 2014 10:45 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: How to display an image
PostPosted: Tue Mar 06, 2007 7:47 pm 
Offline
Familiar Face

Joined: Sun Mar 04, 2007 8:25 pm
Posts: 19
Hi all,

I'm trying to display an image (name: baby.jpg) in a GtkImage widget (name: img_objectdesign) after clicking on a button (name: bt_testimage).

I tried the code which you see below, but nothing happens when clicking on the button :(. I'll be happy if anybody could sort out the problem.

Code:
void   //button to test image
on_bt_testimage_clicked                (GtkButton       *button,
                                        gpointer         user_data)
{
   GtkWidget *img_model;

   GtkWidget *myimage = lookup_widget(GTK_WIDGET(button), "img_objectdesign");

   img_model = gtk_image_new_from_file ("baby.jpg");

   gtk_container_add (GTK_CONTAINER (myimage), img_model);
   gtk_widget_show(img_model);
}

Thanks for helping me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 06, 2007 9:03 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Code:
void   //button to test image
on_bt_testimage_clicked                (GtkButton       *button,
                                        gpointer         user_data)
{
   GtkWidget *myimage = lookup_widget(GTK_WIDGET(button), "img_objectdesign");

   gtk_image_set_from_file (GTK_IMAGE(my_image), "baby.jpg");

}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 06, 2007 11:01 pm 
Offline
Familiar Face

Joined: Sun Mar 04, 2007 8:25 pm
Posts: 19
Ooops never thought it was so easy :o :roll:

Big big thanks :wink:

I've spent so much time on this, making many trial & error...you know I'm new to GTK+ programming.

:arrow: Again many thanks :D


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 4:00 pm 
Offline
Familiar Face

Joined: Sun Mar 04, 2007 8:25 pm
Posts: 19
Ok in a previous post I mentioned that in fact I was trying to display an image after choosing from a combo box which image to display.

To make it clearer, the combo box (name: cbo_pickobject) will contain elements such as Table, Chair, Bed, etc. Assume that I have pictures named Table.jpg, Chair.jpg and Bed.jpg correspondingly.

What I want is after choosing let say Chair from the combo box (which is the 2nd element in the combo box), the image Chair.jpg is displayed automatically in the GtkImage widget (name: img_patternpreview). Seemingly when I choose another element, the corresponding image is displayed.

Can you provide me the code to do that please. I figure out that there should be some if statements, etc but I'm confused with the syntax...GTK+ or C?? Are they mixed in this environment?? :? I mean do we actually use both languages (I qualify GTK+ to be a language in itself, don't know if I'm right?) ???

I was trying the following code:

Code:
void
on_cbo_pickobject_changed              (GtkComboBox     *combobox,
                                        gpointer         user_data)
{
/*
   gchar mystr[50];      
   gchar *pointer_str = mystr;   

   GtkWidget *my_cbopickobject = lookup_widget(GTK_WIDGET(combobox), "cbo_pickobject");

   //mystr = gtk_combo_box_get_active_text(combobox);
   strncpy(pointer_str, gtk_combo_box_get_active_text(GTK_ENTRY(my_cbopickobject)),50);


   /* if (pointer_str..................)  */
}


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 6:53 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
No, GTK+ is not a language. C is the language you are programming in, GTK+ is a toolkit or a bunch of libraries which provide you with functions, data types, enumerations, etc.

If you look at the manual for gtk_combo_box_get_active_text() you'll notice that it returns a "newly allocated string". This means that you don't have to use strncpy on it and that you'll have to free it when you're done.

Also, you dont' need to use the lookup function, the GtkComboBox is already passed to the callback function as combobox.

Code:
void
on_cbo_pickobject_changed              (GtkComboBox     *combobox,
                                        gpointer         user_data)
{
   gchar *mystr;
   
   mystr = gtk_combo_box_get_active_text (combobox);

   if (g_ascii_strncasecmp (mystr, "Chair") == 0)
   {
     /* show chair image */
   }
   else if (g_ascii_strncasecmp (mystr, "Table") == 0)
   {
     /* show table image */
   }
   else
   {
     /* show default image */
   }
   
   /* we have to free the memory used from the return from the
      call to gtk_combo_box_get_active_text() */
     
   g_free (mystr);
}


You may alternately want to use the index of the selected items rather than comparing strings. This assumes that you know the order in which they apprear in the combo box and that order isn't going to change:

Code:
void
on_cbo_pickobject_changed              (GtkComboBox     *combobox,
                                        gpointer         user_data)
{
   gint active;
   
   active = gtk_combo_box_get_active (combobox);

   if (active == 0)
   {
     /* show chair image, assuming chair is the first item in the combo */
   }
   else if (active == 1)
   {
     /* show table image, assuming table is 2nd item in the combo */
   }
   else
   {
     /* show default image, there is NO selected item (active = -1) */
   }
}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 8:10 pm 
Offline
Familiar Face

Joined: Sun Mar 04, 2007 8:25 pm
Posts: 19
Thank you so much.

Indeed gtk_combo_box_get_active_text() and gtk_combo_box_get_active() were the two functions I picked up when looking at the reference manual. But unfortunately I didn't knew how to use them properly. :(

Quote:
No, GTK+ is not a language. C is the language you are programming in, GTK+ is a toolkit or a bunch of libraries which provide you with functions, data types, enumerations, etc.


Can I understand that there is no problem mixing GTK+ and C coding in the same file?? To be more precise, let's take an example (from the code you just posted).

Can I change...

Code:
gint active;


to...

Code:
int active;    //int is a C data type


?? Would it have any effect on the overall code or would it work the same??


Similarly can I use

Code:
strcmp()    //C function from <string.h>


instead of

Code:
g_ascii_strncasecmp()    //gtk function??


??

I'm a bit confused :?

I'm mostly used to program in C++, so I may deal with C somehow. So if there isn't any harm, I would prefer to use some built-in C features where likely possible. So can you provide me with an answer please?
:wink:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 8:55 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
GTK+ provides wrappers for the C datatypes and many functions for the purpose of cross-platform compatibility. You may want to take a look at the GLib introduction in the API documentation.

So yes, you can use strcmp, however, you need to be mindful of character encodings which can vary from platform to platform, etc.

Declaring int and gint are exactly the same thing. A gint IS an int. Just like a gchar IS a char and so forth. You can use the interchangeably. However, using the GLib alternatives to functions and data types is recommended as it will allow you to easily port your application-- not to mention that GLib often provides better error checking. A better function to use in this case would have actually been g_str_equal().

Some of the GLib functions are simply wrappers to the corresponding POSIX C functions, and some are written to perform the same functionality but "safer" (they may check space required and allocate memory or perform the appropriate tasks for the operating system on which they are running).

All the functions beginning with g_ are from the GLib library from which GTK is built, just like all functions beginning with gtk_ are from the GTK+ library.

So, again, I would say get in the habbit of using the GLib data types and functions when possible. That will make your code more conventional as a GTK+ application as well as save you some problems should you want to compile your code on a platform other than the one in which you're developing it on.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 07, 2007 10:34 pm 
Offline
Familiar Face

Joined: Sun Mar 04, 2007 8:25 pm
Posts: 19
Oops I got troubled with g_ascii_strncasecmp()...didn't want to compile saying the following.

Code:
error C2198: 'g_ascii_strncasecmp' : too few actual parameters


I used strcmp() to fix this out...ok it is working now. :)

Ok as you know I'm building my application step by step as I'm new to GTK+. In fact, I added another GtkImage widget on the same window. The thing is that when I choose let say Chair from the combo box, the two GtkImage widgets should display two different images (for e.g. one displays the actual chair and the other displays the chair in wireframe - so 2 different images pertaining to Chair). In fact for each item (Chair, Table, Bed, etc) there will be 2 different images.

So I modified the code as below.

Code:
void
on_cbo_pickobject_changed              (GtkComboBox     *combobox,
                                        gpointer         user_data)
{
   gchar *mystr;
   
   mystr = gtk_combo_box_get_active_text (combobox);

   if (strcmp (mystr, "Chair") == 0)
   {
      GtkWidget *myimage_object = lookup_widget(GTK_WIDGET(combobox), "img_objectpreview");            
      gtk_image_set_from_file (GTK_IMAGE(myimage_object), "Pictures/Chair 1.jpg");


      GtkWidget *myimage_pattern = lookup_widget(GTK_WIDGET(combobox), "img_patternpreview");         
      gtk_image_set_from_file (GTK_IMAGE(myimage_pattern), "Pictures/Chair 1.gif");
   }
   else if (strcmp (mystr, "Table") == 0)
   {
     /* show table image */
   }
   else
   {
     /* show default image */
   }
   
   /* we have to free the memory used from the return from the
      call to gtk_combo_box_get_active_text() */
     
   g_free (mystr);
}



Unfortunately, I'm getting the following 2 errors.

Code:
error C2275: 'GtkWidget' : illegal use of this type as an expression
error C2065: 'myimage_pattern' : undeclared identifier


It seems that it is not accepting as from...

Code:
      GtkWidget *myimage_pattern = lookup_widget(GTK_WIDGET(combobox), "img_patternpreview");         
      gtk_image_set_from_file (GTK_IMAGE(myimage_pattern), "Pictures/Chair 1.gif");


Any idea? :idea:


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 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