]>
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
7 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers (this file is usually all you
27 // need because it includes almost all "standard" wxWidgets 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 #ifndef wxHAS_IMAGES_IN_RESOURCES
43 #include "../sample.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 #define MAX_LINES 10000
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // Define a new application type, each program should derive a class from wxApp
57 class VarScrollApp
: public wxApp
60 // create our main window
61 virtual bool OnInit();
64 // Define a new frame type: this is going to be our main frame
65 class VarScrollFrame
: public wxFrame
71 // event handlers (these functions should _not_ be virtual)
72 void OnQuit(wxCommandEvent
& event
);
73 void OnModeVScroll(wxCommandEvent
& event
);
74 void OnModeHScroll(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(wxT("%dx%d"), sz
.x
, sz
.y
), 1);
87 #endif // wxUSE_STATUSBAR
93 // either a wxVScrolledWindow or a wxHVScrolled window, depending on current mode
94 wxPanel
*m_scrollWindow
;
96 // any class wishing to process wxWidgets events must use this macro
100 class VScrollWindow
: public wxVScrolledWindow
103 VScrollWindow(wxFrame
*frame
) : wxVScrolledWindow(frame
, wxID_ANY
)
107 SetRowCount(MAX_LINES
);
110 for ( i
= 0; i
< MAX_LINES
; ++i
)
111 m_heights
[i
] = rand()%25
+16; // low: 16; high: 40
116 void OnIdle(wxIdleEvent
&)
119 m_frame
->SetStatusText(wxString::Format
121 wxT("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
= OnGetRowHeight(line
);
149 dc
.DrawText(wxString::Format(wxT("Line %lu"), (unsigned long)line
),
150 2, y
+ (hLine
- hText
) / 2);
153 dc
.DrawLine(0, y
, 1000, y
);
157 void OnScroll(wxScrollWinEvent
& event
)
164 void OnMouse(wxMouseEvent
& event
)
168 else if(event
.LeftUp())
173 virtual wxCoord
OnGetRowHeight(size_t n
) const
175 wxASSERT( n
< GetRowCount() );
183 int m_heights
[MAX_LINES
];
187 DECLARE_EVENT_TABLE()
190 BEGIN_EVENT_TABLE(VScrollWindow
, wxVScrolledWindow
)
191 EVT_IDLE(VScrollWindow::OnIdle
)
192 EVT_PAINT(VScrollWindow::OnPaint
)
193 EVT_SCROLLWIN(VScrollWindow::OnScroll
)
194 EVT_MOUSE_EVENTS(VScrollWindow::OnMouse
)
197 class HScrollWindow
: public wxHScrolledWindow
200 HScrollWindow(wxFrame
*frame
) : wxHScrolledWindow(frame
, wxID_ANY
)
204 SetColumnCount(MAX_LINES
);
207 for ( i
= 0; i
< MAX_LINES
; ++i
)
208 m_heights
[i
] = rand()%25
+16; // low: 15; high: 40
213 void OnIdle(wxIdleEvent
&)
216 m_frame
->SetStatusText(wxString::Format
218 wxT("Page size = %d, pos = %d, max = %d"),
219 GetScrollThumb(wxVERTICAL
),
220 GetScrollPos(wxVERTICAL
),
221 GetScrollRange(wxVERTICAL
)
223 #endif // wxUSE_STATUSBAR
227 void OnPaint(wxPaintEvent
&)
231 dc
.SetPen(*wxBLACK_PEN
);
233 const size_t lineFirst
= GetVisibleBegin(),
234 lineLast
= GetVisibleEnd();
236 const wxCoord hText
= dc
.GetCharHeight();
238 wxSize clientSize
= GetClientSize();
241 for ( size_t line
= lineFirst
; line
< lineLast
; line
++ )
243 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
245 wxCoord wLine
= OnGetColumnWidth(line
);
246 dc
.DrawRotatedText(wxString::Format(wxT("Line %lu"), (unsigned long)line
),
247 x
+ (wLine
- hText
) / 2, clientSize
.GetHeight() - 5, 90);
250 dc
.DrawLine(x
, 0, x
, 1000);
254 void OnScroll(wxScrollWinEvent
& event
)
261 void OnMouse(wxMouseEvent
& event
)
265 else if(event
.LeftUp())
270 virtual wxCoord
OnGetColumnWidth(size_t n
) const
272 wxASSERT( n
< GetColumnCount() );
280 int m_heights
[MAX_LINES
];
284 DECLARE_EVENT_TABLE()
287 BEGIN_EVENT_TABLE(HScrollWindow
, wxHScrolledWindow
)
288 EVT_IDLE(HScrollWindow::OnIdle
)
289 EVT_PAINT(HScrollWindow::OnPaint
)
290 EVT_SCROLLWIN(HScrollWindow::OnScroll
)
291 EVT_MOUSE_EVENTS(HScrollWindow::OnMouse
)
294 class HVScrollWindow
: public wxHVScrolledWindow
297 HVScrollWindow(wxFrame
*frame
) : wxHVScrolledWindow(frame
, wxID_ANY
)
301 SetRowColumnCount(MAX_LINES
, MAX_LINES
);
304 for ( i
= 0; i
< MAX_LINES
; ++i
)
306 m_heights
[i
] = rand()%30
+31; // low: 30; high: 60
307 m_widths
[i
] = rand()%30
+61; // low: 60; high: 90
313 void OnIdle(wxIdleEvent
&)
316 m_frame
->SetStatusText(wxString::Format
318 wxT("Page size = %d rows %d columns; pos = row: %d, column: %d; max = %d rows, %d columns"),
319 GetScrollThumb(wxVERTICAL
),
320 GetScrollThumb(wxHORIZONTAL
),
321 GetScrollPos(wxVERTICAL
),
322 GetScrollPos(wxHORIZONTAL
),
323 GetScrollRange(wxVERTICAL
),
324 GetScrollRange(wxHORIZONTAL
)
326 #endif // wxUSE_STATUSBAR
330 void OnPaint(wxPaintEvent
&)
334 dc
.SetPen(*wxBLACK_PEN
);
336 const size_t rowFirst
= GetVisibleRowsBegin(),
337 rowLast
= GetVisibleRowsEnd();
338 const size_t columnFirst
= GetVisibleColumnsBegin(),
339 columnLast
= GetVisibleColumnsEnd();
341 const wxCoord hText
= dc
.GetCharHeight();
343 wxSize clientSize
= GetClientSize();
347 for ( size_t row
= rowFirst
; row
< rowLast
; row
++ )
349 wxCoord rowHeight
= OnGetRowHeight(row
);
350 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
353 for ( size_t col
= columnFirst
; col
< columnLast
; col
++ )
355 wxCoord colWidth
= OnGetColumnWidth(col
);
357 if ( row
== rowFirst
)
358 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
360 dc
.DrawText(wxString::Format(wxT("Row %lu"), (unsigned long)row
),
361 x
+ 2, y
+ rowHeight
/ 2 - hText
);
362 dc
.DrawText(wxString::Format(wxT("Col %lu"), (unsigned long)col
),
363 x
+ 2, y
+ rowHeight
/ 2);
366 if ( row
== rowFirst
)
367 dc
.DrawLine(x
, 0, x
, clientSize
.GetHeight());
371 dc
.DrawLine(0, y
, clientSize
.GetWidth(), y
);
375 void OnScroll(wxScrollWinEvent
& event
)
382 void OnMouse(wxMouseEvent
& event
)
386 else if(event
.LeftUp())
391 virtual wxCoord
OnGetRowHeight(size_t n
) const
393 wxASSERT( n
< GetRowCount() );
398 virtual wxCoord
OnGetColumnWidth(size_t n
) const
400 wxASSERT( n
< GetColumnCount() );
408 int m_heights
[MAX_LINES
];
409 int m_widths
[MAX_LINES
];
413 DECLARE_EVENT_TABLE()
416 BEGIN_EVENT_TABLE(HVScrollWindow
, wxHVScrolledWindow
)
417 EVT_IDLE(HVScrollWindow::OnIdle
)
418 EVT_PAINT(HVScrollWindow::OnPaint
)
419 EVT_SCROLLWIN(HVScrollWindow::OnScroll
)
420 EVT_MOUSE_EVENTS(HVScrollWindow::OnMouse
)
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 // IDs for the controls and the menu commands
431 VScroll_Quit
= wxID_EXIT
,
433 // it is important for the id corresponding to the "About" command to have
434 // this standard value as otherwise it won't be handled properly under Mac
435 // (where it is special and put into the "Apple" menu)
436 VScroll_About
= wxID_ABOUT
,
438 VScroll_VScrollMode
= wxID_HIGHEST
+ 1,
443 // ----------------------------------------------------------------------------
444 // event tables and other macros for wxWidgets
445 // ----------------------------------------------------------------------------
447 // the event tables connect the wxWidgets events with the functions (event
448 // handlers) which process them. It can be also done at run-time, but for the
449 // simple menu events like this the static method is much simpler.
450 BEGIN_EVENT_TABLE(VarScrollFrame
, wxFrame
)
451 EVT_MENU(VScroll_Quit
, VarScrollFrame::OnQuit
)
452 EVT_MENU(VScroll_VScrollMode
, VarScrollFrame::OnModeVScroll
)
453 EVT_MENU(VScroll_HScrollMode
, VarScrollFrame::OnModeHScroll
)
454 EVT_MENU(VScroll_HVScrollMode
, VarScrollFrame::OnModeHVScroll
)
455 EVT_MENU(VScroll_About
, VarScrollFrame::OnAbout
)
456 EVT_SIZE(VarScrollFrame::OnSize
)
459 // Create a new application object: this macro will allow wxWidgets to create
460 // the application object during program execution (it's better than using a
461 // static object for many reasons) and also declares the accessor function
462 // wxGetApp() which will return the reference of the right type (i.e. VarScrollApp and
464 IMPLEMENT_APP(VarScrollApp
)
466 // ============================================================================
468 // ============================================================================
470 // ----------------------------------------------------------------------------
471 // the application class
472 // ----------------------------------------------------------------------------
474 // 'Main program' equivalent: the program execution "starts" here
475 bool VarScrollApp::OnInit()
477 if ( !wxApp::OnInit() )
480 // create the main application window
481 VarScrollFrame
*frame
= new VarScrollFrame
;
483 // and show it (the frames, unlike simple controls, are not shown when
484 // created initially)
491 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
496 VarScrollFrame::VarScrollFrame()
499 wxT("VScroll wxWidgets Sample"),
504 // set the frame icon
505 SetIcon(wxICON(sample
));
509 wxMenu
*menuFile
= new wxMenu
;
511 wxMenu
*menuMode
= new wxMenu
;
513 // the "About" item should be in the help menu
514 wxMenu
*menuHelp
= new wxMenu
;
515 menuHelp
->Append(VScroll_About
, wxT("&About\tF1"), wxT("Show about dialog"));
517 #ifdef wxHAS_RADIO_MENU_ITEMS
518 menuMode
->AppendRadioItem(VScroll_VScrollMode
, wxT("&Vertical\tAlt-V"),
519 wxT("Vertical scrolling only"));
520 menuMode
->AppendRadioItem(VScroll_HScrollMode
, wxT("&Horizontal\tAlt-H"),
521 wxT("Horizontal scrolling only"));
522 menuMode
->AppendRadioItem(VScroll_HVScrollMode
,
523 wxT("Hori&zontal/Vertical\tAlt-Z"),
524 wxT("Horizontal and vertical scrolling"));
525 menuMode
->Check(VScroll_VScrollMode
, true);
527 menuMode
->Append(VScroll_VScrollMode
, wxT("&Vertical\tAlt-V"),
528 wxT("Vertical scrolling only"));
529 menuMode
->Append(VScroll_HScrollMode
, wxT("&Horizontal\tAlt-H"),
530 wxT("Horizontal scrolling only"));
531 menuMode
->Append(VScroll_HVScrollMode
, wxT("Hori&zontal/Vertical\tAlt-Z"),
532 wxT("Horizontal and vertical scrolling"));
535 menuFile
->Append(VScroll_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
537 // now append the freshly created menu to the menu bar...
538 wxMenuBar
*menuBar
= new wxMenuBar
;
539 menuBar
->Append(menuFile
, wxT("&File"));
540 menuBar
->Append(menuMode
, wxT("&Mode"));
541 menuBar
->Append(menuHelp
, wxT("&Help"));
543 // ... and attach this menu bar to the frame
545 #endif // wxUSE_MENUS
548 // create a status bar just for fun (by default with 1 pane only)
550 SetStatusText(wxT("Welcome to wxWidgets!"));
554 SetStatusWidths(2, widths
);
555 #endif // wxUSE_STATUSBAR
557 // create our one and only child -- it will take our entire client area
558 if ( menuMode
->IsChecked(VScroll_VScrollMode
) )
559 m_scrollWindow
= new VScrollWindow(this);
560 else if ( menuMode
->IsChecked(VScroll_HScrollMode
) )
561 m_scrollWindow
= new HScrollWindow(this);
563 m_scrollWindow
= new HVScrollWindow(this);
566 // ----------------------------------------------------------------------------
568 // ----------------------------------------------------------------------------
570 void VarScrollFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
572 // true is to force the frame to close
576 void VarScrollFrame::OnModeVScroll(wxCommandEvent
& WXUNUSED(event
))
578 if ( m_scrollWindow
)
579 m_scrollWindow
->Destroy();
581 m_scrollWindow
= new VScrollWindow(this);
585 void VarScrollFrame::OnModeHScroll(wxCommandEvent
& WXUNUSED(event
))
587 if ( m_scrollWindow
)
588 m_scrollWindow
->Destroy();
590 m_scrollWindow
= new HScrollWindow(this);
594 void VarScrollFrame::OnModeHVScroll(wxCommandEvent
& WXUNUSED(event
))
596 if ( m_scrollWindow
)
597 m_scrollWindow
->Destroy();
599 m_scrollWindow
= new HVScrollWindow(this);
603 void VarScrollFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
605 wxMessageBox(wxT("VScroll shows how to implement scrolling with\n")
606 wxT("variable line widths and heights.\n")
607 wxT("(c) 2003 Vadim Zeitlin"),
608 wxT("About VScroll"),
609 wxOK
| wxICON_INFORMATION
,