started writing wxDisplay sample
[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 OnAbout(wxCommandEvent& event);
69
70 private:
71 // any class wishing to process wxWindows events must use this macro
72 DECLARE_EVENT_TABLE()
73 };
74
75 // ----------------------------------------------------------------------------
76 // constants
77 // ----------------------------------------------------------------------------
78
79 // IDs for the controls and the menu commands
80 enum
81 {
82 // menu items
83 Display_Quit = 1,
84
85 // it is important for the id corresponding to the "About" command to have
86 // this standard value as otherwise it won't be handled properly under Mac
87 // (where it is special and put into the "Apple" menu)
88 Display_About = wxID_ABOUT
89 };
90
91 // ----------------------------------------------------------------------------
92 // event tables and other macros for wxWindows
93 // ----------------------------------------------------------------------------
94
95 // the event tables connect the wxWindows events with the functions (event
96 // handlers) which process them. It can be also done at run-time, but for the
97 // simple menu events like this the static method is much simpler.
98 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
99 EVT_MENU(Display_Quit, MyFrame::OnQuit)
100 EVT_MENU(Display_About, MyFrame::OnAbout)
101 END_EVENT_TABLE()
102
103 // Create a new application object: this macro will allow wxWindows to create
104 // the application object during program execution (it's better than using a
105 // static object for many reasons) and also declares the accessor function
106 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
107 // not wxApp)
108 IMPLEMENT_APP(MyApp)
109
110 // ============================================================================
111 // implementation
112 // ============================================================================
113
114 // ----------------------------------------------------------------------------
115 // the application class
116 // ----------------------------------------------------------------------------
117
118 // 'Main program' equivalent: the program execution "starts" here
119 bool MyApp::OnInit()
120 {
121 // create the main application window
122 MyFrame *frame = new MyFrame(_("Display wxWindows Sample"),
123 wxPoint(50, 50), wxSize(450, 340));
124
125 // and show it (the frames, unlike simple controls, are not shown when
126 // created initially)
127 frame->Show(TRUE);
128
129 // success: wxApp::OnRun() will be called which will enter the main message
130 // loop and the application will run. If we returned FALSE here, the
131 // application would exit immediately.
132 return TRUE;
133 }
134
135 // ----------------------------------------------------------------------------
136 // main frame
137 // ----------------------------------------------------------------------------
138
139 // frame constructor
140 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
141 : wxFrame(NULL, -1, title, pos, size, style)
142 {
143 // set the frame icon
144 SetIcon(wxICON(mondrian));
145
146 #if wxUSE_MENUS
147 // create a menu bar
148 wxMenu *menuFile = new wxMenu;
149
150 // the "About" item should be in the help menu
151 wxMenu *helpMenu = new wxMenu;
152 helpMenu->Append(Display_About, _("&About...\tF1"), _("Show about dialog"));
153
154 menuFile->Append(Display_Quit, _("E&xit\tAlt-X"), _("Quit this program"));
155
156 // now append the freshly created menu to the menu bar...
157 wxMenuBar *menuBar = new wxMenuBar();
158 menuBar->Append(menuFile, _("&File"));
159 menuBar->Append(helpMenu, _("&Help"));
160
161 // ... and attach this menu bar to the frame
162 SetMenuBar(menuBar);
163 #endif // wxUSE_MENUS
164
165 // create child controls
166 wxNotebook *notebook = new wxNotebook(this, -1);
167 const size_t count = wxDisplay::GetCount();
168 for ( size_t n = 0; n < count; n++ )
169 {
170 wxDisplay display(n);
171
172 wxWindow *page = new wxPanel(notebook, -1);
173
174 // create 2 column flex grid sizer with growable 2nd column
175 wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 10, 20);
176 sizer->AddGrowableCol(1);
177
178 const wxRect r(display.GetGeometry());
179 sizer->Add(new wxStaticText(page, -1, _T("Geometry: ")));
180 sizer->Add(new wxStaticText
181 (
182 page,
183 -1,
184 wxString::Format(_T("(%d, %d)-(%d, %d)"),
185 r.x, r.y,
186 r.x + r.width, r.y + r.height)
187 ));
188
189 sizer->Add(new wxStaticText(page, -1, _T("Depth: ")));
190 sizer->Add(new wxStaticText
191 (
192 page,
193 -1,
194 wxString::Format(_T("%d bpp"), display.GetDepth())
195 ));
196
197 sizer->Add(new wxStaticText(page, -1, _T("Colour: ")));
198 sizer->Add(new wxStaticText(page, -1, display.IsColour() ? _T("Yes")
199 : _T("No")));
200
201 // add it to another sizer to have borders around it
202 wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
203 sizerTop->Add(sizer, 1, wxALL | wxEXPAND, 10);
204 page->SetSizer(sizerTop);
205
206 notebook->AddPage(page,
207 wxString::Format(_T("Display %lu"), (unsigned long)n));
208 }
209 }
210
211
212 // event handlers
213
214 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
215 {
216 // TRUE is to force the frame to close
217 Close(TRUE);
218 }
219
220 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
221 {
222 wxMessageBox(_T("Demo program for wxDisplay class.\n\n(c) 2003 Vadim Zeitlin"),
223 _T("About Display Sample"),
224 wxOK | wxICON_INFORMATION,
225 this);
226 }