]>
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 | # Event handlers |
22 | self.Bind(wx.EVT_PAINT, self.OnPaint) | |
23 | self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) | |
24 | ||
25 | # This could also be done by getting the window's default font; | |
26 | # either way, we need to have a font loaded for later on. | |
c8ace611 | 27 | #self.SetBackgroundColour("WHITE") |
8fa876ca | 28 | self.font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL) |
c8ace611 | 29 | |
8fa876ca RD |
30 | # Create drawing area and set its font |
31 | dc = wx.ClientDC(self) | |
c8ace611 RD |
32 | dc.SetFont(self.font) |
33 | ||
8fa876ca RD |
34 | # Using GetFullTextExtent(), we calculate a basic 'building block' |
35 | # that will be used to draw a depiction of the color list. We're | |
36 | # using 'Wy' as the model becuase 'W' is a wide character and 'y' | |
37 | # has a descender. This constitutes a 'worst case' scenario, which means | |
38 | # that no matter what we draw later, text-wise, we'll have room for it | |
39 | w,h,d,e = dc.GetFullTextExtent("Wy") | |
40 | ||
41 | # Height plus descender | |
c8ace611 | 42 | self.textHeight = h + d |
8fa876ca RD |
43 | |
44 | # Pad a little bit | |
c8ace611 | 45 | self.lineHeight = self.textHeight + 5 |
8fa876ca RD |
46 | |
47 | # ... and this is the basic width. | |
c8ace611 RD |
48 | self.cellWidth = w |
49 | ||
8fa876ca | 50 | # jmg 11/8/03: why 24? |
c8ace611 | 51 | numCells = 24 |
8fa876ca RD |
52 | |
53 | # 'prep' our scroll bars. | |
54 | self.SetScrollbars( | |
55 | self.cellWidth, self.lineHeight, numCells, len(self.clrList) + 2 | |
56 | ) | |
c8ace611 RD |
57 | |
58 | ||
8fa876ca | 59 | # tile the background bitmap loaded in __init__() |
c8ace611 | 60 | def TileBackground(self, dc): |
c8ace611 RD |
61 | sz = self.GetClientSize() |
62 | w = self.bg_bmp.GetWidth() | |
63 | h = self.bg_bmp.GetHeight() | |
64 | ||
65 | # adjust for scrolled position | |
66 | spx, spy = self.GetScrollPixelsPerUnit() | |
67 | vsx, vsy = self.GetViewStart() | |
68 | dx, dy = (spx * vsx) % w, (spy * vsy) % h | |
69 | ||
70 | x = -dx | |
8fa876ca | 71 | |
c8ace611 RD |
72 | while x < sz.width: |
73 | y = -dy | |
74 | while y < sz.height: | |
fd3f2efe | 75 | dc.DrawBitmap(self.bg_bmp, (x, y)) |
c8ace611 | 76 | y = y + h |
c8ace611 | 77 | |
8fa876ca | 78 | x = x + w |
c8ace611 | 79 | |
8fa876ca | 80 | # Redraw the background over a 'damaged' area. |
c8ace611 RD |
81 | def OnEraseBackground(self, evt): |
82 | dc = evt.GetDC() | |
8fa876ca | 83 | |
c8ace611 | 84 | if not dc: |
8fa876ca | 85 | dc = wx.ClientDC(self) |
b166c703 | 86 | rect = self.GetUpdateRegion().GetBox() |
4da6d35e | 87 | dc.SetClippingRect(rect) |
8fa876ca | 88 | |
c8ace611 RD |
89 | self.TileBackground(dc) |
90 | ||
91 | ||
92 | def OnPaint(self, evt): | |
8fa876ca | 93 | dc = wx.PaintDC(self) |
c8ace611 RD |
94 | self.PrepareDC(dc) |
95 | self.Draw(dc, self.GetUpdateRegion(), self.GetViewStart()) | |
96 | ||
97 | ||
98 | def Draw(self, dc, rgn=None, vs=None): | |
99 | dc.BeginDrawing() | |
100 | dc.SetTextForeground("BLACK") | |
8fa876ca | 101 | dc.SetPen(wx.Pen("BLACK", 1, wx.SOLID)) |
c8ace611 RD |
102 | dc.SetFont(self.font) |
103 | colours = self.clrList | |
104 | numColours = len(colours) | |
105 | ||
106 | if rgn: | |
8fa876ca RD |
107 | # determine the subset of the color list that has been exposed |
108 | # and needs drawn. This is based on all the precalculation we | |
109 | # did in __init__() | |
c8ace611 RD |
110 | rect = rgn.GetBox() |
111 | pixStart = vs[1]*self.lineHeight + rect.y | |
112 | pixStop = pixStart + rect.height | |
113 | start = pixStart / self.lineHeight - 1 | |
114 | stop = pixStop / self.lineHeight | |
115 | else: | |
116 | start = 0 | |
b166c703 | 117 | stop = numColours |
c8ace611 RD |
118 | |
119 | for line in range(max(0,start), min(stop,numColours)): | |
120 | clr = colours[line] | |
121 | y = (line+1) * self.lineHeight + 2 | |
8fa876ca RD |
122 | |
123 | # Updated for 2.5 - now takes tuple for pos | |
fd3f2efe | 124 | dc.DrawText(clr, (self.cellWidth, y)) |
c8ace611 | 125 | |
8fa876ca | 126 | brush = wx.Brush(clr, wx.SOLID) |
c8ace611 | 127 | dc.SetBrush(brush) |
fd3f2efe RD |
128 | dc.DrawRectangle((12 * self.cellWidth, y), |
129 | (6 * self.cellWidth, self.textHeight)) | |
c8ace611 RD |
130 | |
131 | dc.EndDrawing() | |
132 | ||
133 | ||
299647ac RD |
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. | |
8fa876ca | 136 | class TestPanel(wx.Panel): |
b166c703 | 137 | def __init__(self, parent): |
8fa876ca | 138 | wx.Panel.__init__(self, parent, -1) |
b166c703 | 139 | self.win = TestWindow(self) |
8fa876ca RD |
140 | self.Bind(wx.EVT_SIZE, self.OnSize) |
141 | ||
b166c703 RD |
142 | |
143 | def OnSize(self, evt): | |
144 | self.win.SetSize(evt.GetSize()) | |
145 | ||
146 | ||
c8ace611 RD |
147 | |
148 | #---------------------------------------------------------------------- | |
149 | ||
150 | ||
151 | def runTest(frame, nb, log): | |
8b9a4190 | 152 | # This loads a whole bunch of new color names and values |
8fa876ca RD |
153 | # into TheColourDatabase |
154 | # | |
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. | |
95bfd958 | 158 | wx.lib.colourdb.updateColourDB() |
8b9a4190 | 159 | |
b166c703 | 160 | win = TestPanel(nb) |
8fa876ca | 161 | |
c8ace611 RD |
162 | return win |
163 | ||
164 | #---------------------------------------------------------------------- | |
165 | ||
166 | overview = """ | |
8fa876ca RD |
167 | <html> |
168 | <body> | |
169 | <B><font size=+2>ColourDB</font></b> | |
170 | ||
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>. | |
175 | ||
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). | |
179 | ||
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. | |
183 | ||
184 | <p> | |
185 | <B><font size=+2>Important note</font></b> | |
186 | ||
187 | <p> | |
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. | |
191 | </body> | |
192 | </html> | |
c8ace611 | 193 | """ |
1fded56b RD |
194 | |
195 | ||
196 | if __name__ == '__main__': | |
197 | import sys,os | |
198 | import run | |
8eca4fef | 199 | run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:]) |
1fded56b | 200 |