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