]>
git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample showing the features of wxDisplay class
4 // Author: Vadim Zeitlin
5 // Modified by: Ryan Norton & Brian Victor
7 // Copyright: (c) Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h"
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers explicitly
31 #include "wx/bookctrl.h"
32 #include "wx/sysopt.h"
34 #include "wx/display.h"
37 // the application icon (under Windows and OS/2 it is in resources)
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // Define a new application type, each program should derive a class from wxApp
47 class MyApp
: public wxApp
50 // override base class virtuals
51 // ----------------------------
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();
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame
: public wxFrame
64 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
65 long style
= wxDEFAULT_FRAME_STYLE
);
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent
& event
);
69 void OnFromPoint(wxCommandEvent
& event
);
70 void OnFullScreen(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
74 void OnChangeMode(wxCommandEvent
& event
);
75 void OnResetMode(wxCommandEvent
& event
);
77 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
78 #endif // wxUSE_DISPLAY
80 void OnLeftClick(wxMouseEvent
& event
);
84 // convert video mode to textual description
85 wxString
VideoModeToText(const wxVideoMode
& mode
);
86 #endif // wxUSE_DISPLAY
91 // any class wishing to process wxWidgets events must use this macro
95 // Client data class for the choice control containing the video modes
96 class MyVideoModeClientData
: public wxClientData
99 MyVideoModeClientData(const wxVideoMode
& m
) : mode(m
) { }
101 const wxVideoMode mode
;
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // IDs for the controls and the menu commands
112 Display_FromPoint
= wxID_HIGHEST
+ 1,
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)
124 Display_Quit
= wxID_EXIT
,
125 Display_About
= wxID_ABOUT
128 // ----------------------------------------------------------------------------
129 // event tables and other macros for wxWidgets
130 // ----------------------------------------------------------------------------
132 // the event tables connect the wxWidgets events with the functions (event
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.
135 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
136 EVT_MENU(Display_Quit
, MyFrame::OnQuit
)
137 EVT_MENU(Display_FromPoint
, MyFrame::OnFromPoint
)
138 EVT_MENU(Display_FullScreen
, MyFrame::OnFullScreen
)
139 EVT_MENU(Display_About
, MyFrame::OnAbout
)
142 EVT_CHOICE(Display_ChangeMode
, MyFrame::OnChangeMode
)
143 EVT_BUTTON(Display_ResetMode
, MyFrame::OnResetMode
)
145 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged
)
146 #endif // wxUSE_DISPLAY
148 EVT_LEFT_UP(MyFrame::OnLeftClick
)
151 // Create a new application object: this macro will allow wxWidgets to create
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
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
163 // the application class
164 // ----------------------------------------------------------------------------
166 // 'Main program' equivalent: the program execution "starts" here
169 if ( !wxApp::OnInit() )
173 if ( argc
== 2 && !wxStricmp(argv
[1], wxT("/dx")) )
175 wxSystemOptions::SetOption(wxT("msw.display.directdraw"), 1);
179 // create the main application window
180 MyFrame
*frame
= new MyFrame(_("Display wxWidgets Sample"),
181 wxDefaultPosition
, wxDefaultSize
);
183 // and show it (the frames, unlike simple controls, are not shown when
184 // created initially)
187 // success: wxApp::OnRun() will be called which will enter the main message
188 // loop and the application will run. If we returned false here, the
189 // application would exit immediately.
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
198 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
199 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
201 // set the frame icon
202 SetIcon(wxICON(sample
));
206 wxMenu
*menuDisplay
= new wxMenu
;
207 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
208 menuDisplay
->AppendSeparator();
209 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
210 menuDisplay
->AppendSeparator();
211 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
213 // the "About" item should be in the help menu
214 wxMenu
*helpMenu
= new wxMenu
;
215 helpMenu
->Append(Display_About
, _("&About\tF1"), _("Show about dialog"));
217 // now append the freshly created menu to the menu bar...
218 wxMenuBar
*menuBar
= new wxMenuBar();
219 menuBar
->Append(menuDisplay
, _("&Display"));
220 menuBar
->Append(helpMenu
, _("&Help"));
222 // ... and attach this menu bar to the frame
224 #endif // wxUSE_MENUS
229 #endif // wxUSE_STATUSBAR
231 // create child controls
232 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
234 m_book
= new wxBookCtrl(panel
, wxID_ANY
);
235 const size_t count
= wxDisplay::GetCount();
236 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
238 wxDisplay
display(nDpy
);
240 wxWindow
*page
= new wxPanel(m_book
, wxID_ANY
);
242 // create 2 column flex grid sizer with growable 2nd column
243 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
244 sizer
->AddGrowableCol(1);
246 const wxRect
r(display
.GetGeometry());
247 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Origin: ")));
248 sizer
->Add(new wxStaticText
252 wxString::Format(wxT("(%d, %d)"),
256 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Size: ")));
257 sizer
->Add(new wxStaticText
261 wxString::Format(wxT("(%d, %d)"),
265 const wxRect
rc(display
.GetClientArea());
266 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Client area: ")));
267 sizer
->Add(new wxStaticText
271 wxString::Format(wxT("(%d, %d)-(%d, %d)"),
272 rc
.x
, rc
.y
, rc
.width
, rc
.height
)
275 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Name: ")));
276 sizer
->Add(new wxStaticText(page
, wxID_ANY
, display
.GetName()));
278 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
279 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
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
++ )
287 const wxVideoMode
& mode
= modes
[nMode
];
289 choiceModes
->Append(VideoModeToText(mode
),
290 new MyVideoModeClientData(mode
));
293 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("&Modes: ")));
294 sizer
->Add(choiceModes
, 0, wxEXPAND
);
296 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Current: ")));
297 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
298 VideoModeToText(display
.GetCurrentMode())));
300 // add it to another sizer to have borders around it and button below
301 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, wxT("&Reset mode")),
302 0, wxALL
| wxCENTRE
, 5);
303 #endif // wxUSE_DISPLAY
305 page
->SetSizer(sizerTop
);
307 m_book
->AddPage(page
,
308 wxString::Format(wxT("Display %lu"),
309 (unsigned long)nDpy
));
312 wxBoxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
313 sizer
->Add(m_book
, 1, wxEXPAND
);
314 panel
->SetSizer(sizer
);
315 sizer
->SetSizeHints(this);
320 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
323 s
.Printf(wxT("%dx%d"), mode
.w
, mode
.h
);
327 s
+= wxString::Format(wxT(", %dbpp"), mode
.bpp
);
332 s
+= wxString::Format(wxT(", %dHz"), mode
.refresh
);
338 #endif // wxUSE_DISPLAY
342 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
344 // true is to force the frame to close
348 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
350 wxMessageBox(wxT("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
351 wxT("About Display Sample"),
352 wxOK
| wxICON_INFORMATION
,
356 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
359 SetStatusText(wxT("Press the mouse anywhere..."));
360 #endif // wxUSE_STATUSBAR
365 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
367 ShowFullScreen(event
.IsChecked());
372 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
374 wxDisplay
dpy(m_book
->GetSelection());
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
) )
381 wxLogError(wxT("Changing video mode failed!"));
385 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
387 wxDisplay
dpy(m_book
->GetSelection());
392 #endif // wxUSE_DISPLAY
394 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
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
)
403 wxLogError(wxT("Mouse clicked outside of display!?"));
406 wxLogStatus(this, wxT("Mouse clicked in display %d (at (%d, %d))"),
407 dpy
, ptScreen
.x
, ptScreen
.y
);
415 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
417 // update the current mode text
418 for ( size_t n
= 0; n
< m_book
->GetPageCount(); n
++ )
420 wxStaticText
*label
= wxDynamicCast(m_book
->GetPage(n
)->
421 FindWindow(Display_CurrentMode
),
424 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
428 wxLogStatus(this, wxT("Display resolution was changed."));
433 #endif // wxUSE_DISPLAY