]>
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"
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
36 #include "wx/stattext.h"
38 #include "wx/layout.h"
41 #include "wx/notebook.h"
43 #include "wx/display.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // Define a new application type, each program should derive a class from wxApp
50 class MyApp
: public wxApp
53 // override base class virtuals
54 // ----------------------------
56 // this one is called on application startup and is a good place for the app
57 // initialization (doing it here and not in the ctor allows to have an error
58 // return: if OnInit() returns false, the application terminates)
59 virtual bool OnInit();
62 // Define a new frame type: this is going to be our main frame
63 class MyFrame
: public wxFrame
67 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
68 long style
= wxDEFAULT_FRAME_STYLE
);
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent
& event
);
72 void OnFromPoint(wxCommandEvent
& event
);
73 void OnFullScreen(wxCommandEvent
& event
);
74 void OnAbout(wxCommandEvent
& event
);
76 void OnChangeMode(wxCommandEvent
& event
);
77 void OnResetMode(wxCommandEvent
& event
);
79 void OnLeftClick(wxMouseEvent
& event
);
81 void OnDisplayChanged(wxDisplayChangedEvent
& event
);
84 // convert video mode to textual description
85 wxString
VideoModeToText(const wxVideoMode
& mode
);
88 wxNotebook
*m_notebook
;
90 // any class wishing to process wxWindows events must use this macro
94 // Client data class for the choice control containing the video modes
95 class MyVideoModeClientData
: public wxClientData
98 MyVideoModeClientData(const wxVideoMode
& m
) : mode(m
) { }
100 const wxVideoMode mode
;
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // IDs for the controls and the menu commands
117 Display_ChangeMode
= 1000,
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_About
= wxID_ABOUT
128 // ----------------------------------------------------------------------------
129 // event tables and other macros for wxWindows
130 // ----------------------------------------------------------------------------
132 // the event tables connect the wxWindows 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 wxWindows 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 wxWindows Sample"),
177 wxPoint(50, 50), wxSize(450, 340));
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
, -1, title
, pos
, size
, style
)
197 // set the frame icon
198 SetIcon(wxICON(mondrian
));
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 // create child controls
226 m_notebook
= new wxNotebook(this, -1);
227 const size_t count
= wxDisplay::GetCount();
228 for ( size_t nDpy
= 0; nDpy
< count
; nDpy
++ )
230 wxDisplay
display(nDpy
);
232 wxWindow
*page
= new wxPanel(m_notebook
, -1);
234 // create 2 column flex grid sizer with growable 2nd column
235 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
236 sizer
->AddGrowableCol(1);
238 const wxRect
r(display
.GetGeometry());
239 sizer
->Add(new wxStaticText(page
, -1, _T("Origin: ")));
240 sizer
->Add(new wxStaticText
244 wxString::Format(_T("(%d, %d)"),
248 sizer
->Add(new wxStaticText(page
, -1, _T("Size: ")));
249 sizer
->Add(new wxStaticText
253 wxString::Format(_T("(%d, %d)"),
258 sizer
->Add(new wxStaticText(page
, -1, _T("Name: ")));
259 sizer
->Add(new wxStaticText(page
, -1, display
.GetName()));
261 wxChoice
*choiceModes
= new wxChoice(page
, Display_ChangeMode
);
262 const wxArrayVideoModes modes
= display
.GetModes();
263 const size_t count
= modes
.GetCount();
264 for ( size_t nMode
= 0; nMode
< count
; nMode
++ )
266 const wxVideoMode
& mode
= modes
[nMode
];
268 choiceModes
->Append(VideoModeToText(mode
),
269 new MyVideoModeClientData(mode
));
272 sizer
->Add(new wxStaticText(page
, -1, _T("&Modes: ")));
273 sizer
->Add(choiceModes
, 0, wxEXPAND
);
275 sizer
->Add(new wxStaticText(page
, -1, _T("Current: ")));
276 sizer
->Add(new wxStaticText(page
, Display_CurrentMode
,
277 VideoModeToText(display
.GetCurrentMode())));
279 // add it to another sizer to have borders around it and button below
280 wxSizer
*sizerTop
= new wxBoxSizer(wxVERTICAL
);
281 sizerTop
->Add(sizer
, 1, wxALL
| wxEXPAND
, 10);
282 sizerTop
->Add(new wxButton(page
, Display_ResetMode
, _T("&Reset mode")),
283 0, wxALL
| wxCENTRE
, 5);
284 page
->SetSizer(sizerTop
);
286 m_notebook
->AddPage(page
,
287 wxString::Format(_T("Display %lu"),
288 (unsigned long)nDpy
));
292 wxString
MyFrame::VideoModeToText(const wxVideoMode
& mode
)
295 s
.Printf(_T("%dx%d"), mode
.w
, mode
.h
);
299 s
+= wxString::Format(_T(", %dbpp"), mode
.bpp
);
304 s
+= wxString::Format(_T(", %dHz"), mode
.refresh
);
312 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
314 // TRUE is to force the frame to close
318 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
320 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
321 _T("About Display Sample"),
322 wxOK
| wxICON_INFORMATION
,
326 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
328 SetStatusText(_T("Press the mouse anywhere..."));
333 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
335 ShowFullScreen(event
.IsChecked());
338 void MyFrame::OnChangeMode(wxCommandEvent
& event
)
340 wxDisplay
dpy(m_notebook
->GetSelection());
342 // you wouldn't write this in real code, would you?
343 if ( !dpy
.ChangeMode(((MyVideoModeClientData
*)
344 wxDynamicCast(event
.GetEventObject(), wxChoice
)->
345 GetClientObject(event
.GetInt()))->mode
) )
347 wxLogError(_T("Changing video mode failed!"));
351 void MyFrame::OnResetMode(wxCommandEvent
& WXUNUSED(event
))
353 wxDisplay
dpy(m_notebook
->GetSelection());
358 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
362 // mouse events are in client coords, wxDisplay works in screen ones
363 const wxPoint ptScreen
= ClientToScreen(event
.GetPosition());
364 int dpy
= wxDisplay::GetFromPoint(ptScreen
);
365 if ( dpy
== wxNOT_FOUND
)
367 wxLogError(_T("Mouse clicked outside of display!?"));
370 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
371 dpy
, ptScreen
.x
, ptScreen
.y
);
377 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent
& event
)
379 // update the current mode text
380 for ( int n
= 0; n
< m_notebook
->GetPageCount(); n
++ )
382 wxStaticText
*label
= wxDynamicCast(m_notebook
->GetPage(n
)->
383 FindWindow(Display_CurrentMode
),
386 label
->SetLabel(VideoModeToText(wxDisplay(n
).GetCurrentMode()));
390 wxLogStatus(this, _T("Display resolution was changed."));