Making Modal Dialogs in GTK (manual event handling)

Making Modal Dialogs in GTK (manual event handling)

Post by Eric Hegstro » Tue, 31 Aug 1999 04:00:00



Hi all. I am trying to make a modal dialog in GTK. I would like to have
a routine that returns when the accept button is clicked. What I am
tring to do is make a function that prompts the user for an integer and
returns the entered value when the "accept" is clicked.

I have a function:
       int PromptForInteger( char  *promptStr,
                             long   min,
                             long   max,
                             long   *enteredValue )

that brings up a dialog with a keypad and display label (like a cheap
calculator) and allows the user to enter a number between min and max.

I call gtk_grab_add so it is modal. Currently I do not process events in
PromptForIneger and control returns to the main loop. Is there an easy
way I can process the events in the PromptForInteger function and then
return when accept is clicked.

Basically I would like to be able to do something simple in my code
like:

    int value;

    if ( PromptForInteger("Enter a number:", 1, 100, &value) )
        DoSomethingCool( value );

Any help would be great. is there any documentation on processing events
with GLib would be a great help.

Addendum:
I am looking at:
http://www.gtk.org/rdp/glib/glib-the-main-event-loop.html#GMAINLOOP
right now and I think I may be able to find the answer there.

Also is there a GTK specific newsgroup of. If not is there any interest
in starting one?

Peace,
Eric

--
Eric Hegstrom                          .~.
Senior Software Engineer               /V\  
Sonoran Scanners, Inc.                // \\          L I N U X

520-617-0072 x402                     ^^-^^

 
 
 

Making Modal Dialogs in GTK (manual event handling)

Post by Olaf Klischa » Tue, 31 Aug 1999 04:00:00


Hello!


> Hi all. I am trying to make a modal dialog in GTK. I would like to have
> a routine that returns when the accept button is clicked. What I am
> tring to do is make a function that prompts the user for an integer and
> returns the entered value when the "accept" is clicked.

> I have a function:
>        int PromptForInteger( char  *promptStr,
>                              long   min,
>                              long   max,
>                              long   *enteredValue )

> that brings up a dialog with a keypad and display label (like a cheap
> calculator) and allows the user to enter a number between min and max.

> I call gtk_grab_add so it is modal. Currently I do not process events in
> PromptForIneger and control returns to the main loop. Is there an easy
> way I can process the events in the PromptForInteger function and then
> return when accept is clicked.

After you've used gtk_grab_add(), you should call gtk_main() to enter
a new message loop for the dialog:

gtk_grab_add (GTK_WIDGET(YourDialog)); gtk_main (); gtk_grab_remove
(GTK_WIDGET(YourDialog));

Then, inside the "clicked" handler of your Accept-Button, call
gtk_main_quit() to finish the event loop and return from the
gtk_main() function.

Quote:> [..]  Also is there a GTK specific newsgroup of.


the best place for such questions. To subscribe, write a message with

HTH, Olaf

 
 
 

Making Modal Dialogs in GTK (manual event handling)

Post by Ken Sodema » Wed, 01 Sep 1999 04:00:00


On Mon, 30 Aug 1999 10:42:50 -0700, Eric Hegstrom


>Hi all. I am trying to make a modal dialog in GTK. I would like to have
>a routine that returns when the accept button is clicked. What I am
>tring to do is make a function that prompts the user for an integer and
>returns the entered value when the "accept" is clicked.

>I have a function:
>       int PromptForInteger( char  *promptStr,
>                             long   min,
>                             long   max,
>                             long   *enteredValue )

>that brings up a dialog with a keypad and display label (like a cheap
>calculator) and allows the user to enter a number between min and max.

>I call gtk_grab_add so it is modal. Currently I do not process events in
>PromptForIneger and control returns to the main loop. Is there an easy
>way I can process the events in the PromptForInteger function and then
>return when accept is clicked.

You could do something similar to this:

gint execute_dlg (/*whatever is needed*/)
{
   GtkWidget  *dlg;

   /*
    * Call some function to create your dlg, or if it is simple,
    * just create it here.
    */
   dlg = create_my_dlg (/*whatever is needed*/);

   gtk_window_set_modal (GTK_WINDOW (dlg), TRUE);
   gtk_widget_show (dlg);

   gtk_main ();

   return return_value;

Quote:}

When your dlg is destroyed, return_value will be returned.  Of course,
return_value will need to be set by your signal handler for your 'accept'
button.  Also, it need no be a static value, but could be passed in to
the execute_dlg as a (gint *) and passed around to the signal handlers,
etc, etc, etc...

--
Ken Sodemann

NASCAR fan, Packer fan | Go #23, #24, #36 (BGN #27) | Go Pack!!
DE #3 get's a temporary GO for racing like he's got a pair...