#
#   Python GUI - Scrollable Views - Gtk
#

import gtk
from GScrollableViews import ScrollableView as GScrollableView, \
    default_extent, default_line_scroll_amount

gtk_scroll_policies = [gtk.POLICY_NEVER, gtk.POLICY_ALWAYS]

class ScrollableView(GScrollableView):

    def __init__(self, extent = default_extent,
            line_scroll_amount = default_line_scroll_amount,
            **kwds):
        gtk_scrolled_window = gtk.ScrolledWindow()
        gtk_scrolled_window.show()
        GScrollableView.__init__(self, _gtk_outer = gtk_scrolled_window,
            extent = extent, line_scroll_amount = line_scroll_amount, **kwds)
    
    #
    #   Properties
    #

    def get_hscrolling(self):
        return self._gtk_inner_widget.hscrollbar_policy <> gtk.POLICY_NEVER
    
    def set_hscrolling(self, value):
        self._gtk_inner_widget.hscrollbar_policy = gtk_scroll_policies[value]
    
    def get_vscrolling(self):
        return self._gtk_inner_widget.vscrollbar_policy <> gtk.POLICY_NEVER
    
    def set_vscrolling(self, value):
        self._gtk_inner_widget.vscrollbar_policy = gtk_scroll_policies[value]
    
    def get_extent(self):
        l, t = self._extent_origin
        w, h = self._gtk_inner_widget.get_size()
        return (l, t, l + w, t + h)
    
    def set_extent(self, (l, t, r, b)):
        w = r - l
        h = b - t
        self._extent_origin = (l, t)
        self._gtk_inner_widget.set_size(w, h)

    def get_scroll_offset(self):
        hadj, vadj = self._gtk_adjustments()
        return int(hadj.value), int(vadj.value)

    def set_scroll_offset(self, (x, y)):
        x0, y0 = self._extent_origin
        hadj, vadj = self._gtk_adjustments()
        hadj.set_value(min(float(x - x0), hadj.upper - hadj.page_size))
        vadj.set_value(min(float(y - y0), vadj.upper - vadj.page_size))
    
    def get_line_scroll_amount(self):
        x0, y0 = self._extent_origin
        hadj, vadj = self._gtk_adjustments()
        return x0 + hadj.step_increment, y0 + vadj.step_increment
    
    def set_line_scroll_amount(self, (dx, dy)):
        hadj, vadj = self._gtk_adjustments()
        hadj.step_increment = float(dx) # Amazingly, ints are not
        vadj.step_increment = float(dy) # acceptable here.
    
    #
    #		Internal
    #

    def _gtk_adjustments(self):
        gtk_widget = self._gtk_inner_widget
        hadj = gtk_widget.get_hadjustment()
        vadj = gtk_widget.get_vadjustment()
        return hadj, vadj
