]>
git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows sample showing the features of wxDisplay class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".from here
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers explicitly
32 #include "wx/stattext.h"
34 #include "wx/layout.h"
37 #include "wx/notebook.h"
39 #include "wx/display.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // Define a new application type, each program should derive a class from wxApp
46 class MyApp
: public wxApp
49 // override base class virtuals
50 // ----------------------------
52 // this one is called on application startup and is a good place for the app
53 // initialization (doing it here and not in the ctor allows to have an error
54 // return: if OnInit() returns false, the application terminates)
55 virtual bool OnInit();
58 // Define a new frame type: this is going to be our main frame
59 class MyFrame
: public wxFrame
63 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
64 long style
= wxDEFAULT_FRAME_STYLE
);
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent
& event
);
68 void OnFromPoint(wxCommandEvent
& event
);
69 void OnFullScreen(wxCommandEvent
& event
);
70 void OnAbout(wxCommandEvent
& event
);
72 void OnChangeMode(wxCommandEvent
& event
);
73 void OnResetMode(wxCommandEvent
& event
);
75 void OnLeftClick(wxMouseEvent
& event
);
77 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
80 // convert video mode to textual description
81 wxString
VideoModeToText(const wxVideoMode
& mode
);
84 wxNotebook
*m_notebook
;
86 // any class wishing to process wxWindows events must use this macro
90 // Client data class for the choice control containing the video modes
91 class MyVideoModeClientData
: public wxClientData
94 MyVideoModeClientData(const wxVideoMode
& m
) : mode(m
) { }
96 const wxVideoMode mode
;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // IDs for the controls and the menu commands
113 Display_ChangeMode
= 1000,
118 // it is important for the id corresponding to the "About" command to have
119 // this standard value as otherwise it won't be handled properly under Mac
120 // (where it is special and put into the "Apple" menu)
121 Display_About
= wxID_ABOUT
124 // ----------------------------------------------------------------------------
125 // event tables and other macros for wxWindows
126 // ----------------------------------------------------------------------------
128 // the event tables connect the wxWindows events with the functions (event
129 // handlers) which process them. It can be also done at run-time, but for the
130 // simple menu events like this the static method is much simpler.
131 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
132 EVT_MENU(Display_Quit
, MyFrame::OnQuit
)
133 EVT_MENU(Display_FromPoint
, MyFrame::OnFromPoint
)
134 EVT_MENU(Display_FullScreen
, MyFrame::OnFullScreen
)
135 EVT_MENU(Display_About
, MyFrame::OnAbout
)
137 EVT_CHOICE(Display_ChangeMode
, MyFrame::OnChangeMode
)
138 EVT_BUTTON(Display_ResetMode
, MyFrame::OnResetMode
)
140 EVT_LEFT_UP(MyFrame::OnLeftClick
)
143 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged
)
146 // Create a new application object: this macro will allow wxWindows to create
147 // the application object during program execution (it's better than using a
148 // static object for many reasons) and also declares the accessor function
149 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
158 // the application class
159 // ----------------------------------------------------------------------------
161 // 'Main program' equivalent: the program execution "starts" here
164 // create the main application window
165 MyFrame
*frame
= new MyFrame(_("Display wxWindows Sample"),
166 wxPoint(50, 50), wxSize(450, 340));
168 // and show it (the frames, unlike simple controls, are not shown when
169 // created initially)
172 // success: wxApp::OnRun() will be called which will enter the main message
173 // loop and the application will run. If we returned FALSE here, the
174 // application would exit immediately.
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
183 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
184 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
186 // set the frame icon
187 SetIcon(wxICON(mondrian
));
191 wxMenu
*menuDisplay
= new wxMenu
;
192 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
193 menuDisplay
->AppendSeparator();
194 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
195 menuDisplay
->AppendSeparator();
196 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
198 // the "About" item should be in the help menu
199 wxMenu
*helpMenu
= new wxMenu
;
200 helpMenu
->Append(Display_About
, _("&About...\tF1"), _("Show about dialog"));
202 // now append the freshly created menu to the menu bar...
203 wxMenuBar
*menuBar
= new wxMenuBar();
204 menuBar
->Append(menuDisplay
, _("&Display"));
205 menuBar
->Append(helpMenu
, _("&Help"));
207 // ... and attach this menu bar to the frame
209 #endif // wxUSE_MENUS
214 // create child controls
215 m_notebook
= new wxNotebook(this, -1);
216 const size_t count
= wxDisplay::GetCount();
217 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
219 wxDisplay
display(nDpy
);
221 wxWindow
*page
= new wxPanel(m_notebook
, -1);
223 // create 2 column flex grid sizer with growable 2nd column
224 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
225 sizer
->AddGrowableCol(1);
227 const wxRect
r(display
.GetGeometry());
228 sizer
->Add(new wxStaticText(page
, -1, _T("Origin: ")));
229 sizer
->Add(new wxStaticText
233 wxString::Format(_T("(%d, %d)"),
237 sizer
->Add(new wxStaticText(page
, -1, _T("Size: ")));
238 sizer
->Add(new wxStaticText
242 wxString::Format(_T("(%d, %d)"),
247 sizer
->Add(new wxStaticText(page
, -1, _T("Name: ")));
248 sizer
->Add(new wxStaticText(page
, -1, display
.GetName()));
250 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
251 const wxArrayVideoModes modes
= display
.GetModes();
252 const size_t count
= modes
.GetCount();
253 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
255 const wxVideoMode
& mode
= modes
[nMode
];
257 choiceModes
->Append(VideoModeToText(mode
),
258 new MyVideoModeClientData(mode
));
261 sizer
->Add(new wxStaticText(page
, -1, _T("&Modes: ")));
262 sizer
->Add(choiceModes
, 0, wxEXPAND
);
264 sizer
->Add(new wxStaticText(page
, -1, _T("Current: ")));
265 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
266 VideoModeToText(display
.GetCurrentMode())));
268 // add it to another sizer to have borders around it and button below
269 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
270 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
271 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
272 0, wxALL
| wxCENTRE
, 5);
273 page
->SetSizer(sizerTop
);
275 m_notebook
->AddPage(page
,
276 wxString::Format(_T("Display %lu"),
277 (unsigned long)nDpy
));
281 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
284 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
288 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
293 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
301 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
303 // TRUE is to force the frame to close
307 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
310 if ( !dpy
.ChangeMode(wxVideoMode(800, 600)) )
312 wxLogError("Failed!");
316 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
317 _T("About Display Sample"),
318 wxOK
| wxICON_INFORMATION
,
322 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
324 SetStatusText(_T("Press the mouse anywhere..."));
329 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
331 ShowFullScreen(event
.IsChecked());
334 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
336 wxDisplay
dpy(m_notebook
->GetSelection());
338 // you wouldn't write this in real code, would you?
339 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
340 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
341 GetClientObject(event
.GetInt()))->mode
) )
343 wxLogError(_T("Changing video mode failed!"));
347 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
349 wxDisplay
dpy(m_notebook
->GetSelection());
354 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
358 // mouse events are in client coords, wxDisplay works in screen ones
359 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
360 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
361 if ( dpy
== wxNOT_FOUND
)
363 wxLogError(_T("Mouse clicked outside of display!?"));
366 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
367 dpy
, ptScreen
.x
, ptScreen
.y
);
373 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
375 // update the current mode text
376 for ( int n
= 0; n
< m_notebook
->GetPageCount(); n
++ )
378 wxStaticText
*label
= wxDynamicCast(m_notebook
->GetPage(n
)->
379 FindWindow(Display_CurrentMode
),
382 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
386 wxLogStatus(this, _T("Display resolution was changed."));