]>
git.saurik.com Git - wxWidgets.git/blob - samples/vscroll/vstest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/vscroll/vstest.cpp
3 // Purpose: VScroll wxWidgets sample
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.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" wxWidgets 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(__WXPM__)
44 #include "../sample.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 wxWidgets events must use this macro
89 class VScrollWindow
: public wxVScrolledWindow
92 VScrollWindow(wxFrame
*frame
) : wxVScrolledWindow(frame
, wxID_ANY
)
101 void OnIdle(wxIdleEvent
&)
104 m_frame
->SetStatusText(wxString::Format
106 _T("Page size = %d, pos = %d, max = %d"),
107 GetScrollThumb(wxVERTICAL
),
108 GetScrollPos(wxVERTICAL
),
109 GetScrollRange(wxVERTICAL
)
111 #endif // wxUSE_STATUSBAR
115 void OnPaint(wxPaintEvent
&)
119 dc
.SetPen(*wxBLACK_DASHED_PEN
);
121 const size_t lineFirst
= GetFirstVisibleLine(),
122 lineLast
= GetLastVisibleLine();
124 const wxCoord hText
= dc
.GetCharHeight();
127 for ( size_t line
= lineFirst
; line
<= lineLast
; line
++ )
129 dc
.DrawLine(0, y
, 1000, y
);
131 wxCoord hLine
= OnGetLineHeight(line
);
132 dc
.DrawText(wxString::Format(_T("Line %lu"), (unsigned long)line
),
133 0, y
+ (hLine
- hText
) / 2);
136 dc
.DrawLine(0, y
, 1000, y
);
140 void OnScroll(wxScrollWinEvent
& event
)
148 virtual wxCoord
OnGetLineHeight(size_t n
) const
150 wxASSERT( n
< GetLineCount() );
152 return n
% 2 ? 15 : 30; // 15 + 2*n
160 DECLARE_EVENT_TABLE()
163 BEGIN_EVENT_TABLE(VScrollWindow
, wxVScrolledWindow
)
164 EVT_IDLE(VScrollWindow::OnIdle
)
165 EVT_PAINT(VScrollWindow::OnPaint
)
166 EVT_SCROLLWIN(VScrollWindow::OnScroll
)
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 // IDs for the controls and the menu commands
177 VScroll_Quit
= wxID_EXIT
,
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
185 // ----------------------------------------------------------------------------
186 // event tables and other macros for wxWidgets
187 // ----------------------------------------------------------------------------
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.
192 BEGIN_EVENT_TABLE(VScrollFrame
, wxFrame
)
193 EVT_MENU(VScroll_Quit
, VScrollFrame::OnQuit
)
194 EVT_MENU(VScroll_About
, VScrollFrame::OnAbout
)
195 EVT_SIZE(VScrollFrame::OnSize
)
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
203 IMPLEMENT_APP(VScrollApp
)
205 // ============================================================================
207 // ============================================================================
209 // ----------------------------------------------------------------------------
210 // the application class
211 // ----------------------------------------------------------------------------
213 // 'Main program' equivalent: the program execution "starts" here
214 bool VScrollApp::OnInit()
216 // create the main application window
217 VScrollFrame
*frame
= new VScrollFrame
;
219 // and show it (the frames, unlike simple controls, are not shown when
220 // created initially)
227 // ----------------------------------------------------------------------------
229 // ----------------------------------------------------------------------------
232 VScrollFrame::VScrollFrame()
235 _T("VScroll wxWidgets Sample"),
239 // set the frame icon
240 SetIcon(wxICON(sample
));
244 wxMenu
*menuFile
= new wxMenu
;
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"));
250 menuFile
->Append(VScroll_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
257 // ... and attach this menu bar to the frame
259 #endif // wxUSE_MENUS
262 // create a status bar just for fun (by default with 1 pane only)
264 SetStatusText(_T("Welcome to wxWidgets!"));
265 #endif // wxUSE_STATUSBAR
267 // create our one and only child -- it will take our entire client area
268 new VScrollWindow(this);
271 // ----------------------------------------------------------------------------
273 // ----------------------------------------------------------------------------
275 void VScrollFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
277 // true is to force the frame to close
281 void VScrollFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
283 wxMessageBox(_T("VScroll shows how to implement scrolling with\n")
284 _T("variable line heights.\n")
285 _T("(c) 2003 Vadim Zeitlin"),
287 wxOK
| wxICON_INFORMATION
,