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