]>
Commit | Line | Data |
---|---|---|
8fa876ca RD |
1 | |
2 | import wx | |
95bfd958 | 3 | import wx.lib.colourdb |
c8ace611 RD |
4 | |
5 | import images | |
6 | ||
7 | ||
c8ace611 RD |
8 | #---------------------------------------------------------------------- |
9 | ||
8fa876ca | 10 | class TestWindow(wx.ScrolledWindow): |
c8ace611 | 11 | def __init__(self, parent): |
8fa876ca RD |
12 | wx.ScrolledWindow.__init__(self, parent, -1) |
13 | ||
14 | # Populate our color list | |
95bfd958 | 15 | self.clrList = wx.lib.colourdb.getColourList() |
c8ace611 | 16 | |
8fa876ca | 17 | # Just for style points, we'll use this as a background image. |
fe40185d | 18 | #self.clrList.sort() |
c8ace611 RD |
19 | self.bg_bmp = images.getGridBGBitmap() |
20 | ||
8fa876ca RD |
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. | |
c8ace611 | 23 | #self.SetBackgroundColour("WHITE") |
8fa876ca | 24 | self.font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) |
c8ace611 | 25 | |
8fa876ca RD |
26 | # Create drawing area and set its font |
27 | dc = wx.ClientDC(self) | |
c8ace611 RD |
28 | dc.SetFont(self.font) |
29 | ||
8fa876ca RD |
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") | |
36 | ||
37 | # Height plus descender | |
c8ace611 | 38 | self.textHeight = h + d |
8fa876ca RD |
39 | |
40 | # Pad a little bit | |
c8ace611 | 41 | self.lineHeight = self.textHeight + 5 |
8fa876ca RD |
42 | |
43 | # ... and this is the basic width. | |
c8ace611 RD |
44 | self.cellWidth = w |
45 | ||
8fa876ca | 46 | # jmg 11/8/03: why 24? |
c8ace611 | 47 | numCells = 24 |
8fa876ca RD |
48 | |
49 | # 'prep' our scroll bars. | |
50 | self.SetScrollbars( | |
51 | self.cellWidth, self.lineHeight, numCells, len(self.clrList) + 2 | |
52 | ) | |
0c8f2860 RD |
53 | |
54 | # Event handlers - moved here so events won't fire before init is | |
55 | # finished. | |
56 | self.Bind(wx.EVT_PAINT, self.OnPaint) | |
57 | self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) | |
c8ace611 RD |
58 | |
59 | ||
8fa876ca | 60 | # tile the background bitmap loaded in __init__() |
c8ace611 | 61 | def TileBackground(self, dc): |
c8ace611 RD |
62 | sz = self.GetClientSize() |
63 | w = self.bg_bmp.GetWidth() | |
64 | h = self.bg_bmp.GetHeight() | |
65 | ||
66 | # adjust for scrolled position | |
67 | spx, spy = self.GetScrollPixelsPerUnit() | |
68 | vsx, vsy = self.GetViewStart() | |
69 | dx, dy = (spx * vsx) % w, (spy * vsy) % h | |
70 | ||
71 | x = -dx | |
8fa876ca | 72 | |
c8ace611 RD |
73 | while x < sz.width: |
74 | y = -dy | |
75 | while y < sz.height: | |
fd3f2efe | 76 | dc.DrawBitmap(self.bg_bmp, (x, y)) |
c8ace611 | 77 | y = y + h |
c8ace611 | 78 | |
8fa876ca | 79 | x = x + w |
c8ace611 | 80 | |
8fa876ca | 81 | # Redraw the background over a 'damaged' area. |
c8ace611 RD |
82 | def OnEraseBackground(self, evt): |
83 | dc = evt.GetDC() | |
8fa876ca | 84 | |
c8ace611 | 85 | if not dc: |
8fa876ca | 86 | dc = wx.ClientDC(self) |
b166c703 | 87 | rect = self.GetUpdateRegion().GetBox() |
4da6d35e | 88 | dc.SetClippingRect(rect) |
8fa876ca | 89 | |
c8ace611 RD |
90 | self.TileBackground(dc) |
91 | ||
92 | ||
93 | def OnPaint(self, evt): | |
8fa876ca | 94 | dc = wx.PaintDC(self) |
c8ace611 RD |
95 | self.PrepareDC(dc) |
96 | self.Draw(dc, self.GetUpdateRegion(), self.GetViewStart()) | |
97 | ||
98 | ||
99 | def Draw(self, dc, rgn=None, vs=None): | |
100 | dc.BeginDrawing() | |
101 | dc.SetTextForeground("BLACK") | |
8fa876ca | 102 | dc.SetPen(wx.Pen("BLACK", 1, wx.SOLID)) |
c8ace611 RD |
103 | dc.SetFont(self.font) |
104 | colours = self.clrList | |
105 | numColours = len(colours) | |
106 | ||
107 | if rgn: | |
8fa876ca RD |
108 | # determine the subset of the color list that has been exposed |
109 | # and needs drawn. This is based on all the precalculation we | |
110 | # did in __init__() | |
c8ace611 RD |
111 | rect = rgn.GetBox() |
112 | pixStart = vs[1]*self.lineHeight + rect.y | |
113 | pixStop = pixStart + rect.height | |
114 | start = pixStart / self.lineHeight - 1 | |
115 | stop = pixStop / self.lineHeight | |
116 | else: | |
117 | start = 0 | |
b166c703 | 118 | stop = numColours |
c8ace611 RD |
119 | |
120 | for line in range(max(0,start), min(stop,numColours)): | |
121 | clr = colours[line] | |
122 | y = (line+1) * self.lineHeight + 2 | |
8fa876ca RD |
123 | |
124 | # Updated for 2.5 - now takes tuple for pos | |
fd3f2efe | 125 | dc.DrawText(clr, (self.cellWidth, y)) |
c8ace611 | 126 | |
8fa876ca | 127 | brush = wx.Brush(clr, wx.SOLID) |
c8ace611 | 128 | dc.SetBrush(brush) |
fd3f2efe RD |
129 | dc.DrawRectangle((12 * self.cellWidth, y), |
130 | (6 * self.cellWidth, self.textHeight)) | |
c8ace611 RD |
131 | |
132 | dc.EndDrawing() | |
133 | ||
134 | ||
299647ac RD |
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. | |
8fa876ca | 137 | class TestPanel(wx.Panel): |
b166c703 | 138 | def __init__(self, parent): |
8fa876ca | 139 | wx.Panel.__init__(self, parent, -1) |
b166c703 | 140 | self.win = TestWindow(self) |
8fa876ca RD |
141 | self.Bind(wx.EVT_SIZE, self.OnSize) |
142 | ||
b166c703 RD |
143 | |
144 | def OnSize(self, evt): | |
145 | self.win.SetSize(evt.GetSize()) | |
146 | ||
147 | ||
c8ace611 RD |
148 | |
149 | #---------------------------------------------------------------------- | |
150 | ||
151 | ||
152 | def runTest(frame, nb, log): | |
8b9a4190 | 153 | # This loads a whole bunch of new color names and values |
8fa876ca RD |
154 | # into TheColourDatabase |
155 | # | |
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. | |
95bfd958 | 159 | wx.lib.colourdb.updateColourDB() |
8b9a4190 | 160 | |
b166c703 | 161 | win = TestPanel(nb) |
8fa876ca | 162 | |
c8ace611 RD |
163 | return win |
164 | ||
165 | #---------------------------------------------------------------------- | |
166 | ||
167 | overview = """ | |
8fa876ca RD |
168 | <html> |
169 | <body> | |
170 | <B><font size=+2>ColourDB</font></b> | |
171 | ||
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>. | |
176 | ||
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). | |
180 | ||
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. | |
184 | ||
185 | <p> | |
186 | <B><font size=+2>Important note</font></b> | |
187 | ||
188 | <p> | |
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. | |
192 | </body> | |
193 | </html> | |
c8ace611 | 194 | """ |
1fded56b RD |
195 | |
196 | ||
197 | if __name__ == '__main__': | |
198 | import sys,os | |
199 | import run | |
8eca4fef | 200 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
1fded56b | 201 |