]>
Commit | Line | Data |
---|---|---|
c32b1077 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: display.cpp | |
3 | // Purpose: wxWindows sample showing the features of wxDisplay class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 23.02.03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin <vadim@wxwindows.org> | |
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".from here | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers explicitly | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/app.h" | |
30 | #include "wx/frame.h" | |
31 | ||
32 | #include "wx/stattext.h" | |
33 | ||
34 | #include "wx/layout.h" | |
35 | #endif | |
36 | ||
37 | #include "wx/notebook.h" | |
38 | ||
39 | #include "wx/display.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // private classes | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // Define a new application type, each program should derive a class from wxApp | |
46 | class MyApp : public wxApp | |
47 | { | |
48 | public: | |
49 | // override base class virtuals | |
50 | // ---------------------------- | |
51 | ||
52 | // this one is called on application startup and is a good place for the app | |
53 | // initialization (doing it here and not in the ctor allows to have an error | |
54 | // return: if OnInit() returns false, the application terminates) | |
55 | virtual bool OnInit(); | |
56 | }; | |
57 | ||
58 | // Define a new frame type: this is going to be our main frame | |
59 | class MyFrame : public wxFrame | |
60 | { | |
61 | public: | |
62 | // ctor(s) | |
63 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, | |
64 | long style = wxDEFAULT_FRAME_STYLE); | |
65 | ||
66 | // event handlers (these functions should _not_ be virtual) | |
67 | void OnQuit(wxCommandEvent& event); | |
e9b77bcc | 68 | void OnFromPoint(wxCommandEvent& event); |
84267f0d | 69 | void OnFullScreen(wxCommandEvent& event); |
c32b1077 VZ |
70 | void OnAbout(wxCommandEvent& event); |
71 | ||
84267f0d VZ |
72 | void OnChangeMode(wxCommandEvent& event); |
73 | void OnResetMode(wxCommandEvent& event); | |
74 | ||
e9b77bcc VZ |
75 | void OnLeftClick(wxMouseEvent& event); |
76 | ||
84267f0d VZ |
77 | void OnDisplayChanged(wxDisplayChangedEvent& event); |
78 | ||
c32b1077 | 79 | private: |
84267f0d VZ |
80 | // convert video mode to textual description |
81 | wxString VideoModeToText(const wxVideoMode& mode); | |
82 | ||
83 | // GUI controls | |
84 | wxNotebook *m_notebook; | |
85 | ||
c32b1077 VZ |
86 | // any class wishing to process wxWindows events must use this macro |
87 | DECLARE_EVENT_TABLE() | |
88 | }; | |
89 | ||
84267f0d VZ |
90 | // Client data class for the choice control containing the video modes |
91 | class MyVideoModeClientData : public wxClientData | |
92 | { | |
93 | public: | |
94 | MyVideoModeClientData(const wxVideoMode& m) : mode(m) { } | |
95 | ||
96 | const wxVideoMode mode; | |
97 | }; | |
98 | ||
c32b1077 VZ |
99 | // ---------------------------------------------------------------------------- |
100 | // constants | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | // IDs for the controls and the menu commands | |
104 | enum | |
105 | { | |
106 | // menu items | |
107 | Display_Quit = 1, | |
108 | ||
e9b77bcc | 109 | Display_FromPoint, |
84267f0d VZ |
110 | Display_FullScreen, |
111 | ||
112 | // controls | |
113 | Display_ChangeMode = 1000, | |
114 | Display_ResetMode, | |
115 | Display_CurrentMode, | |
116 | ||
e9b77bcc | 117 | |
c32b1077 VZ |
118 | // it is important for the id corresponding to the "About" command to have |
119 | // this standard value as otherwise it won't be handled properly under Mac | |
120 | // (where it is special and put into the "Apple" menu) | |
121 | Display_About = wxID_ABOUT | |
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(Display_Quit, MyFrame::OnQuit) | |
e9b77bcc | 133 | EVT_MENU(Display_FromPoint, MyFrame::OnFromPoint) |
84267f0d | 134 | EVT_MENU(Display_FullScreen, MyFrame::OnFullScreen) |
c32b1077 | 135 | EVT_MENU(Display_About, MyFrame::OnAbout) |
e9b77bcc | 136 | |
84267f0d VZ |
137 | EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode) |
138 | EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode) | |
139 | ||
e9b77bcc | 140 | EVT_LEFT_UP(MyFrame::OnLeftClick) |
84267f0d VZ |
141 | |
142 | ||
143 | EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged) | |
c32b1077 VZ |
144 | END_EVENT_TABLE() |
145 | ||
146 | // Create a new application object: this macro will allow wxWindows to create | |
147 | // the application object during program execution (it's better than using a | |
148 | // static object for many reasons) and also declares the accessor function | |
149 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
150 | // not wxApp) | |
151 | IMPLEMENT_APP(MyApp) | |
152 | ||
153 | // ============================================================================ | |
154 | // implementation | |
155 | // ============================================================================ | |
156 | ||
157 | // ---------------------------------------------------------------------------- | |
158 | // the application class | |
159 | // ---------------------------------------------------------------------------- | |
160 | ||
161 | // 'Main program' equivalent: the program execution "starts" here | |
162 | bool MyApp::OnInit() | |
163 | { | |
164 | // create the main application window | |
165 | MyFrame *frame = new MyFrame(_("Display wxWindows Sample"), | |
166 | wxPoint(50, 50), wxSize(450, 340)); | |
167 | ||
168 | // and show it (the frames, unlike simple controls, are not shown when | |
169 | // created initially) | |
84267f0d | 170 | frame->Show(); |
c32b1077 VZ |
171 | |
172 | // success: wxApp::OnRun() will be called which will enter the main message | |
173 | // loop and the application will run. If we returned FALSE here, the | |
174 | // application would exit immediately. | |
175 | return TRUE; | |
176 | } | |
177 | ||
178 | // ---------------------------------------------------------------------------- | |
179 | // main frame | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | // frame constructor | |
183 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) | |
184 | : wxFrame(NULL, -1, title, pos, size, style) | |
185 | { | |
186 | // set the frame icon | |
187 | SetIcon(wxICON(mondrian)); | |
188 | ||
189 | #if wxUSE_MENUS | |
190 | // create a menu bar | |
e9b77bcc | 191 | wxMenu *menuDisplay = new wxMenu; |
84267f0d VZ |
192 | menuDisplay->Append(Display_FromPoint, _("Find from &point...")); |
193 | menuDisplay->AppendSeparator(); | |
194 | menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12")); | |
e9b77bcc VZ |
195 | menuDisplay->AppendSeparator(); |
196 | menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program")); | |
c32b1077 VZ |
197 | |
198 | // the "About" item should be in the help menu | |
199 | wxMenu *helpMenu = new wxMenu; | |
200 | helpMenu->Append(Display_About, _("&About...\tF1"), _("Show about dialog")); | |
201 | ||
c32b1077 VZ |
202 | // now append the freshly created menu to the menu bar... |
203 | wxMenuBar *menuBar = new wxMenuBar(); | |
e9b77bcc | 204 | menuBar->Append(menuDisplay, _("&Display")); |
c32b1077 VZ |
205 | menuBar->Append(helpMenu, _("&Help")); |
206 | ||
207 | // ... and attach this menu bar to the frame | |
208 | SetMenuBar(menuBar); | |
209 | #endif // wxUSE_MENUS | |
210 | ||
e9b77bcc VZ |
211 | // create status bar |
212 | CreateStatusBar(); | |
213 | ||
c32b1077 | 214 | // create child controls |
84267f0d | 215 | m_notebook = new wxNotebook(this, -1); |
c32b1077 | 216 | const size_t count = wxDisplay::GetCount(); |
84267f0d | 217 | for ( size_t nDpy = 0; nDpy < count; nDpy++ ) |
c32b1077 | 218 | { |
84267f0d | 219 | wxDisplay display(nDpy); |
c32b1077 | 220 | |
84267f0d | 221 | wxWindow *page = new wxPanel(m_notebook, -1); |
c32b1077 VZ |
222 | |
223 | // create 2 column flex grid sizer with growable 2nd column | |
224 | wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20); | |
225 | sizer->AddGrowableCol(1); | |
226 | ||
227 | const wxRect r(display.GetGeometry()); | |
e9b77bcc VZ |
228 | sizer->Add(new wxStaticText(page, -1, _T("Origin: "))); |
229 | sizer->Add(new wxStaticText | |
230 | ( | |
231 | page, | |
232 | -1, | |
233 | wxString::Format(_T("(%d, %d)"), | |
234 | r.x, r.y) | |
235 | )); | |
236 | ||
237 | sizer->Add(new wxStaticText(page, -1, _T("Size: "))); | |
c32b1077 VZ |
238 | sizer->Add(new wxStaticText |
239 | ( | |
240 | page, | |
241 | -1, | |
e9b77bcc VZ |
242 | wxString::Format(_T("(%d, %d)"), |
243 | r.width, r.height) | |
c32b1077 VZ |
244 | )); |
245 | ||
c32b1077 | 246 | |
e9b77bcc VZ |
247 | sizer->Add(new wxStaticText(page, -1, _T("Name: "))); |
248 | sizer->Add(new wxStaticText(page, -1, display.GetName())); | |
249 | ||
84267f0d VZ |
250 | wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode); |
251 | const wxArrayVideoModes modes = display.GetModes(); | |
252 | const size_t count = modes.GetCount(); | |
253 | for ( size_t nMode = 0; nMode < count; nMode++ ) | |
254 | { | |
255 | const wxVideoMode& mode = modes[nMode]; | |
256 | ||
257 | choiceModes->Append(VideoModeToText(mode), | |
258 | new MyVideoModeClientData(mode)); | |
259 | } | |
260 | ||
261 | sizer->Add(new wxStaticText(page, -1, _T("&Modes: "))); | |
262 | sizer->Add(choiceModes, 0, wxEXPAND); | |
c32b1077 | 263 | |
84267f0d VZ |
264 | sizer->Add(new wxStaticText(page, -1, _T("Current: "))); |
265 | sizer->Add(new wxStaticText(page, Display_CurrentMode, | |
266 | VideoModeToText(display.GetCurrentMode()))); | |
267 | ||
268 | // add it to another sizer to have borders around it and button below | |
c32b1077 VZ |
269 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); |
270 | sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10); | |
84267f0d VZ |
271 | sizerTop->Add(new wxButton(page, Display_ResetMode, _T("&Reset mode")), |
272 | 0, wxALL | wxCENTRE, 5); | |
c32b1077 VZ |
273 | page->SetSizer(sizerTop); |
274 | ||
84267f0d VZ |
275 | m_notebook->AddPage(page, |
276 | wxString::Format(_T("Display %lu"), | |
277 | (unsigned long)nDpy)); | |
c32b1077 VZ |
278 | } |
279 | } | |
280 | ||
84267f0d VZ |
281 | wxString MyFrame::VideoModeToText(const wxVideoMode& mode) |
282 | { | |
283 | wxString s; | |
284 | s.Printf(_T("%dx%d"), mode.w, mode.h); | |
285 | ||
286 | if ( mode.bpp ) | |
287 | { | |
288 | s += wxString::Format(_T(", %dbpp"), mode.bpp); | |
289 | } | |
290 | ||
291 | if ( mode.refresh ) | |
292 | { | |
293 | s += wxString::Format(_T(", %dHz"), mode.refresh); | |
294 | } | |
295 | ||
296 | return s; | |
297 | } | |
c32b1077 VZ |
298 | |
299 | // event handlers | |
300 | ||
301 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
302 | { | |
303 | // TRUE is to force the frame to close | |
304 | Close(TRUE); | |
305 | } | |
306 | ||
307 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
308 | { | |
84267f0d VZ |
309 | wxDisplay dpy(1); |
310 | if ( !dpy.ChangeMode(wxVideoMode(800, 600)) ) | |
311 | { | |
312 | wxLogError("Failed!"); | |
313 | return; | |
314 | } | |
315 | ||
c32b1077 VZ |
316 | wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"), |
317 | _T("About Display Sample"), | |
318 | wxOK | wxICON_INFORMATION, | |
319 | this); | |
320 | } | |
e9b77bcc VZ |
321 | |
322 | void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event)) | |
323 | { | |
324 | SetStatusText(_T("Press the mouse anywhere...")); | |
325 | ||
326 | CaptureMouse(); | |
327 | } | |
328 | ||
84267f0d VZ |
329 | void MyFrame::OnFullScreen(wxCommandEvent& event) |
330 | { | |
331 | ShowFullScreen(event.IsChecked()); | |
332 | } | |
333 | ||
334 | void MyFrame::OnChangeMode(wxCommandEvent& event) | |
335 | { | |
336 | wxDisplay dpy(m_notebook->GetSelection()); | |
337 | ||
338 | // you wouldn't write this in real code, would you? | |
339 | if ( !dpy.ChangeMode(((MyVideoModeClientData *) | |
340 | wxDynamicCast(event.GetEventObject(), wxChoice)-> | |
341 | GetClientObject(event.GetInt()))->mode) ) | |
342 | { | |
343 | wxLogError(_T("Changing video mode failed!")); | |
344 | } | |
345 | } | |
346 | ||
347 | void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event)) | |
348 | { | |
349 | wxDisplay dpy(m_notebook->GetSelection()); | |
350 | ||
351 | dpy.ResetMode(); | |
352 | } | |
353 | ||
e9b77bcc VZ |
354 | void MyFrame::OnLeftClick(wxMouseEvent& event) |
355 | { | |
356 | if ( HasCapture() ) | |
357 | { | |
358 | // mouse events are in client coords, wxDisplay works in screen ones | |
359 | const wxPoint ptScreen = ClientToScreen(event.GetPosition()); | |
360 | int dpy = wxDisplay::GetFromPoint(ptScreen); | |
361 | if ( dpy == wxNOT_FOUND ) | |
362 | { | |
84267f0d | 363 | wxLogError(_T("Mouse clicked outside of display!?")); |
e9b77bcc VZ |
364 | } |
365 | ||
366 | wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"), | |
367 | dpy, ptScreen.x, ptScreen.y); | |
368 | ||
369 | ReleaseMouse(); | |
370 | } | |
371 | } | |
372 | ||
84267f0d VZ |
373 | void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event) |
374 | { | |
375 | // update the current mode text | |
376 | for ( int n = 0; n < m_notebook->GetPageCount(); n++ ) | |
377 | { | |
378 | wxStaticText *label = wxDynamicCast(m_notebook->GetPage(n)-> | |
379 | FindWindow(Display_CurrentMode), | |
380 | wxStaticText); | |
381 | if ( label ) | |
382 | label->SetLabel(VideoModeToText(wxDisplay(n).GetCurrentMode())); | |
383 | } | |
384 | ||
385 | ||
386 | wxLogStatus(this, _T("Display resolution was changed.")); | |
387 | ||
388 | event.Skip(); | |
389 | } | |
390 |