]>
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
5 // Modified by: Brad Anderson
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 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // Define a new application type, each program should derive a class from wxApp
58 class VarScrollApp
: public wxApp
61 // create our main window
62 virtual bool OnInit();
65 // Define a new frame type: this is going to be our main frame
66 class VarScrollFrame
: public wxFrame
72 // event handlers (these functions should _not_ be virtual)
73 void OnQuit(wxCommandEvent
& event
);
74 void OnModeVScroll(wxCommandEvent
& event
);
75 void OnModeHVScroll(wxCommandEvent
& event
);
76 void OnAbout(wxCommandEvent
& event
);
78 void OnSize(wxSizeEvent
& event
)
80 // show current size in the status bar
82 if ( m_frameStatusBar
)
84 wxSize sz
= GetClientSize();
85 SetStatusText(wxString::Format(_T("%dx%d"), sz
.x
, sz
.y
), 1);
87 #endif // wxUSE_STATUSBAR
93 // any class wishing to process wxWidgets events must use this macro
96 // either a wxVScrolledWindow or a wxHVScrolled window, depending on current mode
97 wxPanel
*m_scrollWindow
;
100 class VScrollWindow
: public wxVScrolledWindow
103 VScrollWindow(wxFrame
*frame
) : wxVScrolledWindow(frame
, wxID_ANY
)
107 SetLineCount(MAX_LINES
);
110 for ( i
= 0; i
< MAX_LINES
; ++i
)
111 m_heights
[i
] = rand()%25
+16; // low: 15; high: 40
116 void OnIdle(wxIdleEvent
&)
119 m_frame
->SetStatusText(wxString::Format
121 _T("Page size = %d, pos = %d, max = %d"),
122 GetScrollThumb(wxVERTICAL
),
123 GetScrollPos(wxVERTICAL
),
124 GetScrollRange(wxVERTICAL
)
126 #endif // wxUSE_STATUSBAR
130 void OnPaint(wxPaintEvent
&)
134 dc
.SetPen(*wxBLACK_PEN
);
136 const size_t lineFirst
= GetVisibleBegin(),
137 lineLast
= GetVisibleEnd();
139 const wxCoord hText
= dc
.GetCharHeight();
141 wxSize clientSize
= GetClientSize();
144 for ( size_t line
= lineFirst
; line
< lineLast
; line
++ )
146 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
148 wxCoord hLine
= OnGetLineHeight(line
);
149 dc
.DrawText(wxString::Format(_T("Line %lu"), (unsigned long)line
),
150 2, y
+ (hLine
- hText
) / 2);
153 dc
.DrawLine(0, y
, 1000, y
);
157 void OnScroll(wxScrollWinEvent
& event
)
165 virtual wxCoord
OnGetLineHeight(size_t n
) const
167 wxASSERT( n
< GetLineCount() );
175 int m_heights
[MAX_LINES
];
179 DECLARE_EVENT_TABLE()
182 BEGIN_EVENT_TABLE(VScrollWindow
, wxVScrolledWindow
)
183 EVT_IDLE(VScrollWindow::OnIdle
)
184 EVT_PAINT(VScrollWindow::OnPaint
)
185 EVT_SCROLLWIN(VScrollWindow::OnScroll
)
188 class HVScrollWindow
: public wxHVScrolledWindow
191 HVScrollWindow(wxFrame
*frame
) : wxHVScrolledWindow(frame
, wxID_ANY
)
195 SetRowColumnCounts(MAX_LINES
, MAX_LINES
);
198 for ( i
= 0; i
< MAX_LINES
; ++i
)
200 m_heights
[i
] = rand()%30
+31; // low: 30; high: 60
201 m_widths
[i
] = rand()%30
+61; // low: 60; high: 90
207 void OnIdle(wxIdleEvent
&)
210 m_frame
->SetStatusText(wxString::Format
212 _T("Page size = %d rows %d columns; pos = row: %d, column: %d; max = %d rows, %d columns"),
213 GetScrollThumb(wxVERTICAL
),
214 GetScrollThumb(wxHORIZONTAL
),
215 GetScrollPos(wxVERTICAL
),
216 GetScrollPos(wxHORIZONTAL
),
217 GetScrollRange(wxVERTICAL
),
218 GetScrollRange(wxHORIZONTAL
)
220 #endif // wxUSE_STATUSBAR
224 void OnPaint(wxPaintEvent
&)
228 dc
.SetPen(*wxBLACK_PEN
);
230 const size_t rowFirst
= GetVisibleRowsBegin(),
231 rowLast
= GetVisibleRowsEnd();
232 const size_t columnFirst
= GetVisibleColumnsBegin(),
233 columnLast
= GetVisibleColumnsEnd();
235 const wxCoord hText
= dc
.GetCharHeight();
237 wxSize clientSize
= GetClientSize();
241 for ( size_t row
= rowFirst
; row
< rowLast
; row
++ )
243 wxCoord rowHeight
= OnGetRowHeight(row
);
244 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
247 for ( size_t col
= columnFirst
; col
< columnLast
; col
++ )
249 wxCoord colWidth
= OnGetColumnWidth(col
);
251 if ( row
== rowFirst
)
252 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
254 dc
.DrawText(wxString::Format(_T("Row %lu"), (unsigned long)row
),
255 x
+ 2, y
+ rowHeight
/ 2 - hText
);
256 dc
.DrawText(wxString::Format(_T("Col %lu"), (unsigned long)col
),
257 x
+ 2, y
+ rowHeight
/ 2);
260 if ( row
== rowFirst
)
261 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
265 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
269 void OnScroll(wxScrollWinEvent
& event
)
277 virtual wxCoord
OnGetRowHeight(size_t n
) const
279 wxASSERT( n
< GetRowCount() );
284 virtual wxCoord
OnGetColumnWidth(size_t n
) const
286 wxASSERT( n
< GetColumnCount() );
294 int m_heights
[MAX_LINES
];
295 int m_widths
[MAX_LINES
];
299 DECLARE_EVENT_TABLE()
302 BEGIN_EVENT_TABLE(HVScrollWindow
, wxHVScrolledWindow
)
303 EVT_IDLE(HVScrollWindow::OnIdle
)
304 EVT_PAINT(HVScrollWindow::OnPaint
)
305 EVT_SCROLLWIN(HVScrollWindow::OnScroll
)
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 // IDs for the controls and the menu commands
316 VScroll_Quit
= wxID_EXIT
,
318 // it is important for the id corresponding to the "About" command to have
319 // this standard value as otherwise it won't be handled properly under Mac
320 // (where it is special and put into the "Apple" menu)
321 VScroll_About
= wxID_ABOUT
,
323 VScroll_VScrollMode
= wxID_HIGHEST
+ 1,
327 // ----------------------------------------------------------------------------
328 // event tables and other macros for wxWidgets
329 // ----------------------------------------------------------------------------
331 // the event tables connect the wxWidgets events with the functions (event
332 // handlers) which process them. It can be also done at run-time, but for the
333 // simple menu events like this the static method is much simpler.
334 BEGIN_EVENT_TABLE(VarScrollFrame
, wxFrame
)
335 EVT_MENU(VScroll_Quit
, VarScrollFrame::OnQuit
)
336 EVT_MENU(VScroll_VScrollMode
, VarScrollFrame::OnModeVScroll
)
337 EVT_MENU(VScroll_HVScrollMode
, VarScrollFrame::OnModeHVScroll
)
338 EVT_MENU(VScroll_About
, VarScrollFrame::OnAbout
)
339 EVT_SIZE(VarScrollFrame::OnSize
)
342 // Create a new application object: this macro will allow wxWidgets to create
343 // the application object during program execution (it's better than using a
344 // static object for many reasons) and also declares the accessor function
345 // wxGetApp() which will return the reference of the right type (i.e. VarScrollApp and
347 IMPLEMENT_APP(VarScrollApp
)
349 // ============================================================================
351 // ============================================================================
353 // ----------------------------------------------------------------------------
354 // the application class
355 // ----------------------------------------------------------------------------
357 // 'Main program' equivalent: the program execution "starts" here
358 bool VarScrollApp::OnInit()
360 // create the main application window
361 VarScrollFrame
*frame
= new VarScrollFrame
;
363 // and show it (the frames, unlike simple controls, are not shown when
364 // created initially)
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
376 VarScrollFrame::VarScrollFrame()
379 _T("VScroll wxWidgets Sample"),
384 // set the frame icon
385 SetIcon(wxICON(sample
));
389 wxMenu
*menuFile
= new wxMenu
;
391 wxMenu
*menuMode
= new wxMenu
;
393 // the "About" item should be in the help menu
394 wxMenu
*menuHelp
= new wxMenu
;
395 menuHelp
->Append(VScroll_About
, _T("&About...\tF1"), _T("Show about dialog"));
397 #ifdef wxHAS_RADIO_MENU_ITEMS
398 menuMode
->AppendRadioItem(VScroll_VScrollMode
, _T("&Vertical\tAlt-V"),
399 _T("Vertical scrolling only"));
400 menuMode
->AppendRadioItem(VScroll_HVScrollMode
,
401 _T("&Horizontal/Vertical\tAlt-H"),
402 _T("Horizontal and vertical scrolling"));
403 menuMode
->Check(VScroll_VScrollMode
, true);
405 menuMode
->Append(VScroll_VScrollMode
, _T("&Vertical\tAlt-V"),
406 _T("Vertical scrolling only"));
407 menuMode
->Append(VScroll_HVScrollMode
, _T("&Horizontal/Vertical\tAlt-H"),
408 _T("Horizontal and vertical scrolling"));
411 menuFile
->Append(VScroll_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
413 // now append the freshly created menu to the menu bar...
414 wxMenuBar
*menuBar
= new wxMenuBar
;
415 menuBar
->Append(menuFile
, _T("&File"));
416 menuBar
->Append(menuMode
, _T("&Mode"));
417 menuBar
->Append(menuHelp
, _T("&Help"));
419 // ... and attach this menu bar to the frame
421 #endif // wxUSE_MENUS
424 // create a status bar just for fun (by default with 1 pane only)
426 SetStatusText(_T("Welcome to wxWidgets!"));
430 SetStatusWidths(2, widths
);
431 #endif // wxUSE_STATUSBAR
433 // create our one and only child -- it will take our entire client area
434 if ( menuMode
->IsChecked(VScroll_VScrollMode
) )
435 m_scrollWindow
= new VScrollWindow(this);
436 else if ( menuMode
->IsChecked(VScroll_VScrollMode
) )
437 m_scrollWindow
= new HVScrollWindow(this);
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 void VarScrollFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
446 // true is to force the frame to close
450 void VarScrollFrame::OnModeVScroll(wxCommandEvent
& WXUNUSED(event
))
452 if ( m_scrollWindow
)
453 m_scrollWindow
->Destroy();
455 m_scrollWindow
= new VScrollWindow(this);
459 void VarScrollFrame::OnModeHVScroll(wxCommandEvent
& WXUNUSED(event
))
461 if ( m_scrollWindow
)
462 m_scrollWindow
->Destroy();
464 m_scrollWindow
= new HVScrollWindow(this);
468 void VarScrollFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
470 wxMessageBox(_T("VScroll shows how to implement scrolling with\n")
471 _T("variable line widths and heights.\n")
472 _T("(c) 2003 Vadim Zeitlin"),
474 wxOK
| wxICON_INFORMATION
,