Modal dialog with two gtk_main() [Last try..]




Hello !

Sorry for asking again, but I did not get it right...

What's wrong with the attached program? It's a much simpler version
of my problem, but why does it not work?

The first press on one of the main buttons works. But after closing
the modal dialog, the press on the other main button does not
work any more...

Please help. What's wrong?

Thanks in advance
and best regards
 Sven
-- 
            .-------------------------------------------------------.
    .^.     | Sven Anders, Germany | anderss@fmi.uni-passau.de      |
    /V\     |----------------------| www.fmi.uni-passau.de/~anderss |
   // \\    | Sites I recommend:   `--------------------------------|
  /(   )\   | www.freshmeat.net www.enlightenment.org www.gnome.org |
   ^^-^^    `-------------------------------------------------------'
#include <gtk/gtk.h>

/* compile with:
 *   gcc `gtk-config --cflags --libs` gtkproblem.c -o gtkproblem
 */

void on_my_dialog_button_pressed(GtkButton *button, gpointer user_data)
{
  gtk_main_quit();
}

GtkWidget* create_my_dialog(gchar *ok_text, gchar *cancel_text)
{
  GtkWidget *dialog;
  GtkWidget *dialog_ok_button;
  GtkWidget *dialog_cancel_button;
  GtkWidget *hbuttonbox;

  dialog = gtk_window_new(GTK_WINDOW_DIALOG);
  gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
  hbuttonbox = gtk_hbutton_box_new();
  gtk_widget_show (hbuttonbox);
  gtk_container_add(GTK_CONTAINER(dialog), hbuttonbox);

  dialog_ok_button = gtk_button_new_with_label (ok_text);
  gtk_widget_show (dialog_ok_button);
  gtk_container_add (GTK_CONTAINER (hbuttonbox), dialog_ok_button);
  gtk_signal_connect (GTK_OBJECT (dialog_ok_button), "pressed",
		      GTK_SIGNAL_FUNC (on_my_dialog_button_pressed), NULL);

  dialog_cancel_button = gtk_button_new_with_label (cancel_text);
  gtk_widget_show (dialog_cancel_button);
  gtk_container_add (GTK_CONTAINER (hbuttonbox), dialog_cancel_button);
  gtk_signal_connect (GTK_OBJECT (dialog_cancel_button), "pressed",
		      GTK_SIGNAL_FUNC (on_my_dialog_button_pressed), NULL);

  gtk_widget_show(dialog);
  return dialog;
}

void show_my_dialog(gchar *ok_text, gchar *cancel_text)
{
  GtkWidget *dialog;

  dialog = create_my_dialog(ok_text, cancel_text);
  gtk_main();
  gtk_widget_destroy(dialog);
}

void callback_button1(GtkWidget *widget, gpointer user_data)
{
  show_my_dialog("Ok 1", "Cancel 1");
  printf("do something 1...\n");
}

void callback_button2(GtkWidget *widget, gpointer user_data)
{
  show_my_dialog("Ok 2", "Cancel 2");
  printf("do something 2...\n");
}


GtkWidget* create_my_window(void)
{
  GtkWidget *window;
  GtkWidget *hbuttonbox;
  GtkWidget *window_button1;
  GtkWidget *window_button2;

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_modal(GTK_WINDOW (window), TRUE);

  hbuttonbox = gtk_hbutton_box_new();
  gtk_widget_show (hbuttonbox);
  gtk_container_add(GTK_CONTAINER(window), hbuttonbox);

  window_button1 = gtk_button_new_with_label ("Button1");
  gtk_widget_show (window_button1);
  gtk_container_add (GTK_CONTAINER (hbuttonbox), window_button1);
  gtk_signal_connect (GTK_OBJECT (window_button1), "pressed",
		      GTK_SIGNAL_FUNC (callback_button1), NULL);

  window_button2 = gtk_button_new_with_label ("Button2");
  gtk_widget_show (window_button2);
  gtk_container_add (GTK_CONTAINER (hbuttonbox), window_button2);
  gtk_signal_connect (GTK_OBJECT (window_button2), "pressed",
		      GTK_SIGNAL_FUNC (callback_button2), NULL);

  gtk_widget_show(window);
  return window;
}


int main(int argc, char *argv[])
{
  GtkWidget *main_window;

  gtk_init(&argc, &argv);
  main_window = create_my_window();
  gtk_widget_show(main_window);
  gtk_main();
  gtk_exit(0);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]