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