DirectScrolledFrame.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """Contains the DirectScrolledFrame class.
  2. See the :ref:`directscrolledframe` page in the programming manual for a more
  3. in-depth explanation and an example of how to use this class.
  4. """
  5. __all__ = ['DirectScrolledFrame']
  6. from panda3d.core import *
  7. from . import DirectGuiGlobals as DGG
  8. from .DirectFrame import *
  9. from .DirectScrollBar import *
  10. """
  11. import DirectScrolledFrame
  12. d = DirectScrolledFrame(borderWidth=(0, 0))
  13. """
  14. class DirectScrolledFrame(DirectFrame):
  15. """
  16. DirectScrolledFrame -- a special frame that uses DirectScrollBar to
  17. implement a small window (the frameSize) into a potentially much
  18. larger virtual canvas (the canvasSize, scrolledFrame.getCanvas()).
  19. Unless specified otherwise, scroll bars are automatically created
  20. and managed as needed, based on the relative sizes od the
  21. frameSize and the canvasSize. You can also set manageScrollBars =
  22. 0 and explicitly position and hide or show the scroll bars
  23. yourself.
  24. """
  25. def __init__(self, parent = None, **kw):
  26. optiondefs = (
  27. # Define type of DirectGuiWidget
  28. ('pgFunc', PGScrollFrame, None),
  29. ('frameSize', (-0.5, 0.5, -0.5, 0.5), None),
  30. ('canvasSize', (-1, 1, -1, 1), self.setCanvasSize),
  31. ('manageScrollBars', 1, self.setManageScrollBars),
  32. ('autoHideScrollBars', 1, self.setAutoHideScrollBars),
  33. ('scrollBarWidth', 0.08, self.setScrollBarWidth),
  34. ('borderWidth', (0.01, 0.01), self.setBorderWidth),
  35. )
  36. # Merge keyword options with default options
  37. self.defineoptions(kw, optiondefs)
  38. # Initialize superclasses
  39. DirectFrame.__init__(self, parent)
  40. # The scrollBarWidth parameter is just used at scroll bar
  41. # construction time, and supplies a default frame. It does
  42. # not override an explicit frame specified on the scroll bar.
  43. # If you want to change the frame width after construction,
  44. # you must specify their frameSize tuples explicitly.
  45. w = self['scrollBarWidth']
  46. self.verticalScroll = self.createcomponent(
  47. "verticalScroll", (), None,
  48. DirectScrollBar, (self,),
  49. borderWidth = self['borderWidth'],
  50. frameSize = (-w / 2.0, w / 2.0, -1, 1),
  51. orientation = DGG.VERTICAL)
  52. self.horizontalScroll = self.createcomponent(
  53. "horizontalScroll", (), None,
  54. DirectScrollBar, (self,),
  55. borderWidth = self['borderWidth'],
  56. frameSize = (-1, 1, -w / 2.0, w / 2.0),
  57. orientation = DGG.HORIZONTAL)
  58. self.guiItem.setVerticalSlider(self.verticalScroll.guiItem)
  59. self.guiItem.setHorizontalSlider(self.horizontalScroll.guiItem)
  60. self.canvas = NodePath(self.guiItem.getCanvasNode())
  61. # Call option initialization functions
  62. self.initialiseoptions(DirectScrolledFrame)
  63. def setScrollBarWidth(self):
  64. w = self['scrollBarWidth']
  65. self.verticalScroll["frameSize"] = (-w / 2.0, w / 2.0, -1, 1)
  66. self.horizontalScroll["frameSize"] = (-1, 1, -w / 2.0, w / 2.0)
  67. def setCanvasSize(self):
  68. f = self['canvasSize']
  69. self.guiItem.setVirtualFrame(f[0], f[1], f[2], f[3])
  70. def getCanvas(self):
  71. """Returns the NodePath of the virtual canvas. Nodes parented to this
  72. canvas will show inside the scrolled area.
  73. """
  74. return self.canvas
  75. def setManageScrollBars(self):
  76. self.guiItem.setManagePieces(self['manageScrollBars'])
  77. def setAutoHideScrollBars(self):
  78. self.guiItem.setAutoHide(self['autoHideScrollBars'])
  79. def commandFunc(self):
  80. if self['command']:
  81. self['command'](*self['extraArgs'])
  82. def destroy(self):
  83. # Destroy children of the canvas
  84. for child in self.canvas.getChildren():
  85. childGui = self.guiDict.get(child.getName())
  86. if childGui:
  87. childGui.destroy()
  88. else:
  89. parts = child.getName().split('-')
  90. simpleChildGui = self.guiDict.get(parts[-1])
  91. if simpleChildGui:
  92. simpleChildGui.destroy()
  93. if self.verticalScroll:
  94. self.verticalScroll.destroy()
  95. if self.horizontalScroll:
  96. self.horizontalScroll.destroy()
  97. self.verticalScroll = None
  98. self.horizontalScroll = None
  99. DirectFrame.destroy(self)