]> git.saurik.com Git - wxWidgets.git/blame - samples/scroll/scroll.cpp
oops, removed duplicate SetIcons[s]() added in last commit
[wxWidgets.git] / samples / scroll / scroll.cpp
CommitLineData
fdd3ed7a
RR
1/*
2 * Program: scroll
3 *
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1998, Robert Roebling
2b5f62a0 7 * 2002, Ron Lee
57ac1a56 8 * 2003, Matt Gregory
fdd3ed7a
RR
9 *
10 */
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#include "wx/image.h"
053f9cc1 24#include "wx/listctrl.h"
ed673c6a
RR
25#include "wx/sizer.h"
26#include "wx/log.h"
27
fdd3ed7a
RR
28
29// derived classes
30
31class MyFrame;
32class MyApp;
33
34// MyCanvas
35
36class MyCanvas: public wxScrolledWindow
37{
38public:
8a73bf3d 39 MyCanvas() {}
fdd3ed7a 40 MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
925e9792 41 ~MyCanvas(){};
fdd3ed7a 42 void OnPaint( wxPaintEvent &event );
307f16e8 43 void OnQueryPosition( wxCommandEvent &event );
ed673c6a
RR
44 void OnAddButton( wxCommandEvent &event );
45 void OnDeleteButton( wxCommandEvent &event );
46 void OnMoveButton( wxCommandEvent &event );
47 void OnScrollWin( wxCommandEvent &event );
bf0c00c6 48 void OnMouseDown( wxMouseEvent &event );
ed673c6a
RR
49
50 wxButton *m_button;
fdd3ed7a
RR
51
52 DECLARE_DYNAMIC_CLASS(MyCanvas)
53 DECLARE_EVENT_TABLE()
54};
55
2b5f62a0
VZ
56
57// ----------------------------------------------------------------------------
58// Autoscrolling example.
59// ----------------------------------------------------------------------------
60
61// this class uses the 'virtual' size attribute along with an internal
62// sizer to automatically set up scrollbars as needed
63
64class MyAutoScrollWindow : public wxScrolledWindow
65{
66private:
67
68 wxButton *m_button;
69
70public:
71
72 MyAutoScrollWindow( wxWindow *parent );
73
74 void OnResizeClick( wxCommandEvent &WXUNUSED( event ) );
75
76 DECLARE_EVENT_TABLE()
77};
78
79
8a73bf3d
VZ
80// ----------------------------------------------------------------------------
81// MyScrolledWindow classes: examples of wxScrolledWindow usage
82// ----------------------------------------------------------------------------
83
84// base class for both of them
85class MyScrolledWindowBase : public wxScrolledWindow
86{
87public:
2b5f62a0
VZ
88 MyScrolledWindowBase(wxWindow *parent)
89 : wxScrolledWindow(parent)
90 , m_nLines( 100 )
8a73bf3d 91 {
2b5f62a0
VZ
92 wxClientDC dc(this);
93 dc.GetTextExtent(_T("Line 17"), NULL, &m_hLine);
8a73bf3d
VZ
94 }
95
96protected:
8a73bf3d
VZ
97 // the height of one line on screen
98 wxCoord m_hLine;
99
100 // the number of lines we draw
101 size_t m_nLines;
102};
103
104// this class does "stupid" redrawing - it redraws everything each time
2b5f62a0
VZ
105// and sets the scrollbar extent directly.
106
8a73bf3d
VZ
107class MyScrolledWindowDumb : public MyScrolledWindowBase
108{
109public:
2b5f62a0
VZ
110 MyScrolledWindowDumb(wxWindow *parent) : MyScrolledWindowBase(parent)
111 {
112 // no horz scrolling
b62ca03d 113 SetScrollbars(0, m_hLine, 0, m_nLines + 1, 0, 0, true /* no refresh */);
2b5f62a0 114 }
8a73bf3d
VZ
115
116 virtual void OnDraw(wxDC& dc);
117};
118
119// this class does "smart" redrawing - only redraws the lines which must be
2b5f62a0
VZ
120// redrawn and sets the scroll rate and virtual size to affect the
121// scrollbars.
122//
123// Note that this class should produce identical results to the one above.
124
8a73bf3d
VZ
125class MyScrolledWindowSmart : public MyScrolledWindowBase
126{
127public:
2b5f62a0
VZ
128 MyScrolledWindowSmart(wxWindow *parent) : MyScrolledWindowBase(parent)
129 {
130 // no horz scrolling
131 SetScrollRate( 0, m_hLine );
132 SetVirtualSize( -1, ( m_nLines + 1 ) * m_hLine );
133 }
8a73bf3d
VZ
134
135 virtual void OnDraw(wxDC& dc);
136};
137
57ac1a56
RN
138// ----------------------------------------------------------------------------
139// MyAutoTimedScrollingWindow: implements a text viewer with simple blocksize
140// selection to test auto-scrolling functionality
141// ----------------------------------------------------------------------------
142
143class MyAutoTimedScrollingWindow : public wxScrolledWindow
144{
145protected: // member data
146 // test data variables
147 static const wxChar* sm_testData;
148 static const int sm_lineCnt; // line count
149 static const int sm_lineLen; // line length in characters
150 // sizes for graphical data
151 wxCoord m_fontH, m_fontW;
152 // selection tracking
153 wxPoint m_selStart; // beginning of blockwise selection
154 wxPoint m_cursor; // end of blockwise selection (mouse position)
155
156protected: // gui stuff
157 wxFont m_font;
158
159public: // interface
160 MyAutoTimedScrollingWindow( wxWindow* parent );
161 wxRect DeviceCoordsToGraphicalChars(wxRect updRect) const;
162 wxPoint DeviceCoordsToGraphicalChars(wxPoint pos) const;
163 wxPoint GraphicalCharToDeviceCoords(wxPoint pos) const;
164 wxRect LogicalCoordsToGraphicalChars(wxRect updRect) const;
165 wxPoint LogicalCoordsToGraphicalChars(wxPoint pos) const;
166 wxPoint GraphicalCharToLogicalCoords(wxPoint pos) const;
167 void MyRefresh();
168 bool IsSelected(int chX, int chY) const;
169 static bool IsInside(int k, int bound1, int bound2);
170 static wxRect DCNormalize(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
171
172protected: // event stuff
173 DECLARE_EVENT_TABLE()
174 void OnDraw(wxDC& dc);
175 void OnMouseLeftDown(wxMouseEvent& event);
176 void OnMouseLeftUp(wxMouseEvent& event);
177 void OnMouseMove(wxMouseEvent& event);
178 void OnScroll(wxScrollWinEvent& event);
179};
2b5f62a0 180
8a73bf3d 181// ----------------------------------------------------------------------------
fdd3ed7a 182// MyFrame
8a73bf3d 183// ----------------------------------------------------------------------------
fdd3ed7a
RR
184
185class MyFrame: public wxFrame
186{
187public:
188 MyFrame();
189
190 void OnAbout( wxCommandEvent &event );
191 void OnQuit( wxCommandEvent &event );
8e217128
RR
192 void OnDeleteAll( wxCommandEvent &event );
193 void OnInsertNew( wxCommandEvent &event );
fdd3ed7a
RR
194
195 MyCanvas *m_canvas;
ed673c6a 196 wxTextCtrl *m_log;
fdd3ed7a
RR
197
198 DECLARE_DYNAMIC_CLASS(MyFrame)
199 DECLARE_EVENT_TABLE()
200};
201
57ac1a56 202// ----------------------------------------------------------------------------
fdd3ed7a 203// MyApp
57ac1a56 204// ----------------------------------------------------------------------------
fdd3ed7a
RR
205
206class MyApp: public wxApp
207{
208public:
209 virtual bool OnInit();
210};
211
57ac1a56
RN
212
213// ----------------------------------------------------------------------------
fdd3ed7a 214// main program
57ac1a56 215// ----------------------------------------------------------------------------
fdd3ed7a
RR
216
217IMPLEMENT_APP(MyApp)
218
ed673c6a
RR
219// ids
220
2b5f62a0
VZ
221const long ID_ADDBUTTON = wxNewId();
222const long ID_DELBUTTON = wxNewId();
223const long ID_MOVEBUTTON = wxNewId();
224const long ID_SCROLLWIN = wxNewId();
225const long ID_QUERYPOS = wxNewId();
ed673c6a 226
2b5f62a0 227const long ID_NEWBUTTON = wxNewId();
ed673c6a 228
57ac1a56 229// ----------------------------------------------------------------------------
fdd3ed7a 230// MyCanvas
57ac1a56 231// ----------------------------------------------------------------------------
fdd3ed7a
RR
232
233IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow)
234
235BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
307f16e8 236 EVT_PAINT( MyCanvas::OnPaint)
2b5f62a0 237 EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown)
307f16e8 238 EVT_BUTTON( ID_QUERYPOS, MyCanvas::OnQueryPosition)
ed673c6a
RR
239 EVT_BUTTON( ID_ADDBUTTON, MyCanvas::OnAddButton)
240 EVT_BUTTON( ID_DELBUTTON, MyCanvas::OnDeleteButton)
241 EVT_BUTTON( ID_MOVEBUTTON, MyCanvas::OnMoveButton)
242 EVT_BUTTON( ID_SCROLLWIN, MyCanvas::OnScrollWin)
fdd3ed7a
RR
243END_EVENT_TABLE()
244
245MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
246 const wxPoint &pos, const wxSize &size )
2b5f62a0 247 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER | wxTAB_TRAVERSAL, _T("test canvas") )
fdd3ed7a 248{
2b5f62a0
VZ
249 SetScrollRate( 10, 10 );
250 SetVirtualSize( 500, 1000 );
251
252 (void) new wxButton( this, ID_ADDBUTTON, _T("add button"), wxPoint(10,10) );
253 (void) new wxButton( this, ID_DELBUTTON, _T("del button"), wxPoint(10,40) );
254 (void) new wxButton( this, ID_MOVEBUTTON, _T("move button"), wxPoint(150,10) );
255 (void) new wxButton( this, ID_SCROLLWIN, _T("scroll win"), wxPoint(250,10) );
ed673c6a 256
aa06a8fd
DW
257#if 0
258
fdd3ed7a
RR
259 wxString choices[] =
260 {
261 "This",
262 "is one of my",
263 "really",
264 "wonderful",
265 "examples."
266 };
aa06a8fd 267
307f16e8 268 m_button = new wxButton( this, ID_QUERYPOS, "Query position", wxPoint(10,110) );
aa06a8fd 269
422d0ff0 270 (void) new wxTextCtrl( this, wxID_ANY, "wxTextCtrl", wxPoint(10,150), wxSize(80,wxDefaultCoord) );
aa06a8fd 271
b62ca03d 272 (void) new wxRadioButton( this, wxID_ANY, "Disable", wxPoint(10,190) );
aa06a8fd 273
b62ca03d 274 (void) new wxComboBox( this, wxID_ANY, "This", wxPoint(10,230), wxDefaultSize, 5, choices );
aa06a8fd 275
b62ca03d 276 (void) new wxRadioBox( this, wxID_ANY, "This", wxPoint(10,310), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_COLS );
053f9cc1 277
b62ca03d 278 (void) new wxRadioBox( this, wxID_ANY, "This", wxPoint(10,440), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_ROWS );
e9158f7d 279
ed673c6a 280 wxListCtrl *m_listCtrl = new wxListCtrl(
b62ca03d 281 this, wxID_ANY, wxPoint(200, 110), wxSize(180, 120),
8a73bf3d 282 wxLC_REPORT | wxSIMPLE_BORDER | wxLC_SINGLE_SEL );
053f9cc1 283
ed673c6a
RR
284 m_listCtrl->InsertColumn(0, "First", wxLIST_FORMAT_LEFT, 90);
285 m_listCtrl->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT, 90);
053f9cc1 286
ed673c6a
RR
287 for ( int i=0; i < 30; i++)
288 {
289 char buf[20];
290 sprintf(buf, "Item %d", i);
291 m_listCtrl->InsertItem(i, buf);
292 }
293 m_listCtrl->SetItemState( 3, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
053f9cc1 294
b62ca03d 295 (void) new wxListBox( this, wxID_ANY, wxPoint(260,280), wxSize(120,120), 5, choices, wxLB_ALWAYS_SB );
5e014a0c 296
aa06a8fd
DW
297#endif
298
b62ca03d 299 wxPanel *test = new wxPanel( this, wxID_ANY, wxPoint(10, 110), wxSize(130,50), wxSIMPLE_BORDER | wxTAB_TRAVERSAL );
a60b1f5d 300 test->SetBackgroundColour( wxT("WHEAT") );
aa06a8fd
DW
301
302#if 0
303
b62ca03d 304 wxButton *test2 = new wxButton( test, wxID_ANY, "Hallo", wxPoint(10,10) );
aa06a8fd 305
b62ca03d 306 test = new wxPanel( this, wxID_ANY, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER | wxTAB_TRAVERSAL );
a60b1f5d 307 test->SetBackgroundColour( wxT("WHEAT") );
ed673c6a 308 test->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) );
b62ca03d 309 test2 = new wxButton( test, wxID_ANY, "Hallo", wxPoint(10,10) );
ed673c6a 310 test2->SetCursor( wxCursor( wxCURSOR_PENCIL ) );
aa06a8fd 311
b62ca03d 312 test = new wxPanel( this, wxID_ANY, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER | wxTAB_TRAVERSAL );
a60b1f5d 313 test->SetBackgroundColour( wxT("WHEAT") );
ed673c6a 314 test->SetCursor( wxCursor( wxCURSOR_PENCIL ) );
b62ca03d 315 test2 = new wxButton( test, wxID_ANY, "Hallo", wxPoint(10,10) );
ed673c6a 316 test2->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) );
5e014a0c 317
aa06a8fd
DW
318#endif
319
a60b1f5d 320 SetBackgroundColour( wxT("BLUE") );
aa06a8fd 321
ed673c6a 322 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
fdd3ed7a
RR
323}
324
bf0c00c6
RR
325void MyCanvas::OnMouseDown( wxMouseEvent &event )
326{
a3e7d24d
RR
327 if (event.LeftDown())
328 {
329 wxPoint pt( event.GetPosition() );
330 int x,y;
331 CalcUnscrolledPosition( pt.x, pt.y, &x, &y );
4693b20c 332 wxLogMessage( wxT("Mouse down event at: %d %d, scrolled: %d %d"), pt.x, pt.y, x, y );
f6bcfd97
BP
333
334 if ( !event.LeftIsDown() )
b62ca03d 335 wxLogMessage( wxT("Error: LeftIsDown() should be true if for LeftDown()") );
a3e7d24d 336 }
bf0c00c6
RR
337}
338
339void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
340{
341 wxPaintDC dc( this );
342 PrepareDC( dc );
343
2b5f62a0 344 dc.DrawText( _T("Press mouse button to test calculations!"), 160, 50 );
bf0c00c6 345
2b5f62a0 346 dc.DrawText( _T("Some text"), 140, 140 );
aa06a8fd 347
bf0c00c6
RR
348 dc.DrawRectangle( 100, 160, 200, 200 );
349}
350
307f16e8
RR
351void MyCanvas::OnQueryPosition( wxCommandEvent &WXUNUSED(event) )
352{
353 wxPoint pt( m_button->GetPosition() );
4693b20c 354 wxLogMessage( wxT("Position of \"Query position\" is %d %d"), pt.x, pt.y );
bf0c00c6 355 pt = ClientToScreen( pt );
4693b20c 356 wxLogMessage( wxT("Position of \"Query position\" on screen is %d %d"), pt.x, pt.y );
307f16e8
RR
357}
358
ed673c6a
RR
359void MyCanvas::OnAddButton( wxCommandEvent &WXUNUSED(event) )
360{
4693b20c 361 wxLogMessage( wxT("Inserting button at position 10,70...") );
2b5f62a0 362 wxButton *button = new wxButton( this, ID_NEWBUTTON, wxT("new button"), wxPoint(10,70), wxSize(80,25) );
bf0c00c6 363 wxPoint pt( button->GetPosition() );
4693b20c 364 wxLogMessage( wxT("-> Position after inserting %d %d"), pt.x, pt.y );
ed673c6a
RR
365}
366
256b8649 367void MyCanvas::OnDeleteButton( wxCommandEvent &WXUNUSED(event) )
ed673c6a 368{
4693b20c 369 wxLogMessage( wxT("Deleting button inserted with \"Add button\"...") );
ed673c6a
RR
370 wxWindow *win = FindWindow( ID_NEWBUTTON );
371 if (win)
372 win->Destroy();
373 else
4693b20c 374 wxLogMessage( wxT("-> No window with id = ID_NEWBUTTON found.") );
ed673c6a
RR
375}
376
377void MyCanvas::OnMoveButton( wxCommandEvent &event )
378{
4693b20c 379 wxLogMessage( wxT("Moving button 10 pixels downward..") );
ed673c6a 380 wxWindow *win = FindWindow( event.GetId() );
bf0c00c6 381 wxPoint pt( win->GetPosition() );
4693b20c 382 wxLogMessage( wxT("-> Position before move is %d %d"), pt.x, pt.y );
422d0ff0 383 win->Move( wxDefaultCoord, pt.y + 10 );
bf0c00c6 384 pt = win->GetPosition();
4693b20c 385 wxLogMessage( wxT("-> Position after move is %d %d"), pt.x, pt.y );
ed673c6a
RR
386}
387
388void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) )
389{
4693b20c 390 wxLogMessage( wxT("Scrolling 2 units up.\nThe white square and the controls should move equally!") );
ed673c6a 391 int x,y;
8073eb40 392 GetViewStart( &x, &y );
ed673c6a
RR
393 Scroll( -1, y+2 );
394}
395
57ac1a56 396// ----------------------------------------------------------------------------
2b5f62a0 397// MyAutoScrollWindow
57ac1a56 398// ----------------------------------------------------------------------------
2b5f62a0
VZ
399
400const long ID_RESIZEBUTTON = wxNewId();
401const wxSize SMALL_BUTTON( 100, 50 );
402const wxSize LARGE_BUTTON( 300, 100 );
403
404BEGIN_EVENT_TABLE( MyAutoScrollWindow, wxScrolledWindow)
405 EVT_BUTTON( ID_RESIZEBUTTON, MyAutoScrollWindow::OnResizeClick)
406END_EVENT_TABLE()
407
408MyAutoScrollWindow::MyAutoScrollWindow( wxWindow *parent )
409 : wxScrolledWindow( parent )
410{
411 SetBackgroundColour( wxT("GREEN") );
412
413 // Set the rate we'd like for scrolling.
414
415 SetScrollRate( 5, 5 );
416
417 // Populate a sizer with a 'resizing' button and some
418 // other static decoration
419
420 wxFlexGridSizer *innersizer = new wxFlexGridSizer( 2, 2 );
421
422 m_button = new wxButton( this,
423 ID_RESIZEBUTTON,
424 _T("Press me"),
425 wxDefaultPosition,
426 SMALL_BUTTON );
427
428 // We need to do this here, because wxADJUST_MINSIZE below
429 // will cause the initial size to be ignored for Best/Min size.
430 // It would be nice to fix the sizers to handle this a little
431 // more cleanly.
432
433 m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() );
434
435 innersizer->Add( m_button,
436 0,
437 wxALIGN_CENTER | wxALL | wxADJUST_MINSIZE,
438 20 );
439
b62ca03d 440 innersizer->Add( new wxStaticText( this, wxID_ANY, _T("This is just") ),
2b5f62a0
VZ
441 0,
442 wxALIGN_CENTER );
443
b62ca03d 444 innersizer->Add( new wxStaticText( this, wxID_ANY, _T("some decoration") ),
2b5f62a0
VZ
445 0,
446 wxALIGN_CENTER );
447
b62ca03d 448 innersizer->Add( new wxStaticText( this, wxID_ANY, _T("for you to scroll...") ),
2b5f62a0
VZ
449 0,
450 wxALIGN_CENTER );
451
452 // Then use the sizer to set the scrolled region size.
453
454 SetSizer( innersizer );
455}
456
457void MyAutoScrollWindow::OnResizeClick( wxCommandEvent &WXUNUSED( event ) )
458{
459 // Arbitrarily resize the button to change the minimum size of
460 // the (scrolled) sizer.
461
462 if( m_button->GetSize() == SMALL_BUTTON )
463 m_button->SetSizeHints( LARGE_BUTTON.GetWidth(), LARGE_BUTTON.GetHeight() );
464 else
465 m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() );
466
467 // Force update layout and scrollbars, since nothing we do here
468 // necessarily generates a size event which would do it for us.
469
470 FitInside();
471}
472
57ac1a56 473// ----------------------------------------------------------------------------
fdd3ed7a 474// MyFrame
57ac1a56 475// ----------------------------------------------------------------------------
fdd3ed7a 476
2b5f62a0
VZ
477const long ID_QUIT = wxNewId();
478const long ID_ABOUT = wxNewId();
479const long ID_DELETE_ALL = wxNewId();
480const long ID_INSERT_NEW = wxNewId();
fdd3ed7a
RR
481
482IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
483
484BEGIN_EVENT_TABLE(MyFrame,wxFrame)
8e217128
RR
485 EVT_MENU (ID_DELETE_ALL, MyFrame::OnDeleteAll)
486 EVT_MENU (ID_INSERT_NEW, MyFrame::OnInsertNew)
fdd3ed7a
RR
487 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
488 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
489END_EVENT_TABLE()
490
491MyFrame::MyFrame()
b62ca03d 492 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"),
57ac1a56 493 wxPoint(20,20), wxSize(800,500) )
fdd3ed7a 494{
ed673c6a 495 wxMenu *file_menu = new wxMenu();
2b5f62a0
VZ
496 file_menu->Append( ID_DELETE_ALL, _T("Delete all"));
497 file_menu->Append( ID_INSERT_NEW, _T("Insert new"));
498 file_menu->Append( ID_ABOUT, _T("&About.."));
499 file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X"));
fdd3ed7a 500
ed673c6a 501 wxMenuBar *menu_bar = new wxMenuBar();
2b5f62a0 502 menu_bar->Append(file_menu, _T("&File"));
fdd3ed7a 503
ed673c6a 504 SetMenuBar( menu_bar );
fdd3ed7a 505
8520f137 506#if wxUSE_STATUSBAR
ed673c6a
RR
507 CreateStatusBar(2);
508 int widths[] = { -1, 100 };
509 SetStatusWidths( 2, widths );
8520f137 510#endif // wxUSE_STATUSBAR
fdd3ed7a 511
57ac1a56
RN
512 wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL );
513 // subsizer splits topsizer down the middle
514 wxBoxSizer *subsizer = new wxBoxSizer( wxVERTICAL );
2b5f62a0
VZ
515
516 // Setting an explicit size here is superfluous, it will be overridden
517 // by the sizer in any case.
b62ca03d 518 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(100,100) );
8a73bf3d 519
2b5f62a0
VZ
520 // This is done with ScrollRate/VirtualSize in MyCanvas ctor now,
521 // both should produce identical results.
522 //m_canvas->SetScrollbars( 10, 10, 50, 100 );
57ac1a56
RN
523
524 subsizer->Add( m_canvas, 1, wxEXPAND );
525 subsizer->Add( new MyAutoScrollWindow( this ), 1, wxEXPAND );
8a73bf3d
VZ
526
527 wxSizer *sizerBtm = new wxBoxSizer(wxHORIZONTAL);
528 sizerBtm->Add( new MyScrolledWindowDumb(this), 1, wxEXPAND );
529 sizerBtm->Add( new MyScrolledWindowSmart(this), 1, wxEXPAND );
57ac1a56
RN
530 subsizer->Add( sizerBtm, 1, wxEXPAND );
531
532 topsizer->Add( subsizer, 1, wxEXPAND );
533 topsizer->Add( new MyAutoTimedScrollingWindow( this ), 1, wxEXPAND );
ed673c6a 534
ed673c6a 535 SetSizer( topsizer );
fdd3ed7a
RR
536}
537
8e217128
RR
538void MyFrame::OnDeleteAll( wxCommandEvent &WXUNUSED(event) )
539{
540 m_canvas->DestroyChildren();
541}
542
543void MyFrame::OnInsertNew( wxCommandEvent &WXUNUSED(event) )
544{
b62ca03d 545 (void)new wxButton( m_canvas, wxID_ANY, _T("Hello"), wxPoint(100,100) );
8e217128
RR
546}
547
fdd3ed7a
RR
548void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
549{
b62ca03d 550 Close( true );
fdd3ed7a
RR
551}
552
553void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
554{
57ac1a56
RN
555 (void)wxMessageBox( _T("wxScroll demo\n")
556 _T("Robert Roebling (c) 1998\n")
557 _T("Autoscrolling examples\n")
558 _T("Ron Lee (c) 2002\n")
559 _T("Auto-timed-scrolling example\n")
560 _T("Matt Gregory (c) 2003\n"),
561 _T("About wxScroll Demo"),
562 wxICON_INFORMATION | wxOK );
fdd3ed7a
RR
563}
564
565//-----------------------------------------------------------------------------
566// MyApp
567//-----------------------------------------------------------------------------
568
569bool MyApp::OnInit()
570{
571 wxFrame *frame = new MyFrame();
b62ca03d 572 frame->Show( true );
fdd3ed7a 573
b62ca03d 574 return true;
fdd3ed7a
RR
575}
576
8a73bf3d
VZ
577// ----------------------------------------------------------------------------
578// MyScrolledWindowXXX
579// ----------------------------------------------------------------------------
580
8a73bf3d
VZ
581void MyScrolledWindowDumb::OnDraw(wxDC& dc)
582{
583 // this is useful to see which lines are redrawn
584 static size_t s_redrawCount = 0;
585 dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE);
586
587 wxCoord y = 0;
588 for ( size_t line = 0; line < m_nLines; line++ )
589 {
590 wxCoord yPhys;
591 CalcScrolledPosition(0, y, NULL, &yPhys);
592
593 dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
594 line, y, yPhys), 0, y);
595 y += m_hLine;
596 }
597}
598
599void MyScrolledWindowSmart::OnDraw(wxDC& dc)
600{
601 // this is useful to see which lines are redrawn
602 static size_t s_redrawCount = 0;
603 dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE);
604
605 // update region is always in device coords, translate to logical ones
606 wxRect rectUpdate = GetUpdateRegion().GetBox();
607 CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y,
608 &rectUpdate.x, &rectUpdate.y);
609
610 size_t lineFrom = rectUpdate.y / m_hLine,
611 lineTo = rectUpdate.GetBottom() / m_hLine;
612
613 if ( lineTo > m_nLines - 1)
614 lineTo = m_nLines - 1;
615
616 wxCoord y = lineFrom*m_hLine;
617 for ( size_t line = lineFrom; line <= lineTo; line++ )
618 {
619 wxCoord yPhys;
620 CalcScrolledPosition(0, y, NULL, &yPhys);
621
622 dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
623 line, y, yPhys), 0, y);
624 y += m_hLine;
625 }
626}
57ac1a56
RN
627
628// ----------------------------------------------------------------------------
629// MyAutoTimedScrollingWindow
630// ----------------------------------------------------------------------------
631
632BEGIN_EVENT_TABLE(MyAutoTimedScrollingWindow, wxScrolledWindow)
4ba64bde
RR
633 EVT_LEFT_DOWN(MyAutoTimedScrollingWindow::OnMouseLeftDown)
634 EVT_LEFT_UP(MyAutoTimedScrollingWindow::OnMouseLeftUp)
635 EVT_MOTION(MyAutoTimedScrollingWindow::OnMouseMove)
636 EVT_SCROLLWIN(MyAutoTimedScrollingWindow::OnScroll)
57ac1a56
RN
637END_EVENT_TABLE()
638
639MyAutoTimedScrollingWindow::MyAutoTimedScrollingWindow(wxWindow* parent)
640 : wxScrolledWindow(parent, -1, wxDefaultPosition, wxDefaultSize
641 //, wxSUNKEN_BORDER) // can't seem to do it this way
642 , wxVSCROLL | wxHSCROLL | wxSUNKEN_BORDER)
643 , m_selStart(-1, -1), m_cursor(-1, -1)
644 , m_font(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL
645 , wxFONTWEIGHT_NORMAL)
646{
647 wxClientDC dc(this);
648 // query dc for text size
649 dc.SetFont(m_font);
650 dc.GetTextExtent(wxString(_T("A")), &m_fontW, &m_fontH);
651 // set up the virtual window
652 SetScrollbars(m_fontW, m_fontH, sm_lineLen, sm_lineCnt);
653}
654
655wxRect MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
656 (wxRect updRect) const
657{
658 wxPoint pos(updRect.GetPosition());
659 pos = DeviceCoordsToGraphicalChars(pos);
660 updRect.x = pos.x;
661 updRect.y = pos.y;
662 updRect.width /= m_fontW;
663 updRect.height /= m_fontH;
664 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
665 // so an off-by-one correction is needed
666 ++updRect.width; // kludge
667 ++updRect.height; // kludge
668 return updRect;
669}
670
671wxPoint MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
672 (wxPoint pos) const
673{
674 pos.x /= m_fontW;
675 pos.y /= m_fontH;
676 int vX, vY;
677 GetViewStart(&vX, &vY);
678 pos.x += vX;
679 pos.y += vY;
680 return pos;
681}
682
683wxPoint MyAutoTimedScrollingWindow::GraphicalCharToDeviceCoords
684 (wxPoint pos) const
685{
686 int vX, vY;
687 GetViewStart(&vX, &vY);
688 pos.x -= vX;
689 pos.y -= vY;
690 pos.x *= m_fontW;
691 pos.y *= m_fontH;
692 return pos;
693}
694
695wxRect MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
696 (wxRect updRect) const
697{
698 wxPoint pos(updRect.GetPosition());
699 pos = LogicalCoordsToGraphicalChars(pos);
700 updRect.x = pos.x;
701 updRect.y = pos.y;
702 updRect.width /= m_fontW;
703 updRect.height /= m_fontH;
704 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
705 // so an off-by-one correction is needed
706 ++updRect.width; // kludge
707 ++updRect.height; // kludge
708 return updRect;
709}
710
711wxPoint MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
712 (wxPoint pos) const
713{
714 pos.x /= m_fontW;
715 pos.y /= m_fontH;
716 return pos;
717}
718
719wxPoint MyAutoTimedScrollingWindow::GraphicalCharToLogicalCoords
720 (wxPoint pos) const
721{
722 pos.x *= m_fontW;
723 pos.y *= m_fontH;
724 return pos;
725}
726
727void MyAutoTimedScrollingWindow::MyRefresh()
728{
729 static wxPoint lastSelStart(-1, -1), lastCursor(-1, -1);
730 // refresh last selected area (to deselect previously selected text)
731 wxRect lastUpdRect(
732 GraphicalCharToDeviceCoords(lastSelStart),
733 GraphicalCharToDeviceCoords(lastCursor)
734 );
735 // off-by-one corrections, necessary because it's not possible to know
736 // when to round up until rect is normalized by lastUpdRect constructor
737 lastUpdRect.width += m_fontW; // kludge
738 lastUpdRect.height += m_fontH; // kludge
739 // refresh currently selected (to select previously unselected text)
740 wxRect updRect(
741 GraphicalCharToDeviceCoords(m_selStart),
742 GraphicalCharToDeviceCoords(m_cursor)
743 );
744 // off-by-one corrections
745 updRect.width += m_fontW; // kludge
746 updRect.height += m_fontH; // kludge
747 // find necessary refresh areas
748 wxCoord rx = lastUpdRect.x;
749 wxCoord ry = lastUpdRect.y;
750 wxCoord rw = updRect.x - lastUpdRect.x;
751 wxCoord rh = lastUpdRect.height;
752 if (rw && rh) {
753 RefreshRect(DCNormalize(rx, ry, rw, rh));
754 }
755 rx = updRect.x;
756 ry = updRect.y + updRect.height;
757 rw= updRect.width;
758 rh = (lastUpdRect.y + lastUpdRect.height) - (updRect.y + updRect.height);
759 if (rw && rh) {
760 RefreshRect(DCNormalize(rx, ry, rw, rh));
761 }
762 rx = updRect.x + updRect.width;
763 ry = lastUpdRect.y;
764 rw = (lastUpdRect.x + lastUpdRect.width) - (updRect.x + updRect.width);
765 rh = lastUpdRect.height;
766 if (rw && rh) {
767 RefreshRect(DCNormalize(rx, ry, rw, rh));
768 }
769 rx = updRect.x;
770 ry = lastUpdRect.y;
771 rw = updRect.width;
772 rh = updRect.y - lastUpdRect.y;
773 if (rw && rh) {
774 RefreshRect(DCNormalize(rx, ry, rw, rh));
775 }
776 // update last
777 lastSelStart = m_selStart;
778 lastCursor = m_cursor;
779}
780
781bool MyAutoTimedScrollingWindow::IsSelected(int chX, int chY) const
782{
783 if (IsInside(chX, m_selStart.x, m_cursor.x)
784 && IsInside(chY, m_selStart.y, m_cursor.y)) {
785 return TRUE;
786 }
787 return FALSE;
788}
789
790bool MyAutoTimedScrollingWindow::IsInside(int k, int bound1, int bound2)
791{
792 if ((k >= bound1 && k <= bound2) || (k >= bound2 && k <= bound1)) {
793 return TRUE;
794 }
795 return FALSE;
796}
797
798wxRect MyAutoTimedScrollingWindow::DCNormalize(wxCoord x, wxCoord y
799 , wxCoord w, wxCoord h)
800{
801 // this is needed to get rid of the graphical remnants from the selection
802 // I think it's because DrawRectangle() excludes a pixel in either direction
803 const int kludge = 1;
804 // make (x, y) the top-left corner
805 if (w < 0) {
806 w = -w + kludge;
807 x -= w;
808 } else {
809 x -= kludge;
810 w += kludge;
811 }
812 if (h < 0) {
813 h = -h + kludge;
814 y -= h;
815 } else {
816 y -= kludge;
817 h += kludge;
818 }
819 return wxRect(x, y, w, h);
820}
821
822void MyAutoTimedScrollingWindow::OnDraw(wxDC& dc)
823{
824 dc.SetFont(m_font);
825 wxBrush normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)
826 , wxSOLID);
827 wxBrush selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)
828 , wxSOLID);
829 dc.SetPen(*wxTRANSPARENT_PEN);
830 // draw the characters
831 // 1. for each update region
832 for (wxRegionIterator upd(GetUpdateRegion()); upd; ++upd) {
57ac1a56
RN
833 wxRect updRect = upd.GetRect();
834 wxRect updRectInGChars(DeviceCoordsToGraphicalChars(updRect));
835 // 2. for each row of chars in the update region
836 for (int chY = updRectInGChars.y
837 ; chY <= updRectInGChars.y + updRectInGChars.height; ++chY) {
838 // 3. for each character in the row
839 for (int chX = updRectInGChars.x
840 ; chX <= updRectInGChars.x + updRectInGChars.width
841 ; ++chX) {
842 // 4. set up dc
843 if (IsSelected(chX, chY)) {
844 dc.SetBrush(selBrush);
845 dc.SetTextForeground( wxSystemSettings::GetColour
846 (wxSYS_COLOUR_HIGHLIGHTTEXT));
847 } else {
848 dc.SetBrush(normBrush);
849 dc.SetTextForeground( wxSystemSettings::GetColour
850 (wxSYS_COLOUR_WINDOWTEXT));
851 }
852 // 5. find position info
853 wxPoint charPos = GraphicalCharToLogicalCoords(wxPoint
854 (chX, chY));
855 // 6. draw!
856 dc.DrawRectangle(charPos.x, charPos.y, m_fontW, m_fontH);
857 if (chY < sm_lineCnt && chX < sm_lineLen) {
858 int charIndex = chY * sm_lineLen + chX;
859 dc.DrawText(wxString(sm_testData[charIndex])
860 , charPos.x, charPos.y);
861 }
862 }
863 }
864 }
865}
866
867void MyAutoTimedScrollingWindow::OnMouseLeftDown(wxMouseEvent& event)
868{
869 // initial press of mouse button sets the beginning of the selection
870 m_selStart = DeviceCoordsToGraphicalChars(event.GetPosition());
871 // set the cursor to the same position
872 m_cursor = m_selStart;
873 // draw/erase selection
874 MyRefresh();
875}
876
877void MyAutoTimedScrollingWindow::OnMouseLeftUp(wxMouseEvent& WXUNUSED(event))
878{
879 // this test is necessary
880 if (HasCapture()) {
881 // uncapture mouse
882 ReleaseMouse();
883 }
884}
885
886void MyAutoTimedScrollingWindow::OnMouseMove(wxMouseEvent& event)
887{
888 // if user is dragging
889 if (event.Dragging() && event.LeftIsDown()) {
890 // set the new cursor position
891 m_cursor = DeviceCoordsToGraphicalChars(event.GetPosition());
892 // draw/erase selection
893 MyRefresh();
894 // capture mouse to activate auto-scrolling
895 if (!HasCapture()) {
896 CaptureMouse();
897 }
898 }
899}
900
901void MyAutoTimedScrollingWindow::OnScroll(wxScrollWinEvent& event)
902{
903 // need to move the cursor when autoscrolling
904 // FIXME: the cursor also moves when the scrollbar arrows are clicked
905 if (HasCapture()) {
906 if (event.GetOrientation() == wxHORIZONTAL) {
687706f5 907 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) {
57ac1a56 908 --m_cursor.x;
687706f5 909 } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) {
57ac1a56
RN
910 ++m_cursor.x;
911 }
912 } else if (event.GetOrientation() == wxVERTICAL) {
687706f5 913 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) {
57ac1a56 914 --m_cursor.y;
687706f5 915 } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) {
57ac1a56
RN
916 ++m_cursor.y;
917 }
918 }
919 }
920 MyRefresh();
921 event.Skip();
922}
923
924const int MyAutoTimedScrollingWindow::sm_lineCnt = 125;
925const int MyAutoTimedScrollingWindow::sm_lineLen = 79;
926const wxChar* MyAutoTimedScrollingWindow::sm_testData = _T("\
927