]>
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
5 // Modified by: Ryan Norton & Brian Victor
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"
28 #error "To compile this sample you must build the library with wxUSE_DISPLAY set to 1"
31 // for all others, include the necessary headers explicitly
34 #include "wx/stattext.h"
36 #include "wx/layout.h"
40 #include "wx/choice.h"
41 #include "wx/msgdlg.h"
44 #include "wx/button.h"
47 #include "wx/notebook.h"
49 #include "wx/display.h"
51 // the application icon (under Windows and OS/2 it is in resources)
52 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
53 #include "../sample.xpm"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // Define a new application type, each program should derive a class from wxApp
61 class MyApp
: public wxApp
64 // override base class virtuals
65 // ----------------------------
67 // this one is called on application startup and is a good place for the app
68 // initialization (doing it here and not in the ctor allows to have an error
69 // return: if OnInit() returns false, the application terminates)
70 virtual bool OnInit();
73 // Define a new frame type: this is going to be our main frame
74 class MyFrame
: public wxFrame
78 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
79 long style
= wxDEFAULT_FRAME_STYLE
);
81 // event handlers (these functions should _not_ be virtual)
82 void OnQuit(wxCommandEvent
& event
);
83 void OnFromPoint(wxCommandEvent
& event
);
84 void OnFullScreen(wxCommandEvent
& event
);
85 void OnAbout(wxCommandEvent
& event
);
87 void OnChangeMode(wxCommandEvent
& event
);
88 void OnResetMode(wxCommandEvent
& event
);
90 void OnLeftClick(wxMouseEvent
& event
);
92 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
95 // convert video mode to textual description
96 wxString
VideoModeToText(const wxVideoMode
& mode
);
99 wxNotebook
*m_notebook
;
101 // any class wishing to process wxWindows events must use this macro
102 DECLARE_EVENT_TABLE()
105 // Client data class for the choice control containing the video modes
106 class MyVideoModeClientData
: public wxClientData
109 MyVideoModeClientData(const wxVideoMode
& m
) : mode(m
) { }
111 const wxVideoMode mode
;
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // IDs for the controls and the menu commands
128 Display_ChangeMode
= 1000,
133 // it is important for the id corresponding to the "About" command to have
134 // this standard value as otherwise it won't be handled properly under Mac
135 // (where it is special and put into the "Apple" menu)
136 Display_About
= wxID_ABOUT
139 // ----------------------------------------------------------------------------
140 // event tables and other macros for wxWindows
141 // ----------------------------------------------------------------------------
143 // the event tables connect the wxWindows events with the functions (event
144 // handlers) which process them. It can be also done at run-time, but for the
145 // simple menu events like this the static method is much simpler.
146 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
147 EVT_MENU(Display_Quit
, MyFrame::OnQuit
)
148 EVT_MENU(Display_FromPoint
, MyFrame::OnFromPoint
)
149 EVT_MENU(Display_FullScreen
, MyFrame::OnFullScreen
)
150 EVT_MENU(Display_About
, MyFrame::OnAbout
)
152 EVT_CHOICE(Display_ChangeMode
, MyFrame::OnChangeMode
)
153 EVT_BUTTON(Display_ResetMode
, MyFrame::OnResetMode
)
155 EVT_LEFT_UP(MyFrame::OnLeftClick
)
158 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged
)
161 // Create a new application object: this macro will allow wxWindows to create
162 // the application object during program execution (it's better than using a
163 // static object for many reasons) and also declares the accessor function
164 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
168 // ============================================================================
170 // ============================================================================
172 // ----------------------------------------------------------------------------
173 // the application class
174 // ----------------------------------------------------------------------------
176 // 'Main program' equivalent: the program execution "starts" here
180 if ( argc
== 2 && !wxStricmp(argv
[1], _T("/dx")) )
182 wxDisplay::UseDirectX(true);
186 // create the main application window
187 MyFrame
*frame
= new MyFrame(_("Display wxWindows Sample"),
188 wxPoint(50, 50), wxSize(450, 340));
190 // and show it (the frames, unlike simple controls, are not shown when
191 // created initially)
194 // success: wxApp::OnRun() will be called which will enter the main message
195 // loop and the application will run. If we returned FALSE here, the
196 // application would exit immediately.
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
205 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
206 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
208 // set the frame icon
209 SetIcon(wxICON(sample
));
213 wxMenu
*menuDisplay
= new wxMenu
;
214 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
215 menuDisplay
->AppendSeparator();
216 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
217 menuDisplay
->AppendSeparator();
218 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
220 // the "About" item should be in the help menu
221 wxMenu
*helpMenu
= new wxMenu
;
222 helpMenu
->Append(Display_About
, _("&About...\tF1"), _("Show about dialog"));
224 // now append the freshly created menu to the menu bar...
225 wxMenuBar
*menuBar
= new wxMenuBar();
226 menuBar
->Append(menuDisplay
, _("&Display"));
227 menuBar
->Append(helpMenu
, _("&Help"));
229 // ... and attach this menu bar to the frame
231 #endif // wxUSE_MENUS
236 // create child controls
237 m_notebook
= new wxNotebook(this, -1);
238 const size_t count
= wxDisplay::GetCount();
239 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
241 wxDisplay
display(nDpy
);
243 wxWindow
*page
= new wxPanel(m_notebook
, -1);
245 // create 2 column flex grid sizer with growable 2nd column
246 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
247 sizer
->AddGrowableCol(1);
249 const wxRect
r(display
.GetGeometry());
250 sizer
->Add(new wxStaticText(page
, -1, _T("Origin: ")));
251 sizer
->Add(new wxStaticText
255 wxString::Format(_T("(%d, %d)"),
259 sizer
->Add(new wxStaticText(page
, -1, _T("Size: ")));
260 sizer
->Add(new wxStaticText
264 wxString::Format(_T("(%d, %d)"),
269 sizer
->Add(new wxStaticText(page
, -1, _T("Name: ")));
270 sizer
->Add(new wxStaticText(page
, -1, display
.GetName()));
272 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
273 const wxArrayVideoModes modes
= display
.GetModes();
274 const size_t count
= modes
.GetCount();
275 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
277 const wxVideoMode
& mode
= modes
[nMode
];
279 choiceModes
->Append(VideoModeToText(mode
),
280 new MyVideoModeClientData(mode
));
283 sizer
->Add(new wxStaticText(page
, -1, _T("&Modes: ")));
284 sizer
->Add(choiceModes
, 0, wxEXPAND
);
286 sizer
->Add(new wxStaticText(page
, -1, _T("Current: ")));
287 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
288 VideoModeToText(display
.GetCurrentMode())));
290 // add it to another sizer to have borders around it and button below
291 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
292 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
293 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
294 0, wxALL
| wxCENTRE
, 5);
295 page
->SetSizer(sizerTop
);
297 m_notebook
->AddPage(page
,
298 wxString::Format(_T("Display %lu"),
299 (unsigned long)nDpy
));
303 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
306 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
310 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
315 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
323 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
325 // TRUE is to force the frame to close
329 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
331 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
332 _T("About Display Sample"),
333 wxOK
| wxICON_INFORMATION
,
337 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
339 SetStatusText(_T("Press the mouse anywhere..."));
344 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
346 ShowFullScreen(event
.IsChecked());
349 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
351 wxDisplay
dpy(m_notebook
->GetSelection());
353 // you wouldn't write this in real code, would you?
354 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
355 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
356 GetClientObject(event
.GetInt()))->mode
) )
358 wxLogError(_T("Changing video mode failed!"));
362 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
364 wxDisplay
dpy(m_notebook
->GetSelection());
369 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
373 // mouse events are in client coords, wxDisplay works in screen ones
374 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
375 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
376 if ( dpy
== wxNOT_FOUND
)
378 wxLogError(_T("Mouse clicked outside of display!?"));
381 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
382 dpy
, ptScreen
.x
, ptScreen
.y
);
388 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
390 // update the current mode text
391 for ( int n
= 0; n
< m_notebook
->GetPageCount(); n
++ )
393 wxStaticText
*label
= wxDynamicCast(m_notebook
->GetPage(n
)->
394 FindWindow(Display_CurrentMode
),
397 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
401 wxLogStatus(this, _T("Display resolution was changed."));