Reuse modal dialog with different parent in GTK+ on MS Windows - broken focus

I'm reusing a modal dialog in GTK+ — I set it's parent dialog using gtk_window_set_transient_for, run it, and later reset it's parent to another dialog and run it again.

On Linux everything is fine but on MS Windows the second time this dialog is displayed all GTK windows get confused which one is active.

This is a test program:

#include <gtk/gtk.h>

GtkWidget* main_window;

void run_reused_dialog(
    GtkWidget* reused_dialog,
    const gchar *parent_title,
    const gchar *reused_dialog_title
) {
    GtkWidget* parent_dialog = gtk_dialog_new_with_buttons(
        parent_title,
        GTK_WINDOW(main_window),
        GTK_DIALOG_MODAL,
        GTK_STOCK_OK,
        GTK_RESPONSE_ACCEPT,
        NULL
    );
    gtk_container_add(
        GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(parent_dialog))),
        gtk_label_new(parent_title)
    );
    gtk_widget_show_all(parent_dialog);
    gtk_dialog_run(GTK_DIALOG(parent_dialog));

    gtk_window_set_transient_for(GTK_WINDOW(reused_dialog),GTK_WINDOW(parent_dialog));
    gtk_window_set_title(GTK_WINDOW(reused_dialog), reused_dialog_title);
    GtkWidget* reused_dialog_label = gtk_label_new(GTK_WINDOW(reused_dialog)->title);
    gtk_container_add(
        GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(reused_dialog))),
        reused_dialog_label
    );
    gtk_widget_show_all(reused_dialog);
    gtk_dialog_run(GTK_DIALOG(reused_dialog));
    gtk_widget_hide(reused_dialog);
    gtk_container_remove(
        GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(reused_dialog))),
        reused_dialog_label
    );
    gtk_widget_hide(parent_dialog);
    gtk_widget_destroy(parent_dialog);
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);
    main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(main_window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(main_window);

    /* This is a dialog which will be reused */
    GtkWidget* reused_dialog = gtk_dialog_new_with_buttons(
        "Reused dialog",
        GTK_WINDOW(main_window),
        GTK_DIALOG_MODAL,
        GTK_STOCK_OK,
        GTK_RESPONSE_ACCEPT,
        NULL
    );
    gtk_container_add(
        GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(reused_dialog))),
        gtk_label_new(GTK_WINDOW(reused_dialog)->title)
    );

    /* Show first dialog which will display reused dialog */
    run_reused_dialog(
        reused_dialog,
        "First dialog. Click OK to display reused dialog",
        "Reused dialog. Click OK to close"
    );

    /* Show second dialog which will display reused dialog */
    run_reused_dialog(
        reused_dialog,
        "Second dialog. Click OK to display reused dialog",
        "Reused dialog. Switch to another application and switch back"
    );

    gtk_main();
    return 0;
}

When "Reused dialog. Switch to another application and switch back" dialog is displayed, it ignores mouse, but reacts to keyboard input. Sometimes switching to another application and back triggers this problem.


Questions:

  1. Am I doing something wrong or is it a bug in Windows GTK port?

  2. How to work around it without recreating this dialog every time it is used?

  3. Am I allowed to reuse GTK dialogs at all?


The sample program source and compiled executable with required GTK libraries is here. This is cross-compiled using i686-pc-mingw32-gcc gtk-reused-dialog.c $(mingw32-pkg-config --cflags --libs gtk+-2.0) -mwindows on Fedora Linux 15 with mingw32-gtk2 package and it dependencies installed.

Answers


This is my guess:

  1. the problem is related to focus grabbing;
  2. the window widget calls grab APIs only when the modal flag changes its state;
  3. differences between platforms are mostly due to implementation details;
  4. you cannot have more than a single modal window.

At the time you show reused_dialog, it is not clear which dialog is modal (parent_window is still alive).

The right solution: I suspect if you refactor your code to show reused_dialog from a callback (using the response signal, for example), you'll get the desired result because this is the usual way™.

If you don't want to refactor you could try one of these solutions/work-arounds (roughly in order of what I think is better):

  • always disactivate the modal state of reused_dialog before running it; gtk_dialog_run() will set it again, triggering what requested:

    ...
    gtk_window_set_modal(GTK_WINDOW(reused_dialog), FALSE);
    gtk_dialog_run(GTK_DIALOG(reused_dialog));
    ...
    
  • manually grab the focus in reused_dialog before running it:

    ...
    gtk_grab_add(reused_dialog);
    gtk_dialog_run(GTK_DIALOG(reused_dialog));
    ...
    
  • deactivate the modal status of the parent_window after run:

    ...
    gtk_dialog_run(GTK_DIALOG(parent_dialog));
    gtk_window_set_modal(GTK_WINDOW(parent_dialog), FALSE);
    ...
    

Need Your Help

For a tag database is it better to store filenames per tag or tags per filename?

c# tags protobuf-net

I want to write a small app that manages file tags for my personal files. It's gonna be pretty straightforward but I am not sure if I should be storing filenames for each unique tag, i.e.: