[gtk: 1/2] Remove some duplication in testfilechooserbutton



commit a0056d5ca859e1be8551b15ecd0d64f66da8af3e
Author: Manu Cornet <m ma nu>
Date:   Mon Sep 17 12:27:46 2018 +0000

    Remove some duplication in testfilechooserbutton
    
    Most of the code creating the two types of dialogs (open file,
    choose folder) is the same. This refactors the common code into a
    helper method. This also makes it easier to add other chooser types
    for this test (e.g. save file).

 tests/testfilechooserbutton.c | 92 ++++++++++++++++++++-----------------------
 1 file changed, 42 insertions(+), 50 deletions(-)
---
diff --git a/tests/testfilechooserbutton.c b/tests/testfilechooserbutton.c
index 878a8e6c6e..4aa89dd7bb 100644
--- a/tests/testfilechooserbutton.c
+++ b/tests/testfilechooserbutton.c
@@ -206,13 +206,48 @@ chooser_update_preview_cb (GtkFileChooser *chooser,
     }
 }
 
+static void
+add_new_filechooser_button (const gchar          *mnemonic,
+                            const gchar          *chooser_title,
+                            GtkFileChooserAction  action,
+                            GtkWidget            *group_box,
+                            GtkSizeGroup         *label_group)
+{
+  GtkWidget *hbox, *label, *chooser, *button;
+
+  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
+  gtk_box_pack_start (GTK_BOX (group_box), hbox);
+
+  label = gtk_label_new_with_mnemonic (mnemonic);
+  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
+  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
+  gtk_box_pack_start (GTK_BOX (hbox), label);
+
+  chooser = gtk_file_chooser_button_new (g_strconcat(chooser_title,
+                                                     " - testfilechooserbutton", NULL),
+                                         action);
+  gtk_widget_set_hexpand (chooser, TRUE);
+  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
+  gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
+  gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
+  g_signal_connect (chooser, "current-folder-changed",
+                   G_CALLBACK (chooser_current_folder_changed_cb), NULL);
+  g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
+  g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
+  g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
+  gtk_box_pack_start (GTK_BOX (hbox), chooser);
+
+  button = gtk_button_new_with_label ("Tests");
+  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
+  gtk_box_pack_start (GTK_BOX (hbox), button);
+}
+
 
 int
 main (int   argc,
       char *argv[])
 {
   GtkWidget *win, *vbox, *frame, *group_box;
-  GtkWidget *hbox, *label, *chooser, *button;
   GtkSizeGroup *label_group;
   GOptionContext *context;
   gchar *cwd;
@@ -255,57 +290,14 @@ main (int   argc,
   gtk_container_add (GTK_CONTAINER (frame), group_box);
 
   /* OPEN */
-  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
-  gtk_box_pack_start (GTK_BOX (group_box), hbox);
-
-  label = gtk_label_new_with_mnemonic ("_Open:");
-  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
-  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
-  gtk_box_pack_start (GTK_BOX (hbox), label);
-
-  chooser = gtk_file_chooser_button_new ("Select A File - testfilechooserbutton",
-                                         GTK_FILE_CHOOSER_ACTION_OPEN);
-  gtk_widget_set_hexpand (chooser, TRUE);
-  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
-  gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
-  gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
-  g_signal_connect (chooser, "current-folder-changed",
-                   G_CALLBACK (chooser_current_folder_changed_cb), NULL);
-  g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
-  g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
-  g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
-  gtk_box_pack_start (GTK_BOX (hbox), chooser);
-
-  button = gtk_button_new_with_label ("Tests");
-  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
-  gtk_box_pack_start (GTK_BOX (hbox), button);
+  add_new_filechooser_button ("_Open:", "Select A File",
+                              GTK_FILE_CHOOSER_ACTION_OPEN,
+                              group_box, label_group);
 
   /* SELECT_FOLDER */
-  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
-  gtk_box_pack_start (GTK_BOX (group_box), hbox);
-
-  label = gtk_label_new_with_mnemonic ("Select _Folder:");
-  gtk_size_group_add_widget (GTK_SIZE_GROUP (label_group), label);
-  gtk_label_set_xalign (GTK_LABEL (label), 0.0);
-  gtk_box_pack_start (GTK_BOX (hbox), label);
-
-  chooser = gtk_file_chooser_button_new ("Select A Folder - testfilechooserbutton",
-                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
-  gtk_widget_set_hexpand (chooser, TRUE);
-  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
-  gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
-  gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
-  gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
-  g_signal_connect (chooser, "current-folder-changed",
-                   G_CALLBACK (chooser_current_folder_changed_cb), NULL);
-  g_signal_connect (chooser, "selection-changed", G_CALLBACK (chooser_selection_changed_cb), NULL);
-  g_signal_connect (chooser, "file-activated", G_CALLBACK (chooser_file_activated_cb), NULL);
-  g_signal_connect (chooser, "update-preview", G_CALLBACK (chooser_update_preview_cb), NULL);
-  gtk_box_pack_start (GTK_BOX (hbox), chooser);
-
-  button = gtk_button_new_with_label ("Tests");
-  g_signal_connect (button, "clicked", G_CALLBACK (tests_button_clicked_cb), chooser);
-  gtk_box_pack_start (GTK_BOX (hbox), button);
+  add_new_filechooser_button ("Select _Folder:", "Select A Folder",
+                              GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
+                              group_box, label_group);
 
   g_object_unref (label_group);
 


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