]> git.saurik.com Git - wxWidgets.git/blame - samples/vscroll/vstest.cpp
Add a very simple example of a Win32 application using wx DLL.
[wxWidgets.git] / samples / vscroll / vstest.cpp
CommitLineData
cf7d6329 1/////////////////////////////////////////////////////////////////////////////
cb7d7375 2// Name: samples/vscroll/vstest.cpp
be5a51fb 3// Purpose: VScroll wxWidgets sample
cf7d6329 4// Author: Vadim Zeitlin
f18eaf26 5// Modified by: Brad Anderson
cf7d6329
VZ
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)
cb7d7375 43#if !defined(__WXMSW__) && !defined(__WXPM__)
96ed08f7 44 #include "../sample.xpm"
cf7d6329
VZ
45#endif
46
f18eaf26
VZ
47// ----------------------------------------------------------------------------
48// definitions
49// ----------------------------------------------------------------------------
50
51#define MAX_LINES 10000
52
cf7d6329
VZ
53// ----------------------------------------------------------------------------
54// private classes
55// ----------------------------------------------------------------------------
56
57// Define a new application type, each program should derive a class from wxApp
f18eaf26 58class VarScrollApp : public wxApp
cf7d6329
VZ
59{
60public:
61 // create our main window
62 virtual bool OnInit();
63};
64
65// Define a new frame type: this is going to be our main frame
f18eaf26 66class VarScrollFrame : public wxFrame
cf7d6329
VZ
67{
68public:
69 // ctor
f18eaf26 70 VarScrollFrame();
cf7d6329
VZ
71
72 // event handlers (these functions should _not_ be virtual)
73 void OnQuit(wxCommandEvent& event);
f18eaf26
VZ
74 void OnModeVScroll(wxCommandEvent& event);
75 void OnModeHScroll(wxCommandEvent& event);
76 void OnModeHVScroll(wxCommandEvent& event);
cf7d6329
VZ
77 void OnAbout(wxCommandEvent& event);
78
79 void OnSize(wxSizeEvent& event)
80 {
81 // show current size in the status bar
82#if wxUSE_STATUSBAR
83 if ( m_frameStatusBar )
84 {
85 wxSize sz = GetClientSize();
9a83f860 86 SetStatusText(wxString::Format(wxT("%dx%d"), sz.x, sz.y), 1);
cf7d6329
VZ
87 }
88#endif // wxUSE_STATUSBAR
89
90 event.Skip();
91 }
92
93private:
f18eaf26
VZ
94 // either a wxVScrolledWindow or a wxHVScrolled window, depending on current mode
95 wxPanel *m_scrollWindow;
96
be5a51fb 97 // any class wishing to process wxWidgets events must use this macro
cf7d6329
VZ
98 DECLARE_EVENT_TABLE()
99};
100
101class VScrollWindow : public wxVScrolledWindow
102{
103public:
540a08d2 104 VScrollWindow(wxFrame *frame) : wxVScrolledWindow(frame, wxID_ANY)
cf7d6329
VZ
105 {
106 m_frame = frame;
107
e02c72fa 108 SetRowCount(MAX_LINES);
f18eaf26
VZ
109
110 int i;
111 for ( i = 0; i < MAX_LINES; ++i )
112 m_heights[i] = rand()%25+16; // low: 16; high: 40
cf7d6329
VZ
113
114 m_changed = true;
115 }
116
117 void OnIdle(wxIdleEvent&)
118 {
8520f137 119#if wxUSE_STATUSBAR
cf7d6329
VZ
120 m_frame->SetStatusText(wxString::Format
121 (
9a83f860 122 wxT("Page size = %d, pos = %d, max = %d"),
cf7d6329
VZ
123 GetScrollThumb(wxVERTICAL),
124 GetScrollPos(wxVERTICAL),
125 GetScrollRange(wxVERTICAL)
126 ));
8520f137 127#endif // wxUSE_STATUSBAR
cf7d6329
VZ
128 m_changed = false;
129 }
130
131 void OnPaint(wxPaintEvent&)
132 {
133 wxPaintDC dc(this);
134
f18eaf26 135 dc.SetPen(*wxBLACK_PEN);
cf7d6329 136
f18eaf26
VZ
137 const size_t lineFirst = GetVisibleBegin(),
138 lineLast = GetVisibleEnd();
cf7d6329
VZ
139
140 const wxCoord hText = dc.GetCharHeight();
141
f18eaf26
VZ
142 wxSize clientSize = GetClientSize();
143
cf7d6329 144 wxCoord y = 0;
f18eaf26 145 for ( size_t line = lineFirst; line < lineLast; line++ )
cf7d6329 146 {
f18eaf26 147 dc.DrawLine(0, y, clientSize.GetWidth(), y);
cf7d6329 148
e02c72fa 149 wxCoord hLine = OnGetRowHeight(line);
9a83f860 150 dc.DrawText(wxString::Format(wxT("Line %lu"), (unsigned long)line),
f18eaf26 151 2, y + (hLine - hText) / 2);
cf7d6329
VZ
152
153 y += hLine;
154 dc.DrawLine(0, y, 1000, y);
155 }
156 }
157
158 void OnScroll(wxScrollWinEvent& event)
159 {
160 m_changed = true;
161
162 event.Skip();
163 }
164
f18eaf26
VZ
165 void OnMouse(wxMouseEvent& event)
166 {
167 if(event.LeftDown())
168 CaptureMouse();
169 else if(event.LeftUp())
170 ReleaseMouse();
171 event.Skip();
172 }
cf7d6329 173
e02c72fa 174 virtual wxCoord OnGetRowHeight(size_t n) const
cf7d6329 175 {
e02c72fa 176 wxASSERT( n < GetRowCount() );
cf7d6329 177
f18eaf26 178 return m_heights[n];
cf7d6329
VZ
179 }
180
181private:
182 wxFrame *m_frame;
183
f18eaf26
VZ
184 int m_heights[MAX_LINES];
185
cf7d6329
VZ
186 bool m_changed;
187
188 DECLARE_EVENT_TABLE()
189};
190
191BEGIN_EVENT_TABLE(VScrollWindow, wxVScrolledWindow)
192 EVT_IDLE(VScrollWindow::OnIdle)
193 EVT_PAINT(VScrollWindow::OnPaint)
194 EVT_SCROLLWIN(VScrollWindow::OnScroll)
f18eaf26
VZ
195 EVT_MOUSE_EVENTS(VScrollWindow::OnMouse)
196END_EVENT_TABLE()
197
198class HScrollWindow : public wxHScrolledWindow
199{
200public:
201 HScrollWindow(wxFrame *frame) : wxHScrolledWindow(frame, wxID_ANY)
202 {
203 m_frame = frame;
204
205 SetColumnCount(MAX_LINES);
206
207 int i;
208 for ( i = 0; i < MAX_LINES; ++i )
209 m_heights[i] = rand()%25+16; // low: 15; high: 40
210
211 m_changed = true;
212 }
213
214 void OnIdle(wxIdleEvent&)
215 {
216#if wxUSE_STATUSBAR
217 m_frame->SetStatusText(wxString::Format
218 (
9a83f860 219 wxT("Page size = %d, pos = %d, max = %d"),
f18eaf26
VZ
220 GetScrollThumb(wxVERTICAL),
221 GetScrollPos(wxVERTICAL),
222 GetScrollRange(wxVERTICAL)
223 ));
224#endif // wxUSE_STATUSBAR
225 m_changed = false;
226 }
227
228 void OnPaint(wxPaintEvent&)
229 {
230 wxPaintDC dc(this);
231
232 dc.SetPen(*wxBLACK_PEN);
233
234 const size_t lineFirst = GetVisibleBegin(),
235 lineLast = GetVisibleEnd();
236
237 const wxCoord hText = dc.GetCharHeight();
238
239 wxSize clientSize = GetClientSize();
240
241 wxCoord x = 0;
242 for ( size_t line = lineFirst; line < lineLast; line++ )
243 {
244 dc.DrawLine(x, 0, x, clientSize.GetHeight());
245
246 wxCoord wLine = OnGetColumnWidth(line);
9a83f860 247 dc.DrawRotatedText(wxString::Format(wxT("Line %lu"), (unsigned long)line),
f18eaf26
VZ
248 x + (wLine - hText) / 2, clientSize.GetHeight() - 5, 90);
249
250 x += wLine;
251 dc.DrawLine(x, 0, x, 1000);
252 }
253 }
254
255 void OnScroll(wxScrollWinEvent& event)
256 {
257 m_changed = true;
258
259 event.Skip();
260 }
261
262 void OnMouse(wxMouseEvent& event)
263 {
264 if(event.LeftDown())
265 CaptureMouse();
266 else if(event.LeftUp())
267 ReleaseMouse();
268 event.Skip();
269 }
270
271 virtual wxCoord OnGetColumnWidth(size_t n) const
272 {
273 wxASSERT( n < GetColumnCount() );
274
275 return m_heights[n];
276 }
277
278private:
279 wxFrame *m_frame;
280
281 int m_heights[MAX_LINES];
282
283 bool m_changed;
284
285 DECLARE_EVENT_TABLE()
286};
287
288BEGIN_EVENT_TABLE(HScrollWindow, wxHScrolledWindow)
289 EVT_IDLE(HScrollWindow::OnIdle)
290 EVT_PAINT(HScrollWindow::OnPaint)
291 EVT_SCROLLWIN(HScrollWindow::OnScroll)
292 EVT_MOUSE_EVENTS(HScrollWindow::OnMouse)
293END_EVENT_TABLE()
294
295class HVScrollWindow : public wxHVScrolledWindow
296{
297public:
298 HVScrollWindow(wxFrame *frame) : wxHVScrolledWindow(frame, wxID_ANY)
299 {
300 m_frame = frame;
301
302 SetRowColumnCount(MAX_LINES, MAX_LINES);
e02c72fa 303
f18eaf26
VZ
304 int i;
305 for ( i = 0; i < MAX_LINES; ++i )
306 {
307 m_heights[i] = rand()%30+31; // low: 30; high: 60
308 m_widths[i] = rand()%30+61; // low: 60; high: 90
309 }
310
311 m_changed = true;
312 }
313
314 void OnIdle(wxIdleEvent&)
315 {
316#if wxUSE_STATUSBAR
317 m_frame->SetStatusText(wxString::Format
318 (
9a83f860 319 wxT("Page size = %d rows %d columns; pos = row: %d, column: %d; max = %d rows, %d columns"),
f18eaf26
VZ
320 GetScrollThumb(wxVERTICAL),
321 GetScrollThumb(wxHORIZONTAL),
322 GetScrollPos(wxVERTICAL),
323 GetScrollPos(wxHORIZONTAL),
324 GetScrollRange(wxVERTICAL),
325 GetScrollRange(wxHORIZONTAL)
326 ));
327#endif // wxUSE_STATUSBAR
328 m_changed = false;
329 }
330
331 void OnPaint(wxPaintEvent&)
332 {
333 wxPaintDC dc(this);
334
335 dc.SetPen(*wxBLACK_PEN);
336
337 const size_t rowFirst = GetVisibleRowsBegin(),
338 rowLast = GetVisibleRowsEnd();
339 const size_t columnFirst = GetVisibleColumnsBegin(),
340 columnLast = GetVisibleColumnsEnd();
e02c72fa 341
f18eaf26
VZ
342 const wxCoord hText = dc.GetCharHeight();
343
344 wxSize clientSize = GetClientSize();
345
346 wxCoord y = 0;
347 wxCoord x = 0;
348 for ( size_t row = rowFirst; row < rowLast; row++ )
349 {
350 wxCoord rowHeight = OnGetRowHeight(row);
351 dc.DrawLine(0, y, clientSize.GetWidth(), y);
352
353 x = 0;
354 for ( size_t col = columnFirst; col < columnLast; col++ )
355 {
356 wxCoord colWidth = OnGetColumnWidth(col);
357
358 if ( row == rowFirst )
359 dc.DrawLine(x, 0, x, clientSize.GetHeight());
360
9a83f860 361 dc.DrawText(wxString::Format(wxT("Row %lu"), (unsigned long)row),
f18eaf26 362 x + 2, y + rowHeight / 2 - hText);
9a83f860 363 dc.DrawText(wxString::Format(wxT("Col %lu"), (unsigned long)col),
f18eaf26 364 x + 2, y + rowHeight / 2);
e02c72fa 365
f18eaf26
VZ
366 x += colWidth;
367 if ( row == rowFirst)
368 dc.DrawLine(x, 0, x, clientSize.GetHeight());
369 }
e02c72fa 370
f18eaf26
VZ
371 y += rowHeight;
372 dc.DrawLine(0, y, clientSize.GetWidth(), y);
373 }
374 }
375
376 void OnScroll(wxScrollWinEvent& event)
377 {
378 m_changed = true;
379
380 event.Skip();
381 }
382
383 void OnMouse(wxMouseEvent& event)
384 {
385 if(event.LeftDown())
386 CaptureMouse();
387 else if(event.LeftUp())
388 ReleaseMouse();
389 event.Skip();
390 }
391
392 virtual wxCoord OnGetRowHeight(size_t n) const
393 {
394 wxASSERT( n < GetRowCount() );
395
396 return m_heights[n];
397 }
398
399 virtual wxCoord OnGetColumnWidth(size_t n) const
400 {
401 wxASSERT( n < GetColumnCount() );
402
403 return m_widths[n];
404 }
405
406private:
407 wxFrame *m_frame;
408
409 int m_heights[MAX_LINES];
410 int m_widths[MAX_LINES];
411
412 bool m_changed;
413
414 DECLARE_EVENT_TABLE()
415};
416
417BEGIN_EVENT_TABLE(HVScrollWindow, wxHVScrolledWindow)
418 EVT_IDLE(HVScrollWindow::OnIdle)
419 EVT_PAINT(HVScrollWindow::OnPaint)
420 EVT_SCROLLWIN(HVScrollWindow::OnScroll)
421 EVT_MOUSE_EVENTS(HVScrollWindow::OnMouse)
cf7d6329
VZ
422END_EVENT_TABLE()
423
424// ----------------------------------------------------------------------------
425// constants
426// ----------------------------------------------------------------------------
427
428// IDs for the controls and the menu commands
429enum
430{
431 // menu items
91b07357 432 VScroll_Quit = wxID_EXIT,
cf7d6329
VZ
433
434 // it is important for the id corresponding to the "About" command to have
435 // this standard value as otherwise it won't be handled properly under Mac
436 // (where it is special and put into the "Apple" menu)
f18eaf26
VZ
437 VScroll_About = wxID_ABOUT,
438
439 VScroll_VScrollMode = wxID_HIGHEST + 1,
440 VScroll_HScrollMode,
441 VScroll_HVScrollMode
cf7d6329
VZ
442};
443
444// ----------------------------------------------------------------------------
be5a51fb 445// event tables and other macros for wxWidgets
cf7d6329
VZ
446// ----------------------------------------------------------------------------
447
be5a51fb 448// the event tables connect the wxWidgets events with the functions (event
cf7d6329
VZ
449// handlers) which process them. It can be also done at run-time, but for the
450// simple menu events like this the static method is much simpler.
f18eaf26
VZ
451BEGIN_EVENT_TABLE(VarScrollFrame, wxFrame)
452 EVT_MENU(VScroll_Quit, VarScrollFrame::OnQuit)
453 EVT_MENU(VScroll_VScrollMode, VarScrollFrame::OnModeVScroll)
454 EVT_MENU(VScroll_HScrollMode, VarScrollFrame::OnModeHScroll)
455 EVT_MENU(VScroll_HVScrollMode, VarScrollFrame::OnModeHVScroll)
456 EVT_MENU(VScroll_About, VarScrollFrame::OnAbout)
457 EVT_SIZE(VarScrollFrame::OnSize)
cf7d6329
VZ
458END_EVENT_TABLE()
459
be5a51fb 460// Create a new application object: this macro will allow wxWidgets to create
cf7d6329
VZ
461// the application object during program execution (it's better than using a
462// static object for many reasons) and also declares the accessor function
f18eaf26 463// wxGetApp() which will return the reference of the right type (i.e. VarScrollApp and
cf7d6329 464// not wxApp)
f18eaf26 465IMPLEMENT_APP(VarScrollApp)
cf7d6329
VZ
466
467// ============================================================================
468// implementation
469// ============================================================================
470
471// ----------------------------------------------------------------------------
472// the application class
473// ----------------------------------------------------------------------------
474
475// 'Main program' equivalent: the program execution "starts" here
f18eaf26 476bool VarScrollApp::OnInit()
cf7d6329 477{
45e6e6f8
VZ
478 if ( !wxApp::OnInit() )
479 return false;
480
cf7d6329 481 // create the main application window
f18eaf26 482 VarScrollFrame *frame = new VarScrollFrame;
cf7d6329
VZ
483
484 // and show it (the frames, unlike simple controls, are not shown when
485 // created initially)
540a08d2 486 frame->Show(true);
cf7d6329
VZ
487
488 // ok
540a08d2 489 return true;
cf7d6329
VZ
490}
491
492// ----------------------------------------------------------------------------
493// main frame
494// ----------------------------------------------------------------------------
495
496// frame constructor
f18eaf26
VZ
497VarScrollFrame::VarScrollFrame()
498 : wxFrame(NULL,
499 wxID_ANY,
9a83f860 500 wxT("VScroll wxWidgets Sample"),
f18eaf26
VZ
501 wxDefaultPosition,
502 wxSize(400, 350)),
503 m_scrollWindow(NULL)
cf7d6329
VZ
504{
505 // set the frame icon
96ed08f7 506 SetIcon(wxICON(sample));
cf7d6329
VZ
507
508#if wxUSE_MENUS
509 // create a menu bar
510 wxMenu *menuFile = new wxMenu;
511
f18eaf26
VZ
512 wxMenu *menuMode = new wxMenu;
513
cf7d6329
VZ
514 // the "About" item should be in the help menu
515 wxMenu *menuHelp = new wxMenu;
9a83f860 516 menuHelp->Append(VScroll_About, wxT("&About...\tF1"), wxT("Show about dialog"));
cf7d6329 517
f18eaf26 518#ifdef wxHAS_RADIO_MENU_ITEMS
9a83f860
VZ
519 menuMode->AppendRadioItem(VScroll_VScrollMode, wxT("&Vertical\tAlt-V"),
520 wxT("Vertical scrolling only"));
521 menuMode->AppendRadioItem(VScroll_HScrollMode, wxT("&Horizontal\tAlt-H"),
522 wxT("Horizontal scrolling only"));
f18eaf26 523 menuMode->AppendRadioItem(VScroll_HVScrollMode,
9a83f860
VZ
524 wxT("Hori&zontal/Vertical\tAlt-Z"),
525 wxT("Horizontal and vertical scrolling"));
f18eaf26
VZ
526 menuMode->Check(VScroll_VScrollMode, true);
527#else
9a83f860
VZ
528 menuMode->Append(VScroll_VScrollMode, wxT("&Vertical\tAlt-V"),
529 wxT("Vertical scrolling only"));
530 menuMode->Append(VScroll_HScrollMode, wxT("&Horizontal\tAlt-H"),
531 wxT("Horizontal scrolling only"));
532 menuMode->Append(VScroll_HVScrollMode, wxT("Hori&zontal/Vertical\tAlt-Z"),
533 wxT("Horizontal and vertical scrolling"));
f18eaf26
VZ
534#endif
535
9a83f860 536 menuFile->Append(VScroll_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
cf7d6329
VZ
537
538 // now append the freshly created menu to the menu bar...
539 wxMenuBar *menuBar = new wxMenuBar;
9a83f860
VZ
540 menuBar->Append(menuFile, wxT("&File"));
541 menuBar->Append(menuMode, wxT("&Mode"));
542 menuBar->Append(menuHelp, wxT("&Help"));
cf7d6329
VZ
543
544 // ... and attach this menu bar to the frame
545 SetMenuBar(menuBar);
546#endif // wxUSE_MENUS
547
548#if wxUSE_STATUSBAR
549 // create a status bar just for fun (by default with 1 pane only)
550 CreateStatusBar(2);
9a83f860 551 SetStatusText(wxT("Welcome to wxWidgets!"));
f18eaf26
VZ
552 int widths[2];
553 widths[0] = -1;
554 widths[1] = 100;
555 SetStatusWidths(2, widths);
cf7d6329
VZ
556#endif // wxUSE_STATUSBAR
557
558 // create our one and only child -- it will take our entire client area
f18eaf26
VZ
559 if ( menuMode->IsChecked(VScroll_VScrollMode) )
560 m_scrollWindow = new VScrollWindow(this);
561 else if ( menuMode->IsChecked(VScroll_HScrollMode) )
562 m_scrollWindow = new HScrollWindow(this);
563 else
564 m_scrollWindow = new HVScrollWindow(this);
cf7d6329
VZ
565}
566
567// ----------------------------------------------------------------------------
568// event handlers
569// ----------------------------------------------------------------------------
570
f18eaf26 571void VarScrollFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
cf7d6329 572{
540a08d2
WS
573 // true is to force the frame to close
574 Close(true);
cf7d6329
VZ
575}
576
f18eaf26
VZ
577void VarScrollFrame::OnModeVScroll(wxCommandEvent& WXUNUSED(event))
578{
579 if ( m_scrollWindow )
580 m_scrollWindow->Destroy();
e02c72fa 581
f18eaf26
VZ
582 m_scrollWindow = new VScrollWindow(this);
583 SendSizeEvent();
584}
585
586void VarScrollFrame::OnModeHScroll(wxCommandEvent& WXUNUSED(event))
587{
588 if ( m_scrollWindow )
589 m_scrollWindow->Destroy();
e02c72fa 590
f18eaf26
VZ
591 m_scrollWindow = new HScrollWindow(this);
592 SendSizeEvent();
593}
594
595void VarScrollFrame::OnModeHVScroll(wxCommandEvent& WXUNUSED(event))
596{
597 if ( m_scrollWindow )
598 m_scrollWindow->Destroy();
599
600 m_scrollWindow = new HVScrollWindow(this);
601 SendSizeEvent();
602}
603
604void VarScrollFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
cf7d6329 605{
9a83f860
VZ
606 wxMessageBox(wxT("VScroll shows how to implement scrolling with\n")
607 wxT("variable line widths and heights.\n")
608 wxT("(c) 2003 Vadim Zeitlin"),
609 wxT("About VScroll"),
cf7d6329
VZ
610 wxOK | wxICON_INFORMATION,
611 this);
612}