]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/canvas/test/test.cpp
Corrected typo
[wxWidgets.git] / contrib / samples / canvas / test / test.cpp
CommitLineData
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
34class MywxCanvasImage: public wxCanvasImage
35{
36public:
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
44BEGIN_EVENT_TABLE(MywxCanvasImage,wxCanvasImage)
45 EVT_MOUSE_EVENTS( MywxCanvasImage::OnMouse )
46END_EVENT_TABLE()
47
48MywxCanvasImage::MywxCanvasImage( const wxImage &image, double x, double y, double w, double h )
49 :wxCanvasImage( image, x, y, w, h )
50{
51}
52
53void MywxCanvasImage::OnMouse(wxMouseEvent &event)
54{
21840a6c 55 static bool first=FALSE;
41328253
RR
56 static int dx=0;
57 static int dy=0;
9f3b9298
KH
58
59 int x = event.GetX();
60 int y = event.GetY();
61 if (event.m_leftDown)
21840a6c
RR
62 {
63 if (!first)
9f3b9298 64 {
21840a6c 65 first=TRUE;
9f3b9298
KH
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();
21840a6c
RR
75 first=FALSE;
76 dx=0;
77 dy=0;
9f3b9298
KH
78 }
79}
80
fcbb6b37
KH
81class MywxCanvasObjectGroupRef: public wxCanvasObjectGroupRef
82{
83public:
84 MywxCanvasObjectGroupRef(double x, double y, wxCanvasObjectGroup* group);
85
86 void OnMouse(wxMouseEvent &event);
87
88 DECLARE_EVENT_TABLE()
89};
90
91BEGIN_EVENT_TABLE(MywxCanvasObjectGroupRef,wxCanvasObjectGroupRef)
92 EVT_MOUSE_EVENTS( MywxCanvasObjectGroupRef::OnMouse )
93END_EVENT_TABLE()
94
95MywxCanvasObjectGroupRef::MywxCanvasObjectGroupRef(double x, double y,wxCanvasObjectGroup* group)
96 :wxCanvasObjectGroupRef(x,y,group)
97{
98}
99
100void MywxCanvasObjectGroupRef::OnMouse(wxMouseEvent &event)
101{
21840a6c
RR
102 static bool first=FALSE;
103 static int dx=0;
104 static int dy=0;
fcbb6b37
KH
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)
21840a6c
RR
111 {
112 if (!first)
fcbb6b37 113 {
21840a6c 114 first=FALSE;
fcbb6b37
KH
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();
21840a6c
RR
124 first=FALSE;
125 dx=0;
126 dy=0;
fcbb6b37
KH
127 }
128}
129
6a2c1874
RR
130class MyFrame;
131class MyApp;
132
133// MyFrame
134
135class MyFrame: public wxFrame
136{
137public:
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
239c1f50
RR
146 wxCanvas *m_canvas;
147 wxCanvasObject *m_sm1;
148 wxCanvasObject *m_sm2;
149 wxCanvasObject *m_sm3;
150 wxCanvasObject *m_sm4;
fcbb6b37
KH
151
152 MywxCanvasObjectGroupRef *m_ref;
153 MywxCanvasObjectGroupRef *m_ref2;
154
239c1f50
RR
155 wxTimer *m_timer;
156 wxTextCtrl *m_log;
6a2c1874
RR
157
158private:
159 DECLARE_DYNAMIC_CLASS(MyFrame)
160 DECLARE_EVENT_TABLE()
161};
162
163// MyApp
164
165class MyApp: public wxApp
166{
167public:
168 virtual bool OnInit();
cba349dc
VZ
169
170 const wxString& GetFontPath() const { return m_fontpath; }
171
172private:
173 wxString m_fontpath;
6a2c1874
RR
174};
175
176// main program
177
178IMPLEMENT_APP(MyApp)
179
180// MyFrame
181
182const int ID_QUIT = 108;
183const int ID_ABOUT = 109;
184
185IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
186
187BEGIN_EVENT_TABLE(MyFrame,wxFrame)
188 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
189 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
190 EVT_TIMER (-1, MyFrame::OnTimer)
191END_EVENT_TABLE()
192
193MyFrame::MyFrame()
194 : wxFrame( (wxFrame *)NULL, -1, "wxCanvas sample",
fcbb6b37 195 wxPoint(20,20), wxSize(470,860) )
6a2c1874 196{
239c1f50
RR
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) );
9f3b9298 212
61b64bd9 213 m_canvas->SetArea( 1400, 600 );
239c1f50 214 m_canvas->SetColour( 255, 255, 255 );
6a2c1874 215
fcbb6b37 216
239c1f50
RR
217 wxBitmap bitmap( smile_xpm );
218 wxImage image( bitmap );
6a2c1874 219
4dbd4ee6 220 m_sm1 = new wxCanvasImage( image, 0,70,16,16 );
239c1f50 221 m_canvas->Append( m_sm1 );
fcbb6b37 222
239c1f50
RR
223 int i;
224 for (i = 10; i < 300; i+=10)
225 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
6a2c1874 226
4dbd4ee6 227 m_sm2 = new wxCanvasImage( image, 0,140,24,24 );
239c1f50 228 m_canvas->Append( m_sm2 );
6a2c1874 229
239c1f50
RR
230 for (i = 15; i < 300; i+=10)
231 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
cba349dc 232
239c1f50
RR
233 wxButton *button = new wxButton( m_canvas, -1, "Hello", wxPoint(80,50) );
234 m_canvas->Append( new wxCanvasControl( button ) );
cba349dc 235
239c1f50
RR
236 m_canvas->Append( new wxCanvasText( "Hello", 180, 50,
237 wxGetApp().GetFontPath() + "/times.ttf", 20 ) );
cba349dc 238
4dbd4ee6 239 m_sm3 = new wxCanvasImage( image, 0,210,32,32 );
239c1f50 240 m_canvas->Append( m_sm3 );
cba349dc 241
239c1f50 242 for (i = 10; i < 300; i+=10)
9f3b9298
KH
243 m_canvas->Append( new wxCanvasLine( 10,-15,i,300, 0,255,0 ) );
244
9f3b9298 245 m_sm4 = new MywxCanvasImage( image, 0,270,64,32 );
239c1f50 246 m_canvas->Append( m_sm4 );
cba349dc 247
fcbb6b37 248
61b64bd9
RR
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
fcbb6b37
KH
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
239c1f50
RR
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;
9f3b9298 280
239c1f50 281 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
9f3b9298 282
239c1f50
RR
283 topsizer->Add( m_canvas, 1, wxEXPAND );
284 topsizer->Add( m_log, 0, wxEXPAND );
d1f9b206 285
239c1f50
RR
286 SetAutoLayout( TRUE );
287 SetSizer( topsizer );
cba349dc 288
239c1f50
RR
289 m_timer = new wxTimer( this );
290 m_timer->Start( 100, FALSE );
6a2c1874
RR
291}
292
293MyFrame::~MyFrame()
294{
295 delete m_timer;
296}
297
298void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
299{
300 Close( TRUE );
301}
302
303void MyFrame::OnTimer( wxTimerEvent &WXUNUSED(event) )
304{
fcbb6b37 305 m_sm1->Move( m_sm1->GetX()+1, m_sm1 ->GetY() );
21544859 306 m_sm2->Move( m_sm2->GetX()+1, m_sm2->GetY() );
239c1f50 307 m_sm3->Move( m_sm3->GetX()+1, m_sm3->GetY() );
fcbb6b37
KH
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
6a2c1874
RR
312 wxWakeUpIdle();
313}
314
315void 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
326bool MyApp::OnInit()
327{
cba349dc
VZ
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
6a2c1874
RR
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