]>
Commit | Line | Data |
---|---|---|
6a2c1874 RR |
1 | /* |
2 | * Program: canvas | |
3 | * | |
4 | * Author: Robert Roebling | |
5 | * | |
6 | * Copyright: (C) 1998, Robert Roebling | |
7 | * | |
8 | */ | |
9 | // For compilers that support precompilation, includes "wx/wx.h". | |
10 | #include "wx/wxprec.h" | |
11 | ||
12 | #ifdef __BORLANDC__ | |
13 | #pragma hdrstop | |
14 | #endif | |
15 | ||
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/wx.h" | |
18 | #endif | |
19 | ||
20 | #include <wx/image.h> | |
21 | #include <wx/file.h> | |
22 | #include <wx/timer.h> | |
239c1f50 | 23 | #include <wx/log.h> |
9f3b9298 | 24 | #include "wx/image.h" |
239c1f50 | 25 | |
6a2c1874 RR |
26 | |
27 | #include "smile.xpm" | |
28 | ||
29 | #include "wx/canvas/canvas.h" | |
30 | ||
31 | // derived classes | |
32 | ||
9f3b9298 KH |
33 | |
34 | class MywxCanvasImage: public wxCanvasImage | |
35 | { | |
36 | public: | |
37 | MywxCanvasImage( const wxImage &image, double x, double y, double w, double h ); | |
38 | ||
39 | void MywxCanvasImage::OnMouse(wxMouseEvent &event); | |
40 | ||
41 | DECLARE_EVENT_TABLE() | |
42 | }; | |
43 | ||
44 | BEGIN_EVENT_TABLE(MywxCanvasImage,wxCanvasImage) | |
45 | EVT_MOUSE_EVENTS( MywxCanvasImage::OnMouse ) | |
46 | END_EVENT_TABLE() | |
47 | ||
48 | MywxCanvasImage::MywxCanvasImage( const wxImage &image, double x, double y, double w, double h ) | |
49 | :wxCanvasImage( image, x, y, w, h ) | |
50 | { | |
51 | } | |
52 | ||
53 | void MywxCanvasImage::OnMouse(wxMouseEvent &event) | |
54 | { | |
41328253 RR |
55 | static int dx=0; |
56 | static int dy=0; | |
9f3b9298 KH |
57 | |
58 | int x = event.GetX(); | |
59 | int y = event.GetY(); | |
872f1044 | 60 | if (event.LeftDown()) |
21840a6c | 61 | { |
872f1044 RR |
62 | dx=x; |
63 | dy=y; | |
9f3b9298 | 64 | CaptureMouse(); |
872f1044 RR |
65 | } |
66 | else if (event.LeftUp()) | |
67 | { | |
68 | ReleaseMouse(); | |
9f3b9298 KH |
69 | } |
70 | else if (IsCapturedMouse()) | |
71 | { | |
872f1044 RR |
72 | Move(m_area.x+x-dx,m_area.y+y-dy); |
73 | m_owner->UpdateNow(); | |
9f3b9298 KH |
74 | } |
75 | } | |
76 | ||
fcbb6b37 KH |
77 | class MywxCanvasObjectGroupRef: public wxCanvasObjectGroupRef |
78 | { | |
79 | public: | |
80 | MywxCanvasObjectGroupRef(double x, double y, wxCanvasObjectGroup* group); | |
81 | ||
82 | void OnMouse(wxMouseEvent &event); | |
83 | ||
84 | DECLARE_EVENT_TABLE() | |
85 | }; | |
86 | ||
87 | BEGIN_EVENT_TABLE(MywxCanvasObjectGroupRef,wxCanvasObjectGroupRef) | |
88 | EVT_MOUSE_EVENTS( MywxCanvasObjectGroupRef::OnMouse ) | |
89 | END_EVENT_TABLE() | |
90 | ||
91 | MywxCanvasObjectGroupRef::MywxCanvasObjectGroupRef(double x, double y,wxCanvasObjectGroup* group) | |
92 | :wxCanvasObjectGroupRef(x,y,group) | |
93 | { | |
94 | } | |
95 | ||
96 | void MywxCanvasObjectGroupRef::OnMouse(wxMouseEvent &event) | |
97 | { | |
21840a6c RR |
98 | static int dx=0; |
99 | static int dy=0; | |
fcbb6b37 KH |
100 | |
101 | //new position of object | |
102 | int x = m_owner->GetDeviceX( event.GetX()); | |
103 | int y = m_owner->GetDeviceY( event.GetY()); | |
104 | ||
872f1044 | 105 | if (event.LeftDown()) |
21840a6c | 106 | { |
872f1044 RR |
107 | dx=x; |
108 | dy=y; | |
fcbb6b37 | 109 | CaptureMouse(); |
872f1044 RR |
110 | } |
111 | else if (event.LeftUp()) | |
112 | { | |
113 | ReleaseMouse(); | |
fcbb6b37 KH |
114 | } |
115 | else if (IsCapturedMouse()) | |
116 | { | |
872f1044 RR |
117 | Move(m_x+x-dx,m_y+y-dy); |
118 | m_owner->UpdateNow(); | |
fcbb6b37 KH |
119 | } |
120 | } | |
121 | ||
6a2c1874 RR |
122 | class MyFrame; |
123 | class MyApp; | |
124 | ||
125 | // MyFrame | |
126 | ||
127 | class MyFrame: public wxFrame | |
128 | { | |
129 | public: | |
130 | MyFrame(); | |
131 | ~MyFrame(); | |
132 | ||
133 | void OnAbout( wxCommandEvent &event ); | |
134 | void OnNewFrame( wxCommandEvent &event ); | |
135 | void OnQuit( wxCommandEvent &event ); | |
136 | void OnTimer( wxTimerEvent &event ); | |
137 | ||
239c1f50 RR |
138 | wxCanvas *m_canvas; |
139 | wxCanvasObject *m_sm1; | |
140 | wxCanvasObject *m_sm2; | |
141 | wxCanvasObject *m_sm3; | |
142 | wxCanvasObject *m_sm4; | |
fcbb6b37 KH |
143 | |
144 | MywxCanvasObjectGroupRef *m_ref; | |
145 | MywxCanvasObjectGroupRef *m_ref2; | |
146 | ||
239c1f50 RR |
147 | wxTimer *m_timer; |
148 | wxTextCtrl *m_log; | |
6a2c1874 RR |
149 | |
150 | private: | |
151 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
152 | DECLARE_EVENT_TABLE() | |
153 | }; | |
154 | ||
155 | // MyApp | |
156 | ||
157 | class MyApp: public wxApp | |
158 | { | |
159 | public: | |
160 | virtual bool OnInit(); | |
cba349dc VZ |
161 | |
162 | const wxString& GetFontPath() const { return m_fontpath; } | |
163 | ||
164 | private: | |
165 | wxString m_fontpath; | |
6a2c1874 RR |
166 | }; |
167 | ||
168 | // main program | |
169 | ||
170 | IMPLEMENT_APP(MyApp) | |
171 | ||
172 | // MyFrame | |
173 | ||
174 | const int ID_QUIT = 108; | |
175 | const int ID_ABOUT = 109; | |
176 | ||
177 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
178 | ||
179 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
180 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
181 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
182 | EVT_TIMER (-1, MyFrame::OnTimer) | |
183 | END_EVENT_TABLE() | |
184 | ||
185 | MyFrame::MyFrame() | |
186 | : wxFrame( (wxFrame *)NULL, -1, "wxCanvas sample", | |
872f1044 | 187 | wxPoint(20,20), wxSize(470,460) ) |
6a2c1874 | 188 | { |
239c1f50 RR |
189 | wxMenu *file_menu = new wxMenu(); |
190 | file_menu->Append( ID_ABOUT, "&About..."); | |
191 | file_menu->AppendSeparator(); | |
192 | file_menu->Append( ID_QUIT, "E&xit"); | |
193 | ||
194 | wxMenuBar *menu_bar = new wxMenuBar(); | |
195 | menu_bar->Append(file_menu, "&File"); | |
196 | ||
197 | SetMenuBar( menu_bar ); | |
198 | ||
199 | CreateStatusBar(2); | |
200 | int widths[] = { -1, 100 }; | |
201 | SetStatusWidths( 2, widths ); | |
202 | ||
203 | m_canvas = new wxCanvas( this, -1, wxPoint(0,0), wxSize(10,10) ); | |
9f3b9298 | 204 | |
872f1044 | 205 | m_canvas->SetArea( 1000,1000 ); |
239c1f50 | 206 | m_canvas->SetColour( 255, 255, 255 ); |
6a2c1874 | 207 | |
fcbb6b37 | 208 | |
239c1f50 RR |
209 | wxBitmap bitmap( smile_xpm ); |
210 | wxImage image( bitmap ); | |
6a2c1874 | 211 | |
33ebcd80 | 212 | m_sm1 = new wxCanvasImage( image, 0,70,32,32 ); |
239c1f50 | 213 | m_canvas->Append( m_sm1 ); |
fcbb6b37 | 214 | |
239c1f50 RR |
215 | int i; |
216 | for (i = 10; i < 300; i+=10) | |
27d1065d RR |
217 | { |
218 | wxCanvasRect *r = new wxCanvasRect( i,50,3,140 ); | |
219 | r->SetBrush( *wxRED_BRUSH ); | |
220 | m_canvas->Append( r ); | |
221 | } | |
6a2c1874 | 222 | |
4dbd4ee6 | 223 | m_sm2 = new wxCanvasImage( image, 0,140,24,24 ); |
239c1f50 | 224 | m_canvas->Append( m_sm2 ); |
6a2c1874 | 225 | |
239c1f50 | 226 | for (i = 15; i < 300; i+=10) |
27d1065d | 227 | m_canvas->Append( new wxCanvasRect( i,50,3,140 ) ); |
cba349dc | 228 | |
239c1f50 RR |
229 | wxButton *button = new wxButton( m_canvas, -1, "Hello", wxPoint(80,50) ); |
230 | m_canvas->Append( new wxCanvasControl( button ) ); | |
cba349dc | 231 | |
96f5fca9 RR |
232 | m_canvas->Append( new wxCanvasText( "How are you?", 180, 10, |
233 | wxGetApp().GetFontPath() + "/times.ttf", 8 ) ); | |
234 | ||
235 | m_canvas->Append( new wxCanvasText( "How are you?", 180, 20, | |
236 | wxGetApp().GetFontPath() + "/times.ttf", 10 ) ); | |
237 | ||
238 | m_canvas->Append( new wxCanvasText( "How are you?", 180, 30, | |
239 | wxGetApp().GetFontPath() + "/times.ttf", 12 ) ); | |
cba349dc | 240 | |
4dbd4ee6 | 241 | m_sm3 = new wxCanvasImage( image, 0,210,32,32 ); |
239c1f50 | 242 | m_canvas->Append( m_sm3 ); |
cba349dc | 243 | |
239c1f50 | 244 | for (i = 10; i < 300; i+=10) |
27d1065d | 245 | m_canvas->Append( new wxCanvasLine( 10,-15,i,300 ) ); |
9f3b9298 | 246 | |
9f3b9298 | 247 | m_sm4 = new MywxCanvasImage( image, 0,270,64,32 ); |
239c1f50 | 248 | m_canvas->Append( m_sm4 ); |
cba349dc | 249 | |
fcbb6b37 | 250 | |
61b64bd9 RR |
251 | // m_canvas->Append( new wxCanvasLine( 10,-1500e6,50,300000e6, 0,255,0 ) ); |
252 | // m_canvas->Append( new wxCanvasLine( 10,-150000,50,300000, 0,255,0 ) ); | |
253 | ||
33ebcd80 | 254 | /* |
fcbb6b37 KH |
255 | //make a group of wxCanvasObjects |
256 | wxCanvasObjectGroup* group1 = new wxCanvasObjectGroup(); | |
27d1065d | 257 | group1->Prepend( new wxCanvasLine( 10,-35,50,190 ) ); |
fcbb6b37 KH |
258 | group1->Prepend( new wxCanvasImage( image, 4,38,32,32 ) ); |
259 | group1->Prepend( new wxCanvasRect(20,-20,50,170,0,20,240 ) ); | |
fcbb6b37 | 260 | |
fcbb6b37 KH |
261 | //make another group of wxCanvasObjects |
262 | wxCanvasObjectGroup* group2 = new wxCanvasObjectGroup(); | |
263 | group2->Prepend( new wxCanvasImage( image, 60,38,52,32 ) ); | |
264 | group2->Prepend( new wxCanvasRect(10,20,104,52,10,40,10 ) ); | |
265 | ||
266 | //this a reference to group2 put into group1 | |
267 | wxCanvasObjectGroupRef* m_subref = new wxCanvasObjectGroupRef(60,50, group2); | |
268 | group1->Prepend( m_subref ); | |
269 | ||
270 | //now make two refrences to group1 into root group of the canvas | |
271 | m_ref = new MywxCanvasObjectGroupRef(40,200, group1); | |
272 | m_canvas->Prepend( m_ref ); | |
273 | ||
274 | m_ref2 = new MywxCanvasObjectGroupRef(80,350, group1); | |
275 | m_canvas->Prepend( m_ref2 ); | |
33ebcd80 | 276 | */ |
fcbb6b37 | 277 | |
239c1f50 RR |
278 | m_log = new wxTextCtrl( this, -1, "", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE ); |
279 | wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); | |
280 | delete old_log; | |
9f3b9298 | 281 | |
239c1f50 | 282 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
9f3b9298 | 283 | |
239c1f50 RR |
284 | topsizer->Add( m_canvas, 1, wxEXPAND ); |
285 | topsizer->Add( m_log, 0, wxEXPAND ); | |
d1f9b206 | 286 | |
239c1f50 RR |
287 | SetAutoLayout( TRUE ); |
288 | SetSizer( topsizer ); | |
cba349dc | 289 | |
239c1f50 RR |
290 | m_timer = new wxTimer( this ); |
291 | m_timer->Start( 100, FALSE ); | |
6a2c1874 RR |
292 | } |
293 | ||
294 | MyFrame::~MyFrame() | |
295 | { | |
296 | delete m_timer; | |
297 | } | |
298 | ||
299 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
300 | { | |
301 | Close( TRUE ); | |
302 | } | |
303 | ||
304 | void MyFrame::OnTimer( wxTimerEvent &WXUNUSED(event) ) | |
305 | { | |
fcbb6b37 | 306 | m_sm1->Move( m_sm1->GetX()+1, m_sm1 ->GetY() ); |
21544859 | 307 | m_sm2->Move( m_sm2->GetX()+1, m_sm2->GetY() ); |
239c1f50 | 308 | m_sm3->Move( m_sm3->GetX()+1, m_sm3->GetY() ); |
fcbb6b37 | 309 | m_sm4->Move( m_sm4->GetX()+2, m_sm4->GetY() ); |
872f1044 | 310 | /* |
fcbb6b37 KH |
311 | m_ref->Move( m_ref->GetPosX()+1, m_ref->GetPosY() ); |
312 | m_ref2->Move( m_ref2->GetPosX()+2, m_ref2->GetPosY() ); | |
872f1044 RR |
313 | */ |
314 | ||
6a2c1874 RR |
315 | wxWakeUpIdle(); |
316 | } | |
317 | ||
318 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
319 | { | |
320 | (void)wxMessageBox( "wxCanvas demo\n" | |
321 | "Robert Roebling (c) 1998,2000", | |
322 | "About wxCanvas Demo", wxICON_INFORMATION | wxOK ); | |
323 | } | |
324 | ||
325 | //----------------------------------------------------------------------------- | |
326 | // MyApp | |
327 | //----------------------------------------------------------------------------- | |
328 | ||
329 | bool MyApp::OnInit() | |
330 | { | |
cba349dc VZ |
331 | m_fontpath = getenv("TRUETYPE"); |
332 | if ( !m_fontpath ) | |
333 | { | |
334 | wxLogError("Please set env var TRUETYPE to the path where times.ttf lives."); | |
335 | ||
336 | return FALSE; | |
337 | ||
338 | } | |
339 | ||
6a2c1874 RR |
340 | #if wxUSE_LIBPNG |
341 | wxImage::AddHandler( new wxPNGHandler ); | |
342 | #endif | |
343 | ||
344 | wxFrame *frame = new MyFrame(); | |
345 | frame->Show( TRUE ); | |
346 | ||
347 | return TRUE; | |
348 | } | |
349 | ||
350 |