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