Re: GTK app to change pic on click
- From: Tomas Carnecky <tom dbservice com>
- To: Midhun A <midhunagni gmail com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: GTK app to change pic on click
- Date: Tue, 18 Mar 2008 15:58:44 +0100
Midhun A wrote:
Hi All,
I am very new to GTK+ (started out yesterday). I am writing a simple
application which will show pictures one by one on button click.
Although I am able to get my first picture, the picture does not
change on click of the button (next or previous). The full code is as
below.
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include </usr/include/stdio.h>
#include <glib.h>
GtkWidget *image2;
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *prev_button, *next_button;
gchar *files[6] =
{
"/home/pics/1.jpg",
"/home/pics/2.jpg",
"/home/pics/3.jpg",
"/home/pics/4.jpg",
"/home/pics/5.jpg",
"/home/pics/6.jpg"
};
typedef struct{
gint data;
} data;
void prev_callback (GtkWidget *widget, data *v )
{
gint cnt = v->data;
if (cnt == 0)
cnt = 5;
else
cnt = cnt - 1;
image2 = gtk_image_new_from_file (files[cnt]);
}
This callback only creates the new image, it doesn't display it in the
window. You seem to lack basic C understanding, especially about pointers.
To replace the current image, you have to remove the image widget from
the vbox and replace it with the new image. Like this:
void next_callback (GtkWidget *widget, data *v)
{
int cnt = v->data;
if (cnt == 5)
cnt = 0;
else
cnt = cnt + 1;
gtk_container_remove(GTK_CONTAINER(vbox), image2);
image2 = gtk_image_new_from_file (files[cnt]);
gtk_container_add(GTK_CONTAINER(vbox), image2);
}
int main (int argc, char *argv[])
{
data *w = g_slice_new(data);
w->data = 0;
gtk_init(&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_maximize (window);
vbox = gtk_vbox_new(FALSE, 1);
image2 = gtk_image_new_from_file (files[w->data]);
gtk_container_add(GTK_CONTAINER(vbox), image2);
prev_button = gtk_button_new_with_mnemonic ("Previous");
next_button = gtk_button_new_with_mnemonic ("Next");
gtk_widget_set_name(prev_button,"prev_button");
gtk_widget_set_name(next_button,"next_button");
gtk_container_add(GTK_CONTAINER(vbox), prev_button);
gtk_container_add(GTK_CONTAINER(vbox), next_button);
gtk_container_add(GTK_CONTAINER(window), vbox);
g_signal_connect (G_OBJECT (prev_button), "clicked", G_CALLBACK
(prev_callback),(gpointer)w);
g_signal_connect (G_OBJECT (next_button), "clicked", G_CALLBACK
(next_callback),(gpointer)w);
gtk_widget_show (image2);
gtk_widget_show (prev_button);
gtk_widget_show (next_button);
gtk_widget_show (vbox);
gtk_widget_show (window);
gtk_main();
return 0;
}
This is very primitive code to test the functionality. Whats wrong
with the code? I would like to see the previous picture on click of
previous button and the next picture on click of next button.
tom
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]