]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/gizmos/led/led.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers)
33 #include "wx/gizmos/ledctrl.h"
36 #include "wx/numdlg.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class MyPanel
: public wxPanel
45 MyPanel(wxFrame
*frame
);
49 void OnSmallIncrement();
50 void OnSmallDecrement();
58 wxLEDNumberCtrl
*m_led
;
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp
: public wxApp
66 // override base class virtuals
67 // ----------------------------
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();
75 // Define a new frame type: this is going to be our main frame
76 class MyFrame
: public wxFrame
80 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
81 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
99 // any class wishing to process wxWidgets events must use this macro
100 DECLARE_EVENT_TABLE()
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // IDs for the controls and the menu commands
111 LED_Quit
= wxID_EXIT
,
113 LED_Edit_Increment
= wxID_HIGHEST
+ 1,
115 LED_Edit_Small_Increment
,
116 LED_Edit_Small_Decrement
,
119 LED_Edit_AlignCenter
,
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
129 // ----------------------------------------------------------------------------
130 // event tables and other macros for wxWidgets
131 // ----------------------------------------------------------------------------
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
)
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
157 // ============================================================================
159 // ============================================================================
161 // ----------------------------------------------------------------------------
162 // the application class
163 // ----------------------------------------------------------------------------
165 // 'Main program' equivalent: the program execution "starts" here
168 // create the main application window
169 MyFrame
*frame
= new MyFrame(_T("LED App"),
170 wxDefaultPosition
, wxSize(450, 120));
172 // and show it (the frames, unlike simple controls, are not shown when
173 // created initially)
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.
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
187 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
188 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
192 wxMenu
*menuFile
= new wxMenu
;
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"));
198 menuFile
->Append(LED_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
213 editMenu
->Check(LED_Edit_DrawFaded
, true);
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"));
221 // ... and attach this menu bar to the frame
223 #endif // wxUSE_MENUS
225 m_panel
= new MyPanel(this);
231 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
233 // true is to force the frame to close
237 void MyFrame::OnIncrement(wxCommandEvent
& WXUNUSED(event
))
239 m_panel
->OnIncrement();
242 void MyFrame::OnDecrement(wxCommandEvent
& WXUNUSED(event
))
244 m_panel
->OnDecrement();
247 void MyFrame::OnSmallIncrement(wxCommandEvent
& WXUNUSED(event
))
249 m_panel
->OnSmallIncrement();
252 void MyFrame::OnSmallDecrement(wxCommandEvent
& WXUNUSED(event
))
254 m_panel
->OnSmallDecrement();
257 void MyFrame::OnSetValue(wxCommandEvent
& WXUNUSED(event
))
259 m_panel
->OnSetValue();
262 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
264 m_panel
->OnAlignLeft();
267 void MyFrame::OnAlignCenter(wxCommandEvent
& WXUNUSED(event
))
269 m_panel
->OnAlignCenter();
272 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
274 m_panel
->OnAlignRight();
277 void MyFrame::OnDrawFaded(wxCommandEvent
& WXUNUSED(event
))
279 m_panel
->OnDrawFaded();
282 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
285 msg
.Printf( _T("This is the About dialog of the LED sample.\n")
286 _T("Welcome to %s"), wxVERSION_STRING
);
288 wxMessageBox(msg
, _T("About LED"), wxOK
| wxICON_INFORMATION
, this);
291 // --------------------------------------------------------------------------
293 // --------------------------------------------------------------------------
295 MyPanel::MyPanel(wxFrame
*frame
)
296 : wxPanel(frame
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
)
298 m_led
= new wxLEDNumberCtrl(this, wxID_ANY
,
299 wxDefaultPosition
, wxDefaultSize
,
300 wxLED_ALIGN_LEFT
|wxLED_DRAW_FADED
|wxFULL_REPAINT_ON_RESIZE
);
302 m_led
->SetValue(_T("01.23 7-8-9"));
304 m_sizer
= new wxBoxSizer(wxVERTICAL
);
305 m_sizer
->Add(m_led
, 1, wxEXPAND
|wxALL
, 10);
312 void MyPanel::OnIncrement()
314 wxString strValue
= m_led
->GetValue();
317 strValue
.ToDouble(&dValue
);
319 m_led
->SetValue(wxString::Format(_T("%.2f"), dValue
));
322 void MyPanel::OnDecrement()
324 wxString strValue
= m_led
->GetValue();
327 strValue
.ToDouble(&dValue
);
329 m_led
->SetValue(wxString::Format(_T("%.2f"), dValue
));
332 void MyPanel::OnSmallIncrement()
334 wxString strValue
= m_led
->GetValue();
337 strValue
.ToDouble(&dValue
);
339 m_led
->SetValue(wxString::Format(_T("%.2f"), dValue
));
342 void MyPanel::OnSmallDecrement()
344 wxString strValue
= m_led
->GetValue();
347 strValue
.ToDouble(&dValue
);
349 m_led
->SetValue(wxString::Format(_T("%.2f"), dValue
));
352 void MyPanel::OnSetValue()
354 wxString strValue
= m_led
->GetValue();
356 strValue
= ::wxGetTextFromUser(_T("Please enter a number for LED display"), _T("Please enter a number"), strValue
, this);
358 if (strValue
!= _T(""))
359 m_led
->SetValue(strValue
);
362 void MyPanel::OnAlignLeft()
364 m_led
->SetAlignment(wxLED_ALIGN_LEFT
);
367 void MyPanel::OnAlignCenter()
369 m_led
->SetAlignment(wxLED_ALIGN_CENTER
);
372 void MyPanel::OnAlignRight()
374 m_led
->SetAlignment(wxLED_ALIGN_RIGHT
);
377 void MyPanel::OnDrawFaded()
379 m_led
->SetDrawFaded(!(m_led
->GetDrawFaded()));