2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
import wx
|
2004-01-12 22:17:17 -05:00
|
|
|
import wx.lib.colourdb
|
2001-04-19 19:32:05 -04:00
|
|
|
|
|
|
|
import images
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestWindow(wx.ScrolledWindow):
|
2001-04-19 19:32:05 -04:00
|
|
|
def __init__(self, parent):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.ScrolledWindow.__init__(self, parent, -1)
|
|
|
|
|
|
|
|
# Populate our color list
|
2004-01-12 22:17:17 -05:00
|
|
|
self.clrList = wx.lib.colourdb.getColourList()
|
2001-04-19 19:32:05 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Just for style points, we'll use this as a background image.
|
2002-02-06 20:44:05 -05:00
|
|
|
#self.clrList.sort()
|
2001-04-19 19:32:05 -04:00
|
|
|
self.bg_bmp = images.getGridBGBitmap()
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# This could also be done by getting the window's default font;
|
|
|
|
# either way, we need to have a font loaded for later on.
|
2001-04-19 19:32:05 -04:00
|
|
|
#self.SetBackgroundColour("WHITE")
|
2003-12-08 20:23:28 -05:00
|
|
|
self.font = wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL)
|
2001-04-19 19:32:05 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Create drawing area and set its font
|
|
|
|
dc = wx.ClientDC(self)
|
2001-04-19 19:32:05 -04:00
|
|
|
dc.SetFont(self.font)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Using GetFullTextExtent(), we calculate a basic 'building block'
|
|
|
|
# that will be used to draw a depiction of the color list. We're
|
|
|
|
# using 'Wy' as the model becuase 'W' is a wide character and 'y'
|
|
|
|
# has a descender. This constitutes a 'worst case' scenario, which means
|
|
|
|
# that no matter what we draw later, text-wise, we'll have room for it
|
|
|
|
w,h,d,e = dc.GetFullTextExtent("Wy")
|
|
|
|
|
|
|
|
# Height plus descender
|
2001-04-19 19:32:05 -04:00
|
|
|
self.textHeight = h + d
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# Pad a little bit
|
2001-04-19 19:32:05 -04:00
|
|
|
self.lineHeight = self.textHeight + 5
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# ... and this is the basic width.
|
2001-04-19 19:32:05 -04:00
|
|
|
self.cellWidth = w
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# jmg 11/8/03: why 24?
|
2001-04-19 19:32:05 -04:00
|
|
|
numCells = 24
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
# 'prep' our scroll bars.
|
|
|
|
self.SetScrollbars(
|
|
|
|
self.cellWidth, self.lineHeight, numCells, len(self.clrList) + 2
|
|
|
|
)
|
2004-04-24 20:39:05 -04:00
|
|
|
|
|
|
|
# Event handlers - moved here so events won't fire before init is
|
|
|
|
# finished.
|
|
|
|
self.Bind(wx.EVT_PAINT, self.OnPaint)
|
|
|
|
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
|
2001-04-19 19:32:05 -04:00
|
|
|
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# tile the background bitmap loaded in __init__()
|
2001-04-19 19:32:05 -04:00
|
|
|
def TileBackground(self, dc):
|
|
|
|
sz = self.GetClientSize()
|
|
|
|
w = self.bg_bmp.GetWidth()
|
|
|
|
h = self.bg_bmp.GetHeight()
|
|
|
|
|
|
|
|
# adjust for scrolled position
|
|
|
|
spx, spy = self.GetScrollPixelsPerUnit()
|
|
|
|
vsx, vsy = self.GetViewStart()
|
|
|
|
dx, dy = (spx * vsx) % w, (spy * vsy) % h
|
|
|
|
|
|
|
|
x = -dx
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-04-19 19:32:05 -04:00
|
|
|
while x < sz.width:
|
|
|
|
y = -dy
|
|
|
|
while y < sz.height:
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawBitmap(self.bg_bmp, x, y)
|
2001-04-19 19:32:05 -04:00
|
|
|
y = y + h
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
x = x + w
|
2001-04-19 19:32:05 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Redraw the background over a 'damaged' area.
|
2001-04-19 19:32:05 -04:00
|
|
|
def OnEraseBackground(self, evt):
|
|
|
|
dc = evt.GetDC()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-04-19 19:32:05 -04:00
|
|
|
if not dc:
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(self)
|
2001-07-05 13:27:59 -04:00
|
|
|
rect = self.GetUpdateRegion().GetBox()
|
2003-11-28 13:42:44 -05:00
|
|
|
dc.SetClippingRect(rect)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-04-19 19:32:05 -04:00
|
|
|
self.TileBackground(dc)
|
|
|
|
|
|
|
|
|
|
|
|
def OnPaint(self, evt):
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.PaintDC(self)
|
2001-04-19 19:32:05 -04:00
|
|
|
self.PrepareDC(dc)
|
|
|
|
self.Draw(dc, self.GetUpdateRegion(), self.GetViewStart())
|
|
|
|
|
|
|
|
|
|
|
|
def Draw(self, dc, rgn=None, vs=None):
|
|
|
|
dc.BeginDrawing()
|
|
|
|
dc.SetTextForeground("BLACK")
|
2003-12-08 20:23:28 -05:00
|
|
|
dc.SetPen(wx.Pen("BLACK", 1, wx.SOLID))
|
2001-04-19 19:32:05 -04:00
|
|
|
dc.SetFont(self.font)
|
|
|
|
colours = self.clrList
|
|
|
|
numColours = len(colours)
|
|
|
|
|
|
|
|
if rgn:
|
2003-12-08 20:23:28 -05:00
|
|
|
# determine the subset of the color list that has been exposed
|
|
|
|
# and needs drawn. This is based on all the precalculation we
|
|
|
|
# did in __init__()
|
2001-04-19 19:32:05 -04:00
|
|
|
rect = rgn.GetBox()
|
|
|
|
pixStart = vs[1]*self.lineHeight + rect.y
|
|
|
|
pixStop = pixStart + rect.height
|
|
|
|
start = pixStart / self.lineHeight - 1
|
|
|
|
stop = pixStop / self.lineHeight
|
|
|
|
else:
|
|
|
|
start = 0
|
2001-07-05 13:27:59 -04:00
|
|
|
stop = numColours
|
2001-04-19 19:32:05 -04:00
|
|
|
|
|
|
|
for line in range(max(0,start), min(stop,numColours)):
|
|
|
|
clr = colours[line]
|
|
|
|
y = (line+1) * self.lineHeight + 2
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawText(clr, self.cellWidth, y)
|
2001-04-19 19:32:05 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
brush = wx.Brush(clr, wx.SOLID)
|
2001-04-19 19:32:05 -04:00
|
|
|
dc.SetBrush(brush)
|
2004-05-01 22:41:33 -04:00
|
|
|
dc.DrawRectangle(12 * self.cellWidth, y,
|
|
|
|
6 * self.cellWidth, self.textHeight)
|
2001-04-19 19:32:05 -04:00
|
|
|
|
|
|
|
dc.EndDrawing()
|
|
|
|
|
|
|
|
|
2004-01-02 20:14:17 -05:00
|
|
|
# On wxGTK there needs to be a panel under wx.ScrolledWindows if they are
|
|
|
|
# going to be in a wxNotebook. And, in this demo, we are.
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestPanel(wx.Panel):
|
2001-07-05 13:27:59 -04:00
|
|
|
def __init__(self, parent):
|
2003-12-08 20:23:28 -05:00
|
|
|
wx.Panel.__init__(self, parent, -1)
|
2001-07-05 13:27:59 -04:00
|
|
|
self.win = TestWindow(self)
|
2003-12-08 20:23:28 -05:00
|
|
|
self.Bind(wx.EVT_SIZE, self.OnSize)
|
|
|
|
|
2001-07-05 13:27:59 -04:00
|
|
|
|
|
|
|
def OnSize(self, evt):
|
|
|
|
self.win.SetSize(evt.GetSize())
|
|
|
|
|
|
|
|
|
2001-04-19 19:32:05 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
2003-10-01 20:58:06 -04:00
|
|
|
# This loads a whole bunch of new color names and values
|
2003-12-08 20:23:28 -05:00
|
|
|
# into TheColourDatabase
|
|
|
|
#
|
|
|
|
# Note 11/24/03 - jg - I moved this into runTest() because
|
|
|
|
# there must be a wx.App existing before this function
|
|
|
|
# can be called - this is a change from 2.4 -> 2.5.
|
2004-01-12 22:17:17 -05:00
|
|
|
wx.lib.colourdb.updateColourDB()
|
2003-10-01 20:58:06 -04:00
|
|
|
|
2001-07-05 13:27:59 -04:00
|
|
|
win = TestPanel(nb)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-04-19 19:32:05 -04:00
|
|
|
return win
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
overview = """
|
2003-12-08 20:23:28 -05:00
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<B><font size=+2>ColourDB</font></b>
|
|
|
|
|
|
|
|
<p>wxWindows maintains a database of standard RGB colours for a predefined
|
|
|
|
set of named colours (such as "BLACK'', "LIGHT GREY''). The application
|
|
|
|
may add to this set if desired by using Append. There is only one instance
|
|
|
|
of this class: <b>TheColourDatabase</b>.
|
|
|
|
|
|
|
|
<p>The <code>colourdb</code> library is a lightweight API that pre-defines
|
|
|
|
a multitude of colors for you to use 'out of the box', and this demo serves
|
|
|
|
to show you these colors (it also serves as a handy reference).
|
|
|
|
|
|
|
|
<p>A secondary benefit of this demo is the use of the <b>ScrolledWindow</b> class
|
|
|
|
and the use of various *DC() classes, including background tiling and the use of
|
|
|
|
font data to generate a "building block" type of construct for repetitive use.
|
|
|
|
|
|
|
|
<p>
|
|
|
|
<B><font size=+2>Important note</font></b>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
With implementation of V2.5 and later, it is required to have a wx.App already
|
|
|
|
initialized before <b><code>wx.updateColourDB()</code></b> can be called.
|
|
|
|
Trying to do otherwise will cause an exception to be raised.
|
|
|
|
</body>
|
|
|
|
</html>
|
2001-04-19 19:32:05 -04:00
|
|
|
"""
|
2003-07-02 19:13:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
2004-03-04 19:06:33 -05:00
|
|
|
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
|
2003-07-02 19:13:10 -04:00
|
|
|
|