]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ColourDB.py
   8 #---------------------------------------------------------------------- 
  10 class TestWindow(wx
.ScrolledWindow
): 
  11     def __init__(self
, parent
): 
  12         wx
.ScrolledWindow
.__init
__(self
, parent
, -1) 
  14         # Populate our color list 
  15         self
.clrList 
= wx
.lib
.colourdb
.getColourList() 
  17         # Just for style points, we'll use this as a background image. 
  19         self
.bg_bmp 
= images
.getGridBGBitmap() 
  21         # This could also be done by getting the window's default font; 
  22         # either way, we need to have a font loaded for later on. 
  23         #self.SetBackgroundColour("WHITE") 
  24         self
.font 
= wx
.Font(10, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
) 
  26         # Create drawing area and set its font 
  27         dc 
= wx
.ClientDC(self
) 
  30         # Using GetFullTextExtent(), we calculate a basic 'building block' 
  31         # that will be used to draw a depiction of the color list. We're 
  32         # using 'Wy' as the model becuase 'W' is a wide character and 'y'  
  33         # has a descender. This constitutes a 'worst case' scenario, which means 
  34         # that no matter what we draw later, text-wise, we'll have room for it 
  35         w
,h
,d
,e 
= dc
.GetFullTextExtent("Wy")  
  37         # Height plus descender 
  38         self
.textHeight 
= h 
+ d
 
  41         self
.lineHeight 
= self
.textHeight 
+ 5 
  43         # ... and this is the basic width. 
  46         # jmg 11/8/03: why 24? 
  49         # 'prep' our scroll bars. 
  51             self
.cellWidth
, self
.lineHeight
, numCells
, len(self
.clrList
) + 2 
  54         # Event handlers - moved here so events won't fire before init is  
  56         self
.SetBackgroundStyle(wx
.BG_STYLE_CUSTOM
) 
  57         self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
) 
  58         self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
) 
  61     # tile the background bitmap loaded in __init__() 
  62     def TileBackground(self
, dc
): 
  63         sz 
= self
.GetClientSize() 
  64         w 
= self
.bg_bmp
.GetWidth() 
  65         h 
= self
.bg_bmp
.GetHeight() 
  67         # adjust for scrolled position 
  68         spx
, spy 
= self
.GetScrollPixelsPerUnit() 
  69         vsx
, vsy 
= self
.GetViewStart() 
  70         dx
,  dy  
= (spx 
* vsx
) % w
, (spy 
* vsy
) % h
 
  77                 dc
.DrawBitmap(self
.bg_bmp
, x
, y
) 
  82     # Redraw the background over a 'damaged' area. 
  83     def OnEraseBackground(self
, evt
): 
  87             dc 
= wx
.ClientDC(self
) 
  88             rect 
= self
.GetUpdateRegion().GetBox() 
  89             dc
.SetClippingRect(rect
) 
  91         self
.TileBackground(dc
) 
  94     def OnPaint(self
, evt
): 
  97         self
.Draw(dc
, self
.GetUpdateRegion(), self
.GetViewStart()) 
 100     def Draw(self
, dc
, rgn
=None, vs
=None): 
 102         dc
.SetTextForeground("BLACK") 
 103         dc
.SetPen(wx
.Pen("BLACK", 1, wx
.SOLID
)) 
 104         dc
.SetFont(self
.font
) 
 105         colours 
= self
.clrList
 
 106         numColours 
= len(colours
) 
 109             # determine the subset of the color list that has been exposed  
 110             # and needs drawn. This is based on all the precalculation we 
 113             pixStart 
= vs
[1]*self
.lineHeight 
+ rect
.y
 
 114             pixStop  
= pixStart 
+ rect
.height
 
 115             start 
= pixStart 
/ self
.lineHeight 
- 1 
 116             stop 
= pixStop 
/ self
.lineHeight
 
 121         for line 
in range(max(0,start
), min(stop
,numColours
)): 
 123             y 
= (line
+1) * self
.lineHeight 
+ 2 
 125             dc
.DrawText(clr
, self
.cellWidth
, y
) 
 127             brush 
= wx
.Brush(clr
, wx
.SOLID
) 
 129             dc
.DrawRectangle(12 * self
.cellWidth
, y
, 
 130                              6 * self
.cellWidth
, self
.textHeight
) 
 135 # On wxGTK there needs to be a panel under wx.ScrolledWindows if they are 
 136 # going to be in a wxNotebook. And, in this demo, we are. 
 137 class TestPanel(wx
.Panel
): 
 138     def __init__(self
, parent
): 
 139         wx
.Panel
.__init
__(self
, parent
, -1) 
 140         self
.win 
= TestWindow(self
) 
 141         self
.Bind(wx
.EVT_SIZE
, self
.OnSize
) 
 144     def OnSize(self
, evt
): 
 145         self
.win
.SetSize(evt
.GetSize()) 
 149 #---------------------------------------------------------------------- 
 152 def runTest(frame
, nb
, log
): 
 153     # This loads a whole bunch of new color names and values 
 154     # into TheColourDatabase 
 156     # Note 11/24/03 - jg - I moved this into runTest() because 
 157     # there must be a wx.App existing before this function 
 158     # can be called - this is a change from 2.4 -> 2.5. 
 159     wx
.lib
.colourdb
.updateColourDB() 
 165 #---------------------------------------------------------------------- 
 170 <B><font size=+2>ColourDB</font></b> 
 172 <p>wxWindows maintains a database of standard RGB colours for a predefined  
 173 set of named colours (such as "BLACK'', "LIGHT GREY''). The application  
 174 may add to this set if desired by using Append. There is only one instance  
 175 of this class: <b>TheColourDatabase</b>. 
 177 <p>The <code>colourdb</code> library is a lightweight API that pre-defines 
 178 a multitude of colors for you to use 'out of the box', and this demo serves 
 179 to show you these colors (it also serves as a handy reference). 
 181 <p>A secondary benefit of this demo is the use of the <b>ScrolledWindow</b> class 
 182 and the use of various *DC() classes, including background tiling and the use of 
 183 font data to generate a "building block" type of construct for repetitive use. 
 186 <B><font size=+2>Important note</font></b> 
 189 With implementation of V2.5 and later, it is required to have a wx.App already 
 190 initialized before <b><code>wx.updateColourDB()</code></b> can be called.  
 191 Trying to do otherwise will cause an exception to be raised. 
 197 if __name__ 
== '__main__': 
 200     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])