OSDN Git Service

Application of selection, ThreadView
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 25 Dec 2007 19:53:15 +0000 (04:53 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 25 Dec 2007 19:53:15 +0000 (04:53 +0900)
src/FukuiNoNamari/thread_view.py
src/FukuiNoNamari/thread_window.py

index 46a2911..f2fd89c 100644 (file)
@@ -64,6 +64,9 @@ class ElementEmpty:
 
         return width, y
 
+    def get_text(self, selection=False, start_index=0, end_index=0xffffff):
+        return ""
+
     def draw(self, drawingarea, y_offset, pango_layout,
              selection=False, start_index=0, end_index=0xffffff):
         pass
@@ -142,6 +145,28 @@ class ElementText:
 
         return current_line_x, current_line_y
 
+    def get_text(self, selection=False, start_index=0, end_index=0xffffff):
+
+        text = ""
+
+        for line in self.line_list:
+
+            t = self.text[line.start_index:line.end_index]
+            if selection:
+                s = start_index - line.start_index
+                s = max(s, 0)
+                s = min(s, line.end_index - line.start_index)
+
+                e = end_index - line.start_index
+                e = min(e, line.end_index - line.start_index)
+                e = max(e, 0)
+
+                t = t[s:e]
+
+            text += t
+
+        return text
+
     def draw(self, drawingarea, y_offset, pango_layout,
              selection=False, start_index=0, end_index=0xffffff):
 
@@ -262,6 +287,79 @@ class ResLayout:
     def get_pixel_size(self):
         return self.width, self.height
 
+    def get_text(self, selection_start, selection_end):
+        s_s = selection_start
+        e_s = selection_end
+        s_l, s_e, s_i = selection_start
+        e_l, e_e, e_i = selection_end
+
+        text = ""
+
+        if (s_l is None or s_e is None or s_i is None or
+            e_l is None or e_e is None or e_i is None or
+            self.posY < s_l.posY or self.posY > e_l.posY):
+
+            # nothing to do
+            pass
+
+        elif self.posY > s_s[0].posY and self.posY < e_s[0].posY:
+
+            for element in self.element_list:
+                text += element.get_text(selection=True)
+
+        elif self == s_s[0] and self == e_s[0]:
+
+            selection = False
+
+            for element in self.element_list:
+                if s_e == element:
+                    selection = True
+                    start = s_i
+                    end = 0xffffff
+                    if e_e == element:
+                        end = e_i
+                        selection = False
+                    text += element.get_text(selection=True, start_index=start,
+                                             end_index=end)
+                elif e_e == element:
+                    end = e_i
+                    selection = False
+                    text += element.get_text(
+                        selection=True, end_index=end)
+                elif selection:
+                    text += element.get_text(selection=True)
+
+        elif self == s_s[0]:
+
+            selection = False
+
+            for element in self.element_list:
+                if s_e == element:
+                    selection = True
+                    start = s_i
+                    text += element.get_text(selection=True, start_index=start)
+                elif selection:
+                    text += element.get_text(selection=True)
+
+        elif self == e_s[0]:
+
+            selection = True
+
+            for element in self.element_list:
+                if e_e == element:
+                    end = e_i
+                    text += element.get_text(selection=True, end_index=e_i)
+                    selection = False
+                elif selection:
+                    text += element.get_text(selection=True)
+
+        else:
+            # nothing to do
+            pass
+
+        return text
+
+
     def draw(self, drawingarea, x_offset, y_offset,
              start_selection, end_selection):
 
@@ -581,6 +679,29 @@ class ThreadView(gtk.HBox):
 
         return None, layout, None
 
+    def get_selected_text(self):
+        selection_start, selection_end = self._get_selection_start_end()
+        s_l, s_e, s_i = selection_start
+        e_l, e_e, e_i = selection_end
+
+        if (s_l is None or s_e is None or s_i is None or
+            e_l is None or e_e is None or e_i is None):
+            return ""
+
+        text = ""
+        index = s_l.list_index
+        end = e_l.list_index
+
+        while index <= end:
+            layout = self.res_layout_list[index]
+
+            text += layout.get_text(selection_start, selection_end)
+            if index != end:
+                text += "\n"
+
+            index += 1
+
+        return text
 
     def _set_button_pressed_pt(self, pt):
         self.button_pressed_pt = (None, None, None)
@@ -733,9 +854,15 @@ class ThreadView(gtk.HBox):
                 self.menu_openuri.uri = None
                 self.menu_copylinkaddress.uri = None
 
-            self.menu_copyselection.hide()
-            self.menu_openasuri.hide()
-            self.menu_separator_selection.hide()
+            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)
             return True
index 3cc3f32..5e91f7a 100644 (file)
@@ -290,10 +290,14 @@ class WinWrap(winwrapbase.WinWrapBase):
         clip.set_text(widget.uri, len(widget.uri))
 
     def on_popup_threadview_menu_copyselection_activate(self, widget):
-        pass
+        text = self.threadview.get_selected_text()
+        if text and len(text) > 0:
+            clip = gtk.Clipboard()
+            text = text.encode("utf8")
+            clip.set_text(text, len(text))
 
     def on_popup_threadview_menu_openasuri_activate(self, widget):
-        pass
+        self.on_threadview_uri_clicked(self.threadview.get_selected_text())
 
     def on_popup_threadview_menu_refresh_activate(self, widget):
         self.update(widget)