[gedit/dbus] Implement --geometry



commit aec8ebc9492627a5547fe70ed40418cf9a4a0a7a
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Thu May 6 23:20:27 2010 +0200

    Implement --geometry

 gedit/gedit-command-line.c |   80 +++++++++++++++++++++++++++++++++++++++++++-
 gedit/gedit-command-line.h |   14 +++++++-
 gedit/gedit-dbus.c         |   47 ++++++++++++++++++++++++-
 gedit/gedit-utils.c        |   30 ++++++++++++++++
 gedit/gedit-utils.h        |    3 ++
 gedit/gedit.c              |   14 ++++++++
 6 files changed, 184 insertions(+), 4 deletions(-)
---
diff --git a/gedit/gedit-command-line.c b/gedit/gedit-command-line.c
index 0a0a0cc..f331521 100644
--- a/gedit/gedit-command-line.c
+++ b/gedit/gedit-command-line.c
@@ -42,6 +42,7 @@ struct _GeditCommandLinePrivate
 	/* These are directly set as options */
 	gchar *line_column_position;
 	gchar *encoding_charset;
+	gchar *geometry_string;
 	gboolean new_window;
 	gboolean new_document;
 	gchar **remaining_args;
@@ -55,6 +56,8 @@ struct _GeditCommandLinePrivate
 	gint column_position;
 	GSList *file_list;
 	const GeditEncoding *encoding;
+
+	GeditCommandLineGeometry *geometry;
 };
 
 G_DEFINE_TYPE (GeditCommandLine, gedit_command_line, G_TYPE_INITIALLY_UNOWNED)
@@ -68,6 +71,13 @@ gedit_command_line_finalize (GObject *object)
 	g_free (command_line->priv->line_column_position);
 	g_strfreev (command_line->priv->remaining_args);
 
+	g_free (command_line->priv->geometry_string);
+
+	if (command_line->priv->geometry)
+	{
+		g_slice_free (GeditCommandLineGeometry, command_line->priv->geometry);
+	}
+
 	g_slist_foreach (command_line->priv->file_list, (GFunc)g_object_unref, NULL);
 	g_slist_free (command_line->priv->file_list);
 
@@ -177,6 +187,56 @@ get_line_column_position (GeditCommandLine *command_line,
 	g_strfreev (split);
 }
 
+static GeditCommandLineGeometry *
+parse_geometry (const gchar *geometry_string)
+{
+	/* <width>x<height>[+-]<xoff>[+-]yoff */
+	GRegex *regex;
+	GMatchInfo *match_info;
+	GeditCommandLineGeometry *geometry;
+	gchar *width;
+	gchar *height;
+	gchar *xoff;
+	gchar *yoff;
+
+	if (geometry_string == NULL)
+	{
+		return NULL;
+	}
+
+	regex = g_regex_new ("^([0-9]+)x([0-9]+)([+-][0-9]+)([+-][0-9]+)$", G_REGEX_CASELESS, 0, NULL);
+
+	if (!g_regex_match (regex, geometry_string, 0, &match_info))
+	{
+		g_match_info_free (match_info);
+		g_regex_unref (regex);
+
+		return NULL;
+	}
+
+	geometry = g_slice_new (GeditCommandLineGeometry);
+
+	width = g_match_info_fetch (match_info, 1);
+	height = g_match_info_fetch (match_info, 2);
+	xoff = g_match_info_fetch (match_info, 3);
+	yoff = g_match_info_fetch (match_info, 4);
+
+	geometry->width = g_ascii_strtoll (width, NULL, 10);
+	geometry->height = g_ascii_strtoll (height, NULL, 10);
+	geometry->xoffset = g_ascii_strtoll (xoff, NULL, 10);
+	geometry->yoffset = g_ascii_strtoll (yoff, NULL, 10);
+
+	g_free (width);
+	g_free (height);
+	g_free (xoff);
+	g_free (yoff);
+
+	g_match_info_free (match_info);
+	g_regex_unref (regex);
+
+	return geometry;
+}
+
 static void
 process_remaining_arguments (GeditCommandLine *command_line)
 {
@@ -231,6 +291,9 @@ process_command_line (GeditCommandLine *command_line)
 		command_line->priv->encoding_charset = NULL;
 	}
 
+	/* Parse geometry */
+	command_line->priv->geometry = parse_geometry (command_line->priv->geometry_string);
+
 	/* Parse remaining arguments */
 	process_remaining_arguments (command_line);
 }
