]>
git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
603b14ae9b4fcf1c966e1701cbbd8ec8bf4df7cb
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
33 #error "To compile this sample you must build the library with wxUSE_DISPLAY set to 1"
36 #include "wx/bookctrl.h"
38 #include "wx/display.h"
41 // the application icon (under Windows and OS/2 it is in resources)
42 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
43 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // Define a new application type, each program should derive a class from wxApp
51 class MyApp
: public wxApp
54 // override base class virtuals
55 // ----------------------------
57 // this one is called on application startup and is a good place for the app
58 // initialization (doing it here and not in the ctor allows to have an error
59 // return: if OnInit() returns false, the application terminates)
60 virtual bool OnInit();
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame
: public wxFrame
68 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
69 long style
= wxDEFAULT_FRAME_STYLE
);
71 // event handlers (these functions should _not_ be virtual)
72 void OnQuit(wxCommandEvent
& event
);
73 void OnFromPoint(wxCommandEvent
& event
);
74 void OnFullScreen(wxCommandEvent
& event
);
75 void OnAbout(wxCommandEvent
& event
);
77 void OnChangeMode(wxCommandEvent
& event
);
78 void OnResetMode(wxCommandEvent
& event
);
80 void OnLeftClick(wxMouseEvent
& event
);
82 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
85 // convert video mode to textual description
86 wxString
VideoModeToText(const wxVideoMode
& mode
);
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
)
141 EVT_CHOICE(Display_ChangeMode
, MyFrame::OnChangeMode
)
142 EVT_BUTTON(Display_ResetMode
, MyFrame::OnResetMode
)
144 EVT_LEFT_UP(MyFrame::OnLeftClick
)
147 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged
)
150 // Create a new application object: this macro will allow wxWidgets to create
151 // the application object during program execution (it's better than using a
152 // static object for many reasons) and also declares the accessor function
153 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
157 // ============================================================================
159 // ============================================================================
161 // ----------------------------------------------------------------------------
162 // the application class
163 // ----------------------------------------------------------------------------
165 // 'Main program' equivalent: the program execution "starts" here
169 if ( argc
== 2 && !wxStricmp(argv
[1], _T("/dx")) )
171 wxDisplay::UseDirectX(true);
175 // create the main application window
176 MyFrame
*frame
= new MyFrame(_("Display wxWidgets Sample"),
177 wxDefaultPosition
, wxDefaultSize
);
179 // and show it (the frames, unlike simple controls, are not shown when
180 // created initially)
183 // success: wxApp::OnRun() will be called which will enter the main message
184 // loop and the application will run. If we returned false here, the
185 // application would exit immediately.
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
194 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
195 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
197 // set the frame icon
198 SetIcon(wxICON(sample
));
202 wxMenu
*menuDisplay
= new wxMenu
;
203 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
204 menuDisplay
->AppendSeparator();
205 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
206 menuDisplay
->AppendSeparator();
207 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
209 // the "About" item should be in the help menu
210 wxMenu
*helpMenu
= new wxMenu
;
211 helpMenu
->Append(Display_About
, _("&About...\tF1"), _("Show about dialog"));
213 // now append the freshly created menu to the menu bar...
214 wxMenuBar
*menuBar
= new wxMenuBar();
215 menuBar
->Append(menuDisplay
, _("&Display"));
216 menuBar
->Append(helpMenu
, _("&Help"));
218 // ... and attach this menu bar to the frame
220 #endif // wxUSE_MENUS
225 #endif // wxUSE_STATUSBAR
227 // create child controls
229 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
231 m_book
= new wxBookCtrl(panel
, wxID_ANY
);
232 const size_t count
= wxDisplay::GetCount();
233 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
235 wxDisplay
display(nDpy
);
237 wxWindow
*page
= new wxPanel(m_book
, wxID_ANY
);
239 // create 2 column flex grid sizer with growable 2nd column
240 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
241 sizer
->AddGrowableCol(1);
243 const wxRect
r(display
.GetGeometry());
244 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Origin: ")));
245 sizer
->Add(new wxStaticText
249 wxString::Format(_T("(%d, %d)"),
253 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Size: ")));
254 sizer
->Add(new wxStaticText
258 wxString::Format(_T("(%d, %d)"),
263 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Name: ")));
264 sizer
->Add(new wxStaticText(page
, wxID_ANY
, display
.GetName()));
266 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
267 const wxArrayVideoModes modes
= display
.GetModes();
268 const size_t count
= modes
.GetCount();
269 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
271 const wxVideoMode
& mode
= modes
[nMode
];
273 choiceModes
->Append(VideoModeToText(mode
),
274 new MyVideoModeClientData(mode
));
277 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Modes: ")));
278 sizer
->Add(choiceModes
, 0, wxEXPAND
);
280 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Current: ")));
281 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
282 VideoModeToText(display
.GetCurrentMode())));
284 // add it to another sizer to have borders around it and button below
285 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
286 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
288 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
289 0, wxALL
| wxCENTRE
, 5);
290 page
->SetSizer(sizerTop
);
292 m_book
->AddPage(page
,
293 wxString::Format(_T("Display %lu"),
294 (unsigned long)nDpy
));
297 wxBoxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
298 sizer
->Add(m_book
, 1, wxEXPAND
);
299 panel
->SetSizer(sizer
);
301 sizer
->SetSizeHints(this);
304 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
307 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
311 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
316 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
324 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
326 // true is to force the frame to close
330 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
332 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
333 _T("About Display Sample"),
334 wxOK
| wxICON_INFORMATION
,
338 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
341 SetStatusText(_T("Press the mouse anywhere..."));
342 #endif // wxUSE_STATUSBAR
347 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
349 ShowFullScreen(event
.IsChecked());
352 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
354 wxDisplay
dpy(m_book
->GetSelection());
356 // you wouldn't write this in real code, would you?
357 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
358 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
359 GetClientObject(event
.GetInt()))->mode
) )
361 wxLogError(_T("Changing video mode failed!"));
365 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
367 wxDisplay
dpy(m_book
->GetSelection());
372 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
376 // mouse events are in client coords, wxDisplay works in screen ones
377 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
378 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
379 if ( dpy
== wxNOT_FOUND
)
381 wxLogError(_T("Mouse clicked outside of display!?"));
384 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
385 dpy
, ptScreen
.x
, ptScreen
.y
);
391 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
393 // update the current mode text
394 for ( size_t n
= 0; n
< m_book
->GetPageCount(); n
++ )
396 wxStaticText
*label
= wxDynamicCast(m_book
->GetPage(n
)->
397 FindWindow(Display_CurrentMode
),
400 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
404 wxLogStatus(this, _T("Display resolution was changed."));