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