]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: display.cpp | |
3 | // Purpose: wxWidgets sample showing the features of wxDisplay class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: Ryan Norton & Brian Victor | |
6 | // Created: 23.02.03 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin <vadim@wxwidgets.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" | |
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/wx.h" | |
30 | #endif | |
31 | ||
32 | #include "wx/bookctrl.h" | |
33 | ||
34 | #include "wx/display.h" | |
35 | ||
36 | ||
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 | ||
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); | |
69 | void OnFromPoint(wxCommandEvent& event); | |
70 | void OnFullScreen(wxCommandEvent& event); | |
71 | void OnAbout(wxCommandEvent& event); | |
72 | ||
73 | void OnChangeMode(wxCommandEvent& event); | |
74 | void OnResetMode(wxCommandEvent& event); | |
75 | ||
76 | void OnLeftClick(wxMouseEvent& event); | |
77 | ||
78 | #if wxUSE_DISPLAY | |
79 | void OnDisplayChanged(wxDisplayChangedEvent& event); | |
80 | #endif | |
81 | ||
82 | private: | |
83 | #if wxUSE_DISPLAY | |
84 | // convert video mode to textual description | |
85 | wxString VideoModeToText(const wxVideoMode& mode); | |
86 | #endif | |
87 | ||
88 | // GUI controls | |
89 | wxBookCtrl *m_book; | |
90 | ||
91 | // any class wishing to process wxWidgets events must use this macro | |
92 | DECLARE_EVENT_TABLE() | |
93 | }; | |
94 | ||
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 | ||
104 | // ---------------------------------------------------------------------------- | |
105 | // constants | |
106 | // ---------------------------------------------------------------------------- | |
107 | ||
108 | // IDs for the controls and the menu commands | |
109 | enum | |
110 | { | |
111 | // menu items | |
112 | Display_FromPoint = wxID_HIGHEST + 1, | |
113 | Display_FullScreen, | |
114 | ||
115 | // controls | |
116 | Display_ChangeMode, | |
117 | Display_ResetMode, | |
118 | Display_CurrentMode, | |
119 | ||
120 | ||
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) | |
124 | Display_Quit = wxID_EXIT, | |
125 | Display_About = wxID_ABOUT | |
126 | }; | |
127 | ||
128 | // ---------------------------------------------------------------------------- | |
129 | // event tables and other macros for wxWidgets | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | // the event tables connect the wxWidgets events with the functions (event | |
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) | |
137 | EVT_MENU(Display_FromPoint, MyFrame::OnFromPoint) | |
138 | EVT_MENU(Display_FullScreen, MyFrame::OnFullScreen) | |
139 | EVT_MENU(Display_About, MyFrame::OnAbout) | |
140 | ||
141 | EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode) | |
142 | EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode) | |
143 | ||
144 | EVT_LEFT_UP(MyFrame::OnLeftClick) | |
145 | ||
146 | #if wxUSE_DISPLAY | |
147 | EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged) | |
148 | #endif | |
149 | END_EVENT_TABLE() | |
150 | ||
151 | // Create a new application object: this macro will allow wxWidgets to create | |
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 | { | |
169 | #if !wxUSE_DISPLAY | |
170 | wxMessageBox(_("Please recompile wxWidgets and this sample with wxUSE_DISPLAY set to 1.")); | |
171 | return false; | |
172 | #else | |
173 | ||
174 | #ifdef __WXMSW__ | |
175 | if ( argc == 2 && !wxStricmp(argv[1], _T("/dx")) ) | |
176 | { | |
177 | wxDisplay::UseDirectX(true); | |
178 | } | |
179 | #endif // __WXMSW__ | |
180 | ||
181 | // create the main application window | |
182 | MyFrame *frame = new MyFrame(_("Display wxWidgets Sample"), | |
183 | wxDefaultPosition, wxDefaultSize); | |
184 | ||
185 | // and show it (the frames, unlike simple controls, are not shown when | |
186 | // created initially) | |
187 | frame->Show(); | |
188 | ||
189 | // success: wxApp::OnRun() will be called which will enter the main message | |
190 | // loop and the application will run. If we returned false here, the | |
191 | // application would exit immediately. | |
192 | return true; | |
193 | #endif | |
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) | |
202 | : wxFrame(NULL, wxID_ANY, title, pos, size, style) | |
203 | { | |
204 | // set the frame icon | |
205 | SetIcon(wxICON(sample)); | |
206 | ||
207 | #if wxUSE_MENUS | |
208 | // create a menu bar | |
209 | wxMenu *menuDisplay = new wxMenu; | |
210 | menuDisplay->Append(Display_FromPoint, _("Find from &point...")); | |
211 | menuDisplay->AppendSeparator(); | |
212 | menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12")); | |
213 | menuDisplay->AppendSeparator(); | |
214 | menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program")); | |
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 | ||
220 | // now append the freshly created menu to the menu bar... | |
221 | wxMenuBar *menuBar = new wxMenuBar(); | |
222 | menuBar->Append(menuDisplay, _("&Display")); | |
223 | menuBar->Append(helpMenu, _("&Help")); | |
224 | ||
225 | // ... and attach this menu bar to the frame | |
226 | SetMenuBar(menuBar); | |
227 | #endif // wxUSE_MENUS | |
228 | ||
229 | #if wxUSE_STATUSBAR | |
230 | // create status bar | |
231 | CreateStatusBar(); | |
232 | #endif // wxUSE_STATUSBAR | |
233 | ||
234 | #if wxUSE_DISPLAY | |
235 | // create child controls | |
236 | ||
237 | wxPanel *panel = new wxPanel(this, wxID_ANY); | |
238 | ||
239 | m_book = new wxBookCtrl(panel, wxID_ANY); | |
240 | const size_t count = wxDisplay::GetCount(); | |
241 | for ( size_t nDpy = 0; nDpy < count; nDpy++ ) | |
242 | { | |
243 | wxDisplay display(nDpy); | |
244 | ||
245 | wxWindow *page = new wxPanel(m_book, wxID_ANY); | |
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()); | |
252 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Origin: "))); | |
253 | sizer->Add(new wxStaticText | |
254 | ( | |
255 | page, | |
256 | wxID_ANY, | |
257 | wxString::Format(_T("(%d, %d)"), | |
258 | r.x, r.y) | |
259 | )); | |
260 | ||
261 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Size: "))); | |
262 | sizer->Add(new wxStaticText | |
263 | ( | |
264 | page, | |
265 | wxID_ANY, | |
266 | wxString::Format(_T("(%d, %d)"), | |
267 | r.width, r.height) | |
268 | )); | |
269 | ||
270 | ||
271 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Name: "))); | |
272 | sizer->Add(new wxStaticText(page, wxID_ANY, display.GetName())); | |
273 | ||
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 | ||
285 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("&Modes: "))); | |
286 | sizer->Add(choiceModes, 0, wxEXPAND); | |
287 | ||
288 | sizer->Add(new wxStaticText(page, wxID_ANY, _T("Current: "))); | |
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 | |
293 | wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL); | |
294 | sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10); | |
295 | ||
296 | sizerTop->Add(new wxButton(page, Display_ResetMode, _T("&Reset mode")), | |
297 | 0, wxALL | wxCENTRE, 5); | |
298 | page->SetSizer(sizerTop); | |
299 | ||
300 | m_book->AddPage(page, | |
301 | wxString::Format(_T("Display %lu"), | |
302 | (unsigned long)nDpy)); | |
303 | } | |
304 | ||
305 | wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); | |
306 | sizer->Add(m_book, 1, wxEXPAND); | |
307 | panel->SetSizer(sizer); | |
308 | sizer->Fit(this); | |
309 | sizer->SetSizeHints(this); | |
310 | #endif | |
311 | } | |
312 | ||
313 | #if wxUSE_DISPLAY | |
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 | } | |
331 | #endif | |
332 | ||
333 | // event handlers | |
334 | ||
335 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
336 | { | |
337 | // true is to force the frame to close | |
338 | Close(true); | |
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 | } | |
348 | ||
349 | void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event)) | |
350 | { | |
351 | #if wxUSE_STATUSBAR | |
352 | SetStatusText(_T("Press the mouse anywhere...")); | |
353 | #endif // wxUSE_STATUSBAR | |
354 | ||
355 | CaptureMouse(); | |
356 | } | |
357 | ||
358 | void MyFrame::OnFullScreen(wxCommandEvent& event) | |
359 | { | |
360 | ShowFullScreen(event.IsChecked()); | |
361 | } | |
362 | ||
363 | void MyFrame::OnChangeMode(wxCommandEvent& event) | |
364 | { | |
365 | #if wxUSE_DISPLAY | |
366 | wxDisplay dpy(m_book->GetSelection()); | |
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 | } | |
375 | #endif | |
376 | } | |
377 | ||
378 | void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event)) | |
379 | { | |
380 | #if wxUSE_DISPLAY | |
381 | wxDisplay dpy(m_book->GetSelection()); | |
382 | ||
383 | dpy.ResetMode(); | |
384 | #endif | |
385 | } | |
386 | ||
387 | void MyFrame::OnLeftClick(wxMouseEvent& event) | |
388 | { | |
389 | #if wxUSE_DISPLAY | |
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 | { | |
397 | wxLogError(_T("Mouse clicked outside of display!?")); | |
398 | } | |
399 | ||
400 | wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"), | |
401 | dpy, ptScreen.x, ptScreen.y); | |
402 | ||
403 | ReleaseMouse(); | |
404 | } | |
405 | #endif | |
406 | } | |
407 | ||
408 | #if wxUSE_DISPLAY | |
409 | void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event) | |
410 | { | |
411 | // update the current mode text | |
412 | for ( size_t n = 0; n < m_book->GetPageCount(); n++ ) | |
413 | { | |
414 | wxStaticText *label = wxDynamicCast(m_book->GetPage(n)-> | |
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 | } | |
426 | #endif | |
427 |