1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxScrolledWindow sample
4 // Author: Robert Roebling
8 // Copyright: (C) 1998 Robert Roebling, 2002 Ron Lee, 2003 Matt Gregory
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/listctrl.h"
36 class MyCanvas
: public wxScrolledWindow
40 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
42 void OnPaint( wxPaintEvent
&event
);
43 void OnQueryPosition( wxCommandEvent
&event
);
44 void OnAddButton( wxCommandEvent
&event
);
45 void OnDeleteButton( wxCommandEvent
&event
);
46 void OnMoveButton( wxCommandEvent
&event
);
47 void OnScrollWin( wxCommandEvent
&event
);
48 void OnMouseRightDown( wxMouseEvent
&event
);
49 void OnMouseWheel( wxMouseEvent
&event
);
53 DECLARE_DYNAMIC_CLASS(MyCanvas
)
58 // ----------------------------------------------------------------------------
59 // Autoscrolling example.
60 // ----------------------------------------------------------------------------
62 // this class uses the 'virtual' size attribute along with an internal
63 // sizer to automatically set up scrollbars as needed
65 class MyAutoScrollWindow
: public wxScrolledWindow
73 MyAutoScrollWindow( wxWindow
*parent
);
75 void OnResizeClick( wxCommandEvent
&WXUNUSED( event
) );
81 // ----------------------------------------------------------------------------
82 // MyScrolledWindow classes: examples of wxScrolledWindow usage
83 // ----------------------------------------------------------------------------
85 // base class for both of them
86 class MyScrolledWindowBase
: public wxScrolledWindow
89 MyScrolledWindowBase(wxWindow
*parent
)
90 : wxScrolledWindow(parent
)
94 dc
.GetTextExtent(_T("Line 17"), NULL
, &m_hLine
);
98 // the height of one line on screen
101 // the number of lines we draw
105 // this class does "stupid" redrawing - it redraws everything each time
106 // and sets the scrollbar extent directly.
108 class MyScrolledWindowDumb
: public MyScrolledWindowBase
111 MyScrolledWindowDumb(wxWindow
*parent
) : MyScrolledWindowBase(parent
)
114 SetScrollbars(0, m_hLine
, 0, m_nLines
+ 1, 0, 0, true /* no refresh */);
117 virtual void OnDraw(wxDC
& dc
);
120 // this class does "smart" redrawing - only redraws the lines which must be
121 // redrawn and sets the scroll rate and virtual size to affect the
124 // Note that this class should produce identical results to the one above.
126 class MyScrolledWindowSmart
: public MyScrolledWindowBase
129 MyScrolledWindowSmart(wxWindow
*parent
) : MyScrolledWindowBase(parent
)
132 SetScrollRate( 0, m_hLine
);
133 SetVirtualSize( wxDefaultCoord
, ( m_nLines
+ 1 ) * m_hLine
);
136 virtual void OnDraw(wxDC
& dc
);
139 // ----------------------------------------------------------------------------
140 // MyAutoTimedScrollingWindow: implements a text viewer with simple blocksize
141 // selection to test auto-scrolling functionality
142 // ----------------------------------------------------------------------------
144 class MyAutoTimedScrollingWindow
: public wxScrolledWindow
146 protected: // member data
147 // test data variables
148 static const wxChar
* sm_testData
;
149 static const int sm_lineCnt
; // line count
150 static const int sm_lineLen
; // line length in characters
151 // sizes for graphical data
152 wxCoord m_fontH
, m_fontW
;
153 // selection tracking
154 wxPoint m_selStart
; // beginning of blockwise selection
155 wxPoint m_cursor
; // end of blockwise selection (mouse position)
157 protected: // gui stuff
161 MyAutoTimedScrollingWindow( wxWindow
* parent
);
162 wxRect
DeviceCoordsToGraphicalChars(wxRect updRect
) const;
163 wxPoint
DeviceCoordsToGraphicalChars(wxPoint pos
) const;
164 wxPoint
GraphicalCharToDeviceCoords(wxPoint pos
) const;
165 wxRect
LogicalCoordsToGraphicalChars(wxRect updRect
) const;
166 wxPoint
LogicalCoordsToGraphicalChars(wxPoint pos
) const;
167 wxPoint
GraphicalCharToLogicalCoords(wxPoint pos
) const;
169 bool IsSelected(int chX
, int chY
) const;
170 static bool IsInside(int k
, int bound1
, int bound2
);
171 static wxRect
DCNormalize(wxCoord x
, wxCoord y
, wxCoord w
, wxCoord h
);
173 protected: // event stuff
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
);
180 DECLARE_EVENT_TABLE()
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 class MyFrame
: public wxFrame
192 void OnAbout( wxCommandEvent
&event
);
193 void OnQuit( wxCommandEvent
&event
);
194 void OnDeleteAll( wxCommandEvent
&event
);
195 void OnInsertNew( wxCommandEvent
&event
);
200 DECLARE_DYNAMIC_CLASS(MyFrame
)
201 DECLARE_EVENT_TABLE()
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 class MyApp
: public wxApp
211 virtual bool OnInit();
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
223 const long ID_ADDBUTTON
= wxNewId();
224 const long ID_DELBUTTON
= wxNewId();
225 const long ID_MOVEBUTTON
= wxNewId();
226 const long ID_SCROLLWIN
= wxNewId();
227 const long ID_QUERYPOS
= wxNewId();
229 const long ID_NEWBUTTON
= wxNewId();
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
237 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
238 EVT_PAINT( MyCanvas::OnPaint
)
239 EVT_RIGHT_DOWN( MyCanvas::OnMouseRightDown
)
240 EVT_MOUSEWHEEL( MyCanvas::OnMouseWheel
)
241 EVT_BUTTON( ID_QUERYPOS
, MyCanvas::OnQueryPosition
)
242 EVT_BUTTON( ID_ADDBUTTON
, MyCanvas::OnAddButton
)
243 EVT_BUTTON( ID_DELBUTTON
, MyCanvas::OnDeleteButton
)
244 EVT_BUTTON( ID_MOVEBUTTON
, MyCanvas::OnMoveButton
)
245 EVT_BUTTON( ID_SCROLLWIN
, MyCanvas::OnScrollWin
)
248 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
249 const wxPoint
&pos
, const wxSize
&size
)
250 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
, _T("test canvas") )
252 SetScrollRate( 10, 10 );
253 SetVirtualSize( 500, 1000 );
255 (void) new wxButton( this, ID_ADDBUTTON
, _T("add button"), wxPoint(10,10) );
256 (void) new wxButton( this, ID_DELBUTTON
, _T("del button"), wxPoint(10,40) );
257 (void) new wxButton( this, ID_MOVEBUTTON
, _T("move button"), wxPoint(150,10) );
258 (void) new wxButton( this, ID_SCROLLWIN
, _T("scroll win"), wxPoint(250,10) );
271 m_button
= new wxButton( this, ID_QUERYPOS
, "Query position", wxPoint(10,110) );
273 (void) new wxTextCtrl( this, wxID_ANY
, "wxTextCtrl", wxPoint(10,150), wxSize(80,wxDefaultCoord
) );
275 (void) new wxRadioButton( this, wxID_ANY
, "Disable", wxPoint(10,190) );
277 (void) new wxComboBox( this, wxID_ANY
, "This", wxPoint(10,230), wxDefaultSize
, 5, choices
);
279 (void) new wxRadioBox( this, wxID_ANY
, "This", wxPoint(10,310), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_COLS
);
281 (void) new wxRadioBox( this, wxID_ANY
, "This", wxPoint(10,440), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_ROWS
);
283 wxListCtrl
*m_listCtrl
= new wxListCtrl(
284 this, wxID_ANY
, wxPoint(200, 110), wxSize(180, 120),
285 wxLC_REPORT
| wxSIMPLE_BORDER
| wxLC_SINGLE_SEL
);
287 m_listCtrl
->InsertColumn(0, "First", wxLIST_FORMAT_LEFT
, 90);
288 m_listCtrl
->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT
, 90);
290 for ( int i
=0; i
< 30; i
++)
293 sprintf(buf
, "Item %d", i
);
294 m_listCtrl
->InsertItem(i
, buf
);
296 m_listCtrl
->SetItemState( 3, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
298 (void) new wxListBox( this, wxID_ANY
, wxPoint(260,280), wxSize(120,120), 5, choices
, wxLB_ALWAYS_SB
);
302 wxPanel
*test
= new wxPanel( this, wxID_ANY
, wxPoint(10, 110), wxSize(130,50), wxSIMPLE_BORDER
| wxTAB_TRAVERSAL
);
303 test
->SetBackgroundColour( wxT("WHEAT") );
307 wxButton
*test2
= new wxButton( test
, wxID_ANY
, "Hallo", wxPoint(10,10) );
309 test
= new wxPanel( this, wxID_ANY
, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
310 test
->SetBackgroundColour( wxT("WHEAT") );
311 test
->SetCursor( wxCursor( wxCURSOR_NO_ENTRY
) );
312 test2
= new wxButton( test
, wxID_ANY
, "Hallo", wxPoint(10,10) );
313 test2
->SetCursor( wxCursor( wxCURSOR_PENCIL
) );
315 test
= new wxPanel( this, wxID_ANY
, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER
| wxTAB_TRAVERSAL
);
316 test
->SetBackgroundColour( wxT("WHEAT") );
317 test
->SetCursor( wxCursor( wxCURSOR_PENCIL
) );
318 test2
= new wxButton( test
, wxID_ANY
, "Hallo", wxPoint(10,10) );
319 test2
->SetCursor( wxCursor( wxCURSOR_NO_ENTRY
) );
323 SetBackgroundColour( wxT("BLUE") );
325 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
328 void MyCanvas::OnMouseRightDown( wxMouseEvent
&event
)
330 wxPoint
pt( event
.GetPosition() );
332 CalcUnscrolledPosition( pt
.x
, pt
.y
, &x
, &y
);
333 wxLogMessage( wxT("Mouse down event at: %d %d, scrolled: %d %d"), pt
.x
, pt
.y
, x
, y
);
336 void MyCanvas::OnMouseWheel( wxMouseEvent
&event
)
338 wxPoint
pt( event
.GetPosition() );
340 CalcUnscrolledPosition( pt
.x
, pt
.y
, &x
, &y
);
341 wxLogMessage( wxT("Mouse wheel event at: %d %d, scrolled: %d %d\n")
342 wxT("Rotation: %d, delta = %d"),
344 event
.GetWheelRotation(), event
.GetWheelDelta() );
349 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
351 wxPaintDC
dc( this );
354 dc
.DrawText( _T("Press mouse button to test calculations!"), 160, 50 );
356 dc
.DrawText( _T("Some text"), 140, 140 );
358 dc
.DrawRectangle( 100, 160, 200, 200 );
361 void MyCanvas::OnQueryPosition( wxCommandEvent
&WXUNUSED(event
) )
363 wxPoint
pt( m_button
->GetPosition() );
364 wxLogMessage( wxT("Position of \"Query position\" is %d %d"), pt
.x
, pt
.y
);
365 pt
= ClientToScreen( pt
);
366 wxLogMessage( wxT("Position of \"Query position\" on screen is %d %d"), pt
.x
, pt
.y
);
369 void MyCanvas::OnAddButton( wxCommandEvent
&WXUNUSED(event
) )
371 wxLogMessage( wxT("Inserting button at position 10,70...") );
372 wxButton
*button
= new wxButton( this, ID_NEWBUTTON
, wxT("new button"), wxPoint(10,70), wxSize(80,25) );
373 wxPoint
pt( button
->GetPosition() );
374 wxLogMessage( wxT("-> Position after inserting %d %d"), pt
.x
, pt
.y
);
377 void MyCanvas::OnDeleteButton( wxCommandEvent
&WXUNUSED(event
) )
379 wxLogMessage( wxT("Deleting button inserted with \"Add button\"...") );
380 wxWindow
*win
= FindWindow( ID_NEWBUTTON
);
384 wxLogMessage( wxT("-> No window with id = ID_NEWBUTTON found.") );
387 void MyCanvas::OnMoveButton( wxCommandEvent
&event
)
389 wxLogMessage( wxT("Moving button 10 pixels downward..") );
390 wxWindow
*win
= FindWindow( event
.GetId() );
391 wxPoint
pt( win
->GetPosition() );
392 wxLogMessage( wxT("-> Position before move is %d %d"), pt
.x
, pt
.y
);
393 win
->Move( wxDefaultCoord
, pt
.y
+ 10 );
394 pt
= win
->GetPosition();
395 wxLogMessage( wxT("-> Position after move is %d %d"), pt
.x
, pt
.y
);
398 void MyCanvas::OnScrollWin( wxCommandEvent
&WXUNUSED(event
) )
400 wxLogMessage( wxT("Scrolling 2 units up.\nThe white square and the controls should move equally!") );
402 GetViewStart( &x
, &y
);
403 Scroll( wxDefaultCoord
, y
+2 );
406 // ----------------------------------------------------------------------------
407 // MyAutoScrollWindow
408 // ----------------------------------------------------------------------------
410 const long ID_RESIZEBUTTON
= wxNewId();
411 const wxSize
SMALL_BUTTON( 100, 50 );
412 const wxSize
LARGE_BUTTON( 300, 100 );
414 BEGIN_EVENT_TABLE( MyAutoScrollWindow
, wxScrolledWindow
)
415 EVT_BUTTON( ID_RESIZEBUTTON
, MyAutoScrollWindow::OnResizeClick
)
418 MyAutoScrollWindow::MyAutoScrollWindow( wxWindow
*parent
)
419 : wxScrolledWindow( parent
, -1, wxDefaultPosition
, wxDefaultSize
,
420 wxSUNKEN_BORDER
|wxScrolledWindowStyle
)
422 SetBackgroundColour( wxT("GREEN") );
424 // Set the rate we'd like for scrolling.
426 SetScrollRate( 5, 5 );
428 // Populate a sizer with a 'resizing' button and some
429 // other static decoration
431 wxFlexGridSizer
*innersizer
= new wxFlexGridSizer( 2, 2 );
433 m_button
= new wxButton( this,
439 // We need to do this here, because wxADJUST_MINSIZE below
440 // will cause the initial size to be ignored for Best/Min size.
441 // It would be nice to fix the sizers to handle this a little
444 m_button
->SetSizeHints( SMALL_BUTTON
.GetWidth(), SMALL_BUTTON
.GetHeight() );
446 innersizer
->Add( m_button
,
448 wxALIGN_CENTER
| wxALL
| wxADJUST_MINSIZE
,
451 innersizer
->Add( new wxStaticText( this, wxID_ANY
, _T("This is just") ),
455 innersizer
->Add( new wxStaticText( this, wxID_ANY
, _T("some decoration") ),
459 innersizer
->Add( new wxStaticText( this, wxID_ANY
, _T("for you to scroll...") ),
463 // Then use the sizer to set the scrolled region size.
465 SetSizer( innersizer
);
468 void MyAutoScrollWindow::OnResizeClick( wxCommandEvent
&WXUNUSED( event
) )
470 // Arbitrarily resize the button to change the minimum size of
471 // the (scrolled) sizer.
473 if( m_button
->GetSize() == SMALL_BUTTON
)
474 m_button
->SetSizeHints( LARGE_BUTTON
.GetWidth(), LARGE_BUTTON
.GetHeight() );
476 m_button
->SetSizeHints( SMALL_BUTTON
.GetWidth(), SMALL_BUTTON
.GetHeight() );
478 // Force update layout and scrollbars, since nothing we do here
479 // necessarily generates a size event which would do it for us.
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 const long ID_QUIT
= wxID_EXIT
;
489 const long ID_ABOUT
= wxID_ABOUT
;
490 const long ID_DELETE_ALL
= 100;
491 const long ID_INSERT_NEW
= 101;
493 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
495 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
496 EVT_MENU (ID_DELETE_ALL
, MyFrame::OnDeleteAll
)
497 EVT_MENU (ID_INSERT_NEW
, MyFrame::OnInsertNew
)
498 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
499 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
503 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxScrolledWindow sample"),
504 wxPoint(20,20), wxSize(800,500) )
506 wxMenu
*file_menu
= new wxMenu();
507 file_menu
->Append( ID_DELETE_ALL
, _T("Delete all"));
508 file_menu
->Append( ID_INSERT_NEW
, _T("Insert new"));
509 file_menu
->Append( ID_ABOUT
, _T("&About.."));
510 file_menu
->Append( ID_QUIT
, _T("E&xit\tAlt-X"));
512 wxMenuBar
*menu_bar
= new wxMenuBar();
513 menu_bar
->Append(file_menu
, _T("&File"));
515 SetMenuBar( menu_bar
);
519 int widths
[] = { -1, 100 };
520 SetStatusWidths( 2, widths
);
521 #endif // wxUSE_STATUSBAR
523 wxBoxSizer
*topsizer
= new wxBoxSizer( wxHORIZONTAL
);
524 // subsizer splits topsizer down the middle
525 wxBoxSizer
*subsizer
= new wxBoxSizer( wxVERTICAL
);
527 // Setting an explicit size here is superfluous, it will be overridden
528 // by the sizer in any case.
529 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(100,100) );
531 // This is done with ScrollRate/VirtualSize in MyCanvas ctor now,
532 // both should produce identical results.
533 //m_canvas->SetScrollbars( 10, 10, 50, 100 );
535 subsizer
->Add( m_canvas
, 1, wxEXPAND
);
536 subsizer
->Add( new MyAutoScrollWindow( this ), 1, wxEXPAND
);
538 wxSizer
*sizerBtm
= new wxBoxSizer(wxHORIZONTAL
);
539 sizerBtm
->Add( new MyScrolledWindowDumb(this), 1, wxEXPAND
);
540 sizerBtm
->Add( new MyScrolledWindowSmart(this), 1, wxEXPAND
);
541 subsizer
->Add( sizerBtm
, 1, wxEXPAND
);
543 topsizer
->Add( subsizer
, 1, wxEXPAND
);
544 topsizer
->Add( new MyAutoTimedScrollingWindow( this ), 1, wxEXPAND
);
546 SetSizer( topsizer
);
549 void MyFrame::OnDeleteAll( wxCommandEvent
&WXUNUSED(event
) )
551 m_canvas
->DestroyChildren();
554 void MyFrame::OnInsertNew( wxCommandEvent
&WXUNUSED(event
) )
556 (void)new wxButton( m_canvas
, wxID_ANY
, _T("Hello"), wxPoint(100,100) );
559 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
564 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
566 (void)wxMessageBox( _T("wxScroll demo\n")
567 _T("Robert Roebling (c) 1998\n")
568 _T("Autoscrolling examples\n")
569 _T("Ron Lee (c) 2002\n")
570 _T("Auto-timed-scrolling example\n")
571 _T("Matt Gregory (c) 2003\n"),
572 _T("About wxScroll Demo"),
573 wxICON_INFORMATION
| wxOK
);
576 //-----------------------------------------------------------------------------
578 //-----------------------------------------------------------------------------
582 wxFrame
*frame
= new MyFrame();
588 // ----------------------------------------------------------------------------
589 // MyScrolledWindowXXX
590 // ----------------------------------------------------------------------------
592 void MyScrolledWindowDumb::OnDraw(wxDC
& dc
)
594 // this is useful to see which lines are redrawn
595 static size_t s_redrawCount
= 0;
596 dc
.SetTextForeground(s_redrawCount
++ % 2 ? *wxRED
: *wxBLUE
);
599 for ( size_t line
= 0; line
< m_nLines
; line
++ )
602 CalcScrolledPosition(0, y
, NULL
, &yPhys
);
604 dc
.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
605 line
, y
, yPhys
), 0, y
);
610 void MyScrolledWindowSmart::OnDraw(wxDC
& dc
)
612 // this is useful to see which lines are redrawn
613 static size_t s_redrawCount
= 0;
614 dc
.SetTextForeground(s_redrawCount
++ % 2 ? *wxRED
: *wxBLUE
);
616 // update region is always in device coords, translate to logical ones
617 wxRect rectUpdate
= GetUpdateRegion().GetBox();
618 CalcUnscrolledPosition(rectUpdate
.x
, rectUpdate
.y
,
619 &rectUpdate
.x
, &rectUpdate
.y
);
621 size_t lineFrom
= rectUpdate
.y
/ m_hLine
,
622 lineTo
= rectUpdate
.GetBottom() / m_hLine
;
624 if ( lineTo
> m_nLines
- 1)
625 lineTo
= m_nLines
- 1;
627 wxCoord y
= lineFrom
*m_hLine
;
628 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
631 CalcScrolledPosition(0, y
, NULL
, &yPhys
);
633 dc
.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
634 line
, y
, yPhys
), 0, y
);
639 // ----------------------------------------------------------------------------
640 // MyAutoTimedScrollingWindow
641 // ----------------------------------------------------------------------------
643 BEGIN_EVENT_TABLE(MyAutoTimedScrollingWindow
, wxScrolledWindow
)
644 EVT_LEFT_DOWN(MyAutoTimedScrollingWindow::OnMouseLeftDown
)
645 EVT_LEFT_UP(MyAutoTimedScrollingWindow::OnMouseLeftUp
)
646 EVT_MOTION(MyAutoTimedScrollingWindow::OnMouseMove
)
647 EVT_SCROLLWIN(MyAutoTimedScrollingWindow::OnScroll
)
650 MyAutoTimedScrollingWindow::MyAutoTimedScrollingWindow(wxWindow
* parent
)
651 : wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
652 //, wxSUNKEN_BORDER) // can't seem to do it this way
653 , wxVSCROLL
| wxHSCROLL
| wxSUNKEN_BORDER
)
654 , m_selStart(-1, -1), m_cursor(-1, -1)
655 , m_font(9, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
656 , wxFONTWEIGHT_NORMAL
)
659 // query dc for text size
661 dc
.GetTextExtent(wxString(_T("A")), &m_fontW
, &m_fontH
);
662 // set up the virtual window
663 SetScrollbars(m_fontW
, m_fontH
, sm_lineLen
, sm_lineCnt
);
666 wxRect
MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
667 (wxRect updRect
) const
669 wxPoint
pos(updRect
.GetPosition());
670 pos
= DeviceCoordsToGraphicalChars(pos
);
673 updRect
.width
/= m_fontW
;
674 updRect
.height
/= m_fontH
;
675 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
676 // so an off-by-one correction is needed
677 ++updRect
.width
; // kludge
678 ++updRect
.height
; // kludge
682 wxPoint
MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
688 GetViewStart(&vX
, &vY
);
694 wxPoint
MyAutoTimedScrollingWindow::GraphicalCharToDeviceCoords
698 GetViewStart(&vX
, &vY
);
706 wxRect
MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
707 (wxRect updRect
) const
709 wxPoint
pos(updRect
.GetPosition());
710 pos
= LogicalCoordsToGraphicalChars(pos
);
713 updRect
.width
/= m_fontW
;
714 updRect
.height
/= m_fontH
;
715 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
716 // so an off-by-one correction is needed
717 ++updRect
.width
; // kludge
718 ++updRect
.height
; // kludge
722 wxPoint
MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
730 wxPoint
MyAutoTimedScrollingWindow::GraphicalCharToLogicalCoords
738 void MyAutoTimedScrollingWindow::MyRefresh()
740 static wxPoint
lastSelStart(-1, -1), lastCursor(-1, -1);
741 // refresh last selected area (to deselect previously selected text)
743 GraphicalCharToDeviceCoords(lastSelStart
),
744 GraphicalCharToDeviceCoords(lastCursor
)
746 // off-by-one corrections, necessary because it's not possible to know
747 // when to round up until rect is normalized by lastUpdRect constructor
748 lastUpdRect
.width
+= m_fontW
; // kludge
749 lastUpdRect
.height
+= m_fontH
; // kludge
750 // refresh currently selected (to select previously unselected text)
752 GraphicalCharToDeviceCoords(m_selStart
),
753 GraphicalCharToDeviceCoords(m_cursor
)
755 // off-by-one corrections
756 updRect
.width
+= m_fontW
; // kludge
757 updRect
.height
+= m_fontH
; // kludge
758 // find necessary refresh areas
759 wxCoord rx
= lastUpdRect
.x
;
760 wxCoord ry
= lastUpdRect
.y
;
761 wxCoord rw
= updRect
.x
- lastUpdRect
.x
;
762 wxCoord rh
= lastUpdRect
.height
;
764 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
767 ry
= updRect
.y
+ updRect
.height
;
769 rh
= (lastUpdRect
.y
+ lastUpdRect
.height
) - (updRect
.y
+ updRect
.height
);
771 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
773 rx
= updRect
.x
+ updRect
.width
;
775 rw
= (lastUpdRect
.x
+ lastUpdRect
.width
) - (updRect
.x
+ updRect
.width
);
776 rh
= lastUpdRect
.height
;
778 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
783 rh
= updRect
.y
- lastUpdRect
.y
;
785 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
788 lastSelStart
= m_selStart
;
789 lastCursor
= m_cursor
;
792 bool MyAutoTimedScrollingWindow::IsSelected(int chX
, int chY
) const
794 if (IsInside(chX
, m_selStart
.x
, m_cursor
.x
)
795 && IsInside(chY
, m_selStart
.y
, m_cursor
.y
)) {
801 bool MyAutoTimedScrollingWindow::IsInside(int k
, int bound1
, int bound2
)
803 if ((k
>= bound1
&& k
<= bound2
) || (k
>= bound2
&& k
<= bound1
)) {
809 wxRect
MyAutoTimedScrollingWindow::DCNormalize(wxCoord x
, wxCoord y
810 , wxCoord w
, wxCoord h
)
812 // this is needed to get rid of the graphical remnants from the selection
813 // I think it's because DrawRectangle() excludes a pixel in either direction
814 const int kludge
= 1;
815 // make (x, y) the top-left corner
830 return wxRect(x
, y
, w
, h
);
833 void MyAutoTimedScrollingWindow::OnDraw(wxDC
& dc
)
836 wxBrush
normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
)
838 wxBrush
selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
)
840 dc
.SetPen(*wxTRANSPARENT_PEN
);
841 wxString str
= sm_testData
;
843 // draw the characters
844 // 1. for each update region
845 for (wxRegionIterator
upd(GetUpdateRegion()); upd
; ++upd
) {
846 wxRect updRect
= upd
.GetRect();
847 wxRect
updRectInGChars(DeviceCoordsToGraphicalChars(updRect
));
848 // 2. for each row of chars in the update region
849 for (int chY
= updRectInGChars
.y
850 ; chY
<= updRectInGChars
.y
+ updRectInGChars
.height
; ++chY
) {
851 // 3. for each character in the row
852 for (int chX
= updRectInGChars
.x
853 ; chX
<= updRectInGChars
.x
+ updRectInGChars
.width
856 if (IsSelected(chX
, chY
)) {
857 dc
.SetBrush(selBrush
);
858 dc
.SetTextForeground( wxSystemSettings::GetColour
859 (wxSYS_COLOUR_HIGHLIGHTTEXT
));
861 dc
.SetBrush(normBrush
);
862 dc
.SetTextForeground( wxSystemSettings::GetColour
863 (wxSYS_COLOUR_WINDOWTEXT
));
865 // 5. find position info
866 wxPoint charPos
= GraphicalCharToLogicalCoords(wxPoint
869 dc
.DrawRectangle(charPos
.x
, charPos
.y
, m_fontW
, m_fontH
);
870 size_t charIndex
= chY
* sm_lineLen
+ chX
;
871 if (chY
< sm_lineCnt
&&
873 charIndex
< str
.Length())
875 dc
.DrawText(str
.Mid(charIndex
,1),
876 charPos
.x
, charPos
.y
);
883 void MyAutoTimedScrollingWindow::OnMouseLeftDown(wxMouseEvent
& event
)
885 // initial press of mouse button sets the beginning of the selection
886 m_selStart
= DeviceCoordsToGraphicalChars(event
.GetPosition());
887 // set the cursor to the same position
888 m_cursor
= m_selStart
;
889 // draw/erase selection
893 void MyAutoTimedScrollingWindow::OnMouseLeftUp(wxMouseEvent
& WXUNUSED(event
))
895 // this test is necessary
902 void MyAutoTimedScrollingWindow::OnMouseMove(wxMouseEvent
& event
)
904 // if user is dragging
905 if (event
.Dragging() && event
.LeftIsDown()) {
906 // set the new cursor position
907 m_cursor
= DeviceCoordsToGraphicalChars(event
.GetPosition());
908 // draw/erase selection
910 // capture mouse to activate auto-scrolling
917 void MyAutoTimedScrollingWindow::OnScroll(wxScrollWinEvent
& event
)
919 // need to move the cursor when autoscrolling
920 // FIXME: the cursor also moves when the scrollbar arrows are clicked
922 if (event
.GetOrientation() == wxHORIZONTAL
) {
923 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
) {
925 } else if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
) {
928 } else if (event
.GetOrientation() == wxVERTICAL
) {
929 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
) {
931 } else if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
) {
940 const int MyAutoTimedScrollingWindow::sm_lineCnt
= 125;
941 const int MyAutoTimedScrollingWindow::sm_lineLen
= 79;
942 const wxChar
* MyAutoTimedScrollingWindow::sm_testData
=
943 _T("162 Cult of the genius out of vanity. Because we think well of ourselves, but ")
944 _T("nonetheless never suppose ourselves capable of producing a painting like one of ")
945 _T("Raphael's or a dramatic scene like one of Shakespeare's, we convince ourselves ")
946 _T("that the capacity to do so is quite extraordinarily marvelous, a wholly ")
947 _T("uncommon accident, or, if we are still religiously inclined, a mercy from on ")
948 _T("high. Thus our vanity, our self-love, promotes the cult of the genius: for only ")
949 _T("if we think of him as being very remote from us, as a miraculum, does he not ")
950 _T("aggrieve us (even Goethe, who was without envy, called Shakespeare his star of ")
951 _T("the most distant heights [\"William! Stern der schonsten Ferne\": from Goethe's, ")
952 _T("\"Between Two Worlds\"]; in regard to which one might recall the lines: \"the ")
953 _T("stars, these we do not desire\" [from Goethe's, \"Comfort in Tears\"]). But, aside ")
954 _T("from these suggestions of our vanity, the activity of the genius seems in no ")
955 _T("way fundamentally different from the activity of the inventor of machines, the ")
956 _T("scholar of astronomy or history, the master of tactics. All these activities ")
957 _T("are explicable if one pictures to oneself people whose thinking is active in ")
958 _T("one direction, who employ everything as material, who always zealously observe ")
959 _T("their own inner life and that of others, who perceive everywhere models and ")
960 _T("incentives, who never tire of combining together the means available to them. ")
961 _T("Genius too does nothing except learn first how to lay bricks then how to build, ")
962 _T("except continually seek for material and continually form itself around it. ")
963 _T("Every activity of man is amazingly complicated, not only that of the genius: ")
964 _T("but none is a \"miracle.\" Whence, then, the belief that genius exists only in ")
965 _T("the artist, orator and philosopher? that only they have \"intuition\"? (Whereby ")
966 _T("they are supposed to possess a kind of miraculous eyeglass with which they can ")
967 _T("see directly into \"the essence of the thing\"!) It is clear that people speak of ")
968 _T("genius only where the effects of the great intellect are most pleasant to them ")
969 _T("and where they have no desire to feel envious. To call someone \"divine\" means: ")
970 _T("\"here there is no need for us to compete.\" Then, everything finished and ")
971 _T("complete is regarded with admiration, everything still becoming is undervalued. ")
972 _T("But no one can see in the work of the artist how it has become; that is its ")
973 _T("advantage, for wherever one can see the act of becoming one grows somewhat ")
974 _T("cool. The finished and perfect art of representation repulses all thinking as ")
975 _T("to how it has become; it tyrannizes as present completeness and perfection. ")
976 _T("That is why the masters of the art of representation count above all as gifted ")
977 _T("with genius and why men of science do not. In reality, this evaluation of the ")
978 _T("former and undervaluation of the latter is only a piece of childishness in the ")
979 _T("realm of reason. ")
981 _T("163 The serious workman. Do not talk about giftedness, inborn talents! One can ")
982 _T("name great men of all kinds who were very little gifted. The acquired ")
983 _T("greatness, became \"geniuses\" (as we put it), through qualities the lack of ")
984 _T("which no one who knew what they were would boast of: they all possessed that ")
985 _T("seriousness of the efficient workman which first learns to construct the parts ")
986 _T("properly before it ventures to fashion a great whole; they allowed themselves ")
987 _T("time for it, because they took more pleasure in making the little, secondary ")
988 _T("things well than in the effect of a dazzling whole. the recipe for becoming a ")
989 _T("good novelist, for example, is easy to give, but to carry it out presupposes ")
990 _T("qualities one is accustomed to overlook when one says \"I do not have enough ")
991 _T("talent.\" One has only to make a hundred or so sketches for novels, none longer ")
992 _T("than two pages but of such distinctness that every word in them is necessary; ")
993 _T("one should write down anecdotes each day until one has learned how to give them ")
994 _T("the most pregnant and effective form; one should be tireless in collecting and ")
995 _T("describing human types and characters; one should above all relate things to ")
996 _T("others and listen to others relate, keeping one's eyes and ears open for the ")
997 _T("effect produced on those present, one should travel like a landscape painter or ")
998 _T("costume designer; one should excerpt for oneself out of the individual sciences ")
999 _T("everything that will produce an artistic effect when it is well described, one ")
1000 _T("should, finally, reflect on the motives of human actions, disdain no signpost ")
1001 _T("to instruction about them and be a collector of these things by day and night. ")
1002 _T("One should continue in this many-sided exercise some ten years: what is then ")
1003 _T("created in the workshop, however, will be fit to go out into the world. What, ")
1004 _T("however, do most people do? They begin, not with the parts, but with the whole. ")
1005 _T("Perhaps they chance to strike a right note, excite attention and from then on ")
1006 _T("strike worse and worse notes, for good, natural reasons. Sometimes, when the ")
1007 _T("character and intellect needed to formulate such a life-plan are lacking, fate ")
1008 _T("and need take their place and lead the future master step by step through all ")
1009 _T("the stipulations of his trade. ")
1011 _T("164 Peril and profit in the cult of the genius. The belief in great, superior, ")
1012 _T("fruitful spirits is not necessarily, yet nonetheless is very frequently ")
1013 _T("associated with that religious or semi-religious superstition that these ")
1014 _T("spirits are of supra-human origin and possess certain miraculous abilities by ")
1015 _T("virtue of which they acquire their knowledge by quite other means than the rest ")
1016 _T("of mankind. One ascribes to them, it seems, a direct view of the nature of the ")
1017 _T("world, as it were a hole in the cloak of appearance, and believes that, by ")
1018 _T("virtue of this miraculous seer's vision, they are able to communicate something ")
1019 _T("conclusive and decisive about man and the world without the toil and ")
1020 _T("rigorousness required by science. As long as there continue to be those who ")
1021 _T("believe in the miraculous in the domain of knowledge one can perhaps concede ")
1022 _T("that these people themselves derive some benefit from their belief, inasmuch as ")
1023 _T("through their unconditional subjection to the great spirits they create for ")
1024 _T("their own spirit during its time of development the finest form of discipline ")
1025 _T("and schooling. On the other hand, it is at least questionable whether the ")
1026 _T("superstitious belief in genius, in its privileges and special abilities, is of ")
1027 _T("benefit to the genius himself if it takes root in him. It is in any event a ")
1028 _T("dangerous sign when a man is assailed by awe of himself, whether it be the ")
1029 _T("celebrated Caesar's awe of Caesar or the awe of one's own genius now under ")
1030 _T("consideration; when the sacrificial incense which is properly rendered only to ")
1031 _T("a god penetrates the brain of the genius, so that his head begins to swim and ")
1032 _T("he comes to regard himself as something supra-human. The consequences that ")
1033 _T("slowly result are: the feeling of irresponsibility, of exceptional rights, the ")
1034 _T("belief that he confers a favor by his mere presence, insane rage when anyone ")
1035 _T("attempts even to compare him with others, let alone to rate him beneath them, ")
1036 _T("or to draw attention to lapses in his work. Because he ceases to practice ")
1037 _T("criticism of himself, at last one pinion after the other falls out of his ")
1038 _T("plumage: that superstitious eats at the roots of his powers and perhaps even ")
1039 _T("turns him into a hypocrite after his powers have fled from him. For the great ")
1040 _T("spirits themselves it is therefore probably more beneficial if they acquire an ")
1041 _T("insight into the nature and origin of their powers, if they grasp, that is to ")
1042 _T("say, what purely human qualities have come together in them and what fortunate ")
1043 _T("circumstances attended them: in the first place undiminished energy, resolute ")
1044 _T("application to individual goals, great personal courage, then the good fortune ")
1045 _T("to receive an upbringing which offered in the early years the finest teachers, ")
1046 _T("models and methods. To be sure, when their goal is the production of the ")
1047 _T("greatest possible effect, unclarity with regard to oneself and that ")
1048 _T("semi-insanity superadded to it has always achieved much; for what has been ")
1049 _T("admired and envied at all times has been that power in them by virtue of which ")
1050 _T("they render men will-less and sweep them away into the delusion that the ")
1051 _T("leaders they are following are supra-natural. Indeed, it elevates and inspires ")
1052 _T("men to believe that someone is in possession of supra-natural powers: to this ")
1053 _T("extent Plato was right to say [Plato: Phaedrus, 244a] that madness has brought ")
1054 _T("the greatest of blessings upon mankind. In rare individual cases this portion ")
1055 _T("of madness may, indeed, actually have been the means by which such a nature, ")
1056 _T("excessive in all directions, was held firmly together: in the life of ")
1057 _T("individuals, too, illusions that are in themselves poisons often play the role ")
1058 _T("of healers; yet, in the end, in the case of every \"genius\" who believes in his ")
1059 _T("own divinity the poison shows itself to the same degree as his \"genius\" grows ")
1060 _T("old: one may recall, for example, the case of Napoleon, whose nature certainly ")
1061 _T("grew into the mighty unity that sets him apart from all men of modern times ")
1062 _T("precisely through his belief in himself and his star and through the contempt ")
1063 _T("for men that flowed from it; until in the end, however, this same belief went ")
1064 _T("over into an almost insane fatalism, robbed him of his acuteness and swiftness ")
1065 _T("of perception, and became the cause of his destruction.");