@@ -260,7 +323,8 @@ gedit_command_line_parse (GeditCommandLine   *command_line,
 
 		/* Encoding */
 		{
-			"encoding", '\0', 0, G_OPTION_ARG_STRING, &command_line->priv->encoding_charset,
+			"encoding", '\0', 0, G_OPTION_ARG_STRING,
+			&command_line->priv->encoding_charset,
 			N_("Set the character encoding to be used to open the files listed on the command line"),
 			N_("ENCODING")
 		},
@@ -281,6 +345,14 @@ gedit_command_line_parse (GeditCommandLine   *command_line,
 			NULL
 		},
 
+		/* Window geometry */
+		{
+			"geometry", 'g', 0, G_OPTION_ARG_STRING,
+			&command_line->priv->geometry_string,
+			N_("Set the X geometry window size (WIDTHxHEIGHT+X+Y)"),
+			N_("GEOMETRY")
+		},
+
 		/* Wait for closing documents */
 		{
 			"wait", 'w', 0, G_OPTION_ARG_NONE,
@@ -413,5 +485,11 @@ gedit_command_line_get_standalone (GeditCommandLine *command_line)
 	return command_line->priv->standalone;
 }
 
+const GeditCommandLineGeometry *
+gedit_command_line_get_geometry (GeditCommandLine *command_line)
+{
+	g_return_val_if_fail (GEDIT_IS_COMMAND_LINE (command_line), NULL);
+	return command_line->priv->geometry;
+}
 
 /* ex:ts=8:noet: */
diff --git a/gedit/gedit-command-line.h b/gedit/gedit-command-line.h
index e17e10e..8665792 100644
--- a/gedit/gedit-command-line.h
+++ b/gedit/gedit-command-line.h
@@ -24,7 +24,7 @@
 #define __GEDIT_COMMAND_LINE_H__
 
 #include <glib-object.h>
-#include "gedit-encodings.h"
+#include <gedit/gedit-encodings.h>
 
 G_BEGIN_DECLS
 
@@ -40,6 +40,8 @@ typedef struct _GeditCommandLine	GeditCommandLine;
 typedef struct _GeditCommandLineClass	GeditCommandLineClass;
 typedef struct _GeditCommandLinePrivate	GeditCommandLinePrivate;
 
+typedef struct _GeditCommandLineGeometry GeditCommandLineGeometry;
+
 struct _GeditCommandLine {
 	GInitiallyUnowned parent;
 
@@ -50,6 +52,13 @@ struct _GeditCommandLineClass {
 	GObjectClass parent_class;
 };
 
+struct _GeditCommandLineGeometry {
+	gint width;
+	gint height;
+	gint xoffset;
+	gint yoffset;
+};
+
 GType gedit_command_line_get_type (void) G_GNUC_CONST;
 
 GeditCommandLine *gedit_command_line_get_default (void);
@@ -70,6 +79,9 @@ gboolean gedit_command_line_get_wait (GeditCommandLine *command_line);
 gboolean gedit_command_line_get_background (GeditCommandLine *command_line);
 gboolean gedit_command_line_get_standalone (GeditCommandLine *command_line);
 
+const GeditCommandLineGeometry *
+         gedit_command_line_get_geometry (GeditCommandLine *command_line);
+
 G_END_DECLS
 
 #endif /* __GEDIT_COMMAND_LINE_H__ */
diff --git a/gedit/gedit-dbus.c b/gedit/gedit-dbus.c
index 6a36a13..11ceb44 100644
--- a/gedit/gedit-dbus.c
+++ b/gedit/gedit-dbus.c
@@ -305,6 +305,7 @@ compose_open_parameters (GeditDBus *dbus)
 	const GeditEncoding *encoding;
 	DisplayParameters dparams;
 	GeditCommandLine *command_line;
+	const GeditCommandLineGeometry *geometry;
 
 	command_line = gedit_command_line_get_default ();
 
@@ -416,6 +417,32 @@ compose_open_parameters (GeditDBus *dbus)
 	                       "viewport_y",
 	                       g_variant_new_int32 (dparams.viewport_y));
 
+	/* set geometry */
+	geometry = gedit_command_line_get_geometry (command_line);
+
+	if (geometry)
+	{
+		g_variant_builder_add (&options,
+		                       "{sv}",
+		                       "geometry_width",
+		                       g_variant_new_int32 (geometry->width));
+
+		g_variant_builder_add (&options,
+		                       "{sv}",
+		                       "geometry_height",
+		                       g_variant_new_int32 (geometry->height));
+
+		g_variant_builder_add (&options,
+		                       "{sv}",
+		                       "geometry_xoffset",
+		                       g_variant_new_int32 (geometry->xoffset));
+
+		g_variant_builder_add (&options,
+		                       "{sv}",
+		                       "geometry_yoffset",
+		                       g_variant_new_int32 (geometry->yoffset));
+	}
+
 	return g_variant_new ("(asa{sv})", &file_list, &options);
 }
 
@@ -1199,16 +1226,17 @@ handle_open_pipe (GeditDBus      *dbus,
 #endif
 }
 
-static void
+static gboolean
 extract_optional_parameters (GHashTable  *parameters,
                              ...) G_GNUC_NULL_TERMINATED;
 
