[easytag/wip/application-window: 53/70] Refactor popup menu handling



commit 33a030b245957489585964faf4b53213522c4492
Author: David King <amigadave amigadave com>
Date:   Mon Jul 14 17:16:41 2014 +0100

    Refactor popup menu handling
    
    Use gdk_event_triggers_context_menu() in button-press-event handlers.
    Use GDK_BUTTON_PRIMARY instead of 1 where appropriate.

 TODO                     |    1 +
 src/application_window.c |   70 ++++++++++++++++++++-------------------------
 src/browser.c            |   16 ++++++----
 src/cddb_dialog.c        |    6 ++--
 src/load_files_dialog.c  |   13 +++++---
 src/log.c                |   10 ++++--
 src/picture.c            |    5 ++-
 7 files changed, 62 insertions(+), 59 deletions(-)
---
diff --git a/TODO b/TODO
index 02caecd..dca374a 100644
--- a/TODO
+++ b/TODO
@@ -9,6 +9,7 @@ General tidying
 ** Additionally, make I/O asynchronous
 ** Always use the GLib encoding for filenames (convert for display)
 * Avoid using gtk_events_pending(), use asynchronous operations instead
+* Use the popup-menu signal on GtkWidget
 
 After GTK+ 3
 ------------
diff --git a/src/application_window.c b/src/application_window.c
index 471eeaa..0e3ab51 100644
--- a/src/application_window.c
+++ b/src/application_window.c
@@ -218,38 +218,18 @@ Convert_Remove_All_Text (GtkWidget *entry)
     gtk_entry_set_text (GTK_ENTRY (entry), "");
 }
 
-/* Show the popup menu when the third mouse button is pressed. */
-static gboolean
-Entry_Popup_Menu_Handler (GtkMenu *menu,
-                          GdkEventButton *event,
-                          gpointer user_data)
-{
-    if (event && (event->type == GDK_BUTTON_PRESS) && (event->button == 3))
-    {
-        gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button,
-                        event->time);
-        return TRUE;
-    }
-
-    return FALSE;
-}
-
-/*
+/* TODO: Support populate-all and do not assume the widget is a GtkMenu.
  * Popup menu attached to all entries of tag + filename + rename combobox.
  * Displayed when pressing the right mouse button and contains functions to process ths strings.
  */
 static void
