[gnote/gnome-3-38] Do not use deprecated Gtk::Menu::popup



commit b18d4b324c17bed80830f1c176dbc67b1fbc2f5b
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Nov 15 15:09:57 2020 +0200

    Do not use deprecated Gtk::Menu::popup
    
    Fixes https://gitlab.gnome.org/GNOME/gnote/-/issues/16

 src/searchnoteswidget.cpp | 92 ++++++++++++++++++++---------------------------
 src/searchnoteswidget.hpp |  5 ++-
 2 files changed, 41 insertions(+), 56 deletions(-)
---
diff --git a/src/searchnoteswidget.cpp b/src/searchnoteswidget.cpp
index de76c330..760b78a6 100644
--- a/src/searchnoteswidget.cpp
+++ b/src/searchnoteswidget.cpp
@@ -425,7 +425,7 @@ bool SearchNotesWidget::on_notebooks_tree_button_pressed(GdkEventButton *ev)
     }
 
     Gtk::Menu *menu = get_notebook_list_context_menu();
-    popup_context_menu_at_location(menu, ev->x, ev->y);
+    popup_context_menu_at_location(menu, reinterpret_cast<GdkEvent*>(ev));
     return true;
   }
   return false;
@@ -440,7 +440,7 @@ bool SearchNotesWidget::on_notebooks_key_pressed(GdkEventKey *ev)
   case GDK_KEY_Menu:
   {
     Gtk::Menu *menu = get_notebook_list_context_menu();
-    popup_context_menu_at_location(menu, 0, 0);
+    popup_context_menu_at_location(menu, reinterpret_cast<GdkEvent*>(ev));
     break;
   }
   default:
@@ -540,18 +540,46 @@ void SearchNotesWidget::update_results()
   }
 }
 
-void SearchNotesWidget::popup_context_menu_at_location(Gtk::Menu *menu, int x, int y)
+void SearchNotesWidget::popup_context_menu_at_location(Gtk::Menu *menu, GdkEvent *trigger_event)
 {
   menu->show_all();
+  Glib::RefPtr<Gdk::Window> rect_window;
+  Gdk::Rectangle cell_rect;
+  if(trigger_event->type != GDK_BUTTON_PRESS) {
+    Gtk::Window *parent = get_owning_window();
+    if(parent) {
+      Gtk::Widget *focus_widget = parent->get_focus();
+      if(focus_widget) {
+        int x, y;
+        focus_widget->get_window()->get_origin(x, y);
+
+        Gtk::TreeView *tree = dynamic_cast<Gtk::TreeView*>(focus_widget);
+        if(tree) {
+          rect_window = tree->get_bin_window();
+          if(rect_window) {
+            rect_window->get_origin(x, y);
+          }
+
+          const Glib::RefPtr<Gtk::TreeSelection> selection = tree->get_selection();
+          const std::vector<Gtk::TreePath> selected_rows = selection->get_selected_rows();
+          if(selected_rows.empty()) {
+            rect_window.reset();
+          }
+          else {
+            const Gtk::TreePath & dest_path = selected_rows.front();
+            const std::vector<Gtk::TreeViewColumn*> columns = tree->get_columns();
+            tree->get_cell_area(dest_path, *columns.front(), cell_rect);
+          }
+        }
+      }
+    }
+  }
 
-  // Set up the funtion to position the context menu
-  // if we were called by the keyboard Gdk.Key.Menu.
-  if(x == 0 && y == 0) {
-    menu->popup(sigc::mem_fun(*this, &SearchNotesWidget::position_context_menu),
-                0, gtk_get_current_event_time());
+  if(rect_window) {
+    menu->popup_at_rect(rect_window, cell_rect, Gdk::GRAVITY_NORTH_WEST, Gdk::GRAVITY_NORTH_WEST, 
trigger_event);
   }
   else {
-    menu->popup(0, gtk_get_current_event_time());
+    menu->popup_at_pointer(trigger_event);
   }
 }
 
@@ -707,48 +735,6 @@ void SearchNotesWidget::select_notes(const Note::List & notes)
   } while(++iter);
 }
 
