2003-12-08 20:23:28 -05:00
|
|
|
# 11/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
|
|
|
|
#
|
|
|
|
# o Updated for wx namespace
|
|
|
|
#
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import wx
|
|
|
|
import wx.ogl as ogl
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
import images
|
2001-04-09 15:36:36 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
##wx.Trap()
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class DiamondShape(ogl.PolygonShape):
|
1999-09-10 02:25:47 -04:00
|
|
|
def __init__(self, w=0.0, h=0.0):
|
2003-12-08 20:23:28 -05:00
|
|
|
ogl.PolygonShape.__init__(self)
|
1999-09-10 02:25:47 -04:00
|
|
|
if w == 0.0:
|
|
|
|
w = 60.0
|
|
|
|
if h == 0.0:
|
|
|
|
h = 60.0
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
# Either ogl.RealPoints or 2-tuples of floats works.
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
#points = [ ogl.RealPoint(0.0, -h/2.0),
|
|
|
|
# ogl.RealPoint(w/2.0, 0.0),
|
|
|
|
# ogl.RealPoint(0.0, h/2.0),
|
|
|
|
# ogl.RealPoint(-w/2.0, 0.0),
|
1999-09-10 02:25:47 -04:00
|
|
|
# ]
|
|
|
|
points = [ (0.0, -h/2.0),
|
|
|
|
(w/2.0, 0.0),
|
|
|
|
(0.0, h/2.0),
|
|
|
|
(-w/2.0, 0.0),
|
|
|
|
]
|
|
|
|
|
|
|
|
self.Create(points)
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class RoundedRectangleShape(ogl.RectangleShape):
|
1999-09-10 02:25:47 -04:00
|
|
|
def __init__(self, w=0.0, h=0.0):
|
2003-12-08 20:23:28 -05:00
|
|
|
ogl.RectangleShape.__init__(self, w, h)
|
1999-09-10 02:25:47 -04:00
|
|
|
self.SetCornerRadius(-0.3)
|
|
|
|
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class DividedShape(ogl.DividedShape):
|
2003-03-25 01:35:27 -05:00
|
|
|
def __init__(self, width, height, canvas):
|
2003-12-08 20:23:28 -05:00
|
|
|
ogl.DividedShape.__init__(self, width, height)
|
2003-03-25 01:35:27 -05:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
region1 = ogl.ShapeRegion()
|
|
|
|
region1.SetText('DividedShape')
|
2003-03-25 01:35:27 -05:00
|
|
|
region1.SetProportions(0.0, 0.2)
|
2003-12-08 20:23:28 -05:00
|
|
|
region1.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.AddRegion(region1)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
region2 = ogl.ShapeRegion()
|
2003-03-25 01:35:27 -05:00
|
|
|
region2.SetText('This is Region number two.')
|
|
|
|
region2.SetProportions(0.0, 0.3)
|
2003-12-08 20:23:28 -05:00
|
|
|
region2.SetFormatMode(ogl.FORMAT_CENTRE_HORIZ|ogl.FORMAT_CENTRE_VERT)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.AddRegion(region2)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
region3 = ogl.ShapeRegion()
|
2003-03-25 01:35:27 -05:00
|
|
|
region3.SetText('Region 3\nwith embedded\nline breaks')
|
|
|
|
region3.SetProportions(0.0, 0.5)
|
2003-12-08 20:23:28 -05:00
|
|
|
region3.SetFormatMode(ogl.FORMAT_NONE)
|
2003-03-25 01:35:27 -05:00
|
|
|
self.AddRegion(region3)
|
|
|
|
|
|
|
|
self.SetRegionSizes()
|
|
|
|
self.ReformatRegions(canvas)
|
|
|
|
|
|
|
|
|
|
|
|
def ReformatRegions(self, canvas=None):
|
|
|
|
rnum = 0
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if canvas is None:
|
|
|
|
canvas = self.GetCanvas()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
dc = wx.ClientDC(canvas) # used for measuring
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
for region in self.GetRegions():
|
|
|
|
text = region.GetText()
|
|
|
|
self.FormatText(dc, text, rnum)
|
|
|
|
rnum += 1
|
|
|
|
|
|
|
|
|
|
|
|
def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
|
2003-11-12 16:34:20 -05:00
|
|
|
print "***", self
|
2003-03-25 01:35:27 -05:00
|
|
|
self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
|
|
|
|
self.SetRegionSizes()
|
|
|
|
self.ReformatRegions()
|
|
|
|
self.GetCanvas().Refresh()
|
|
|
|
|
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class MyEvtHandler(ogl.ShapeEvtHandler):
|
1999-09-10 02:25:47 -04:00
|
|
|
def __init__(self, log, frame):
|
2003-12-08 20:23:28 -05:00
|
|
|
ogl.ShapeEvtHandler.__init__(self)
|
1999-09-10 02:25:47 -04:00
|
|
|
self.log = log
|
|
|
|
self.statbarFrame = frame
|
|
|
|
|
|
|
|
def UpdateStatusBar(self, shape):
|
|
|
|
x,y = shape.GetX(), shape.GetY()
|
|
|
|
width, height = shape.GetBoundingBoxMax()
|
|
|
|
self.statbarFrame.SetStatusText("Pos: (%d,%d) Size: (%d, %d)" %
|
|
|
|
(x, y, width, height))
|
|
|
|
|
|
|
|
|
|
|
|
def OnLeftClick(self, x, y, keys = 0, attachment = 0):
|
|
|
|
shape = self.GetShape()
|
2003-07-02 19:13:10 -04:00
|
|
|
print shape.__class__, shape.GetClassName()
|
1999-09-10 02:25:47 -04:00
|
|
|
canvas = shape.GetCanvas()
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(canvas)
|
1999-09-10 02:25:47 -04:00
|
|
|
canvas.PrepareDC(dc)
|
|
|
|
|
|
|
|
if shape.Selected():
|
2003-03-25 01:35:27 -05:00
|
|
|
shape.Select(False, dc)
|
1999-09-10 02:25:47 -04:00
|
|
|
canvas.Redraw(dc)
|
|
|
|
else:
|
2003-03-25 01:35:27 -05:00
|
|
|
redraw = False
|
1999-09-10 02:25:47 -04:00
|
|
|
shapeList = canvas.GetDiagram().GetShapeList()
|
|
|
|
toUnselect = []
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
for s in shapeList:
|
|
|
|
if s.Selected():
|
|
|
|
# If we unselect it now then some of the objects in
|
|
|
|
# shapeList will become invalid (the control points are
|
|
|
|
# shapes too!) and bad things will happen...
|
|
|
|
toUnselect.append(s)
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
shape.Select(True, dc)
|
1999-09-10 02:25:47 -04:00
|
|
|
|
|
|
|
if toUnselect:
|
|
|
|
for s in toUnselect:
|
2003-03-25 01:35:27 -05:00
|
|
|
s.Select(False, dc)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
canvas.Redraw(dc)
|
|
|
|
|
|
|
|
self.UpdateStatusBar(shape)
|
|
|
|
|
|
|
|
|
|
|
|
def OnEndDragLeft(self, x, y, keys = 0, attachment = 0):
|
|
|
|
shape = self.GetShape()
|
|
|
|
self.base_OnEndDragLeft(x, y, keys, attachment)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
if not shape.Selected():
|
|
|
|
self.OnLeftClick(x, y, keys, attachment)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
self.UpdateStatusBar(shape)
|
|
|
|
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
def OnSizingEndDragLeft(self, pt, x, y, keys, attch):
|
|
|
|
self.base_OnSizingEndDragLeft(pt, x, y, keys, attch)
|
1999-09-10 02:25:47 -04:00
|
|
|
self.UpdateStatusBar(self.GetShape())
|
|
|
|
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
def OnMovePost(self, dc, x, y, oldX, oldY, display):
|
|
|
|
self.base_OnMovePost(dc, x, y, oldX, oldY, display)
|
|
|
|
self.UpdateStatusBar(self.GetShape())
|
1999-09-10 02:25:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
def OnRightClick(self, *dontcare):
|
|
|
|
self.log.WriteText("%s\n" % self.GetShape())
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
class TestWindow(ogl.ShapeCanvas):
|
1999-09-10 02:25:47 -04:00
|
|
|
def __init__(self, parent, log, frame):
|
2003-12-08 20:23:28 -05:00
|
|
|
ogl.ShapeCanvas.__init__(self, parent)
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2000-07-15 15:51:35 -04:00
|
|
|
maxWidth = 1000
|
|
|
|
maxHeight = 1000
|
|
|
|
self.SetScrollbars(20, 20, maxWidth/20, maxHeight/20)
|
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
self.log = log
|
|
|
|
self.frame = frame
|
2003-12-08 20:23:28 -05:00
|
|
|
self.SetBackgroundColour("LIGHT BLUE") #wx.WHITE)
|
|
|
|
self.diagram = ogl.Diagram()
|
1999-09-10 02:25:47 -04:00
|
|
|
self.SetDiagram(self.diagram)
|
|
|
|
self.diagram.SetCanvas(self)
|
|
|
|
self.shapes = []
|
2001-04-01 23:49:52 -04:00
|
|
|
self.save_gdi = []
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
rRectBrush = wx.Brush("MEDIUM TURQUOISE", wx.SOLID)
|
|
|
|
dsBrush = wx.Brush("WHEAT", wx.SOLID)
|
|
|
|
|
|
|
|
self.MyAddShape(
|
|
|
|
ogl.CircleShape(80),
|
|
|
|
100, 100, wx.Pen(wx.BLUE, 3), wx.GREEN_BRUSH, "Circle"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.MyAddShape(
|
|
|
|
ogl.RectangleShape(85, 50),
|
|
|
|
305, 60, wx.BLACK_PEN, wx.LIGHT_GREY_BRUSH, "Rectangle"
|
|
|
|
)
|
|
|
|
|
|
|
|
ds = self.MyAddShape(
|
|
|
|
DividedShape(140, 150, self),
|
|
|
|
495, 145, wx.BLACK_PEN, dsBrush, ''
|
|
|
|
)
|
|
|
|
|
|
|
|
self.MyAddShape(
|
|
|
|
DiamondShape(90, 90),
|
|
|
|
345, 235, wx.Pen(wx.BLUE, 3, wx.DOT), wx.RED_BRUSH, "Polygon"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.MyAddShape(
|
|
|
|
RoundedRectangleShape(95,70),
|
|
|
|
140, 255, wx.Pen(wx.RED, 2), rRectBrush, "Rounded Rect"
|
|
|
|
)
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2001-04-09 15:36:36 -04:00
|
|
|
bmp = images.getTest2Bitmap()
|
2003-12-08 20:23:28 -05:00
|
|
|
mask = wx.MaskColour(bmp, wx.BLUE)
|
2001-04-01 23:49:52 -04:00
|
|
|
bmp.SetMask(mask)
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
s = ogl.BitmapShape()
|
2001-04-01 23:49:52 -04:00
|
|
|
s.SetBitmap(bmp)
|
|
|
|
self.MyAddShape(s, 225, 150, None, None, "Bitmap")
|
|
|
|
|
2003-12-08 20:23:28 -05:00
|
|
|
dc = wx.ClientDC(self)
|
1999-09-10 02:25:47 -04:00
|
|
|
self.PrepareDC(dc)
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
for x in range(len(self.shapes)):
|
|
|
|
fromShape = self.shapes[x]
|
|
|
|
if x+1 == len(self.shapes):
|
|
|
|
toShape = self.shapes[0]
|
|
|
|
else:
|
|
|
|
toShape = self.shapes[x+1]
|
2003-12-08 20:23:28 -05:00
|
|
|
|
|
|
|
line = ogl.LineShape()
|
1999-09-10 02:25:47 -04:00
|
|
|
line.SetCanvas(self)
|
2003-12-08 20:23:28 -05:00
|
|
|
line.SetPen(wx.BLACK_PEN)
|
|
|
|
line.SetBrush(wx.BLACK_BRUSH)
|
|
|
|
line.AddArrow(ogl.ARROW_ARROW)
|
1999-09-10 02:25:47 -04:00
|
|
|
line.MakeLineControlPoints(2)
|
|
|
|
fromShape.AddLine(line, toShape)
|
|
|
|
self.diagram.AddShape(line)
|
2003-03-25 01:35:27 -05:00
|
|
|
line.Show(True)
|
1999-09-10 02:25:47 -04:00
|
|
|
|
|
|
|
# for some reason, the shapes have to be moved for the line to show up...
|
|
|
|
fromShape.Move(dc, fromShape.GetX(), fromShape.GetY())
|
|
|
|
|
2004-01-20 22:06:55 -05:00
|
|
|
self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
|
1999-09-10 02:25:47 -04:00
|
|
|
|
|
|
|
|
1999-09-30 03:11:20 -04:00
|
|
|
def MyAddShape(self, shape, x, y, pen, brush, text):
|
2003-03-25 01:35:27 -05:00
|
|
|
shape.SetDraggable(True, True)
|
1999-09-10 02:25:47 -04:00
|
|
|
shape.SetCanvas(self)
|
|
|
|
shape.SetX(x)
|
|
|
|
shape.SetY(y)
|
2001-04-01 23:49:52 -04:00
|
|
|
if pen: shape.SetPen(pen)
|
|
|
|
if brush: shape.SetBrush(brush)
|
|
|
|
if text: shape.AddText(text)
|
2003-12-08 20:23:28 -05:00
|
|
|
#shape.SetShadowMode(ogl.SHADOW_RIGHT)
|
1999-09-10 02:25:47 -04:00
|
|
|
self.diagram.AddShape(shape)
|
2003-03-25 01:35:27 -05:00
|
|
|
shape.Show(True)
|
1999-09-10 02:25:47 -04:00
|
|
|
|
|
|
|
evthandler = MyEvtHandler(self.log, self.frame)
|
|
|
|
evthandler.SetShape(shape)
|
|
|
|
evthandler.SetPreviousHandler(shape.GetEventHandler())
|
|
|
|
shape.SetEventHandler(evthandler)
|
|
|
|
|
|
|
|
self.shapes.append(shape)
|
2003-03-25 01:35:27 -05:00
|
|
|
return shape
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2001-04-01 23:49:52 -04:00
|
|
|
|
2002-05-11 23:20:42 -04:00
|
|
|
def OnDestroy(self, evt):
|
|
|
|
# Do some cleanup
|
1999-09-10 02:25:47 -04:00
|
|
|
for shape in self.diagram.GetShapeList():
|
|
|
|
if shape.GetParent() == None:
|
|
|
|
shape.SetCanvas(None)
|
|
|
|
shape.Destroy()
|
2003-12-08 20:23:28 -05:00
|
|
|
|
2001-05-18 02:53:25 -04:00
|
|
|
self.diagram.Destroy()
|
1999-09-10 02:25:47 -04:00
|
|
|
|
|
|
|
|
2001-09-26 22:11:47 -04:00
|
|
|
def OnBeginDragLeft(self, x, y, keys):
|
|
|
|
self.log.write("OnBeginDragLeft: %s, %s, %s\n" % (x, y, keys))
|
|
|
|
|
|
|
|
def OnEndDragLeft(self, x, y, keys):
|
|
|
|
self.log.write("OnEndDragLeft: %s, %s, %s\n" % (x, y, keys))
|
|
|
|
|
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
def runTest(frame, nb, log):
|
2003-11-12 16:34:20 -05:00
|
|
|
# This creates some pens and brushes that the OGL library uses.
|
|
|
|
# It should be called after the app object has been created, but
|
|
|
|
# before OGL is used.
|
2003-12-11 14:55:48 -05:00
|
|
|
ogl.OGLInitialize()
|
2003-11-12 16:34:20 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
win = TestWindow(nb, log, frame)
|
|
|
|
return win
|
2003-12-08 20:23:28 -05:00
|
|
|
|
1999-09-10 02:25:47 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2004-01-20 20:50:22 -05:00
|
|
|
# The OGL library holds some resources that need to be freed before
|
|
|
|
# the app shuts down.
|
1999-09-10 02:25:47 -04:00
|
|
|
class __Cleanup:
|
2004-01-20 20:50:22 -05:00
|
|
|
def __del__(self, cleanup=ogl.OGLCleanUp):
|
|
|
|
cleanup()
|
1999-09-10 02:25:47 -04:00
|
|
|
|
2004-01-20 20:50:22 -05:00
|
|
|
# When this module gets cleaned up by Python then __cu will be cleaned
|
|
|
|
# up and it's __dell__ is called, which will then call ogl.OGLCleanUp.
|
1999-09-10 02:25:47 -04:00
|
|
|
__cu = __Cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
overview = """\
|
|
|
|
The Object Graphics Library is a library supporting the creation and
|
|
|
|
manipulation of simple and complex graphic images on a canvas.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2003-03-25 01:35:27 -05:00
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys,os
|
|
|
|
import run
|
|
|
|
run.main(['', os.path.basename(sys.argv[0])])
|
|
|
|
|