]>
git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
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
171 if ( argc
== 2 && !wxStricmp(argv
[1], _T("/dx")) )
173 wxSystemOptions::SetOption(_T("msw.display.directdraw"), 1);
177 // create the main application window
178 MyFrame
*frame
= new MyFrame(_("Display wxWidgets Sample"),
179 wxDefaultPosition
, wxDefaultSize
);
181 // and show it (the frames, unlike simple controls, are not shown when
182 // created initially)
185 // success: wxApp::OnRun() will be called which will enter the main message
186 // loop and the application will run. If we returned false here, the
187 // application would exit immediately.
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
196 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
197 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
199 // set the frame icon
200 SetIcon(wxICON(sample
));
204 wxMenu
*menuDisplay
= new wxMenu
;
205 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
206 menuDisplay
->AppendSeparator();
207 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
208 menuDisplay
->AppendSeparator();
209 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
211 // the "About" item should be in the help menu
212 wxMenu
*helpMenu
= new wxMenu
;
213 helpMenu
->Append(Display_About
, _("&About...\tF1"), _("Show about dialog"));
215 // now append the freshly created menu to the menu bar...
216 wxMenuBar
*menuBar
= new wxMenuBar();
217 menuBar
->Append(menuDisplay
, _("&Display"));
218 menuBar
->Append(helpMenu
, _("&Help"));
220 // ... and attach this menu bar to the frame
222 #endif // wxUSE_MENUS
227 #endif // wxUSE_STATUSBAR
229 // create child controls
230 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
232 m_book
= new wxBookCtrl(panel
, wxID_ANY
);
233 const size_t count
= wxDisplay::GetCount();
234 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
236 wxDisplay
display(nDpy
);
238 wxWindow
*page
= new wxPanel(m_book
, wxID_ANY
);
240 // create 2 column flex grid sizer with growable 2nd column
241 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
242 sizer
->AddGrowableCol(1);
244 const wxRect
r(display
.GetGeometry());
245 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Origin: ")));
246 sizer
->Add(new wxStaticText
250 wxString::Format(_T("(%d, %d)"),
254 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Size: ")));
255 sizer
->Add(new wxStaticText
259 wxString::Format(_T("(%d, %d)"),
263 const wxRect
rc(display
.GetClientArea());
264 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Client area: ")));
265 sizer
->Add(new wxStaticText
269 wxString::Format(_T("(%d, %d)-(%d, %d)"),
270 rc
.x
, rc
.y
, rc
.width
, rc
.height
)
273 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Name: ")));
274 sizer
->Add(new wxStaticText(page
, wxID_ANY
, display
.GetName()));
276 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
277 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
280 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
281 const wxArrayVideoModes modes
= display
.GetModes();
282 const size_t count
= modes
.GetCount();
283 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
285 const wxVideoMode
& mode
= modes
[nMode
];
287 choiceModes
->Append(VideoModeToText(mode
),
288 new MyVideoModeClientData(mode
));
291 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Modes: ")));
292 sizer
->Add(choiceModes
, 0, wxEXPAND
);
294 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Current: ")));
295 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
296 VideoModeToText(display
.GetCurrentMode())));
298 // add it to another sizer to have borders around it and button below
299 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
300 0, wxALL
| wxCENTRE
, 5);
301 #endif // wxUSE_DISPLAY
303 page
->SetSizer(sizerTop
);
305 m_book
->AddPage(page
,
306 wxString::Format(_T("Display %lu"),
307 (unsigned long)nDpy
));
310 wxBoxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
311 sizer
->Add(m_book
, 1, wxEXPAND
);
312 panel
->SetSizer(sizer
);
314 sizer
->SetSizeHints(this);
319 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
322 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
326 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
331 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
337 #endif // wxUSE_DISPLAY
341 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
343 // true is to force the frame to close
347 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
349 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
350 _T("About Display Sample"),
351 wxOK
| wxICON_INFORMATION
,
355 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
358 SetStatusText(_T("Press the mouse anywhere..."));
359 #endif // wxUSE_STATUSBAR
364 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
366 ShowFullScreen(event
.IsChecked());
371 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
373 wxDisplay
dpy(m_book
->GetSelection());
375 // you wouldn't write this in real code, would you?
376 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
377 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
378 GetClientObject(event
.GetInt()))->mode
) )
380 wxLogError(_T("Changing video mode failed!"));
384 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
386 wxDisplay
dpy(m_book
->GetSelection());
391 #endif // wxUSE_DISPLAY
393 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
397 // mouse events are in client coords, wxDisplay works in screen ones
398 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
399 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
400 if ( dpy
== wxNOT_FOUND
)
402 wxLogError(_T("Mouse clicked outside of display!?"));
405 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
406 dpy
, ptScreen
.x
, ptScreen
.y
);
414 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
416 // update the current mode text
417 for ( size_t n
= 0; n
< m_book
->GetPageCount(); n
++ )
419 wxStaticText
*label
= wxDynamicCast(m_book
->GetPage(n
)->
420 FindWindow(Display_CurrentMode
),
423 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
427 wxLogStatus(this, _T("Display resolution was changed."));
432 #endif // wxUSE_DISPLAY