]> git.saurik.com Git - wxWidgets.git/blame - samples/scroll/scroll.cpp
Artwork replacement (c) Julian Smart
[wxWidgets.git] / samples / scroll / scroll.cpp
CommitLineData
4e04f777
WS
1/////////////////////////////////////////////////////////////////////////////
2// Name: scroll.cpp
3// Purpose: wxScrolledWindow sample
4// Author: Robert Roebling
5// Modified by:
6// Created:
7// RCS-ID: $Id$
8// Copyright: (C) 1998 Robert Roebling, 2002 Ron Lee, 2003 Matt Gregory
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
fdd3ed7a
RR
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 );
4e04f777 132 SetVirtualSize( wxDefaultCoord, ( m_nLines + 1 ) * m_hLine );
2b5f62a0 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 );
4e04f777 393 Scroll( wxDefaultCoord, y+2 );
ed673c6a
RR
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 )
3fa5153e
RR
409 : wxScrolledWindow( parent, -1, wxDefaultPosition, wxDefaultSize,
410 wxSUNKEN_BORDER|wxScrolledWindowStyle )
2b5f62a0
VZ
411{
412 SetBackgroundColour( wxT("GREEN") );
413
414 // Set the rate we'd like for scrolling.
415
416 SetScrollRate( 5, 5 );
417
418 // Populate a sizer with a 'resizing' button and some
419 // other static decoration
420
421 wxFlexGridSizer *innersizer = new wxFlexGridSizer( 2, 2 );
422
423 m_button = new wxButton( this,
424 ID_RESIZEBUTTON,
425 _T("Press me"),
426 wxDefaultPosition,
427 SMALL_BUTTON );
428
429 // We need to do this here, because wxADJUST_MINSIZE below
430 // will cause the initial size to be ignored for Best/Min size.
431 // It would be nice to fix the sizers to handle this a little
432 // more cleanly.
433
434 m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() );
435
436 innersizer->Add( m_button,
437 0,
438 wxALIGN_CENTER | wxALL | wxADJUST_MINSIZE,
439 20 );
440
b62ca03d 441 innersizer->Add( new wxStaticText( this, wxID_ANY, _T("This is just") ),
2b5f62a0
VZ
442 0,
443 wxALIGN_CENTER );
444
b62ca03d 445 innersizer->Add( new wxStaticText( this, wxID_ANY, _T("some decoration") ),
2b5f62a0
VZ
446 0,
447 wxALIGN_CENTER );
448
b62ca03d 449 innersizer->Add( new wxStaticText( this, wxID_ANY, _T("for you to scroll...") ),
2b5f62a0
VZ
450 0,
451 wxALIGN_CENTER );
452
453 // Then use the sizer to set the scrolled region size.
454
455 SetSizer( innersizer );
456}
457
458void MyAutoScrollWindow::OnResizeClick( wxCommandEvent &WXUNUSED( event ) )
459{
460 // Arbitrarily resize the button to change the minimum size of
461 // the (scrolled) sizer.
462
463 if( m_button->GetSize() == SMALL_BUTTON )
464 m_button->SetSizeHints( LARGE_BUTTON.GetWidth(), LARGE_BUTTON.GetHeight() );
465 else
466 m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() );
467
468 // Force update layout and scrollbars, since nothing we do here
469 // necessarily generates a size event which would do it for us.
470
471 FitInside();
472}
473
57ac1a56 474// ----------------------------------------------------------------------------
fdd3ed7a 475// MyFrame
57ac1a56 476// ----------------------------------------------------------------------------
fdd3ed7a 477
2b5f62a0
VZ
478const long ID_QUIT = wxNewId();
479const long ID_ABOUT = wxNewId();
480const long ID_DELETE_ALL = wxNewId();
481const long ID_INSERT_NEW = wxNewId();
fdd3ed7a
RR
482
483IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
484
485BEGIN_EVENT_TABLE(MyFrame,wxFrame)
8e217128
RR
486 EVT_MENU (ID_DELETE_ALL, MyFrame::OnDeleteAll)
487 EVT_MENU (ID_INSERT_NEW, MyFrame::OnInsertNew)
fdd3ed7a
RR
488 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
489 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
490END_EVENT_TABLE()
491
492MyFrame::MyFrame()
b62ca03d 493 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"),
57ac1a56 494 wxPoint(20,20), wxSize(800,500) )
fdd3ed7a 495{
ed673c6a 496 wxMenu *file_menu = new wxMenu();
2b5f62a0
VZ
497 file_menu->Append( ID_DELETE_ALL, _T("Delete all"));
498 file_menu->Append( ID_INSERT_NEW, _T("Insert new"));
499 file_menu->Append( ID_ABOUT, _T("&About.."));
500 file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X"));
fdd3ed7a 501
ed673c6a 502 wxMenuBar *menu_bar = new wxMenuBar();
2b5f62a0 503 menu_bar->Append(file_menu, _T("&File"));
fdd3ed7a 504
ed673c6a 505 SetMenuBar( menu_bar );
fdd3ed7a 506
8520f137 507#if wxUSE_STATUSBAR
ed673c6a
RR
508 CreateStatusBar(2);
509 int widths[] = { -1, 100 };
510 SetStatusWidths( 2, widths );
8520f137 511#endif // wxUSE_STATUSBAR
fdd3ed7a 512
57ac1a56
RN
513 wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL );
514 // subsizer splits topsizer down the middle
515 wxBoxSizer *subsizer = new wxBoxSizer( wxVERTICAL );
2b5f62a0
VZ
516
517 // Setting an explicit size here is superfluous, it will be overridden
518 // by the sizer in any case.
b62ca03d 519 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(100,100) );
8a73bf3d 520
2b5f62a0
VZ
521 // This is done with ScrollRate/VirtualSize in MyCanvas ctor now,
522 // both should produce identical results.
523 //m_canvas->SetScrollbars( 10, 10, 50, 100 );
4e04f777 524
57ac1a56
RN
525 subsizer->Add( m_canvas, 1, wxEXPAND );
526 subsizer->Add( new MyAutoScrollWindow( this ), 1, wxEXPAND );
8a73bf3d
VZ
527
528 wxSizer *sizerBtm = new wxBoxSizer(wxHORIZONTAL);
529 sizerBtm->Add( new MyScrolledWindowDumb(this), 1, wxEXPAND );
530 sizerBtm->Add( new MyScrolledWindowSmart(this), 1, wxEXPAND );
57ac1a56
RN
531 subsizer->Add( sizerBtm, 1, wxEXPAND );
532
533 topsizer->Add( subsizer, 1, wxEXPAND );
534 topsizer->Add( new MyAutoTimedScrollingWindow( this ), 1, wxEXPAND );
ed673c6a 535
ed673c6a 536 SetSizer( topsizer );
fdd3ed7a
RR
537}
538
8e217128
RR
539void MyFrame::OnDeleteAll( wxCommandEvent &WXUNUSED(event) )
540{
541 m_canvas->DestroyChildren();
542}
543
544void MyFrame::OnInsertNew( wxCommandEvent &WXUNUSED(event) )
545{
b62ca03d 546 (void)new wxButton( m_canvas, wxID_ANY, _T("Hello"), wxPoint(100,100) );
8e217128
RR
547}
548
fdd3ed7a
RR
549void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
550{
b62ca03d 551 Close( true );
fdd3ed7a
RR
552}
553
554void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
555{
57ac1a56
RN
556 (void)wxMessageBox( _T("wxScroll demo\n")
557 _T("Robert Roebling (c) 1998\n")
558 _T("Autoscrolling examples\n")
559 _T("Ron Lee (c) 2002\n")
560 _T("Auto-timed-scrolling example\n")
561 _T("Matt Gregory (c) 2003\n"),
562 _T("About wxScroll Demo"),
563 wxICON_INFORMATION | wxOK );
fdd3ed7a
RR
564}
565
566//-----------------------------------------------------------------------------
567// MyApp
568//-----------------------------------------------------------------------------
569
570bool MyApp::OnInit()
571{
572 wxFrame *frame = new MyFrame();
b62ca03d 573 frame->Show( true );
fdd3ed7a 574
b62ca03d 575 return true;
fdd3ed7a
RR
576}
577
8a73bf3d
VZ
578// ----------------------------------------------------------------------------
579// MyScrolledWindowXXX
580// ----------------------------------------------------------------------------
581
8a73bf3d
VZ
582void MyScrolledWindowDumb::OnDraw(wxDC& dc)
583{
584 // this is useful to see which lines are redrawn
585 static size_t s_redrawCount = 0;
586 dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE);
587
588 wxCoord y = 0;
589 for ( size_t line = 0; line < m_nLines; line++ )
590 {
591 wxCoord yPhys;
592 CalcScrolledPosition(0, y, NULL, &yPhys);
593
594 dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
595 line, y, yPhys), 0, y);
596 y += m_hLine;
597 }
598}
599
600void MyScrolledWindowSmart::OnDraw(wxDC& dc)
601{
602 // this is useful to see which lines are redrawn
603 static size_t s_redrawCount = 0;
604 dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE);
605
606 // update region is always in device coords, translate to logical ones
607 wxRect rectUpdate = GetUpdateRegion().GetBox();
608 CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y,
609 &rectUpdate.x, &rectUpdate.y);
610
611 size_t lineFrom = rectUpdate.y / m_hLine,
612 lineTo = rectUpdate.GetBottom() / m_hLine;
613
614 if ( lineTo > m_nLines - 1)
615 lineTo = m_nLines - 1;
616
617 wxCoord y = lineFrom*m_hLine;
618 for ( size_t line = lineFrom; line <= lineTo; line++ )
619 {
620 wxCoord yPhys;
621 CalcScrolledPosition(0, y, NULL, &yPhys);
622
623 dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
624 line, y, yPhys), 0, y);
625 y += m_hLine;
626 }
627}
57ac1a56
RN
628
629// ----------------------------------------------------------------------------
630// MyAutoTimedScrollingWindow
631// ----------------------------------------------------------------------------
632
633BEGIN_EVENT_TABLE(MyAutoTimedScrollingWindow, wxScrolledWindow)
4ba64bde
RR
634 EVT_LEFT_DOWN(MyAutoTimedScrollingWindow::OnMouseLeftDown)
635 EVT_LEFT_UP(MyAutoTimedScrollingWindow::OnMouseLeftUp)
636 EVT_MOTION(MyAutoTimedScrollingWindow::OnMouseMove)
637 EVT_SCROLLWIN(MyAutoTimedScrollingWindow::OnScroll)
57ac1a56
RN
638END_EVENT_TABLE()
639
640MyAutoTimedScrollingWindow::MyAutoTimedScrollingWindow(wxWindow* parent)
4e04f777 641 : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize
57ac1a56
RN
642 //, wxSUNKEN_BORDER) // can't seem to do it this way
643 , wxVSCROLL | wxHSCROLL | wxSUNKEN_BORDER)
644 , m_selStart(-1, -1), m_cursor(-1, -1)
645 , m_font(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL
646 , wxFONTWEIGHT_NORMAL)
647{
648 wxClientDC dc(this);
649 // query dc for text size
650 dc.SetFont(m_font);
651 dc.GetTextExtent(wxString(_T("A")), &m_fontW, &m_fontH);
652 // set up the virtual window
653 SetScrollbars(m_fontW, m_fontH, sm_lineLen, sm_lineCnt);
654}
655
656wxRect MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
657 (wxRect updRect) const
658{
659 wxPoint pos(updRect.GetPosition());
660 pos = DeviceCoordsToGraphicalChars(pos);
661 updRect.x = pos.x;
662 updRect.y = pos.y;
663 updRect.width /= m_fontW;
664 updRect.height /= m_fontH;
665 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
666 // so an off-by-one correction is needed
667 ++updRect.width; // kludge
668 ++updRect.height; // kludge
669 return updRect;
670}
671
672wxPoint MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
673 (wxPoint pos) const
674{
675 pos.x /= m_fontW;
676 pos.y /= m_fontH;
677 int vX, vY;
678 GetViewStart(&vX, &vY);
679 pos.x += vX;
680 pos.y += vY;
681 return pos;
682}
683
684wxPoint MyAutoTimedScrollingWindow::GraphicalCharToDeviceCoords
685 (wxPoint pos) const
686{
687 int vX, vY;
688 GetViewStart(&vX, &vY);
689 pos.x -= vX;
690 pos.y -= vY;
691 pos.x *= m_fontW;
692 pos.y *= m_fontH;
693 return pos;
694}
695
696wxRect MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
697 (wxRect updRect) const
698{
699 wxPoint pos(updRect.GetPosition());
700 pos = LogicalCoordsToGraphicalChars(pos);
701 updRect.x = pos.x;
702 updRect.y = pos.y;
703 updRect.width /= m_fontW;
704 updRect.height /= m_fontH;
705 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
706 // so an off-by-one correction is needed
707 ++updRect.width; // kludge
708 ++updRect.height; // kludge
709 return updRect;
710}
711
712wxPoint MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
713 (wxPoint pos) const
714{
715 pos.x /= m_fontW;
716 pos.y /= m_fontH;
717 return pos;
718}
719
720wxPoint MyAutoTimedScrollingWindow::GraphicalCharToLogicalCoords
721 (wxPoint pos) const
722{
723 pos.x *= m_fontW;
724 pos.y *= m_fontH;
725 return pos;
726}
727
728void MyAutoTimedScrollingWindow::MyRefresh()
729{
730 static wxPoint lastSelStart(-1, -1), lastCursor(-1, -1);
731 // refresh last selected area (to deselect previously selected text)
732 wxRect lastUpdRect(
733 GraphicalCharToDeviceCoords(lastSelStart),
734 GraphicalCharToDeviceCoords(lastCursor)
735 );
736 // off-by-one corrections, necessary because it's not possible to know
737 // when to round up until rect is normalized by lastUpdRect constructor
738 lastUpdRect.width += m_fontW; // kludge
739 lastUpdRect.height += m_fontH; // kludge
740 // refresh currently selected (to select previously unselected text)
741 wxRect updRect(
742 GraphicalCharToDeviceCoords(m_selStart),
743 GraphicalCharToDeviceCoords(m_cursor)
744 );
745 // off-by-one corrections
746 updRect.width += m_fontW; // kludge
747 updRect.height += m_fontH; // kludge
748 // find necessary refresh areas
749 wxCoord rx = lastUpdRect.x;
750 wxCoord ry = lastUpdRect.y;
751 wxCoord rw = updRect.x - lastUpdRect.x;
752 wxCoord rh = lastUpdRect.height;
753 if (rw && rh) {
754 RefreshRect(DCNormalize(rx, ry, rw, rh));
755 }
756 rx = updRect.x;
757 ry = updRect.y + updRect.height;
758 rw= updRect.width;
759 rh = (lastUpdRect.y + lastUpdRect.height) - (updRect.y + updRect.height);
760 if (rw && rh) {
761 RefreshRect(DCNormalize(rx, ry, rw, rh));
762 }
763 rx = updRect.x + updRect.width;
764 ry = lastUpdRect.y;
765 rw = (lastUpdRect.x + lastUpdRect.width) - (updRect.x + updRect.width);
766 rh = lastUpdRect.height;
767 if (rw && rh) {
768 RefreshRect(DCNormalize(rx, ry, rw, rh));
769 }
770 rx = updRect.x;
771 ry = lastUpdRect.y;
772 rw = updRect.width;
773 rh = updRect.y - lastUpdRect.y;
774 if (rw && rh) {
775 RefreshRect(DCNormalize(rx, ry, rw, rh));
776 }
777 // update last
778 lastSelStart = m_selStart;
779 lastCursor = m_cursor;
780}
781
782bool MyAutoTimedScrollingWindow::IsSelected(int chX, int chY) const
783{
784 if (IsInside(chX, m_selStart.x, m_cursor.x)
785 && IsInside(chY, m_selStart.y, m_cursor.y)) {
4e04f777 786 return true;
57ac1a56 787 }
4e04f777 788 return false;
57ac1a56
RN
789}
790
791bool MyAutoTimedScrollingWindow::IsInside(int k, int bound1, int bound2)
792{
793 if ((k >= bound1 && k <= bound2) || (k >= bound2 && k <= bound1)) {
4e04f777 794 return true;
57ac1a56 795 }
4e04f777 796 return false;
57ac1a56
RN
797}
798
799wxRect MyAutoTimedScrollingWindow::DCNormalize(wxCoord x, wxCoord y
800 , wxCoord w, wxCoord h)
801{
802 // this is needed to get rid of the graphical remnants from the selection
803 // I think it's because DrawRectangle() excludes a pixel in either direction
804 const int kludge = 1;
805 // make (x, y) the top-left corner
806 if (w < 0) {
807 w = -w + kludge;
808 x -= w;
809 } else {
810 x -= kludge;
811 w += kludge;
812 }
813 if (h < 0) {
814 h = -h + kludge;
815 y -= h;
816 } else {
817 y -= kludge;
818 h += kludge;
819 }
820 return wxRect(x, y, w, h);
821}
822
823void MyAutoTimedScrollingWindow::OnDraw(wxDC& dc)
824{
825 dc.SetFont(m_font);
826 wxBrush normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)
827 , wxSOLID);
828 wxBrush selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)
829 , wxSOLID);
830 dc.SetPen(*wxTRANSPARENT_PEN);
4e04f777
WS
831 wxString str = sm_testData;
832
57ac1a56
RN
833 // draw the characters
834 // 1. for each update region
835 for (wxRegionIterator upd(GetUpdateRegion()); upd; ++upd) {
57ac1a56
RN
836 wxRect updRect = upd.GetRect();
837 wxRect updRectInGChars(DeviceCoordsToGraphicalChars(updRect));
838 // 2. for each row of chars in the update region
839 for (int chY = updRectInGChars.y
840 ; chY <= updRectInGChars.y + updRectInGChars.height; ++chY) {
841 // 3. for each character in the row
842 for (int chX = updRectInGChars.x
843 ; chX <= updRectInGChars.x + updRectInGChars.width
844 ; ++chX) {
845 // 4. set up dc
846 if (IsSelected(chX, chY)) {
847 dc.SetBrush(selBrush);
848 dc.SetTextForeground( wxSystemSettings::GetColour
849 (wxSYS_COLOUR_HIGHLIGHTTEXT));
850 } else {
851 dc.SetBrush(normBrush);
852 dc.SetTextForeground( wxSystemSettings::GetColour
853 (wxSYS_COLOUR_WINDOWTEXT));
854 }
855 // 5. find position info
856 wxPoint charPos = GraphicalCharToLogicalCoords(wxPoint
857 (chX, chY));
858 // 6. draw!
859 dc.DrawRectangle(charPos.x, charPos.y, m_fontW, m_fontH);
4e04f777
WS
860 size_t charIndex = chY * sm_lineLen + chX;
861 if (chY < sm_lineCnt &&
862 chX < sm_lineLen &&
863 charIndex < str.Length())
864 {
865 dc.DrawText(str.Mid(charIndex,1),
866 charPos.x, charPos.y);
57ac1a56
RN
867 }
868 }
869 }
870 }
871}
872
873void MyAutoTimedScrollingWindow::OnMouseLeftDown(wxMouseEvent& event)
874{
875 // initial press of mouse button sets the beginning of the selection
876 m_selStart = DeviceCoordsToGraphicalChars(event.GetPosition());
877 // set the cursor to the same position
878 m_cursor = m_selStart;
879 // draw/erase selection
880 MyRefresh();
881}
882
883void MyAutoTimedScrollingWindow::OnMouseLeftUp(wxMouseEvent& WXUNUSED(event))
884{
885 // this test is necessary
886 if (HasCapture()) {
887 // uncapture mouse
888 ReleaseMouse();
889 }
890}
891
892void MyAutoTimedScrollingWindow::OnMouseMove(wxMouseEvent& event)
893{
894 // if user is dragging
895 if (event.Dragging() && event.LeftIsDown()) {
896 // set the new cursor position
897 m_cursor = DeviceCoordsToGraphicalChars(event.GetPosition());
898 // draw/erase selection
899 MyRefresh();
900 // capture mouse to activate auto-scrolling
901 if (!HasCapture()) {
902 CaptureMouse();
903 }
904 }
905}
906
907void MyAutoTimedScrollingWindow::OnScroll(wxScrollWinEvent& event)
908{
909 // need to move the cursor when autoscrolling
910 // FIXME: the cursor also moves when the scrollbar arrows are clicked
911 if (HasCapture()) {
912 if (event.GetOrientation() == wxHORIZONTAL) {
687706f5 913 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) {
57ac1a56 914 --m_cursor.x;
687706f5 915 } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) {
57ac1a56
RN
916 ++m_cursor.x;
917 }
918 } else if (event.GetOrientation() == wxVERTICAL) {
687706f5 919 if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) {
57ac1a56 920 --m_cursor.y;
687706f5 921 } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) {
57ac1a56
RN
922 ++m_cursor.y;
923 }
924 }
925 }
926 MyRefresh();
927 event.Skip();
928}
929
930const int MyAutoTimedScrollingWindow::sm_lineCnt = 125;
931const int MyAutoTimedScrollingWindow::sm_lineLen = 79;
4e04f777
WS
932const wxChar* MyAutoTimedScrollingWindow::sm_testData =
933