Re: A problem about programming with Gtk+
- From: "Richard Boaz" <riboaz xs4all nl>
- To: gtk-list gnome org
- Cc: haitao_yang foxitsoftware com
- Subject: Re: A problem about programming with Gtk+
- Date: Sun, 28 Dec 2008 10:18:04 +0100 (CET)
Hi,
You're doing a couple of things wrong in the setup:
1) you are not using any gdk() routines in threads, therefore, you do not
need to call gtk_threads_init(). (call all gtk_ and gdk_ routines only
from routines executing as part of the main loop and strange behaviours
are minimized.)
2) you should call g_thread_init() before you call gtk_init().
3) calling g_thread_supported() only tells you whether or not you've
already called g_thread_init(), if you call g_thread_init() only once in
your main routine, calling g_thread_supported() is not necessary.
so rewrite the thread initialisation part of your main() as:
g_thread_init(NULL);
gtk_init (&argc, &argv);
and your dialog will work as it should.
other recommendations:
1) declare your busy cursor static, create it only once, and never destroy
it.
2) make all your widgets and call gtk_widget_show_all(window), i.e., do
not call gtk_widget_show() as you make each widget.
cheers,
richard
========================
Hello all:
Firstly, Thanks Richard for his valuable infomation and thanks all who
reply me.
I still follow the thread "A problem about programming with
Gtk+(gtk-list Digest, Vol 56, Issue 22)". Maybe I unintentionally
changed the subject in my code:)
I followed the step of Richard's guide, everything run well, but I can
not exit from the callback of g_idle_add correctly. Why?
Following is the code:
//gcc -o tbug t_bug3.c `pkg-config --cflags --libs gtk+-2.0`-lgthread-2.0 -g
//
#include <unistd.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <pthread.h>
#include <glib.h>
GtkWidget *win;
static GtkWidget *xpm_label_box( gchar *xpm_filename,
gchar *label_text )
{
GtkWidget *box;
GtkWidget *label;
GtkWidget *image;
box = gtk_hbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (box), 2);
image = gtk_image_new_from_file (xpm_filename);
label = gtk_label_new (label_text);
gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 3);
gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 3);
gtk_widget_show (image);
gtk_widget_show (label);
return box;
}
static gboolean Result (gpointer data)
{
gint param = (gint)data;
GtkWidget* pDialog = gtk_message_dialog_new(GTK_WINDOW(win),
GTK_DIALOG_MODAL,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"The thread result: %d ",
param);
gtk_dialog_run(GTK_DIALOG(pDialog));
gtk_widget_destroy(pDialog);
return FALSE;
}
static gpointer cursor_change(gpointer data)
{
//1) do your work
GtkWidget *window = (GtkWidget *) data;
win = window;
int i,j;
for(i=0;i<100;i++)
{
g_print ("Hello again - %s was pressed %d\n", (char *) data, i);
}
//2) call g_idle_add to render the result to user
g_idle_add( Result, (gpointer)i);
return NULL;
}
static void callback( GtkWidget *widget, gpointer data )
{
//1) modify the cursor as necessary
GtkWidget *window = (GtkWidget *) data;
GdkCursor *new_cursor;
new_cursor=gdk_cursor_new(GDK_WATCH);
g_print ("I make it!");
gdk_window_set_cursor(window->window, new_cursor);
gdk_cursor_destroy(new_cursor);
//2) create the thread to do the background work
g_thread_create ( cursor_change, data, TRUE, NULL);
//3) quit
return;
}
int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *box;
gtk_init (&argc, &argv);
if (!g_thread_supported ())
{
g_thread_init(NULL);
gdk_threads_init();
}
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), NULL);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new ();
g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback),
(gpointer) window);
box = xpm_label_box ("info.xpm", "cool button");
gtk_widget_show (box);
gtk_container_add (GTK_CONTAINER (button), box);
gtk_widget_show (button);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]