| 1 | #!/bin/env python |
| 2 | #---------------------------------------------------------------------------- |
| 3 | # Name: test2.py |
| 4 | # Purpose: Testing GDI stuff and events. |
| 5 | # |
| 6 | # Author: Robin Dunn |
| 7 | # |
| 8 | # Created: |
| 9 | # RCS-ID: $Id$ |
| 10 | # Copyright: (c) 1998 by Total Control Software |
| 11 | # Licence: wxWindows license |
| 12 | #---------------------------------------------------------------------------- |
| 13 | |
| 14 | |
| 15 | from wxPython.wx import * |
| 16 | |
| 17 | |
| 18 | #--------------------------------------------------------------------------- |
| 19 | |
| 20 | |
| 21 | |
| 22 | class MyCanvas(wxScrolledWindow): |
| 23 | def __init__(self, parent): |
| 24 | wxScrolledWindow.__init__(self, parent, -1, wxPoint(0, 0), wxPyDefaultSize, wxSUNKEN_BORDER) |
| 25 | |
| 26 | self.SetBackgroundColour(wxNamedColor("WHITE")) |
| 27 | self.Connect(-1, -1, wxEVT_LEFT_DOWN, self.OnLeftButtonEvent) |
| 28 | self.Connect(-1, -1, wxEVT_LEFT_UP, self.OnLeftButtonEvent) |
| 29 | self.Connect(-1, -1, wxEVT_MOTION, self.OnLeftButtonEvent) |
| 30 | |
| 31 | self.SetCursor(wxStockCursor(wxCURSOR_PENCIL)) |
| 32 | bmp = wxBitmap('bitmaps/test2.bmp', wxBITMAP_TYPE_BMP) |
| 33 | self.bmp = bmp |
| 34 | |
| 35 | self.SetScrollbars(20, 20, 50, 50) |
| 36 | |
| 37 | self.lines = [] |
| 38 | |
| 39 | |
| 40 | |
| 41 | def OnPaint(self, event): |
| 42 | dc = wxPaintDC(self) |
| 43 | self.PrepareDC(dc) |
| 44 | self.DoDrawing(dc) |
| 45 | |
| 46 | |
| 47 | |
| 48 | def DoDrawing(self, dc): |
| 49 | dc.BeginDrawing() |
| 50 | #dc.Clear() |
| 51 | pen1 = wxPen(wxNamedColour('RED')) |
| 52 | dc.SetPen(pen1) |
| 53 | dc.DrawRectangle(5, 5, 50, 50) |
| 54 | |
| 55 | dc.SetBrush(wxLIGHT_GREY_BRUSH) |
| 56 | dc.SetPen(wxPen(wxNamedColour('BLUE'), 4)) |
| 57 | dc.DrawRectangle(15, 15, 50, 50) |
| 58 | |
| 59 | font = wxFont(14, wxSWISS, wxNORMAL, wxNORMAL) |
| 60 | dc.SetFont(font) |
| 61 | dc.SetTextForeground(wxColour(0xFF, 0x20, 0xFF)) |
| 62 | te = dc.GetTextExtent("Hello World") |
| 63 | dc.DrawText("Hello World", 60, 65) |
| 64 | |
| 65 | dc.SetPen(wxPen(wxNamedColour('VIOLET'), 4)) |
| 66 | dc.DrawLine(5, 65+te[1], 60+te[0], 65+te[1]) |
| 67 | |
| 68 | lst = [(100,110), (150,110), (150,160), (100,160)] |
| 69 | dc.DrawLines(lst, -60) |
| 70 | dc.SetPen(wxGREY_PEN) |
| 71 | dc.DrawPolygon(lst, 75) |
| 72 | dc.SetPen(wxGREEN_PEN) |
| 73 | dc.DrawSpline(lst+[(100,100)]) |
| 74 | |
| 75 | dc.DrawBitmap(self.bmp, 200, 20) |
| 76 | dc.SetTextForeground(wxColour(0, 0xFF, 0x80)) |
| 77 | dc.DrawText("a bitmap", 200, 80) |
| 78 | |
| 79 | self.DrawSavedLines(dc) |
| 80 | dc.EndDrawing() |
| 81 | |
| 82 | |
| 83 | def DrawSavedLines(self, dc): |
| 84 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) |
| 85 | for line in self.lines: |
| 86 | for coords in line: |
| 87 | apply(dc.DrawLine, coords) |
| 88 | |
| 89 | |
| 90 | |
| 91 | def OnLeftButtonEvent(self, event): |
| 92 | if event.LeftDown(): |
| 93 | self.x, self.y = event.GetX(), event.GetY() |
| 94 | self.curLine = [] |
| 95 | elif event.Dragging(): |
| 96 | dc = wxClientDC(self) |
| 97 | dc.BeginDrawing() |
| 98 | dc.SetPen(wxPen(wxNamedColour('MEDIUM FOREST GREEN'), 4)) |
| 99 | coords = (self.x, self.y, event.GetX(), event.GetY()) |
| 100 | self.curLine.append(coords) |
| 101 | apply(dc.DrawLine, coords) |
| 102 | self.x, self.y = event.GetX(), event.GetY() |
| 103 | dc.EndDrawing() |
| 104 | elif event.LeftUp(): |
| 105 | self.lines.append(self.curLine) |
| 106 | self.curLine = [] |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | #--------------------------------------------------------------------------- |
| 113 | |
| 114 | class MyFrame(wxFrame): |
| 115 | def __init__(self, parent, id, title): |
| 116 | wxFrame.__init__(self, parent, id, title, wxPyDefaultPosition, wxSize(320, 200)) |
| 117 | self.canvas = MyCanvas(self) |
| 118 | |
| 119 | def OnCloseWindow(self, event): |
| 120 | self.Destroy() |
| 121 | |
| 122 | def OnSize(self, event): |
| 123 | size = self.GetClientSize() |
| 124 | self.canvas.SetDimensions(5, 5, size.width-10, size.height-10) |
| 125 | |
| 126 | |
| 127 | #--------------------------------------------------------------------------- |
| 128 | |
| 129 | |
| 130 | class MyApp(wxApp): |
| 131 | def OnInit(self): |
| 132 | frame = MyFrame(NULL, -1, "Test 2") |
| 133 | frame.Show(true) |
| 134 | self.SetTopWindow(frame) |
| 135 | return true |
| 136 | |
| 137 | |
| 138 | #--------------------------------------------------------------------------- |
| 139 | |
| 140 | |
| 141 | def main(): |
| 142 | app = MyApp(0) |
| 143 | app.MainLoop() |
| 144 | |
| 145 | |
| 146 | def t(): |
| 147 | import pdb |
| 148 | pdb.run('main()') |
| 149 | |
| 150 | if __name__ == '__main__': |
| 151 | main() |
| 152 | |
| 153 | |
| 154 | #---------------------------------------------------------------------------- |
| 155 | # |
| 156 | # $Log$ |
| 157 | # Revision 1.4 2001/02/16 08:19:38 robind |
| 158 | # Copied/merged from the 2.2 branch. |
| 159 | # |
| 160 | # Changes needed to build with new event system |
| 161 | # |
| 162 | # Revision 1.1.2.2 2001/01/30 20:54:16 robind |
| 163 | # |
| 164 | # Gobs of changes move from the main trunk to the 2.2 branch in |
| 165 | # preparataion for 2.2.5 release. See CHANGES.txt for details. |
| 166 | # |
| 167 | # Revision 1.3 2000/10/30 21:05:22 robind |
| 168 | # |
| 169 | # Merged wxPython 2.2.2 over to the main branch |
| 170 | # |
| 171 | # Revision 1.1.2.1 2000/05/16 02:07:01 RD |
| 172 | # |
| 173 | # Moved and reorganized wxPython directories |
| 174 | # |
| 175 | # Now builds into an intermediate wxPython package directory before |
| 176 | # installing |
| 177 | # |
| 178 | # Revision 1.3 1999/04/30 03:29:53 RD |
| 179 | # |
| 180 | # wxPython 2.0b9, first phase (win32) |
| 181 | # Added gobs of stuff, see wxPython/README.txt for details |
| 182 | # |
| 183 | # Revision 1.2.4.1 1999/03/27 23:30:00 RD |
| 184 | # |
| 185 | # wxPython 2.0b8 |
| 186 | # Python thread support |
| 187 | # various minor additions |
| 188 | # various minor fixes |
| 189 | # |
| 190 | # Revision 1.2 1998/12/15 20:44:34 RD |
| 191 | # Changed the import semantics from "from wxPython import *" to "from |
| 192 | # wxPython.wx import *" This is for people who are worried about |
| 193 | # namespace pollution, they can use "from wxPython import wx" and then |
| 194 | # prefix all the wxPython identifiers with "wx." |
| 195 | # |
| 196 | # Added wxTaskbarIcon for wxMSW. |
| 197 | # |
| 198 | # Made the events work for wxGrid. |
| 199 | # |
| 200 | # Added wxConfig. |
| 201 | # |
| 202 | # Added wxMiniFrame for wxGTK, (untested.) |
| 203 | # |
| 204 | # Changed many of the args and return values that were pointers to gdi |
| 205 | # objects to references to reflect changes in the wxWindows API. |
| 206 | # |
| 207 | # Other assorted fixes and additions. |
| 208 | # |
| 209 | # Revision 1.1 1998/08/09 08:28:05 RD |
| 210 | # Initial version |
| 211 | # |
| 212 | # |