]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/canvas/test/test.cpp
93392729724bee3056f5af18ab8ca8785512bfe3
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
)
55 static bool first
=FALSE
;
69 Move(m_area
.x
+x
-dx
,m_area
.y
+y
-dy
);
72 else if (IsCapturedMouse())
81 class MywxCanvasObjectGroupRef
: public wxCanvasObjectGroupRef
84 MywxCanvasObjectGroupRef(double x
, double y
, wxCanvasObjectGroup
* group
);
86 void OnMouse(wxMouseEvent
&event
);
91 BEGIN_EVENT_TABLE(MywxCanvasObjectGroupRef
,wxCanvasObjectGroupRef
)
92 EVT_MOUSE_EVENTS( MywxCanvasObjectGroupRef::OnMouse
)
95 MywxCanvasObjectGroupRef::MywxCanvasObjectGroupRef(double x
, double y
,wxCanvasObjectGroup
* group
)
96 :wxCanvasObjectGroupRef(x
,y
,group
)
100 void MywxCanvasObjectGroupRef::OnMouse(wxMouseEvent
&event
)
102 static bool first
=FALSE
;
106 //new position of object
107 int x
= m_owner
->GetDeviceX( event
.GetX());
108 int y
= m_owner
->GetDeviceY( event
.GetY());
110 if (event
.m_leftDown
)
118 Move(m_x
+x
-dx
,m_y
+y
-dy
);
121 else if (IsCapturedMouse())
135 class MyFrame
: public wxFrame
141 void OnAbout( wxCommandEvent
&event
);
142 void OnNewFrame( wxCommandEvent
&event
);
143 void OnQuit( wxCommandEvent
&event
);
144 void OnTimer( wxTimerEvent
&event
);
147 wxCanvasObject
*m_sm1
;
148 wxCanvasObject
*m_sm2
;
149 wxCanvasObject
*m_sm3
;
150 wxCanvasObject
*m_sm4
;
152 MywxCanvasObjectGroupRef
*m_ref
;
153 MywxCanvasObjectGroupRef
*m_ref2
;
159 DECLARE_DYNAMIC_CLASS(MyFrame
)
160 DECLARE_EVENT_TABLE()
165 class MyApp
: public wxApp
168 virtual bool OnInit();
170 const wxString
& GetFontPath() const { return m_fontpath
; }
182 const int ID_QUIT
= 108;
183 const int ID_ABOUT
= 109;
185 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
187 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
188 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
189 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
190 EVT_TIMER (-1, MyFrame::OnTimer
)
194 : wxFrame( (wxFrame
*)NULL
, -1, "wxCanvas sample",
195 wxPoint(20,20), wxSize(470,860) )
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");
202 wxMenuBar
*menu_bar
= new wxMenuBar();
203 menu_bar
->Append(file_menu
, "&File");
205 SetMenuBar( menu_bar
);
208 int widths
[] = { -1, 100 };
209 SetStatusWidths( 2, widths
);
211 m_canvas
= new wxCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
213 m_canvas
->SetArea( 1400, 600 );
214 m_canvas
->SetColour( 255, 255, 255 );
217 wxBitmap
bitmap( smile_xpm
);
218 wxImage
image( bitmap
);
220 m_sm1
= new wxCanvasImage( image
, 0,70,16,16 );
221 m_canvas
->Append( m_sm1
);
224 for (i
= 10; i
< 300; i
+=10)
225 m_canvas
->Append( new wxCanvasRect( i
,50,3,140, 255,0,0 ) );
227 m_sm2
= new wxCanvasImage( image
, 0,140,24,24 );
228 m_canvas
->Append( m_sm2
);
230 for (i
= 15; i
< 300; i
+=10)
231 m_canvas
->Append( new wxCanvasRect( i
,50,3,140, 255,0,0 ) );
233 wxButton
*button
= new wxButton( m_canvas
, -1, "Hello", wxPoint(80,50) );
234 m_canvas
->Append( new wxCanvasControl( button
) );
236 m_canvas
->Append( new wxCanvasText( "Hello", 180, 50,
237 wxGetApp().GetFontPath() + "/times.ttf", 20 ) );
239 m_sm3
= new wxCanvasImage( image
, 0,210,32,32 );
240 m_canvas
->Append( m_sm3
);
242 for (i
= 10; i
< 300; i
+=10)
243 m_canvas
->Append( new wxCanvasLine( 10,-15,i
,300, 0,255,0 ) );
245 m_sm4
= new MywxCanvasImage( image
, 0,270,64,32 );
246 m_canvas
->Append( m_sm4
);
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 ) );
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 ) );
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 ) );
265 //this a reference to group2 put into group1
266 wxCanvasObjectGroupRef
* m_subref
= new wxCanvasObjectGroupRef(60,50, group2
);
267 group1
->Prepend( m_subref
);
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
);
273 m_ref2
= new MywxCanvasObjectGroupRef(80,350, group1
);
274 m_canvas
->Prepend( m_ref2
);
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
) );
281 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
283 topsizer
->Add( m_canvas
, 1, wxEXPAND
);
284 topsizer
->Add( m_log
, 0, wxEXPAND
);
286 SetAutoLayout( TRUE
);
287 SetSizer( topsizer
);
289 m_timer
= new wxTimer( this );
290 m_timer
->Start( 100, FALSE
);
298 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
303 void MyFrame::OnTimer( wxTimerEvent
&WXUNUSED(event
) )
305 m_sm1
->Move( m_sm1
->GetX()+1, m_sm1
->GetY() );
306 m_sm2
->Move( m_sm2
->GetX()+1, m_sm2
->GetY() );
307 m_sm3
->Move( m_sm3
->GetX()+1, m_sm3
->GetY() );
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() );
315 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
317 (void)wxMessageBox( "wxCanvas demo\n"
318 "Robert Roebling (c) 1998,2000",
319 "About wxCanvas Demo", wxICON_INFORMATION
| wxOK
);
322 //-----------------------------------------------------------------------------
324 //-----------------------------------------------------------------------------
328 m_fontpath
= getenv("TRUETYPE");
331 wxLogError("Please set env var TRUETYPE to the path where times.ttf lives.");
338 wxImage::AddHandler( new wxPNGHandler
);
341 wxFrame
*frame
= new MyFrame();