How can I assign X11 native (or SDL) window as a parent for a GTK dialog?
Asked Answered
D

1

6

I have a project using GTK+ 3 only for certain tasks, for example to provide file open dialog with gtk_file_chooser_dialog_new. I have to set parent to NULL as I have no "main" GTK window. However it's quite annoying that stdout of my program is flooded with message like this (especially because I use stdin with libreadline to have command line interface as well):

Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.

How can I avoid this? Or better (probably also more correct method), not just avoid: can I assign somehow an X11 window as parent for a GTK widget, without too much work? Actually, it's an SDL project, but I don't want to "embed" SDL into a GTK window for just this. However I can say the X11 Window id of my SDL window with SDL_GetWindowWMInfo(). As far as I see this would be better than just "avoiding" the message from GTK (but I even don't know how to do that), because -for example - window manager knows about the relation of dialog and main (SDL/X11) window, etc ...

That's only a side note (and should be another question indeed) that it seems, various GUI events (mouse clicks, key presses etc) done during the GTK file selection may be "sensed" by SDL then (after dialog box is closed, etc) which is not wanted of course. I guess I should try to empty SDL event queue from the unwanted events ...

Doughboy answered 17/4, 2016 at 1:0 Comment(0)
S
0

Not an entirely elegant solution, but I guess you could make a (empty) main window, and remove the 'decorations', making basically invisible on the screen.

You don't specify which language you are using, which makes it difficult to help.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
#  test_invisible_main.py
#
#  Copyright 2022 John Coppens <[email protected]>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#


import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GooCanvas', '2.0')
from gi.repository import Gtk, GooCanvas

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())
        self.set_decorated(False)
        self.set_size_request(1, 1)

        dlg = Gtk.FileChooserDialog(parent = self,
                    action = Gtk.FileChooserAction.OPEN)
        if len(args) == 2:
            dlg.set_current_folder(arg[1])
        dlg.add_buttons('Cancel', Gtk.ResponseType.CANCEL,
                        'Ok', Gtk.ResponseType.ACCEPT)
        if dlg.run() == Gtk.ResponseType.ACCEPT:
            with open('filename', 'w') as outfile:
                outfile.write(dlg.get_filename())
        dlg.destroy()

        self.show_all()
        exit(1)
        Gtk.main_quit()

    def run(self):
        Gtk.main()


def main(args):
    mainwdw = MainWindow()
    mainwdw.run()

    return 0

if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv))

This program creates a main window of size 1 x 1 - basically invisible. It starts a file chooser, waits for the selection, and writes the filename to file . On starting the program, a single argument can set the initial folder of the FileChooser.

If you want, the exit(1) can be set to, say, 0/1 to communicate if Ok or Cancel was pressed.

Scutellation answered 14/5, 2022 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.