Re: [gtk-list] Dialog Boxes - help



Mark Volpe wrote:
> 
> I'm writing a function that pops up a modal dialog with some
> text and buttons ( a la the Windows MessageBox function ). By itself
> it works fine, but what do I need to do to make sure it interfaces
> correctly with its parent window? I need to prevent the parent window
> from receiving delete events or else it wreaks
> havoc on my application. Here is what I have so far:

Here is a suggestion from what I do in such a situation.

In this example I have a toggle button ps_write_button in my main dialog, which
launches a sub-dialog, ps_main.

My code creatres all dialogs at one, assuring 

First the handler for the "clicked" event of ps_write_button:

static void ps_write_clicked (GtkWidget *ps_write_button,
			      gpointer user_data)
{
  GtkWidget *ps_main = GTK_WIDGET(user_data);

  gtk_widget_show(ps_main);
  // set window in a modal state
  gtk_window_set_modal (GTK_WINDOW(ps_main),TRUE);
}

Then the handler for the ok-button of the sub-dialog....

static void ps_ok_clicked (GtkWidget *ps_ok_button,
			   gpointer user_data)
{
  BsaPlot *plot = (BsaPlot *)user_data;

  // Ican hide the widget without taking care of the focus..
  gtk_widget_hide(gtk_widget_get_toplevel(ps_ok_button));

  // Do some action, actually this is a C++ program that uses the C-interface of
gtk+
  plot -> write_ps();
}

An alltime favourite, which handles all the destroy events....

static void quit (GtkWidget *widget)
{
  gtk_main_quit();
}
Then a sketch of the function that creates all my widgets (the main dialog as
well as the sub-dialog:


// launch a beatiful gui.
void start_gui()
{
 GtkWidget *window1;
 .....
 GtkWidget *ps_write_button;
 ....
 GtkWidget *ps_main;
 .....
 GtkWidget *ps_ok_button;
 GtkWidget *ps_cancel_button;
  
 ....

 // main dialog toplevel widget with a destroy handler.
 window1=gtk_window_new(GTK_WINDOW_TOPLEVEL);
 gtk_window_set_title(GTK_WINDOW(window1), "BsaView");

 gtk_signal_connect (GTK_OBJECT (window1), "destroy",
		     GTK_SIGNAL_FUNC (quit), NULL);

  
 ... All the stuff for the main dialog
 ... Among this stuff:

  // write postscript button
 ps_write_button = gtk_button_new_with_label ("Write PS");
 gtk_box_pack_start (GTK_BOX (vbox1), ps_write_button, FALSE, TRUE, 0);
 
 gtk_widget_show (ps_write_button);

 ... and now the sub-panel toplevel widget. 

 // A comlete dialog for the postscript writing...
 ps_main=gtk_window_new(GTK_WINDOW_TOPLEVEL);
 gtk_window_set_title(GTK_WINDOW(ps_main), "Write Postscipt");

 gtk_container_border_width(GTK_CONTAINER(ps_main),0);

 // pass the pointer to ps_main to the handler for ps_write, who launches
 // the postscript panel.
 gtk_signal_connect (GTK_OBJECT (ps_write_button), "clicked",
		     GTK_SIGNAL_FUNC (ps_write_clicked),
		     (gpointer)(ps_main));

 // if ps_main is destroyed, quit the application
 gtk_signal_connect (GTK_OBJECT (ps_main), "destroy",
 		     GTK_SIGNAL_FUNC (quit), NULL);


 // redirect delete to hide (Prevents Alt-F4 from destroying the widget
 // , so the widget is always hidden and never destoyed.)

 gtk_signal_connect (GTK_OBJECT (ps_main), "delete-event",
		     GTK_SIGNAL_FUNC (gtk_widget_hide_on_delete), NULL);


 ... some containers etc.

 // ok button
 ps_ok_button = gtk_button_new_with_label ("Ok");
 gtk_box_pack_start (GTK_BOX (ps_hbox), ps_ok_button, FALSE, TRUE, 0);
 
 gtk_signal_connect (GTK_OBJECT (ps_ok_button), "clicked",
		     GTK_SIGNAL_FUNC (ps_ok_clicked),
		     (gpointer)(&plot));
 gtk_widget_show (ps_ok_button);

 // cancel button
 ps_cancel_button = gtk_button_new_with_label ("Cancel");
 gtk_box_pack_start (GTK_BOX (ps_hbox), ps_cancel_button, FALSE, TRUE, 0);
 
 // cancel does nothing else but hiding the widget.
 gtk_signal_connect_object (GTK_OBJECT (ps_cancel_button), "clicked",
			    GTK_SIGNAL_FUNC (gtk_widget_hide),
			    GTK_OBJECT (ps_main));

 gtk_widget_show (ps_cancel_button);
 gtk_main();

}

Maybe this help you a bit. Also any suggestions are welcome on how to make this
code more elegant. (i.e. by using gtk_window_set_transient_for) 


  Wolfgang 

--
Mag. Wolfgang Glas
Institut fuer hydraulische Stroemungsmaschinen
Kopernikusgasse 24                              Phone:++43/316/873/7578
A-8010 Graz                                     Fax:  ++43/316/873/7577

mailto:Wolfgang.Glas@hfm.tu-graz.ac.at   
http://fhysmsg01.tu-graz.ac.at/Wolfgang.Glas/



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