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