display.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // for compilers that support precompilation, includes "wx/wx.h"
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers explicitly
  22. #ifndef WX_PRECOMP
  23. #include "wx/wx.h"
  24. #endif
  25. #include "wx/bookctrl.h"
  26. #include "wx/sysopt.h"
  27. #include "wx/display.h"
  28. // the application icon (under Windows and OS/2 it is in resources)
  29. #ifndef wxHAS_IMAGES_IN_RESOURCES
  30. #include "../sample.xpm"
  31. #endif
  32. // ----------------------------------------------------------------------------
  33. // private classes
  34. // ----------------------------------------------------------------------------
  35. // Define a new application type, each program should derive a class from wxApp
  36. class MyApp : public wxApp
  37. {
  38. public:
  39. // override base class virtuals
  40. // ----------------------------
  41. // this one is called on application startup and is a good place for the app
  42. // initialization (doing it here and not in the ctor allows to have an error
  43. // return: if OnInit() returns false, the application terminates)
  44. virtual bool OnInit();
  45. };
  46. // Define a new frame type: this is going to be our main frame
  47. class MyFrame : public wxFrame
  48. {
  49. public:
  50. // ctor(s)
  51. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
  52. long style = wxDEFAULT_FRAME_STYLE);
  53. // event handlers (these functions should _not_ be virtual)
  54. void OnQuit(wxCommandEvent& event);
  55. void OnFromPoint(wxCommandEvent& event);
  56. void OnFullScreen(wxCommandEvent& event);
  57. void OnAbout(wxCommandEvent& event);
  58. #if wxUSE_DISPLAY
  59. void OnChangeMode(wxCommandEvent& event);
  60. void OnResetMode(wxCommandEvent& event);
  61. void OnDisplayChanged(wxDisplayChangedEvent& event);
  62. #endif // wxUSE_DISPLAY
  63. void OnLeftClick(wxMouseEvent& event);
  64. private:
  65. #if wxUSE_DISPLAY
  66. // convert video mode to textual description
  67. wxString VideoModeToText(const wxVideoMode& mode);
  68. #endif // wxUSE_DISPLAY
  69. // GUI controls
  70. wxBookCtrl *m_book;
  71. // any class wishing to process wxWidgets events must use this macro
  72. wxDECLARE_EVENT_TABLE();
  73. };
  74. // Client data class for the choice control containing the video modes
  75. class MyVideoModeClientData : public wxClientData
  76. {
  77. public:
  78. MyVideoModeClientData(const wxVideoMode& m) : mode(m) { }
  79. const wxVideoMode mode;
  80. };
  81. // ----------------------------------------------------------------------------
  82. // constants
  83. // ----------------------------------------------------------------------------
  84. // IDs for the controls and the menu commands
  85. enum
  86. {
  87. // menu items
  88. Display_FromPoint = wxID_HIGHEST + 1,
  89. Display_FullScreen,
  90. // controls
  91. Display_ChangeMode,
  92. Display_ResetMode,
  93. Display_CurrentMode,
  94. // it is important for the id corresponding to the "About" command to have
  95. // this standard value as otherwise it won't be handled properly under Mac
  96. // (where it is special and put into the "Apple" menu)
  97. Display_Quit = wxID_EXIT,
  98. Display_About = wxID_ABOUT
  99. };
  100. // ----------------------------------------------------------------------------
  101. // event tables and other macros for wxWidgets
  102. // ----------------------------------------------------------------------------
  103. // the event tables connect the wxWidgets events with the functions (event
  104. // handlers) which process them. It can be also done at run-time, but for the
  105. // simple menu events like this the static method is much simpler.
  106. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  107. EVT_MENU(Display_Quit, MyFrame::OnQuit)
  108. EVT_MENU(Display_FromPoint, MyFrame::OnFromPoint)
  109. EVT_MENU(Display_FullScreen, MyFrame::OnFullScreen)
  110. EVT_MENU(Display_About, MyFrame::OnAbout)
  111. #if wxUSE_DISPLAY
  112. EVT_CHOICE(Display_ChangeMode, MyFrame::OnChangeMode)
  113. EVT_BUTTON(Display_ResetMode, MyFrame::OnResetMode)
  114. EVT_DISPLAY_CHANGED(MyFrame::OnDisplayChanged)
  115. #endif // wxUSE_DISPLAY
  116. EVT_LEFT_UP(MyFrame::OnLeftClick)
  117. wxEND_EVENT_TABLE()
  118. // Create a new application object: this macro will allow wxWidgets to create
  119. // the application object during program execution (it's better than using a
  120. // static object for many reasons) and also declares the accessor function
  121. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  122. // not wxApp)
  123. IMPLEMENT_APP(MyApp)
  124. // ============================================================================
  125. // implementation
  126. // ============================================================================
  127. // ----------------------------------------------------------------------------
  128. // the application class
  129. // ----------------------------------------------------------------------------
  130. // 'Main program' equivalent: the program execution "starts" here
  131. bool MyApp::OnInit()
  132. {
  133. if ( !wxApp::OnInit() )
  134. return false;
  135. #ifdef __WXMSW__
  136. if ( argc == 2 && !wxStricmp(argv[1], wxT("/dx")) )
  137. {
  138. wxSystemOptions::SetOption(wxT("msw.display.directdraw"), 1);
  139. }
  140. #endif // __WXMSW__
  141. // create the main application window
  142. MyFrame *frame = new MyFrame(_("Display wxWidgets Sample"),
  143. wxDefaultPosition, wxDefaultSize);
  144. // and show it (the frames, unlike simple controls, are not shown when
  145. // created initially)
  146. frame->Show();
  147. // success: wxApp::OnRun() will be called which will enter the main message
  148. // loop and the application will run. If we returned false here, the
  149. // application would exit immediately.
  150. return true;
  151. }
  152. // ----------------------------------------------------------------------------
  153. // main frame
  154. // ----------------------------------------------------------------------------
  155. // frame constructor
  156. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
  157. : wxFrame(NULL, wxID_ANY, title, pos, size, style)
  158. {
  159. // set the frame icon
  160. SetIcon(wxICON(sample));
  161. #if wxUSE_MENUS
  162. // create a menu bar
  163. wxMenu *menuDisplay = new wxMenu;
  164. menuDisplay->Append(Display_FromPoint, _("Find from &point..."));
  165. menuDisplay->AppendSeparator();
  166. menuDisplay->AppendCheckItem(Display_FullScreen, _("Full &screen\tF12"));
  167. menuDisplay->AppendSeparator();
  168. menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program"));
  169. // the "About" item should be in the help menu
  170. wxMenu *helpMenu = new wxMenu;
  171. helpMenu->Append(Display_About, _("&About\tF1"), _("Show about dialog"));
  172. // now append the freshly created menu to the menu bar...
  173. wxMenuBar *menuBar = new wxMenuBar();
  174. menuBar->Append(menuDisplay, _("&Display"));
  175. menuBar->Append(helpMenu, _("&Help"));
  176. // ... and attach this menu bar to the frame
  177. SetMenuBar(menuBar);
  178. #endif // wxUSE_MENUS
  179. #if wxUSE_STATUSBAR
  180. // create status bar
  181. CreateStatusBar();
  182. #endif // wxUSE_STATUSBAR
  183. // create child controls
  184. wxPanel *panel = new wxPanel(this, wxID_ANY);
  185. m_book = new wxBookCtrl(panel, wxID_ANY);
  186. const size_t count = wxDisplay::GetCount();
  187. for ( size_t nDpy = 0; nDpy < count; nDpy++ )
  188. {
  189. wxDisplay display(nDpy);
  190. wxWindow *page = new wxPanel(m_book, wxID_ANY);
  191. // create 2 column flex grid sizer with growable 2nd column
  192. wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20);
  193. sizer->AddGrowableCol(1);
  194. const wxRect r(display.GetGeometry());
  195. sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Origin: ")));
  196. sizer->Add(new wxStaticText
  197. (
  198. page,
  199. wxID_ANY,
  200. wxString::Format(wxT("(%d, %d)"),
  201. r.x, r.y)
  202. ));
  203. sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Size: ")));
  204. sizer->Add(new wxStaticText
  205. (
  206. page,
  207. wxID_ANY,
  208. wxString::Format(wxT("(%d, %d)"),
  209. r.width, r.height)
  210. ));
  211. const wxRect rc(display.GetClientArea());
  212. sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Client area: ")));
  213. sizer->Add(new wxStaticText
  214. (
  215. page,
  216. wxID_ANY,
  217. wxString::Format(wxT("(%d, %d)-(%d, %d)"),
  218. rc.x, rc.y, rc.width, rc.height)
  219. ));
  220. sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Name: ")));
  221. sizer->Add(new wxStaticText(page, wxID_ANY, display.GetName()));
  222. wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
  223. sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10);
  224. #if wxUSE_DISPLAY
  225. wxChoice *choiceModes = new wxChoice(page, Display_ChangeMode);
  226. const wxArrayVideoModes modes = display.GetModes();
  227. const size_t count = modes.GetCount();
  228. for ( size_t nMode = 0; nMode < count; nMode++ )
  229. {
  230. const wxVideoMode& mode = modes[nMode];
  231. choiceModes->Append(VideoModeToText(mode),
  232. new MyVideoModeClientData(mode));
  233. }
  234. sizer->Add(new wxStaticText(page, wxID_ANY, wxT("&Modes: ")));
  235. sizer->Add(choiceModes, 0, wxEXPAND);
  236. sizer->Add(new wxStaticText(page, wxID_ANY, wxT("Current: ")));
  237. sizer->Add(new wxStaticText(page, Display_CurrentMode,
  238. VideoModeToText(display.GetCurrentMode())));
  239. // add it to another sizer to have borders around it and button below
  240. sizerTop->Add(new wxButton(page, Display_ResetMode, wxT("&Reset mode")),
  241. 0, wxALL | wxCENTRE, 5);
  242. #endif // wxUSE_DISPLAY
  243. page->SetSizer(sizerTop);
  244. m_book->AddPage(page,
  245. wxString::Format(wxT("Display %lu"),
  246. (unsigned long)nDpy));
  247. }
  248. wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
  249. sizer->Add(m_book, 1, wxEXPAND);
  250. panel->SetSizer(sizer);
  251. sizer->SetSizeHints(this);
  252. }
  253. #if wxUSE_DISPLAY
  254. wxString MyFrame::VideoModeToText(const wxVideoMode& mode)
  255. {
  256. wxString s;
  257. s.Printf(wxT("%dx%d"), mode.w, mode.h);
  258. if ( mode.bpp )
  259. {
  260. s += wxString::Format(wxT(", %dbpp"), mode.bpp);
  261. }
  262. if ( mode.refresh )
  263. {
  264. s += wxString::Format(wxT(", %dHz"), mode.refresh);
  265. }
  266. return s;
  267. }
  268. #endif // wxUSE_DISPLAY
  269. // event handlers
  270. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  271. {
  272. // true is to force the frame to close
  273. Close(true);
  274. }
  275. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  276. {
  277. wxMessageBox(wxT("Demo program for wxDisplay class.\n\n(c) 2003-2006 Vadim Zeitlin"),
  278. wxT("About Display Sample"),
  279. wxOK | wxICON_INFORMATION,
  280. this);
  281. }
  282. void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event))
  283. {
  284. #if wxUSE_STATUSBAR
  285. SetStatusText(wxT("Press the mouse anywhere..."));
  286. #endif // wxUSE_STATUSBAR
  287. CaptureMouse();
  288. }
  289. void MyFrame::OnFullScreen(wxCommandEvent& event)
  290. {
  291. ShowFullScreen(event.IsChecked());
  292. }
  293. #if wxUSE_DISPLAY
  294. void MyFrame::OnChangeMode(wxCommandEvent& event)
  295. {
  296. wxDisplay dpy(m_book->GetSelection());
  297. // you wouldn't write this in real code, would you?
  298. if ( !dpy.ChangeMode(((MyVideoModeClientData *)
  299. wxDynamicCast(event.GetEventObject(), wxChoice)->
  300. GetClientObject(event.GetInt()))->mode) )
  301. {
  302. wxLogError(wxT("Changing video mode failed!"));
  303. }
  304. }
  305. void MyFrame::OnResetMode(wxCommandEvent& WXUNUSED(event))
  306. {
  307. wxDisplay dpy(m_book->GetSelection());
  308. dpy.ResetMode();
  309. }
  310. #endif // wxUSE_DISPLAY
  311. void MyFrame::OnLeftClick(wxMouseEvent& event)
  312. {
  313. if ( HasCapture() )
  314. {
  315. // mouse events are in client coords, wxDisplay works in screen ones
  316. const wxPoint ptScreen = ClientToScreen(event.GetPosition());
  317. int dpy = wxDisplay::GetFromPoint(ptScreen);
  318. if ( dpy == wxNOT_FOUND )
  319. {
  320. wxLogError(wxT("Mouse clicked outside of display!?"));
  321. }
  322. wxLogStatus(this, wxT("Mouse clicked in display %d (at (%d, %d))"),
  323. dpy, ptScreen.x, ptScreen.y);
  324. ReleaseMouse();
  325. }
  326. }
  327. #if wxUSE_DISPLAY
  328. void MyFrame::OnDisplayChanged(wxDisplayChangedEvent& event)
  329. {
  330. // update the current mode text
  331. for ( size_t n = 0; n < m_book->GetPageCount(); n++ )
  332. {
  333. wxStaticText *label = wxDynamicCast(m_book->GetPage(n)->
  334. FindWindow(Display_CurrentMode),
  335. wxStaticText);
  336. if ( label )
  337. label->SetLabel(VideoModeToText(wxDisplay(n).GetCurrentMode()));
  338. }
  339. wxLogStatus(this, wxT("Display resolution was changed."));
  340. event.Skip();
  341. }
  342. #endif // wxUSE_DISPLAY