]>
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"
34 #include "wx/display.h"
37 // the application icon (under Windows and OS/2 it is in resources)
38 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
39 #include "../sample.xpm"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // Define a new application type, each program should derive a class from wxApp
47 class MyApp
: public wxApp
50 // override base class virtuals
51 // ----------------------------
53 // this one is called on application startup and is a good place for the app
54 // initialization (doing it here and not in the ctor allows to have an error
55 // return: if OnInit() returns false, the application terminates)
56 virtual bool OnInit();
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame
: public wxFrame
64 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
65 long style
= wxDEFAULT_FRAME_STYLE
);
67 // event handlers (these functions should _not_ be virtual)
68 void OnQuit(wxCommandEvent
& event
);
69 void OnFromPoint(wxCommandEvent
& event
);
70 void OnFullScreen(wxCommandEvent
& event
);
71 void OnAbout(wxCommandEvent
& event
);
73 void OnChangeMode(wxCommandEvent
& event
);
74 void OnResetMode(wxCommandEvent
& event
);
76 void OnLeftClick(wxMouseEvent
& event
);
79 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
84 // convert video mode to textual description
85 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
)
151 // Create a new application object: this macro will allow wxWidgets to create
152 // the application object during program execution (it's better than using a
153 // static object for many reasons) and also declares the accessor function
154 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
158 // ============================================================================
160 // ============================================================================
162 // ----------------------------------------------------------------------------
163 // the application class
164 // ----------------------------------------------------------------------------
166 // 'Main program' equivalent: the program execution "starts" here
170 wxMessageBox(_("Please recompile wxWidgets and this sample with wxUSE_DISPLAY set to 1."));
175 if ( argc
== 2 && !wxStricmp(argv
[1], _T("/dx")) )
177 wxDisplay::UseDirectX(true);
181 // create the main application window
182 MyFrame
*frame
= new MyFrame(_("Display wxWidgets Sample"),
183 wxDefaultPosition
, wxDefaultSize
);
185 // and show it (the frames, unlike simple controls, are not shown when
186 // created initially)
189 // success: wxApp::OnRun() will be called which will enter the main message
190 // loop and the application will run. If we returned false here, the
191 // application would exit immediately.
196 // ----------------------------------------------------------------------------
198 // ----------------------------------------------------------------------------
201 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
202 : wxFrame(NULL
, wxID_ANY
, title
, pos
, size
, style
)
204 // set the frame icon
205 SetIcon(wxICON(sample
));
209 wxMenu
*menuDisplay
= new wxMenu
;
210 menuDisplay
->Append(Display_FromPoint
, _("Find from &point..."));
211 menuDisplay
->AppendSeparator();
212 menuDisplay
->AppendCheckItem(Display_FullScreen
, _("Full &screen\tF12"));
213 menuDisplay
->AppendSeparator();
214 menuDisplay
->Append(Display_Quit
, _("E&xit\tAlt-X"), _("Quit this program"));
216 // the "About" item should be in the help menu
217 wxMenu
*helpMenu
= new wxMenu
;
218 helpMenu
->Append(Display_About
, _("&About...\tF1"), _("Show about dialog"));
220 // now append the freshly created menu to the menu bar...
221 wxMenuBar
*menuBar
= new wxMenuBar();
222 menuBar
->Append(menuDisplay
, _("&Display"));
223 menuBar
->Append(helpMenu
, _("&Help"));
225 // ... and attach this menu bar to the frame
227 #endif // wxUSE_MENUS
232 #endif // wxUSE_STATUSBAR
235 // create child controls
237 wxPanel
*panel
= new wxPanel(this, wxID_ANY
);
239 m_book
= new wxBookCtrl(panel
, wxID_ANY
);
240 const size_t count
= wxDisplay::GetCount();
241 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
243 wxDisplay
display(nDpy
);
245 wxWindow
*page
= new wxPanel(m_book
, wxID_ANY
);
247 // create 2 column flex grid sizer with growable 2nd column
248 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
249 sizer
->AddGrowableCol(1);
251 const wxRect
r(display
.GetGeometry());
252 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Origin: ")));
253 sizer
->Add(new wxStaticText
257 wxString::Format(_T("(%d, %d)"),
261 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Size: ")));
262 sizer
->Add(new wxStaticText
266 wxString::Format(_T("(%d, %d)"),
271 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Name: ")));
272 sizer
->Add(new wxStaticText(page
, wxID_ANY
, display
.GetName()));
274 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
275 const wxArrayVideoModes modes
= display
.GetModes();
276 const size_t count
= modes
.GetCount();
277 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
279 const wxVideoMode
& mode
= modes
[nMode
];
281 choiceModes
->Append(VideoModeToText(mode
),
282 new MyVideoModeClientData(mode
));
285 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("&Modes: ")));
286 sizer
->Add(choiceModes
, 0, wxEXPAND
);
288 sizer
->Add(new wxStaticText(page
, wxID_ANY
, _T("Current: ")));
289 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
290 VideoModeToText(display
.GetCurrentMode())));
292 // add it to another sizer to have borders around it and button below
293 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
294 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
296 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
297 0, wxALL
| wxCENTRE
, 5);
298 page
->SetSizer(sizerTop
);
300 m_book
->AddPage(page
,
301 wxString::Format(_T("Display %lu"),
302 (unsigned long)nDpy
));
305 wxBoxSizer
*sizer
= new wxBoxSizer(wxHORIZONTAL
);
306 sizer
->Add(m_book
, 1, wxEXPAND
);
307 panel
->SetSizer(sizer
);
309 sizer
->SetSizeHints(this);
314 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
317 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
321 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
326 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
335 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
337 // true is to force the frame to close
341 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
343 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
344 _T("About Display Sample"),
345 wxOK
| wxICON_INFORMATION
,
349 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
352 SetStatusText(_T("Press the mouse anywhere..."));
353 #endif // wxUSE_STATUSBAR
358 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
360 ShowFullScreen(event
.IsChecked());
363 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
366 wxDisplay
dpy(m_book
->GetSelection());
368 // you wouldn't write this in real code, would you?
369 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
370 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
371 GetClientObject(event
.GetInt()))->mode
) )
373 wxLogError(_T("Changing video mode failed!"));
378 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
381 wxDisplay
dpy(m_book
->GetSelection());
387 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
392 // mouse events are in client coords, wxDisplay works in screen ones
393 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
394 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
395 if ( dpy
== wxNOT_FOUND
)
397 wxLogError(_T("Mouse clicked outside of display!?"));
400 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
401 dpy
, ptScreen
.x
, ptScreen
.y
);
409 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
411 // update the current mode text
412 for ( size_t n
= 0; n
< m_book
->GetPageCount(); n
++ )
414 wxStaticText
*label
= wxDynamicCast(m_book
->GetPage(n
)->
415 FindWindow(Display_CurrentMode
),
418 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
422 wxLogStatus(this, _T("Display resolution was changed."));