]> git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
Added setters for table row and column count
[wxWidgets.git] / samples / display / display.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: display.cpp
3 // Purpose: wxWidgets sample showing the features of wxDisplay class
4 // Author: Vadim Zeitlin
5 // Modified by: Ryan Norton & Brian Victor
6 // Created: 23.02.03
7 // Copyright: (c) Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx/wx.h"
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 // for all others, include the necessary headers explicitly
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 #include "wx/bookctrl.h"
32 #include "wx/sysopt.h"
33
34 #include "wx/display.h"
35
36
37 // the application icon (under Windows and OS/2 it is in resources)
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
40 #endif
41
42 // ----------------------------------------------------------------------------
43 // private classes
44 // ----------------------------------------------------------------------------
45
46 // Define a new application type, each program should derive a class from wxApp
47 class MyApp : public wxApp
48 {
49 public:
50 // override base class virtuals
51 // ----------------------------
52
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();
57 };
58
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame : public wxFrame
61 {
62 public:
63 // ctor(s)
64 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
65 long style = wxDEFAULT_FRAME_STYLE);
66
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);
72
73 #if wxUSE_DISPLAY
74 void OnChangeMode(wxCommandEvent& event);
75 void OnResetMode(wxCommandEvent& event);
76
77 void OnDisplayChanged(wxDisplayChangedEvent& event);
78 #endif // wxUSE_DISPLAY
79
80 void OnLeftClick(wxMouseEvent& event);
81
82 private:
83 #if wxUSE_DISPLAY
84 // convert video mode to textual description
85 wxString VideoModeToText(const wxVideoMode& mode);
86 #endif // wxUSE_DISPLAY
87
88 // GUI controls
89 wxBookCtrl *m_book;
90
91 // any class wishing to process wxWidgets events must use this macro
92 DECLARE_EVENT_TABLE()
93 };
94
95 // Client data class for the choice control containing the video modes
96 class MyVideoModeClientData : public wxClientData
97 {
98 public:
99 MyVideoModeClientData(const wxVideoMode& m) : mode(m) { }
100
101 const wxVideoMode mode;
102 };
103
104 // ----------------------------------------------------------------------------
105 // constants
106 // ----------------------------------------------------------------------------
107
108 // IDs for the controls and the menu commands
109 enum
110 {
111 // menu items
112 Display_FromPoint = wxID_HIGHEST + 1,
113 Display_FullScreen,
114
115 // controls
116 Display_ChangeMode,
117 Display_ResetMode,
118 Display_CurrentMode,
119
120
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
126 };
127
128 // ----------------------------------------------------------------------------
129 // event tables and other macros for wxWidgets
130 // ----------------------------------------------------------------------------
131
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)
140
141 #if wxUSE_DISPLAY
142 EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode)
143 EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode)
144
145 EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged)
146 #endif // wxUSE_DISPLAY
147
148 EVT_LEFT_UP(MyFrame::OnLeftClick)
149 END_EVENT_TABLE()
150
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
155 // not wxApp)
156 IMPLEMENT_APP(MyApp)
157
158 // ============================================================================
159 // implementation
160 // ============================================================================
161
162 // ----------------------------------------------------------------------------
163 // the application class
164 // ----------------------------------------------------------------------------
165
166 // 'Main program' equivalent: the program execution "starts" here
167 bool MyApp::OnInit()
168 {
169 if ( !wxApp::OnInit() )
170 return false;
171
172 #ifdef __WXMSW__
173 if ( argc == 2 && !wxStricmp(argv[1], wxT("/dx")) )
174 {
175 wxSystemOptions::SetOption(wxT("msw.display.directdraw"), 1);
176 }
177 #endif // __WXMSW__
178
179 // create the main application window
180 MyFrame *frame = new MyFrame(_("Display wxWidgets Sample"),
181 wxDefaultPosition, wxDefaultSize);
182
183 // and show it (the frames, unlike simple controls, are not shown when
184 // created initially)
185 frame->Show();
186
187 // success: wxApp::OnRun() will be called which will enter the main message
188 // loop and the application will run. If we returned false here, the
189 // application would exit immediately.
190 return true;
191 }
192
193 // ----------------------------------------------------------------------------
194 // main frame
195 // ----------------------------------------------------------------------------
196
197 // frame constructor
198 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
199 : wxFrame(NULL, wxID_ANY, title, pos, size, style)
200 {
201 // set the frame icon
202 SetIcon(wxICON(sample));
203
204 #if wxUSE_MENUS
205 // create a menu bar
206 wxMenu *menuDisplay = new wxMenu;
207 menuDisplay->Append(Display_FromPoint, _("Find from &point..."));
208 menuDisplay->AppendSeparator();
209 menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12"));
210 menuDisplay->AppendSeparator();
211 menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program"));
212
213 // the "About" item should be in the help menu
214 wxMenu *helpMenu = new wxMenu;
215 helpMenu->Append(Display_About, _("&About\tF1"), _("Show about dialog"));
216
217 // now append the freshly created menu to the menu bar...
218 wxMenuBar *menuBar = new wxMenuBar();
219 menuBar->Append(menuDisplay, _("&Display"));
220 menuBar->Append(helpMenu, _("&Help"));
221
222 // ... and attach this menu bar to the frame
223 SetMenuBar(menuBar);
224 #endif // wxUSE_MENUS
225
226 #if wxUSE_STATUSBAR
227 // create status bar
228 CreateStatusBar();
229 #endif // wxUSE_STATUSBAR
230
231 // create child controls
232 wxPanel *panel = new wxPanel(this, wxID_ANY);
233
234 m_book = new wxBookCtrl(panel, wxID_ANY);
235 const size_t count = wxDisplay::GetCount();
236 for ( size_t nDpy = 0; nDpy < count; nDpy++ )
237 {
238 wxDisplay display(nDpy);
239
240 wxWindow *page = new wxPanel(m_book, wxID_ANY);
241
242 // create 2 column flex grid sizer with growable 2nd column
243 wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20);
244 sizer->AddGrowableCol(1);
245
246 const wxRect r(display.GetGeometry());
247 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Origin: ")));
248 sizer->Add(new wxStaticText
249 (
250 page,
251 wxID_ANY,
252 wxString::Format(wxT("(%d, %d)"),
253 r.x, r.y)
254 ));
255
256 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Size: ")));
257 sizer->Add(new wxStaticText
258 (
259 page,
260 wxID_ANY,
261 wxString::Format(wxT("(%d, %d)"),
262 r.width, r.height)
263 ));
264
265 const wxRect rc(display.GetClientArea());
266 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Client area: ")));
267 sizer->Add(new wxStaticText
268 (
269 page,
270 wxID_ANY,
271 wxString::Format(wxT("(%d, %d)-(%d, %d)"),
272 rc.x, rc.y, rc.width, rc.height)
273 ));
274
275 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Name: ")));
276 sizer->Add(new wxStaticText(page, wxID_ANY, display.GetName()));
277
278 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
279 sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10);
280
281 #if wxUSE_DISPLAY
282 wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode);
283 const wxArrayVideoModes modes = display.GetModes();
284 const size_t count = modes.GetCount();
285 for ( size_t nMode = 0; nMode < count; nMode++ )
286 {
287 const wxVideoMode& mode = modes[nMode];
288
289 choiceModes->Append(VideoModeToText(mode),
290 new MyVideoModeClientData(mode));
291 }
292
293 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("&Modes: ")));
294 sizer->Add(choiceModes, 0, wxEXPAND);
295
296 sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Current: ")));
297 sizer->Add(new wxStaticText(page, Display_CurrentMode,
298 VideoModeToText(display.GetCurrentMode())));
299
300 // add it to another sizer to have borders around it and button below
301 sizerTop->Add(new wxButton(page, Display_ResetMode, wxT("&Reset mode")),
302 0, wxALL | wxCENTRE, 5);
303 #endif // wxUSE_DISPLAY
304
305 page->SetSizer(sizerTop);
306
307 m_book->AddPage(page,
308 wxString::Format(wxT("Display %lu"),
309 (unsigned long)nDpy));
310 }
311
312 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
313 sizer->Add(m_book, 1, wxEXPAND);
314 panel->SetSizer(sizer);
315 sizer->SetSizeHints(this);
316 }
317
318 #if wxUSE_DISPLAY
319
320 wxString MyFrame::VideoModeToText(const wxVideoMode& mode)
321 {
322 wxString s;
323 s.Printf(wxT("%dx%d"), mode.w, mode.h);
324
325 if ( mode.bpp )
326 {
327 s += wxString::Format(wxT(", %dbpp"), mode.bpp);
328 }
329
330 if ( mode.refresh )
331 {
332 s += wxString::Format(wxT(", %dHz"), mode.refresh);
333 }
334
335 return s;
336 }
337
338 #endif // wxUSE_DISPLAY
339
340 // event handlers
341
342 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
343 {
344 // true is to force the frame to close
345 Close(true);
346 }
347
348 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
349 {
350 wxMessageBox(wxT("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
351 wxT("About Display Sample"),
352 wxOK | wxICON_INFORMATION,
353 this);
354 }
355
356 void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event))
357 {
358 #if wxUSE_STATUSBAR
359 SetStatusText(wxT("Press the mouse anywhere..."));
360 #endif // wxUSE_STATUSBAR
361
362 CaptureMouse();
363 }
364
365 void MyFrame::OnFullScreen(wxCommandEvent& event)
366 {
367 ShowFullScreen(event.IsChecked());
368 }
369
370 #if wxUSE_DISPLAY
371
372 void MyFrame::OnChangeMode(wxCommandEvent& event)
373 {
374 wxDisplay dpy(m_book->GetSelection());
375
376 // you wouldn't write this in real code, would you?
377 if ( !dpy.ChangeMode(((MyVideoModeClientData *)
378 wxDynamicCast(event.GetEventObject(), wxChoice)->
379 GetClientObject(event.GetInt()))->mode) )
380 {
381 wxLogError(wxT("Changing video mode failed!"));
382 }
383 }
384
385 void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event))
386 {
387 wxDisplay dpy(m_book->GetSelection());
388
389 dpy.ResetMode();
390 }
391
392 #endif // wxUSE_DISPLAY
393
394 void MyFrame::OnLeftClick(wxMouseEvent& event)
395 {
396 if ( HasCapture() )
397 {
398 // mouse events are in client coords, wxDisplay works in screen ones
399 const wxPoint ptScreen = ClientToScreen(event.GetPosition());
400 int dpy = wxDisplay::GetFromPoint(ptScreen);
401 if ( dpy == wxNOT_FOUND )
402 {
403 wxLogError(wxT("Mouse clicked outside of display!?"));
404 }
405
406 wxLogStatus(this, wxT("Mouse clicked in display %d (at (%d, %d))"),
407 dpy, ptScreen.x, ptScreen.y);
408
409 ReleaseMouse();
410 }
411 }
412
413 #if wxUSE_DISPLAY
414
415 void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event)
416 {
417 // update the current mode text
418 for ( size_t n = 0; n < m_book->GetPageCount(); n++ )
419 {
420 wxStaticText *label = wxDynamicCast(m_book->GetPage(n)->
421 FindWindow(Display_CurrentMode),
422 wxStaticText);
423 if ( label )
424 label->SetLabel(VideoModeToText(wxDisplay(n).GetCurrentMode()));
425 }
426
427
428 wxLogStatus(this, wxT("Display resolution was changed."));
429
430 event.Skip();
431 }
432
433 #endif // wxUSE_DISPLAY