]> git.saurik.com Git - wxWidgets.git/blame - samples/display/display.cpp
Fix horizontal mouse wheel scrolling in wxGTK.
[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 6// Created: 23.02.03
be5a51fb 7// Copyright: (c) Vadim Zeitlin <vadim@wxwidgets.org>
c32b1077
VZ
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
6ae4d29a 19// for compilers that support precompilation, includes "wx/wx.h"
c32b1077
VZ
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26// for all others, include the necessary headers explicitly
27#ifndef WX_PRECOMP
6ae4d29a
DS
28 #include "wx/wx.h"
29#endif
30
2ad495fb 31#include "wx/bookctrl.h"
22d679f3 32#include "wx/sysopt.h"
c32b1077
VZ
33
34#include "wx/display.h"
35
6ae4d29a 36
63c51a93 37// the application icon (under Windows and OS/2 it is in resources)
e7092398 38#ifndef wxHAS_IMAGES_IN_RESOURCES
63c51a93
RN
39 #include "../sample.xpm"
40#endif
41
c32b1077
VZ
42// ----------------------------------------------------------------------------
43// private classes
44// ----------------------------------------------------------------------------
45
46// Define a new application type, each program should derive a class from wxApp
47class MyApp : public wxApp
48{
49public:
50 // override base class virtuals
51 // ----------------------------
52
53 // this one is called on application startup and is a good place for the app
54 // initialization (doing it here and not in the ctor allows to have an error
55 // return: if OnInit() returns false, the application terminates)
56 virtual bool OnInit();
57};
58
59// Define a new frame type: this is going to be our main frame
60class MyFrame : public wxFrame
61{
62public:
63 // ctor(s)
64 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
65 long style = wxDEFAULT_FRAME_STYLE);
66
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent& event);
e9b77bcc 69 void OnFromPoint(wxCommandEvent& event);
84267f0d 70 void OnFullScreen(wxCommandEvent& event);
c32b1077
VZ
71 void OnAbout(wxCommandEvent& event);
72
22d679f3 73#if wxUSE_DISPLAY
84267f0d
VZ
74 void OnChangeMode(wxCommandEvent& event);
75 void OnResetMode(wxCommandEvent& event);
76
84267f0d 77 void OnDisplayChanged(wxDisplayChangedEvent& event);
22d679f3
VZ
78#endif // wxUSE_DISPLAY
79
80 void OnLeftClick(wxMouseEvent& event);
84267f0d 81
c32b1077 82private:
2c1957be 83#if wxUSE_DISPLAY
84267f0d
VZ
84 // convert video mode to textual description
85 wxString VideoModeToText(const wxVideoMode& mode);
22d679f3 86#endif // wxUSE_DISPLAY
84267f0d
VZ
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
22d679f3 141#if wxUSE_DISPLAY
84267f0d
VZ
142 EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode)
143 EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode)
144
84267f0d 145 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged)
22d679f3
VZ
146#endif // wxUSE_DISPLAY
147
148 EVT_LEFT_UP(MyFrame::OnLeftClick)
c32b1077
VZ
149END_EVENT_TABLE()
150
be5a51fb 151// Create a new application object: this macro will allow wxWidgets to create
c32b1077
VZ
152// the application object during program execution (it's better than using a
153// static object for many reasons) and also declares the accessor function
154// wxGetApp() which will return the reference of the right type (i.e. MyApp and
155// not wxApp)
156IMPLEMENT_APP(MyApp)
157
158// ============================================================================
159// implementation
160// ============================================================================
161
162// ----------------------------------------------------------------------------
163// the application class
164// ----------------------------------------------------------------------------
165
166// 'Main program' equivalent: the program execution "starts" here
167bool MyApp::OnInit()
168{
45e6e6f8
VZ
169 if ( !wxApp::OnInit() )
170 return false;
171
e40bef9f 172#ifdef __WXMSW__
9a83f860 173 if ( argc == 2 && !wxStricmp(argv[1], wxT("/dx")) )
e40bef9f 174 {
9a83f860 175 wxSystemOptions::SetOption(wxT("msw.display.directdraw"), 1);
e40bef9f
VZ
176 }
177#endif // __WXMSW__
178
c32b1077 179 // create the main application window
be5a51fb 180 MyFrame *frame = new MyFrame(_("Display wxWidgets Sample"),
6ae4d29a 181 wxDefaultPosition, wxDefaultSize);
c32b1077
VZ
182
183 // and show it (the frames, unlike simple controls, are not shown when
184 // created initially)
84267f0d 185 frame->Show();
c32b1077
VZ
186
187 // success: wxApp::OnRun() will be called which will enter the main message
6ae4d29a 188 // loop and the application will run. If we returned false here, the
c32b1077 189 // application would exit immediately.
6ae4d29a 190 return true;
c32b1077
VZ
191}
192
193// ----------------------------------------------------------------------------
194// main frame
195// ----------------------------------------------------------------------------
196
197// frame constructor
198MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
6ae4d29a 199 : wxFrame(NULL, wxID_ANY, title, pos, size, style)
c32b1077
VZ
200{
201 // set the frame icon
63c51a93 202 SetIcon(wxICON(sample));
c32b1077
VZ
203
204#if wxUSE_MENUS
205 // create a menu bar
e9b77bcc 206 wxMenu *menuDisplay = new wxMenu;
84267f0d
VZ
207 menuDisplay->Append(Display_FromPoint, _("Find from &point..."));
208 menuDisplay->AppendSeparator();
209 menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12"));
e9b77bcc
VZ
210 menuDisplay->AppendSeparator();
211 menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program"));
c32b1077
VZ
212
213 // the "About" item should be in the help menu
214 wxMenu *helpMenu = new wxMenu;
2d143b66 215 helpMenu->Append(Display_About, _("&About\tF1"), _("Show about dialog"));
c32b1077 216
c32b1077
VZ
217 // now append the freshly created menu to the menu bar...
218 wxMenuBar *menuBar = new wxMenuBar();
e9b77bcc 219 menuBar->Append(menuDisplay, _("&Display"));
c32b1077
VZ
220 menuBar->Append(helpMenu, _("&Help"));
221
222 // ... and attach this menu bar to the frame
223 SetMenuBar(menuBar);
224#endif // wxUSE_MENUS
225
8520f137 226#if wxUSE_STATUSBAR
e9b77bcc
VZ
227 // create status bar
228 CreateStatusBar();
8520f137 229#endif // wxUSE_STATUSBAR
e9b77bcc 230
c32b1077 231 // create child controls
6ae4d29a
DS
232 wxPanel *panel = new wxPanel(this, wxID_ANY);
233
2ad495fb 234 m_book = new wxBookCtrl(panel, wxID_ANY);
c32b1077 235 const size_t count = wxDisplay::GetCount();
84267f0d 236 for ( size_t nDpy = 0; nDpy < count; nDpy++ )
c32b1077 237 {
84267f0d 238 wxDisplay display(nDpy);
c32b1077 239
2ad495fb 240 wxWindow *page = new wxPanel(m_book, wxID_ANY);
c32b1077
VZ
241
242 // create 2 column flex grid sizer with growable 2nd column
243 wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20);
244 sizer->AddGrowableCol(1);
245
246 const wxRect r(display.GetGeometry());
9a83f860 247 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Origin: ")));
e9b77bcc
VZ
248 sizer->Add(new wxStaticText
249 (
250 page,
6ae4d29a 251 wxID_ANY,
9a83f860 252 wxString::Format(wxT("(%d, %d)"),
e9b77bcc
VZ
253 r.x, r.y)
254 ));
255
9a83f860 256 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Size: ")));
c32b1077
VZ
257 sizer->Add(new wxStaticText
258 (
259 page,
6ae4d29a 260 wxID_ANY,
9a83f860 261 wxString::Format(wxT("(%d, %d)"),
e9b77bcc 262 r.width, r.height)
c32b1077
VZ
263 ));
264
6c5d6291 265 const wxRect rc(display.GetClientArea());
9a83f860 266 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Client area: ")));
6c5d6291
VZ
267 sizer->Add(new wxStaticText
268 (
269 page,
270 wxID_ANY,
9a83f860 271 wxString::Format(wxT("(%d, %d)-(%d, %d)"),
6c5d6291
VZ
272 rc.x, rc.y, rc.width, rc.height)
273 ));
c32b1077 274
9a83f860 275 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Name: ")));
6ae4d29a 276 sizer->Add(new wxStaticText(page, wxID_ANY, display.GetName()));
e9b77bcc 277
22d679f3
VZ
278 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
279 sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10);
280
281#if wxUSE_DISPLAY
84267f0d
VZ
282 wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode);
283 const wxArrayVideoModes modes = display.GetModes();
284 const size_t count = modes.GetCount();
285 for ( size_t nMode = 0; nMode < count; nMode++ )
286 {
287 const wxVideoMode& mode = modes[nMode];
288
289 choiceModes->Append(VideoModeToText(mode),
290 new MyVideoModeClientData(mode));
291 }
292
9a83f860 293 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("&Modes: ")));
84267f0d 294 sizer->Add(choiceModes, 0, wxEXPAND);
c32b1077 295
9a83f860 296 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Current: ")));
84267f0d
VZ
297 sizer->Add(new wxStaticText(page, Display_CurrentMode,
298 VideoModeToText(display.GetCurrentMode())));
299
300 // add it to another sizer to have borders around it and button below
9a83f860 301 sizerTop->Add(new wxButton(page, Display_ResetMode, wxT("&Reset mode")),
84267f0d 302 0, wxALL | wxCENTRE, 5);
22d679f3
VZ
303#endif // wxUSE_DISPLAY
304
c32b1077
VZ
305 page->SetSizer(sizerTop);
306
2ad495fb 307 m_book->AddPage(page,
9a83f860 308 wxString::Format(wxT("Display %lu"),
2ad495fb 309 (unsigned long)nDpy));
c32b1077 310 }
6ae4d29a 311
adbf2d73 312 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
2ad495fb 313 sizer->Add(m_book, 1, wxEXPAND);
adbf2d73 314 panel->SetSizer(sizer);
adbf2d73 315 sizer->SetSizeHints(this);
c32b1077
VZ
316}
317
2c1957be 318#if wxUSE_DISPLAY
22d679f3 319
84267f0d
VZ
320wxString MyFrame::VideoModeToText(const wxVideoMode& mode)
321{
322 wxString s;
9a83f860 323 s.Printf(wxT("%dx%d"), mode.w, mode.h);
84267f0d
VZ
324
325 if ( mode.bpp )
326 {
9a83f860 327 s += wxString::Format(wxT(", %dbpp"), mode.bpp);
84267f0d
VZ
328 }
329
330 if ( mode.refresh )
331 {
9a83f860 332 s += wxString::Format(wxT(", %dHz"), mode.refresh);
84267f0d
VZ
333 }
334
335 return s;
336}
22d679f3
VZ
337
338#endif // wxUSE_DISPLAY
c32b1077
VZ
339
340// event handlers
341
342void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
343{
6ae4d29a
DS
344 // true is to force the frame to close
345 Close(true);
c32b1077
VZ
346}
347
348void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
349{
9a83f860
VZ
350 wxMessageBox(wxT("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
351 wxT("About Display Sample"),
c32b1077
VZ
352 wxOK | wxICON_INFORMATION,
353 this);
354}
e9b77bcc
VZ
355
356void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event))
357{
8520f137 358#if wxUSE_STATUSBAR
9a83f860 359 SetStatusText(wxT("Press the mouse anywhere..."));
8520f137 360#endif // wxUSE_STATUSBAR
e9b77bcc
VZ
361
362 CaptureMouse();
363}
364
84267f0d
VZ
365void MyFrame::OnFullScreen(wxCommandEvent& event)
366{
367 ShowFullScreen(event.IsChecked());
368}
369
22d679f3
VZ
370#if wxUSE_DISPLAY
371
84267f0d
VZ
372void MyFrame::OnChangeMode(wxCommandEvent& event)
373{
2ad495fb 374 wxDisplay dpy(m_book->GetSelection());
84267f0d
VZ
375
376 // you wouldn't write this in real code, would you?
377 if ( !dpy.ChangeMode(((MyVideoModeClientData *)
378 wxDynamicCast(event.GetEventObject(), wxChoice)->
379 GetClientObject(event.GetInt()))->mode) )
380 {
9a83f860 381 wxLogError(wxT("Changing video mode failed!"));
84267f0d
VZ
382 }
383}
384
385void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event))
386{
2ad495fb 387 wxDisplay dpy(m_book->GetSelection());
84267f0d
VZ
388
389 dpy.ResetMode();
390}
391
22d679f3
VZ
392#endif // wxUSE_DISPLAY
393
e9b77bcc
VZ
394void MyFrame::OnLeftClick(wxMouseEvent& event)
395{
396 if ( HasCapture() )
397 {
398 // mouse events are in client coords, wxDisplay works in screen ones
399 const wxPoint ptScreen = ClientToScreen(event.GetPosition());
400 int dpy = wxDisplay::GetFromPoint(ptScreen);
401 if ( dpy == wxNOT_FOUND )
402 {
9a83f860 403 wxLogError(wxT("Mouse clicked outside of display!?"));
e9b77bcc
VZ
404 }
405
9a83f860 406 wxLogStatus(this, wxT("Mouse clicked in display %d (at (%d, %d))"),
e9b77bcc
VZ
407 dpy, ptScreen.x, ptScreen.y);
408
409 ReleaseMouse();
410 }
411}
412
2c1957be 413#if wxUSE_DISPLAY
22d679f3 414
84267f0d
VZ
415void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event)
416{
417 // update the current mode text
2ad495fb 418 for ( size_t n = 0; n < m_book->GetPageCount(); n++ )
84267f0d 419 {
2ad495fb 420 wxStaticText *label = wxDynamicCast(m_book->GetPage(n)->
84267f0d
VZ
421 FindWindow(Display_CurrentMode),
422 wxStaticText);
423 if ( label )
424 label->SetLabel(VideoModeToText(wxDisplay(n).GetCurrentMode()));
425 }
426
427
9a83f860 428 wxLogStatus(this, wxT("Display resolution was changed."));
84267f0d
VZ
429
430 event.Skip();
431}
2c1957be 432
22d679f3 433#endif // wxUSE_DISPLAY