Masy
Masy

Reputation: 181

How do you create a gtk window handle from an X11 window handle

I'm trying to open a gtk file dialog window with a GLFW window. Now since GLFW is a pretty low level API it only exposes the X11 window and display, because it just creates a window without any GUI stuff.

The problem I'm having is that gtk_file_chooser_dialog_new() expects a parent window to be passed on, but since I only have an X11 handle I'm not quite sure how to create a GTK handle from it.

I followed this tutorial which resulted in the following code:

    glfwSetKeyCallback(windowHandle1, [](GLFWwindow *window, int keyCode, int scanCode, int action, int mods) {
        if (action == GLFW_PRESS)
        {
            if (keyCode == GLFW_KEY_O && mods == (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL))
            {
                GtkWidget *dialog;
                GtkFileChooserAction fileAction = GTK_FILE_CHOOSER_ACTION_OPEN;
                gint res;

//              Window x11Window = glfwGetX11Window(window);
//              Display *x11Display = glfwGetX11Display();

                int argc = 0;
                gtk_init(&argc, nullptr);  // TODO: don't do this every time

                dialog = gtk_file_chooser_dialog_new("Open File",
                                                     nullptr, // should be _GtkWindow of the GLFWwindow
                                                     fileAction,
                                                     _("_Cancel"),
                                                     GTK_RESPONSE_CANCEL,
                                                     _("_Open"),
                                                     GTK_RESPONSE_ACCEPT,
                                                     nullptr);

                res = gtk_dialog_run(GTK_DIALOG(dialog));
                if (res == GTK_RESPONSE_ACCEPT)
                {
                    char *filename;
                    GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
                    filename = gtk_file_chooser_get_filename(chooser);
                    std::cout << filename << std::endl;
                    g_free(filename);
                }

                gtk_widget_destroy(dialog);
                std::cout << "destroyed file dialog" << std::endl;
            }
        }
    });

This opens an open file dialog, but because I didn't specify a parent window the main window can still be focused, and another problem is that the dialog doesn't close for some reason even though I call gtk_widget_destroy(dialog).

I already took a look at this post, but the only answer seems to be getting the xid of the file dialog window, which is not what I want to do.

This google search result doesn't seem to help either, as it creates a completely new gdk (not gtk) window on the default display.

Upvotes: 2

Views: 776

Answers (1)

milo1000
milo1000

Reputation: 1

I've got the same problem and found hackish way to fix this. Null parent is not really problem here, but lack of event dispatching, so I've added:

gtk_widget_destroy(dialog);
while (g_main_context_iteration(nullptr, false));

Upvotes: 0

Related Questions