]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/canvas/test/test.cpp
4 * Author: Robert Roebling
6 * Copyright: (C) 1998, Robert Roebling
9 // For compilers that support precompilation, includes "wx/wx.h".
10 #include "wx/wxprec.h"
29 #include "wx/canvas/canvas.h"
34 class MywxCanvasImage
: public wxCanvasImage
37 MywxCanvasImage( const wxImage
&image
, double x
, double y
, double w
, double h
);
39 void MywxCanvasImage::OnMouse(wxMouseEvent
&event
);
44 BEGIN_EVENT_TABLE(MywxCanvasImage
,wxCanvasImage
)
45 EVT_MOUSE_EVENTS( MywxCanvasImage::OnMouse
)
48 MywxCanvasImage::MywxCanvasImage( const wxImage
&image
, double x
, double y
, double w
, double h
)
49 :wxCanvasImage( image
, x
, y
, w
, h
)
53 void MywxCanvasImage::OnMouse(wxMouseEvent
&event
)
66 else if (event
.LeftUp())
70 else if (IsCapturedMouse())
72 Move(m_area
.x
+x
-dx
,m_area
.y
+y
-dy
);
77 class MywxCanvasObjectGroupRef
: public wxCanvasObjectGroupRef
80 MywxCanvasObjectGroupRef(double x
, double y
, wxCanvasObjectGroup
* group
);
82 void OnMouse(wxMouseEvent
&event
);
87 BEGIN_EVENT_TABLE(MywxCanvasObjectGroupRef
,wxCanvasObjectGroupRef
)
88 EVT_MOUSE_EVENTS( MywxCanvasObjectGroupRef::OnMouse
)
91 MywxCanvasObjectGroupRef::MywxCanvasObjectGroupRef(double x
, double y
,wxCanvasObjectGroup
* group
)
92 :wxCanvasObjectGroupRef(x
,y
,group
)
96 void MywxCanvasObjectGroupRef::OnMouse(wxMouseEvent
&event
)
101 //new position of object
102 int x
= m_owner
->GetDeviceX( event
.GetX());
103 int y
= m_owner
->GetDeviceY( event
.GetY());
105 if (event
.LeftDown())
111 else if (event
.LeftUp())
115 else if (IsCapturedMouse())
117 Move(m_x
+x
-dx
,m_y
+y
-dy
);
118 m_owner
->UpdateNow();
127 class MyFrame
: public wxFrame
133 void OnAbout( wxCommandEvent
&event
);
134 void OnNewFrame( wxCommandEvent
&event
);
135 void OnQuit( wxCommandEvent
&event
);
136 void OnTimer( wxTimerEvent
&event
);
139 wxCanvasObject
*m_sm1
;
140 wxCanvasObject
*m_sm2
;
141 wxCanvasObject
*m_sm3
;
142 wxCanvasObject
*m_sm4
;
144 MywxCanvasObjectGroupRef
*m_ref
;
145 MywxCanvasObjectGroupRef
*m_ref2
;
151 DECLARE_DYNAMIC_CLASS(MyFrame
)
152 DECLARE_EVENT_TABLE()
157 class MyApp
: public wxApp
160 virtual bool OnInit();
162 const wxString
& GetFontPath() const { return m_fontpath
; }
174 const int ID_QUIT
= 108;
175 const int ID_ABOUT
= 109;
177 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
179 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
180 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
181 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
182 EVT_TIMER (-1, MyFrame::OnTimer
)
186 : wxFrame( (wxFrame
*)NULL
, -1, "wxCanvas sample",
187 wxPoint(20,20), wxSize(470,460) )
189 wxMenu
*file_menu
= new wxMenu();
190 file_menu
->Append( ID_ABOUT
, "&About...");
191 file_menu
->AppendSeparator();
192 file_menu
->Append( ID_QUIT
, "E&xit");
194 wxMenuBar
*menu_bar
= new wxMenuBar();
195 menu_bar
->Append(file_menu
, "&File");
197 SetMenuBar( menu_bar
);
200 int widths
[] = { -1, 100 };
201 SetStatusWidths( 2, widths
);
203 m_canvas
= new wxCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
205 m_canvas
->SetArea( 1000,1000 );
206 m_canvas
->SetColour( 255, 255, 255 );
209 wxBitmap
bitmap( smile_xpm
);
210 wxImage
image( bitmap
);
212 m_sm1
= new wxCanvasImage( image
, 0,70,32,32 );
213 m_canvas
->Append( m_sm1
);
216 for (i
= 10; i
< 300; i
+=10)
218 wxCanvasRect
*r
= new wxCanvasRect( i
,50,3,140 );
219 r
->SetBrush( *wxRED_BRUSH
);
220 m_canvas
->Append( r
);
223 m_sm2
= new wxCanvasImage( image
, 0,140,24,24 );
224 m_canvas
->Append( m_sm2
);
226 for (i
= 15; i
< 300; i
+=10)
227 m_canvas
->Append( new wxCanvasRect( i
,50,3,140 ) );
229 wxButton
*button
= new wxButton( m_canvas
, -1, "Hello", wxPoint(80,50) );
230 m_canvas
->Append( new wxCanvasControl( button
) );
232 m_canvas
->Append( new wxCanvasText( "Hello", 180, 50,
233 wxGetApp().GetFontPath() + "/times.ttf", 20 ) );
235 m_sm3
= new wxCanvasImage( image
, 0,210,32,32 );
236 m_canvas
->Append( m_sm3
);
238 for (i
= 10; i
< 300; i
+=10)
239 m_canvas
->Append( new wxCanvasLine( 10,-15,i
,300 ) );
241 m_sm4
= new MywxCanvasImage( image
, 0,270,64,32 );
242 m_canvas
->Append( m_sm4
);
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 ) );
249 //make a group of wxCanvasObjects
250 wxCanvasObjectGroup* group1 = new wxCanvasObjectGroup();
251 group1->Prepend( new wxCanvasLine( 10,-35,50,190 ) );
252 group1->Prepend( new wxCanvasImage( image, 4,38,32,32 ) );
253 group1->Prepend( new wxCanvasRect(20,-20,50,170,0,20,240 ) );
255 //make another group of wxCanvasObjects
256 wxCanvasObjectGroup* group2 = new wxCanvasObjectGroup();
257 group2->Prepend( new wxCanvasImage( image, 60,38,52,32 ) );
258 group2->Prepend( new wxCanvasRect(10,20,104,52,10,40,10 ) );
260 //this a reference to group2 put into group1
261 wxCanvasObjectGroupRef* m_subref = new wxCanvasObjectGroupRef(60,50, group2);
262 group1->Prepend( m_subref );
264 //now make two refrences to group1 into root group of the canvas
265 m_ref = new MywxCanvasObjectGroupRef(40,200, group1);
266 m_canvas->Prepend( m_ref );
268 m_ref2 = new MywxCanvasObjectGroupRef(80,350, group1);
269 m_canvas->Prepend( m_ref2 );
272 m_log
= new wxTextCtrl( this, -1, "", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE
);
273 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
276 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
278 topsizer
->Add( m_canvas
, 1, wxEXPAND
);
279 topsizer
->Add( m_log
, 0, wxEXPAND
);
281 SetAutoLayout( TRUE
);
282 SetSizer( topsizer
);
284 m_timer
= new wxTimer( this );
285 m_timer
->Start( 100, FALSE
);
293 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
298 void MyFrame::OnTimer( wxTimerEvent
&WXUNUSED(event
) )
300 m_sm1
->Move( m_sm1
->GetX()+1, m_sm1
->GetY() );
301 m_sm2
->Move( m_sm2
->GetX()+1, m_sm2
->GetY() );
302 m_sm3
->Move( m_sm3
->GetX()+1, m_sm3
->GetY() );
303 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() );
312 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
314 (void)wxMessageBox( "wxCanvas demo\n"
315 "Robert Roebling (c) 1998,2000",
316 "About wxCanvas Demo", wxICON_INFORMATION
| wxOK
);
319 //-----------------------------------------------------------------------------
321 //-----------------------------------------------------------------------------
325 m_fontpath
= getenv("TRUETYPE");
328 wxLogError("Please set env var TRUETYPE to the path where times.ttf lives.");
335 wxImage::AddHandler( new wxPNGHandler
);
338 wxFrame
*frame
= new MyFrame();