OSDN Git Service

Popup menu for ThreadView is not prepared by the glade xml. (#16526)
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 4 May 2009 12:16:52 +0000 (21:16 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Mon, 4 May 2009 12:16:52 +0000 (21:16 +0900)
src/FukuiNoNamari/thread_popup.py
src/FukuiNoNamari/thread_view.py
src/FukuiNoNamari/thread_window.py
src/data/thread_window.glade

index 3e14983..8459eca 100644 (file)
@@ -6,6 +6,13 @@ import urlparse
 import copy
 import thread_view
 
+
+class ThreadViewForPopup(thread_view.ThreadView):
+
+    # no popup
+    def _do_popup(self, x, y, button, time): pass
+
+
 class ThreadPopup(gobject.GObject):
     '''
     classdocs
@@ -128,7 +135,7 @@ class ThreadPopup(gobject.GObject):
  
         popupwin = gtk.Window(gtk.WINDOW_POPUP)
 
-        view = thread_view.ThreadView()
+        view = ThreadViewForPopup()
         popupwin.add(thread_view.ThreadViewContainer(view))
         view.connect("uri-clicked-event", self.on_threadview_uri_clicked)
         self.push_thread_view(view)
index 658c689..f06e3bf 100644 (file)
@@ -520,6 +520,9 @@ class ThreadView(gtk.DrawingArea):
         "set-scroll-adjustments" : (gobject.SIGNAL_RUN_LAST,
                                    gobject.TYPE_NONE,
                                    (gtk.Adjustment, gtk.Adjustment)),
+        "populate-popup": (gobject.SIGNAL_RUN_LAST,
+                           gobject.TYPE_NONE,
+                           (gtk.Menu, )),
         "configure-event": "override",
         "expose-event": "override",
         "motion-notify-event": "override",
@@ -555,16 +558,6 @@ class ThreadView(gtk.DrawingArea):
 
         self.button1_pressed = False
         self.current_pressed_uri = None
-            
-        self.popupmenu = None
-        self.menu_openuri = None
-        self.menu_copylinkaddress = None
-        self.menu_separator_link = None
-        self.menu_copyselection = None
-        self.menu_openasuri = None
-        self.menu_separator_selection = None
-
-        self.menud_uri = None
 
         # for selection
         self.button_pressed_pt = (None, None, None)
@@ -830,6 +823,45 @@ class ThreadView(gtk.DrawingArea):
 
         self.button_moving_pt = (layout, element, index)
 
+    def _copy_text_to_clipboard(self, text):
+        if text and len(text) > 0:
+            clip = gtk.Clipboard()
+            text = text.encode("utf8")
+            clip.set_text(text, len(text))
+
+    def _do_popup(self, x, y, button, time):
+        selection = self.get_selected_text()
+        uri, layout, element = self.ptrpos_to_uri(x, y)
+
+        menu = gtk.Menu()
+        if uri is not None and len(uri) > 0:
+            menuitem = gtk.MenuItem("Open _Link")
+            menuitem.connect("activate", self.on_open_uri_activated, uri)
+            menu.append(menuitem)
+
+            menuitem = gtk.MenuItem("Copy Link _Address")
+            menuitem.connect("activate", self.on_copy_uri_activated, uri)
+            menu.append(menuitem)
+
+            menu.append(gtk.SeparatorMenuItem())
+
+        if selection is not None and len(selection) > 0:
+            menuitem = gtk.ImageMenuItem(gtk.STOCK_COPY)
+            menuitem.connect("activate",
+                             self.on_copy_selection_activated, selection)
+            menu.append(menuitem)
+
+            menuitem = gtk.MenuItem("Open _Selected as URL")
+            menuitem.connect("activate",
+                             self.on_open_as_uri_activated, selection)
+            menu.append(menuitem)
+
+            menu.append(gtk.SeparatorMenuItem())
+
+        self.emit("populate-popup", menu)
+        menu.show_all()
+        menu.popup(None, None, None, button, time)
+
     def do_expose_event(self, event):
         self.draw_viewport(event.area)
 
@@ -899,34 +931,8 @@ class ThreadView(gtk.DrawingArea):
                 self.queue_draw()
                 
         elif event.button == 3:
-            time = event.time
-            uri, layout, element = self.ptrpos_to_uri(event.x, event.y)
-            if uri is not None and layout is not None and element is not None:
-                self.menu_openuri.show()
-                self.menu_copylinkaddress.show()
-                self.menu_separator_link.show()
-                self.menu_openuri.uri = uri
-                self.menu_copylinkaddress.uri = uri
-            else:
-                self.menu_openuri.hide()
-                self.menu_copylinkaddress.hide()
-                self.menu_separator_link.hide()
-                self.menu_openuri.uri = None
-                self.menu_copylinkaddress.uri = None
-
-            text = self.get_selected_text()
-            if text and len(text) > 0:
-                self.menu_copyselection.show()
-                self.menu_openasuri.show()
-                self.menu_separator_selection.show()
-            else:
-                self.menu_copyselection.hide()
-                self.menu_openasuri.hide()
-                self.menu_separator_selection.hide()
-
-            self.popupmenu.popup(None, None, None, event.button, time)
+            self._do_popup(event.x, event.y, event.button, event.time)
             return True
-            
 
     def do_button_release_event(self, event):
         if event.button == 1:
@@ -984,6 +990,20 @@ class ThreadView(gtk.DrawingArea):
             self.adjustment.connect(
                 "value-changed", self.on_adjustment_value_changed)
 
+    def on_copy_uri_activated(self, widget, uri):
+        self._copy_text_to_clipboard(uri)
+
+    def on_open_uri_activated(self, widget, uri):
+        self.emit("uri-clicked-event", uri)
+
+    def on_copy_selection_activated(self, widget, selection):
+        self._copy_text_to_clipboard(selection)
+
+    def on_open_as_uri_activated(self, widget, selection):
+        if not selection.startswith("http://"):
+            selection = "http://" + selection
+        self.emit("uri-clicked-event", selection)
+
 
 class ThreadViewScrollbar(gtk.VScrollbar):
 
index 7d93d13..3ec0869 100644 (file)
@@ -156,8 +156,8 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.vbox.reorder_child(container, 2)
         self.window.set_focus(self.threadview)
 
-        self._get_popupmenu_widgets()
-
+        self.threadview.connect(
+            "populate-popup", self.on_thread_view_populate_popup)
         self.threadview.connect(
             "uri-clicked-event", self.on_thread_view_uri_clicked)
         self.threadpopup.connect(
@@ -180,22 +180,6 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.statusbar = self.widget_tree.get_widget("statusbar")
         self.vbox = self.widget_tree.get_widget("vbox")
 
-    def _get_popupmenu_widgets(self):
-        self.threadview.popupmenu = self.widget_tree.get_widget(
-            "popup_threadview_menu")
-        self.threadview.menu_openuri = self.widget_tree.get_widget(
-            "popup_threadview_menu_openuri")
-        self.threadview.menu_copylinkaddress = self.widget_tree.get_widget(
-            "popup_threadview_menu_copylinkaddress")
-        self.threadview.menu_separator_link = self.widget_tree.get_widget(
-            "popup_threadview_menu_separator_link")
-        self.threadview.menu_copyselection = self.widget_tree.get_widget(
-            "popup_threadview_menu_copyselection")
-        self.threadview.menu_openasuri = self.widget_tree.get_widget(
-            "popup_threadview_menu_openasuri")
-        self.threadview.menu_separator_selection = self.widget_tree.get_widget(
-            "popup_threadview_menu_separator_selection")
-
     def initialize_buffer(self):
         self.threadview.initialize_buffer()
 
@@ -649,6 +633,12 @@ class WinWrap(winwrapbase.WinWrapBase):
 
     # signal handlers
     
+    def on_thread_view_populate_popup(self, widget, menu):
+        menuitem = gtk.ImageMenuItem(gtk.STOCK_REFRESH)
+        menuitem.connect("activate",
+                         self.on_popup_threadview_menu_refresh_activate)
+        menu.append(menuitem)
+
     def on_thread_view_uri_clicked(self, widget, uri):
         self._open_uri(uri)
 
@@ -719,20 +709,5 @@ class WinWrap(winwrapbase.WinWrapBase):
 
     # popup menus
     
-    def on_popup_threadview_menu_openuri_activate(self, widget):
-        self._open_uri(widget.uri)
-
-    def on_popup_threadview_menu_copylinkaddress_activate(self, widget):
-        self._copy_text_to_clipboard(widget.uri)
-
-    def on_popup_threadview_menu_copyselection_activate(self, widget):
-        text = self.threadview.get_selected_text()
-        self._copy_text_to_clipboard(text)
-
-    def on_popup_threadview_menu_openasuri_activate(self, widget):
-        text = self.threadview.get_selected_text()
-        uri = self._modify_uri(text)
-        self._open_uri(uri)
-
     def on_popup_threadview_menu_refresh_activate(self, widget):
         self.update()
index aa50147..bcd4482 100644 (file)
       </widget>
     </child>
   </widget>
-  <widget class="GtkMenu" id="popup_threadview_menu">
-    <property name="visible">True</property>
-    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-    <child>
-      <widget class="GtkImageMenuItem" id="popup_threadview_menu_openuri">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-        <property name="label" translatable="yes">_Open Link</property>
-        <property name="use_underline">True</property>
-        <signal name="activate" handler="on_popup_threadview_menu_openuri_activate"/>
-      </widget>
-    </child>
-    <child>
-      <widget class="GtkMenuItem" id="popup_threadview_menu_copylinkaddress">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-        <property name="label" translatable="yes">_Copy Link Address</property>
-        <property name="use_underline">True</property>
-        <signal name="activate" handler="on_popup_threadview_menu_copylinkaddress_activate"/>
-      </widget>
-    </child>
-    <child>
-      <widget class="GtkSeparatorMenuItem" id="popup_threadview_menu_separator_link">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-      </widget>
-    </child>
-    <child>
-      <widget class="GtkImageMenuItem" id="popup_threadview_menu_copyselection">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-        <property name="label" translatable="yes">gtk-copy</property>
-        <property name="use_underline">True</property>
-        <property name="use_stock">True</property>
-        <signal name="activate" handler="on_popup_threadview_menu_copyselection_activate"/>
-      </widget>
-    </child>
-    <child>
-      <widget class="GtkMenuItem" id="popup_threadview_menu_openasuri">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-        <property name="label" translatable="yes">Open Selection as URL</property>
-        <property name="use_underline">True</property>
-        <signal name="activate" handler="on_popup_threadview_menu_openasuri_activate"/>
-      </widget>
-    </child>
-    <child>
-      <widget class="GtkSeparatorMenuItem" id="popup_threadview_menu_separator_selection">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-      </widget>
-    </child>
-    <child>
-      <widget class="GtkImageMenuItem" id="popup_threadview_menu_refresh">
-        <property name="visible">True</property>
-        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
-        <property name="label" translatable="yes">gtk-refresh</property>
-        <property name="use_underline">True</property>
-        <property name="use_stock">True</property>
-        <signal name="activate" handler="on_popup_threadview_menu_refresh_activate"/>
-      </widget>
-    </child>
-  </widget>
 </glade-interface>