OSDN Git Service

ThreadView is split to View(ThreadView) and Container(ThreadViewContainer).
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 3 May 2009 17:04:02 +0000 (02:04 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 3 May 2009 17:04:02 +0000 (02:04 +0900)
ThreadViewContainer can be replaced with GtkScrolledWindow.

src/FukuiNoNamari/thread_popup.py
src/FukuiNoNamari/thread_view.py
src/FukuiNoNamari/thread_window.py

index 95a7091..3e14983 100644 (file)
@@ -129,11 +129,12 @@ class ThreadPopup(gobject.GObject):
         popupwin = gtk.Window(gtk.WINDOW_POPUP)
 
         view = thread_view.ThreadView()
+        popupwin.add(thread_view.ThreadViewContainer(view))
         view.connect("uri-clicked-event", self.on_threadview_uri_clicked)
         self.push_thread_view(view)
 
         # set width
-        view.drawingarea.size_allocate(gtk.gdk.Rectangle(0, 0, 300, 200))
+        view.size_allocate(gtk.gdk.Rectangle(0, 0, 300, 200))
         view.adjustment.page_size = 200
 
         for num in numlist:
@@ -146,15 +147,14 @@ class ThreadPopup(gobject.GObject):
                     view.add_layout(clone)
         popupwin.view = view
         popupwin.uri = uri
-        popupwin.add(view)
         popupwin.set_property("border_width", ThreadPopup._BORDER_WIDTH)
 
-        view.drawingarea.size_allocate(
+        view.size_allocate(
             gtk.gdk.Rectangle(0, 0, 300, int(min(200, view.adjustment.upper))))
 
-        popupwin.set_default_size(view.drawingarea.allocation.width
+        popupwin.set_default_size(view.allocation.width
                                   +ThreadPopup._BORDER_WIDTH*2,
-                                  view.drawingarea.allocation.height
+                                  view.allocation.height
                                   +ThreadPopup._BORDER_WIDTH*2)
 
         # show the popup at an appropriate position.
index 636b73e..658c689 100644 (file)
@@ -511,12 +511,22 @@ class ResLayout:
         return layout
 
 
-class ThreadView(gtk.HBox):
+class ThreadView(gtk.DrawingArea):
     __gsignals__ = {
         "cursor-over-link-event":
         (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, object, )),
         "uri-clicked-event":
-        (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, ))
+        (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (object, )),
+        "set-scroll-adjustments" : (gobject.SIGNAL_RUN_LAST,
+                                   gobject.TYPE_NONE,
+                                   (gtk.Adjustment, gtk.Adjustment)),
+        "configure-event": "override",
+        "expose-event": "override",
+        "motion-notify-event": "override",
+        "button-press-event": "override",
+        "button-release-event": "override",
+        "style-set": "override",
+        "key-press-event": "override"
         }
 
     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
@@ -524,17 +534,13 @@ class ThreadView(gtk.HBox):
     arrow_cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
 
     def __init__(self):
-        gtk.HBox.__init__(self, False, 0)
-        self.drawingarea = gtk.DrawingArea()
-        self.drawingarea.set_property("can_focus", True)
+        super(ThreadView, self).__init__()
+        self.set_set_scroll_adjustments_signal("set-scroll-adjustments")
+        self.set_property("can_focus", True)
 
-        self.vscrollbar = gtk.VScrollbar()
-        self.vscrollbar.set_property("no-show-all", True)
-        self.pack_start(self.drawingarea)
-        self.pack_start(self.vscrollbar, expand=False)
-        self.adjustment  = self.vscrollbar.get_adjustment()
+        #self.adjustment = gtk.Adjustment()
 
-        self.drawingarea.add_events(
+        self.add_events(
             gtk.gdk.KEY_PRESS_MASK |
             gtk.gdk.SCROLL_MASK |
             gtk.gdk.POINTER_MOTION_MASK |
@@ -543,28 +549,7 @@ class ThreadView(gtk.HBox):
 
         self.drawingarea_prev_width = 0
 
-        self.drawingarea.connect(
-            "expose-event", self.on_drawingarea_expose_event)
-        self.drawingarea.connect(
-            "configure-event", self.on_drawingarea_configure_event)
-        self.drawingarea.connect(
-            "scroll-event", self.on_drawingarea_scroll_event)
-        self.drawingarea.connect(
-            "motion-notify-event", self.on_drawingrarea_motion_notify_event)
-        self.drawingarea.connect(
-            "button-press-event", self.on_drawingarea_button_press_event)
-        self.drawingarea.connect(
-            "button-release-event", self.on_drawingarea_button_release_event)
-        self.drawingarea.connect(
-            "style-set", self.on_drawingarea_style_set)
-        self.drawingarea.connect(
-            "key-press-event", self.on_drawingarea_key_press_event)
-        self.adjustment.connect(
-            "value-changed", self.on_adjustment_value_changed)
-        self.adjustment.connect(
-            "changed", self.on_adjustment_changed)
-
-        self.pango_layout = self.drawingarea.create_pango_layout("")
+        self.pango_layout = self.create_pango_layout("")
 
         self.initialize_buffer()
 
@@ -606,11 +591,11 @@ class ThreadView(gtk.HBox):
         return ResLayout(left_margin, resnum, self.pango_layout)
 
     def set_layout_width(self, layout):
-        width = self.drawingarea.allocation.width
+        width = self.allocation.width
         layout.set_width(width)
         
     def redraw(self):
-        self.drawingarea.queue_draw()
+        self.queue_draw()
 
     def wrap_relayout(self):
         # before relayout, find top layout on gdkwindow
@@ -621,14 +606,14 @@ class ThreadView(gtk.HBox):
             delta = top_layout.posY - self.adjustment.value
 
         self.relayout()
-        self.drawingarea_prev_width = self.drawingarea.allocation.width
+        self.drawingarea_prev_width = self.allocation.width
 
         # after relayout, set adjustment.value to top layout's posY
         if top_layout is not None:
             self.adjustment.value = top_layout.posY - delta
 
     def relayout(self):
-        width = self.drawingarea.allocation.width
+        width = self.allocation.width
         sum_height = 0
         for layout in self.res_layout_list:
             layout.set_width(width)
@@ -638,12 +623,6 @@ class ThreadView(gtk.HBox):
 
         self.adjustment.upper = sum_height
 
-    def change_vscrollbar_visible(self):
-        if self.adjustment.upper <= self.adjustment.page_size:
-            self.vscrollbar.hide()
-        else:
-            self.vscrollbar.show()
-
     def _set_adjustment_value(self, value):
         value = min(self.adjustment.upper - self.adjustment.page_size, value)
         value = max(self.adjustment.lower, value)
@@ -694,8 +673,8 @@ class ThreadView(gtk.HBox):
 
     def draw_viewport(self, area):
         view_y = self.adjustment.value
-        self.drawingarea.window.draw_rectangle(
-            self.drawingarea.style.base_gc[0],
+        self.window.draw_rectangle(
+            self.style.base_gc[0],
             True, area.x, area.y, area.width, area.height)
 
         selection_start, selection_end = self._get_selection_start_end()
@@ -710,7 +689,7 @@ class ThreadView(gtk.HBox):
         iter = itertools.imap(lambda index: self.res_layout_list[index], iter)
         iter = itertools.takewhile(lambda lay: lay.posY <= area_bottom, iter)
         for layout in iter:
-            layout.draw(self.drawingarea, 0, layout.posY - int(view_y),
+            layout.draw(self, 0, layout.posY - int(view_y),
                 selection_start, selection_end)
 
     def transform_coordinate_gdk_to_adj(self, y):
@@ -851,30 +830,24 @@ class ThreadView(gtk.HBox):
 
         self.button_moving_pt = (layout, element, index)
 
-    def on_drawingarea_expose_event(self, widget, event, data=None):
+    def do_expose_event(self, event):
         self.draw_viewport(event.area)
 
-    def on_drawingarea_configure_event(self, widget, event, data=None):
+    def do_configure_event(self, event):
         if event.width != self.drawingarea_prev_width:
             self.wrap_relayout()
 
-        self.adjustment.page_size = self.drawingarea.allocation.height
+        self.adjustment.page_size = self.allocation.height
         self.adjustment.step_increment = 20
-        self.adjustment.page_increment = self.drawingarea.allocation.height
+        self.adjustment.page_increment = self.allocation.height
 
         # re-set 'value' for prevent overflow
         self._set_adjustment_value(self.adjustment.value)
 
     def on_adjustment_value_changed(self, widget, data=None):
-        self.drawingarea.queue_draw()
-
-    def on_adjustment_changed(self, widget, data=None):
-        self.change_vscrollbar_visible()
+        self.queue_draw()
 
-    def on_drawingarea_scroll_event(self, widget, event, data=None):
-        self.vscrollbar.emit("scroll-event", event)
-
-    def on_drawingrarea_motion_notify_event(self, widget, event, data=None):
+    def do_motion_notify_event(self, event):
         if event.state & gtk.gdk.BUTTON1_MASK != gtk.gdk.BUTTON1_MASK:
             self.button1_pressed = False
 
@@ -898,8 +871,8 @@ class ThreadView(gtk.HBox):
 
                 y -= view_y
 
-                self.drawingarea.queue_draw_area(0, y, n_width, height+1)
-                #self.drawingarea.window.process_updates(False)
+                self.queue_draw_area(0, y, n_width, height+1)
+                #self.window.process_updates(False)
 
         cursor = ThreadView.regular_cursor
 
@@ -911,9 +884,9 @@ class ThreadView(gtk.HBox):
                 cursor = ThreadView.hand_cursor
                 self.emit("cursor-over-link-event", event, uri)
 
-        self.drawingarea.window.set_cursor(cursor)
+        self.window.set_cursor(cursor)
 
-    def on_drawingarea_button_press_event(self, widget, event, data=None):
+    def do_button_press_event(self, event):
         if event.button == 1:
             self.current_pressed_uri = None
             self.button1_pressed = True
@@ -923,7 +896,7 @@ class ThreadView(gtk.HBox):
             else:
                 self._set_button_moving_pt((event.x, event.y))
                 self._set_button_pressed_pt((event.x, event.y))
-                self.drawingarea.queue_draw()
+                self.queue_draw()
                 
         elif event.button == 3:
             time = event.time
@@ -955,7 +928,7 @@ class ThreadView(gtk.HBox):
             return True
             
 
-    def on_drawingarea_button_release_event(self, widget, event, data=None):
+    def do_button_release_event(self, event):
         if event.button == 1:
             button1_pressed = self.button1_pressed
             self.button1_pressed = False
@@ -968,18 +941,18 @@ class ThreadView(gtk.HBox):
                     element == p_element):
                     self.emit("uri-clicked-event", uri)
 
-    def on_drawingarea_style_set(self, widget, previous_style, data=None):
+    def do_style_set(self, previous_style):
         if previous_style is None:
             return False
 
-        new = widget.style.font_desc.hash()
+        new = self.style.font_desc.hash()
         old = previous_style.font_desc.hash()
         if new != old:
             for layout in self.res_layout_list:
                 layout.recalc_char_widths()
             self.wrap_relayout()
 
-    def on_drawingarea_key_press_event(self, widget, event, data=None):
+    def do_key_press_event(self, event):
         if event.type is not gtk.gdk.KEY_PRESS:
             return
 
@@ -1004,3 +977,44 @@ class ThreadView(gtk.HBox):
             self.jump(value)
         elif event.keyval == gtk.keysyms.End:
             self.jump_to_the_end()
+
+    def do_set_scroll_adjustments(self, hadjustment, vadjustment):
+        self.adjustment = vadjustment
+        if self.adjustment is not None:
+            self.adjustment.connect(
+                "value-changed", self.on_adjustment_value_changed)
+
+
+class ThreadViewScrollbar(gtk.VScrollbar):
+
+    def __init__(self, adjustment=None):
+        super(ThreadViewScrollbar, self).__init__(adjustment)
+        self.set_property("no-show-all", True)
+        adjustment.connect("changed", self.on_adjustment_changed)
+
+    def change_vscrollbar_visible(self):
+        adjustment = self.get_adjustment()
+        if adjustment.upper <= adjustment.page_size:
+            self.hide()
+        else:
+            self.show()
+
+    def on_adjustment_changed(self, widget, data=None):
+        self.change_vscrollbar_visible()
+
+
+class ThreadViewContainer(gtk.HBox):
+
+    def __init__(self, child):
+        super(ThreadViewContainer, self).__init__(False, 2)
+        adjustment = gtk.Adjustment()
+        self.drawingarea = child
+        child.set_scroll_adjustments(None, adjustment)
+        self.vscrollbar = ThreadViewScrollbar(adjustment)
+        self.pack_start(child)
+        self.pack_start(self.vscrollbar, expand=False)
+
+        child.connect("scroll-event", self.on_child_scroll_event)
+
+    def on_child_scroll_event(self, widget, event, data=None):
+        self.vscrollbar.emit("scroll-event", event)
index b2786db..7d93d13 100644 (file)
@@ -149,11 +149,12 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.toolbar.unset_style()
 
         self.threadview = thread_view.ThreadView()
+        container = thread_view.ThreadViewContainer(self.threadview)
         self.threadpopup = thread_popup.ThreadPopup(self.bbs_type)
         self.threadpopup.push_thread_view(self.threadview)
-        self.vbox.pack_start(self.threadview)
-        self.vbox.reorder_child(self.threadview, 2)
-        self.window.set_focus(self.threadview.drawingarea)
+        self.vbox.pack_start(container)
+        self.vbox.reorder_child(container, 2)
+        self.window.set_focus(self.threadview)
 
         self._get_popupmenu_widgets()