[Xfce4-commits] <squeeze:stephan/ui> Add 'quit' callback and closure

Stephan Arts noreply at xfce.org
Sat Jan 7 10:28:02 CET 2012


Updating branch refs/heads/stephan/ui
         to d65dcac78aed25d62473c617062834a01659f00d (commit)
       from 000e10d8cc6aaa271fbf48a7a7c181924917dd32 (commit)

commit d65dcac78aed25d62473c617062834a01659f00d
Author: Stephan Arts <stephan at xfce.org>
Date:   Sat Nov 5 23:39:36 2011 +0100

    Add 'quit' callback and closure
    
     - The window closes when quit is pressed
     - The application is quit when the window is closed
     - The indent-style of main.c is improved.

 src/main.c        |  278 ++++++++++++++++++++++++++++-------------------------
 src/main_window.c |   45 ++++++++-
 2 files changed, 184 insertions(+), 139 deletions(-)

diff --git a/src/main.c b/src/main.c
index ab6135f..6c44f84 100644
--- a/src/main.c
+++ b/src/main.c
@@ -46,89 +46,89 @@ gint opened_archives = 0;
 
 static GOptionEntry entries[] =
 {
-	{	"extract-to", 'x', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &extract_archive_path,
-		NULL,
-		N_("[destination path]")
-	},
-	{	"extract", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &extract_archive,
-		NULL,
-		NULL
-	},
-	{	"add-to", 'd', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &add_archive_path,
-		NULL,
-		N_("[archive path] [file1] [file2] ... [fileN]")
-	},
-	{	"new", 'n', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &new_archive,
-		NULL,
-		N_("[file1] [file2] ... [fileN]")
-	},
-	{ "version", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version,
-		N_("Version information"),
-		NULL
-	},
-	{ NULL }
+    {    "extract-to", 'x', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &extract_archive_path,
+        NULL,
+        N_("[destination path]")
+    },
+    {    "extract", 'e', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &extract_archive,
+        NULL,
+        NULL
+    },
+    {    "add-to", 'd', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &add_archive_path,
+        NULL,
+        N_("[archive path] [file1] [file2] ... [fileN]")
+    },
+    {    "new", 'n', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING, &new_archive,
+        NULL,
+        N_("[file1] [file2] ... [fileN]")
+    },
+    { "version", 'v', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version,
+        N_("Version information"),
+        NULL
+    },
+    { NULL }
 };
 
 #if 0
 void
 cb_main_window_destroy(SQMainWindow *window, gpointer data)
 {
-	gtk_main_quit();
+    gtk_main_quit();
 }
 #endif
 
 int main(int argc, char **argv)
 {
-	GtkWidget *main_window = NULL;
-	GtkIconTheme *sq_icon_theme;
-	SQApplication *sq_app = NULL;
-	GError *cli_error = NULL;
-	gint i = 0;
+    GtkWidget *main_window = NULL;
+    GtkIconTheme *sq_icon_theme;
+    SQApplication *sq_app = NULL;
+    GError *cli_error = NULL;
+    gint i = 0;
     GFile *file = NULL;
-	gchar *current_dir;
-
-	#ifdef ENABLE_NLS
-	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
- 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- 	textdomain (GETTEXT_PACKAGE);
-	#endif
-
-	if(!gtk_init_with_args(&argc, &argv, _("[archive name]"), entries, PACKAGE, &cli_error))
-	{
-		if ( cli_error != NULL )
-		{
-			g_print (_("%s: %s\nTry %s --help to see a full list of available command line options.\n"), PACKAGE, cli_error->message, PACKAGE_NAME);
-			g_error_free (cli_error);
-			return 1;
-		}
-	}
-	current_dir = g_get_current_dir();
-
-	lsq_init();
-
-	sq_icon_theme = gtk_icon_theme_get_default();
-	sq_app = sq_application_new(sq_icon_theme);
-
-	gtk_window_set_default_icon_name("squeeze");
-
-	g_signal_connect(G_OBJECT(sq_app), "destroy", G_CALLBACK(gtk_main_quit), NULL);
-
-	if(version)
-	{
-		g_print("%s\n", PACKAGE_STRING);
-		return 0;
-	}
-
-
-	if(extract_archive_path || extract_archive)
-	{
-		gint err = 0;
-		if(argc == 1)
-		{
-			return 1;
-		}
-		for(i = 1; i < argc; i++)
-		{
+    gchar *current_dir;
+
+    #ifdef ENABLE_NLS
+    bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
+     bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+     textdomain (GETTEXT_PACKAGE);
+    #endif
+
+    if(!gtk_init_with_args(&argc, &argv, _("[archive name]"), entries, PACKAGE, &cli_error))
+    {
+        if ( cli_error != NULL )
+        {
+            g_print (_("%s: %s\nTry %s --help to see a full list of available command line options.\n"), PACKAGE, cli_error->message, PACKAGE_NAME);
+            g_error_free (cli_error);
+            return 1;
+        }
+    }
+    current_dir = g_get_current_dir();
+
+    lsq_init();
+
+    sq_icon_theme = gtk_icon_theme_get_default();
+    sq_app = sq_application_new(sq_icon_theme);
+
+    gtk_window_set_default_icon_name("squeeze");
+
+    g_signal_connect(G_OBJECT(sq_app), "destroy", G_CALLBACK(gtk_main_quit), NULL);
+
+    if(version)
+    {
+        g_print("%s\n", PACKAGE_STRING);
+        return 0;
+    }
+
+
+    if(extract_archive_path || extract_archive)
+    {
+        gint err = 0;
+        if(argc == 1)
+        {
+            return 1;
+        }
+        for(i = 1; i < argc; i++)
+        {
             file = g_file_new_for_path(argv[i]);
             if (file)
             {
@@ -136,73 +136,85 @@ int main(int argc, char **argv)
                     err++;
                 g_object_unref(file);
             }
-		}
-		if(err)
-			return 1;
-	}
-	if(new_archive || add_archive_path)
-	{
-		GSList *files = NULL;
-
-		/*
-		 * Remove prefix if it is the pwd
-		 */
-		for(i = 1; i < argc; i++)
-		{
-			gchar *filename;
-			if(g_str_has_prefix(argv[i], current_dir))
-			{
-				filename = g_strdup(&(argv[i][strlen(current_dir)+1]));
-			}
-			else
-				filename = g_strdup(argv[i]);
-			files = g_slist_prepend(files, filename);
-		}
-
-		/*
-		 * Remove prefix if it is the pwd
-		 */
-		if(new_archive)
-		{
-			gchar *filename;
-			if(g_str_has_prefix(new_archive, current_dir))
-			{
-				filename = g_strdup(&(new_archive[strlen(current_dir)+1]));
-			}
-			else
-				filename = g_strdup(new_archive);
-			files = g_slist_prepend(files, filename);
-		}
-
-		if(sq_application_new_archive(sq_app, file, files))
-			return 1;
-	}
-
-	if(!new_archive && !add_archive_path && !extract_archive && !extract_archive_path)
-	{
-		if(argc > 1)
-		{
-			if(sq_app->props._tabs)
-				main_window = sq_application_new_window(sq_app);
-			for(i = 1; i < argc; i++)
-			{
+        }
+        if(err)
+            return 1;
+    }
+    if(new_archive || add_archive_path)
+    {
+        GSList *files = NULL;
+
+        /*
+         * Remove prefix if it is the pwd
+         */
+        for(i = 1; i < argc; i++)
+        {
+            gchar *filename;
+            if(g_str_has_prefix(argv[i], current_dir))
+            {
+                filename = g_strdup(&(argv[i][strlen(current_dir)+1]));
+            }
+            else
+            {
+                filename = g_strdup(argv[i]);
+            }
+            files = g_slist_prepend(files, filename);
+        }
+
+        /*
+         * Remove prefix if it is the pwd
+         */
+        if(new_archive)
+        {
+            gchar *filename;
+            if(g_str_has_prefix(new_archive, current_dir))
+            {
+                filename = g_strdup(&(new_archive[strlen(current_dir)+1]));
+            }
+            else
+            {
+                filename = g_strdup(new_archive);
+            }
+            files = g_slist_prepend(files, filename);
+        }
+
+        if(sq_application_new_archive(sq_app, file, files))
+            return 1;
+    }
+
+    if(!new_archive && !add_archive_path && !extract_archive && !extract_archive_path)
+    {
+        if(argc > 1)
+        {
+            if(sq_app->props._tabs)
+                main_window = sq_application_new_window(sq_app);
+            for(i = 1; i < argc; i++)
+            {
                 file = g_file_new_for_path (argv[i]);
                 if (file)
                 {
-				    sq_application_open_archive(sq_app, main_window, file);
+                    sq_application_open_archive(sq_app, main_window, file);
                 }
-			}
-		} else
-		{
-			main_window = sq_application_new_window(sq_app);
-			gtk_widget_show_all (GTK_WIDGET(main_window));
-		}
-	}
+            }
+        }
+        else
+        {
+            main_window = sq_application_new_window(sq_app);
+            gtk_widget_show_all (GTK_WIDGET(main_window));
+        }
+    }
+
+
+    g_signal_connect (
+            G_OBJECT(main_window),
+            "destroy",
+            G_CALLBACK ( gtk_main_quit ),
+            NULL);
 
 
-	g_object_unref(sq_app);
-	gtk_main();
-	lsq_shutdown();
+    g_object_unref(sq_app);
+    gtk_main();
+    lsq_shutdown();
 
-	return 0;
+    return 0;
 }
