]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/canvas/test/test.cpp
added moving an object with the mouse
[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 dx=0;
57 static dy=0;
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
79 class MyFrame;
80 class MyApp;
81
82 // MyFrame
83
84 class MyFrame: public wxFrame
85 {
86 public:
87 MyFrame();
88 ~MyFrame();
89
90 void OnAbout( wxCommandEvent &event );
91 void OnNewFrame( wxCommandEvent &event );
92 void OnQuit( wxCommandEvent &event );
93 void OnTimer( wxTimerEvent &event );
94
95 wxCanvas *m_canvas;
96 wxCanvasObject *m_sm1;
97 wxCanvasObject *m_sm2;
98 wxCanvasObject *m_sm3;
99 wxCanvasObject *m_sm4;
100 wxTimer *m_timer;
101 wxTextCtrl *m_log;
102
103 private:
104 DECLARE_DYNAMIC_CLASS(MyFrame)
105 DECLARE_EVENT_TABLE()
106 };
107
108 // MyApp
109
110 class MyApp: public wxApp
111 {
112 public:
113 virtual bool OnInit();
114
115 const wxString& GetFontPath() const { return m_fontpath; }
116
117 private:
118 wxString m_fontpath;
119 };
120
121 // main program
122
123 IMPLEMENT_APP(MyApp)
124
125 // MyFrame
126
127 const int ID_QUIT = 108;
128 const int ID_ABOUT = 109;
129
130 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
131
132 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
133 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
134 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
135 EVT_TIMER (-1, MyFrame::OnTimer)
136 END_EVENT_TABLE()
137
138 MyFrame::MyFrame()
139 : wxFrame( (wxFrame *)NULL, -1, "wxCanvas sample",
140 wxPoint(20,20), wxSize(470,460) )
141 {
142 wxMenu *file_menu = new wxMenu();
143 file_menu->Append( ID_ABOUT, "&About...");
144 file_menu->AppendSeparator();
145 file_menu->Append( ID_QUIT, "E&xit");
146
147 wxMenuBar *menu_bar = new wxMenuBar();
148 menu_bar->Append(file_menu, "&File");
149
150 SetMenuBar( menu_bar );
151
152 CreateStatusBar(2);
153 int widths[] = { -1, 100 };
154 SetStatusWidths( 2, widths );
155
156 m_canvas = new wxCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
157
158 m_canvas->Freeze();
159
160 m_canvas->SetArea( 400, 600 );
161 m_canvas->SetColour( 255, 255, 255 );
162
163 wxBitmap bitmap( smile_xpm );
164 wxImage image( bitmap );
165
166 m_sm1 = new wxCanvasImage( image, 0,70,16,16 );
167 m_canvas->Append( m_sm1 );
168
169 int i;
170 for (i = 10; i < 300; i+=10)
171 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
172
173 m_sm2 = new wxCanvasImage( image, 0,140,24,24 );
174 m_canvas->Append( m_sm2 );
175
176 for (i = 15; i < 300; i+=10)
177 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
178
179 wxButton *button = new wxButton( m_canvas, -1, "Hello", wxPoint(80,50) );
180 m_canvas->Append( new wxCanvasControl( button ) );
181
182 m_canvas->Append( new wxCanvasText( "Hello", 180, 50,
183 wxGetApp().GetFontPath() + "/times.ttf", 20 ) );
184
185 m_sm3 = new wxCanvasImage( image, 0,210,32,32 );
186 m_canvas->Append( m_sm3 );
187
188 for (i = 10; i < 300; i+=10)
189 m_canvas->Append( new wxCanvasLine( 10,-15,i,300, 0,255,0 ) );
190
191 // m_canvas->Append( new wxCanvasLine( 10,-1500e6,50,300000e6, 0,255,0 ) );
192 // m_canvas->Append( new wxCanvasLine( 10,-150000,50,300000, 0,255,0 ) );
193
194 m_sm4 = new MywxCanvasImage( image, 0,270,64,32 );
195 m_canvas->Append( m_sm4 );
196
197 m_canvas->Thaw();
198
199 m_log = new wxTextCtrl( this, -1, "", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
200 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
201 delete old_log;
202
203 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
204
205 topsizer->Add( m_canvas, 1, wxEXPAND );
206 topsizer->Add( m_log, 0, wxEXPAND );
207
208 SetAutoLayout( TRUE );
209 SetSizer( topsizer );
210
211 m_timer = new wxTimer( this );
212 m_timer->Start( 100, FALSE );
213 }
214
215 MyFrame::~MyFrame()
216 {
217 delete m_timer;
218 }
219
220 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
221 {
222 Close( TRUE );
223 }
224
225 void MyFrame::OnTimer( wxTimerEvent &WXUNUSED(event) )
226 {
227 m_sm1->Move( m_sm1->GetX()+1, m_sm1->GetY() );
228 m_sm2->Move( m_sm2->GetX()+1, m_sm2->GetY() );
229 m_sm3->Move( m_sm3->GetX()+1, m_sm3->GetY() );
230 m_sm4->Move( m_sm4->GetX()+1, m_sm4->GetY() );
231 wxWakeUpIdle();
232 }
233
234 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
235 {
236 (void)wxMessageBox( "wxCanvas demo\n"
237 "Robert Roebling (c) 1998,2000",
238 "About wxCanvas Demo", wxICON_INFORMATION | wxOK );
239 }
240
241 //-----------------------------------------------------------------------------
242 // MyApp
243 //-----------------------------------------------------------------------------
244
245 bool MyApp::OnInit()
246 {
247 m_fontpath = getenv("TRUETYPE");
248 if ( !m_fontpath )
249 {
250 wxLogError("Please set env var TRUETYPE to the path where times.ttf lives.");
251
252 return FALSE;
253
254 }
255
256 #if wxUSE_LIBPNG
257 wxImage::AddHandler( new wxPNGHandler );
258 #endif
259
260 wxFrame *frame = new MyFrame();
261 frame->Show( TRUE );
262
263 return TRUE;
264 }
265
266