/GUI/Win32/ScrollableViews.py

https://bitbucket.org/alsh/pygui-mirror · Python · 90 lines · 55 code · 23 blank · 12 comment · 0 complexity · c85476e02319b3ac02621a8a96d4eaad MD5 · raw file

  1. #--------------------------------------------------------------------
  2. #
  3. # PyGUI - ScrollableView - Win32
  4. #
  5. #--------------------------------------------------------------------
  6. import win32con as wc, win32ui as ui
  7. from Components import win_none
  8. from Canvases import Canvas
  9. from GScrollableViews import ScrollableView as GScrollableView, \
  10. default_extent, default_line_scroll_amount
  11. win_style = ui.AFX_WS_DEFAULT_VIEW
  12. #print "ScrollableViews: Creating dummy doc"
  13. # PyWin32 insists on being given a CDocument when creating a CScrollView,
  14. # and doesn't provide any way of creating a real one without using
  15. # a resource. The following hack creates something that looks enough
  16. # like a CDocument to keep it happy.
  17. win_dummy_doc = ui.CreateRichEditView().GetDocument()
  18. #print "ScrollableViews: Created dummy doc"
  19. class ScrollableView(GScrollableView):
  20. _line_scroll_amount = default_line_scroll_amount
  21. def __init__(self, **kwds):
  22. kwds.setdefault('extent', default_extent)
  23. win = ui.CreateView(win_dummy_doc)
  24. win.CreateWindow(win_none, 0, win_style, (0, 0, 100, 100))
  25. GScrollableView.__init__(self, _win = win)
  26. self.set(**kwds)
  27. def get_hscrolling(self):
  28. return self._win.GetStyle() & wc.WS_HSCROLL != 0
  29. def get_vscrolling(self):
  30. return self._win.GetStyle() & wc.WS_VSCROLL != 0
  31. def set_hscrolling(self, x):
  32. self._win_set_flag(x, wc.WS_HSCROLL)
  33. def set_vscrolling(self, x):
  34. self._win_set_flag(x, wc.WS_VSCROLL)
  35. def get_line_scroll_amount(self):
  36. return self._line_scroll_amount
  37. def get_extent(self):
  38. return self._win.GetTotalSize()
  39. def set_extent(self, extent):
  40. self._win_update_scroll_sizes(extent)
  41. def get_scroll_offset(self):
  42. return self._win.GetScrollPosition()
  43. def set_scroll_offset(self, p):
  44. px, py = p
  45. ex, ey = self.extent
  46. vx, vy = self.content_size
  47. xmax = max(0, ex - vx)
  48. ymax = max(0, ey - vy)
  49. x = max(0, min(px, xmax))
  50. y = max(0, min(py, ymax))
  51. self._win.ScrollToPosition((x, y))
  52. def set_bounds(self, bounds):
  53. GScrollableView.set_bounds(self, bounds)
  54. extent = self._win.GetTotalSize()
  55. self._win_update_scroll_sizes(extent)
  56. def _win_update_scroll_sizes(self, extent):
  57. ph = self.h_page_scroll_amount()
  58. pv = self.v_page_scroll_amount()
  59. ls = self.line_scroll_amount
  60. self._win.SetScrollSizes(wc.MM_TEXT, extent, (ph, pv), ls)
  61. def OnDraw(self, dc):
  62. #print "ScrollableView.OnDraw" ###
  63. update_rect = dc.GetClipBox()
  64. canvas = Canvas._from_win_dc(dc)
  65. self.draw(canvas, update_rect)
  66. def _win_prepare_dc(self, dc, pinfo = None):
  67. self._win.OnPrepareDC(dc, None)