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