]>
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"
36 #include "wx/numdlg.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 class MyPanel
: public wxPanel
45 MyPanel(wxFrame
*frame
);
56 wxLEDNumberCtrl
*m_led
;
59 // Define a new application type, each program should derive a class from wxApp
60 class MyApp
: public wxApp
63 // override base class virtuals
64 // ----------------------------
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();
72 // Define a new frame type: this is going to be our main frame
73 class MyFrame
: public wxFrame
77 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
78 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
94 // any class wishing to process wxWindows events must use this macro
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // IDs for the controls and the menu commands
112 LED_Edit_AlignCenter
,
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
,
125 // ----------------------------------------------------------------------------
126 // event tables and other macros for wxWindows
127 // ----------------------------------------------------------------------------
129 // the event tables connect the wxWindows 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
)
144 // Create a new application object: this macro will allow wxWindows 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
151 // ============================================================================
153 // ============================================================================
155 // ----------------------------------------------------------------------------
156 // the application class
157 // ----------------------------------------------------------------------------
159 // 'Main program' equivalent: the program execution "starts" here
162 // create the main application window
163 MyFrame
*frame
= new MyFrame(_T("LED App"),
164 wxPoint(50, 50), wxSize(450, 340));
166 // and show it (the frames, unlike simple controls, are not shown when
167 // created initially)
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.
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
181 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
182 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
186 wxMenu
*menuFile
= new wxMenu
;
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"));
192 menuFile
->Append(LED_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
205 editMenu
->Check(LED_Edit_DrawFaded
, TRUE
);
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"));
213 // ... and attach this menu bar to the frame
215 #endif // wxUSE_MENUS
217 m_panel
= new MyPanel(this);
223 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
225 // TRUE is to force the frame to close
229 void MyFrame::OnIncrement(wxCommandEvent
& WXUNUSED(event
))
231 m_panel
->OnIncrement();
234 void MyFrame::OnDecrement(wxCommandEvent
& WXUNUSED(event
))
236 m_panel
->OnDecrement();
239 void MyFrame::OnSetValue(wxCommandEvent
& WXUNUSED(event
))
241 m_panel
->OnSetValue();
244 void MyFrame::OnAlignLeft(wxCommandEvent
& WXUNUSED(event
))
246 m_panel
->OnAlignLeft();
249 void MyFrame::OnAlignCenter(wxCommandEvent
& WXUNUSED(event
))
251 m_panel
->OnAlignCenter();
254 void MyFrame::OnAlignRight(wxCommandEvent
& WXUNUSED(event
))
256 m_panel
->OnAlignRight();
259 void MyFrame::OnDrawFaded(wxCommandEvent
& WXUNUSED(event
))
261 m_panel
->OnDrawFaded();
264 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
267 msg
.Printf( _T("This is the About dialog of the LED sample.\n")
268 _T("Welcome to %s"), wxVERSION_STRING
);
270 wxMessageBox(msg
, _T("About LED"), wxOK
| wxICON_INFORMATION
, this);
273 // --------------------------------------------------------------------------
275 // --------------------------------------------------------------------------
277 MyPanel::MyPanel(wxFrame
*frame
)
278 : wxPanel(frame
, MY_PANEL
)
280 m_led
= new wxLEDNumberCtrl(this, MY_LED
,
281 wxPoint(20, 20), wxSize(300, 200),
282 wxLED_ALIGN_LEFT
| wxLED_DRAW_FADED
);
284 m_led
->SetValue(_T("50"));
287 void MyPanel::OnIncrement()
289 wxString strValue
= m_led
->GetValue();
290 if ( strValue
== _T("99") )
294 strValue
.ToLong(&lValue
);
296 m_led
->SetValue(wxString::Format(_T("%ld"), lValue
));
299 void MyPanel::OnDecrement()
301 wxString strValue
= m_led
->GetValue();
304 strValue
.ToLong(&lValue
);
309 m_led
->SetValue(wxString::Format(_T("%ld"), lValue
));
312 void MyPanel::OnSetValue()
314 wxString strValue
= m_led
->GetValue();
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);
321 m_led
->SetValue(wxString::Format(_T("%ld"), lValue
));
324 void MyPanel::OnAlignLeft()
326 m_led
->SetAlignment(wxLED_ALIGN_LEFT
);
329 void MyPanel::OnAlignCenter()
331 m_led
->SetAlignment(wxLED_ALIGN_CENTER
);
334 void MyPanel::OnAlignRight()
336 m_led
->SetAlignment(wxLED_ALIGN_RIGHT
);
339 void MyPanel::OnDrawFaded()
341 m_led
->SetDrawFaded(!(m_led
->GetDrawFaded()));