]>
Commit | Line | Data |
---|---|---|
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> | |
23 | ||
24 | #include "smile.xpm" | |
25 | ||
26 | #include "wx/canvas/canvas.h" | |
27 | ||
28 | // derived classes | |
29 | ||
30 | class MyFrame; | |
31 | class MyApp; | |
32 | ||
33 | // MyFrame | |
34 | ||
35 | class MyFrame: public wxFrame | |
36 | { | |
37 | public: | |
38 | MyFrame(); | |
39 | ~MyFrame(); | |
40 | ||
41 | void OnAbout( wxCommandEvent &event ); | |
42 | void OnNewFrame( wxCommandEvent &event ); | |
43 | void OnQuit( wxCommandEvent &event ); | |
44 | void OnTimer( wxTimerEvent &event ); | |
45 | ||
46 | wxCanvas *m_canvas; | |
21544859 RR |
47 | wxCanvasObject *m_sm1; |
48 | wxCanvasObject *m_sm2; | |
6a2c1874 RR |
49 | wxTimer *m_timer; |
50 | ||
51 | private: | |
52 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
53 | DECLARE_EVENT_TABLE() | |
54 | }; | |
55 | ||
56 | // MyApp | |
57 | ||
58 | class MyApp: public wxApp | |
59 | { | |
60 | public: | |
61 | virtual bool OnInit(); | |
cba349dc VZ |
62 | |
63 | const wxString& GetFontPath() const { return m_fontpath; } | |
64 | ||
65 | private: | |
66 | wxString m_fontpath; | |
6a2c1874 RR |
67 | }; |
68 | ||
69 | // main program | |
70 | ||
71 | IMPLEMENT_APP(MyApp) | |
72 | ||
73 | // MyFrame | |
74 | ||
75 | const int ID_QUIT = 108; | |
76 | const int ID_ABOUT = 109; | |
77 | ||
78 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
79 | ||
80 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
81 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) | |
82 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
83 | EVT_TIMER (-1, MyFrame::OnTimer) | |
84 | END_EVENT_TABLE() | |
85 | ||
86 | MyFrame::MyFrame() | |
87 | : wxFrame( (wxFrame *)NULL, -1, "wxCanvas sample", | |
88 | wxPoint(20,20), wxSize(470,360) ) | |
89 | { | |
90 | wxMenu *file_menu = new wxMenu(); | |
91 | file_menu->Append( ID_ABOUT, "&About..."); | |
92 | file_menu->AppendSeparator(); | |
93 | file_menu->Append( ID_QUIT, "E&xit"); | |
94 | ||
95 | wxMenuBar *menu_bar = new wxMenuBar(); | |
96 | menu_bar->Append(file_menu, "&File"); | |
97 | ||
98 | SetMenuBar( menu_bar ); | |
99 | ||
100 | CreateStatusBar(2); | |
101 | int widths[] = { -1, 100 }; | |
102 | SetStatusWidths( 2, widths ); | |
103 | ||
104 | m_canvas = new wxCanvas( this, -1, wxPoint(0,0), wxSize(10,10) ); | |
105 | ||
106 | m_canvas->SetArea( 400, 600 ); | |
cb281cfc | 107 | m_canvas->SetColour( 255, 255, 255 ); |
cba349dc | 108 | |
6a2c1874 RR |
109 | wxBitmap bitmap( smile_xpm ); |
110 | wxImage image( bitmap ); | |
cba349dc | 111 | |
21544859 RR |
112 | m_sm1 = new wxCanvasImage( image, 0, 70 ); |
113 | m_canvas->Append( m_sm1 ); | |
cba349dc | 114 | |
21544859 RR |
115 | int i; |
116 | for (i = 10; i < 300; i+=10) | |
117 | m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) ); | |
cba349dc | 118 | |
21544859 RR |
119 | m_sm2 = new wxCanvasImage( image, 0, 140 ); |
120 | m_canvas->Append( m_sm2 ); | |
cba349dc | 121 | |
21544859 RR |
122 | for (i = 15; i < 300; i+=10) |
123 | m_canvas->Append( new wxCanvasRect( i,50,3,140, 255,0,0 ) ); | |
cba349dc | 124 | |
cb281cfc | 125 | wxButton *button = new wxButton( m_canvas, -1, "Hello", wxPoint(80,50) ); |
3b111dbe | 126 | m_canvas->Append( new wxCanvasControl( button ) ); |
d1f9b206 | 127 | |
cba349dc VZ |
128 | m_canvas->Append( new wxCanvasText( "Hello", 180, 50, |
129 | wxGetApp().GetFontPath() + "/times.ttf", 20 ) ); | |
130 | ||
6a2c1874 | 131 | m_timer = new wxTimer( this ); |
d1f9b206 | 132 | m_timer->Start( 100, FALSE ); |
6a2c1874 RR |
133 | } |
134 | ||
135 | MyFrame::~MyFrame() | |
136 | { | |
137 | delete m_timer; | |
138 | } | |
139 | ||
140 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) | |
141 | { | |
142 | Close( TRUE ); | |
143 | } | |
144 | ||
145 | void MyFrame::OnTimer( wxTimerEvent &WXUNUSED(event) ) | |
146 | { | |
21544859 RR |
147 | m_sm1->Move( m_sm1->GetX()+1, m_sm1->GetY() ); |
148 | m_sm2->Move( m_sm2->GetX()+1, m_sm2->GetY() ); | |
6a2c1874 RR |
149 | wxWakeUpIdle(); |
150 | } | |
151 | ||
152 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
153 | { | |
154 | (void)wxMessageBox( "wxCanvas demo\n" | |
155 | "Robert Roebling (c) 1998,2000", | |
156 | "About wxCanvas Demo", wxICON_INFORMATION | wxOK ); | |
157 | } | |
158 | ||
159 | //----------------------------------------------------------------------------- | |
160 | // MyApp | |
161 | //----------------------------------------------------------------------------- | |
162 | ||
163 | bool MyApp::OnInit() | |
164 | { | |
cba349dc VZ |
165 | m_fontpath = getenv("TRUETYPE"); |
166 | if ( !m_fontpath ) | |
167 | { | |
168 | wxLogError("Please set env var TRUETYPE to the path where times.ttf lives."); | |
169 | ||
170 | return FALSE; | |
171 | ||
172 | } | |
173 | ||
6a2c1874 RR |
174 | #if wxUSE_LIBPNG |
175 | wxImage::AddHandler( new wxPNGHandler ); | |
176 | #endif | |
177 | ||
178 | wxFrame *frame = new MyFrame(); | |
179 | frame->Show( TRUE ); | |
180 | ||
181 | return TRUE; | |
182 | } | |
183 | ||
184 |