]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | # 11/4/03 - grimmtooth@softhome.net (Jeff Grimmett) |
2 | # | |
3 | # o Updated to use wx namespace | |
4 | # | |
5 | # 11/24/03 - grimmtooth@softhome.net (Jeff Grimmett) | |
6 | # | |
7 | # o Had to move the call to wx.updateColourDB() | |
8 | # o Updated several places to change discrete pos and size parameters | |
9 | # into two-tuples. | |
10 | # | |
11 | ||
12 | import wx | |
13 | import wx.lib.colourdb as cdb | |
c8ace611 RD |
14 | |
15 | import images | |
16 | ||
17 | ||
c8ace611 RD |
18 | #---------------------------------------------------------------------- |
19 | ||
8fa876ca | 20 | class TestWindow(wx.ScrolledWindow): |
c8ace611 | 21 | def __init__(self, parent): |
8fa876ca RD |
22 | wx.ScrolledWindow.__init__(self, parent, -1) |
23 | ||
24 | # Populate our color list | |
25 | self.clrList = cdb.getColourList() | |
c8ace611 | 26 | |
8fa876ca | 27 | # Just for style points, we'll use this as a background image. |
fe40185d | 28 | #self.clrList.sort() |
c8ace611 RD |
29 | self.bg_bmp = images.getGridBGBitmap() |
30 | ||
8fa876ca RD |
31 | # Event handlers |
32 | self.Bind(wx.EVT_PAINT, self.OnPaint) | |
33 | self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) | |
34 | ||
35 | # This could also be done by getting the window's default font; | |
36 | # either way, we need to have a font loaded for later on. | |
c8ace611 | 37 | #self.SetBackgroundColour("WHITE") |
8fa876ca | 38 | self.font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) |
c8ace611 | 39 | |
8fa876ca RD |
40 | # Create drawing area and set its font |
41 | dc = wx.ClientDC(self) | |
c8ace611 RD |
42 | dc.SetFont(self.font) |
43 | ||
8fa876ca RD |
44 | # Using GetFullTextExtent(), we calculate a basic 'building block' |
45 | # that will be used to draw a depiction of the color list. We're | |
46 | # using 'Wy' as the model becuase 'W' is a wide character and 'y' | |
47 | # has a descender. This constitutes a 'worst case' scenario, which means | |
48 | # that no matter what we draw later, text-wise, we'll have room for it | |
49 | w,h,d,e = dc.GetFullTextExtent("Wy") | |
50 | ||
51 | # Height plus descender | |
c8ace611 | 52 | self.textHeight = h + d |
8fa876ca RD |
53 | |
54 | # Pad a little bit | |
c8ace611 | 55 | self.lineHeight = self.textHeight + 5 |
8fa876ca RD |
56 | |
57 | # ... and this is the basic width. | |
c8ace611 RD |
58 | self.cellWidth = w |
59 | ||
8fa876ca | 60 | # jmg 11/8/03: why 24? |
c8ace611 | 61 | numCells = 24 |
8fa876ca RD |
62 | |
63 | # 'prep' our scroll bars. | |
64 | self.SetScrollbars( | |
65 | self.cellWidth, self.lineHeight, numCells, len(self.clrList) + 2 | |
66 | ) | |
c8ace611 RD |
67 | |
68 | ||
8fa876ca | 69 | # tile the background bitmap loaded in __init__() |
c8ace611 | 70 | def TileBackground(self, dc): |
c8ace611 RD |
71 | sz = self.GetClientSize() |
72 | w = self.bg_bmp.GetWidth() | |
73 | h = self.bg_bmp.GetHeight() | |
74 | ||
75 | # adjust for scrolled position | |
76 | spx, spy = self.GetScrollPixelsPerUnit() | |
77 | vsx, vsy = self.GetViewStart() | |
78 | dx, dy = (spx * vsx) % w, (spy * vsy) % h | |
79 | ||
80 | x = -dx | |
8fa876ca | 81 | |
c8ace611 RD |
82 | while x < sz.width: |
83 | y = -dy | |
84 | while y < sz.height: | |
fd3f2efe | 85 | dc.DrawBitmap(self.bg_bmp, (x, y)) |
c8ace611 | 86 | y = y + h |
c8ace611 | 87 | |
8fa876ca | 88 | x = x + w |
c8ace611 | 89 | |
8fa876ca | 90 | # Redraw the background over a 'damaged' area. |
c8ace611 RD |
91 | def OnEraseBackground(self, evt): |
92 | dc = evt.GetDC() | |
8fa876ca | 93 | |
c8ace611 | 94 | if not dc: |
8fa876ca | 95 | dc = wx.ClientDC(self) |
b166c703 | 96 | rect = self.GetUpdateRegion().GetBox() |
4da6d35e | 97 | dc.SetClippingRect(rect) |
8fa876ca | 98 | |
c8ace611 RD |
99 | self.TileBackground(dc) |
100 | ||
101 | ||
102 | def OnPaint(self, evt): | |
8fa876ca | 103 | dc = wx.PaintDC(self) |
c8ace611 RD |
104 | self.PrepareDC(dc) |
105 | self.Draw(dc, self.GetUpdateRegion(), self.GetViewStart()) | |
106 | ||
107 | ||
108 | def Draw(self, dc, rgn=None, vs=None): | |
109 | dc.BeginDrawing() | |
110 | dc.SetTextForeground("BLACK") | |
8fa876ca | 111 | dc.SetPen(wx.Pen("BLACK", 1, wx.SOLID)) |
c8ace611 RD |
112 | dc.SetFont(self.font) |
113 | colours = self.clrList | |
114 | numColours = len(colours) | |
115 | ||
116 | if rgn: | |
8fa876ca RD |
117 | # determine the subset of the color list that has been exposed |
118 | # and needs drawn. This is based on all the precalculation we | |
119 | # did in __init__() | |
c8ace611 RD |
120 | rect = rgn.GetBox() |
121 | pixStart = vs[1]*self.lineHeight + rect.y | |
122 | pixStop = pixStart + rect.height | |
123 | start = pixStart / self.lineHeight - 1 | |
124 | stop = pixStop / self.lineHeight | |
125 | else: | |
126 | start = 0 | |
b166c703 | 127 | stop = numColours |
c8ace611 RD |
128 | |
129 | for line in range(max(0,start), min(stop,numColours)): | |
130 | clr = colours[line] | |
131 | y = (line+1) * self.lineHeight + 2 | |
8fa876ca RD |
132 | |
133 | # Updated for 2.5 - now takes tuple for pos | |
fd3f2efe | 134 | dc.DrawText(clr, (self.cellWidth, y)) |
c8ace611 | 135 | |
8fa876ca | 136 | brush = wx.Brush(clr, wx.SOLID) |
c8ace611 | 137 | dc.SetBrush(brush) |
fd3f2efe RD |
138 | dc.DrawRectangle((12 * self.cellWidth, y), |
139 | (6 * self.cellWidth, self.textHeight)) | |
c8ace611 RD |
140 | |
141 | dc.EndDrawing() | |
142 | ||
143 | ||
b166c703 | 144 | # On wxGTK there needs to be a panel under wxScrolledWindows if they are |
8fa876ca RD |
145 | # going to be in a wxNotebook. And, in the demo, we are. |
146 | class TestPanel(wx.Panel): | |
b166c703 | 147 | def __init__(self, parent): |
8fa876ca | 148 | wx.Panel.__init__(self, parent, -1) |
b166c703 | 149 | self.win = TestWindow(self) |
8fa876ca RD |
150 | self.Bind(wx.EVT_SIZE, self.OnSize) |
151 | ||
b166c703 RD |
152 | |
153 | def OnSize(self, evt): | |
154 | self.win.SetSize(evt.GetSize()) | |
155 | ||
156 | ||
c8ace611 RD |
157 | |
158 | #---------------------------------------------------------------------- | |
159 | ||
160 | ||
161 | def runTest(frame, nb, log): | |
8b9a4190 | 162 | # This loads a whole bunch of new color names and values |
8fa876ca RD |
163 | # into TheColourDatabase |
164 | # | |
165 | # Note 11/24/03 - jg - I moved this into runTest() because | |
166 | # there must be a wx.App existing before this function | |
167 | # can be called - this is a change from 2.4 -> 2.5. | |
168 | cdb.updateColourDB() | |
8b9a4190 | 169 | |
b166c703 | 170 | win = TestPanel(nb) |
8fa876ca | 171 | |
c8ace611 RD |
172 | return win |
173 | ||
174 | #---------------------------------------------------------------------- | |
175 | ||
176 | overview = """ | |
8fa876ca RD |
177 | <html> |
178 | <body> | |
179 | <B><font size=+2>ColourDB</font></b> | |
180 | ||
181 | <p>wxWindows maintains a database of standard RGB colours for a predefined | |
182 | set of named colours (such as "BLACK'', "LIGHT GREY''). The application | |
183 | may add to this set if desired by using Append. There is only one instance | |
184 | of this class: <b>TheColourDatabase</b>. | |
185 | ||
186 | <p>The <code>colourdb</code> library is a lightweight API that pre-defines | |
187 | a multitude of colors for you to use 'out of the box', and this demo serves | |
188 | to show you these colors (it also serves as a handy reference). | |
189 | ||
190 | <p>A secondary benefit of this demo is the use of the <b>ScrolledWindow</b> class | |
191 | and the use of various *DC() classes, including background tiling and the use of | |
192 | font data to generate a "building block" type of construct for repetitive use. | |
193 | ||
194 | <p> | |
195 | <B><font size=+2>Important note</font></b> | |
196 | ||
197 | <p> | |
198 | With implementation of V2.5 and later, it is required to have a wx.App already | |
199 | initialized before <b><code>wx.updateColourDB()</code></b> can be called. | |
200 | Trying to do otherwise will cause an exception to be raised. | |
201 | </body> | |
202 | </html> | |
c8ace611 | 203 | """ |
1fded56b RD |
204 | |
205 | ||
206 | if __name__ == '__main__': | |
207 | import sys,os | |
208 | import run | |
209 | run.main(['', os.path.basename(sys.argv[0])]) | |
210 |