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