[gnote/stable-0.6] Correctly position the context menu



commit e9066ee5bd3277aa4f779bbd318903902ab063eb
Author: Aurimas Ä?ernius <aurisc4 gmail com>
Date:   Tue Nov 10 23:31:20 2009 +0200

    Correctly position the context menu
    
    Pressing the context menu key in the notebook or notes list places the
    menu at the top-left corner of the screen. It should be placed on the
    selected row of the focussed TreeView.
    
    Fixes: https://bugzilla.gnome.org/601459
    
    Signed-off-by: Debarshi Ray <debarshir src gnome org>
    (cherry picked from commit aa93949855d808c281bdc33239203c71592e5267)

 src/recentchanges.cpp |   71 ++++++++++++++++--------------------------------
 src/recentchanges.hpp |    1 -
 2 files changed, 24 insertions(+), 48 deletions(-)
---
diff --git a/src/recentchanges.cpp b/src/recentchanges.cpp
index 5cefcc5..a590c32 100644
--- a/src/recentchanges.cpp
+++ b/src/recentchanges.cpp
@@ -902,67 +902,44 @@ namespace gnote {
   // the context menu with the keyboard.
   void NoteRecentChanges::position_context_menu(int & x, int & y, bool & push_in)
   {
-    Gtk::TreeIter iter;
-    Gtk::TreePath dest_path;
-    Glib::RefPtr<Gtk::TreeSelection> selection;
-
     // Set default "return" values
     push_in = false; // not used
     x = 0;
     y = 0;
 
-    selection = m_tree->get_selection();
-    iter = selection->get_selected();
-    if (!iter)
+    Gtk::Widget * const focus_widget = this->get_focus();
+    if (!focus_widget)
       return;
+    focus_widget->get_window()->get_origin(x, y);
 
-    dest_path = m_store_sort->get_path (iter);
+    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);
 
-    int pos_x = 0;
-    int pos_y = 0;
+    const Glib::RefPtr<Gtk::TreeSelection> selection
+                                             = tree->get_selection();
+    const std::list<Gtk::TreePath> selected_rows
+                                     = selection->get_selected_rows();
+    if (selected_rows.empty())
+      return;
 
-    get_widget_screen_pos (*m_tree, pos_x, pos_y);
+    const Gtk::TreePath & dest_path = selected_rows.front();
+    const std::list<Gtk::TreeViewColumn *> columns
+                                             = tree->get_columns();
     Gdk::Rectangle cell_rect;
-    m_tree->get_cell_area (dest_path, **m_tree->get_columns().begin(), cell_rect);
+    tree->get_cell_area (dest_path, *columns.front(), cell_rect);
 
-    // Add 100 to x so it's not be at the far left
-    x = pos_x + cell_rect.get_x() + 100;
-    y = pos_y + cell_rect.get_y();
+    x += cell_rect.get_x();
+    y += cell_rect.get_y();
   }
 
 
-  // Walk the widget hiearchy to figure out
-  // where to position the context menu.
-  void NoteRecentChanges::get_widget_screen_pos(Gtk::Widget & widget, int & x, int & y)
-  {
-    int widget_x;
-    int widget_y;
-    Gtk::Window *win;
-
-    win = dynamic_cast<Gtk::Window*>(&widget);
-    if (win) {
-      win->get_position (widget_x, widget_y);
-    } 
-    else {
-      get_widget_screen_pos(*widget.get_parent(), x, y);
-
-      // Special case the TreeView because it adds
-      // too much since it's in a scrolled window.
-      if (&widget == m_tree) {
-        widget_x = 2;
-        widget_y = 2;
-      } 
-      else {
-        Gdk::Rectangle widget_rect = widget.get_allocation();
-        widget_x = widget_rect.get_x();
-        widget_y = widget_rect.get_y();
-      }
-    }
-
-    x += widget_x;
-    y += widget_y;
-  }
-
   Note::List NoteRecentChanges::get_selected_notes()
   {
     Note::List selected_notes;
diff --git a/src/recentchanges.hpp b/src/recentchanges.hpp
index 62beb07..408d6f4 100644
--- a/src/recentchanges.hpp
+++ b/src/recentchanges.hpp
@@ -99,7 +99,6 @@ private:
   bool on_treeview_button_released(GdkEventButton *);  
   void popup_context_menu_at_location(Gtk::Menu *, int, int);
   void position_context_menu(int & x, int & y, bool & push_in);
-  void get_widget_screen_pos(Gtk::Widget &, int &, int &);
   Note::List get_selected_notes();
   Note::Ptr get_note(const Gtk::TreePath & p);
   void on_open_note();



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