 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
flyjason Familiar Face
Joined: 04 Mar 2007 Posts: 19
|
Posted: Tue Mar 06, 2007 7:47 pm Post subject: How to display an image |
|
|
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: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13
| 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. |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 427 Location: Portland, OR USA
|
Posted: Tue Mar 06, 2007 9:03 pm Post subject: |
|
|
| Code: (Plaintext) | 1 2 3 4 5 6 7 8 9
| 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");
} | |
|
| Back to top |
|
 |
flyjason Familiar Face
Joined: 04 Mar 2007 Posts: 19
|
Posted: Tue Mar 06, 2007 11:01 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
flyjason Familiar Face
Joined: 04 Mar 2007 Posts: 19
|
Posted: Wed Mar 07, 2007 4:00 pm Post subject: |
|
|
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: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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 |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 427 Location: Portland, OR USA
|
Posted: Wed Mar 07, 2007 6:53 pm Post subject: |
|
|
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: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 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: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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) */
}
} | |
|
| Back to top |
|
 |
flyjason Familiar Face
Joined: 04 Mar 2007 Posts: 19
|
Posted: Wed Mar 07, 2007 8:10 pm Post subject: |
|
|
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: (Plaintext) | 1
| gint active; |
to...
| Code: (Plaintext) | 1
| 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: (Plaintext) | 1
| strcmp() //C function from <string.h> |
instead of
| Code: (Plaintext) | 1
| 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: |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 427 Location: Portland, OR USA
|
Posted: Wed Mar 07, 2007 8:55 pm Post subject: |
|
|
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. |
|
| Back to top |
|
 |
flyjason Familiar Face
Joined: 04 Mar 2007 Posts: 19
|
Posted: Wed Mar 07, 2007 10:34 pm Post subject: |
|
|
Oops I got troubled with g_ascii_strncasecmp()...didn't want to compile saying the following.
| Code: (Plaintext) | 1
| 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: (Plaintext) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 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: (Plaintext) | 1 2
| 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: (Plaintext) | 1 2
| 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: |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|