]>
git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
7172e2ae613cba4a9f6e6f551b45030348c26d9a
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample showing the features of wxDisplay class
4 // Author: Vadim Zeitlin
5 // Modified by: Ryan Norton & Brian Victor
8 // Copyright: (c) Vadim Zeitlin <vadim@wxwidgets.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h"
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers explicitly
32 #include "wx/bookctrl.h"
33 #include "wx/sysopt.h"
35 #include "wx/display.h"
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"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp
: public wxApp
51 // override base class virtuals
52 // ----------------------------
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();
60 // Define a new frame type: this is going to be our main frame
61 class MyFrame
: public wxFrame
65 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
66 long style
= wxDEFAULT_FRAME_STYLE
);
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
);
75 void OnChangeMode(wxCommandEvent
& event
);
76 void OnResetMode(wxCommandEvent
& event
);
78 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
79 #endif // wxUSE_DISPLAY
81 void OnLeftClick(wxMouseEvent
& event
);
85 // convert video mode to textual description
86 wxString
VideoModeToText(const wxVideoMode
& mode
);
87 #endif // wxUSE_DISPLAY
92 // any class wishing to process wxWidgets events must use this macro
96 // Client data class for the choice control containing the video modes
97 class MyVideoModeClientData
: public wxClientData
100 MyVideoModeClientData(const wxVideoMode
& m
) : mode(m
) { }
102 const wxVideoMode mode
;
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // IDs for the controls and the menu commands
113 Display_FromPoint
= wxID_HIGHEST
+ 1,
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
129 // ----------------------------------------------------------------------------
130 // event tables and other macros for wxWidgets
131 // ----------------------------------------------------------------------------
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.
136 BEGIN_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
)
143 EVT_CHOICE(Display_ChangeMode
, MyFrame::OnChangeMode
)
144 EVT_BUTTON(Display_ResetMode
, MyFrame::OnResetMode
)
146 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged
)
147 #endif // wxUSE_DISPLAY
149 EVT_LEFT_UP(MyFrame::OnLeftClick
)
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
159 // ============================================================================
161 // ============================================================================
163 // ----------------------------------------------------------------------------
164 // the application class
165 // ----------------------------------------------------------------------------
167 // 'Main program' equivalent: the program execution "starts" here
170 if ( !wxApp::OnInit() )
174 if ( argc
== 2 && !wxStricmp(argv
[1], wxT("/dx")) )
176 wxSystemOptions::SetOption(wxT("msw.display.directdraw"), 1);
180 // create the main application window
181 MyFrame
*frame
= new MyFrame(_("Display wxWidgets Sample"),
182 wxDefaultPosition
, wxDefaultSize
);
184 // and show it (the frames, unlike simple controls, are not shown when
185 // created initially)
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.
194 // ----------------------------------------------------------------------------
196 // ----------------------------------------------------------------------------
199 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
200 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
202 // set the frame icon
203 SetIcon(wxICON(sample
));
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"));
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"));
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"));
223 // ... and attach this menu bar to the frame
225 #endif // wxUSE_MENUS
230 #endif // wxUSE_STATUSBAR
232 // create child controls
233 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
235 m_book
= new wxBookCtrl(panel
, wxID_ANY
);
236 const size_t count
= wxDisplay::GetCount();
237 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
239 wxDisplay
display(nDpy
);
241 wxWindow
*page
= new wxPanel(m_book
, wxID_ANY
);
243 // create 2 column flex grid sizer with growable 2nd column
244 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
245 sizer
->AddGrowableCol(1);
247 const wxRect
r(display
.GetGeometry());
248 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Origin: ")));
249 sizer
->Add(new wxStaticText
253 wxString::Format(wxT("(%d, %d)"),
257 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Size: ")));
258 sizer
->Add(new wxStaticText
262 wxString::Format(wxT("(%d, %d)"),
266 const wxRect
rc(display
.GetClientArea());
267 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Client area: ")));
268 sizer
->Add(new wxStaticText
272 wxString::Format(wxT("(%d, %d)-(%d, %d)"),
273 rc
.x
, rc
.y
, rc
.width
, rc
.height
)
276 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Name: ")));
277 sizer
->Add(new wxStaticText(page
, wxID_ANY
, display
.GetName()));
279 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
280 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
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
++ )
288 const wxVideoMode
& mode
= modes
[nMode
];
290 choiceModes
->Append(VideoModeToText(mode
),
291 new MyVideoModeClientData(mode
));
294 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("&Modes: ")));
295 sizer
->Add(choiceModes
, 0, wxEXPAND
);
297 sizer
->Add(new wxStaticText(page
, wxID_ANY
, wxT("Current: ")));
298 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
299 VideoModeToText(display
.GetCurrentMode())));
301 // add it to another sizer to have borders around it and button below
302 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, wxT("&Reset mode")),
303 0, wxALL
| wxCENTRE
, 5);
304 #endif // wxUSE_DISPLAY
306 page
->SetSizer(sizerTop
);
308 m_book
->AddPage(page
,
309 wxString::Format(wxT("Display %lu"),
310 (unsigned long)nDpy
));
313 wxBoxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
314 sizer
->Add(m_book
, 1, wxEXPAND
);
315 panel
->SetSizer(sizer
);
316 sizer
->SetSizeHints(this);
321 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
324 s
.Printf(wxT("%dx%d"), mode
.w
, mode
.h
);
328 s
+= wxString::Format(wxT(", %dbpp"), mode
.bpp
);
333 s
+= wxString::Format(wxT(", %dHz"), mode
.refresh
);
339 #endif // wxUSE_DISPLAY
343 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
345 // true is to force the frame to close
349 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
351 wxMessageBox(wxT("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
352 wxT("About Display Sample"),
353 wxOK
| wxICON_INFORMATION
,
357 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
360 SetStatusText(wxT("Press the mouse anywhere..."));
361 #endif // wxUSE_STATUSBAR
366 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
368 ShowFullScreen(event
.IsChecked());
373 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
375 wxDisplay
dpy(m_book
->GetSelection());
377 // you wouldn't write this in real code, would you?
378 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
379 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
380 GetClientObject(event
.GetInt()))->mode
) )
382 wxLogError(wxT("Changing video mode failed!"));
386 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
388 wxDisplay
dpy(m_book
->GetSelection());
393 #endif // wxUSE_DISPLAY
395 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
399 // mouse events are in client coords, wxDisplay works in screen ones
400 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
401 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
402 if ( dpy
== wxNOT_FOUND
)
404 wxLogError(wxT("Mouse clicked outside of display!?"));
407 wxLogStatus(this, wxT("Mouse clicked in display %d (at (%d, %d))"),
408 dpy
, ptScreen
.x
, ptScreen
.y
);
416 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
418 // update the current mode text
419 for ( size_t n
= 0; n
< m_book
->GetPageCount(); n
++ )
421 wxStaticText
*label
= wxDynamicCast(m_book
->GetPage(n
)->
422 FindWindow(Display_CurrentMode
),
425 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
429 wxLogStatus(this, wxT("Display resolution was changed."));
434 #endif // wxUSE_DISPLAY