]>
Commit | Line | Data |
---|---|---|
cf7d6329 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: vscroll.cpp | |
be5a51fb | 3 | // Purpose: VScroll wxWidgets sample |
cf7d6329 VZ |
4 | // Author: Vadim Zeitlin |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
be5a51fb | 8 | // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org> |
cf7d6329 VZ |
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". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 28 | // need because it includes almost all "standard" wxWidgets headers) |
cf7d6329 | 29 | #ifndef WX_PRECOMP |
dacaa6f1 | 30 | #include "wx/wx.h" |
cf7d6329 VZ |
31 | #include "wx/app.h" |
32 | #include "wx/frame.h" | |
33 | #endif | |
34 | ||
35 | // we need to include the headers not included from wx/wx.h explicitly anyhow | |
36 | #include "wx/vscroll.h" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // resources | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // the application icon (under Windows and OS/2 it is in resources) | |
43 | #if !defined(__WXMSW__) && !defined(__WXOS2__) | |
44 | #include "mondrian.xpm" | |
45 | #endif | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // private classes | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // Define a new application type, each program should derive a class from wxApp | |
52 | class VScrollApp : public wxApp | |
53 | { | |
54 | public: | |
55 | // create our main window | |
56 | virtual bool OnInit(); | |
57 | }; | |
58 | ||
59 | // Define a new frame type: this is going to be our main frame | |
60 | class VScrollFrame : public wxFrame | |
61 | { | |
62 | public: | |
63 | // ctor | |
64 | VScrollFrame(); | |
65 | ||
66 | // event handlers (these functions should _not_ be virtual) | |
67 | void OnQuit(wxCommandEvent& event); | |
68 | void OnAbout(wxCommandEvent& event); | |
69 | ||
70 | void OnSize(wxSizeEvent& event) | |
71 | { | |
72 | // show current size in the status bar | |
73 | #if wxUSE_STATUSBAR | |
74 | if ( m_frameStatusBar ) | |
75 | { | |
76 | wxSize sz = GetClientSize(); | |
87728739 | 77 | SetStatusText(wxString::Format(_T("%dx%d"), sz.x, sz.y), 1); |
cf7d6329 VZ |
78 | } |
79 | #endif // wxUSE_STATUSBAR | |
80 | ||
81 | event.Skip(); | |
82 | } | |
83 | ||
84 | private: | |
be5a51fb | 85 | // any class wishing to process wxWidgets events must use this macro |
cf7d6329 VZ |
86 | DECLARE_EVENT_TABLE() |
87 | }; | |
88 | ||
89 | class VScrollWindow : public wxVScrolledWindow | |
90 | { | |
91 | public: | |
540a08d2 | 92 | VScrollWindow(wxFrame *frame) : wxVScrolledWindow(frame, wxID_ANY) |
cf7d6329 VZ |
93 | { |
94 | m_frame = frame; | |
95 | ||
96 | SetLineCount(200); | |
97 | ||
98 | m_changed = true; | |
99 | } | |
100 | ||
101 | void OnIdle(wxIdleEvent&) | |
102 | { | |
8520f137 | 103 | #if wxUSE_STATUSBAR |
cf7d6329 VZ |
104 | m_frame->SetStatusText(wxString::Format |
105 | ( | |
87728739 | 106 | _T("Page size = %d, pos = %d, max = %d"), |
cf7d6329 VZ |
107 | GetScrollThumb(wxVERTICAL), |
108 | GetScrollPos(wxVERTICAL), | |
109 | GetScrollRange(wxVERTICAL) | |
110 | )); | |
8520f137 | 111 | #endif // wxUSE_STATUSBAR |
cf7d6329 VZ |
112 | m_changed = false; |
113 | } | |
114 | ||
115 | void OnPaint(wxPaintEvent&) | |
116 | { | |
117 | wxPaintDC dc(this); | |
118 | ||
119 | dc.SetPen(*wxBLACK_DASHED_PEN); | |
120 | ||
121 | const size_t lineFirst = GetFirstVisibleLine(), | |
122 | lineLast = GetLastVisibleLine(); | |
123 | ||
124 | const wxCoord hText = dc.GetCharHeight(); | |
125 | ||
126 | wxCoord y = 0; | |
127 | for ( size_t line = lineFirst; line <= lineLast; line++ ) | |
128 | { | |
129 | dc.DrawLine(0, y, 1000, y); | |
130 | ||
131 | wxCoord hLine = OnGetLineHeight(line); | |
132 | dc.DrawText(wxString::Format(_T("Line %lu"), (unsigned long)line), | |
133 | 0, y + (hLine - hText) / 2); | |
134 | ||
135 | y += hLine; | |
136 | dc.DrawLine(0, y, 1000, y); | |
137 | } | |
138 | } | |
139 | ||
140 | void OnScroll(wxScrollWinEvent& event) | |
141 | { | |
142 | m_changed = true; | |
143 | ||
144 | event.Skip(); | |
145 | } | |
146 | ||
147 | ||
148 | virtual wxCoord OnGetLineHeight(size_t n) const | |
149 | { | |
150 | wxASSERT( n < GetLineCount() ); | |
151 | ||
152 | return n % 2 ? 15 : 30; // 15 + 2*n | |
153 | } | |
154 | ||
155 | private: | |
156 | wxFrame *m_frame; | |
157 | ||
158 | bool m_changed; | |
159 | ||
160 | DECLARE_EVENT_TABLE() | |
161 | }; | |
162 | ||
163 | BEGIN_EVENT_TABLE(VScrollWindow, wxVScrolledWindow) | |
164 | EVT_IDLE(VScrollWindow::OnIdle) | |
165 | EVT_PAINT(VScrollWindow::OnPaint) | |
166 | EVT_SCROLLWIN(VScrollWindow::OnScroll) | |
167 | END_EVENT_TABLE() | |
168 | ||
169 | // ---------------------------------------------------------------------------- | |
170 | // constants | |
171 | // ---------------------------------------------------------------------------- | |
172 | ||
173 | // IDs for the controls and the menu commands | |
174 | enum | |
175 | { | |
176 | // menu items | |
177 | VScroll_Quit = 1, | |
178 | ||
179 | // it is important for the id corresponding to the "About" command to have | |
180 | // this standard value as otherwise it won't be handled properly under Mac | |
181 | // (where it is special and put into the "Apple" menu) | |
182 | VScroll_About = wxID_ABOUT | |
183 | }; | |
184 | ||
185 | // ---------------------------------------------------------------------------- | |
be5a51fb | 186 | // event tables and other macros for wxWidgets |
cf7d6329 VZ |
187 | // ---------------------------------------------------------------------------- |
188 | ||
be5a51fb | 189 | // the event tables connect the wxWidgets events with the functions (event |
cf7d6329 VZ |
190 | // handlers) which process them. It can be also done at run-time, but for the |
191 | // simple menu events like this the static method is much simpler. | |
192 | BEGIN_EVENT_TABLE(VScrollFrame, wxFrame) | |
193 | EVT_MENU(VScroll_Quit, VScrollFrame::OnQuit) | |
194 | EVT_MENU(VScroll_About, VScrollFrame::OnAbout) | |
195 | EVT_SIZE(VScrollFrame::OnSize) | |
196 | END_EVENT_TABLE() | |
197 | ||
be5a51fb | 198 | // Create a new application object: this macro will allow wxWidgets to create |
cf7d6329 VZ |
199 | // the application object during program execution (it's better than using a |
200 | // static object for many reasons) and also declares the accessor function | |
201 | // wxGetApp() which will return the reference of the right type (i.e. VScrollApp and | |
202 | // not wxApp) | |
203 | IMPLEMENT_APP(VScrollApp) | |
204 | ||
205 | // ============================================================================ | |
206 | // implementation | |
207 | // ============================================================================ | |
208 | ||
209 | // ---------------------------------------------------------------------------- | |
210 | // the application class | |
211 | // ---------------------------------------------------------------------------- | |
212 | ||
213 | // 'Main program' equivalent: the program execution "starts" here | |
214 | bool VScrollApp::OnInit() | |
215 | { | |
216 | // create the main application window | |
217 | VScrollFrame *frame = new VScrollFrame; | |
218 | ||
219 | // and show it (the frames, unlike simple controls, are not shown when | |
220 | // created initially) | |
540a08d2 | 221 | frame->Show(true); |
cf7d6329 VZ |
222 | |
223 | // ok | |
540a08d2 | 224 | return true; |
cf7d6329 VZ |
225 | } |
226 | ||
227 | // ---------------------------------------------------------------------------- | |
228 | // main frame | |
229 | // ---------------------------------------------------------------------------- | |
230 | ||
231 | // frame constructor | |
232 | VScrollFrame::VScrollFrame() | |
233 | : wxFrame(NULL, | |
540a08d2 | 234 | wxID_ANY, |
be5a51fb | 235 | _T("VScroll wxWidgets Sample"), |
cf7d6329 VZ |
236 | wxDefaultPosition, |
237 | wxSize(400, 350)) | |
238 | { | |
239 | // set the frame icon | |
240 | SetIcon(wxICON(mondrian)); | |
241 | ||
242 | #if wxUSE_MENUS | |
243 | // create a menu bar | |
244 | wxMenu *menuFile = new wxMenu; | |
245 | ||
246 | // the "About" item should be in the help menu | |
247 | wxMenu *menuHelp = new wxMenu; | |
248 | menuHelp->Append(VScroll_About, _T("&About...\tF1"), _T("Show about dialog")); | |
249 | ||
250 | menuFile->Append(VScroll_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); | |
251 | ||
252 | // now append the freshly created menu to the menu bar... | |
253 | wxMenuBar *menuBar = new wxMenuBar; | |
254 | menuBar->Append(menuFile, _T("&File")); | |
255 | menuBar->Append(menuHelp, _T("&Help")); | |
256 | ||
257 | // ... and attach this menu bar to the frame | |
258 | SetMenuBar(menuBar); | |
259 | #endif // wxUSE_MENUS | |
260 | ||
261 | #if wxUSE_STATUSBAR | |
262 | // create a status bar just for fun (by default with 1 pane only) | |
263 | CreateStatusBar(2); | |
be5a51fb | 264 | SetStatusText(_T("Welcome to wxWidgets!")); |
cf7d6329 VZ |
265 | #endif // wxUSE_STATUSBAR |
266 | ||
267 | // create our one and only child -- it will take our entire client area | |
268 | new VScrollWindow(this); | |
269 | } | |
270 | ||
271 | // ---------------------------------------------------------------------------- | |
272 | // event handlers | |
273 | // ---------------------------------------------------------------------------- | |
274 | ||
275 | void VScrollFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
276 | { | |
540a08d2 WS |
277 | // true is to force the frame to close |
278 | Close(true); | |
cf7d6329 VZ |
279 | } |
280 | ||
281 | void VScrollFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
282 | { | |
283 | wxMessageBox(_T("VScroll shows how to implement scrolling with\n") | |
284 | _T("variable line heights.\n") | |
285 |