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