1/////////////////////////////////////////////////////////////////////////////
2// Name:        samples/vscroll/vstest.cpp
3// Purpose:     VScroll wxWidgets sample
4// Author:      Vadim Zeitlin
5// Modified by:
6// Created:     04/01/98
7// RCS-ID:      $Id: vstest.cpp 38728 2006-04-14 22:03:07Z VZ $
8// Copyright:   (c) 2003 Vadim Zeitlin <vadim@wxwidgets.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" wxWidgets 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(__WXPM__)
44    #include "../sample.xpm"
45#endif
46
47// ----------------------------------------------------------------------------
48// private classes
49// ----------------------------------------------------------------------------
50
51// Define a new application type, each program should derive a class from wxApp
52class VScrollApp : public wxApp
53{
54public:
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
60class VScrollFrame : public wxFrame
61{
62public:
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
84private:
85    // any class wishing to process wxWidgets events must use this macro
86    DECLARE_EVENT_TABLE()
87};
88
89class VScrollWindow : public wxVScrolledWindow
90{
91public:
92    VScrollWindow(wxFrame *frame) : wxVScrolledWindow(frame, wxID_ANY)
93    {
94        m_frame = frame;
95
96        SetLineCount(200);
97
98        m_changed = true;
99    }
100
101    void OnIdle(wxIdleEvent&)
102    {
103#if wxUSE_STATUSBAR
104        m_frame->SetStatusText(wxString::Format
105                               (
106                                    _T("Page size = %d, pos = %d, max = %d"),
107                                    GetScrollThumb(wxVERTICAL),
108                                    GetScrollPos(wxVERTICAL),
109                                    GetScrollRange(wxVERTICAL)
110                               ));
111#endif // wxUSE_STATUSBAR
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
155private:
156    wxFrame *m_frame;
157
158    bool m_changed;
159
160    DECLARE_EVENT_TABLE()
161};
162
163BEGIN_EVENT_TABLE(VScrollWindow, wxVScrolledWindow)
164    EVT_IDLE(VScrollWindow::OnIdle)
165    EVT_PAINT(VScrollWindow::OnPaint)
166    EVT_SCROLLWIN(VScrollWindow::OnScroll)
167END_EVENT_TABLE()
168
169// ----------------------------------------------------------------------------
170// constants
171// ----------------------------------------------------------------------------
172
173// IDs for the controls and the menu commands
174enum
175{
176    // menu items
177    VScroll_Quit = wxID_EXIT,
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// ----------------------------------------------------------------------------
186// event tables and other macros for wxWidgets
187// ----------------------------------------------------------------------------
188
189// the event tables connect the wxWidgets events with the functions (event
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.
192BEGIN_EVENT_TABLE(VScrollFrame, wxFrame)
193    EVT_MENU(VScroll_Quit,  VScrollFrame::OnQuit)
194    EVT_MENU(VScroll_About, VScrollFrame::OnAbout)
195    EVT_SIZE(VScrollFrame::OnSize)
196END_EVENT_TABLE()
197
198// Create a new application object: this macro will allow wxWidgets to create
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)
203IMPLEMENT_APP(VScrollApp)
204
205// ============================================================================
206// implementation
207// ============================================================================
208
209// ----------------------------------------------------------------------------
210// the application class
211// ----------------------------------------------------------------------------
212
213// 'Main program' equivalent: the program execution "starts" here
214bool 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)
221    frame->Show(true);
222
223    // ok
224    return true;
225}
226
227// ----------------------------------------------------------------------------
228// main frame
229// ----------------------------------------------------------------------------
230
231// frame constructor
232VScrollFrame::VScrollFrame()
233            : wxFrame(NULL,
234                      wxID_ANY,
235                      _T("VScroll wxWidgets Sample"),
236                      wxDefaultPosition,
237                      wxSize(400, 350))
238{
239    // set the frame icon
240    SetIcon(wxICON(sample));
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);
264    SetStatusText(_T("Welcome to wxWidgets!"));
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
275void VScrollFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
276{
277    // true is to force the frame to close
278    Close(true);
279}
280
281void VScrollFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
282{
283    wxMessageBox(_T("VScroll shows how to implement scrolling with\n")
284                 _T("variable line heights.\n")
285                 _T("(c) 2003 Vadim Zeitlin"),
286                 _T("About VScroll"),
287                 wxOK | wxICON_INFORMATION,
288                 this);
289}
290