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