added FromPoint test
[wxWidgets.git] / samples / display / display.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: display.cpp
3 // Purpose: wxWindows sample showing the features of wxDisplay class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 23.02.03
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".from here
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers explicitly
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/frame.h"
31
32 #include "wx/stattext.h"
33
34 #include "wx/layout.h"
35 #endif
36
37 #include "wx/notebook.h"
38
39 #include "wx/display.h"
40
41 // ----------------------------------------------------------------------------
42 // private classes
43 // ----------------------------------------------------------------------------
44
45 // Define a new application type, each program should derive a class from wxApp
46 class MyApp : public wxApp
47 {
48 public:
49 // override base class virtuals
50 // ----------------------------
51
52 // this one is called on application startup and is a good place for the app
53 // initialization (doing it here and not in the ctor allows to have an error
54 // return: if OnInit() returns false, the application terminates)
55 virtual bool OnInit();
56 };
57
58 // Define a new frame type: this is going to be our main frame
59 class MyFrame : public wxFrame
60 {
61 public:
62 // ctor(s)
63 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
64 long style = wxDEFAULT_FRAME_STYLE);
65
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent& event);
68 void OnFromPoint(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
70
71 void OnLeftClick(wxMouseEvent& event);
72
73 private:
74 // any class wishing to process wxWindows events must use this macro
75 DECLARE_EVENT_TABLE()
76 };
77
78 // ----------------------------------------------------------------------------
79 // constants
80 // ----------------------------------------------------------------------------
81
82 // IDs for the controls and the menu commands
83 enum
84 {
85 // menu items
86 Display_Quit = 1,
87
88 Display_FromPoint,
89
90 // it is important for the id corresponding to the "About" command to have
91 // this standard value as otherwise it won't be handled properly under Mac
92 // (where it is special and put into the "Apple" menu)
93 Display_About = wxID_ABOUT
94 };
95
96 // ----------------------------------------------------------------------------
97 // event tables and other macros for wxWindows
98 // ----------------------------------------------------------------------------
99
100 // the event tables connect the wxWindows events with the functions (event
101 // handlers) which process them. It can be also done at run-time, but for the
102 // simple menu events like this the static method is much simpler.
103 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
104 EVT_MENU(Display_Quit, MyFrame::OnQuit)
105 EVT_MENU(Display_FromPoint, MyFrame::OnFromPoint)
106 EVT_MENU(Display_About, MyFrame::OnAbout)
107
108 EVT_LEFT_UP(MyFrame::OnLeftClick)
109 END_EVENT_TABLE()
110
111 // Create a new application object: this macro will allow wxWindows to create
112 // the application object during program execution (it's better than using a
113 // static object for many reasons) and also declares the accessor function
114 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
115 // not wxApp)
116 IMPLEMENT_APP(MyApp)
117
118 // ============================================================================
119 // implementation
120 // ============================================================================
121
122 // ----------------------------------------------------------------------------
123 // the application class
124 // ----------------------------------------------------------------------------
125
126 // 'Main program' equivalent: the program execution "starts" here
127 bool MyApp::OnInit()
128 {
129 // create the main application window
130 MyFrame *frame = new MyFrame(_("Display wxWindows Sample"),
131 wxPoint(50, 50), wxSize(450, 340));
132
133 // and show it (the frames, unlike simple controls, are not shown when
134 // created initially)
135 frame->Show(TRUE);
136
137 // success: wxApp::OnRun() will be called which will enter the main message
138 // loop and the application will run. If we returned FALSE here, the
139 // application would exit immediately.
140 return TRUE;
141 }
142
143 // ----------------------------------------------------------------------------
144 // main frame
145 // ----------------------------------------------------------------------------
146
147 // frame constructor
148 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
149 : wxFrame(NULL, -1, title, pos, size, style)
150 {
151 // set the frame icon
152 SetIcon(wxICON(mondrian));
153
154 #if wxUSE_MENUS
155 // create a menu bar
156 wxMenu *menuDisplay = new wxMenu;
157 menuDisplay->Append(Display_FromPoint, _("&Find from point..."));
158 menuDisplay->AppendSeparator();
159 menuDisplay->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program"));
160
161 // the "About" item should be in the help menu
162 wxMenu *helpMenu = new wxMenu;
163 helpMenu->Append(Display_About, _("&About...\tF1"), _("Show about dialog"));
164
165 // now append the freshly created menu to the menu bar...
166 wxMenuBar *menuBar = new wxMenuBar();
167 menuBar->Append(menuDisplay, _("&Display"));
168 menuBar->Append(helpMenu, _("&Help"));
169
170 // ... and attach this menu bar to the frame
171 SetMenuBar(menuBar);
172 #endif // wxUSE_MENUS
173
174 // create status bar
175 CreateStatusBar();
176
177 // create child controls
178 wxNotebook *notebook = new wxNotebook(this, -1);
179 const size_t count = wxDisplay::GetCount();
180 for ( size_t n = 0; n < count; n++ )
181 {
182 wxDisplay display(n);
183
184 wxWindow *page = new wxPanel(notebook, -1);
185
186 // create 2 column flex grid sizer with growable 2nd column
187 wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20);
188 sizer->AddGrowableCol(1);
189
190 const wxRect r(display.GetGeometry());
191 sizer->Add(new wxStaticText(page, -1, _T("Origin: ")));
192 sizer->Add(new wxStaticText
193 (
194 page,
195 -1,
196 wxString::Format(_T("(%d, %d)"),
197 r.x, r.y)
198 ));
199
200 sizer->Add(new wxStaticText(page, -1, _T("Size: ")));
201 sizer->Add(new wxStaticText
202 (
203 page,
204 -1,
205 wxString::Format(_T("(%d, %d)"),
206 r.width, r.height)
207 ));
208
209 sizer->Add(new wxStaticText(page, -1, _T("Depth: ")));
210 sizer->Add(new wxStaticText
211 (
212 page,
213 -1,
214 wxString::Format(_T("%d bpp"), display.GetDepth())
215 ));
216
217 sizer->Add(new wxStaticText(page, -1, _T("Name: ")));
218 sizer->Add(new wxStaticText(page, -1, display.GetName()));
219
220 sizer->Add(new wxStaticText(page, -1, _T("Colour: ")));
221 sizer->Add(new wxStaticText(page, -1, display.IsColour() ? _T("Yes")
222 : _T("No")));
223
224 // add it to another sizer to have borders around it
225 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
226 sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10);
227 page->SetSizer(sizerTop);
228
229 notebook->AddPage(page,
230 wxString::Format(_T("Display %lu"), (unsigned long)n));
231 }
232 }
233
234
235 // event handlers
236
237 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
238 {
239 // TRUE is to force the frame to close
240 Close(TRUE);
241 }
242
243 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
244 {
245 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
246 _T("About Display Sample"),
247 wxOK | wxICON_INFORMATION,
248 this);
249 }
250
251 void MyFrame::OnFromPoint(wxCommandEvent& WXUNUSED(event))
252 {
253 SetStatusText(_T("Press the mouse anywhere..."));
254
255 CaptureMouse();
256 }
257
258 void MyFrame::OnLeftClick(wxMouseEvent& event)
259 {
260 if ( HasCapture() )
261 {
262 // mouse events are in client coords, wxDisplay works in screen ones
263 const wxPoint ptScreen = ClientToScreen(event.GetPosition());
264 int dpy = wxDisplay::GetFromPoint(ptScreen);
265 if ( dpy == wxNOT_FOUND )
266 {
267 wxLogError(_T("Mouse clicked outside of display!!"));
268 }
269
270 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
271 dpy, ptScreen.x, ptScreen.y);
272
273 ReleaseMouse();
274 }
275 }
276