]>
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 | ||
2ad495fb | 32 | #include "wx/bookctrl.h" |
c32b1077 VZ |
33 | |
34 | #include "wx/display.h" | |
35 | ||
6ae4d29a | 36 | |
63c51a93 RN |
37 | // the application icon (under Windows and OS/2 it is in resources) |
38 | #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__) | |
39 | #include "../sample.xpm" | |
40 | #endif | |
41 | ||
c32b1077 VZ |
42 | // ---------------------------------------------------------------------------- |
43 | // private classes | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // Define a new application type, each program should derive a class from wxApp | |
47 | class MyApp : public wxApp | |
48 | { | |
49 | public: | |
50 | // override base class virtuals | |
51 | // ---------------------------- | |
52 | ||
53 | // this one is called on application startup and is a good place for the app | |
54 | // initialization (doing it here and not in the ctor allows to have an error | |
55 | // return: if OnInit() returns false, the application terminates) | |
56 | virtual bool OnInit(); | |
57 | }; | |
58 | ||
59 | // Define a new frame type: this is going to be our main frame | |
60 | class MyFrame : public wxFrame | |
61 | { | |
62 | public: | |
63 | // ctor(s) | |
64 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, | |
65 | long style = wxDEFAULT_FRAME_STYLE); | |
66 | ||
67 | // event handlers (these functions should _not_ be virtual) | |
68 | void OnQuit(wxCommandEvent& event); | |
e9b77bcc | 69 | void OnFromPoint(wxCommandEvent& event); |
84267f0d | 70 | void OnFullScreen(wxCommandEvent& event); |
c32b1077 VZ |
71 | void OnAbout(wxCommandEvent& event); |
72 | ||
84267f0d VZ |
73 | void OnChangeMode(wxCommandEvent& event); |
74 | void OnResetMode(wxCommandEvent& event); | |
75 | ||
e9b77bcc VZ |
76 | void OnLeftClick(wxMouseEvent& event); |
77 | ||
2c1957be | 78 | #if wxUSE_DISPLAY |
84267f0d | 79 | void OnDisplayChanged(wxDisplayChangedEvent& event); |
2c1957be | 80 | #endif |
84267f0d | 81 | |
c32b1077 | 82 | private: |
2c1957be | 83 | #if wxUSE_DISPLAY |
84267f0d VZ |
84 | // convert video mode to textual description |
85 | wxString VideoModeToText(const wxVideoMode& mode); | |
2c1957be | 86 | #endif |
84267f0d VZ |
87 | |
88 | // GUI controls | |
2ad495fb | 89 | wxBookCtrl *m_book; |
84267f0d | 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 | |
2ad495fb | 112 | Display_FromPoint = wxID_HIGHEST + 1, |
84267f0d VZ |
113 | Display_FullScreen, |
114 | ||
115 | // controls | |
2ad495fb | 116 | Display_ChangeMode, |
84267f0d VZ |
117 | Display_ResetMode, |
118 | Display_CurrentMode, | |
119 | ||
e9b77bcc | 120 | |
c32b1077 VZ |
121 | // it is important for the id corresponding to the "About" command to have |
122 | // this standard value as otherwise it won't be handled properly under Mac | |
123 | // (where it is special and put into the "Apple" menu) | |
2ad495fb | 124 | Display_Quit = wxID_EXIT, |
c32b1077 VZ |
125 | Display_About = wxID_ABOUT |
126 | }; | |
127 | ||
128 | // ---------------------------------------------------------------------------- | |
be5a51fb | 129 | // event tables and other macros for wxWidgets |
c32b1077 VZ |
130 | // ---------------------------------------------------------------------------- |
131 | ||
be5a51fb | 132 | // the event tables connect the wxWidgets events with the functions (event |
c32b1077 VZ |
133 | // handlers) which process them. It can be also done at run-time, but for the |
134 | // simple menu events like this the static method is much simpler. | |
135 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
136 | EVT_MENU(Display_Quit, MyFrame::OnQuit) | |
e9b77bcc | 137 | EVT_MENU(Display_FromPoint, MyFrame::OnFromPoint) |
84267f0d | 138 | EVT_MENU(Display_FullScreen, MyFrame::OnFullScreen) |
c32b1077 | 139 | EVT_MENU(Display_About, MyFrame::OnAbout) |
e9b77bcc | 140 | |
84267f0d VZ |
141 | EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode) |
142 | EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode) | |
143 | ||
e9b77bcc | 144 | EVT_LEFT_UP(MyFrame::OnLeftClick) |
84267f0d | 145 | |
2c1957be | 146 | #if wxUSE_DISPLAY |
84267f0d | 147 | EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged) |
2c1957be | 148 | #endif |
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 | { | |
2c1957be JS |
169 | #if !wxUSE_DISPLAY |
170 | wxMessageBox(_("Please recompile wxWidgets and this sample with wxUSE_DISPLAY set to 1.")); | |
171 | return false; | |
172 | #else | |
173 | ||
e40bef9f VZ |
174 | #ifdef __WXMSW__ |
175 | if ( argc == 2 && !wxStricmp(argv[1], _T("/dx")) ) | |
176 | { | |
177 | wxDisplay::UseDirectX(true); | |
178 | } | |
179 | #endif // __WXMSW__ | |
180 | ||
c32b1077 | 181 | // create the main application window |
be5a51fb | 182 | MyFrame *frame = new MyFrame(_("Display wxWidgets Sample"), |
6ae4d29a | 183 | wxDefaultPosition, wxDefaultSize); |
c32b1077 VZ |
184 | |
185 | // and show it (the frames, unlike simple controls, are not shown when | |
186 | // created initially) | |
84267f0d | 187 | frame->Show(); |
c32b1077 VZ |
188 | |
189 | // success: wxApp::OnRun() will be called which will enter the main message | |
6ae4d29a | 190 | // loop and the application will run. If we returned false here, the |
c32b1077 | 191 | // application would exit immediately. |
6ae4d29a | 192 | return true; |
2c1957be | 193 | #endif |
c32b1077 VZ |
194 | } |
195 | ||
196 | // ---------------------------------------------------------------------------- | |
197 | // main frame | |
198 | // ---------------------------------------------------------------------------- | |
199 | ||
200 | // frame constructor | |
201 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) | |
6ae4d29a | 202 | : wxFrame(NULL, wxID_ANY, title, pos, size, style) |
c32b1077 VZ |
203 | { |
204 | // set the frame icon | |
63c51a93 | 205 | SetIcon(wxICON(sample)); |
c32b1077 VZ |
206 | |
207 | #if wxUSE_MENUS | |
208 | // create a menu bar | |
e9b77bcc | 209 | wxMenu *menuDisplay = new wxMenu; |
84267f0d VZ |
210 | menuDisplay->Append(Display_FromPoint, _("Find from &point...")); |
211 | menuDisplay->AppendSeparator(); | |
212 | menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12")); | |
e9b77bcc VZ |
213 | menuDisplay->AppendSeparator(); |
214 | menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program")); | |
c32b1077 VZ |
215 | |
216 | // the "About" item should be in the help menu | |
217 | wxMenu *helpMenu = new wxMenu; | |
218 | helpMenu->Append(Display_About, _("&About...\tF1"), _("Show about dialog")); | |
219 | ||
c32b1077 VZ |
220 | // now append the freshly created menu to the menu bar... |
221 | wxMenuBar *menuBar = new wxMenuBar(); | |
e9b77bcc | 222 | menuBar->Append(menuDisplay, _("&Display")); |
c32b1077 VZ |
223 | menuBar->Append(helpMenu, _("&Help")); |
224 | ||
225 | // ... and attach this menu bar to the frame | |
226 | SetMenuBar(menuBar); | |
227 | #endif // wxUSE_MENUS | |
228 | ||
8520f137 | 229 | #if wxUSE_STATUSBAR |
e9b77bcc VZ |
230 | // create status bar |
231 | CreateStatusBar(); | |
8520f137 | 232 | #endif // wxUSE_STATUSBAR |
e9b77bcc | 233 | |
2c1957be | 234 | #if wxUSE_DISPLAY |
c32b1077 | 235 | // create child controls |
6ae4d29a DS |
236 | |
237 | wxPanel *panel = new wxPanel(this, wxID_ANY); | |
238 | ||
2ad495fb | 239 | m_book = new wxBookCtrl(panel, wxID_ANY); |
c32b1077 | 240 | const size_t count = wxDisplay::GetCount(); |
84267f0d | 241 | for ( size_t nDpy = 0; nDpy < count; nDpy++ ) |
c32b1077 | 242 | { |
84267f0d | 243 | wxDisplay display(nDpy); |
c32b1077 | 244 | |
2ad495fb | 245 | wxWindow *page = new wxPanel(m_book, wxID_ANY); |
c32b1077 VZ |
246 | |
247 | // create 2 column flex grid sizer with growable 2nd column | |
248 | wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20); | |
249 | sizer->AddGrowableCol(1); | |
250 | ||
251 | const wxRect r(display.GetGeometry()); | |
6ae4d29a | 252 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Origin: "))); |
e9b77bcc VZ |
253 | sizer->Add(new wxStaticText |
254 | ( | |
255 | page, | |
6ae4d29a | 256 | wxID_ANY, |
e9b77bcc VZ |
257 | wxString::Format(_T("(%d, %d)"), |
258 | r.x, r.y) | |
259 | )); | |
260 | ||
6ae4d29a | 261 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Size: "))); |
c32b1077 VZ |
262 | sizer->Add(new wxStaticText |
263 | ( | |
264 | page, | |
6ae4d29a | 265 | wxID_ANY, |
e9b77bcc VZ |
266 | wxString::Format(_T("(%d, %d)"), |
267 | r.width, r.height) | |
c32b1077 VZ |
268 | )); |
269 | ||
c32b1077 | 270 | |
6ae4d29a DS |
271 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Name: "))); |
272 | sizer->Add(new wxStaticText(page, wxID_ANY, display.GetName())); | |
e9b77bcc | 273 | |
84267f0d VZ |
274 | wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode); |
275 | const wxArrayVideoModes modes = display.GetModes(); | |
276 | const size_t count = modes.GetCount(); | |
277 | for ( size_t nMode = 0; nMode < count; nMode++ ) | |
278 | { | |
279 | const wxVideoMode& mode = modes[nMode]; | |
280 | ||
281 | choiceModes->Append(VideoModeToText(mode), | |
282 | new MyVideoModeClientData(mode)); | |
283 | } | |
284 | ||
6ae4d29a | 285 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("&Modes: "))); |
84267f0d | 286 | sizer->Add(choiceModes, 0, wxEXPAND); |
c32b1077 | 287 | |
6ae4d29a | 288 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Current: "))); |
84267f0d VZ |
289 | sizer->Add(new wxStaticText(page, Display_CurrentMode, |
290 | VideoModeToText(display.GetCurrentMode()))); | |
291 | ||
292 | // add it to another sizer to have borders around it and button below | |
c32b1077 VZ |
293 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); |
294 | sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10); | |
6ae4d29a | 295 | |
84267f0d VZ |
296 | sizerTop->Add(new wxButton(page, Display_ResetMode, _T("&Reset mode")), |
297 | 0, wxALL | wxCENTRE, 5); | |
c32b1077 VZ |
298 | page->SetSizer(sizerTop); |
299 | ||
2ad495fb WS |
300 | m_book->AddPage(page, |
301 | wxString::Format(_T("Display %lu"), | |
302 | (unsigned long)nDpy)); | |
c32b1077 | 303 | } |
6ae4d29a | 304 | |
adbf2d73 | 305 | wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); |
2ad495fb | 306 | sizer->Add(m_book, 1, wxEXPAND); |
adbf2d73 VS |
307 | panel->SetSizer(sizer); |
308 | sizer->Fit(this); | |
309 | sizer->SetSizeHints(this); | |
2c1957be | 310 | #endif |
c32b1077 VZ |
311 | } |
312 | ||
2c1957be | 313 | #if wxUSE_DISPLAY |
84267f0d VZ |
314 | wxString MyFrame::VideoModeToText(const wxVideoMode& mode) |
315 | { | |
316 | wxString s; | |
317 | s.Printf(_T("%dx%d"), mode.w, mode.h); | |
318 | ||
319 | if ( mode.bpp ) | |
320 | { | |
321 | s += wxString::Format(_T(", %dbpp"), mode.bpp); | |
322 | } | |
323 | ||
324 | if ( mode.refresh ) | |
325 | { | |
326 | s += wxString::Format(_T(", %dHz"), mode.refresh); | |
327 | } | |
328 | ||
329 | return s; | |
330 | } | |
2c1957be | 331 | #endif |
c32b1077 VZ |
332 | |
333 | // event handlers | |
334 | ||
335 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
336 | { | |
6ae4d29a DS |
337 | // true is to force the frame to close |
338 | Close(true); | |
c32b1077 VZ |
339 | } |
340 | ||
341 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
342 | { | |
343 | wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"), | |
344 | _T("About Display Sample"), | |
345 | wxOK | wxICON_INFORMATION, | |
346 | this); | |
347 | } | |
e9b77bcc VZ |
348 | |
349 | void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event)) | |
350 | { | |
8520f137 | 351 | #if wxUSE_STATUSBAR |
e9b77bcc | 352 | SetStatusText(_T("Press the mouse anywhere...")); |
8520f137 | 353 | #endif // wxUSE_STATUSBAR |
e9b77bcc VZ |
354 | |
355 | CaptureMouse(); | |
356 | } | |
357 | ||
84267f0d VZ |
358 | void MyFrame::OnFullScreen(wxCommandEvent& event) |
359 | { | |
360 | ShowFullScreen(event.IsChecked()); | |
361 | } | |
362 | ||
363 | void MyFrame::OnChangeMode(wxCommandEvent& event) | |
364 | { | |
2c1957be | 365 | #if wxUSE_DISPLAY |
2ad495fb | 366 | wxDisplay dpy(m_book->GetSelection()); |
84267f0d VZ |
367 | |
368 | // you wouldn't write this in real code, would you? | |
369 | if ( !dpy.ChangeMode(((MyVideoModeClientData *) | |
370 | wxDynamicCast(event.GetEventObject(), wxChoice)-> | |
371 | GetClientObject(event.GetInt()))->mode) ) | |
372 | { | |
373 | wxLogError(_T("Changing video mode failed!")); | |
374 | } | |
2c1957be | 375 | #endif |
84267f0d VZ |
376 | } |
377 | ||
378 | void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event)) | |
379 | { | |
2c1957be | 380 | #if wxUSE_DISPLAY |
2ad495fb | 381 | wxDisplay dpy(m_book->GetSelection()); |
84267f0d VZ |
382 | |
383 | dpy.ResetMode(); | |
2c1957be | 384 | #endif |
84267f0d VZ |
385 | } |
386 | ||
e9b77bcc VZ |
387 | void MyFrame::OnLeftClick(wxMouseEvent& event) |
388 | { | |
2c1957be | 389 | #if wxUSE_DISPLAY |
e9b77bcc VZ |
390 | if ( HasCapture() ) |
391 | { | |
392 | // mouse events are in client coords, wxDisplay works in screen ones | |
393 | const wxPoint ptScreen = ClientToScreen(event.GetPosition()); | |
394 | int dpy = wxDisplay::GetFromPoint(ptScreen); | |
395 | if ( dpy == wxNOT_FOUND ) | |
396 | { | |
84267f0d | 397 | wxLogError(_T("Mouse clicked outside of display!?")); |
e9b77bcc VZ |
398 | } |
399 | ||
400 | wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"), | |
401 | dpy, ptScreen.x, ptScreen.y); | |
402 | ||
403 | ReleaseMouse(); | |
404 | } | |
2c1957be | 405 | #endif |
e9b77bcc VZ |
406 | } |
407 | ||
2c1957be | 408 | #if wxUSE_DISPLAY |
84267f0d VZ |
409 | void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event) |
410 | { | |
411 | // update the current mode text | |
2ad495fb | 412 | for ( size_t n = 0; n < m_book->GetPageCount(); n++ ) |
84267f0d | 413 | { |
2ad495fb | 414 | wxStaticText *label = wxDynamicCast(m_book->GetPage(n)-> |
84267f0d VZ |
415 | FindWindow(Display_CurrentMode), |
416 | wxStaticText); | |
417 | if ( label ) | |
418 | label->SetLabel(VideoModeToText(wxDisplay(n).GetCurrentMode())); | |
419 | } | |
420 | ||
421 | ||
422 | wxLogStatus(this, _T("Display resolution was changed.")); | |
423 | ||
424 | event.Skip(); | |
425 | } | |
2c1957be JS |
426 | #endif |
427 |