-void SearchNotesWidget::position_context_menu(int & x, int & y, bool & push_in)
-{
-  // Set default "return" values
-  push_in = false; // not used
-  x = 0;
-  y = 0;
-
-  Gtk::Window *parent = get_owning_window();
-  if(!parent) {
-    return;
-  }
-  Gtk::Widget * const focus_widget = parent->get_focus();
-  if(!focus_widget) {
-    return;
-  }
-  focus_widget->get_window()->get_origin(x, y);
-
-  Gtk::TreeView * const tree = dynamic_cast<Gtk::TreeView*>(focus_widget);
-  if(!tree) {
-    return;
-  }
-  const Glib::RefPtr<Gdk::Window> tree_area = tree->get_bin_window();
-  if(!tree_area) {
-    return;
-  }
-  tree_area->get_origin(x, y);
-
-  const Glib::RefPtr<Gtk::TreeSelection> selection = tree->get_selection();
-  const std::vector<Gtk::TreePath> selected_rows = selection->get_selected_rows();
-  if(selected_rows.empty()) {
-    return;
-  }
-
-  const Gtk::TreePath & dest_path = selected_rows.front();
-  const std::vector<Gtk::TreeViewColumn*> columns = tree->get_columns();
-  Gdk::Rectangle cell_rect;
-  tree->get_cell_area(dest_path, *columns.front(), cell_rect);
-
-  x += cell_rect.get_x();
-  y += cell_rect.get_y();
-}
-
 Note::Ptr SearchNotesWidget::get_note(const Gtk::TreePath & p)
 {
   Gtk::TreeIter iter = m_store_sort->get_iter(p);
@@ -860,7 +846,7 @@ bool SearchNotesWidget::on_treeview_button_pressed(GdkEventButton *ev)
         }
       }
       Gtk::Menu *menu = get_note_list_context_menu();
-      popup_context_menu_at_location(menu, ev->x, ev->y);
+      popup_context_menu_at_location(menu, reinterpret_cast<GdkEvent*>(ev));
 
       // Return true so that the base handler won't
       // run, which causes the selection to change to
@@ -955,7 +941,7 @@ bool SearchNotesWidget::on_treeview_key_pressed(GdkEventKey * ev)
     // Pop up the context menu if a note is selected
     Note::List selected_notes = get_selected_notes();
     if(!selected_notes.empty()) {
-      popup_context_menu_at_location(menu, 0, 0);
+      popup_context_menu_at_location(menu, reinterpret_cast<GdkEvent*>(ev));
     }
     break;
   }
diff --git a/src/searchnoteswidget.hpp b/src/searchnoteswidget.hpp
index d9f4ab3f..d58e1ebd 100644
--- a/src/searchnoteswidget.hpp
+++ b/src/searchnoteswidget.hpp
@@ -1,7 +1,7 @@
 /*
  * gnote
  *
- * Copyright (C) 2010-2015,2017,2019 Aurimas Cernius
+ * Copyright (C) 2010-2015,2017,2019-2020 Aurimas Cernius
  * Copyright (C) 2010 Debarshi Ray
  * Copyright (C) 2009 Hubert Figuiere
  *
@@ -81,14 +81,13 @@ private:
   bool on_notebooks_key_pressed(GdkEventKey *);
   notebooks::Notebook::Ptr get_selected_notebook() const;
   void update_results();
-  void popup_context_menu_at_location(Gtk::Menu *, int, int);
+  void popup_context_menu_at_location(Gtk::Menu*, GdkEvent*);
   Note::List get_selected_notes();
   bool filter_notes(const Gtk::TreeIter &);
   int compare_titles(const Gtk::TreeIter &, const Gtk::TreeIter &);
   int compare_dates(const Gtk::TreeIter &, const Gtk::TreeIter &);
   void make_recent_tree();
   void select_notes(const Note::List &);
-  void position_context_menu(int & x, int & y, bool & push_in);
   Note::Ptr get_note(const Gtk::TreePath & p);
   bool filter_by_search(const Note::Ptr &);
   bool filter_by_tag(const Note::Ptr &);


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