]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/canvas/test/test.cpp
Trying to make wxCanvas less simple.
[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 { 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->SetArea( 400, 600 );
159 m_canvas->SetColour( 255, 255, 255 );
160
161 wxBitmap bitmap( smile_xpm );
162 wxImage image( bitmap );
163
164 m_sm1 = new wxCanvasImage( image, 0,70,16,16 );
165 m_canvas->Append( m_sm1 );
166
167 int i;
168 for (i = 10; i < 300; i+=10)
169 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
170
171 m_sm2 = new wxCanvasImage( image, 0,140,24,24 );
172 m_canvas->Append( m_sm2 );
173
174 for (i = 15; i < 300; i+=10)
175 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
176
177 wxButton *button = new wxButton( m_canvas, -1, "Hello", wxPoint(80,50) );
178 m_canvas->Append( new wxCanvasControl( button ) );
179
180 m_canvas->Append( new wxCanvasText( "Hello", 180, 50,
181 wxGetApp().GetFontPath() + "/times.ttf", 20 ) );
182
183 m_sm3 = new wxCanvasImage( image, 0,210,32,32 );
184 m_canvas->Append( m_sm3 );
185
186 for (i = 10; i < 300; i+=10)
187 m_canvas->Append( new wxCanvasLine( 10,-15,i,300, 0,255,0 ) );
188
189 // m_canvas->Append( new wxCanvasLine( 10,-1500e6,50,300000e6, 0,255,0 ) );
190 // m_canvas->Append( new wxCanvasLine( 10,-150000,50,300000, 0,255,0 ) );
191
192 m_sm4 = new MywxCanvasImage( image, 0,270,64,32 );
193 m_canvas->Append( m_sm4 );
194
195 m_log = new wxTextCtrl( this, -1, "", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
196 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
197 delete old_log;
198
199 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
200
201 topsizer->Add( m_canvas, 1, wxEXPAND );
202 topsizer->Add( m_log, 0, wxEXPAND );
203
204 SetAutoLayout( TRUE );
205 SetSizer( topsizer );
206
207 m_timer = new wxTimer( this );
208 m_timer->Start( 100, FALSE );
209 }
210
211 MyFrame::~MyFrame()
212 {
213 delete m_timer;
214 }
215
216 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
217 {
218 Close( TRUE );
219 }
220
221 void MyFrame::OnTimer( wxTimerEvent &WXUNUSED(event) )
222 {
223 m_sm1->Move( m_sm1->GetX()+1, m_sm1->GetY() );
224 m_sm2->Move( m_sm2->GetX()+1, m_sm2->GetY() );
225 m_sm3->Move( m_sm3->GetX()+1, m_sm3->GetY() );
226 m_sm4->Move( m_sm4->GetX()+1, m_sm4->GetY() );
227
228 wxWakeUpIdle();
229 }
230
231 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
232 {
233 (void)wxMessageBox( "wxCanvas demo\n"
234 "Robert Roebling (c) 1998,2000",
235 "About wxCanvas Demo", wxICON_INFORMATION | wxOK );
236 }
237
238 //-----------------------------------------------------------------------------
239 // MyApp
240 //-----------------------------------------------------------------------------
241
242 bool MyApp::OnInit()
243 {
244 m_fontpath = getenv("TRUETYPE");
245 if ( !m_fontpath )
246 {
247 wxLogError("Please set env var TRUETYPE to the path where times.ttf lives.");
248
249 return FALSE;
250
251 }
252
253 #if wxUSE_LIBPNG
254 wxImage::AddHandler( new wxPNGHandler );
255 #endif
256
257 wxFrame *frame = new MyFrame();
258 frame->Show( TRUE );
259
260 return TRUE;
261 }
262
263