]>
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
.getColourInfoList()
17 # Just for style points, we'll use this as a background image.
18 self
.bg_bmp
= images
.getGridBGBitmap()
20 # This could also be done by getting the window's default font;
21 # either way, we need to have a font loaded for later on.
22 #self.SetBackgroundColour("WHITE")
23 self
.font
= wx
.Font(10, wx
.SWISS
, wx
.NORMAL
, wx
.NORMAL
)
25 # Create drawing area and set its font
26 dc
= wx
.ClientDC(self
)
29 # Using GetFullTextExtent(), we calculate a basic 'building block'
30 # that will be used to draw a depiction of the color list. We're
31 # using 'Wy' as the model becuase 'W' is a wide character and 'y'
32 # has a descender. This constitutes a 'worst case' scenario, which means
33 # that no matter what we draw later, text-wise, we'll have room for it
34 w
,h
,d
,e
= dc
.GetFullTextExtent("Wy")
36 # Height plus descender
37 self
.textHeight
= h
+ d
40 self
.lineHeight
= self
.textHeight
+ 5
42 # ... and this is the basic width.
45 # jmg 11/8/03: why 24?
48 # 'prep' our scroll bars.
50 self
.cellWidth
, self
.lineHeight
, numCells
, len(self
.clrList
) + 2
53 # Event handlers - moved here so events won't fire before init is
55 self
.SetBackgroundStyle(wx
.BG_STYLE_CUSTOM
)
56 self
.Bind(wx
.EVT_PAINT
, self
.OnPaint
)
57 self
.Bind(wx
.EVT_ERASE_BACKGROUND
, self
.OnEraseBackground
)
60 # tile the background bitmap loaded in __init__()
61 def TileBackground(self
, dc
):
62 sz
= self
.GetClientSize()
63 w
= self
.bg_bmp
.GetWidth()
64 h
= self
.bg_bmp
.GetHeight()
66 # adjust for scrolled position
67 spx
, spy
= self
.GetScrollPixelsPerUnit()
68 vsx
, vsy
= self
.GetViewStart()
69 dx
, dy
= (spx
* vsx
) % w
, (spy
* vsy
) % h
76 dc
.DrawBitmap(self
.bg_bmp
, x
, y
)
81 # Redraw the background over a 'damaged' area.
82 def OnEraseBackground(self
, evt
):
86 dc
= wx
.ClientDC(self
)
87 rect
= self
.GetUpdateRegion().GetBox()
88 dc
.SetClippingRect(rect
)
90 self
.TileBackground(dc
)
93 def OnPaint(self
, evt
):
96 self
.Draw(dc
, self
.GetUpdateRegion(), self
.GetViewStart())
99 def Draw(self
, dc
, rgn
=None, vs
=None):
100 dc
.SetTextForeground("BLACK")
101 dc
.SetPen(wx
.Pen("BLACK", 1, wx
.SOLID
))
102 dc
.SetFont(self
.font
)
103 colours
= self
.clrList
104 numColours
= len(colours
)
107 # determine the subset of the color list that has been exposed
108 # and needs drawn. This is based on all the precalculation we
111 pixStart
= vs
[1]*self
.lineHeight
+ rect
.y
112 pixStop
= pixStart
+ rect
.height
113 start
= pixStart
/ self
.lineHeight
- 1
114 stop
= pixStop
/ self
.lineHeight
119 for line
in range(max(0,start
), min(stop
,numColours
)):
120 clr
= colours
[line
][0]
121 y
= (line
+1) * self
.lineHeight
+ 2
123 dc
.DrawText(clr
, self
.cellWidth
, y
)
125 brush
= wx
.Brush(clr
, wx
.SOLID
)
127 dc
.DrawRectangle(10 * self
.cellWidth
, y
,
128 6 * self
.cellWidth
, self
.textHeight
)
130 dc
.DrawText(str(tuple(colours
[line
][1:])),
131 18 * self
.cellWidth
, y
)
134 # On wxGTK there needs to be a panel under wx.ScrolledWindows if they are
135 # going to be in a wxNotebook. And, in this demo, we are.
136 class TestPanel(wx
.Panel
):
137 def __init__(self
, parent
):
138 wx
.Panel
.__init
__(self
, parent
, -1)
139 self
.win
= TestWindow(self
)
140 self
.Bind(wx
.EVT_SIZE
, self
.OnSize
)
143 def OnSize(self
, evt
):
144 self
.win
.SetSize(evt
.GetSize())
148 #----------------------------------------------------------------------
151 def runTest(frame
, nb
, log
):
152 # This loads a whole bunch of new color names and values
153 # into TheColourDatabase
155 # Note 11/24/03 - jg - I moved this into runTest() because
156 # there must be a wx.App existing before this function
157 # can be called - this is a change from 2.4 -> 2.5.
158 wx
.lib
.colourdb
.updateColourDB()
164 #----------------------------------------------------------------------
169 <B><font size=+2>ColourDB</font></b>
171 <p>wxWindows maintains a database of standard RGB colours for a predefined
172 set of named colours (such as "BLACK'', "LIGHT GREY''). The application
173 may add to this set if desired by using Append. There is only one instance
174 of this class: <b>TheColourDatabase</b>.
176 <p>The <code>colourdb</code> library is a lightweight API that pre-defines
177 a multitude of colors for you to use 'out of the box', and this demo serves
178 to show you these colors (it also serves as a handy reference).
180 <p>A secondary benefit of this demo is the use of the <b>ScrolledWindow</b> class
181 and the use of various *DC() classes, including background tiling and the use of
182 font data to generate a "building block" type of construct for repetitive use.
185 <B><font size=+2>Important note</font></b>
188 With implementation of V2.5 and later, it is required to have a wx.App already
189 initialized before <b><code>wx.updateColourDB()</code></b> can be called.
190 Trying to do otherwise will cause an exception to be raised.
196 if __name__
== '__main__':
199 run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])