]> git.saurik.com Git - wxWidgets.git/blame - samples/display/display.cpp
use wxBitmap objects instead of pointers
[wxWidgets.git] / samples / display / display.cpp
CommitLineData
c32b1077
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: display.cpp
be5a51fb 3// Purpose: wxWidgets sample showing the features of wxDisplay class
c32b1077 4// Author: Vadim Zeitlin
63c51a93 5// Modified by: Ryan Norton & Brian Victor
c32b1077
VZ
6// Created: 23.02.03
7// RCS-ID: $Id$
be5a51fb 8// Copyright: (c) Vadim Zeitlin <vadim@wxwidgets.org>
c32b1077
VZ
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
6ae4d29a 20// for compilers that support precompilation, includes "wx/wx.h"
c32b1077
VZ
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
6ae4d29a
DS
29 #include "wx/wx.h"
30#endif
31
2ad495fb 32#include "wx/bookctrl.h"
22d679f3 33#include "wx/sysopt.h"
c32b1077
VZ
34
35#include "wx/display.h"
36
6ae4d29a 37
63c51a93
RN
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
c32b1077
VZ
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// Define a new application type, each program should derive a class from wxApp
48class MyApp : public wxApp
49{
50public:
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
61class MyFrame : public wxFrame
62{
63public:
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);
e9b77bcc 70 void OnFromPoint(wxCommandEvent& event);
84267f0d 71 void OnFullScreen(wxCommandEvent& event);
c32b1077
VZ
72 void OnAbout(wxCommandEvent& event);
73
22d679f3 74#if wxUSE_DISPLAY
84267f0d
VZ
75 void OnChangeMode(wxCommandEvent& event);
76 void OnResetMode(wxCommandEvent& event);
77
84267f0d 78 void OnDisplayChanged(wxDisplayChangedEvent& event);
22d679f3
VZ
79#endif // wxUSE_DISPLAY
80
81 void OnLeftClick(wxMouseEvent& event);
84267f0d 82
c32b1077 83private:
2c1957be 84#if wxUSE_DISPLAY
84267f0d
VZ
85 // convert video mode to textual description
86 wxString VideoModeToText(const wxVideoMode& mode);
22d679f3 87#endif // wxUSE_DISPLAY
84267f0d
VZ
88
89 // GUI controls
2ad495fb 90 wxBookCtrl *m_book;
84267f0d 91
be5a51fb 92 // any class wishing to process wxWidgets events must use this macro
c32b1077
VZ
93 DECLARE_EVENT_TABLE()
94};
95
84267f0d
VZ
96// Client data class for the choice control containing the video modes
97class MyVideoModeClientData : public wxClientData
98{
99public:
100 MyVideoModeClientData(const wxVideoMode& m) : mode(m) { }
101
102 const wxVideoMode mode;
103};
104
c32b1077
VZ
105// ----------------------------------------------------------------------------
106// constants
107// ----------------------------------------------------------------------------
108
109// IDs for the controls and the menu commands
110enum
111{
112 // menu items
2ad495fb 113 Display_FromPoint = wxID_HIGHEST + 1,
84267f0d
VZ
114 Display_FullScreen,
115
116 // controls
2ad495fb 117 Display_ChangeMode,
84267f0d
VZ
118 Display_ResetMode,
119 Display_CurrentMode,
120
e9b77bcc 121
c32b1077
VZ
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)
2ad495fb 125 Display_Quit = wxID_EXIT,
c32b1077
VZ
126 Display_About = wxID_ABOUT
127};
128
129// ----------------------------------------------------------------------------
be5a51fb 130// event tables and other macros for wxWidgets
c32b1077
VZ
131// ----------------------------------------------------------------------------
132
be5a51fb 133// the event tables connect the wxWidgets events with the functions (event
c32b1077
VZ
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.
136BEGIN_EVENT_TABLE(MyFrame, wxFrame)
137 EVT_MENU(Display_Quit, MyFrame::OnQuit)
e9b77bcc 138 EVT_MENU(Display_FromPoint, MyFrame::OnFromPoint)
84267f0d 139 EVT_MENU(Display_FullScreen, MyFrame::OnFullScreen)
c32b1077 140 EVT_MENU(Display_About, MyFrame::OnAbout)
e9b77bcc 141
22d679f3 142#if wxUSE_DISPLAY
84267f0d
VZ
143 EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode)
144 EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode)
145
84267f0d 146 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged)
22d679f3
VZ
147#endif // wxUSE_DISPLAY
148
149 EVT_LEFT_UP(MyFrame::OnLeftClick)
c32b1077
VZ
150END_EVENT_TABLE()
151
be5a51fb 152// Create a new application object: this macro will allow wxWidgets to create
c32b1077
VZ
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)
157IMPLEMENT_APP(MyApp)
158
159// ============================================================================
160// implementation
161// ============================================================================
162
163// ----------------------------------------------------------------------------
164// the application class
165// ----------------------------------------------------------------------------
166
167// 'Main program' equivalent: the program execution "starts" here
168bool MyApp::OnInit()
169{
e40bef9f
VZ
170#ifdef __WXMSW__
171 if ( argc == 2 && !wxStricmp(argv[1], _T("/dx")) )
172 {
22d679f3 173 wxSystemOptions::SetOption(_T("msw.display.directdraw"), 1);
e40bef9f
VZ
174 }
175#endif // __WXMSW__
176
c32b1077 177 // create the main application window
be5a51fb 178 MyFrame *frame = new MyFrame(_("Display wxWidgets Sample"),
6ae4d29a 179 wxDefaultPosition, wxDefaultSize);
c32b1077
VZ
180
181 // and show it (the frames, unlike simple controls, are not shown when
182 // created initially)
84267f0d 183 frame->Show();
c32b1077
VZ
184
185 // success: wxApp::OnRun() will be called which will enter the main message
6ae4d29a 186 // loop and the application will run. If we returned false here, the
c32b1077 187 // application would exit immediately.
6ae4d29a 188 return true;
c32b1077
VZ
189}
190
191// ----------------------------------------------------------------------------
192// main frame
193// ----------------------------------------------------------------------------
194
195// frame constructor
196MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
6ae4d29a 197 : wxFrame(NULL, wxID_ANY, title, pos, size, style)
c32b1077
VZ
198{
199 // set the frame icon
63c51a93 200 SetIcon(wxICON(sample));
c32b1077
VZ
201
202#if wxUSE_MENUS
203 // create a menu bar
e9b77bcc 204 wxMenu *menuDisplay = new wxMenu;
84267f0d
VZ
205 menuDisplay->Append(Display_FromPoint, _("Find from &point..."));
206 menuDisplay->AppendSeparator();
207 menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12"));
e9b77bcc
VZ
208 menuDisplay->AppendSeparator();
209 menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program"));
c32b1077
VZ
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
c32b1077
VZ
215 // now append the freshly created menu to the menu bar...
216 wxMenuBar *menuBar = new wxMenuBar();
e9b77bcc 217 menuBar->Append(menuDisplay, _("&Display"));
c32b1077
VZ
218 menuBar->Append(helpMenu, _("&Help"));
219
220 // ... and attach this menu bar to the frame
221 SetMenuBar(menuBar);
222#endif // wxUSE_MENUS
223
8520f137 224#if wxUSE_STATUSBAR
e9b77bcc
VZ
225 // create status bar
226 CreateStatusBar();
8520f137 227#endif // wxUSE_STATUSBAR
e9b77bcc 228
c32b1077 229 // create child controls
6ae4d29a
DS
230 wxPanel *panel = new wxPanel(this, wxID_ANY);
231
2ad495fb 232 m_book = new wxBookCtrl(panel, wxID_ANY);
c32b1077 233 const size_t count = wxDisplay::GetCount();
84267f0d 234 for ( size_t nDpy = 0; nDpy < count; nDpy++ )
c32b1077 235 {
84267f0d 236 wxDisplay display(nDpy);
c32b1077 237
2ad495fb 238 wxWindow *page = new wxPanel(m_book, wxID_ANY);
c32b1077
VZ
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());
6ae4d29a 245 sizer->Add(new wxStaticText(page, wxID_ANY, _T("Origin: ")));
e9b77bcc
VZ
246 sizer->Add(new wxStaticText
247 (
248 page,
6ae4d29a 249 wxID_ANY,
e9b77bcc
VZ
250 wxString::Format(_T("(%d, %d)"),
251 r.x, r.y)
252 ));
253
6ae4d29a 254 sizer->Add(new wxStaticText(page, wxID_ANY, _T("Size: ")));
c32b1077
VZ
255 sizer->Add(new wxStaticText
256 (
257 page,
6ae4d29a 258 wxID_ANY,
e9b77bcc
VZ
259 wxString::Format(_T("(%d, %d)"),
260 r.width, r.height)
c32b1077
VZ
261 ));
262
6c5d6291
VZ
263 const wxRect rc(display.GetClientArea());
264 sizer->Add(new wxStaticText(page, wxID_ANY, _T("Client area: ")));
265 sizer->Add(new wxStaticText
266 (
267 page,
268 wxID_ANY,
269 wxString::Format(_T("(%d, %d)-(%d, %d)"),
270 rc.x, rc.y, rc.width, rc.height)
271 ));
c32b1077 272
6ae4d29a
DS
273 sizer->Add(new wxStaticText(page, wxID_ANY, _T("Name: ")));
274 sizer->Add(new wxStaticText(page, wxID_ANY, display.GetName()));
e9b77bcc 275
22d679f3
VZ
276 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
277 sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10);
278
279#if wxUSE_DISPLAY
84267f0d
VZ
280 wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode);
281 const wxArrayVideoModes modes = display.GetModes();
282 const size_t count = modes.GetCount();
283 for ( size_t nMode = 0; nMode < count; nMode++ )
284 {
285 const wxVideoMode& mode = modes[nMode];
286
287 choiceModes->Append(VideoModeToText(mode),
288 new MyVideoModeClientData(mode));
289 }
290
6ae4d29a 291 sizer->Add(new wxStaticText(page, wxID_ANY, _T("&Modes: ")));
84267f0d 292 sizer->Add(choiceModes, 0, wxEXPAND);
c32b1077 293
6ae4d29a 294 sizer->Add(new wxStaticText(page, wxID_ANY, _T("Current: ")));
84267f0d
VZ
295 sizer->Add(new wxStaticText(page, Display_CurrentMode,
296 VideoModeToText(display.GetCurrentMode())));
297
298 // add it to another sizer to have borders around it and button below
84267f0d
VZ
299 sizerTop->Add(new wxButton(page, Display_ResetMode, _T("&Reset mode")),
300 0, wxALL | wxCENTRE, 5);
22d679f3
VZ
301#endif // wxUSE_DISPLAY
302
c32b1077
VZ
303 page->SetSizer(sizerTop);
304
2ad495fb
WS
305 m_book->AddPage(page,
306 wxString::Format(_T("Display %lu"),
307 (unsigned long)nDpy));
c32b1077 308 }
6ae4d29a 309
adbf2d73 310 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
2ad495fb 311 sizer->Add(m_book, 1, wxEXPAND);
adbf2d73
VS
312 panel->SetSizer(sizer);
313 sizer->Fit(this);
314 sizer->SetSizeHints(this);
c32b1077
VZ
315}
316
2c1957be 317#if wxUSE_DISPLAY
22d679f3 318
84267f0d
VZ
319wxString MyFrame::VideoModeToText(const wxVideoMode& mode)
320{
321 wxString s;
322 s.Printf(_T("%dx%d"), mode.w, mode.h);
323
324 if ( mode.bpp )
325 {
326 s += wxString::Format(_T(", %dbpp"), mode.bpp);
327 }
328
329 if ( mode.refresh )
330 {
331 s += wxString::Format(_T(", %dHz"), mode.refresh);
332 }
333
334 return s;
335}
22d679f3
VZ
336
337#endif // wxUSE_DISPLAY
c32b1077
VZ
338
339// event handlers
340
341void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
342{
6ae4d29a
DS
343 // true is to force the frame to close
344 Close(true);
c32b1077
VZ
345}
346
347void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
348{
22d679f3 349 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
c32b1077
VZ
350 _T("About Display Sample"),
351 wxOK | wxICON_INFORMATION,
352 this);
353}
e9b77bcc
VZ
354
355void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event))
356{
8520f137 357#if wxUSE_STATUSBAR
e9b77bcc 358 SetStatusText(_T("Press the mouse anywhere..."));
8520f137 359#endif // wxUSE_STATUSBAR
e9b77bcc
VZ
360
361 CaptureMouse();
362}
363
84267f0d
VZ
364void MyFrame::OnFullScreen(wxCommandEvent& event)
365{
366 ShowFullScreen(event.IsChecked());
367}
368
22d679f3
VZ
369#if wxUSE_DISPLAY
370
84267f0d
VZ
371void MyFrame::OnChangeMode(wxCommandEvent& event)
372{
2ad495fb 373 wxDisplay dpy(m_book->GetSelection());
84267f0d
VZ
374
375 // you wouldn't write this in real code, would you?
376 if ( !dpy.ChangeMode(((MyVideoModeClientData *)
377 wxDynamicCast(event.GetEventObject(), wxChoice)->
378 GetClientObject(event.GetInt()))->mode) )
379 {
380 wxLogError(_T("Changing video mode failed!"));
381 }
382}
383
384void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event))
385{
2ad495fb 386 wxDisplay dpy(m_book->GetSelection());
84267f0d
VZ
387
388 dpy.ResetMode();
389}
390
22d679f3
VZ
391#endif // wxUSE_DISPLAY
392
e9b77bcc
VZ
393void MyFrame::OnLeftClick(wxMouseEvent& event)
394{
395 if ( HasCapture() )
396 {
397 // mouse events are in client coords, wxDisplay works in screen ones
398 const wxPoint ptScreen = ClientToScreen(event.GetPosition());
399 int dpy = wxDisplay::GetFromPoint(ptScreen);
400 if ( dpy == wxNOT_FOUND )
401 {
84267f0d 402 wxLogError(_T("Mouse clicked outside of display!?"));
e9b77bcc
VZ
403 }
404
405 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
406 dpy, ptScreen.x, ptScreen.y);
407
408 ReleaseMouse();
409 }
410}
411
2c1957be 412#if wxUSE_DISPLAY
22d679f3 413
84267f0d
VZ
414void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event)
415{
416 // update the current mode text
2ad495fb 417 for ( size_t n = 0; n < m_book->GetPageCount(); n++ )
84267f0d 418 {
2ad495fb 419 wxStaticText *label = wxDynamicCast(m_book->GetPage(n)->
84267f0d
VZ
420 FindWindow(Display_CurrentMode),
421 wxStaticText);
422 if ( label )
423 label->SetLabel(VideoModeToText(wxDisplay(n).GetCurrentMode()));
424 }
425
426
427 wxLogStatus(this, _T("Display resolution was changed."));
428
429 event.Skip();
430}
2c1957be 431
22d679f3 432#endif // wxUSE_DISPLAY