]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/gizmos/led/led.cpp
Rebake all the VC++ project files and makefiles
[wxWidgets.git] / contrib / samples / gizmos / led / led.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: led.cpp
3 // Purpose: LED sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
29 #ifndef WX_PRECOMP
30 #include "wx/wx.h"
31 #endif
32
33 #include "wx/gizmos/ledctrl.h"
34 #include "wx/sizer.h"
35 #include "wx/panel.h"
36 #include "wx/numdlg.h"
37
38 // ----------------------------------------------------------------------------
39 // private classes
40 // ----------------------------------------------------------------------------
41
42 class MyPanel : public wxPanel
43 {
44 public:
45 MyPanel(wxFrame *frame);
46
47 void OnIncrement();
48 void OnDecrement();
49 void OnSmallIncrement();
50 void OnSmallDecrement();
51 void OnSetValue();
52 void OnAlignLeft();
53 void OnAlignCenter();
54 void OnAlignRight();
55 void OnDrawFaded();
56
57 private:
58 wxLEDNumberCtrl *m_led;
59 wxBoxSizer *m_sizer;
60 };
61
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp : public wxApp
64 {
65 public:
66 // override base class virtuals
67 // ----------------------------
68
69 // this one is called on application startup and is a good place for the app
70 // initialization (doing it here and not in the ctor allows to have an error
71 // return: if OnInit() returns false, the application terminates)
72 virtual bool OnInit();
73 };
74
75 // Define a new frame type: this is going to be our main frame
76 class MyFrame : public wxFrame
77 {
78 public:
79 // ctor(s)
80 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
81 long style = wxDEFAULT_FRAME_STYLE);
82
83 // event handlers (these functions should _not_ be virtual)
84 void OnQuit(wxCommandEvent& event);
85 void OnIncrement(wxCommandEvent& event);
86 void OnDecrement(wxCommandEvent& event);
87 void OnSmallIncrement(wxCommandEvent& event);
88 void OnSmallDecrement(wxCommandEvent& event);
89 void OnSetValue(wxCommandEvent& event);
90 void OnAlignLeft(wxCommandEvent& event);
91 void OnAlignCenter(wxCommandEvent& event);
92 void OnAlignRight(wxCommandEvent& event);
93 void OnDrawFaded(wxCommandEvent& event);
94 void OnAbout(wxCommandEvent& event);
95
96 private:
97 MyPanel *m_panel;
98
99 // any class wishing to process wxWidgets events must use this macro
100 DECLARE_EVENT_TABLE()
101 };
102
103 // ----------------------------------------------------------------------------
104 // constants
105 // ----------------------------------------------------------------------------
106
107 // IDs for the controls and the menu commands
108 enum
109 {
110 // menu items
111 LED_Quit = wxID_EXIT,
112
113 LED_Edit_Increment = wxID_HIGHEST + 1,
114 LED_Edit_Decrement,
115 LED_Edit_Small_Increment,
116 LED_Edit_Small_Decrement,
117 LED_Edit_SetValue,
118 LED_Edit_AlignLeft,
119 LED_Edit_AlignCenter,
120 LED_Edit_AlignRight,
121 LED_Edit_DrawFaded,
122
123 // it is important for the id corresponding to the "About" command to have
124 // this standard value as otherwise it won't be handled properly under Mac
125 // (where it is special and put into the "Apple" menu)
126 LED_About = wxID_ABOUT
127 };
128
129 // ----------------------------------------------------------------------------
130 // event tables and other macros for wxWidgets
131 // ----------------------------------------------------------------------------
132
133 // the event tables connect the wxWidgets events with the functions (event
134 // handlers) which process them. It can be also done at run-time, but for the
135 // simple menu events like this the static method is much simpler.
136 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
137 EVT_MENU(LED_Quit, MyFrame::OnQuit)
138 EVT_MENU(LED_Edit_Increment, MyFrame::OnIncrement)
139 EVT_MENU(LED_Edit_Decrement, MyFrame::OnDecrement)
140 EVT_MENU(LED_Edit_Small_Increment, MyFrame::OnSmallIncrement)
141 EVT_MENU(LED_Edit_Small_Decrement, MyFrame::OnSmallDecrement)
142 EVT_MENU(LED_Edit_SetValue, MyFrame::OnSetValue)
143 EVT_MENU(LED_Edit_AlignLeft, MyFrame::OnAlignLeft)
144 EVT_MENU(LED_Edit_AlignCenter, MyFrame::OnAlignCenter)
145 EVT_MENU(LED_Edit_AlignRight, MyFrame::OnAlignRight)
146 EVT_MENU(LED_Edit_DrawFaded, MyFrame::OnDrawFaded)
147 EVT_MENU(LED_About, MyFrame::OnAbout)
148 END_EVENT_TABLE()
149
150 // Create a new application object: this macro will allow wxWidgets to create
151 // the application object during program execution (it's better than using a
152 // static object for many reasons) and also declares the accessor function
153 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
154 // not wxApp)
155 IMPLEMENT_APP(MyApp)
156
157 // ============================================================================
158 // implementation
159 // ============================================================================
160
161 // ----------------------------------------------------------------------------
162 // the application class
163 // ----------------------------------------------------------------------------
164
165 // 'Main program' equivalent: the program execution "starts" here
166 bool MyApp::OnInit()
167 {
168 // create the main application window
169 MyFrame *frame = new MyFrame(_T("LED App"),
170 wxDefaultPosition, wxSize(450, 120));
171
172 // and show it (the frames, unlike simple controls, are not shown when
173 // created initially)
174 frame->Show(true);
175
176 // success: wxApp::OnRun() will be called which will enter the main message
177 // loop and the application will run. If we returned false here, the
178 // application would exit immediately.
179 return true;
180 }
181
182 // ----------------------------------------------------------------------------
183 // main frame
184 // ----------------------------------------------------------------------------
185
186 // frame constructor
187 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
188 : wxFrame(NULL, wxID_ANY, title, pos, size, style)
189 {
190 #if wxUSE_MENUS
191 // create a menu bar
192 wxMenu *menuFile = new wxMenu;
193
194 // the "About" item should be in the help menu
195 wxMenu *helpMenu = new wxMenu;
196 helpMenu->Append(LED_About, _T("&About...\tF1"), _T("Show about dialog"));
197
198 menuFile->Append(LED_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
199
200 wxMenu *editMenu = new wxMenu;
201 editMenu->Append(LED_Edit_Increment, _T("&Increment LED (1)\tCtrl-I"));
202 editMenu->Append(LED_Edit_Small_Increment, _T("&Increment LED (0.01)\tAlt-I"));
203 editMenu->Append(LED_Edit_Decrement, _T("&Decrement LED (1)\tCtrl-D"));
204 editMenu->Append(LED_Edit_Small_Decrement, _T("&Decrement LED (0.01)\tAlt-D"));
205 editMenu->Append(LED_Edit_SetValue, _T("&Set LED Value...\tCtrl-S"));
206 editMenu->AppendSeparator();
207 editMenu->AppendRadioItem(LED_Edit_AlignLeft, _T("Align &Left"));
208 editMenu->AppendRadioItem(LED_Edit_AlignCenter, _T("Align &Center"));
209 editMenu->AppendRadioItem(LED_Edit_AlignRight, _T("Align &Right"));
210 editMenu->AppendSeparator();
211 editMenu->AppendCheckItem(LED_Edit_DrawFaded, _T("Draw &Faded\tCtrl-F"));
212
213 editMenu->Check(LED_Edit_DrawFaded, true);
214
215 // now append the freshly created menu to the menu bar...
216 wxMenuBar *menuBar = new wxMenuBar();
217 menuBar->Append(menuFile, _T("&File"));
218 menuBar->Append(editMenu, _T("&Edit"));
219 menuBar->Append(helpMenu, _T("&Help"));
220
221 // ... and attach this menu bar to the frame
222 SetMenuBar(menuBar);
223 #endif // wxUSE_MENUS
224
225 m_panel = new MyPanel(this);
226 }
227
228
229 // event handlers
230
231 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
232 {
233 // true is to force the frame to close
234 Close(true);
235 }
236
237 void MyFrame::OnIncrement(wxCommandEvent& WXUNUSED(event))
238 {
239 m_panel->OnIncrement();
240 }
241
242 void MyFrame::OnDecrement(wxCommandEvent& WXUNUSED(event))
243 {
244 m_panel->OnDecrement();
245 }
246
247 void MyFrame::OnSmallIncrement(wxCommandEvent& WXUNUSED(event))
248 {
249 m_panel->OnSmallIncrement();
250 }
251
252 void MyFrame::OnSmallDecrement(wxCommandEvent& WXUNUSED(event))
253 {
254 m_panel->OnSmallDecrement();
255 }
256
257 void MyFrame::OnSetValue(wxCommandEvent& WXUNUSED(event))
258 {
259 m_panel->OnSetValue();
260 }
261
262 void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
263 {
264 m_panel->OnAlignLeft();
265 }
266
267 void MyFrame::OnAlignCenter(wxCommandEvent& WXUNUSED(event))
268 {
269 m_panel->OnAlignCenter();
270 }
271
272 void MyFrame::OnAlignRight(wxCommandEvent& WXUNUSED(event))
273 {
274 m_panel->OnAlignRight();
275 }
276
277 void MyFrame::OnDrawFaded(wxCommandEvent& WXUNUSED(event))
278 {
279 m_panel->OnDrawFaded();
280 }
281
282 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
283 {
284 wxString msg;
285 msg.Printf( _T("This is the About dialog of the LED sample.\n")
286 _T("Welcome to %s"), wxVERSION_STRING);
287
288 wxMessageBox(msg, _T("About LED"), wxOK | wxICON_INFORMATION, this);
289 }
290
291 // --------------------------------------------------------------------------
292 // MyPanel
293 // --------------------------------------------------------------------------
294
295 MyPanel::MyPanel(wxFrame *frame)
296 : wxPanel(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN)
297 {
298 m_led = new wxLEDNumberCtrl(this, wxID_ANY,
299 wxDefaultPosition, wxDefaultSize,
300 wxLED_ALIGN_LEFT|wxLED_DRAW_FADED|wxFULL_REPAINT_ON_RESIZE);
301
302 m_led->SetValue(_T("01.23 7-8-9"));
303
304 m_sizer = new wxBoxSizer(wxVERTICAL);
305 m_sizer->Add(m_led, 1, wxEXPAND|wxALL, 10);
306 m_sizer->Fit(this);
307
308 SetSizer(m_sizer);
309 SetAutoLayout(true);
310 }
311
312 void MyPanel::OnIncrement()
313 {
314 wxString strValue = m_led->GetValue();
315
316 double dValue;
317 strValue.ToDouble(&dValue);
318 dValue += 1.0;
319 m_led->SetValue(wxString::Format(_T("%.2f"), dValue));
320 }
321
322 void MyPanel::OnDecrement()
323 {
324 wxString strValue = m_led->GetValue();
325
326 double dValue;
327 strValue.ToDouble(&dValue);
328 dValue -= 1.0;
329 m_led->SetValue(wxString::Format(_T("%.2f"), dValue));
330 }
331
332 void MyPanel::OnSmallIncrement()
333 {
334 wxString strValue = m_led->GetValue();
335
336 double dValue;
337 strValue.ToDouble(&dValue);
338 dValue += 0.01;
339 m_led->SetValue(wxString::Format(_T("%.2f"), dValue));
340 }
341
342 void MyPanel::OnSmallDecrement()
343 {
344 wxString strValue = m_led->GetValue();
345
346 double dValue;
347 strValue.ToDouble(&dValue);
348 dValue -= 0.01;
349 m_led->SetValue(wxString::Format(_T("%.2f"), dValue));
350 }
351
352 void MyPanel::OnSetValue()
353 {
354 wxString strValue = m_led->GetValue();
355
356 strValue = ::wxGetTextFromUser(_T("Please enter a number for LED display"), _T("Please enter a number"), strValue, this);
357
358 if (strValue != _T(""))
359 m_led->SetValue(strValue);
360 }
361
362 void MyPanel::OnAlignLeft()
363 {
364 m_led->SetAlignment(wxLED_ALIGN_LEFT);
365 }
366
367 void MyPanel::OnAlignCenter()
368 {
369 m_led->SetAlignment(wxLED_ALIGN_CENTER);
370 }
371
372 void MyPanel::OnAlignRight()
373 {
374 m_led->SetAlignment(wxLED_ALIGN_RIGHT);
375 }
376
377 void MyPanel::OnDrawFaded()
378 {
379 m_led->SetDrawFaded(!(m_led->GetDrawFaded()));
380 }