diff --git a/src/main_window.c b/src/main_window.c
index 2e3e994..f3eac3e 100644
--- a/src/main_window.c
+++ b/src/main_window.c
@@ -79,6 +79,11 @@ cb_archive_refreshed (
         gpointer user_data);
 
 static void
+cb_window_quit (
+        GtkWidget *widget,
+        gpointer user_data);
+
+static void
 sq_treeview_reset_columns (
         GtkTreeView *treeview,
         LSQArchive *archive );
@@ -121,12 +126,12 @@ static GtkActionEntry action_entries[] =
             N_( "Close Archive"),   /* Tooltip              */
             G_CALLBACK ( NULL),     /* Callback function    */
     },
-    {"quit",                        /* Name                 */
-            GTK_STOCK_QUIT,         /* Icon-name            */
-            N_("_Quit"),            /* Label                */
-            "<control>Q",           /* Accelerator          */
-            N_( "Quit Squeeze"),    /* Tooltip              */
-            G_CALLBACK ( NULL),     /* Callback function    */
+    {"quit",                                /* Name                 */
+            GTK_STOCK_QUIT,                 /* Icon-name            */
+            N_("_Quit"),                    /* Label                */
+            "<control>Q",                   /* Accelerator          */
+            N_( "Quit Squeeze"),            /* Tooltip              */
+            G_CALLBACK ( cb_window_quit ),  /* Callback function    */
     },
 /* Edit menu */
     {"edit-menu",
@@ -278,9 +283,15 @@ sq_main_window_init ( GObject *object )
     SQMainWindow    *window = SQ_MAIN_WINDOW (object);
     GtkAccelGroup   *accel_group;
     GtkWidget       *scrolled_window;
+    GClosure        *quit_closure = g_cclosure_new (
+            (GCallback)cb_window_quit,
+            window,
+            NULL);
+
 
     GtkWidget       *main_vbox = gtk_vbox_new (FALSE, 0);
 
+  
     window->priv = g_new0 (SQMainWindowPriv, 1);
 
     window->priv->icon_theme = gtk_icon_theme_get_default();
@@ -289,6 +300,10 @@ sq_main_window_init ( GObject *object )
     
     accel_group = gtk_ui_manager_get_accel_group ( window->priv->ui_manager);
     gtk_window_add_accel_group (GTK_WINDOW ( window ), accel_group);
+
+    gtk_accel_group_connect_by_path (accel_group, "<Window>/quit", quit_closure);
+
+    gtk_accel_map_change_entry ("<Window>/quit", GDK_q, 0, FALSE);
  
     gtk_ui_manager_insert_action_group (
             window->priv->ui_manager,
@@ -564,3 +579,21 @@ cb_archive_refreshed (
             GTK_TREE_VIEW (window->priv->tree_view),
             archive );
 } 
+
+/**
+ * cb_window_quit:
+ * @widget:
+ * @window:
+ *
+ *
+ */
+static void
+cb_window_quit (
+        GtkWidget *widget,
+        gpointer user_data)
+{
+    GtkWindow *window = GTK_WINDOW (user_data);
+
+    gtk_widget_destroy (GTK_WIDGET (window));
+}
+


More information about the Xfce4-commits mailing list