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