]>
git.saurik.com Git - wxWidgets.git/blob - samples/vscroll/vstest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: VScroll wxWindows sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWindows headers)
35 // we need to include the headers not included from wx/wx.h explicitly anyhow
36 #include "wx/vscroll.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // the application icon (under Windows and OS/2 it is in resources)
43 #if !defined(__WXMSW__) && !defined(__WXOS2__)
44 #include "mondrian.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // Define a new application type, each program should derive a class from wxApp
52 class VScrollApp
: public wxApp
55 // create our main window
56 virtual bool OnInit();
59 // Define a new frame type: this is going to be our main frame
60 class VScrollFrame
: public wxFrame
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent
& event
);
68 void OnAbout(wxCommandEvent
& event
);
70 void OnSize(wxSizeEvent
& event
)
72 // show current size in the status bar
74 if ( m_frameStatusBar
)
76 wxSize sz
= GetClientSize();
77 SetStatusText(wxString::Format(_T("%dx%d"), sz
.x
, sz
.y
), 1);
79 #endif // wxUSE_STATUSBAR
85 // any class wishing to process wxWindows events must use this macro
89 class VScrollWindow
: public wxVScrolledWindow
92 VScrollWindow(wxFrame
*frame
) : wxVScrolledWindow(frame
, -1)
101 void OnIdle(wxIdleEvent
&)
103 m_frame
->SetStatusText(wxString::Format
105 _T("Page size = %d, pos = %d, max = %d"),
106 GetScrollThumb(wxVERTICAL
),
107 GetScrollPos(wxVERTICAL
),
108 GetScrollRange(wxVERTICAL
)
113 void OnPaint(wxPaintEvent
&)
117 dc
.SetPen(*wxBLACK_DASHED_PEN
);
119 const size_t lineFirst
= GetFirstVisibleLine(),
120 lineLast
= GetLastVisibleLine();
122 const wxCoord hText
= dc
.GetCharHeight();
125 for ( size_t line
= lineFirst
; line
<= lineLast
; line
++ )
127 dc
.DrawLine(0, y
, 1000, y
);
129 wxCoord hLine
= OnGetLineHeight(line
);
130 dc
.DrawText(wxString::Format(_T("Line %lu"), (unsigned long)line
),
131 0, y
+ (hLine
- hText
) / 2);
134 dc
.DrawLine(0, y
, 1000, y
);
138 void OnScroll(wxScrollWinEvent
& event
)
146 virtual wxCoord
OnGetLineHeight(size_t n
) const
148 wxASSERT( n
< GetLineCount() );
150 return n
% 2 ? 15 : 30; // 15 + 2*n
158 DECLARE_EVENT_TABLE()
161 BEGIN_EVENT_TABLE(VScrollWindow
, wxVScrolledWindow
)
162 EVT_IDLE(VScrollWindow::OnIdle
)
163 EVT_PAINT(VScrollWindow::OnPaint
)
164 EVT_SCROLLWIN(VScrollWindow::OnScroll
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 // IDs for the controls and the menu commands
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
183 // ----------------------------------------------------------------------------
184 // event tables and other macros for wxWindows
185 // ----------------------------------------------------------------------------
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
)
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
201 IMPLEMENT_APP(VScrollApp
)
203 // ============================================================================
205 // ============================================================================
207 // ----------------------------------------------------------------------------
208 // the application class
209 // ----------------------------------------------------------------------------
211 // 'Main program' equivalent: the program execution "starts" here
212 bool VScrollApp::OnInit()
214 // create the main application window
215 VScrollFrame
*frame
= new VScrollFrame
;
217 // and show it (the frames, unlike simple controls, are not shown when
218 // created initially)
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
230 VScrollFrame::VScrollFrame()
233 _T("VScroll wxWindows Sample"),
237 // set the frame icon
238 SetIcon(wxICON(mondrian
));
242 wxMenu
*menuFile
= new wxMenu
;
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"));
248 menuFile
->Append(VScroll_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
255 // ... and attach this menu bar to the frame
257 #endif // wxUSE_MENUS
260 // create a status bar just for fun (by default with 1 pane only)
262 SetStatusText(_T("Welcome to wxWindows!"));
263 #endif // wxUSE_STATUSBAR
265 // create our one and only child -- it will take our entire client area
266 new VScrollWindow(this);
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 void VScrollFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
275 // TRUE is to force the frame to close
279 void VScrollFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
281 wxMessageBox(_T("VScroll shows how to implement scrolling with\n")
282 _T("variable line heights.\n")
283 _T("© 2003 Vadim Zeitlin"),
285 wxOK
| wxICON_INFORMATION
,