]>
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 #ifndef wxHAS_IMAGES_IN_RESOURCES
44 #include "../sample.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 #define MAX_LINES 10000
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 OnModeHScroll(wxCommandEvent
& event
);
76 void OnModeHVScroll(wxCommandEvent
& event
);
77 void OnAbout(wxCommandEvent
& event
);
79 void OnSize(wxSizeEvent
& event
)
81 // show current size in the status bar
83 if ( m_frameStatusBar
)
85 wxSize sz
= GetClientSize();
86 SetStatusText(wxString::Format(wxT("%dx%d"), sz
.x
, sz
.y
), 1);
88 #endif // wxUSE_STATUSBAR
94 // either a wxVScrolledWindow or a wxHVScrolled window, depending on current mode
95 wxPanel
*m_scrollWindow
;
97 // any class wishing to process wxWidgets events must use this macro
101 class VScrollWindow
: public wxVScrolledWindow
104 VScrollWindow(wxFrame
*frame
) : wxVScrolledWindow(frame
, wxID_ANY
)
108 SetRowCount(MAX_LINES
);
111 for ( i
= 0; i
< MAX_LINES
; ++i
)
112 m_heights
[i
] = rand()%25
+16; // low: 16; high: 40
117 void OnIdle(wxIdleEvent
&)
120 m_frame
->SetStatusText(wxString::Format
122 wxT("Page size = %d, pos = %d, max = %d"),
123 GetScrollThumb(wxVERTICAL
),
124 GetScrollPos(wxVERTICAL
),
125 GetScrollRange(wxVERTICAL
)
127 #endif // wxUSE_STATUSBAR
131 void OnPaint(wxPaintEvent
&)
135 dc
.SetPen(*wxBLACK_PEN
);
137 const size_t lineFirst
= GetVisibleBegin(),
138 lineLast
= GetVisibleEnd();
140 const wxCoord hText
= dc
.GetCharHeight();
142 wxSize clientSize
= GetClientSize();
145 for ( size_t line
= lineFirst
; line
< lineLast
; line
++ )
147 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
149 wxCoord hLine
= OnGetRowHeight(line
);
150 dc
.DrawText(wxString::Format(wxT("Line %lu"), (unsigned long)line
),
151 2, y
+ (hLine
- hText
) / 2);
154 dc
.DrawLine(0, y
, 1000, y
);
158 void OnScroll(wxScrollWinEvent
& event
)
165 void OnMouse(wxMouseEvent
& event
)
169 else if(event
.LeftUp())
174 virtual wxCoord
OnGetRowHeight(size_t n
) const
176 wxASSERT( n
< GetRowCount() );
184 int m_heights
[MAX_LINES
];
188 DECLARE_EVENT_TABLE()
191 BEGIN_EVENT_TABLE(VScrollWindow
, wxVScrolledWindow
)
192 EVT_IDLE(VScrollWindow::OnIdle
)
193 EVT_PAINT(VScrollWindow::OnPaint
)
194 EVT_SCROLLWIN(VScrollWindow::OnScroll
)
195 EVT_MOUSE_EVENTS(VScrollWindow::OnMouse
)
198 class HScrollWindow
: public wxHScrolledWindow
201 HScrollWindow(wxFrame
*frame
) : wxHScrolledWindow(frame
, wxID_ANY
)
205 SetColumnCount(MAX_LINES
);
208 for ( i
= 0; i
< MAX_LINES
; ++i
)
209 m_heights
[i
] = rand()%25
+16; // low: 15; high: 40
214 void OnIdle(wxIdleEvent
&)
217 m_frame
->SetStatusText(wxString::Format
219 wxT("Page size = %d, pos = %d, max = %d"),
220 GetScrollThumb(wxVERTICAL
),
221 GetScrollPos(wxVERTICAL
),
222 GetScrollRange(wxVERTICAL
)
224 #endif // wxUSE_STATUSBAR
228 void OnPaint(wxPaintEvent
&)
232 dc
.SetPen(*wxBLACK_PEN
);
234 const size_t lineFirst
= GetVisibleBegin(),
235 lineLast
= GetVisibleEnd();
237 const wxCoord hText
= dc
.GetCharHeight();
239 wxSize clientSize
= GetClientSize();
242 for ( size_t line
= lineFirst
; line
< lineLast
; line
++ )
244 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
246 wxCoord wLine
= OnGetColumnWidth(line
);
247 dc
.DrawRotatedText(wxString::Format(wxT("Line %lu"), (unsigned long)line
),
248 x
+ (wLine
- hText
) / 2, clientSize
.GetHeight() - 5, 90);
251 dc
.DrawLine(x
, 0, x
, 1000);
255 void OnScroll(wxScrollWinEvent
& event
)
262 void OnMouse(wxMouseEvent
& event
)
266 else if(event
.LeftUp())
271 virtual wxCoord
OnGetColumnWidth(size_t n
) const
273 wxASSERT( n
< GetColumnCount() );
281 int m_heights
[MAX_LINES
];
285 DECLARE_EVENT_TABLE()
288 BEGIN_EVENT_TABLE(HScrollWindow
, wxHScrolledWindow
)
289 EVT_IDLE(HScrollWindow::OnIdle
)
290 EVT_PAINT(HScrollWindow::OnPaint
)
291 EVT_SCROLLWIN(HScrollWindow::OnScroll
)
292 EVT_MOUSE_EVENTS(HScrollWindow::OnMouse
)
295 class HVScrollWindow
: public wxHVScrolledWindow
298 HVScrollWindow(wxFrame
*frame
) : wxHVScrolledWindow(frame
, wxID_ANY
)
302 SetRowColumnCount(MAX_LINES
, MAX_LINES
);
305 for ( i
= 0; i
< MAX_LINES
; ++i
)
307 m_heights
[i
] = rand()%30
+31; // low: 30; high: 60
308 m_widths
[i
] = rand()%30
+61; // low: 60; high: 90
314 void OnIdle(wxIdleEvent
&)
317 m_frame
->SetStatusText(wxString::Format
319 wxT("Page size = %d rows %d columns; pos = row: %d, column: %d; max = %d rows, %d columns"),
320 GetScrollThumb(wxVERTICAL
),
321 GetScrollThumb(wxHORIZONTAL
),
322 GetScrollPos(wxVERTICAL
),
323 GetScrollPos(wxHORIZONTAL
),
324 GetScrollRange(wxVERTICAL
),
325 GetScrollRange(wxHORIZONTAL
)
327 #endif // wxUSE_STATUSBAR
331 void OnPaint(wxPaintEvent
&)
335 dc
.SetPen(*wxBLACK_PEN
);
337 const size_t rowFirst
= GetVisibleRowsBegin(),
338 rowLast
= GetVisibleRowsEnd();
339 const size_t columnFirst
= GetVisibleColumnsBegin(),
340 columnLast
= GetVisibleColumnsEnd();
342 const wxCoord hText
= dc
.GetCharHeight();
344 wxSize clientSize
= GetClientSize();
348 for ( size_t row
= rowFirst
; row
< rowLast
; row
++ )
350 wxCoord rowHeight
= OnGetRowHeight(row
);
351 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
354 for ( size_t col
= columnFirst
; col
< columnLast
; col
++ )
356 wxCoord colWidth
= OnGetColumnWidth(col
);
358 if ( row
== rowFirst
)
359 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
361 dc
.DrawText(wxString::Format(wxT("Row %lu"), (unsigned long)row
),
362 x
+ 2, y
+ rowHeight
/ 2 - hText
);
363 dc
.DrawText(wxString::Format(wxT("Col %lu"), (unsigned long)col
),
364 x
+ 2, y
+ rowHeight
/ 2);
367 if ( row
== rowFirst
)
368 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
372 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
376 void OnScroll(wxScrollWinEvent
& event
)
383 void OnMouse(wxMouseEvent
& event
)
387 else if(event
.LeftUp())
392 virtual wxCoord
OnGetRowHeight(size_t n
) const
394 wxASSERT( n
< GetRowCount() );
399 virtual wxCoord
OnGetColumnWidth(size_t n
) const
401 wxASSERT( n
< GetColumnCount() );
409 int m_heights
[MAX_LINES
];
410 int m_widths
[MAX_LINES
];
414 DECLARE_EVENT_TABLE()
417 BEGIN_EVENT_TABLE(HVScrollWindow
, wxHVScrolledWindow
)
418 EVT_IDLE(HVScrollWindow::OnIdle
)
419 EVT_PAINT(HVScrollWindow::OnPaint
)
420 EVT_SCROLLWIN(HVScrollWindow::OnScroll
)
421 EVT_MOUSE_EVENTS(HVScrollWindow::OnMouse
)
424 // ----------------------------------------------------------------------------
426 // ----------------------------------------------------------------------------
428 // IDs for the controls and the menu commands
432 VScroll_Quit
= wxID_EXIT
,
434 // it is important for the id corresponding to the "About" command to have
435 // this standard value as otherwise it won't be handled properly under Mac
436 // (where it is special and put into the "Apple" menu)
437 VScroll_About
= wxID_ABOUT
,
439 VScroll_VScrollMode
= wxID_HIGHEST
+ 1,
444 // ----------------------------------------------------------------------------
445 // event tables and other macros for wxWidgets
446 // ----------------------------------------------------------------------------
448 // the event tables connect the wxWidgets events with the functions (event
449 // handlers) which process them. It can be also done at run-time, but for the
450 // simple menu events like this the static method is much simpler.
451 BEGIN_EVENT_TABLE(VarScrollFrame
, wxFrame
)
452 EVT_MENU(VScroll_Quit
, VarScrollFrame::OnQuit
)
453 EVT_MENU(VScroll_VScrollMode
, VarScrollFrame::OnModeVScroll
)
454 EVT_MENU(VScroll_HScrollMode
, VarScrollFrame::OnModeHScroll
)
455 EVT_MENU(VScroll_HVScrollMode
, VarScrollFrame::OnModeHVScroll
)
456 EVT_MENU(VScroll_About
, VarScrollFrame::OnAbout
)
457 EVT_SIZE(VarScrollFrame::OnSize
)
460 // Create a new application object: this macro will allow wxWidgets to create
461 // the application object during program execution (it's better than using a
462 // static object for many reasons) and also declares the accessor function
463 // wxGetApp() which will return the reference of the right type (i.e. VarScrollApp and
465 IMPLEMENT_APP(VarScrollApp
)
467 // ============================================================================
469 // ============================================================================
471 // ----------------------------------------------------------------------------
472 // the application class
473 // ----------------------------------------------------------------------------
475 // 'Main program' equivalent: the program execution "starts" here
476 bool VarScrollApp::OnInit()
478 if ( !wxApp::OnInit() )
481 // create the main application window
482 VarScrollFrame
*frame
= new VarScrollFrame
;
484 // and show it (the frames, unlike simple controls, are not shown when
485 // created initially)
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
497 VarScrollFrame::VarScrollFrame()
500 wxT("VScroll wxWidgets Sample"),
505 // set the frame icon
506 SetIcon(wxICON(sample
));
510 wxMenu
*menuFile
= new wxMenu
;
512 wxMenu
*menuMode
= new wxMenu
;
514 // the "About" item should be in the help menu
515 wxMenu
*menuHelp
= new wxMenu
;
516 menuHelp
->Append(VScroll_About
, wxT("&About\tF1"), wxT("Show about dialog"));
518 #ifdef wxHAS_RADIO_MENU_ITEMS
519 menuMode
->AppendRadioItem(VScroll_VScrollMode
, wxT("&Vertical\tAlt-V"),
520 wxT("Vertical scrolling only"));
521 menuMode
->AppendRadioItem(VScroll_HScrollMode
, wxT("&Horizontal\tAlt-H"),
522 wxT("Horizontal scrolling only"));
523 menuMode
->AppendRadioItem(VScroll_HVScrollMode
,
524 wxT("Hori&zontal/Vertical\tAlt-Z"),
525 wxT("Horizontal and vertical scrolling"));
526 menuMode
->Check(VScroll_VScrollMode
, true);
528 menuMode
->Append(VScroll_VScrollMode
, wxT("&Vertical\tAlt-V"),
529 wxT("Vertical scrolling only"));
530 menuMode
->Append(VScroll_HScrollMode
, wxT("&Horizontal\tAlt-H"),
531 wxT("Horizontal scrolling only"));
532 menuMode
->Append(VScroll_HVScrollMode
, wxT("Hori&zontal/Vertical\tAlt-Z"),
533 wxT("Horizontal and vertical scrolling"));
536 menuFile
->Append(VScroll_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
538 // now append the freshly created menu to the menu bar...
539 wxMenuBar
*menuBar
= new wxMenuBar
;
540 menuBar
->Append(menuFile
, wxT("&File"));
541 menuBar
->Append(menuMode
, wxT("&Mode"));
542 menuBar
->Append(menuHelp
, wxT("&Help"));
544 // ... and attach this menu bar to the frame
546 #endif // wxUSE_MENUS
549 // create a status bar just for fun (by default with 1 pane only)
551 SetStatusText(wxT("Welcome to wxWidgets!"));
555 SetStatusWidths(2, widths
);
556 #endif // wxUSE_STATUSBAR
558 // create our one and only child -- it will take our entire client area
559 if ( menuMode
->IsChecked(VScroll_VScrollMode
) )
560 m_scrollWindow
= new VScrollWindow(this);
561 else if ( menuMode
->IsChecked(VScroll_HScrollMode
) )
562 m_scrollWindow
= new HScrollWindow(this);
564 m_scrollWindow
= new HVScrollWindow(this);
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 void VarScrollFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
573 // true is to force the frame to close
577 void VarScrollFrame::OnModeVScroll(wxCommandEvent
& WXUNUSED(event
))
579 if ( m_scrollWindow
)
580 m_scrollWindow
->Destroy();
582 m_scrollWindow
= new VScrollWindow(this);
586 void VarScrollFrame::OnModeHScroll(wxCommandEvent
& WXUNUSED(event
))
588 if ( m_scrollWindow
)
589 m_scrollWindow
->Destroy();
591 m_scrollWindow
= new HScrollWindow(this);
595 void VarScrollFrame::OnModeHVScroll(wxCommandEvent
& WXUNUSED(event
))
597 if ( m_scrollWindow
)
598 m_scrollWindow
->Destroy();
600 m_scrollWindow
= new HVScrollWindow(this);
604 void VarScrollFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
606 wxMessageBox(wxT("VScroll shows how to implement scrolling with\n")
607 wxT("variable line widths and heights.\n")
608 wxT("(c) 2003 Vadim Zeitlin"),
609 wxT("About VScroll"),
610 wxOK
| wxICON_INFORMATION
,