IT.
IT.

Reputation: 321

How to position an independent Window in an exact location on the screen?

I'm actually working on a relatively complex GTK+2 application. My application obviously have a main window; then I need to open a new "independent" Window.

I need to place the "flying" window in a *precise position* of the screen, exactly at the vertices of a widget (a DrawingArea). I need to place the new window in a precise position of the screen, exactly at the vertices of a widget (a gtk.DrawingArea).

So I thought the following algorithm:

  1. I get vertex coordinates of the DrawingArea(relative to parent window);

  2. Then, I convert relative coordinates to get absolute coordinates on-screen;

  3. Finally, I can simply move my window to desired position on screen, that is, on the vertex of gtk.DrawingArea. Is it right?

Unfortunately, I can not translate this algorithm into code. p.s. I'm working with Python 2.7 and Gtk+2.24; despite to this, C/C++ examples are welcome too.

Upvotes: 3

Views: 1679

Answers (1)

LiuLang
LiuLang

Reputation: 773

I'm not sure if this is what you want, has a try:

#!/usr/bin/env python3


from gi.repository import Gtk


class Demo:

    def __init__(self):
        self.window = Gtk.Window()
        self.window.set_title('Demo')
        self.window.set_default_size(300, 300)
        self.window.set_border_width(5)
        self.window.connect('delete-event', self.on_app_exit)

        hbox = Gtk.Box()
        hbox.set_halign(Gtk.Align.CENTER)
        hbox.set_valign(Gtk.Align.CENTER)
        self.window.add(hbox)

        da = Gtk.DrawingArea()
        da.set_size_request(100, 100)
        hbox.pack_start(da, False, False, 5)

        button = Gtk.Button('Show')
        button.connect('clicked', self.on_button_clicked, da)
        hbox.pack_start(button, False, False, 5)

        self.second_win = Gtk.Window()
        self.second_win.set_title('flying window')
        # You may also want to remove window decoration.
        #self.second_win.set_decorated(False)

        label = Gtk.Label('second window')
        self.second_win.add(label)


    def run(self):
        self.window.show_all()
        Gtk.main()

    def on_app_exit(self, widget, event=None):
        Gtk.main_quit()

    def on_button_clicked(self, button, da):
        allocation = da.get_allocation()
        self.second_win.set_default_size(allocation.width, 
                allocation.height)
        pos = self.window.get_position()
        self.second_win.move(pos[0] + allocation.x, pos[1] + allocation.y)
        self.second_win.show_all()


if __name__ == '__main__':
    demo = Demo()
    demo.run()

And a screenshot:

Screenshot

Upvotes: 1

Related Questions