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