]>
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" wxWindows headers)
33 #include "../../../include/wx/gizmos/ledctrl.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 class MyPanel
: public wxPanel
44 MyPanel(wxFrame
*frame
);
55 wxLEDNumberCtrl
*m_led
;
58 // Define a new application type, each program should derive a class from wxApp
59 class MyApp
: public wxApp
62 // override base class virtuals
63 // ----------------------------
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();
71 // Define a new frame type: this is going to be our main frame
72 class MyFrame
: public wxFrame
76 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
77 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
93 // any class wishing to process wxWindows events must use this macro
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // IDs for the controls and the menu commands
111 LED_Edit_AlignCenter
,
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
,
124 // ----------------------------------------------------------------------------
125 // event tables and other macros for wxWindows
126 // ----------------------------------------------------------------------------
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
)
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
150 // ============================================================================
152 // ============================================================================
154 // ----------------------------------------------------------------------------
155 // the application class
156 // ----------------------------------------------------------------------------
158 // 'Main program' equivalent: the program execution "starts" here
161 // create the main application window
162 MyFrame
*frame
= new MyFrame(_T("LED App"),
163 wxPoint(50, 50), wxSize(450, 340));
165 // and show it (the frames, unlike simple controls, are not shown when
166 // created initially)
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.
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
180 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
181 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
185 wxMenu
*menuFile
= new wxMenu
;
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"));
191 menuFile
->Append(LED_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
204 editMenu
->Check(LED_Edit_DrawFaded
, TRUE
);
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"));
212 // ... and attach this menu bar to the frame
214 #endif // wxUSE_MENUS
216 m_panel
= new MyPanel(this);
222 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
224 // TRUE is to force the frame to close
228 void MyFrame::OnIncrement(wxCommandEvent
& WXUNUSED(event
))
230 m_panel
->OnIncrement();
233 void MyFrame::OnDecrement(wxCommandEvent
& WXUNUSED(event
))
235 m_panel
->OnDecrement();
238 void MyFrame::OnSetValue(wxCommandEvent
& WXUNUSED(event
))
240 m_panel
->OnSetValue();
243 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
245 m_panel
->OnAlignLeft();
248 void MyFrame::OnAlignCenter(wxCommandEvent
& WXUNUSED(event
))
250 m_panel
->OnAlignCenter();
253 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
255 m_panel
->OnAlignRight();
258 void MyFrame::OnDrawFaded(wxCommandEvent
& WXUNUSED(event
))
260 m_panel
->OnDrawFaded();
263 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
266 msg
.Printf( _T("This is the About dialog of the LED sample.\n")
267 _T("Welcome to %s"), wxVERSION_STRING
);
269 wxMessageBox(msg
, _T("About LED"), wxOK
| wxICON_INFORMATION
, this);
272 // --------------------------------------------------------------------------
274 // --------------------------------------------------------------------------
276 MyPanel::MyPanel(wxFrame
*frame
)
277 : wxPanel(frame
, MY_PANEL
)
279 m_led
= new wxLEDNumberCtrl(this, MY_LED
,
280 wxPoint(20, 20), wxSize(300, 200),
281 wxLED_ALIGN_LEFT
| wxLED_DRAW_FADED
);
283 m_led
->SetValue(_T("50"));
286 void MyPanel::OnIncrement()
288 wxString strValue
= m_led
->GetValue();
289 if ( strValue
== _T("99") )
293 strValue
.ToLong(&lValue
);
295 m_led
->SetValue(wxString::Format(_T("%ld"), lValue
));
298 void MyPanel::OnDecrement()
300 wxString strValue
= m_led
->GetValue();
303 strValue
.ToLong(&lValue
);
308 m_led
->SetValue(wxString::Format(_T("%ld"), lValue
));
311 void MyPanel::OnSetValue()
313 wxString strValue
= m_led
->GetValue();
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);
320 m_led
->SetValue(wxString::Format(_T("%ld"), lValue
));
323 void MyPanel::OnAlignLeft()
325 m_led
->SetAlignment(wxLED_ALIGN_LEFT
);
328 void MyPanel::OnAlignCenter()
330 m_led
->SetAlignment(wxLED_ALIGN_CENTER
);
333 void MyPanel::OnAlignRight()
335 m_led
->SetAlignment(wxLED_ALIGN_RIGHT
);
338 void MyPanel::OnDrawFaded()
340 m_led
->SetDrawFaded(!(m_led
->GetDrawFaded()));