3

I subclassed Gtk::Window and i want to move it to a position on the screen, but i cannot find any method to do this. Was Gtk::Window::move removed in Gtk4 ? If yes, how can i move my window ?
I currently have this in the constructor of my window class :

this->set_title(_("Screenshot"));
// this is for getting the size of the screen, and calculating the center position
Glib::RefPtr<Glib::ObjectBase> first_monitor = Gdk::Display::get_default()->get_monitors()->get_object(0);
Gdk::Rectangle rect;
first_monitor->get_rectangle(rect);
int posx = (rect.get_width() - this->get_width()) / 2;
this->move(posx, -1)
this->show();

and it gives me following errors when compiling :

src/prefswindow.cpp: In constructor ‘PrefsWindow::PrefsWindow()’:
src/prefswindow.cpp:7:17: error: ‘using element_type = class Glib::ObjectBase {aka class Glib::ObjectBase}’ has no member named ‘get_rectangle’
  first_monitor->get_rectangle(rect);
                 ^~~~~~~~~~~~~
src/prefswindow.cpp:9:8: error: ‘class PrefsWindow’ has no member named ‘move’; did you mean ‘Role’?
  this->move(posx, -1)
        ^~~~
        Role

where class PrefsWindow inherits from Gtk::Window.
Thanks in advance !

TheEagle
  • 5,808
  • 3
  • 11
  • 39

1 Answers1

6

Looks like GTK continues to devolve. See How to center GtkWindows in gtk4?, where the answer appears to be "no way, the gtk_window_set_position() API has been removed" (confirmed by a GTK core developer). And the comments in that thread seem to imply that GTK is going into the direction of removing everything related to window management and anything else related to screen, rather than the internal details of the application windows.

So, as I understand it now, you have the following options:

  • use some platform-specific way of moving your window
  • switch widget toolkit (to e.g. Qt, or an older version of GTK+)
  • abandon the idea of moving your window.
Ruslan
  • 18,162
  • 8
  • 67
  • 136
  • thats what i call improvement :( – TheEagle Dec 31 '20 at 17:13
  • but how can i get the first monitor from the list of Gdk::Monitor's returned by Gdk::Display::get_monitors ? Glib::ListModel::get_object returns a Glib::ObjectBase, not a Gdk::Monitor – TheEagle Dec 31 '20 at 17:16
  • @frederic `Gdk::Monitor` is supposed to inherit from `Glib::ObjectBase`. So try using `dynamic_cast`/`static_cast` to cast to the required type (the former to make sure you understand it right, the latter for production). – Ruslan Dec 31 '20 at 17:18
  • oh thanks, i will try this out ! But another question : if Gtk cannot handle screen coordinates because this is up to the window manager, how comes it that my Gtk::Window gets ALWAYS positioned in the center when using Gtk3 and Window::set_position(Gtk::WIN_POSITION_CENTER) ? – TheEagle Dec 31 '20 at 17:23
  • 1
    @frederic because GTK devs seem to have become obsessed with removing lots of APIs and imposing their world view on the users (both end users and programmers). They've been removing API for quite some years, the first for me was the [removal of theming engine API from GTK3](https://bugzilla.gnome.org/show_bug.cgi?id=735211) in 2014 (which broke a style I had invested quite some time in). That doesn't mean that what you want to do is impossible, they just want to be agnostic, and thus don't provide the means to achieve it. – Ruslan Dec 31 '20 at 17:31
  • Yeah, i only switched to from Gtk3 to Gtk4 because of the new media capabilities and Gsk. I think it just sucks when the window opens up somewhere else every time you open it. – TheEagle Dec 31 '20 at 17:36
  • @frederic I suppose (although I've used neither), that GSK counterpart outside of GTK is Qt's _Qt Quick_. So you might want to look at Qt if your project is not too tied to GTK. – Ruslan Dec 31 '20 at 17:38
  • No, I do not want to use qt. i considered that at first, but Qt is not suitable for my project. But thanks all the same ! – TheEagle Dec 31 '20 at 17:44
  • i am getting following error when using `auto first_monitor = std::static_pointer_cast(Gdk::Display::get_default()->get_monitors()->get_object(0));` : `/usr/include/c++/7/bits/shared_ptr.h:483:23: error: cannot convert from pointer to base class ‘Glib::ObjectBase’ to pointer to derived class ‘Gdk::Monitor’ because the base is virtual` – TheEagle Dec 31 '20 at 17:51
  • ok with dynamic_pointer_cast it works ! Thanks so much for pointing me into the right direction ! – TheEagle Dec 31 '20 at 17:55