-Attach_Popup_Menu_To_Tag_Entries (GtkEntry *entry)
+on_entry_populate_popup (GtkEntry *entry,
+                         GtkWidget *menu,
+                         gpointer user_data)
 {
-    GtkWidget *menu;
     GtkWidget *image;
     GtkWidget *menu_item;
 
-    menu = gtk_menu_new ();
-    g_signal_connect_swapped (entry, "button-press-event",
-                              G_CALLBACK (Entry_Popup_Menu_Handler),
-                              G_OBJECT (menu));
-
     /* Menu items */
     menu_item = gtk_image_menu_item_new_with_label (_("Tag selected files with this field"));
     image = gtk_image_new_from_icon_name ("go-jump", GTK_ICON_SIZE_MENU);
@@ -997,7 +977,8 @@ create_file_area (void)
     gtk_editable_set_editable (GTK_EDITABLE (FileEntry), TRUE);
     gtk_box_pack_start (GTK_BOX (hbox), FileEntry, TRUE, TRUE, 2);
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (FileEntry));
+    g_signal_connect (FileEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /*
      *  File Infos
@@ -1138,7 +1119,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this title"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (TitleEntry));
+    g_signal_connect (TitleEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Artist */
     priv->artist_label = gtk_label_new (_("Artist:"));
@@ -1157,7 +1139,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this artist"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (ArtistEntry));
+    g_signal_connect (ArtistEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Album Artist */
     priv->album_artist_label = gtk_label_new (_("Album artist:"));
@@ -1176,7 +1159,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this album artist"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (AlbumArtistEntry));
+    g_signal_connect (AlbumArtistEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Album */
     priv->album_label = gtk_label_new (_("Album:"));
@@ -1195,7 +1179,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this album name"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (AlbumEntry));
+    g_signal_connect (AlbumEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Disc Number */
     priv->disc_number_label = gtk_label_new (_("CD:"));
@@ -1217,7 +1202,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this disc number"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (DiscNumberEntry));
+    g_signal_connect (DiscNumberEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Year */
     priv->year_label = gtk_label_new (_("Year:"));
@@ -1367,7 +1353,9 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this genre"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (GenreCombo))));
+    g_signal_connect (gtk_bin_get_child (GTK_BIN (GenreCombo)),
+                      "populate-popup", G_CALLBACK (on_entry_populate_popup),
+                      NULL);
 
     /* Comment */
     priv->comment_label = gtk_label_new (_("Comment:"));
@@ -1386,7 +1374,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this comment"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (CommentEntry));
+    g_signal_connect (CommentEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Composer (name of the composers) */
     priv->composer_label = gtk_label_new (_("Composer:"));
@@ -1405,7 +1394,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this composer"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (ComposerEntry));
+    g_signal_connect (ComposerEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Translators: Original Artist / Performer. Please try to keep this string
      * as short as possible, as it must fit into a narrow column. */
@@ -1425,7 +1415,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this original artist"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (OrigArtistEntry));
+    g_signal_connect (OrigArtistEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
 
     /* Copyright */
@@ -1445,7 +1436,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this copyright"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (CopyrightEntry));
+    g_signal_connect (CopyrightEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
 
     /* URL */
@@ -1465,7 +1457,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this URL"));
 
-    Attach_Popup_Menu_To_Tag_Entries(GTK_ENTRY(URLEntry));
+    g_signal_connect (URLEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
 
     /* Encoded by */
@@ -1485,7 +1478,8 @@ create_tag_area (EtApplicationWindow *self)
                                      GTK_ENTRY_ICON_SECONDARY,
                                      _("Tag selected files with this encoder name"));
 
-    Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (EncodedByEntry));
+    g_signal_connect (EncodedByEntry, "populate-popup",
+                      G_CALLBACK (on_entry_populate_popup), NULL);
 
     /* Set focus chain. */
     focus_chain = g_list_prepend (focus_chain, TitleEntry);
@@ -1642,8 +1636,6 @@ create_tag_area (EtApplicationWindow *self)
     g_signal_connect (priv->apply_image_toolitem, "clicked",
                       G_CALLBACK (Mini_Button_Clicked), NULL);
 
-    /*Attach_Popup_Menu_To_Tag_Entries (GTK_ENTRY (PictureEntryView));*/
-
     gtk_widget_show_all (TagFrame);
     return TagFrame;
 }
diff --git a/src/browser.c b/src/browser.c
index e78f627..e10eae6 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -730,7 +730,8 @@ Browser_List_Button_Press (EtBrowser *self,
 {
     g_return_val_if_fail (event != NULL, FALSE);
 
-    if (event->type==GDK_2BUTTON_PRESS && event->button==1)
+    if (event->type == GDK_2BUTTON_PRESS
+        && event->button == GDK_BUTTON_PRIMARY)
     {
         /* Double left mouse click */
         // Select files of the same directory (useful when browsing sub-directories)
@@ -766,7 +767,9 @@ Browser_List_Button_Press (EtBrowser *self,
         g_free(path_ref);
         if (currentPath)
             gtk_tree_path_free(currentPath);
-    }else if (event->type==GDK_3BUTTON_PRESS && event->button==1)
+    }
+    else if (event->type == GDK_3BUTTON_PRESS
+             && event->button == GDK_BUTTON_PRIMARY)
     {
         /* Triple left mouse click, select all files of the list. */
         g_action_group_activate_action (G_ACTION_GROUP (MainWindow),
@@ -2795,10 +2798,11 @@ et_browser_set_sensitive (EtBrowser *self, gboolean sensitive)
  * Browser_Popup_Menu_Handler : displays the corresponding menu
  */
 static gboolean
-Browser_Popup_Menu_Handler (GtkWidget *widget, GdkEventButton *event,
+Browser_Popup_Menu_Handler (GtkWidget *widget,
+                            GdkEventButton *event,
                             GtkMenu *menu)
 {
-    if (event && (event->type == GDK_BUTTON_PRESS) && (event->button == 3))
+    if (gdk_event_triggers_context_menu ((GdkEvent *)event))
     {
         if (GTK_IS_TREE_VIEW (widget))
         {
@@ -2824,10 +2828,10 @@ Browser_Popup_Menu_Handler (GtkWidget *widget, GdkEventButton *event,
         gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button,
                        event->time);
 
-        return TRUE;
+        return GDK_EVENT_STOP;
     }
 
-    return FALSE;
+    return GDK_EVENT_PROPAGATE;
 }
 
 /*
diff --git a/src/cddb_dialog.c b/src/cddb_dialog.c
index 3f19850..d221be9 100644
--- a/src/cddb_dialog.c
+++ b/src/cddb_dialog.c
@@ -2881,13 +2881,13 @@ track_list_select_all (EtCDDBDialog *self)
 static gboolean
 on_track_list_button_press_event (EtCDDBDialog *self, GdkEventButton *event)
 {
-    g_return_val_if_fail (event != NULL, FALSE);
-
-    if (event->type == GDK_2BUTTON_PRESS && event->button == 1)
+    if (event->type == GDK_2BUTTON_PRESS
+        && event->button == GDK_BUTTON_PRIMARY)
     {
         /* Double left mouse click */
         track_list_select_all (self);
     }
+
     return FALSE;
 }
 
diff --git a/src/load_files_dialog.c b/src/load_files_dialog.c
index 1cb659f..b8bf46e 100644
--- a/src/load_files_dialog.c
+++ b/src/load_files_dialog.c
@@ -623,15 +623,18 @@ Load_Filename_Select_Row_In_Other_List (GtkWidget* treeview_target, gpointer ori
  * Create and attach a popup menu on the two clist of the LoadFileWindow
  */
 static gboolean
-Load_Filename_Popup_Menu_Handler (GtkWidget *treeview, GdkEventButton *event,
+Load_Filename_Popup_Menu_Handler (GtkWidget *treeview,
+                                  GdkEventButton *event,
                                   GtkMenu *menu)
 {
-    if (event && (event->type==GDK_BUTTON_PRESS) && (event->button==3))
+    if (gdk_event_triggers_context_menu ((GdkEvent *)event))
     {
-        gtk_menu_popup(menu,NULL,NULL,NULL,NULL,event->button,event->time);
-        return TRUE;
+        gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button,
+                        event->time);
+        return GDK_EVENT_STOP;
     }
-    return FALSE;
+
+    return GDK_EVENT_PROPAGATE;
 }
 
 static void
diff --git a/src/log.c b/src/log.c
index 1293704..74718d9 100644
--- a/src/log.c
+++ b/src/log.c
@@ -196,12 +196,14 @@ static gboolean
 Log_Popup_Menu_Handler (GtkWidget *treeview, GdkEventButton *event,
                         GtkMenu *menu)
 {
-    if (event && (event->type==GDK_BUTTON_PRESS) && (event->button == 3))
+    if (gdk_event_triggers_context_menu ((GdkEvent *)event))
     {
-        gtk_menu_popup(menu,NULL,NULL,NULL,NULL,event->button,event->time);
-        return TRUE;
+        gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button,
+                        event->time);
+        return GDK_EVENT_STOP;
     }
-    return FALSE;
+
+    return GDK_EVENT_PROPAGATE;
 }
 
 
diff --git a/src/picture.c b/src/picture.c
index 1abfbf7..e8e6121 100644
--- a/src/picture.c
+++ b/src/picture.c
@@ -1285,7 +1285,7 @@ et_picture_save_file_data (const Picture *pic, GFile *file, GError **error)
  */
 gboolean Picture_Entry_View_Button_Pressed (GtkTreeView *treeview, GdkEventButton *event, gpointer data)
 {
-    if (event->type == GDK_BUTTON_PRESS && event->button == 1)
+    if (event->type == GDK_BUTTON_PRESS && event->button == GDK_BUTTON_PRIMARY)
     {
         if (event->window == gtk_tree_view_get_bin_window (treeview))
         {
@@ -1297,7 +1297,8 @@ gboolean Picture_Entry_View_Button_Pressed (GtkTreeView *treeview, GdkEventButto
         }
     }
 
-    if (event->type==GDK_2BUTTON_PRESS && event->button==1)
+    if (event->type == GDK_2BUTTON_PRESS
+        && event->button == GDK_BUTTON_PRIMARY)
     {
         GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(PictureEntryView));
 


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