]>
git.saurik.com Git - wxWidgets.git/blob - samples/display/display.cpp
dbe0311e6f2cf699dd1b3e28283351820a1057a2
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"
27 // for all others, include the necessary headers explicitly
32 #include "wx/stattext.h"
34 #include "wx/layout.h"
37 #include "wx/notebook.h"
39 #include "wx/display.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // Define a new application type, each program should derive a class from wxApp
46 class MyApp
: public wxApp
49 // override base class virtuals
50 // ----------------------------
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();
58 // Define a new frame type: this is going to be our main frame
59 class MyFrame
: public wxFrame
63 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
64 long style
= wxDEFAULT_FRAME_STYLE
);
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent
& event
);
68 void OnFromPoint(wxCommandEvent
& event
);
69 void OnAbout(wxCommandEvent
& event
);
71 void OnLeftClick(wxMouseEvent
& event
);
74 // any class wishing to process wxWindows events must use this macro
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // IDs for the controls and the menu commands
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
96 // ----------------------------------------------------------------------------
97 // event tables and other macros for wxWindows
98 // ----------------------------------------------------------------------------
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
)
108 EVT_LEFT_UP(MyFrame::OnLeftClick
)
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
118 // ============================================================================
120 // ============================================================================
122 // ----------------------------------------------------------------------------
123 // the application class
124 // ----------------------------------------------------------------------------
126 // 'Main program' equivalent: the program execution "starts" here
129 // create the main application window
130 MyFrame
*frame
= new MyFrame(_("Display wxWindows Sample"),
131 wxPoint(50, 50), wxSize(450, 340));
133 // and show it (the frames, unlike simple controls, are not shown when
134 // created initially)
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.
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
148 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
, long style
)
149 : wxFrame(NULL
, -1, title
, pos
, size
, style
)
151 // set the frame icon
152 SetIcon(wxICON(mondrian
));
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"));
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"));
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"));
170 // ... and attach this menu bar to the frame
172 #endif // wxUSE_MENUS
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
++ )
182 wxDisplay
display(n
);
184 wxWindow
*page
= new wxPanel(notebook
, -1);
186 // create 2 column flex grid sizer with growable 2nd column
187 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 10, 20);
188 sizer
->AddGrowableCol(1);
190 const wxRect
r(display
.GetGeometry());
191 sizer
->Add(new wxStaticText(page
, -1, _T("Origin: ")));
192 sizer
->Add(new wxStaticText
196 wxString::Format(_T("(%d, %d)"),
200 sizer
->Add(new wxStaticText(page
, -1, _T("Size: ")));
201 sizer
->Add(new wxStaticText
205 wxString::Format(_T("(%d, %d)"),
209 sizer
->Add(new wxStaticText(page
, -1, _T("Depth: ")));
210 sizer
->Add(new wxStaticText
214 wxString::Format(_T("%d bpp"), display
.GetDepth())
217 sizer
->Add(new wxStaticText(page
, -1, _T("Name: ")));
218 sizer
->Add(new wxStaticText(page
, -1, display
.GetName()));
220 sizer
->Add(new wxStaticText(page
, -1, _T("Colour: ")));
221 sizer
->Add(new wxStaticText(page
, -1, display
.IsColour() ? _T("Yes")
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
);
229 notebook
->AddPage(page
,
230 wxString::Format(_T("Display %lu"), (unsigned long)n
));
237 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
239 // TRUE is to force the frame to close
243 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
245 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
246 _T("About Display Sample"),
247 wxOK
| wxICON_INFORMATION
,
251 void MyFrame::OnFromPoint(wxCommandEvent
& WXUNUSED(event
))
253 SetStatusText(_T("Press the mouse anywhere..."));
258 void MyFrame::OnLeftClick(wxMouseEvent
& event
)
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
)
267 wxLogError(_T("Mouse clicked outside of display!!"));
270 wxLogStatus(this, _T("Mouse clicked in display %d (at (%d, %d))"),
271 dpy
, ptScreen
.x
, ptScreen
.y
);