]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/canvas/test/test.cpp
Added #include in helpbase.cpp
[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;
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
6a2c1874
RR
79class MyFrame;
80class MyApp;
81
82// MyFrame
83
84class MyFrame: public wxFrame
85{
86public:
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
239c1f50
RR
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;
6a2c1874
RR
102
103private:
104 DECLARE_DYNAMIC_CLASS(MyFrame)
105 DECLARE_EVENT_TABLE()
106};
107
108// MyApp
109
110class MyApp: public wxApp
111{
112public:
113 virtual bool OnInit();
cba349dc
VZ
114
115 const wxString& GetFontPath() const { return m_fontpath; }
116
117private:
118 wxString m_fontpath;
6a2c1874
RR
119};
120
121// main program
122
123IMPLEMENT_APP(MyApp)
124
125// MyFrame
126
127const int ID_QUIT = 108;
128const int ID_ABOUT = 109;
129
130IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
131
132BEGIN_EVENT_TABLE(MyFrame,wxFrame)
133 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
134 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
135 EVT_TIMER (-1, MyFrame::OnTimer)
136END_EVENT_TABLE()
137
138MyFrame::MyFrame()
139 : wxFrame( (wxFrame *)NULL, -1, "wxCanvas sample",
239c1f50 140 wxPoint(20,20), wxSize(470,460) )
6a2c1874 141{
239c1f50
RR
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) );
9f3b9298 157
239c1f50
RR
158 m_canvas->Freeze();
159
160 m_canvas->SetArea( 400, 600 );
161 m_canvas->SetColour( 255, 255, 255 );
6a2c1874 162
239c1f50
RR
163 wxBitmap bitmap( smile_xpm );
164 wxImage image( bitmap );
6a2c1874 165
4dbd4ee6 166 m_sm1 = new wxCanvasImage( image, 0,70,16,16 );
239c1f50 167 m_canvas->Append( m_sm1 );
6a2c1874 168
239c1f50
RR
169 int i;
170 for (i = 10; i < 300; i+=10)
171 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
6a2c1874 172
4dbd4ee6 173 m_sm2 = new wxCanvasImage( image, 0,140,24,24 );
239c1f50 174 m_canvas->Append( m_sm2 );
6a2c1874 175
239c1f50
RR
176 for (i = 15; i < 300; i+=10)
177 m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) );
cba349dc 178
239c1f50
RR
179 wxButton *button = new wxButton( m_canvas, -1, "Hello", wxPoint(80,50) );
180 m_canvas->Append( new wxCanvasControl( button ) );
cba349dc 181
239c1f50
RR
182 m_canvas->Append( new wxCanvasText( "Hello", 180, 50,
183 wxGetApp().GetFontPath() + "/times.ttf", 20 ) );
cba349dc 184
4dbd4ee6 185 m_sm3 = new wxCanvasImage( image, 0,210,32,32 );
239c1f50 186 m_canvas->Append( m_sm3 );
cba349dc 187
239c1f50 188 for (i = 10; i < 300; i+=10)
9f3b9298
KH
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 );
239c1f50 195 m_canvas->Append( m_sm4 );
cba349dc 196
239c1f50 197 m_canvas->Thaw();
cba349dc 198
239c1f50
RR
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;
9f3b9298 202
239c1f50 203 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
9f3b9298 204
239c1f50
RR
205 topsizer->Add( m_canvas, 1, wxEXPAND );
206 topsizer->Add( m_log, 0, wxEXPAND );
d1f9b206 207
239c1f50
RR
208 SetAutoLayout( TRUE );
209 SetSizer( topsizer );
cba349dc 210
239c1f50
RR
211 m_timer = new wxTimer( this );
212 m_timer->Start( 100, FALSE );
6a2c1874
RR
213}
214
215MyFrame::~MyFrame()
216{
217 delete m_timer;
218}
219
220void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
221{
222 Close( TRUE );
223}
224
225void MyFrame::OnTimer( wxTimerEvent &WXUNUSED(event) )
226{
21544859
RR
227 m_sm1->Move( m_sm1->GetX()+1, m_sm1->GetY() );
228 m_sm2->Move( m_sm2->GetX()+1, m_sm2->GetY() );
239c1f50
RR
229 m_sm3->Move( m_sm3->GetX()+1, m_sm3->GetY() );
230 m_sm4->Move( m_sm4->GetX()+1, m_sm4->GetY() );
6a2c1874
RR
231 wxWakeUpIdle();
232}
233
234void 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
245bool MyApp::OnInit()
246{
cba349dc
VZ
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
6a2c1874
RR
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