]>
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
165 if ( argc
== 2 && !wxStricmp(argv
[1], _T("/dx")) )
167 wxDisplay::UseDirectX(true);
171 // create the main application window
172 MyFrame
*frame
= new MyFrame(_("Display wxWindows Sample"),
173 wxPoint(50, 50), wxSize(450, 340));
175 // and show it (the frames, unlike simple controls, are not shown when
176 // created initially)
179 // success: wxApp::OnRun() will be called which will enter the main message
180 // loop and the application will run. If we returned FALSE here, the
181 // application would exit immediately.
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
191 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
193 // set the frame icon
194 SetIcon(wxICON(mondrian
));
198 wxMenu
*menuDisplay
= new wxMenu
;
199 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
200 menuDisplay
->AppendSeparator();
201 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
202 menuDisplay
->AppendSeparator();
203 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
205 // the "About" item should be in the help menu
206 wxMenu
*helpMenu
= new wxMenu
;
207 helpMenu
->Append(Display_About
, _("&About...\tF1"), _("Show about dialog"));
209 // now append the freshly created menu to the menu bar...
210 wxMenuBar
*menuBar
= new wxMenuBar();
211 menuBar
->Append(menuDisplay
, _("&Display"));
212 menuBar
->Append(helpMenu
, _("&Help"));
214 // ... and attach this menu bar to the frame
216 #endif // wxUSE_MENUS
221 // create child controls
222 m_notebook
= new wxNotebook(this, -1);
223 const size_t count
= wxDisplay::GetCount();
224 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
226 wxDisplay
display(nDpy
);
228 wxWindow
*page
= new wxPanel(m_notebook
, -1);
230 // create 2 column flex grid sizer with growable 2nd column
231 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
232 sizer
->AddGrowableCol(1);
234 const wxRect
r(display
.GetGeometry());
235 sizer
->Add(new wxStaticText(page
, -1, _T("Origin: ")));
236 sizer
->Add(new wxStaticText
240 wxString::Format(_T("(%d, %d)"),
244 sizer
->Add(new wxStaticText(page
, -1, _T("Size: ")));
245 sizer
->Add(new wxStaticText
249 wxString::Format(_T("(%d, %d)"),
254 sizer
->Add(new wxStaticText(page
, -1, _T("Name: ")));
255 sizer
->Add(new wxStaticText(page
, -1, display
.GetName()));
257 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
258 const wxArrayVideoModes modes
= display
.GetModes();
259 const size_t count
= modes
.GetCount();
260 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
262 const wxVideoMode
& mode
= modes
[nMode
];
264 choiceModes
->Append(VideoModeToText(mode
),
265 new MyVideoModeClientData(mode
));
268 sizer
->Add(new wxStaticText(page
, -1, _T("&Modes: ")));
269 sizer
->Add(choiceModes
, 0, wxEXPAND
);
271 sizer
->Add(new wxStaticText(page
, -1, _T("Current: ")));
272 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
273 VideoModeToText(display
.GetCurrentMode())));
275 // add it to another sizer to have borders around it and button below
276 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
277 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
278 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
279 0, wxALL
| wxCENTRE
, 5);
280 page
->SetSizer(sizerTop
);
282 m_notebook
->AddPage(page
,
283 wxString::Format(_T("Display %lu"),
284 (unsigned long)nDpy
));
288 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
291 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
295 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
300 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
308 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
310 // TRUE is to force the frame to close
314 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
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."));