-static void
+static gboolean
 extract_optional_parameters (GHashTable  *parameters,
                              ...)
 {
 	va_list va_args;
 	const gchar *key;
+	gboolean ret = FALSE;
 
 	va_start (va_args, parameters);
 
@@ -1225,6 +1253,8 @@ extract_optional_parameters (GHashTable  *parameters,
 			continue;
 		}
 
+		ret = TRUE;
+
 		g_variant_get_va (value,
 		                  g_variant_get_type_string (value),
 		                  NULL,
@@ -1232,6 +1262,7 @@ extract_optional_parameters (GHashTable  *parameters,
 	}
 
 	va_end (va_args);
+	return ret;
 }
 
 static GHashTable *
@@ -1325,6 +1356,7 @@ dbus_handle_open (GeditDBus             *dbus,
 
 	{
 		DisplayParameters display_parameters = {NULL, -1, -1, -1, -1};
+		GdkRectangle geometry;
 
 		extract_optional_parameters (options_hash,
 		                             "new_window", &new_window,
@@ -1340,6 +1372,17 @@ dbus_handle_open (GeditDBus             *dbus,
 		                                        TRUE);
 
 		g_free (display_parameters.display_name);
+
+		if (extract_optional_parameters (options_hash,
+		                                 "geometry_width", &geometry.width,
+		                                 "geometry_height", &geometry.height,
+		                                 "geometry_xoffset", &geometry.x,
+		                                 "geometry_yoffset", &geometry.y,
+		                                 NULL))
+		{
+			gedit_utils_set_window_geometry (GTK_WINDOW (window),
+			                                 &geometry);
+		}
 	}
 
 	extract_optional_parameters (options_hash, "encoding", &charset_encoding, NULL);
diff --git a/gedit/gedit-utils.c b/gedit/gedit-utils.c
index 071e137..7aa2985 100644
--- a/gedit/gedit-utils.c
+++ b/gedit/gedit-utils.c
@@ -1540,4 +1540,34 @@ gedit_utils_can_read_from_stdin (void)
 #endif
 }
 
+void
+gedit_utils_set_window_geometry	(GtkWindow    *window,
+                                 GdkRectangle *geometry)
+{
+	GdkGravity gravity;
+
+	gravity = gtk_window_get_gravity (window);
+	gtk_window_resize (window, geometry->width, geometry->height);
+
+	if (geometry->x >= 0 && geometry->y >= 0)
+	{
+		gtk_window_set_gravity (window, GDK_GRAVITY_NORTH_WEST);
+	}
+	else if (geometry->x >= 0)
+	{
+		gtk_window_set_gravity (window, GDK_GRAVITY_SOUTH_WEST);
+	}
+	else if (geometry->y >= 0)
+	{
+		gtk_window_set_gravity (window, GDK_GRAVITY_NORTH_EAST);
+	}
+	else
+	{
+		gtk_window_set_gravity (window, GDK_GRAVITY_SOUTH_EAST);
+	}
+
+	gtk_window_move (window, geometry->x, geometry->y);
+	gtk_window_set_gravity (window, gravity);
+}
+
 /* ex:ts=8:noet: */
diff --git a/gedit/gedit-utils.h b/gedit/gedit-utils.h
index 8c8c6d4..0bacacc 100644
--- a/gedit/gedit-utils.h
+++ b/gedit/gedit-utils.h
@@ -156,6 +156,9 @@ gchar 	       **gedit_utils_drop_get_uris		(GtkSelectionData *selection_data);
 
 gboolean	 gedit_utils_can_read_from_stdin	(void);
 
+void		 gedit_utils_set_window_geometry	(GtkWindow    *window,
+                                                         GdkRectangle *geometry);
+
 G_END_DECLS
 
 #endif /* __GEDIT_UTILS_H__ */
diff --git a/gedit/gedit.c b/gedit/gedit.c
index c6d5c64..28d0f2f 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -101,6 +101,7 @@ gedit_main_window (void)
 	GeditCommandLine *command_line;
 	GeditApp *app;
 	gboolean doc_created = FALSE;
+	const GeditCommandLineGeometry *geometry;
 
 	app = gedit_app_get_default ();
 
@@ -144,6 +145,19 @@ gedit_main_window (void)
 		gedit_window_create_tab (window, TRUE);
 	}
 
+	geometry = gedit_command_line_get_geometry (command_line);
+
+	if (geometry)
+	{
+		GdkRectangle rect = {geometry->xoffset,
+		                     geometry->yoffset,
+		                     geometry->width,
+		                     geometry->height};
+
+		gedit_utils_set_window_geometry (GTK_WINDOW (window),
+		                                 &rect);
+	}
+
 	gedit_debug_message (DEBUG_APP, "Show window");
 	gtk_widget_show (GTK_WIDGET (window));
 }



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