1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxScrolled sample
4 // Author: Robert Roebling
6 // Copyright: (C) 1998 Robert Roebling, 2002 Ron Lee, 2003 Matt Gregory
7 // (C) 2008 Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
23 #include "wx/tglbtn.h"
25 #ifndef wxHAS_IMAGES_IN_RESOURCES
26 #include "../sample.xpm"
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 // MySimpleCanvas: a scrolled window which draws a simple rectangle
34 class MySimpleCanvas
: public wxScrolled
<wxWindow
>
39 // these numbers are not multiple of 10 (our scroll step) to test for
40 // the absence of rounding errors (e.g. we should have one more page
41 // than WIDTH/10 to show the right side of the rectangle)
46 MySimpleCanvas(wxWindow
*parent
)
47 : wxScrolled
<wxWindow
>(parent
, wxID_ANY
)
49 SetScrollRate( 10, 10 );
50 SetVirtualSize( WIDTH
, HEIGHT
);
51 SetBackgroundColour( *wxWHITE
);
53 Connect(wxEVT_PAINT
, wxPaintEventHandler(MySimpleCanvas::OnPaint
));
57 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
61 // this call is vital: it adjusts the dc to account for the current
65 dc
.SetPen( *wxRED_PEN
);
66 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
67 dc
.DrawRectangle( 0, 0, WIDTH
, HEIGHT
);
72 // MySimpleFrame: a frame which contains a MySimpleCanvas
73 class MySimpleFrame
: public wxFrame
76 MySimpleFrame(wxWindow
*parent
)
77 : wxFrame(parent
, wxID_ANY
, "MySimpleCanvas")
79 new MySimpleCanvas(this);
81 // ensure that we have scrollbars initially
82 SetClientSize(MySimpleCanvas::WIDTH
/2, MySimpleCanvas::HEIGHT
/2);
88 // ----------------------------------------------------------------------
89 // a more complex example
90 // ----------------------------------------------------------------------
93 class MyCanvas
: public wxScrolled
<wxPanel
>
96 MyCanvas(wxWindow
*parent
);
99 void OnPaint(wxPaintEvent
& event
);
100 void OnQueryPosition(wxCommandEvent
& event
);
101 void OnAddButton(wxCommandEvent
& event
);
102 void OnDeleteButton(wxCommandEvent
& event
);
103 void OnMoveButton(wxCommandEvent
& event
);
104 void OnScrollWin(wxCommandEvent
& event
);
105 void OnMouseRightDown(wxMouseEvent
& event
);
106 void OnMouseWheel(wxMouseEvent
& event
);
110 DECLARE_EVENT_TABLE()
113 class MyCanvasFrame
: public wxFrame
116 MyCanvasFrame(wxWindow
*parent
)
117 : wxFrame(parent
, wxID_ANY
, "MyCanvas")
119 m_canvas
= new MyCanvas(this);
121 wxMenu
*menuFile
= new wxMenu();
122 menuFile
->Append(wxID_DELETE
, "&Delete all");
123 menuFile
->Append(wxID_NEW
, "Insert &new");
125 wxMenuBar
*mbar
= new wxMenuBar();
126 mbar
->Append(menuFile
, "&File");
129 Connect(wxID_DELETE
, wxEVT_COMMAND_MENU_SELECTED
,
130 wxCommandEventHandler(MyCanvasFrame::OnDeleteAll
));
131 Connect(wxID_NEW
, wxEVT_COMMAND_MENU_SELECTED
,
132 wxCommandEventHandler(MyCanvasFrame::OnInsertNew
));
138 void OnDeleteAll(wxCommandEvent
& WXUNUSED(event
))
140 m_canvas
->DestroyChildren();
143 void OnInsertNew(wxCommandEvent
& WXUNUSED(event
))
145 (void)new wxButton(m_canvas
, wxID_ANY
, "Hello", wxPoint(100,100));
151 // ----------------------------------------------------------------------------
152 // example using sizers with wxScrolled
153 // ----------------------------------------------------------------------------
155 const wxSize
SMALL_BUTTON( 100, 50 );
156 const wxSize
LARGE_BUTTON( 300, 200 );
158 class MySizerScrolledWindow
: public wxScrolled
<wxWindow
>
161 MySizerScrolledWindow(wxWindow
*parent
);
164 // this button can be clicked to change its own size in the handler below,
165 // the window size will be automatically adjusted to fit the button
168 void OnResizeClick(wxCommandEvent
& event
);
171 class MySizerFrame
: public wxFrame
174 MySizerFrame(wxWindow
*parent
)
175 : wxFrame(parent
, wxID_ANY
, "MySizerScrolledWindow")
177 new MySizerScrolledWindow(this);
179 // ensure that the scrollbars appear when the button becomes large
180 SetClientSize(LARGE_BUTTON
/2);
185 // ----------------------------------------------------------------------------
186 // example showing scrolling only part of the window
187 // ----------------------------------------------------------------------------
189 // this window consists of an empty space in its corner, column labels window
190 // along its top, row labels window along its left hand side and a canvas in
191 // the remaining space
193 class MySubColLabels
: public wxWindow
196 MySubColLabels(wxScrolled
<wxWindow
> *parent
)
197 : wxWindow(parent
, wxID_ANY
)
201 Connect(wxEVT_PAINT
, wxPaintEventHandler(MySubColLabels::OnPaint
));
205 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
209 // This is wrong.. it will translate both x and y if the
210 // window is scrolled, the label windows are active in one
211 // direction only. Do the action below instead -- RL.
212 //m_owner->PrepareDC( dc );
214 int xScrollUnits
, xOrigin
;
216 m_owner
->GetViewStart( &xOrigin
, 0 );
217 m_owner
->GetScrollPixelsPerUnit( &xScrollUnits
, 0 );
218 dc
.SetDeviceOrigin( -xOrigin
* xScrollUnits
, 0 );
220 dc
.DrawText("Column 1", 5, 5);
221 dc
.DrawText("Column 2", 105, 5);
222 dc
.DrawText("Column 3", 205, 5);
225 wxScrolled
<wxWindow
> *m_owner
;
228 class MySubRowLabels
: public wxWindow
231 MySubRowLabels(wxScrolled
<wxWindow
> *parent
)
232 : wxWindow(parent
, wxID_ANY
)
236 Connect(wxEVT_PAINT
, wxPaintEventHandler(MySubRowLabels::OnPaint
));
240 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
244 // This is wrong.. it will translate both x and y if the
245 // window is scrolled, the label windows are active in one
246 // direction only. Do the action below instead -- RL.
247 //m_owner->PrepareDC( dc );
249 int yScrollUnits
, yOrigin
;
251 m_owner
->GetViewStart( 0, &yOrigin
);
252 m_owner
->GetScrollPixelsPerUnit( 0, &yScrollUnits
);
253 dc
.SetDeviceOrigin( 0, -yOrigin
* yScrollUnits
);
255 dc
.DrawText("Row 1", 5, 5);
256 dc
.DrawText("Row 2", 5, 30);
257 dc
.DrawText("Row 3", 5, 55);
258 dc
.DrawText("Row 4", 5, 80);
259 dc
.DrawText("Row 5", 5, 105);
260 dc
.DrawText("Row 6", 5, 130);
263 wxScrolled
<wxWindow
> *m_owner
;
266 class MySubCanvas
: public wxPanel
269 MySubCanvas(wxScrolled
<wxWindow
> *parent
, wxWindow
*cols
, wxWindow
*rows
)
270 : wxPanel(parent
, wxID_ANY
)
276 (void)new wxButton(this, wxID_ANY
, "Hallo I",
277 wxPoint(0,50), wxSize(100,25) );
278 (void)new wxButton(this, wxID_ANY
, "Hallo II",
279 wxPoint(200,50), wxSize(100,25) );
281 (void)new wxTextCtrl(this, wxID_ANY
, "Text I",
282 wxPoint(0,100), wxSize(100,25) );
283 (void)new wxTextCtrl(this, wxID_ANY
, "Text II",
284 wxPoint(200,100), wxSize(100,25) );
286 (void)new wxComboBox(this, wxID_ANY
, "ComboBox I",
287 wxPoint(0,150), wxSize(100,25));
288 (void)new wxComboBox(this, wxID_ANY
, "ComboBox II",
289 wxPoint(200,150), wxSize(100,25));
291 SetBackgroundColour("WHEAT");
293 Connect(wxEVT_PAINT
, wxPaintEventHandler(MySubCanvas::OnPaint
));
296 // override the base class function so that when this window is scrolled,
297 // the labels are scrolled in sync
298 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
)
300 wxPanel::ScrollWindow( dx
, dy
, rect
);
301 m_colLabels
->ScrollWindow( dx
, 0, rect
);
302 m_rowLabels
->ScrollWindow( 0, dy
, rect
);
306 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
308 wxPaintDC
dc( this );
309 m_owner
->PrepareDC( dc
);
311 dc
.SetPen( *wxBLACK_PEN
);
313 // OK, let's assume we are a grid control and we have two
314 // grid cells. Here in OnPaint we want to know which cell
315 // to redraw so that we prevent redrawing cells that don't
316 // need to get redrawn. We have one cell at (0,0) and one
317 // more at (200,0), both having a size of (100,25).
319 // We can query how much the window has been scrolled
320 // by calling CalcUnscrolledPosition()
324 m_owner
->CalcUnscrolledPosition( scroll_x
, scroll_y
, &scroll_x
, &scroll_y
);
326 // We also need to know the size of the window to see which
327 // cells are completely hidden and not get redrawn
331 GetClientSize( &size_x
, &size_y
);
333 // First cell: (0,0)(100,25)
335 if ((0+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
336 (0-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
338 // Has the region on screen been exposed?
339 if (IsExposed(0,0,100,25))
341 dc
.DrawRectangle( 0, 0, 100, 25 );
342 dc
.DrawText("First Cell", 5, 5);
347 // Second cell: (0,200)(100,25)
349 if ((200+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
350 (200-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
352 // Has the region on screen been exposed?
353 if (IsExposed(200,0,100,25))
355 dc
.DrawRectangle( 200, 0, 100, 25 );
356 dc
.DrawText("Second Cell", 205, 5);
361 wxScrolled
<wxWindow
> *m_owner
;
362 wxWindow
*m_colLabels
,
366 class MySubScrolledWindow
: public wxScrolled
<wxWindow
>
375 MySubScrolledWindow(wxWindow
*parent
)
376 : wxScrolled
<wxWindow
>(parent
, wxID_ANY
)
378 // create the children
379 MySubColLabels
*cols
= new MySubColLabels(this);
380 MySubRowLabels
*rows
= new MySubRowLabels(this);
382 m_canvas
= new MySubCanvas(this, cols
, rows
);
385 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2, 2, 10, 10);
386 sizer
->Add(CORNER_WIDTH
, CORNER_HEIGHT
); // just a spacer
387 sizer
->Add(cols
, wxSizerFlags().Expand());
388 sizer
->Add(rows
, wxSizerFlags().Expand());
389 sizer
->Add(m_canvas
, wxSizerFlags().Expand());
390 sizer
->AddGrowableRow(1);
391 sizer
->AddGrowableCol(1);
394 // this is the key call: it means that only m_canvas will be scrolled
395 // and not this window itself
396 SetTargetWindow(m_canvas
);
398 SetScrollbars(10, 10, 50, 50);
400 Connect(wxEVT_SIZE
, wxSizeEventHandler(MySubScrolledWindow::OnSize
));
404 // scrolled windows which use scroll target different from the window
405 // itself must override this virtual method
406 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
)
408 // decrease the total size by the size of the non-scrollable parts
409 // above/to the left of the canvas
410 wxSize
sizeCanvas(size
);
417 void OnSize(wxSizeEvent
& WXUNUSED(event
))
419 // We need to override OnSize so that our scrolled
420 // window a) does call Layout() to use sizers for
421 // positioning the controls but b) does not query
422 // the sizer for their size and use that for setting
423 // the scrollable area as set that ourselves by
424 // calling SetScrollbar() further down.
431 MySubCanvas
*m_canvas
;
434 class MySubFrame
: public wxFrame
437 MySubFrame(wxWindow
*parent
)
438 : wxFrame(parent
, wxID_ANY
, "MySubScrolledWindow")
440 new MySubScrolledWindow(this);
446 // ----------------------------------------------------------------------------
447 // more simple examples of wxScrolled usage
448 // ----------------------------------------------------------------------------
450 // base class for both of them
451 class MyScrolledWindowBase
: public wxScrolled
<wxWindow
>
454 MyScrolledWindowBase(wxWindow
*parent
)
455 : wxScrolled
<wxWindow
>(parent
, wxID_ANY
,
456 wxDefaultPosition
, wxDefaultSize
,
464 dc
.GetTextExtent("Line 17", NULL
, &m_hLine
);
467 // this scrolled window can be synchronized with another one: if this
468 // function is called with a non-NULL pointer, the given window will be
469 // scrolled to the same position as this one
470 void SyncWith(MyScrolledWindowBase
*win
)
477 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
)
479 wxScrolled
<wxWindow
>::ScrollWindow(dx
, dy
, rect
);
485 // the height of one line on screen
488 // the number of lines we draw
492 bool WasScrolledFirst() const { return m_inDoSync
; }
494 void DoSyncIfNecessary()
496 if ( m_winSync
&& !m_winSync
->WasScrolledFirst() )
500 m_winSync
->Scroll(GetViewStart());
506 // the window to synchronize with this one or NULL
507 MyScrolledWindowBase
*m_winSync
;
509 // the flag preventing infinite recursion which would otherwise happen if
510 // one window synchronized the other one which in turn synchronized this
515 // this class does "stupid" redrawing - it redraws everything each time
516 // and sets the scrollbar extent directly.
518 class MyScrolledWindowDumb
: public MyScrolledWindowBase
521 MyScrolledWindowDumb(wxWindow
*parent
) : MyScrolledWindowBase(parent
)
524 SetScrollbars(0, m_hLine
, 0, m_nLines
+ 1, 0, 0, true /* no refresh */);
527 virtual void OnDraw(wxDC
& dc
);
530 // this class does "smart" redrawing - only redraws the lines which must be
531 // redrawn and sets the scroll rate and virtual size to affect the
534 // Note that this class should produce identical results to the one above.
536 class MyScrolledWindowSmart
: public MyScrolledWindowBase
539 MyScrolledWindowSmart(wxWindow
*parent
) : MyScrolledWindowBase(parent
)
542 SetScrollRate( 0, m_hLine
);
543 SetVirtualSize( wxDefaultCoord
, ( m_nLines
+ 1 ) * m_hLine
);
546 virtual void OnDraw(wxDC
& dc
);
549 // ----------------------------------------------------------------------------
550 // implements a text viewer with simple block selection to test auto-scrolling
552 // ----------------------------------------------------------------------------
554 class MyAutoScrollingWindow
: public wxScrolled
<wxWindow
>
557 MyAutoScrollingWindow( wxWindow
* parent
);
558 wxRect
DeviceCoordsToGraphicalChars(wxRect updRect
) const;
559 wxPoint
DeviceCoordsToGraphicalChars(wxPoint pos
) const;
560 wxPoint
GraphicalCharToDeviceCoords(wxPoint pos
) const;
561 wxRect
LogicalCoordsToGraphicalChars(wxRect updRect
) const;
562 wxPoint
LogicalCoordsToGraphicalChars(wxPoint pos
) const;
563 wxPoint
GraphicalCharToLogicalCoords(wxPoint pos
) const;
565 bool IsSelected(int chX
, int chY
) const;
566 static bool IsInside(int k
, int bound1
, int bound2
);
567 static wxRect
DCNormalize(int x
, int y
, int w
, int h
);
571 void OnDraw(wxDC
& dc
);
572 void OnMouseLeftDown(wxMouseEvent
& event
);
573 void OnMouseLeftUp(wxMouseEvent
& event
);
574 void OnMouseMove(wxMouseEvent
& event
);
575 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
576 void OnScroll(wxScrollWinEvent
& event
);
578 // test data variables
579 static const char* sm_testData
;
580 static const int sm_lineCnt
; // line count
581 static const int sm_lineLen
; // line length in characters
582 // sizes for graphical data
583 int m_fontH
, m_fontW
;
584 // selection tracking
585 wxPoint m_selStart
; // beginning of blockwise selection
586 wxPoint m_cursor
; // end of blockwise selection (mouse position)
592 DECLARE_EVENT_TABLE()
595 class MyAutoFrame
: public wxFrame
598 MyAutoFrame(wxWindow
*parent
)
599 : wxFrame(parent
, wxID_ANY
, "MyAutoScrollingWindow")
601 new MyAutoScrollingWindow(this);
608 // ----------------------------------------------------------------------------
609 // MyFrame: the main application frame showing all the classes above
610 // ----------------------------------------------------------------------------
612 class MyFrame
: public wxFrame
618 void OnAbout(wxCommandEvent
& event
);
619 void OnQuit(wxCommandEvent
& event
);
621 void OnTestSimple(wxCommandEvent
& WXUNUSED(event
)) { new MySimpleFrame(this); }
622 void OnTestCanvas(wxCommandEvent
& WXUNUSED(event
)) { new MyCanvasFrame(this); }
623 void OnTestSizer(wxCommandEvent
& WXUNUSED(event
)) { new MySizerFrame(this); }
624 void OnTestSub(wxCommandEvent
& WXUNUSED(event
)) { new MySubFrame(this); }
625 void OnTestAuto(wxCommandEvent
& WXUNUSED(event
)) { new MyAutoFrame(this); }
627 void OnToggleSync(wxCommandEvent
& event
);
628 void OnScrollbarVisibility(wxCommandEvent
& event
);
630 MyScrolledWindowBase
*m_win1
,
633 DECLARE_EVENT_TABLE()
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
640 class MyApp
: public wxApp
643 virtual bool OnInit();
647 // ============================================================================
649 // ============================================================================
651 // ----------------------------------------------------------------------------
653 // ----------------------------------------------------------------------------
655 const wxWindowIDRef ID_ADDBUTTON
= wxWindow::NewControlId();
656 const wxWindowIDRef ID_DELBUTTON
= wxWindow::NewControlId();
657 const wxWindowIDRef ID_MOVEBUTTON
= wxWindow::NewControlId();
658 const wxWindowIDRef ID_SCROLLWIN
= wxWindow::NewControlId();
659 const wxWindowIDRef ID_QUERYPOS
= wxWindow::NewControlId();
661 const wxWindowIDRef ID_NEWBUTTON
= wxWindow::NewControlId();
663 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolled
<wxPanel
>)
664 EVT_PAINT( MyCanvas::OnPaint
)
665 EVT_RIGHT_DOWN( MyCanvas::OnMouseRightDown
)
666 EVT_MOUSEWHEEL( MyCanvas::OnMouseWheel
)
667 EVT_BUTTON( ID_QUERYPOS
, MyCanvas::OnQueryPosition
)
668 EVT_BUTTON( ID_ADDBUTTON
, MyCanvas::OnAddButton
)
669 EVT_BUTTON( ID_DELBUTTON
, MyCanvas::OnDeleteButton
)
670 EVT_BUTTON( ID_MOVEBUTTON
, MyCanvas::OnMoveButton
)
671 EVT_BUTTON( ID_SCROLLWIN
, MyCanvas::OnScrollWin
)
674 MyCanvas::MyCanvas(wxWindow
*parent
)
675 : wxScrolled
<wxPanel
>(parent
, wxID_ANY
,
676 wxDefaultPosition
, wxDefaultSize
,
677 wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
)
679 // you can use either a single SetScrollbars() call or these 2 functions,
680 // usually using them is better because you normally won't need to change
681 // the scroll rate in the future and the sizer can be used to update the
682 // virtual size automatically
683 SetScrollRate( 10, 10 );
684 SetVirtualSize( 500, 1000 );
686 (void) new wxButton( this, ID_ADDBUTTON
, "add button", wxPoint(10,10) );
687 (void) new wxButton( this, ID_DELBUTTON
, "del button", wxPoint(10,40) );
688 (void) new wxButton( this, ID_MOVEBUTTON
, "move button", wxPoint(150,10) );
689 (void) new wxButton( this, ID_SCROLLWIN
, "scroll win", wxPoint(250,10) );
691 wxPanel
*test
= new wxPanel( this, wxID_ANY
,
692 wxPoint(10, 110), wxSize(130,50),
693 wxSIMPLE_BORDER
| wxTAB_TRAVERSAL
);
694 test
->SetBackgroundColour( "WHEAT" );
696 SetBackgroundColour( "BLUE" );
699 void MyCanvas::OnMouseRightDown( wxMouseEvent
&event
)
701 wxPoint
pt( event
.GetPosition() );
703 CalcUnscrolledPosition( pt
.x
, pt
.y
, &x
, &y
);
704 wxLogMessage("Mouse down event at: %d %d, scrolled: %d %d",
708 void MyCanvas::OnMouseWheel( wxMouseEvent
&event
)
710 wxPoint
pt( event
.GetPosition() );
712 CalcUnscrolledPosition( pt
.x
, pt
.y
, &x
, &y
);
713 wxLogMessage( "Mouse wheel event at: %d %d, scrolled: %d %d\n"
714 "Rotation: %d, delta = %d",
716 event
.GetWheelRotation(), event
.GetWheelDelta() );
721 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
723 wxPaintDC
dc( this );
726 dc
.DrawText( "Press right mouse button to test calculations!", 160, 50 );
728 dc
.DrawText( "Some text", 140, 140 );
730 dc
.DrawRectangle( 100, 160, 200, 200 );
733 void MyCanvas::OnQueryPosition( wxCommandEvent
&WXUNUSED(event
) )
735 wxPoint
pt( m_button
->GetPosition() );
736 wxLogMessage( "Position of \"Query position\" is %d %d", pt
.x
, pt
.y
);
737 pt
= ClientToScreen( pt
);
738 wxLogMessage("Position of \"Query position\" on screen is %d %d",
742 void MyCanvas::OnAddButton( wxCommandEvent
&WXUNUSED(event
) )
744 wxLogMessage( "Inserting button at position 10,70..." );
745 wxButton
*button
= new wxButton( this, ID_NEWBUTTON
, "new button",
746 wxPoint(10,70), wxSize(80,25) );
747 wxPoint
pt( button
->GetPosition() );
748 wxLogMessage( "-> Position after inserting %d %d", pt
.x
, pt
.y
);
751 void MyCanvas::OnDeleteButton( wxCommandEvent
&WXUNUSED(event
) )
753 wxLogMessage( "Deleting button inserted with \"Add button\"..." );
754 wxWindow
*win
= FindWindow( ID_NEWBUTTON
);
758 wxLogMessage( "-> No window with id = ID_NEWBUTTON found." );
761 void MyCanvas::OnMoveButton( wxCommandEvent
&event
)
763 wxLogMessage( "Moving button 10 pixels downward.." );
764 wxWindow
*win
= FindWindow( event
.GetId() );
765 wxPoint
pt( win
->GetPosition() );
766 wxLogMessage( "-> Position before move is %d %d", pt
.x
, pt
.y
);
767 win
->Move( wxDefaultCoord
, pt
.y
+ 10 );
768 pt
= win
->GetPosition();
769 wxLogMessage( "-> Position after move is %d %d", pt
.x
, pt
.y
);
772 void MyCanvas::OnScrollWin( wxCommandEvent
&WXUNUSED(event
) )
774 wxLogMessage("Scrolling 2 units up.\n"
775 "The white square and the controls should move equally!");
776 Scroll( wxDefaultCoord
, GetViewStart().y
+2 );
779 // ----------------------------------------------------------------------------
780 // MySizerScrolledWindow
781 // ----------------------------------------------------------------------------
783 MySizerScrolledWindow::MySizerScrolledWindow(wxWindow
*parent
)
784 : wxScrolled
<wxWindow
>(parent
)
786 SetBackgroundColour( "GREEN" );
788 // Set the rate we'd like for scrolling.
790 SetScrollRate( 5, 5 );
792 // Populate a sizer with a 'resizing' button and some other static
795 wxFlexGridSizer
*sizer
= new wxFlexGridSizer(2);
797 m_button
= new wxButton( this, wxID_RESIZE_FRAME
, "Press me",
798 wxDefaultPosition
, SMALL_BUTTON
);
800 sizer
->Add(m_button
, wxSizerFlags().Centre().Border(wxALL
, 20));
801 sizer
->Add(new wxStaticText(this, wxID_ANY
, "This is just"),
802 wxSizerFlags().Centre());
803 sizer
->Add(new wxStaticText(this, wxID_ANY
, "some decoration"),
804 wxSizerFlags().Centre());
805 sizer
->Add(new wxStaticText(this, wxID_ANY
, "for you to scroll..."),
806 wxSizerFlags().Centre());
808 // Then use the sizer to set the scrolled region size.
812 Connect(wxID_RESIZE_FRAME
, wxEVT_COMMAND_BUTTON_CLICKED
,
813 wxCommandEventHandler(MySizerScrolledWindow::OnResizeClick
));
816 void MySizerScrolledWindow::OnResizeClick(wxCommandEvent
&WXUNUSED(event
))
818 // Arbitrarily resize the button to change the minimum size of
819 // the (scrolled) sizer.
821 if ( m_button
->GetSize() == SMALL_BUTTON
)
822 m_button
->SetSizeHints(LARGE_BUTTON
);
824 m_button
->SetSizeHints(SMALL_BUTTON
);
826 // Force update layout and scrollbars, since nothing we do here
827 // necessarily generates a size event which would do it for us.
831 // ----------------------------------------------------------------------------
833 // ----------------------------------------------------------------------------
835 const wxWindowID Scroll_Test_Simple
= wxWindow::NewControlId();
836 const wxWindowID Scroll_Test_Canvas
= wxWindow::NewControlId();
837 const wxWindowID Scroll_Test_Sizers
= wxWindow::NewControlId();
838 const wxWindowID Scroll_Test_Sub
= wxWindow::NewControlId();
839 const wxWindowID Scroll_Test_Auto
= wxWindow::NewControlId();
841 const wxWindowID Scroll_TglBtn_Sync
= wxWindow::NewControlId();
842 const wxWindowID Scroll_Radio_ShowScrollbar
= wxWindow::NewControlId();
844 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
845 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
846 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
848 EVT_MENU(Scroll_Test_Simple
, MyFrame::OnTestSimple
)
849 EVT_MENU(Scroll_Test_Canvas
, MyFrame::OnTestCanvas
)
850 EVT_MENU(Scroll_Test_Sizers
, MyFrame::OnTestSizer
)
851 EVT_MENU(Scroll_Test_Sub
, MyFrame::OnTestSub
)
852 EVT_MENU(Scroll_Test_Auto
, MyFrame::OnTestAuto
)
854 EVT_TOGGLEBUTTON(Scroll_TglBtn_Sync
, MyFrame::OnToggleSync
)
855 EVT_RADIOBOX(Scroll_Radio_ShowScrollbar
, MyFrame::OnScrollbarVisibility
)
859 : wxFrame(NULL
, wxID_ANY
, "wxWidgets scroll sample")
861 SetIcon(wxICON(sample
));
863 wxMenu
*menuFile
= new wxMenu
;
864 menuFile
->Append(wxID_ABOUT
, "&About..");
865 menuFile
->AppendSeparator();
866 menuFile
->Append(wxID_EXIT
, "E&xit\tAlt-X");
868 wxMenu
*menuTest
= new wxMenu
;
869 menuTest
->Append(Scroll_Test_Simple
, "&Simple scroll window\tF1",
870 "Simplest possible scrolled window test.");
871 menuTest
->Append(Scroll_Test_Canvas
, "Scrolled window with &children\tF2",
872 "Scrolled window with controls on it.");
873 menuTest
->Append(Scroll_Test_Sizers
, "Scrolled window with si&zer\tF3",
874 "Scrolled window with children managed by sizer.");
875 menuTest
->Append(Scroll_Test_Sub
, "Scrolled s&ub-window\tF4",
876 "Window only part of which is scrolled.");
877 menuTest
->Append(Scroll_Test_Auto
, "&Auto-scrolled window\tF5",
878 "Window which scrolls when the mouse is held pressed "
881 wxMenuBar
*mbar
= new wxMenuBar
;
882 mbar
->Append(menuFile
, "&File");
883 mbar
->Append(menuTest
, "&Test");
888 wxPanel
*panel
= new wxPanel(this);
890 const wxSizerFlags
flagsExpand(wxSizerFlags(1).Expand());
892 wxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
893 topsizer
->Add(new wxStaticText(panel
, wxID_ANY
,
894 "The windows below should behave in the same way, even though\n"
895 "they're implemented quite differently, see the code for details.\n"
897 "The lines redrawn during odd/even repaint iterations are drawn in\n"
898 "red/blue colour to allow seeing immediately how much is repainted,\n"
899 "don't be surprised by this."),
900 wxSizerFlags().Centre().Border());
902 m_win1
= new MyScrolledWindowDumb(panel
);
903 m_win2
= new MyScrolledWindowSmart(panel
);
905 wxSizer
*sizerScrollWin
= new wxBoxSizer(wxHORIZONTAL
);
906 sizerScrollWin
->Add(m_win1
, flagsExpand
);
907 sizerScrollWin
->Add(m_win2
, flagsExpand
);
908 topsizer
->Add(sizerScrollWin
, flagsExpand
);
911 flagsHBorder(wxSizerFlags().Centre().Border(wxLEFT
| wxRIGHT
));
913 wxSizer
*sizerBtns
= new wxBoxSizer(wxHORIZONTAL
);
915 // the radio buttons are in the same order as wxSHOW_SB_XXX values but
917 const wxString visibilities
[] = { "&never", "&default", "&always" };
918 wxRadioBox
*radio
= new wxRadioBox(panel
, Scroll_Radio_ShowScrollbar
,
919 "Left &scrollbar visibility: ",
920 wxDefaultPosition
, wxDefaultSize
,
921 WXSIZEOF(visibilities
), visibilities
);
922 radio
->SetSelection(wxSHOW_SB_DEFAULT
+ 1);
923 sizerBtns
->Add(radio
, flagsHBorder
);
925 sizerBtns
->Add(new wxToggleButton(panel
, Scroll_TglBtn_Sync
, "S&ynchronize"),
928 topsizer
->Add(sizerBtns
, wxSizerFlags().Centre().Border());
930 panel
->SetSizer(topsizer
);
932 wxSize size
= panel
->GetBestSize();
934 SetClientSize(2*size
);
939 void MyFrame::OnToggleSync(wxCommandEvent
& event
)
941 if ( event
.IsChecked() )
943 m_win1
->SyncWith(m_win2
);
944 m_win2
->SyncWith(m_win1
);
948 m_win1
->SyncWith(NULL
);
949 m_win2
->SyncWith(NULL
);
953 void MyFrame::OnScrollbarVisibility(wxCommandEvent
& event
)
955 m_win1
->ShowScrollbars(wxSHOW_SB_NEVER
,
956 wxScrollbarVisibility(event
.GetSelection() - 1));
959 void MyFrame::OnQuit(wxCommandEvent
&WXUNUSED(event
))
964 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
966 (void)wxMessageBox( "Scrolled window sample\n"
968 "Robert Roebling (c) 1998\n"
969 "Vadim Zeitlin (c) 2008\n"
970 "Autoscrolling examples\n"
972 "Auto-timed-scrolling example\n"
973 "Matt Gregory (c) 2003\n",
974 "About wxWidgets scroll sample",
975 wxICON_INFORMATION
| wxOK
);
978 // ----------------------------------------------------------------------------
980 // ----------------------------------------------------------------------------
986 if ( !wxApp::OnInit() )
994 // ----------------------------------------------------------------------------
995 // MyScrolledWindowXXX
996 // ----------------------------------------------------------------------------
998 void MyScrolledWindowDumb::OnDraw(wxDC
& dc
)
1000 // this is useful to see which lines are redrawn
1001 static size_t s_redrawCount
= 0;
1002 dc
.SetTextForeground(s_redrawCount
++ % 2 ? *wxRED
: *wxBLUE
);
1005 for ( size_t line
= 0; line
< m_nLines
; line
++ )
1008 CalcScrolledPosition(0, y
, NULL
, &yPhys
);
1010 dc
.DrawText(wxString::Format("Line %u (logical %d, physical %d)",
1011 unsigned(line
), y
, yPhys
), 0, y
);
1016 void MyScrolledWindowSmart::OnDraw(wxDC
& dc
)
1018 // this is useful to see which lines are redrawn
1019 static size_t s_redrawCount
= 0;
1020 dc
.SetTextForeground(s_redrawCount
++ % 2 ? *wxRED
: *wxBLUE
);
1022 // update region is always in device coords, translate to logical ones
1023 wxRect rectUpdate
= GetUpdateRegion().GetBox();
1024 CalcUnscrolledPosition(rectUpdate
.x
, rectUpdate
.y
,
1025 &rectUpdate
.x
, &rectUpdate
.y
);
1027 size_t lineFrom
= rectUpdate
.y
/ m_hLine
,
1028 lineTo
= rectUpdate
.GetBottom() / m_hLine
;
1030 if ( lineTo
> m_nLines
- 1)
1031 lineTo
= m_nLines
- 1;
1033 int y
= lineFrom
*m_hLine
;
1034 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
1037 CalcScrolledPosition(0, y
, NULL
, &yPhys
);
1039 dc
.DrawText(wxString::Format("Line %u (logical %d, physical %d)",
1040 unsigned(line
), y
, yPhys
), 0, y
);
1045 // ----------------------------------------------------------------------------
1046 // MyAutoScrollingWindow
1047 // ----------------------------------------------------------------------------
1049 BEGIN_EVENT_TABLE(MyAutoScrollingWindow
, wxScrolled
<wxWindow
>)
1050 EVT_LEFT_DOWN(MyAutoScrollingWindow::OnMouseLeftDown
)
1051 EVT_LEFT_UP(MyAutoScrollingWindow::OnMouseLeftUp
)
1052 EVT_MOTION(MyAutoScrollingWindow::OnMouseMove
)
1053 EVT_MOUSE_CAPTURE_LOST(MyAutoScrollingWindow::OnMouseCaptureLost
)
1054 EVT_SCROLLWIN(MyAutoScrollingWindow::OnScroll
)
1057 MyAutoScrollingWindow::MyAutoScrollingWindow(wxWindow
* parent
)
1058 : wxScrolled
<wxWindow
>(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
1059 wxVSCROLL
| wxHSCROLL
| wxSUNKEN_BORDER
),
1062 m_font(9, wxFONTFAMILY_TELETYPE
, wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
)
1064 wxClientDC
dc(this);
1065 // query dc for text size
1067 dc
.GetTextExtent(wxString("A"), &m_fontW
, &m_fontH
);
1068 // set up the virtual window
1069 SetScrollbars(m_fontW
, m_fontH
, sm_lineLen
, sm_lineCnt
);
1073 MyAutoScrollingWindow::DeviceCoordsToGraphicalChars(wxRect updRect
) const
1075 wxPoint
pos(updRect
.GetPosition());
1076 pos
= DeviceCoordsToGraphicalChars(pos
);
1079 updRect
.width
/= m_fontW
;
1080 updRect
.height
/= m_fontH
;
1081 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
1082 // so an off-by-one correction is needed
1083 ++updRect
.width
; // kludge
1084 ++updRect
.height
; // kludge
1089 MyAutoScrollingWindow::DeviceCoordsToGraphicalChars(wxPoint pos
) const
1093 pos
+= GetViewStart();
1098 MyAutoScrollingWindow::GraphicalCharToDeviceCoords(wxPoint pos
) const
1100 pos
-= GetViewStart();
1107 MyAutoScrollingWindow::LogicalCoordsToGraphicalChars(wxRect updRect
) const
1109 wxPoint
pos(updRect
.GetPosition());
1110 pos
= LogicalCoordsToGraphicalChars(pos
);
1113 updRect
.width
/= m_fontW
;
1114 updRect
.height
/= m_fontH
;
1115 // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
1116 // so an off-by-one correction is needed
1117 ++updRect
.width
; // kludge
1118 ++updRect
.height
; // kludge
1123 MyAutoScrollingWindow::LogicalCoordsToGraphicalChars(wxPoint pos
) const
1131 MyAutoScrollingWindow::GraphicalCharToLogicalCoords(wxPoint pos
) const
1138 void MyAutoScrollingWindow::MyRefresh()
1140 static wxPoint
lastSelStart(-1, -1), lastCursor(-1, -1);
1141 // refresh last selected area (to deselect previously selected text)
1143 GraphicalCharToDeviceCoords(lastSelStart
),
1144 GraphicalCharToDeviceCoords(lastCursor
)
1146 // off-by-one corrections, necessary because it's not possible to know
1147 // when to round up until rect is normalized by lastUpdRect constructor
1148 lastUpdRect
.width
+= m_fontW
; // kludge
1149 lastUpdRect
.height
+= m_fontH
; // kludge
1150 // refresh currently selected (to select previously unselected text)
1152 GraphicalCharToDeviceCoords(m_selStart
),
1153 GraphicalCharToDeviceCoords(m_cursor
)
1155 // off-by-one corrections
1156 updRect
.width
+= m_fontW
; // kludge
1157 updRect
.height
+= m_fontH
; // kludge
1158 // find necessary refresh areas
1159 int rx
= lastUpdRect
.x
;
1160 int ry
= lastUpdRect
.y
;
1161 int rw
= updRect
.x
- lastUpdRect
.x
;
1162 int rh
= lastUpdRect
.height
;
1164 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
1167 ry
= updRect
.y
+ updRect
.height
;
1169 rh
= (lastUpdRect
.y
+ lastUpdRect
.height
) - (updRect
.y
+ updRect
.height
);
1171 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
1173 rx
= updRect
.x
+ updRect
.width
;
1175 rw
= (lastUpdRect
.x
+ lastUpdRect
.width
) - (updRect
.x
+ updRect
.width
);
1176 rh
= lastUpdRect
.height
;
1178 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
1183 rh
= updRect
.y
- lastUpdRect
.y
;
1185 RefreshRect(DCNormalize(rx
, ry
, rw
, rh
));
1188 lastSelStart
= m_selStart
;
1189 lastCursor
= m_cursor
;
1192 bool MyAutoScrollingWindow::IsSelected(int chX
, int chY
) const
1194 if (IsInside(chX
, m_selStart
.x
, m_cursor
.x
)
1195 && IsInside(chY
, m_selStart
.y
, m_cursor
.y
)) {
1201 bool MyAutoScrollingWindow::IsInside(int k
, int bound1
, int bound2
)
1203 if ((k
>= bound1
&& k
<= bound2
) || (k
>= bound2
&& k
<= bound1
)) {
1210 MyAutoScrollingWindow::DCNormalize(int x
, int y
, int w
, int h
)
1212 // this is needed to get rid of the graphical remnants from the selection
1213 // I think it's because DrawRectangle() excludes a pixel in either direction
1214 const int kludge
= 1;
1215 // make (x, y) the top-left corner
1230 return wxRect(x
, y
, w
, h
);
1233 void MyAutoScrollingWindow::OnDraw(wxDC
& dc
)
1236 wxBrush
normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
1237 wxBrush
selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
));
1238 dc
.SetPen(*wxTRANSPARENT_PEN
);
1239 const wxString str
= sm_testData
;
1240 size_t strLength
= str
.length();
1241 wxString::const_iterator str_i
= str
.begin();
1243 // draw the characters
1244 // 1. for each update region
1245 for (wxRegionIterator
upd(GetUpdateRegion()); upd
; ++upd
) {
1246 wxRect updRect
= upd
.GetRect();
1247 wxRect
updRectInGChars(DeviceCoordsToGraphicalChars(updRect
));
1248 // 2. for each row of chars in the update region
1249 for (int chY
= updRectInGChars
.y
1250 ; chY
<= updRectInGChars
.y
+ updRectInGChars
.height
; ++chY
) {
1251 // 3. for each character in the row
1252 bool isFirstX
= true;
1253 for (int chX
= updRectInGChars
.x
1254 ; chX
<= updRectInGChars
.x
+ updRectInGChars
.width
1257 if (IsSelected(chX
, chY
)) {
1258 dc
.SetBrush(selBrush
);
1259 dc
.SetTextForeground( wxSystemSettings::GetColour
1260 (wxSYS_COLOUR_HIGHLIGHTTEXT
));
1262 dc
.SetBrush(normBrush
);
1263 dc
.SetTextForeground( wxSystemSettings::GetColour
1264 (wxSYS_COLOUR_WINDOWTEXT
));
1266 // 5. find position info
1267 wxPoint charPos
= GraphicalCharToLogicalCoords(wxPoint
1270 dc
.DrawRectangle(charPos
.x
, charPos
.y
, m_fontW
, m_fontH
);
1271 size_t charIndex
= chY
* sm_lineLen
+ chX
;
1272 if (chY
< sm_lineCnt
&&
1274 charIndex
< strLength
)
1278 str_i
= str
.begin() + charIndex
;
1281 dc
.DrawText(*str_i
, charPos
.x
, charPos
.y
);
1289 void MyAutoScrollingWindow::OnMouseLeftDown(wxMouseEvent
& event
)
1291 // initial press of mouse button sets the beginning of the selection
1292 m_selStart
= DeviceCoordsToGraphicalChars(event
.GetPosition());
1293 // set the cursor to the same position
1294 m_cursor
= m_selStart
;
1295 // draw/erase selection
1299 void MyAutoScrollingWindow::OnMouseLeftUp(wxMouseEvent
& WXUNUSED(event
))
1301 // this test is necessary
1308 void MyAutoScrollingWindow::OnMouseMove(wxMouseEvent
& event
)
1310 // if user is dragging
1311 if (event
.Dragging() && event
.LeftIsDown()) {
1312 // set the new cursor position
1313 m_cursor
= DeviceCoordsToGraphicalChars(event
.GetPosition());
1314 // draw/erase selection
1316 // capture mouse to activate auto-scrolling
1317 if (!HasCapture()) {
1324 MyAutoScrollingWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent
&
1327 // we only capture mouse for timed scrolling, so nothing is needed here
1328 // other than making sure to not call event.Skip()
1331 void MyAutoScrollingWindow::OnScroll(wxScrollWinEvent
& event
)
1333 // need to move the cursor when autoscrolling
1334 // FIXME: the cursor also moves when the scrollbar arrows are clicked
1336 if (event
.GetOrientation() == wxHORIZONTAL
) {
1337 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
) {
1339 } else if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
) {
1342 } else if (event
.GetOrientation() == wxVERTICAL
) {
1343 if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEUP
) {
1345 } else if (event
.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN
) {
1354 const int MyAutoScrollingWindow::sm_lineCnt
= 125;
1355 const int MyAutoScrollingWindow::sm_lineLen
= 79;
1356 const char *MyAutoScrollingWindow::sm_testData
=
1357 "162 Cult of the genius out of vanity. Because we think well of ourselves, but "
1358 "nonetheless never suppose ourselves capable of producing a painting like one of "
1359 "Raphael's or a dramatic scene like one of Shakespeare's, we convince ourselves "
1360 "that the capacity to do so is quite extraordinarily marvelous, a wholly "
1361 "uncommon accident, or, if we are still religiously inclined, a mercy from on "
1362 "high. Thus our vanity, our self-love, promotes the cult of the genius: for only "
1363 "if we think of him as being very remote from us, as a miraculum, does he not "
1364 "aggrieve us (even Goethe, who was without envy, called Shakespeare his star of "
1365 "the most distant heights [\"William! Stern der schonsten Ferne\": from Goethe's, "
1366 "\"Between Two Worlds\"]; in regard to which one might recall the lines: \"the "
1367 "stars, these we do not desire\" [from Goethe's, \"Comfort in Tears\"]). But, aside "
1368 "from these suggestions of our vanity, the activity of the genius seems in no "
1369 "way fundamentally different from the activity of the inventor of machines, the "
1370 "scholar of astronomy or history, the master of tactics. All these activities "
1371 "are explicable if one pictures to oneself people whose thinking is active in "
1372 "one direction, who employ everything as material, who always zealously observe "
1373 "their own inner life and that of others, who perceive everywhere models and "
1374 "incentives, who never tire of combining together the means available to them. "
1375 "Genius too does nothing except learn first how to lay bricks then how to build, "
1376 "except continually seek for material and continually form itself around it. "
1377 "Every activity of man is amazingly complicated, not only that of the genius: "
1378 "but none is a \"miracle.\" Whence, then, the belief that genius exists only in "
1379 "the artist, orator and philosopher? that only they have \"intuition\"? (Whereby "
1380 "they are supposed to possess a kind of miraculous eyeglass with which they can "
1381 "see directly into \"the essence of the thing\"!) It is clear that people speak of "
1382 "genius only where the effects of the great intellect are most pleasant to them "
1383 "and where they have no desire to feel envious. To call someone \"divine\" means: "
1384 "\"here there is no need for us to compete.\" Then, everything finished and "
1385 "complete is regarded with admiration, everything still becoming is undervalued. "
1386 "But no one can see in the work of the artist how it has become; that is its "
1387 "advantage, for wherever one can see the act of becoming one grows somewhat "
1388 "cool. The finished and perfect art of representation repulses all thinking as "
1389 "to how it has become; it tyrannizes as present completeness and perfection. "
1390 "That is why the masters of the art of representation count above all as gifted "
1391 "with genius and why men of science do not. In reality, this evaluation of the "
1392 "former and undervaluation of the latter is only a piece of childishness in the "
1395 "163 The serious workman. Do not talk about giftedness, inborn talents! One can "
1396 "name great men of all kinds who were very little gifted. The acquired "
1397 "greatness, became \"geniuses\" (as we put it), through qualities the lack of "
1398 "which no one who knew what they were would boast of: they all possessed that "
1399 "seriousness of the efficient workman which first learns to construct the parts "
1400 "properly before it ventures to fashion a great whole; they allowed themselves "
1401 "time for it, because they took more pleasure in making the little, secondary "
1402 "things well than in the effect of a dazzling whole. the recipe for becoming a "
1403 "good novelist, for example, is easy to give, but to carry it out presupposes "
1404 "qualities one is accustomed to overlook when one says \"I do not have enough "
1405 "talent.\" One has only to make a hundred or so sketches for novels, none longer "
1406 "than two pages but of such distinctness that every word in them is necessary; "
1407 "one should write down anecdotes each day until one has learned how to give them "
1408 "the most pregnant and effective form; one should be tireless in collecting and "
1409 "describing human types and characters; one should above all relate things to "
1410 "others and listen to others relate, keeping one's eyes and ears open for the "
1411 "effect produced on those present, one should travel like a landscape painter or "
1412 "costume designer; one should excerpt for oneself out of the individual sciences "
1413 "everything that will produce an artistic effect when it is well described, one "
1414 "should, finally, reflect on the motives of human actions, disdain no signpost "
1415 "to instruction about them and be a collector of these things by day and night. "
1416 "One should continue in this many-sided exercise some ten years: what is then "
1417 "created in the workshop, however, will be fit to go out into the world. What, "
1418 "however, do most people do? They begin, not with the parts, but with the whole. "
1419 "Perhaps they chance to strike a right note, excite attention and from then on "
1420 "strike worse and worse notes, for good, natural reasons. Sometimes, when the "
1421 "character and intellect needed to formulate such a life-plan are lacking, fate "
1422 "and need take their place and lead the future master step by step through all "
1423 "the stipulations of his trade. "
1425 "164 Peril and profit in the cult of the genius. The belief in great, superior, "
1426 "fruitful spirits is not necessarily, yet nonetheless is very frequently "
1427 "associated with that religious or semi-religious superstition that these "
1428 "spirits are of supra-human origin and possess certain miraculous abilities by "
1429 "virtue of which they acquire their knowledge by quite other means than the rest "
1430 "of mankind. One ascribes to them, it seems, a direct view of the nature of the "
1431 "world, as it were a hole in the cloak of appearance, and believes that, by "
1432 "virtue of this miraculous seer's vision, they are able to communicate something "
1433 "conclusive and decisive about man and the world without the toil and "
1434 "rigorousness required by science. As long as there continue to be those who "
1435 "believe in the miraculous in the domain of knowledge one can perhaps concede "
1436 "that these people themselves derive some benefit from their belief, inasmuch as "
1437 "through their unconditional subjection to the great spirits they create for "
1438 "their own spirit during its time of development the finest form of discipline "
1439 "and schooling. On the other hand, it is at least questionable whether the "
1440 "superstitious belief in genius, in its privileges and special abilities, is of "
1441 "benefit to the genius himself if it takes root in him. It is in any event a "
1442 "dangerous sign when a man is assailed by awe of himself, whether it be the "
1443 "celebrated Caesar's awe of Caesar or the awe of one's own genius now under "
1444 "consideration; when the sacrificial incense which is properly rendered only to "
1445 "a god penetrates the brain of the genius, so that his head begins to swim and "
1446 "he comes to regard himself as something supra-human. The consequences that "
1447 "slowly result are: the feeling of irresponsibility, of exceptional rights, the "
1448 "belief that he confers a favor by his mere presence, insane rage when anyone "
1449 "attempts even to compare him with others, let alone to rate him beneath them, "
1450 "or to draw attention to lapses in his work. Because he ceases to practice "
1451 "criticism of himself, at last one pinion after the other falls out of his "
1452 "plumage: that superstitious eats at the roots of his powers and perhaps even "
1453 "turns him into a hypocrite after his powers have fled from him. For the great "
1454 "spirits themselves it is therefore probably more beneficial if they acquire an "
1455 "insight into the nature and origin of their powers, if they grasp, that is to "
1456 "say, what purely human qualities have come together in them and what fortunate "
1457 "circumstances attended them: in the first place undiminished energy, resolute "
1458 "application to individual goals, great personal courage, then the good fortune "
1459 "to receive an upbringing which offered in the early years the finest teachers, "
1460 "models and methods. To be sure, when their goal is the production of the "
1461 "greatest possible effect, unclarity with regard to oneself and that "
1462 "semi-insanity superadded to it has always achieved much; for what has been "
1463 "admired and envied at all times has been that power in them by virtue of which "
1464 "they render men will-less and sweep them away into the delusion that the "
1465 "leaders they are following are supra-natural. Indeed, it elevates and inspires "
1466 "men to believe that someone is in possession of supra-natural powers: to this "
1467 "extent Plato was right to say [Plato: Phaedrus, 244a] that madness has brought "
1468 "the greatest of blessings upon mankind. In rare individual cases this portion "
1469 "of madness may, indeed, actually have been the means by which such a nature, "
1470 "excessive in all directions, was held firmly together: in the life of "
1471 "individuals, too, illusions that are in themselves poisons often play the role "
1472 "of healers; yet, in the end, in the case of every \"genius\" who believes in his "
1473 "own divinity the poison shows itself to the same degree as his \"genius\" grows "
1474 "old: one may recall, for example, the case of Napoleon, whose nature certainly "
1475 "grew into the mighty unity that sets him apart from all men of modern times "
1476 "precisely through his belief in himself and his star and through the contempt "
1477 "for men that flowed from it; until in the end, however, this same belief went "
1478 "over into an almost insane fatalism, robbed him of his acuteness and swiftness "
1479 "of perception, and became the cause of his destruction.";