Valhalla Legends Forums Archive | C/C++ Programming | Terminate GTK Program On Unload and Size Problems

AuthorMessageTime
rabbit
Right now I've got a fairly simple little GTK program with a window, TextView and ScrollWindow.  When I run my program from my terminal, it shows up in the proper size and all that, but when I run it from my context menu, it is very small and annoying.

My MAIN issue, however, is that when I click the X in the corner to kill it, the window disappears, but the program itself doesn't terminate.  I've tried a few methods, but I don't know how to kill it completely.  My code:
[code]#include <gtk/gtk.h>

GtkWidget *window;
GtkWidget *text;
GtkWidget *sw;

void printmsg(char* msg);

int main(int argc, char *argv[])
{
gtk_init (&argc, &argv);
   
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
text = gtk_text_view_new();

gtk_window_set_resizable((GtkWindow *) window, 1);

sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), GTK_SHADOW_IN);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

gtk_container_add(GTK_CONTAINER(window), sw);

gtk_container_add(GTK_CONTAINER(sw), text);

gtk_widget_show(sw);
gtk_widget_show(text);
gtk_widget_show(window);

gtk_window_resize((GtkWindow *) window, 650, 350);

gtk_widget_show(text);
gtk_widget_show(window);

printmsg("hello\n");
printmsg("world\n");

gtk_main();
   
return 0;
}

void printmsg(char* msg)
{
GtkTextBuffer* buffer;
GtkTextIter iter;

buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_buffer_place_cursor(buffer, &iter);

gtk_text_buffer_insert_at_cursor(buffer, msg, -1);
gtk_text_buffer_get_end_iter(buffer, &iter);
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(text), &iter, 0, TRUE, 0, 1);
}[/code]
May 20, 2006, 3:50 PM
Kp
You must call gtk_main_quit when the top level window is destroyed.  Otherwise, gtk_main will not return.
May 20, 2006, 5:48 PM
rabbit
I don't really know how to do that.  I figure it's not just putting it after gtk_main() (because that didn't work).
May 20, 2006, 7:23 PM
Kp
You must register for the "destroy" signal and react to it by calling gtk_main_quit.  Just add this somewhere before you call gtk_main:[code]

g_signal_connect (win, "destroy", G_CALLBACK (gtk_main_quit), NULL);[/code]
May 20, 2006, 7:35 PM
rabbit
Thanks.  You know what could be causing the size variations?
May 20, 2006, 8:32 PM

Search