]>
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)
34 // we need to include the headers not included from wx/wx.h explicitly anyhow
35 #include "wx/vscroll.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // the application icon (under Windows and OS/2 it is in resources)
42 #if !defined(__WXMSW__) && !defined(__WXOS2__)
43 #include "mondrian.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // Define a new application type, each program should derive a class from wxApp
51 class VScrollApp
: public wxApp
54 // create our main window
55 virtual bool OnInit();
58 // Define a new frame type: this is going to be our main frame
59 class VScrollFrame
: public wxFrame
65 // event handlers (these functions should _not_ be virtual)
66 void OnQuit(wxCommandEvent
& event
);
67 void OnAbout(wxCommandEvent
& event
);
69 void OnSize(wxSizeEvent
& event
)
71 // show current size in the status bar
73 if ( m_frameStatusBar
)
75 wxSize sz
= GetClientSize();
76 SetStatusText(wxString::Format("%dx%d", sz
.x
, sz
.y
), 1);
78 #endif // wxUSE_STATUSBAR
84 // any class wishing to process wxWindows events must use this macro
88 class VScrollWindow
: public wxVScrolledWindow
91 VScrollWindow(wxFrame
*frame
) : wxVScrolledWindow(frame
, -1)
100 void OnIdle(wxIdleEvent
&)
102 m_frame
->SetStatusText(wxString::Format
104 "Page size = %d, pos = %d, max = %d",
105 GetScrollThumb(wxVERTICAL
),
106 GetScrollPos(wxVERTICAL
),
107 GetScrollRange(wxVERTICAL
)
112 void OnPaint(wxPaintEvent
&)
116 dc
.SetPen(*wxBLACK_DASHED_PEN
);
118 const size_t lineFirst
= GetFirstVisibleLine(),
119 lineLast
= GetLastVisibleLine();
121 const wxCoord hText
= dc
.GetCharHeight();
124 for ( size_t line
= lineFirst
; line
<= lineLast
; line
++ )
126 dc
.DrawLine(0, y
, 1000, y
);
128 wxCoord hLine
= OnGetLineHeight(line
);
129 dc
.DrawText(wxString::Format(_T("Line %lu"), (unsigned long)line
),
130 0, y
+ (hLine
- hText
) / 2);
133 dc
.DrawLine(0, y
, 1000, y
);
137 void OnScroll(wxScrollWinEvent
& event
)
145 virtual wxCoord
OnGetLineHeight(size_t n
) const
147 wxASSERT( n
< GetLineCount() );
149 return n
% 2 ? 15 : 30; // 15 + 2*n
157 DECLARE_EVENT_TABLE()
160 BEGIN_EVENT_TABLE(VScrollWindow
, wxVScrolledWindow
)
161 EVT_IDLE(VScrollWindow::OnIdle
)
162 EVT_PAINT(VScrollWindow::OnPaint
)
163 EVT_SCROLLWIN(VScrollWindow::OnScroll
)
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 // IDs for the controls and the menu commands
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
182 // ----------------------------------------------------------------------------
183 // event tables and other macros for wxWindows
184 // ----------------------------------------------------------------------------
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
)
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
200 IMPLEMENT_APP(VScrollApp
)
202 // ============================================================================
204 // ============================================================================
206 // ----------------------------------------------------------------------------
207 // the application class
208 // ----------------------------------------------------------------------------
210 // 'Main program' equivalent: the program execution "starts" here
211 bool VScrollApp::OnInit()
213 // create the main application window
214 VScrollFrame
*frame
= new VScrollFrame
;
216 // and show it (the frames, unlike simple controls, are not shown when
217 // created initially)
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
229 VScrollFrame::VScrollFrame()
232 _T("VScroll wxWindows Sample"),
236 // set the frame icon
237 SetIcon(wxICON(mondrian
));
241 wxMenu
*menuFile
= new wxMenu
;
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"));
247 menuFile
->Append(VScroll_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
254 // ... and attach this menu bar to the frame
256 #endif // wxUSE_MENUS
259 // create a status bar just for fun (by default with 1 pane only)
261 SetStatusText(_T("Welcome to wxWindows!"));
262 #endif // wxUSE_STATUSBAR
264 // create our one and only child -- it will take our entire client area
265 new VScrollWindow(this);
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 void VScrollFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
274 // TRUE is to force the frame to close
278 void VScrollFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
280 wxMessageBox(_T("VScroll shows how to implement scrolling with\n")
281 _T("variable line heights.\n")
282 _T("© 2003 Vadim Zeitlin"),
284 wxOK
| wxICON_INFORMATION
,