[gedit/wip/gtkapp: 22/26] Restore proper handling of workspaces



commit b936751e5c080ba0649e89836eb4a36674aec85e
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Dec 22 18:07:18 2012 +0100

    Restore proper handling of workspaces
    
    Activate an existing window only if it is on the current workspace. "Current" here is
    defined as "current for the X server"... there is a theoretical race because we ask
    what is the "current" workspace when we receive the message on the primary instance,
    but that's not a problem and the X reply is async anyway.

 gedit/gedit-app.c |  199 +++++++++++++++++++++++++----------------------------
 gedit/gedit-app.h |    5 --
 2 files changed, 93 insertions(+), 111 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index d8588af..519eecd 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -416,15 +416,105 @@ gedit_app_startup (GApplication *application)
 	                            app);
 }
 
+static gboolean
+is_in_viewport (GtkWindow    *window,
+		GdkScreen    *screen,
+		gint          workspace,
+		gint          viewport_x,
+		gint          viewport_y)
+{
+	GdkScreen *s;
+	GdkDisplay *display;
+	GdkWindow *gdkwindow;
+	const gchar *cur_name;
+	const gchar *name;
+	gint cur_n;
+	gint n;
+	gint ws;
+	gint sc_width, sc_height;
+	gint x, y, width, height;
+	gint vp_x, vp_y;
+
+	/* Check for screen and display match */
+	display = gdk_screen_get_display (screen);
+	cur_name = gdk_display_get_name (display);
+	cur_n = gdk_screen_get_number (screen);
+
+	s = gtk_window_get_screen (window);
+	display = gdk_screen_get_display (s);
+	name = gdk_display_get_name (display);
+	n = gdk_screen_get_number (s);
+
+	if (strcmp (cur_name, name) != 0 || cur_n != n)
+	{
+		return FALSE;
+	}
+
+	/* Check for workspace match */
+	ws = gedit_utils_get_window_workspace (window);
+	if (ws != workspace && ws != GEDIT_ALL_WORKSPACES)
+	{
+		return FALSE;
+	}
+
+	/* Check for viewport match */
+	gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
+	gdk_window_get_position (gdkwindow, &x, &y);
+	width = gdk_window_get_width (gdkwindow);
+	height = gdk_window_get_height (gdkwindow);
+	gedit_utils_get_current_viewport (screen, &vp_x, &vp_y);
+	x += vp_x;
+	y += vp_y;
+
+	sc_width = gdk_screen_get_width (screen);
+	sc_height = gdk_screen_get_height (screen);
+
+	return x + width * .25 >= viewport_x &&
+	       x + width * .75 <= viewport_x + sc_width &&
+	       y >= viewport_y &&
+	       y + height <= viewport_y + sc_height;
+}
+
+static GeditWindow *
+get_active_window (GtkApplication *app)
+{
+	GdkScreen *screen;
+	guint workspace;
+	gint viewport_x, viewport_y;
+	GList *windows, *l;
+
+	screen = gdk_screen_get_default ();
+
+	workspace = gedit_utils_get_current_workspace (screen);
+	gedit_utils_get_current_viewport (screen, &viewport_x, &viewport_y);
+
+	/* Gtk documentation says the window list is always in MRU order */
+	windows = gtk_application_get_windows (app);
+	for (l = windows; l != NULL; l = l->next)
+	{
+		GtkWindow *window = l->data;
+
+		if (is_in_viewport (window, screen, workspace, viewport_x, viewport_y))
+		{
+			return GEDIT_WINDOW (window);
+		}
+	}
+
+	return NULL;
+}
+
 static void
 gedit_app_activate (GApplication *application)
 {
-	GeditWindow *window;
+	GeditWindow *window = NULL;
 	gboolean doc_created = FALSE;
 
-	window = GEDIT_WINDOW (gtk_application_get_active_window (GTK_APPLICATION (application)));
+	if (!new_window)
+	{
+		window = get_active_window (GTK_APPLICATION (application));
+	}
 
-	if (window == NULL || new_window)
+	if (window == NULL)
 	{
 		gedit_debug_message (DEBUG_APP, "Create main window");
 		window = gedit_app_create_window (GEDIT_APP (application), NULL);
@@ -1082,109 +1172,6 @@ gedit_app_create_window (GeditApp  *app,
 	return window;
 }
 
-static gboolean
-is_in_viewport (GeditWindow  *window,
-		GdkScreen    *screen,
-		gint          workspace,
-		gint          viewport_x,
-		gint          viewport_y)
-{
-	GdkScreen *s;
-	GdkDisplay *display;
-	GdkWindow *gdkwindow;
-	const gchar *cur_name;
-	const gchar *name;
-	gint cur_n;
-	gint n;
-	gint ws;
-	gint sc_width, sc_height;
-	gint x, y, width, height;
-	gint vp_x, vp_y;
-
-	/* Check for screen and display match */
-	display = gdk_screen_get_display (screen);
-	cur_name = gdk_display_get_name (display);
-	cur_n = gdk_screen_get_number (screen);
-
-	s = gtk_window_get_screen (GTK_WINDOW (window));
-	display = gdk_screen_get_display (s);
-	name = gdk_display_get_name (display);
-	n = gdk_screen_get_number (s);
-
-	if (strcmp (cur_name, name) != 0 || cur_n != n)
-		return FALSE;
-
-	/* Check for workspace match */
-	ws = gedit_utils_get_window_workspace (GTK_WINDOW (window));
-	if (ws != workspace && ws != GEDIT_ALL_WORKSPACES)
-		return FALSE;
-
-	/* Check for viewport match */
-	gdkwindow = gtk_widget_get_window (GTK_WIDGET (window));
-	gdk_window_get_position (gdkwindow, &x, &y);
-	width = gdk_window_get_width (gdkwindow);
-	height = gdk_window_get_height (gdkwindow);
-	gedit_utils_get_current_viewport (screen, &vp_x, &vp_y);
-	x += vp_x;
-	y += vp_y;
-
-	sc_width = gdk_screen_get_width (screen);
-	sc_height = gdk_screen_get_height (screen);
-
-	return x + width * .25 >= viewport_x &&
-	       x + width * .75 <= viewport_x + sc_width &&
-	       y  >= viewport_y &&
-	       y + height <= viewport_y + sc_height;
-}
-
-/**
- * _gedit_app_get_window_in_viewport:
- * @app: the #GeditApp
- * @screen: the #GdkScreen
- * @workspace: the workspace number
- * @viewport_x: the viewport horizontal origin
- * @viewport_y: the viewport vertical origin
- *
- * Since a workspace can be larger than the screen, it is divided into several
- * equal parts called viewports. This function retrives the #GeditWindow in
- * the given viewport of the given workspace.
- *
- * Return value: the #GeditWindow in the given viewport of the given workspace
- *    or %NULL% if no window is found.
- */
-GeditWindow *
-_gedit_app_get_window_in_viewport (GeditApp  *app,
-				   GdkScreen *screen,
-				   gint       workspace,
-				   gint       viewport_x,
-				   gint       viewport_y)
-{
-	GeditWindow *window;
-	GList *windows, *l;
-
-	g_return_val_if_fail (GEDIT_IS_APP (app), NULL);
-
-	/* first try if the active window */
-	window = GEDIT_WINDOW (gtk_application_get_active_window (GTK_APPLICATION (app)));
-
-	if (window != NULL && is_in_viewport (window, screen, workspace, viewport_x, viewport_y))
-	{
-		return window;
-	}
-
-	/* otherwise try to see if there is a window on this workspace */
-	windows = gtk_application_get_windows (GTK_APPLICATION (app));
-	for (l = windows; l != NULL; l = l->next)
-	{
-		window = l->data;
-
-		if (is_in_viewport (window, screen, workspace, viewport_x, viewport_y))
-			return window;
-	}
-
-	return NULL;
-}
-
 /**
  * gedit_app_get_documents:
  * @app: the #GeditApp
diff --git a/gedit/gedit-app.h b/gedit/gedit-app.h
index b6df192..fd9d53d 100644
--- a/gedit/gedit-app.h
+++ b/gedit/gedit-app.h
@@ -122,11 +122,6 @@ gboolean	gedit_app_process_window_event		(GeditApp    *app,
 							 GdkEvent    *event);
 
 /* Non exported functions */
-GeditWindow	*_gedit_app_get_window_in_viewport	(GeditApp     *app,
-							 GdkScreen   *screen,
-							 gint         workspace,
-							 gint         viewport_x,
-							 gint         viewport_y);
 void		 _gedit_app_set_lockdown		(GeditApp          *app,
 							 GeditLockdownMask  lockdown);
 void		 _gedit_app_set_lockdown_bit		(GeditApp          *app,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]