PageRenderTime 55ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/controls/PlateButtonDemo.py

https://github.com/mogaal/editra
Python | 280 lines | 229 code | 16 blank | 35 comment | 0 complexity | 41441a6466ecaeb665d21c08fde98371 MD5 | raw file
  1. ###############################################################################
  2. # Name: PlateButtonDemo.py #
  3. # Purpose: PlateButton Test and Demo File #
  4. # Author: Cody Precord <cprecord@editra.org> #
  5. # Copyright: (c) 2007 Cody Precord <staff@editra.org> #
  6. # Licence: wxWindows Licence #
  7. ###############################################################################
  8. """
  9. Test file for testing the PlateButton control
  10. """
  11. __author__ = "Cody Precord <cprecord@editra.org>"
  12. __svnid__ = "$Id: PlateButtonDemo.py 63348 2010-02-01 22:01:17Z CJP $"
  13. __revision__ = "$Revision: 63348 $"
  14. #-----------------------------------------------------------------------------#
  15. # Imports
  16. import os
  17. import sys
  18. import webbrowser
  19. import wx
  20. import wx.lib.scrolledpanel as scrolled
  21. #sys.path.insert(0, os.path.abspath('../../src'))
  22. import eclib
  23. from IconFile import *
  24. #-----------------------------------------------------------------------------#
  25. class TestPanel(scrolled.ScrolledPanel):
  26. def __init__(self, parent, log):
  27. self.log = log
  28. scrolled.ScrolledPanel.__init__(self, parent, wx.ID_ANY, size=(500,500))
  29. # Layout
  30. self.__DoLayout()
  31. self.SetupScrolling()
  32. # Event Handlers
  33. self.Bind(wx.EVT_BUTTON, self.OnButton)
  34. self.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggleButton)
  35. self.Bind(wx.EVT_MENU, self.OnMenu)
  36. def __DoLayout(self):
  37. """Layout the panel"""
  38. # Make three different panels of buttons with different backgrounds
  39. # to test transparency and appearance of buttons under different use
  40. # cases
  41. p1 = wx.Panel(self)
  42. p2 = GradientPanel(self)
  43. p3 = wx.Panel(self)
  44. p3.SetBackgroundColour(wx.BLUE)
  45. self.__LayoutPanel(p1, "Default Background:")
  46. self.__LayoutPanel(p2, "Gradient Background:", exstyle=True)
  47. self.__LayoutPanel(p3, "Solid Background:")
  48. sizer = wx.BoxSizer(wx.VERTICAL)
  49. sizer.AddMany([(p1, 0, wx.EXPAND), (p2, 0, wx.EXPAND),
  50. (p3, 0, wx.EXPAND)])
  51. self.SetSizer(sizer)
  52. self.SetAutoLayout(True)
  53. def __LayoutPanel(self, panel, label, exstyle=False):
  54. """Puts a set of controls in the panel
  55. @param panel: panel to layout
  56. @param label: panels title
  57. @keyword exstyle: Set the PB_STYLE_NOBG or not
  58. """
  59. # Bitmaps (32x32) and (16x16)
  60. devil = Devil.GetBitmap() # 32x32
  61. monkey = Monkey.GetBitmap() # 32x32
  62. address = Address.GetBitmap() # 16x16
  63. folder = Home.GetBitmap()
  64. bookmark = Book.GetBitmap() # 16x16
  65. vsizer = wx.BoxSizer(wx.VERTICAL)
  66. hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
  67. hsizer1.Add((15, 15))
  68. hsizer2 = wx.BoxSizer(wx.HORIZONTAL)
  69. hsizer2.Add((15, 15))
  70. hsizer3 = wx.BoxSizer(wx.HORIZONTAL)
  71. hsizer3.Add((15, 15))
  72. hsizer4 = wx.BoxSizer(wx.HORIZONTAL)
  73. hsizer4.Add((15, 15))
  74. # Button Styles
  75. default = eclib.PB_STYLE_DEFAULT
  76. toggle = default | eclib.PB_STYLE_TOGGLE
  77. square = eclib.PB_STYLE_SQUARE
  78. sqgrad = eclib.PB_STYLE_SQUARE | eclib.PB_STYLE_GRADIENT
  79. gradient = eclib.PB_STYLE_GRADIENT
  80. # Create a number of different PlateButtons
  81. # Each button is created in the below loop by using the data set in this
  82. # lists tuple
  83. # (bmp, label, Style, Variant, Menu, Color, Enable)
  84. btype = [(None, "Normal PlateButton", default, None, None, None, True),
  85. (devil, "Normal w/Bitmap", default, None, None, None, True),
  86. (devil, "Disabled", default, None, None, None, False),
  87. (None, "Normal w/Menu", default, None, True, None, True),
  88. (folder, "Home Folder", default, None, True, None, True),
  89. # Row 2
  90. (None, "Square PlateButton", square, None, None, None, True),
  91. (address, "Square/Bitmap", square, None, None, None, True),
  92. (monkey, "Square/Gradient", sqgrad, None, None, None, True),
  93. (address, "Square/Small", square, wx.WINDOW_VARIANT_SMALL, True, None, True),
  94. (address, "Small Bitmap", default, wx.WINDOW_VARIANT_SMALL, None, wx.Colour(33, 33, 33), True),
  95. # Row 3
  96. (devil, "Custom Color", default, None, None, wx.RED, True),
  97. (monkey, "Gradient Highlight", gradient, None, None, None, True),
  98. (monkey, "Custom Gradient", gradient, None, None, wx.Colour(245, 55, 245), True),
  99. (devil, "", default, None, None, None, True),
  100. (bookmark, "", default, None, True, None, True),
  101. (monkey, "", square, None, None, None, True),
  102. # Row 4 Toggle buttons
  103. (None, "Toggle PlateButton", toggle, None, None, None, True),
  104. (devil, "Toggle w/Bitmap", toggle, None, None, None, True),
  105. (devil, "Toggle Disabled", toggle, None, None, None, False),
  106. (None, "Toggle w/Menu", toggle, None, True, None, True),
  107. (folder, "Toggle Home Folder", toggle, None, True, None, True),
  108. ]
  109. # Make and layout three rows of buttons in the panel
  110. for btn in btype:
  111. if exstyle:
  112. # With this style flag set the button can appear transparent on
  113. # on top of a background that is not solid in color, such as the
  114. # gradient panel in this demo.
  115. #
  116. # Note: This flag only has affect on wxMSW and should only be
  117. # set when the background is not a solid color. On wxMac
  118. # it is a no-op as this type of transparency is achieved
  119. # without any help needed. On wxGtk it doesn't hurt to
  120. # set but also unfortunatly doesn't help at all.
  121. bstyle = btn[2] | eclib.PB_STYLE_NOBG
  122. else:
  123. bstyle = btn[2]
  124. if btype.index(btn) < 5:
  125. tsizer = hsizer1
  126. elif btype.index(btn) < 10:
  127. tsizer = hsizer2
  128. elif btype.index(btn) < 16:
  129. tsizer = hsizer3
  130. else:
  131. tsizer = hsizer4
  132. tbtn = eclib.PlateButton(panel, wx.ID_ANY, btn[1], btn[0], style=bstyle)
  133. # Set a custom window size variant?
  134. if btn[3] is not None:
  135. tbtn.SetWindowVariant(btn[3])
  136. # Make a menu for the button?
  137. if btn[4] is not None:
  138. menu = wx.Menu()
  139. if btn[0] is not None and btn[0] == folder:
  140. for fname in os.listdir(wx.GetHomeDir()):
  141. if not fname.startswith('.'):
  142. menu.Append(wx.NewId(), fname)
  143. elif btn[0] is not None and btn[0] == bookmark:
  144. for url in ['http://wxpython.org', 'http://slashdot.org',
  145. 'http://editra.org', 'http://xkcd.com']:
  146. menu.Append(wx.NewId(), url, "Open %s in your browser" % url)
  147. else:
  148. menu.Append(wx.NewId(), "Menu Item 1")
  149. menu.Append(wx.NewId(), "Menu Item 2")
  150. menu.Append(wx.NewId(), "Menu Item 3")
  151. tbtn.SetMenu(menu)
  152. # Set a custom colour?
  153. if btn[5] is not None:
  154. tbtn.SetPressColor(btn[5])
  155. # Enable/Disable button state
  156. tbtn.Enable(btn[6])
  157. tsizer.AddMany([(tbtn, 0, wx.ALIGN_CENTER), ((10, 10))])
  158. txt_sz = wx.BoxSizer(wx.HORIZONTAL)
  159. txt_sz.AddMany([((5, 5)), (wx.StaticText(panel, label=label), 0, wx.ALIGN_LEFT)])
  160. vsizer.AddMany([((10, 10)),
  161. (txt_sz, 0, wx.ALIGN_LEFT),
  162. ((10, 10)), (hsizer1, 0, wx.EXPAND), ((10, 10)),
  163. (hsizer2, 0, wx.EXPAND), ((10, 10)),
  164. (hsizer3, 0, wx.EXPAND), ((10, 10)),
  165. (hsizer4, 0, wx.EXPAND), ((10, 10))])
  166. panel.SetSizer(vsizer)
  167. def OnButton(self, evt):
  168. self.log.write("BUTTON CLICKED: Id: %d, Label: %s" % \
  169. (evt.GetId(), evt.GetEventObject().LabelText))
  170. def OnToggleButton(self, evt):
  171. self.log.write("Toggle TOGGLEBUTTON CLICKED: Id: %d, Label: %s Pressed: %s" % \
  172. (evt.GetId(), evt.GetEventObject().LabelText,
  173. str(evt.GetEventObject().IsPressed())))
  174. def OnChildFocus(self, evt):
  175. """Override ScrolledPanel.OnChildFocus to prevent erratic
  176. scrolling on wxMac.
  177. """
  178. if wx.Platform != '__WXMAC__':
  179. evt.Skip()
  180. child = evt.GetWindow()
  181. self.ScrollChildIntoView(child)
  182. def OnMenu(self, evt):
  183. """Events from button menus"""
  184. self.log.write("MENU SELECTED: %d" % evt.GetId())
  185. e_obj = evt.GetEventObject()
  186. mitem = e_obj.FindItemById(evt.GetId())
  187. if mitem != wx.NOT_FOUND:
  188. label = mitem.GetLabel()
  189. if label.startswith('http://'):
  190. webbrowser.open(label, True)
  191. #-----------------------------------------------------------------------------#
  192. class GradientPanel(wx.Panel):
  193. def __init__(self, parent):
  194. wx.Panel.__init__(self, parent)
  195. self.Bind(wx.EVT_PAINT, self.OnPaint)
  196. def OnPaint(self, evt):
  197. dc = wx.PaintDC(self)
  198. gc = wx.GraphicsContext.Create(dc)
  199. col1 = wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DSHADOW)
  200. col2 = eclib.AdjustColour(col1, -90)
  201. col1 = eclib.AdjustColour(col1, 90)
  202. rect = self.GetClientRect()
  203. grad = gc.CreateLinearGradientBrush(0, 1, 0, rect.height - 1, col2, col1)
  204. pen_col = tuple([min(190, x) for x in eclib.AdjustColour(col1, -60)])
  205. gc.SetPen(gc.CreatePen(wx.Pen(pen_col, 1)))
  206. gc.SetBrush(grad)
  207. gc.DrawRectangle(0, 1, rect.width - 0.5, rect.height - 0.5)
  208. evt.Skip()
  209. #----------------------------------------------------------------------
  210. def runTest(frame, nb, log):
  211. win = TestPanel(nb, log)
  212. return win
  213. class TestLog:
  214. def __init__(self):
  215. pass
  216. def write(self, msg):
  217. print msg
  218. #----------------------------------------------------------------------
  219. overview = eclib.platebtn.__doc__
  220. title = "PlateButton"
  221. #-----------------------------------------------------------------------------#
  222. if __name__ == '__main__':
  223. try:
  224. import run
  225. except ImportError:
  226. app = wx.PySimpleApp(False)
  227. frame = wx.Frame(None, title="PlateButton Test")
  228. sizer = wx.BoxSizer(wx.HORIZONTAL)
  229. sizer.Add(TestPanel(frame, TestLog()), 1, wx.EXPAND)
  230. frame.CreateStatusBar()
  231. frame.SetSizer(sizer)
  232. frame.SetInitialSize()
  233. frame.Show()
  234. app.MainLoop()
  235. else:
  236. run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])