[devhelp] Don't use gdk_screen_get_width/height() (deprecated)
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp] Don't use gdk_screen_get_width/height() (deprecated)
- Date: Fri, 15 Dec 2017 09:38:30 +0000 (UTC)
commit 7d5a6a87c737de61799c75b7c288b0d4fe6521a2
Author: Sébastien Wilmet <swilmet gnome org>
Date: Fri Dec 15 09:25:18 2017 +0100
Don't use gdk_screen_get_width/height() (deprecated)
It's more complicated with the new API…
It would be possible to override the GtkWidget::realize vfunc to call
dh_util_window_settings_restore(), but ::realize can be called several
times since it can be unrealized, so it would require to store a boolean
instance variable to know that ::realize has already been called once.
So, calling gtk_widget_realize() is simpler.
src/dh-assistant.c | 21 +++++++++++++--------
src/dh-util.c | 29 +++++++++++++++++++++--------
src/dh-util.h | 2 +-
src/dh-window.c | 1 +
4 files changed, 36 insertions(+), 17 deletions(-)
---
diff --git a/src/dh-assistant.c b/src/dh-assistant.c
index d049974..620dfa9 100644
--- a/src/dh-assistant.c
+++ b/src/dh-assistant.c
@@ -87,25 +87,30 @@ static void
dh_assistant_init (DhAssistant *assistant)
{
DhAssistantPrivate *priv = dh_assistant_get_instance_private (assistant);
- DhSettings *settings;
gtk_widget_init_template (GTK_WIDGET (assistant));
g_signal_connect (priv->view, "open-uri",
G_CALLBACK (assistant_view_open_uri_cb),
assistant);
-
- settings = dh_settings_get_singleton ();
- dh_util_window_settings_restore (GTK_WINDOW (assistant),
- dh_settings_peek_assistant_settings (settings));
}
GtkWidget *
dh_assistant_new (DhApp *application)
{
- return g_object_new (DH_TYPE_ASSISTANT,
- "application", application,
- NULL);
+ GtkWidget *assistant;
+ DhSettings *settings;
+
+ assistant = g_object_new (DH_TYPE_ASSISTANT,
+ "application", application,
+ NULL);
+
+ settings = dh_settings_get_singleton ();
+ gtk_widget_realize (assistant);
+ dh_util_window_settings_restore (GTK_WINDOW (assistant),
+ dh_settings_peek_assistant_settings (settings));
+
+ return assistant;
}
gboolean
diff --git a/src/dh-util.c b/src/dh-util.c
index 8b46c1b..6554773 100644
--- a/src/dh-util.c
+++ b/src/dh-util.c
@@ -192,8 +192,12 @@ dh_util_window_settings_save (GtkWindow *window,
g_settings_set_int (settings, "height", height);
}
+/* This should be called when @gtk_window is realized (i.e. its GdkWindow is
+ * created) but not yet mapped (i.e. gtk_widget_show() has not yet been called,
+ * so that when it is shown it already has the good size).
+ */
void
-dh_util_window_settings_restore (GtkWindow *window,
+dh_util_window_settings_restore (GtkWindow *gtk_window,
GSettings *settings)
{
gboolean has_required_keys;
@@ -201,7 +205,8 @@ dh_util_window_settings_restore (GtkWindow *window,
gint width;
gint height;
- g_return_if_fail (GTK_IS_WINDOW (window));
+ g_return_if_fail (GTK_IS_WINDOW (gtk_window));
+ g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (gtk_window)));
g_return_if_fail (G_IS_SETTINGS (settings));
introspect_window_gsettings (settings, &has_required_keys, &has_maximized_key);
@@ -211,22 +216,30 @@ dh_util_window_settings_restore (GtkWindow *window,
height = g_settings_get_int (settings, "height");
if (width > 1 && height > 1) {
- GdkScreen *screen;
+ GdkDisplay *display;
+ GdkWindow *gdk_window;
+ GdkMonitor *monitor;
+ GdkRectangle monitor_geometry;
gint max_width;
gint max_height;
- screen = gtk_widget_get_screen (GTK_WIDGET (window));
- max_width = gdk_screen_get_width (screen);
- max_height = gdk_screen_get_height (screen);
+ display = gtk_widget_get_display (GTK_WIDGET (gtk_window));
+ /* To get the GdkWindow the widget must be realized. */
+ gdk_window = gtk_widget_get_window (GTK_WIDGET (gtk_window));
+ monitor = gdk_display_get_monitor_at_window (display, gdk_window);
+ gdk_monitor_get_geometry (monitor, &monitor_geometry);
+
+ max_width = monitor_geometry.width;
+ max_height = monitor_geometry.height;
width = CLAMP (width, 0, max_width);
height = CLAMP (height, 0, max_height);
- gtk_window_set_default_size (window, width, height);
+ gtk_window_set_default_size (gtk_window, width, height);
}
if (has_maximized_key && g_settings_get_boolean (settings, "maximized"))
- gtk_window_maximize (window);
+ gtk_window_maximize (gtk_window);
}
/* Adds q2 onto the end of q1, and frees q2. */
diff --git a/src/dh-util.h b/src/dh-util.h
index ef0e75e..f34aa2f 100644
--- a/src/dh-util.h
+++ b/src/dh-util.h
@@ -41,7 +41,7 @@ void dh_util_view_set_font (WebKitWebView *view,
void dh_util_window_settings_save (GtkWindow *window,
GSettings *settings);
-void dh_util_window_settings_restore (GtkWindow *window,
+void dh_util_window_settings_restore (GtkWindow *gtk_window,
GSettings *settings);
void dh_util_queue_concat (GQueue *q1,
diff --git a/src/dh-window.c b/src/dh-window.c
index 36228e5..fd64588 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -1555,6 +1555,7 @@ dh_window_new (DhApp *application)
window_populate (window);
settings = dh_settings_get_singleton ();
+ gtk_widget_realize (GTK_WIDGET (window));
dh_util_window_settings_restore (GTK_WINDOW (window),
dh_settings_peek_window_settings (settings));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]