简体   繁体   中英

gtkmm: Modal widgets within window

I have the following issue: I created a program which prompts the user for some input. This is done using a derived Gtk::Dialog that is used like follows:

if (modal_dialog->run() == SUCCESS){
  // do stuff depending on modal_dialog inputs here
}

Now, I would like to replace the dialog by an embedded widget that is shown within my main window, but acts just like my modal dialog. This means I would like to be able to call something like:

if (modal_widget->run == SUCSESS){
  // hide modal widget and do stuff
}

Is this easily possible in C++/gtkmm?

Note: I managed to get the desired behaviour (it feels the same for the end user) using state variables etc... This means, I manually show the wannabe-modal widget, and tell the confirmation button within the wannabe-modal widget to do the rest of the routine (body of the above if -statements). However, I'm using the same wannabe-modal widget for different actions, so that I have always to keep track of what I'm doing, which is not very elegant.

Instead of calling run() You can create a local loop object and make a nested loop. I don't know gtkmm very well, but the gtk+ code would be:

GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
g_main_loop_unref(loop);

Now, in the clicked event of your Ok/Cancel buttons simply call g_main_loop_quit(loop) and the nested loop will break.

Actually this is, more or less, what the gtk_dialog_run function does.

But beware! Your widget run() is blocking but not modal, and this is a dangerous beast: any other widget in the application will still be responsible to the user. It is up to you to avoid any undesired reentry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM