[ghex] Port to GtkApplication



commit 5ed357f6dd387c12061842a63d0db0983f8fa54e
Author: Kalev Lember <kalevlember gmail com>
Date:   Wed Aug 8 14:59:56 2012 +0300

    Port to GtkApplication
    
    Make GHex a non-unique GtkApplication; this is a
    prerequisite for recent desktop integration features.

 src/ghex-window.c |   19 ++++++++++---------
 src/ghex-window.h |    8 +++++---
 src/main.c        |   23 +++++++++++++++++++----
 src/ui.c          |    8 +++++---
 4 files changed, 39 insertions(+), 19 deletions(-)
---
diff --git a/src/ghex-window.c b/src/ghex-window.c
index 78b78d2..6474097 100644
--- a/src/ghex-window.c
+++ b/src/ghex-window.c
@@ -96,7 +96,7 @@ ghex_window_drag_data_received(GtkWidget *widget,
         }
 
         if (newwin == NULL)
-            newwin = ghex_window_new ();
+            newwin = ghex_window_new (GTK_APPLICATION (g_application_get_default ()));
         if (ghex_window_load (GHEX_WINDOW (newwin), filename)) {
             if (newwin != GTK_WIDGET (win))
                 gtk_widget_show (newwin);
@@ -319,10 +319,8 @@ ghex_window_destroy (GtkWidget *object)
 
         window_list = g_list_remove(window_list, win);
 
-        if(window_list == NULL) {
-            gtk_main_quit ();
+        if (window_list == NULL)
             active_window = NULL;
-        }
         else if(active_window == win)
             active_window = GHEX_WINDOW(window_list->data);
 
@@ -652,7 +650,7 @@ ghex_window_sync_char_table_item(GHexWindow *win, gboolean state)
 }
 
 GtkWidget *
-ghex_window_new(void)
+ghex_window_new (GtkApplication *application)
 {
     GHexWindow *win;
     const GList *doc_list;
@@ -662,6 +660,7 @@ ghex_window_new(void)
 	};
 
 	win = GHEX_WINDOW(g_object_new(GHEX_TYPE_WINDOW,
+                                   "application", application,
                                    "title", _("GHex"),
                                    NULL));
 
@@ -706,9 +705,10 @@ create_document_view(HexDocument *doc)
 }
 
 GtkWidget *
-ghex_window_new_from_doc(HexDocument *doc)
+ghex_window_new_from_doc (GtkApplication *application,
+                          HexDocument    *doc)
 {
-    GtkWidget *win = ghex_window_new();
+    GtkWidget *win = ghex_window_new (application);
     GtkWidget *gh = create_document_view(doc);
 
     gtk_widget_show(gh);
@@ -724,9 +724,10 @@ ghex_window_new_from_doc(HexDocument *doc)
 }
 
 GtkWidget *
-ghex_window_new_from_file(const gchar *filename)
+ghex_window_new_from_file (GtkApplication *application,
+                           const gchar    *filename)
 {
-    GtkWidget *win = ghex_window_new();
+    GtkWidget *win = ghex_window_new (application);
 
     if(!ghex_window_load(GHEX_WINDOW(win), filename)) {
         gtk_widget_destroy(win);
diff --git a/src/ghex-window.h b/src/ghex-window.h
index df1854e..e9d92bb 100644
--- a/src/ghex-window.h
+++ b/src/ghex-window.h
@@ -71,9 +71,11 @@ struct _GHexWindowClass
 };
 
 GType             ghex_window_get_type           (void) G_GNUC_CONST;
-GtkWidget         *ghex_window_new               (void);
-GtkWidget         *ghex_window_new_from_doc      (HexDocument *doc);
-GtkWidget         *ghex_window_new_from_file     (const gchar *filename);
+GtkWidget         *ghex_window_new               (GtkApplication    *application);
+GtkWidget         *ghex_window_new_from_doc      (GtkApplication    *application,
+                                                  HexDocument       *doc);
+GtkWidget         *ghex_window_new_from_file     (GtkApplication    *application,
+                                                  const gchar       *filename);
 void              ghex_window_set_contents       (GHexWindow *win, GtkWidget  *child);
 void              ghex_window_destroy_contents   (GHexWindow *win);
 gboolean          ghex_window_load(GHexWindow *win, const gchar *filename);
diff --git a/src/main.c b/src/main.c
index 050896d..5a6355e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -70,13 +70,22 @@ ghex_locale_dir (void)
 #endif
 }
 
+static void
+ghex_activate (GApplication *application,
+               gpointer      unused)
+{
+    GList *windows = gtk_application_get_windows (GTK_APPLICATION (application));
+    gtk_window_present (GTK_WINDOW (windows->data));
+}
 
 int
 main(int argc, char **argv)
 {
 	GtkWidget *win;
 	GError *error = NULL;
+	GtkApplication *application;
 	gchar *locale_dir;
+	gint retval;
 
 	locale_dir = ghex_locale_dir ();
 	bindtextdomain (GETTEXT_PACKAGE, locale_dir);
@@ -106,11 +115,16 @@ main(int argc, char **argv)
 	/* accessibility setup */
 	setup_factory();
 
+	application = gtk_application_new ("org.gnome.GHexApplication",
+	                                   G_APPLICATION_NON_UNIQUE);
+	g_signal_connect (application, "activate",
+	                  G_CALLBACK (ghex_activate), NULL);
+
 	if (args_remaining != NULL) {
 		gchar **filename;
 		for (filename = args_remaining; *filename != NULL; filename++) {
 			if (g_file_test (*filename, G_FILE_TEST_EXISTS)) {
-				win = ghex_window_new_from_file(*filename);
+				win = ghex_window_new_from_file (application, *filename);
 				if(win != NULL) {
 					if(geometry) {
 						if(!gtk_window_parse_geometry(GTK_WINDOW(win), geometry))
@@ -124,7 +138,7 @@ main(int argc, char **argv)
 	}
 
 	if(ghex_window_get_list() == NULL) {
-		win = ghex_window_new();
+		win = ghex_window_new (application);
 		if(geometry) {
 			if(!gtk_window_parse_geometry(GTK_WINDOW(win), geometry))
 				g_warning(_("Invalid geometry string \"%s\"\n"), geometry);
@@ -134,7 +148,8 @@ main(int argc, char **argv)
 	}
 	else win = GTK_WIDGET(ghex_window_get_list()->data);
 
-	gtk_main ();
+	retval = g_application_run (G_APPLICATION (application), argc, argv);
+	g_object_unref (application);
 
-	return 0;
+	return retval;
 }
diff --git a/src/ui.c b/src/ui.c
index 41928ea..3f714f5 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -266,7 +266,7 @@ quit_app_cb (GtkAction *action,
 			return;
 		doc_node = doc_node->next;
 	}
-	gtk_main_quit ();
+	g_application_quit (g_application_get_default ());
 }
 
 void
@@ -331,7 +331,8 @@ open_cb (GtkAction *action,
 		gchar *flash;
 
 		if(GHEX_WINDOW(win)->gh != NULL) {
-			win = GHEX_WINDOW(ghex_window_new_from_file(gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel))));
+			win = GHEX_WINDOW (ghex_window_new_from_file (GTK_APPLICATION (g_application_get_default ()),
+			                                              gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_sel))));
 			if(win != NULL)
 				gtk_widget_show(GTK_WIDGET(win));
 		}
@@ -883,7 +884,8 @@ add_view_cb (GtkAction *action,
 	if(win->gh == NULL)
 		return;
 
-	newwin = ghex_window_new_from_doc(win->gh->document);
+	newwin = ghex_window_new_from_doc (GTK_APPLICATION (g_application_get_default ()),
+	                                   win->gh->document);
 	gtk_widget_show(newwin);
 }
 



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