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