]>
Commit | Line | Data |
---|---|---|
4e04f777 WS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: scroll.cpp | |
3 | // Purpose: wxScrolledWindow sample | |
4 | // Author: Robert Roebling | |
4e04f777 WS |
5 | // RCS-ID: $Id$ |
6 | // Copyright: (C) 1998 Robert Roebling, 2002 Ron Lee, 2003 Matt Gregory | |
6a5a7fba | 7 | // (C) 2008 Vadim Zeitlin |
4e04f777 WS |
8 | // Licence: wxWindows license |
9 | ///////////////////////////////////////////////////////////////////////////// | |
fdd3ed7a | 10 | |
fdd3ed7a RR |
11 | #include "wx/wxprec.h" |
12 | ||
13 | #ifdef __BORLANDC__ | |
6a5a7fba | 14 | #pragma hdrstop |
fdd3ed7a RR |
15 | #endif |
16 | ||
17 | #ifndef WX_PRECOMP | |
6a5a7fba | 18 | #include "wx/wx.h" |
fdd3ed7a RR |
19 | #endif |
20 | ||
ed673c6a RR |
21 | #include "wx/sizer.h" |
22 | #include "wx/log.h" | |
23 | ||
6a5a7fba | 24 | // ---------------------------------------------------------------------------- |
db4ad642 | 25 | // a trivial example |
6a5a7fba | 26 | // ---------------------------------------------------------------------------- |
db4ad642 | 27 | |
d9f4cc10 | 28 | // MySimpleCanvas: a scrolled window which draws a simple rectangle |
6a5a7fba | 29 | class MySimpleCanvas : public wxScrolledWindow |
db4ad642 RR |
30 | { |
31 | public: | |
d9f4cc10 VZ |
32 | enum |
33 | { | |
6a5a7fba VZ |
34 | // these numbers are not multiple of 10 (our scroll step) to test for |
35 | // the absence of rounding errors (e.g. we should have one more page | |
36 | // than WIDTH/10 to show the right side of the rectangle) | |
37 | WIDTH = 292, | |
38 | HEIGHT = 297 | |
d9f4cc10 VZ |
39 | }; |
40 | ||
6a5a7fba VZ |
41 | MySimpleCanvas(wxWindow *parent) |
42 | : wxScrolledWindow(parent, wxID_ANY) | |
43 | { | |
44 | SetScrollRate( 10, 10 ); | |
45 | SetVirtualSize( WIDTH, HEIGHT ); | |
46 | SetBackgroundColour( *wxWHITE ); | |
db4ad642 | 47 | |
6a5a7fba VZ |
48 | Connect(wxEVT_PAINT, wxPaintEventHandler(MySimpleCanvas::OnPaint)); |
49 | } | |
db4ad642 | 50 | |
6a5a7fba VZ |
51 | private: |
52 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
53 | { | |
54 | wxPaintDC dc(this); | |
db4ad642 | 55 | |
6a5a7fba VZ |
56 | // this call is vital: it adjusts the dc to account for the current |
57 | // scroll offset | |
58 | PrepareDC(dc); | |
db4ad642 | 59 | |
6a5a7fba VZ |
60 | dc.SetPen( *wxRED_PEN ); |
61 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); | |
62 | dc.DrawRectangle( 0, 0, WIDTH, HEIGHT ); | |
63 | } | |
64 | }; | |
c5a3900a | 65 | |
db4ad642 | 66 | |
d9f4cc10 | 67 | // MySimpleFrame: a frame which contains a MySimpleCanvas |
6a5a7fba | 68 | class MySimpleFrame : public wxFrame |
db4ad642 RR |
69 | { |
70 | public: | |
6a5a7fba VZ |
71 | MySimpleFrame(wxWindow *parent) |
72 | : wxFrame(parent, wxID_ANY, "MySimpleCanvas") | |
73 | { | |
74 | new MySimpleCanvas(this); | |
db4ad642 | 75 | |
6a5a7fba VZ |
76 | // ensure that we have scrollbars initially |
77 | SetClientSize(MySimpleCanvas::WIDTH/2, MySimpleCanvas::HEIGHT/2); | |
d9f4cc10 | 78 | |
6a5a7fba VZ |
79 | Show(); |
80 | } | |
db4ad642 RR |
81 | }; |
82 | ||
6a5a7fba VZ |
83 | // ---------------------------------------------------------------------- |
84 | // a more complex example | |
85 | // ---------------------------------------------------------------------- | |
86 | ||
87 | // MyCanvas | |
88 | class MyCanvas : public wxScrolledWindow | |
89 | { | |
90 | public: | |
91 | MyCanvas(wxWindow *parent); | |
db4ad642 | 92 | |
6a5a7fba VZ |
93 | private: |
94 | void OnPaint(wxPaintEvent& event); | |
95 | void OnQueryPosition(wxCommandEvent& event); | |
96 | void OnAddButton(wxCommandEvent& event); | |
97 | void OnDeleteButton(wxCommandEvent& event); | |
98 | void OnMoveButton(wxCommandEvent& event); | |
99 | void OnScrollWin(wxCommandEvent& event); | |
100 | void OnMouseRightDown(wxMouseEvent& event); | |
101 | void OnMouseWheel(wxMouseEvent& event); | |
db4ad642 | 102 | |
6a5a7fba | 103 | wxButton *m_button; |
db4ad642 | 104 | |
6a5a7fba VZ |
105 | DECLARE_EVENT_TABLE() |
106 | }; | |
107 | ||
108 | class MyCanvasFrame : public wxFrame | |
db4ad642 | 109 | { |
6a5a7fba VZ |
110 | public: |
111 | MyCanvasFrame(wxWindow *parent) | |
112 | : wxFrame(parent, wxID_ANY, "MyCanvas") | |
113 | { | |
114 | m_canvas = new MyCanvas(this); | |
db4ad642 | 115 | |
6a5a7fba VZ |
116 | wxMenu *menuFile = new wxMenu(); |
117 | menuFile->Append(wxID_DELETE, "&Delete all"); | |
118 | menuFile->Append(wxID_NEW, "Insert &new"); | |
db4ad642 | 119 | |
6a5a7fba VZ |
120 | wxMenuBar *mbar = new wxMenuBar(); |
121 | mbar->Append(menuFile, "&File"); | |
122 | SetMenuBar( mbar ); | |
db4ad642 | 123 | |
6a5a7fba VZ |
124 | Connect(wxID_DELETE, wxEVT_COMMAND_MENU_SELECTED, |
125 | wxCommandEventHandler(MyCanvasFrame::OnDeleteAll)); | |
126 | Connect(wxID_NEW, wxEVT_COMMAND_MENU_SELECTED, | |
127 | wxCommandEventHandler(MyCanvasFrame::OnInsertNew)); | |
db4ad642 | 128 | |
6a5a7fba VZ |
129 | Show(); |
130 | } | |
fdd3ed7a | 131 | |
6a5a7fba VZ |
132 | private: |
133 | void OnDeleteAll(wxCommandEvent& WXUNUSED(event)) | |
134 | { | |
135 | m_canvas->DestroyChildren(); | |
136 | } | |
fdd3ed7a | 137 | |
6a5a7fba VZ |
138 | void OnInsertNew(wxCommandEvent& WXUNUSED(event)) |
139 | { | |
140 | (void)new wxButton(m_canvas, wxID_ANY, "Hello", wxPoint(100,100)); | |
141 | } | |
fdd3ed7a | 142 | |
6a5a7fba VZ |
143 | MyCanvas *m_canvas; |
144 | }; | |
145 | ||
146 | // ---------------------------------------------------------------------------- | |
147 | // example using sizers with wxScrolledWindow | |
148 | // ---------------------------------------------------------------------------- | |
fdd3ed7a | 149 | |
6a5a7fba VZ |
150 | const wxSize SMALL_BUTTON( 100, 50 ); |
151 | const wxSize LARGE_BUTTON( 300, 200 ); | |
152 | ||
153 | class MySizerScrolledWindow : public wxScrolledWindow | |
fdd3ed7a RR |
154 | { |
155 | public: | |
6a5a7fba | 156 | MySizerScrolledWindow(wxWindow *parent); |
ed673c6a | 157 | |
6a5a7fba VZ |
158 | private: |
159 | // this button can be clicked to change its own size in the handler below, | |
160 | // the window size will be automatically adjusted to fit the button | |
ed673c6a | 161 | wxButton *m_button; |
fdd3ed7a | 162 | |
6a5a7fba | 163 | void OnResizeClick(wxCommandEvent& event); |
fdd3ed7a RR |
164 | }; |
165 | ||
6a5a7fba VZ |
166 | class MySizerFrame : public wxFrame |
167 | { | |
168 | public: | |
169 | MySizerFrame(wxWindow *parent) | |
170 | : wxFrame(parent, wxID_ANY, "MySizerScrolledWindow") | |
171 | { | |
172 | new MySizerScrolledWindow(this); | |
173 | ||
174 | // ensure that the scrollbars appear when the button becomes large | |
175 | SetClientSize(LARGE_BUTTON/2); | |
176 | Show(); | |
177 | } | |
178 | }; | |
2b5f62a0 VZ |
179 | |
180 | // ---------------------------------------------------------------------------- | |
6a5a7fba | 181 | // example showing scrolling only part of the window |
2b5f62a0 VZ |
182 | // ---------------------------------------------------------------------------- |
183 | ||
6a5a7fba VZ |
184 | // this window consists of an empty space in its corner, column labels window |
185 | // along its top, row labels window along its left hand side and a canvas in | |
186 | // the remaining space | |
2b5f62a0 | 187 | |
6a5a7fba | 188 | class MySubColLabels : public wxWindow |
2b5f62a0 | 189 | { |
6a5a7fba VZ |
190 | public: |
191 | MySubColLabels(wxScrolledWindow *parent) | |
192 | : wxWindow(parent, wxID_ANY) | |
193 | { | |
194 | m_owner = parent; | |
195 | ||
196 | Connect(wxEVT_PAINT, wxPaintEventHandler(MySubColLabels::OnPaint)); | |
197 | } | |
198 | ||
2b5f62a0 | 199 | private: |
6a5a7fba VZ |
200 | void OnPaint(wxPaintEvent& WXUNUSED(event)) |
201 | { | |
202 | wxPaintDC dc(this); | |
203 | ||
204 | // This is wrong.. it will translate both x and y if the | |
205 | // window is scrolled, the label windows are active in one | |
206 | // direction only. Do the action below instead -- RL. | |
207 | //m_owner->PrepareDC( dc ); | |
208 | ||
209 | int xScrollUnits, xOrigin; | |
2b5f62a0 | 210 | |
6a5a7fba VZ |
211 | m_owner->GetViewStart( &xOrigin, 0 ); |
212 | m_owner->GetScrollPixelsPerUnit( &xScrollUnits, 0 ); | |
213 | dc.SetDeviceOrigin( -xOrigin * xScrollUnits, 0 ); | |
2b5f62a0 | 214 | |
6a5a7fba VZ |
215 | dc.DrawText("Column 1", 5, 5); |
216 | dc.DrawText("Column 2", 105, 5); | |
217 | dc.DrawText("Column 3", 205, 5); | |
218 | } | |
219 | ||
220 | wxScrolledWindow *m_owner; | |
221 | }; | |
222 | ||
223 | class MySubRowLabels : public wxWindow | |
224 | { | |
2b5f62a0 | 225 | public: |
6a5a7fba VZ |
226 | MySubRowLabels(wxScrolledWindow *parent) |
227 | : wxWindow(parent, wxID_ANY) | |
228 | { | |
229 | m_owner = parent; | |
2b5f62a0 | 230 | |
6a5a7fba VZ |
231 | Connect(wxEVT_PAINT, wxPaintEventHandler(MySubRowLabels::OnPaint)); |
232 | } | |
2b5f62a0 | 233 | |
6a5a7fba VZ |
234 | private: |
235 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
236 | { | |
237 | wxPaintDC dc(this); | |
2b5f62a0 | 238 | |
6a5a7fba VZ |
239 | // This is wrong.. it will translate both x and y if the |
240 | // window is scrolled, the label windows are active in one | |
241 | // direction only. Do the action below instead -- RL. | |
242 | //m_owner->PrepareDC( dc ); | |
243 | ||
244 | int yScrollUnits, yOrigin; | |
245 | ||
246 | m_owner->GetViewStart( 0, &yOrigin ); | |
247 | m_owner->GetScrollPixelsPerUnit( 0, &yScrollUnits ); | |
248 | dc.SetDeviceOrigin( 0, -yOrigin * yScrollUnits ); | |
249 | ||
250 | dc.DrawText("Row 1", 5, 5); | |
251 | dc.DrawText("Row 2", 5, 30); | |
252 | dc.DrawText("Row 3", 5, 55); | |
253 | dc.DrawText("Row 4", 5, 80); | |
254 | dc.DrawText("Row 5", 5, 105); | |
255 | dc.DrawText("Row 6", 5, 130); | |
256 | } | |
257 | ||
258 | wxScrolledWindow *m_owner; | |
259 | }; | |
260 | ||
261 | class MySubCanvas : public wxPanel | |
262 | { | |
263 | public: | |
264 | MySubCanvas(wxScrolledWindow *parent, wxWindow *cols, wxWindow *rows) | |
265 | : wxPanel(parent, wxID_ANY) | |
266 | { | |
267 | m_owner = parent; | |
268 | m_colLabels = cols; | |
269 | m_rowLabels = rows; | |
270 | ||
271 | (void)new wxButton(this, wxID_ANY, "Hallo I", | |
272 | wxPoint(0,50), wxSize(100,25) ); | |
273 | (void)new wxButton(this, wxID_ANY, "Hallo II", | |
274 | wxPoint(200,50), wxSize(100,25) ); | |
275 | ||
276 | (void)new wxTextCtrl(this, wxID_ANY, "Text I", | |
277 | wxPoint(0,100), wxSize(100,25) ); | |
278 | (void)new wxTextCtrl(this, wxID_ANY, "Text II", | |
279 | wxPoint(200,100), wxSize(100,25) ); | |
280 | ||
281 | (void)new wxComboBox(this, wxID_ANY, "ComboBox I", | |
282 | wxPoint(0,150), wxSize(100,25)); | |
283 | (void)new wxComboBox(this, wxID_ANY, "ComboBox II", | |
284 | wxPoint(200,150), wxSize(100,25)); | |
285 | ||
286 | SetBackgroundColour("WHEAT"); | |
287 | ||
288 | Connect(wxEVT_PAINT, wxPaintEventHandler(MySubCanvas::OnPaint)); | |
289 | } | |
290 | ||
291 | // override the base class function so that when this window is scrolled, | |
292 | // the labels are scrolled in sync | |
293 | virtual void ScrollWindow(int dx, int dy, const wxRect *rect) | |
294 | { | |
295 | wxPanel::ScrollWindow( dx, dy, rect ); | |
296 | m_colLabels->ScrollWindow( dx, 0, rect ); | |
297 | m_rowLabels->ScrollWindow( 0, dy, rect ); | |
298 | } | |
299 | ||
300 | private: | |
301 | void OnPaint(wxPaintEvent& WXUNUSED(event)) | |
302 | { | |
303 | wxPaintDC dc( this ); | |
304 | m_owner->PrepareDC( dc ); | |
305 | ||
306 | dc.SetPen( *wxBLACK_PEN ); | |
307 | ||
308 | // OK, let's assume we are a grid control and we have two | |
309 | // grid cells. Here in OnPaint we want to know which cell | |
310 | // to redraw so that we prevent redrawing cells that don't | |
311 | // need to get redrawn. We have one cell at (0,0) and one | |
312 | // more at (200,0), both having a size of (100,25). | |
313 | ||
314 | // We can query how much the window has been scrolled | |
315 | // by calling CalcUnscrolledPosition() | |
316 | ||
317 | int scroll_x = 0; | |
318 | int scroll_y = 0; | |
319 | m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y ); | |
320 | ||
321 | // We also need to know the size of the window to see which | |
322 | // cells are completely hidden and not get redrawn | |
323 | ||
324 | int size_x = 0; | |
325 | int size_y = 0; | |
326 | GetClientSize( &size_x, &size_y ); | |
327 | ||
328 | // First cell: (0,0)(100,25) | |
329 | // It it on screen? | |
330 | if ((0+100-scroll_x > 0) && (0+25-scroll_y > 0) && | |
331 | (0-scroll_x < size_x) && (0-scroll_y < size_y)) | |
332 | { | |
333 | // Has the region on screen been exposed? | |
334 | if (IsExposed(0,0,100,25)) | |
335 | { | |
336 | dc.DrawRectangle( 0, 0, 100, 25 ); | |
337 | dc.DrawText("First Cell", 5, 5); | |
338 | } | |
339 | } | |
340 | ||
341 | ||
342 | // Second cell: (0,200)(100,25) | |
343 | // It it on screen? | |
344 | if ((200+100-scroll_x > 0) && (0+25-scroll_y > 0) && | |
345 | (200-scroll_x < size_x) && (0-scroll_y < size_y)) | |
346 | { | |
347 | // Has the region on screen been exposed? | |
348 | if (IsExposed(200,0,100,25)) | |
349 | { | |
350 | dc.DrawRectangle( 200, 0, 100, 25 ); | |
351 | dc.DrawText("Second Cell", 205, 5); | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
356 | wxScrolledWindow *m_owner; | |
357 | wxWindow *m_colLabels, | |
358 | *m_rowLabels; | |
359 | }; | |
360 | ||
361 | class MySubScrolledWindow : public wxScrolledWindow | |
362 | { | |
363 | public: | |
364 | enum | |
365 | { | |
366 | CORNER_WIDTH = 60, | |
367 | CORNER_HEIGHT = 25 | |
368 | }; | |
369 | ||
370 | MySubScrolledWindow(wxWindow *parent) | |
371 | : wxScrolledWindow(parent, wxID_ANY) | |
372 | { | |
373 | // create the children | |
374 | MySubColLabels *cols = new MySubColLabels(this); | |
375 | MySubRowLabels *rows = new MySubRowLabels(this); | |
376 | ||
377 | m_canvas = new MySubCanvas(this, cols, rows); | |
378 | ||
379 | // lay them out | |
380 | wxFlexGridSizer *sizer = new wxFlexGridSizer(2, 2, 10, 10); | |
381 | sizer->Add(CORNER_WIDTH, CORNER_HEIGHT); // just a spacer | |
382 | sizer->Add(cols, wxSizerFlags().Expand()); | |
383 | sizer->Add(rows, wxSizerFlags().Expand()); | |
384 | sizer->Add(m_canvas, wxSizerFlags().Expand()); | |
385 | sizer->AddGrowableRow(1); | |
386 | sizer->AddGrowableCol(1); | |
387 | SetSizer(sizer); | |
388 | ||
389 | // this is the key call: it means that only m_canvas will be scrolled | |
390 | // and not this window itself | |
391 | SetTargetWindow(m_canvas); | |
392 | ||
393 | SetScrollbars(10, 10, 50, 50); | |
394 | ||
395 | Connect(wxEVT_SIZE, wxSizeEventHandler(MySubScrolledWindow::OnSize)); | |
396 | } | |
397 | ||
398 | protected: | |
399 | // scrolled windows which use scroll target different from the window | |
400 | // itself must override this virtual method | |
401 | virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size) | |
402 | { | |
403 | // decrease the total size by the size of the non-scrollable parts | |
404 | // above/to the left of the canvas | |
405 | wxSize sizeCanvas(size); | |
406 | sizeCanvas.x -= 60; | |
407 | sizeCanvas.y -= 25; | |
408 | return sizeCanvas; | |
409 | } | |
410 | ||
411 | private: | |
412 | void OnSize(wxSizeEvent& WXUNUSED(event)) | |
413 | { | |
414 | // We need to override OnSize so that our scrolled | |
415 | // window a) does call Layout() to use sizers for | |
416 | // positioning the controls but b) does not query | |
417 | // the sizer for their size and use that for setting | |
418 | // the scrollable area as set that ourselves by | |
419 | // calling SetScrollbar() further down. | |
420 | ||
421 | Layout(); | |
422 | ||
423 | AdjustScrollbars(); | |
424 | } | |
425 | ||
426 | MySubCanvas *m_canvas; | |
2b5f62a0 VZ |
427 | }; |
428 | ||
6a5a7fba VZ |
429 | class MySubFrame : public wxFrame |
430 | { | |
431 | public: | |
432 | MySubFrame(wxWindow *parent) | |
433 | : wxFrame(parent, wxID_ANY, "MySubScrolledWindow") | |
434 | { | |
435 | new MySubScrolledWindow(this); | |
436 | ||
437 | Show(); | |
438 | } | |
439 | }; | |
2b5f62a0 | 440 | |
8a73bf3d | 441 | // ---------------------------------------------------------------------------- |
6a5a7fba | 442 | // more simple examples of wxScrolledWindow usage |
8a73bf3d VZ |
443 | // ---------------------------------------------------------------------------- |
444 | ||
445 | // base class for both of them | |
446 | class MyScrolledWindowBase : public wxScrolledWindow | |
447 | { | |
448 | public: | |
2b5f62a0 | 449 | MyScrolledWindowBase(wxWindow *parent) |
6a5a7fba VZ |
450 | : wxScrolledWindow(parent, wxID_ANY, |
451 | wxDefaultPosition, wxDefaultSize, | |
452 | wxBORDER_SUNKEN), | |
453 | m_nLines( 100 ) | |
8a73bf3d | 454 | { |
2b5f62a0 | 455 | wxClientDC dc(this); |
6a5a7fba | 456 | dc.GetTextExtent("Line 17", NULL, &m_hLine); |
8a73bf3d VZ |
457 | } |
458 | ||
459 | protected: | |
8a73bf3d | 460 | // the height of one line on screen |
6a5a7fba | 461 | int m_hLine; |
8a73bf3d VZ |
462 | |
463 | // the number of lines we draw | |
464 | size_t m_nLines; | |
465 | }; | |
466 | ||
467 | // this class does "stupid" redrawing - it redraws everything each time | |
2b5f62a0 VZ |
468 | // and sets the scrollbar extent directly. |
469 | ||
8a73bf3d VZ |
470 | class MyScrolledWindowDumb : public MyScrolledWindowBase |
471 | { | |
472 | public: | |
2b5f62a0 VZ |
473 | MyScrolledWindowDumb(wxWindow *parent) : MyScrolledWindowBase(parent) |
474 | { | |
475 | // no horz scrolling | |
b62ca03d | 476 | SetScrollbars(0, m_hLine, 0, m_nLines + 1, 0, 0, true /* no refresh */); |
2b5f62a0 | 477 | } |
8a73bf3d VZ |
478 | |
479 | virtual void OnDraw(wxDC& dc); | |
480 | }; | |
481 | ||
482 | // this class does "smart" redrawing - only redraws the lines which must be | |
2b5f62a0 VZ |
483 | // redrawn and sets the scroll rate and virtual size to affect the |
484 | // scrollbars. | |
485 | // | |
486 | // Note that this class should produce identical results to the one above. | |
487 | ||
8a73bf3d VZ |
488 | class MyScrolledWindowSmart : public MyScrolledWindowBase |
489 | { | |
490 | public: | |
2b5f62a0 VZ |
491 | MyScrolledWindowSmart(wxWindow *parent) : MyScrolledWindowBase(parent) |
492 | { | |
493 | // no horz scrolling | |
494 | SetScrollRate( 0, m_hLine ); | |
4e04f777 | 495 | SetVirtualSize( wxDefaultCoord, ( m_nLines + 1 ) * m_hLine ); |
2b5f62a0 | 496 | } |
8a73bf3d VZ |
497 | |
498 | virtual void OnDraw(wxDC& dc); | |
499 | }; | |
500 | ||
57ac1a56 | 501 | // ---------------------------------------------------------------------------- |
6a5a7fba VZ |
502 | // implements a text viewer with simple block selection to test auto-scrolling |
503 | // functionality | |
57ac1a56 RN |
504 | // ---------------------------------------------------------------------------- |
505 | ||
6a5a7fba | 506 | class MyAutoScrollingWindow : public wxScrolledWindow |
57ac1a56 | 507 | { |
6a5a7fba VZ |
508 | public: |
509 | MyAutoScrollingWindow( wxWindow* parent ); | |
57ac1a56 RN |
510 | wxRect DeviceCoordsToGraphicalChars(wxRect updRect) const; |
511 | wxPoint DeviceCoordsToGraphicalChars(wxPoint pos) const; | |
512 | wxPoint GraphicalCharToDeviceCoords(wxPoint pos) const; | |
513 | wxRect LogicalCoordsToGraphicalChars(wxRect updRect) const; | |
514 | wxPoint LogicalCoordsToGraphicalChars(wxPoint pos) const; | |
515 | wxPoint GraphicalCharToLogicalCoords(wxPoint pos) const; | |
516 | void MyRefresh(); | |
517 | bool IsSelected(int chX, int chY) const; | |
518 | static bool IsInside(int k, int bound1, int bound2); | |
6a5a7fba | 519 | static wxRect DCNormalize(int x, int y, int w, int h); |
57ac1a56 | 520 | |
6a5a7fba VZ |
521 | private: |
522 | // event handlers | |
57ac1a56 RN |
523 | void OnDraw(wxDC& dc); |
524 | void OnMouseLeftDown(wxMouseEvent& event); | |
525 | void OnMouseLeftUp(wxMouseEvent& event); | |
526 | void OnMouseMove(wxMouseEvent& event); | |
e0666bdc | 527 | void OnMouseCaptureLost(wxMouseCaptureLostEvent& event); |
57ac1a56 | 528 | void OnScroll(wxScrollWinEvent& event); |
4686e0f6 | 529 | |
6a5a7fba VZ |
530 | // test data variables |
531 | static const char* sm_testData; | |
532 | static const int sm_lineCnt; // line count | |
533 | static const int sm_lineLen; // line length in characters | |
534 | // sizes for graphical data | |
535 | int m_fontH, m_fontW; | |
536 | // selection tracking | |
537 | wxPoint m_selStart; // beginning of blockwise selection | |
538 | wxPoint m_cursor; // end of blockwise selection (mouse position) | |
539 | ||
540 | // gui stuff | |
541 | wxFont m_font; | |
542 | ||
543 | ||
4686e0f6 | 544 | DECLARE_EVENT_TABLE() |
57ac1a56 | 545 | }; |
2b5f62a0 | 546 | |
6a5a7fba VZ |
547 | class MyAutoFrame : public wxFrame |
548 | { | |
549 | public: | |
550 | MyAutoFrame(wxWindow *parent) | |
551 | : wxFrame(parent, wxID_ANY, "MyAutoScrollingWindow") | |
552 | { | |
553 | new MyAutoScrollingWindow(this); | |
554 | ||
555 | Show(); | |
556 | } | |
557 | }; | |
558 | ||
559 | ||
8a73bf3d | 560 | // ---------------------------------------------------------------------------- |
6a5a7fba | 561 | // MyFrame: the main application frame showing all the classes above |
8a73bf3d | 562 | // ---------------------------------------------------------------------------- |
fdd3ed7a RR |
563 | |
564 | class MyFrame: public wxFrame | |
565 | { | |
566 | public: | |
567 | MyFrame(); | |
568 | ||
6a5a7fba VZ |
569 | private: |
570 | void OnAbout(wxCommandEvent& event); | |
571 | void OnQuit(wxCommandEvent& event); | |
fdd3ed7a | 572 | |
6a5a7fba VZ |
573 | void OnTestSimple(wxCommandEvent& WXUNUSED(event)) { new MySimpleFrame(this); } |
574 | void OnTestCanvas(wxCommandEvent& WXUNUSED(event)) { new MyCanvasFrame(this); } | |
575 | void OnTestSizer(wxCommandEvent& WXUNUSED(event)) { new MySizerFrame(this); } | |
576 | void OnTestSub(wxCommandEvent& WXUNUSED(event)) { new MySubFrame(this); } | |
577 | void OnTestAuto(wxCommandEvent& WXUNUSED(event)) { new MyAutoFrame(this); } | |
fdd3ed7a | 578 | |
fdd3ed7a RR |
579 | DECLARE_EVENT_TABLE() |
580 | }; | |
581 | ||
57ac1a56 | 582 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 583 | // MyApp |
57ac1a56 | 584 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 585 | |
6a5a7fba | 586 | class MyApp : public wxApp |
fdd3ed7a RR |
587 | { |
588 | public: | |
589 | virtual bool OnInit(); | |
590 | }; | |
591 | ||
57ac1a56 | 592 | |
6a5a7fba VZ |
593 | // ============================================================================ |
594 | // implementation | |
595 | // ============================================================================ | |
ed673c6a | 596 | |
57ac1a56 | 597 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 598 | // MyCanvas |
57ac1a56 | 599 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 600 | |
6a5a7fba VZ |
601 | const wxWindowID ID_ADDBUTTON = wxWindow::NewControlId(); |
602 | const wxWindowID ID_DELBUTTON = wxWindow::NewControlId(); | |
603 | const wxWindowID ID_MOVEBUTTON = wxWindow::NewControlId(); | |
604 | const wxWindowID ID_SCROLLWIN = wxWindow::NewControlId(); | |
605 | const wxWindowID ID_QUERYPOS = wxWindow::NewControlId(); | |
606 | ||
607 | const wxWindowID ID_NEWBUTTON = wxWindow::NewControlId(); | |
fdd3ed7a RR |
608 | |
609 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
6a5a7fba VZ |
610 | EVT_PAINT( MyCanvas::OnPaint) |
611 | EVT_RIGHT_DOWN( MyCanvas::OnMouseRightDown) | |
612 | EVT_MOUSEWHEEL( MyCanvas::OnMouseWheel) | |
613 | EVT_BUTTON( ID_QUERYPOS, MyCanvas::OnQueryPosition) | |
614 | EVT_BUTTON( ID_ADDBUTTON, MyCanvas::OnAddButton) | |
615 | EVT_BUTTON( ID_DELBUTTON, MyCanvas::OnDeleteButton) | |
616 | EVT_BUTTON( ID_MOVEBUTTON, MyCanvas::OnMoveButton) | |
617 | EVT_BUTTON( ID_SCROLLWIN, MyCanvas::OnScrollWin) | |
fdd3ed7a RR |
618 | END_EVENT_TABLE() |
619 | ||
6a5a7fba VZ |
620 | MyCanvas::MyCanvas(wxWindow *parent) |
621 | : wxScrolledWindow(parent, wxID_ANY, | |
622 | wxDefaultPosition, wxDefaultSize, | |
623 | wxSUNKEN_BORDER | wxTAB_TRAVERSAL) | |
fdd3ed7a | 624 | { |
6a5a7fba VZ |
625 | // you can use either a single SetScrollbars() call or these 2 functions, |
626 | // usually using them is better because you normally won't need to change | |
627 | // the scroll rate in the future and the sizer can be used to update the | |
628 | // virtual size automatically | |
2b5f62a0 VZ |
629 | SetScrollRate( 10, 10 ); |
630 | SetVirtualSize( 500, 1000 ); | |
631 | ||
6a5a7fba VZ |
632 | (void) new wxButton( this, ID_ADDBUTTON, "add button", wxPoint(10,10) ); |
633 | (void) new wxButton( this, ID_DELBUTTON, "del button", wxPoint(10,40) ); | |
634 | (void) new wxButton( this, ID_MOVEBUTTON, "move button", wxPoint(150,10) ); | |
635 | (void) new wxButton( this, ID_SCROLLWIN, "scroll win", wxPoint(250,10) ); | |
aa06a8fd | 636 | |
6a5a7fba VZ |
637 | wxPanel *test = new wxPanel( this, wxID_ANY, |
638 | wxPoint(10, 110), wxSize(130,50), | |
639 | wxSIMPLE_BORDER | wxTAB_TRAVERSAL ); | |
640 | test->SetBackgroundColour( "WHEAT" ); | |
5e014a0c | 641 | |
6a5a7fba | 642 | SetBackgroundColour( "BLUE" ); |
fdd3ed7a RR |
643 | } |
644 | ||
4686e0f6 | 645 | void MyCanvas::OnMouseRightDown( wxMouseEvent &event ) |
bf0c00c6 | 646 | { |
4686e0f6 VZ |
647 | wxPoint pt( event.GetPosition() ); |
648 | int x,y; | |
649 | CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); | |
6a5a7fba VZ |
650 | wxLogMessage("Mouse down event at: %d %d, scrolled: %d %d", |
651 | pt.x, pt.y, x, y); | |
4686e0f6 | 652 | } |
f6bcfd97 | 653 | |
4686e0f6 VZ |
654 | void MyCanvas::OnMouseWheel( wxMouseEvent &event ) |
655 | { | |
656 | wxPoint pt( event.GetPosition() ); | |
657 | int x,y; | |
658 | CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); | |
6a5a7fba VZ |
659 | wxLogMessage( "Mouse wheel event at: %d %d, scrolled: %d %d\n" |
660 | "Rotation: %d, delta = %d", | |
4686e0f6 VZ |
661 | pt.x, pt.y, x, y, |
662 | event.GetWheelRotation(), event.GetWheelDelta() ); | |
663 | ||
664 | event.Skip(); | |
bf0c00c6 RR |
665 | } |
666 | ||
667 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
668 | { | |
669 | wxPaintDC dc( this ); | |
670 | PrepareDC( dc ); | |
671 | ||
6a5a7fba | 672 | dc.DrawText( "Press right mouse button to test calculations!", 160, 50 ); |
bf0c00c6 | 673 | |
6a5a7fba | 674 | dc.DrawText( "Some text", 140, 140 ); |
aa06a8fd | 675 | |
bf0c00c6 RR |
676 | dc.DrawRectangle( 100, 160, 200, 200 ); |
677 | } | |
678 | ||
307f16e8 RR |
679 | void MyCanvas::OnQueryPosition( wxCommandEvent &WXUNUSED(event) ) |
680 | { | |
681 | wxPoint pt( m_button->GetPosition() ); | |
6a5a7fba | 682 | wxLogMessage( "Position of \"Query position\" is %d %d", pt.x, pt.y ); |
bf0c00c6 | 683 | pt = ClientToScreen( pt ); |
6a5a7fba VZ |
684 | wxLogMessage("Position of \"Query position\" on screen is %d %d", |
685 | pt.x, pt.y); | |
307f16e8 RR |
686 | } |
687 | ||
ed673c6a RR |
688 | void MyCanvas::OnAddButton( wxCommandEvent &WXUNUSED(event) ) |
689 | { | |
6a5a7fba VZ |
690 | wxLogMessage( "Inserting button at position 10,70..." ); |
691 | wxButton *button = new wxButton( this, ID_NEWBUTTON, "new button", | |
692 | wxPoint(10,70), wxSize(80,25) ); | |
bf0c00c6 | 693 | wxPoint pt( button->GetPosition() ); |
6a5a7fba | 694 | wxLogMessage( "-> Position after inserting %d %d", pt.x, pt.y ); |
ed673c6a RR |
695 | } |
696 | ||
256b8649 | 697 | void MyCanvas::OnDeleteButton( wxCommandEvent &WXUNUSED(event) ) |
ed673c6a | 698 | { |
6a5a7fba | 699 | wxLogMessage( "Deleting button inserted with \"Add button\"..." ); |
ed673c6a RR |
700 | wxWindow *win = FindWindow( ID_NEWBUTTON ); |
701 | if (win) | |
702 | win->Destroy(); | |
703 | else | |
6a5a7fba | 704 | wxLogMessage( "-> No window with id = ID_NEWBUTTON found." ); |
ed673c6a RR |
705 | } |
706 | ||
707 | void MyCanvas::OnMoveButton( wxCommandEvent &event ) | |
708 | { | |
6a5a7fba | 709 | wxLogMessage( "Moving button 10 pixels downward.." ); |
ed673c6a | 710 | wxWindow *win = FindWindow( event.GetId() ); |
bf0c00c6 | 711 | wxPoint pt( win->GetPosition() ); |
6a5a7fba | 712 | wxLogMessage( "-> Position before move is %d %d", pt.x, pt.y ); |
422d0ff0 | 713 | win->Move( wxDefaultCoord, pt.y + 10 ); |
bf0c00c6 | 714 | pt = win->GetPosition(); |
6a5a7fba | 715 | wxLogMessage( "-> Position after move is %d %d", pt.x, pt.y ); |
ed673c6a RR |
716 | } |
717 | ||
718 | void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) ) | |
719 | { | |
6a5a7fba VZ |
720 | wxLogMessage("Scrolling 2 units up.\n" |
721 | "The white square and the controls should move equally!"); | |
ed673c6a | 722 | int x,y; |
8073eb40 | 723 | GetViewStart( &x, &y ); |
4e04f777 | 724 | Scroll( wxDefaultCoord, y+2 ); |
ed673c6a RR |
725 | } |
726 | ||
57ac1a56 | 727 | // ---------------------------------------------------------------------------- |
6a5a7fba | 728 | // MySizerScrolledWindow |
57ac1a56 | 729 | // ---------------------------------------------------------------------------- |
2b5f62a0 | 730 | |
6a5a7fba VZ |
731 | MySizerScrolledWindow::MySizerScrolledWindow(wxWindow *parent) |
732 | : wxScrolledWindow(parent) | |
2b5f62a0 | 733 | { |
6a5a7fba | 734 | SetBackgroundColour( "GREEN" ); |
2b5f62a0 VZ |
735 | |
736 | // Set the rate we'd like for scrolling. | |
737 | ||
738 | SetScrollRate( 5, 5 ); | |
739 | ||
6a5a7fba VZ |
740 | // Populate a sizer with a 'resizing' button and some other static |
741 | // decoration | |
2b5f62a0 | 742 | |
6a5a7fba | 743 | wxFlexGridSizer *sizer = new wxFlexGridSizer(2); |
2b5f62a0 | 744 | |
6a5a7fba VZ |
745 | m_button = new wxButton( this, wxID_RESIZE_FRAME, "Press me", |
746 | wxDefaultPosition, SMALL_BUTTON ); | |
2b5f62a0 | 747 | |
6a5a7fba VZ |
748 | sizer->Add(m_button, wxSizerFlags().Centre().Border(20)); |
749 | sizer->Add(new wxStaticText(this, wxID_ANY, "This is just"), | |
750 | wxSizerFlags().Centre()); | |
751 | sizer->Add(new wxStaticText(this, wxID_ANY, "some decoration"), | |
752 | wxSizerFlags().Centre()); | |
753 | sizer->Add(new wxStaticText(this, wxID_ANY, "for you to scroll..."), | |
754 | wxSizerFlags().Centre()); | |
2b5f62a0 VZ |
755 | |
756 | // Then use the sizer to set the scrolled region size. | |
757 | ||
6a5a7fba VZ |
758 | SetSizer( sizer ); |
759 | ||
760 | Connect(wxID_RESIZE_FRAME, wxEVT_COMMAND_BUTTON_CLICKED, | |
761 | wxCommandEventHandler(MySizerScrolledWindow::OnResizeClick)); | |
2b5f62a0 VZ |
762 | } |
763 | ||
6a5a7fba | 764 | void MySizerScrolledWindow::OnResizeClick(wxCommandEvent &WXUNUSED(event)) |
2b5f62a0 VZ |
765 | { |
766 | // Arbitrarily resize the button to change the minimum size of | |
767 | // the (scrolled) sizer. | |
768 | ||
6a5a7fba VZ |
769 | if ( m_button->GetSize() == SMALL_BUTTON ) |
770 | m_button->SetSizeHints(LARGE_BUTTON); | |
2b5f62a0 | 771 | else |
6a5a7fba | 772 | m_button->SetSizeHints(SMALL_BUTTON); |
2b5f62a0 VZ |
773 | |
774 | // Force update layout and scrollbars, since nothing we do here | |
775 | // necessarily generates a size event which would do it for us. | |
2b5f62a0 VZ |
776 | FitInside(); |
777 | } | |
778 | ||
57ac1a56 | 779 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 780 | // MyFrame |
57ac1a56 | 781 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 782 | |
6a5a7fba VZ |
783 | const wxWindowID Scroll_Test_Simple = wxWindow::NewControlId(); |
784 | const wxWindowID Scroll_Test_Canvas = wxWindow::NewControlId(); | |
785 | const wxWindowID Scroll_Test_Sizers = wxWindow::NewControlId(); | |
786 | const wxWindowID Scroll_Test_Sub = wxWindow::NewControlId(); | |
787 | const wxWindowID Scroll_Test_Auto = wxWindow::NewControlId(); | |
fdd3ed7a RR |
788 | |
789 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
6a5a7fba VZ |
790 | EVT_MENU(wxID_ABOUT, MyFrame::OnAbout) |
791 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
792 | ||
793 | EVT_MENU(Scroll_Test_Simple, MyFrame::OnTestSimple) | |
794 | EVT_MENU(Scroll_Test_Canvas, MyFrame::OnTestCanvas) | |
795 | EVT_MENU(Scroll_Test_Sizers, MyFrame::OnTestSizer) | |
796 | EVT_MENU(Scroll_Test_Sub, MyFrame::OnTestSub) | |
797 | EVT_MENU(Scroll_Test_Auto, MyFrame::OnTestAuto) | |
fdd3ed7a RR |
798 | END_EVENT_TABLE() |
799 | ||
800 | MyFrame::MyFrame() | |
6a5a7fba | 801 | : wxFrame(NULL, wxID_ANY, "wxWidgets scroll sample") |
fdd3ed7a | 802 | { |
6a5a7fba VZ |
803 | wxMenu *menuFile = new wxMenu; |
804 | menuFile->Append(wxID_ABOUT, "&About.."); | |
805 | menuFile->AppendSeparator(); | |
806 | menuFile->Append(wxID_EXIT, "E&xit\tAlt-X"); | |
807 | ||
808 | wxMenu *menuTest = new wxMenu; | |
809 | menuTest->Append(Scroll_Test_Simple, "&Simple scroll window\tF1", | |
810 | "Simplest possible scrolled window test."); | |
811 | menuTest->Append(Scroll_Test_Canvas, "Scrolled window with &children\tF2", | |
812 | "Scrolled window with controls on it."); | |
813 | menuTest->Append(Scroll_Test_Sizers, "Scrolled window with si&zer\tF3", | |
814 | "Scrolled window with children managed by sizer."); | |
815 | menuTest->Append(Scroll_Test_Sub, "Scrolled s&ub-window\tF4", | |
816 | "Window only part of which is scrolled."); | |
817 | menuTest->Append(Scroll_Test_Auto, "&Auto-scrolled window\tF5", | |
818 | "Window which scrolls when the mouse is held pressed " | |
819 | "outside of it."); | |
820 | ||
821 | wxMenuBar *mbar = new wxMenuBar; | |
822 | mbar->Append(menuFile, "&File"); | |
823 | mbar->Append(menuTest, "&Test"); | |
824 | ||
825 | SetMenuBar( mbar ); | |
826 | ||
827 | ||
828 | const wxSizerFlags flagsExpand(wxSizerFlags(1).Expand()); | |
829 | ||
830 | wxSizer *topsizer = new wxBoxSizer(wxVERTICAL); | |
831 | topsizer->Add(new wxStaticText(this, wxID_ANY, | |
832 | "The windows below should behave in the same way, even though\n" | |
833 | "they're implemented quite differently, see the code for details.\n" | |
834 | "\n" | |
835 | "The lines redrawn during odd/even repaint iterations are drawn in\n" | |
836 | "red/blue colour to allow seeing immediately how much is repainted,\n" | |
837 | "don't be surprised by this."), | |
838 | wxSizerFlags().Centre().Border()); | |
8a73bf3d VZ |
839 | |
840 | wxSizer *sizerBtm = new wxBoxSizer(wxHORIZONTAL); | |
6a5a7fba VZ |
841 | sizerBtm->Add(new MyScrolledWindowDumb(this), flagsExpand); |
842 | sizerBtm->Add(new MyScrolledWindowSmart(this), flagsExpand); | |
843 | topsizer->Add(sizerBtm, flagsExpand); | |
57ac1a56 | 844 | |
6a5a7fba | 845 | SetSizer(topsizer); |
ed673c6a | 846 | |
fdd3ed7a | 847 | |
6a5a7fba | 848 | Show(); |
8e217128 RR |
849 | } |
850 | ||
6a5a7fba | 851 | void MyFrame::OnQuit(wxCommandEvent &WXUNUSED(event)) |
8e217128 | 852 | { |
6a5a7fba | 853 | Close(true); |
fdd3ed7a RR |
854 | } |
855 | ||
856 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
857 | { | |
6a5a7fba VZ |
858 | (void)wxMessageBox( "wxScrolledWindow sample\n" |
859 | "\n" | |
860 | "Robert Roebling (c) 1998\n" | |
861 | "Vadim Zeitlin (c) 2008\n" | |
862 | "Autoscrolling examples\n" | |
863 | "Ron Lee (c) 2002\n" | |
864 | "Auto-timed-scrolling example\n" | |
865 | "Matt Gregory (c) 2003\n", | |
866 | "About wxWidgets scroll sample", | |
867 | wxICON_INFORMATION | wxOK ); | |
fdd3ed7a RR |
868 | } |
869 | ||
6a5a7fba | 870 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 871 | // MyApp |
6a5a7fba VZ |
872 | // ---------------------------------------------------------------------------- |
873 | ||
874 | IMPLEMENT_APP(MyApp) | |
fdd3ed7a RR |
875 | |
876 | bool MyApp::OnInit() | |
877 | { | |
45e6e6f8 VZ |
878 | if ( !wxApp::OnInit() ) |
879 | return false; | |
880 | ||
6a5a7fba | 881 | new MyFrame(); |
fdd3ed7a | 882 | |
db4ad642 | 883 | return true; |
fdd3ed7a RR |
884 | } |
885 | ||
8a73bf3d VZ |
886 | // ---------------------------------------------------------------------------- |
887 | // MyScrolledWindowXXX | |
888 | // ---------------------------------------------------------------------------- | |
889 | ||
8a73bf3d VZ |
890 | void MyScrolledWindowDumb::OnDraw(wxDC& dc) |
891 | { | |
892 | // this is useful to see which lines are redrawn | |
893 | static size_t s_redrawCount = 0; | |
894 | dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE); | |
895 | ||
6a5a7fba | 896 | int y = 0; |
8a73bf3d VZ |
897 | for ( size_t line = 0; line < m_nLines; line++ ) |
898 | { | |
6a5a7fba | 899 | int yPhys; |
8a73bf3d VZ |
900 | CalcScrolledPosition(0, y, NULL, &yPhys); |
901 | ||
6a5a7fba | 902 | dc.DrawText(wxString::Format("Line %u (logical %d, physical %d)", |
b143cf70 | 903 | unsigned(line), y, yPhys), 0, y); |
8a73bf3d VZ |
904 | y += m_hLine; |
905 | } | |
906 | } | |
907 | ||
908 | void MyScrolledWindowSmart::OnDraw(wxDC& dc) | |
909 | { | |
910 | // this is useful to see which lines are redrawn | |
911 | static size_t s_redrawCount = 0; | |
912 | dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE); | |
913 | ||
914 | // update region is always in device coords, translate to logical ones | |
915 | wxRect rectUpdate = GetUpdateRegion().GetBox(); | |
916 | CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y, | |
917 | &rectUpdate.x, &rectUpdate.y); | |
918 | ||
919 | size_t lineFrom = rectUpdate.y / m_hLine, | |
920 | lineTo = rectUpdate.GetBottom() / m_hLine; | |
921 | ||
922 | if ( lineTo > m_nLines - 1) | |
923 | lineTo = m_nLines - 1; | |
924 | ||
6a5a7fba | 925 | int y = lineFrom*m_hLine; |
8a73bf3d VZ |
926 | for ( size_t line = lineFrom; line <= lineTo; line++ ) |
927 | { | |
6a5a7fba | 928 | int yPhys; |
8a73bf3d VZ |
929 | CalcScrolledPosition(0, y, NULL, &yPhys); |
930 | ||
6a5a7fba | 931 | dc.DrawText(wxString::Format("Line %u (logical %d, physical %d)", |
b143cf70 | 932 | unsigned(line), y, yPhys), 0, y); |
8a73bf3d VZ |
933 | y += m_hLine; |
934 | } | |
935 | } | |
57ac1a56 RN |
936 | |
937 | // ---------------------------------------------------------------------------- | |
6a5a7fba | 938 | // MyAutoScrollingWindow |
57ac1a56 RN |
939 | // ---------------------------------------------------------------------------- |
940 | ||
6a5a7fba VZ |
941 | BEGIN_EVENT_TABLE(MyAutoScrollingWindow, wxScrolledWindow) |
942 | EVT_LEFT_DOWN(MyAutoScrollingWindow::OnMouseLeftDown) | |
943 | EVT_LEFT_UP(MyAutoScrollingWindow::OnMouseLeftUp) | |
944 | EVT_MOTION(MyAutoScrollingWindow::OnMouseMove) | |
945 | EVT_MOUSE_CAPTURE_LOST(MyAutoScrollingWindow::OnMouseCaptureLost) | |
946 | EVT_SCROLLWIN(MyAutoScrollingWindow::OnScroll) | |
57ac1a56 RN |
947 | END_EVENT_TABLE() |
948 | ||
6a5a7fba VZ |
949 | MyAutoScrollingWindow::MyAutoScrollingWindow(wxWindow* parent) |
950 | : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, | |
951 | wxVSCROLL | wxHSCROLL | wxSUNKEN_BORDER), | |
952 | m_selStart(-1, -1), | |
953 | m_cursor(-1, -1), | |
954 | m_font(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL) | |
57ac1a56 RN |
955 | { |
956 | wxClientDC dc(this); | |
957 | // query dc for text size | |
958 | dc.SetFont(m_font); | |
6a5a7fba | 959 | dc.GetTextExtent(wxString("A"), &m_fontW, &m_fontH); |
57ac1a56 RN |
960 | // set up the virtual window |
961 | SetScrollbars(m_fontW, m_fontH, sm_lineLen, sm_lineCnt); | |
962 | } | |
963 | ||
6a5a7fba VZ |
964 | wxRect |
965 | MyAutoScrollingWindow::DeviceCoordsToGraphicalChars(wxRect updRect) const | |
57ac1a56 RN |
966 | { |
967 | wxPoint pos(updRect.GetPosition()); | |
968 | pos = DeviceCoordsToGraphicalChars(pos); | |
969 | updRect.x = pos.x; | |
970 | updRect.y = pos.y; | |
971 | updRect.width /= m_fontW; | |
972 | updRect.height /= m_fontH; | |
973 | // the *CoordsToGraphicalChars() funcs round down to upper-left corner, | |
974 | // so an off-by-one correction is needed | |
975 | ++updRect.width; // kludge | |
976 | ++updRect.height; // kludge | |
977 | return updRect; | |
978 | } | |
979 | ||
6a5a7fba VZ |
980 | wxPoint |
981 | MyAutoScrollingWindow::DeviceCoordsToGraphicalChars(wxPoint pos) const | |
57ac1a56 RN |
982 | { |
983 | pos.x /= m_fontW; | |
984 | pos.y /= m_fontH; | |
985 | int vX, vY; | |
986 | GetViewStart(&vX, &vY); | |
987 | pos.x += vX; | |
988 | pos.y += vY; | |
989 | return pos; | |
990 | } | |
991 | ||
6a5a7fba VZ |
992 | wxPoint |
993 | MyAutoScrollingWindow::GraphicalCharToDeviceCoords(wxPoint pos) const | |
57ac1a56 RN |
994 | { |
995 | int vX, vY; | |
996 | GetViewStart(&vX, &vY); | |
997 | pos.x -= vX; | |
998 | pos.y -= vY; | |
999 | pos.x *= m_fontW; | |
1000 | pos.y *= m_fontH; | |
1001 | return pos; | |
1002 | } | |
1003 | ||
6a5a7fba VZ |
1004 | wxRect |
1005 | MyAutoScrollingWindow::LogicalCoordsToGraphicalChars(wxRect updRect) const | |
57ac1a56 RN |
1006 | { |
1007 | wxPoint pos(updRect.GetPosition()); | |
1008 | pos = LogicalCoordsToGraphicalChars(pos); | |
1009 | updRect.x = pos.x; | |
1010 | updRect.y = pos.y; | |
1011 | updRect.width /= m_fontW; | |
1012 | updRect.height /= m_fontH; | |
1013 | // the *CoordsToGraphicalChars() funcs round down to upper-left corner, | |
1014 | // so an off-by-one correction is needed | |
1015 | ++updRect.width; // kludge | |
1016 | ++updRect.height; // kludge | |
1017 | return updRect; | |
1018 | } | |
1019 | ||
6a5a7fba VZ |
1020 | wxPoint |
1021 | MyAutoScrollingWindow::LogicalCoordsToGraphicalChars(wxPoint pos) const | |
57ac1a56 RN |
1022 | { |
1023 | pos.x /= m_fontW; | |
1024 | pos.y /= m_fontH; | |
1025 | return pos; | |
1026 | } | |
1027 | ||
6a5a7fba VZ |
1028 | wxPoint |
1029 | MyAutoScrollingWindow::GraphicalCharToLogicalCoords(wxPoint pos) const | |
57ac1a56 RN |
1030 | { |
1031 | pos.x *= m_fontW; | |
1032 | pos.y *= m_fontH; | |
1033 | return pos; | |
1034 | } | |
1035 | ||
6a5a7fba | 1036 | void MyAutoScrollingWindow::MyRefresh() |
57ac1a56 RN |
1037 | { |
1038 | static wxPoint lastSelStart(-1, -1), lastCursor(-1, -1); | |
1039 | // refresh last selected area (to deselect previously selected text) | |
1040 | wxRect lastUpdRect( | |
1041 | GraphicalCharToDeviceCoords(lastSelStart), | |
1042 | GraphicalCharToDeviceCoords(lastCursor) | |
1043 | ); | |
1044 | // off-by-one corrections, necessary because it's not possible to know | |
1045 | // when to round up until rect is normalized by lastUpdRect constructor | |
1046 | lastUpdRect.width += m_fontW; // kludge | |
1047 | lastUpdRect.height += m_fontH; // kludge | |
1048 | // refresh currently selected (to select previously unselected text) | |
1049 | wxRect updRect( | |
1050 | GraphicalCharToDeviceCoords(m_selStart), | |
1051 | GraphicalCharToDeviceCoords(m_cursor) | |
1052 | ); | |
1053 | // off-by-one corrections | |
1054 | updRect.width += m_fontW; // kludge | |
1055 | updRect.height += m_fontH; // kludge | |
1056 | // find necessary refresh areas | |
6a5a7fba VZ |
1057 | int rx = lastUpdRect.x; |
1058 | int ry = lastUpdRect.y; | |
1059 | int rw = updRect.x - lastUpdRect.x; | |
1060 | int rh = lastUpdRect.height; | |
57ac1a56 RN |
1061 | if (rw && rh) { |
1062 | RefreshRect(DCNormalize(rx, ry, rw, rh)); | |
1063 | } | |
1064 | rx = updRect.x; | |
1065 | ry = updRect.y + updRect.height; | |
1066 | rw= updRect.width; | |
1067 | rh = (lastUpdRect.y + lastUpdRect.height) - (updRect.y + updRect.height); | |
1068 | if (rw && rh) { | |
1069 | RefreshRect(DCNormalize(rx, ry, rw, rh)); | |
1070 | } | |
1071 | rx = updRect.x + updRect.width; | |
1072 | ry = lastUpdRect.y; | |
1073 | rw = (lastUpdRect.x + lastUpdRect.width) - (updRect.x + updRect.width); | |
1074 | rh = lastUpdRect.height; | |
1075 | if (rw && rh) { | |
1076 | RefreshRect(DCNormalize(rx, ry, rw, rh)); | |
1077 | } | |
1078 | rx = updRect.x; | |
1079 | ry = lastUpdRect.y; | |
1080 | rw = updRect.width; | |
1081 | rh = updRect.y - lastUpdRect.y; | |
1082 | if (rw && rh) { | |
1083 | RefreshRect(DCNormalize(rx, ry, rw, rh)); | |
1084 | } | |
1085 | // update last | |
1086 | lastSelStart = m_selStart; | |
1087 | lastCursor = m_cursor; | |
1088 | } | |
1089 | ||
6a5a7fba | 1090 | bool MyAutoScrollingWindow::IsSelected(int chX, int chY) const |
57ac1a56 RN |
1091 | { |
1092 | if (IsInside(chX, m_selStart.x, m_cursor.x) | |
1093 | && IsInside(chY, m_selStart.y, m_cursor.y)) { | |
4e04f777 | 1094 | return true; |
57ac1a56 | 1095 | } |
4e04f777 | 1096 | return false; |
57ac1a56 RN |
1097 | } |
1098 | ||
6a5a7fba | 1099 | bool MyAutoScrollingWindow::IsInside(int k, int bound1, int bound2) |
57ac1a56 RN |
1100 | { |
1101 | if ((k >= bound1 && k <= bound2) || (k >= bound2 && k <= bound1)) { | |
4e04f777 | 1102 | return true; |
57ac1a56 | 1103 | } |
4e04f777 | 1104 | return false; |
57ac1a56 RN |
1105 | } |
1106 | ||
6a5a7fba VZ |
1107 | wxRect |
1108 | MyAutoScrollingWindow::DCNormalize(int x, int y, int w, int h) | |
57ac1a56 RN |
1109 | { |
1110 | // this is needed to get rid of the graphical remnants from the selection | |
1111 | // I think it's because DrawRectangle() excludes a pixel in either direction | |
1112 | const int kludge = 1; | |
1113 | // make (x, y) the top-left corner | |
1114 | if (w < 0) { | |
1115 | w = -w + kludge; | |
1116 | x -= w; | |
1117 | } else { | |
1118 | x -= kludge; | |
1119 | w += kludge; | |
1120 | } | |
1121 | if (h < 0) { | |
1122 | h = -h + kludge; | |
1123 | y -= h; | |
1124 | } else { | |
1125 | y -= kludge; | |
1126 | h += kludge; | |
1127 | } | |
1128 | return wxRect(x, y, w, h); | |
1129 | } | |
1130 | ||
6a5a7fba | 1131 | void MyAutoScrollingWindow::OnDraw(wxDC& dc) |
57ac1a56 RN |
1132 | { |
1133 | dc.SetFont(m_font); | |
1134 | wxBrush normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW) | |
1135 | , wxSOLID); | |
1136 | wxBrush selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT) | |
1137 | , wxSOLID); | |
1138 | dc.SetPen(*wxTRANSPARENT_PEN); | |
1663ec5f PC |
1139 | const wxString str = sm_testData; |
1140 | size_t strLength = str.length(); | |
1141 | wxString::const_iterator str_i; | |
4e04f777 | 1142 | |
57ac1a56 RN |
1143 | // draw the characters |
1144 | // 1. for each update region | |
1145 | for (wxRegionIterator upd(GetUpdateRegion()); upd; ++upd) { | |
57ac1a56 RN |
1146 | wxRect updRect = upd.GetRect(); |
1147 | wxRect updRectInGChars(DeviceCoordsToGraphicalChars(updRect)); | |
1148 | // 2. for each row of chars in the update region | |
1149 | for (int chY = updRectInGChars.y | |
1150 | ; chY <= updRectInGChars.y + updRectInGChars.height; ++chY) { | |
1151 | // 3. for each character in the row | |
1663ec5f | 1152 | bool isFirstX = true; |
57ac1a56 RN |
1153 | for (int chX = updRectInGChars.x |
1154 | ; chX <= updRectInGChars.x + updRectInGChars.width | |
1155 | ; ++chX) { | |
1156 | // 4. set up dc | |
1157 | if (IsSelected(chX, chY)) { | |
1158 | dc.SetBrush(selBrush); | |
1159 | dc.SetTextForeground( wxSystemSettings::GetColour | |
1160 | (wxSYS_COLOUR_HIGHLIGHTTEXT)); | |
1161 | } else { | |
1162 | dc.SetBrush(normBrush); | |
1163 | dc.SetTextForeground( wxSystemSettings::GetColour | |
1164 | (wxSYS_COLOUR_WINDOWTEXT)); | |
1165 | } | |
1166 | // 5. find position info | |
1167 | wxPoint charPos = GraphicalCharToLogicalCoords(wxPoint | |
1168 | (chX, chY)); | |
1169 | // 6. draw! | |
1170 | dc.DrawRectangle(charPos.x, charPos.y, m_fontW, m_fontH); | |
4e04f777 WS |
1171 | size_t charIndex = chY * sm_lineLen + chX; |
1172 | if (chY < sm_lineCnt && | |
1173 | chX < sm_lineLen && | |
1663ec5f | 1174 | charIndex < strLength) |
4e04f777 | 1175 | { |
1663ec5f PC |
1176 | if (isFirstX) |
1177 | { | |
1178 | str_i = str.begin() + charIndex; | |
1179 | isFirstX = false; | |
1180 | } | |
1181 | dc.DrawText(*str_i, charPos.x, charPos.y); | |
1182 | ++str_i; | |
57ac1a56 RN |
1183 | } |
1184 | } | |
1185 | } | |
1186 | } | |
1187 | } | |
1188 | ||
6a5a7fba | 1189 | void MyAutoScrollingWindow::OnMouseLeftDown(wxMouseEvent& event) |
57ac1a56 RN |
1190 | { |
1191 | // initial press of mouse button sets the beginning of the selection | |
1192 | m_selStart = DeviceCoordsToGraphicalChars(event.GetPosition()); | |
1193 | // set the cursor to the same position | |
1194 | m_cursor = m_selStart; | |
1195 | // draw/erase selection | |
1196 | MyRefresh(); | |
1197 | } | |
1198 | ||
6a5a7fba | 1199 | void MyAutoScrollingWindow::OnMouseLeftUp(wxMouseEvent& WXUNUSED(event)) |
57ac1a56 RN |
1200 | { |
1201 | // this test is necessary | |
1202 | if (HasCapture()) { | |
1203 | // uncapture mouse | |
1204 | ReleaseMouse(); | |
1205 | } | |
1206 | } | |
1207 | ||
6a5a7fba | 1208 | void MyAutoScrollingWindow::OnMouseMove(wxMouseEvent& event) |
57ac1a56 RN |
1209 | { |
1210 | // if user is dragging | |
1211 | if (event.Dragging() && event.LeftIsDown()) { | |
1212 | // set the new cursor position | |
1213 | m_cursor = DeviceCoordsToGraphicalChars(event.GetPosition()); | |
1214 | // draw/erase selection | |
1215 | MyRefresh(); | |
1216 | // capture mouse to activate auto-scrolling | |
1217 | if (!HasCapture()) { | |
1218 | CaptureMouse(); | |
1219 | } | |
1220 | } | |
1221 | } | |
1222 | ||
6a5a7fba VZ |
1223 | void |
1224 | MyAutoScrollingWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& | |
1225 | WXUNUSED(event)) | |
e0666bdc VZ |
1226 | { |
1227 | // we only capture mouse for timed scrolling, so nothing is needed here | |
1228 | // other than making sure to not call event.Skip() | |
1229 | } | |
1230 | ||
6a5a7fba | 1231 | void MyAutoScrollingWindow::OnScroll(wxScrollWinEvent& event) |
57ac1a56 RN |
1232 | { |
1233 | // need to move the cursor when autoscrolling | |
1234 | // FIXME: the cursor also moves when the scrollbar arrows are clicked | |
1235 | if (HasCapture()) { | |
1236 | if (event.GetOrientation() == wxHORIZONTAL) { | |
687706f5 | 1237 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) { |
57ac1a56 | 1238 | --m_cursor.x; |
687706f5 | 1239 | } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) { |
57ac1a56 RN |
1240 | ++m_cursor.x; |
1241 | } | |
1242 | } else if (event.GetOrientation() == wxVERTICAL) { | |
687706f5 | 1243 | if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) { |
57ac1a56 | 1244 | --m_cursor.y; |
687706f5 | 1245 | } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) { |
57ac1a56 RN |
1246 | ++m_cursor.y; |
1247 | } | |
1248 | } | |
1249 | } | |
1250 | MyRefresh(); | |
1251 | event.Skip(); | |
1252 | } | |
1253 | ||
6a5a7fba VZ |
1254 | const int MyAutoScrollingWindow::sm_lineCnt = 125; |
1255 | const int MyAutoScrollingWindow::sm_lineLen = 79; | |
1256 | const char *MyAutoScrollingWindow::sm_testData = | |
1257 | "162 Cult of the genius out of vanity. Because we think well of ourselves, but " | |
1258 | "nonetheless never suppose ourselves capable of producing a painting like one of " | |
1259 | "Raphael's or a dramatic scene like one of Shakespeare's, we convince ourselves " | |
1260 | "that the capacity to do so is quite extraordinarily marvelous, a wholly " | |
1261 | "uncommon accident, or, if we are still religiously inclined, a mercy from on " | |
1262 | "high. Thus our vanity, our self-love, promotes the cult of the genius: for only " | |
1263 | "if we think of him as being very remote from us, as a miraculum, does he not " | |
1264 | "aggrieve us (even Goethe, who was without envy, called Shakespeare his star of " | |
1265 | "the most distant heights [\"William! Stern der schonsten Ferne\": from Goethe's, " | |
1266 | "\"Between Two Worlds\"]; in regard to which one might recall the lines: \"the " | |
1267 | "stars, these we do not desire\" [from Goethe's, \"Comfort in Tears\"]). But, aside " | |
1268 | "from these suggestions of our vanity, the activity of the genius seems in no " | |
1269 | "way fundamentally different from the activity of the inventor of machines, the " | |
1270 | "scholar of astronomy or history, the master of tactics. All these activities " | |
1271 | "are explicable if one pictures to oneself people whose thinking is active in " | |
1272 | "one direction, who employ everything as material, who always zealously observe " | |
1273 | "their own inner life and that of others, who perceive everywhere models and " | |
1274 | "incentives, who never tire of combining together the means available to them. " | |
1275 | "Genius too does nothing except learn first how to lay bricks then how to build, " | |
1276 | "except continually seek for material and continually form itself around it. " | |
1277 | "Every activity of man is amazingly complicated, not only that of the genius: " | |
1278 | "but none is a \"miracle.\" Whence, then, the belief that genius exists only in " | |
1279 | "the artist, orator and philosopher? that only they have \"intuition\"? (Whereby " | |
1280 | "they are supposed to possess a kind of miraculous eyeglass with which they can " | |
1281 | "see directly into \"the essence of the thing\"!) It is clear that people speak of " | |
1282 | "genius only where the effects of the great intellect are most pleasant to them " | |
1283 | "and where they have no desire to feel envious. To call someone \"divine\" means: " | |
1284 | "\"here there is no need for us to compete.\" Then, everything finished and " | |
1285 | "complete is regarded with admiration, everything still becoming is undervalued. " | |
1286 | "But no one can see in the work of the artist how it has become; that is its " | |
1287 | "advantage, for wherever one can see the act of becoming one grows somewhat " | |
1288 | "cool. The finished and perfect art of representation repulses all thinking as " | |
1289 | "to how it has become; it tyrannizes as present completeness and perfection. " | |
1290 | "That is why the masters of the art of representation count above all as gifted " | |
1291 | "with genius and why men of science do not. In reality, this evaluation of the " | |
1292 | "former and undervaluation of the latter is only a piece of childishness in the " | |
1293 | "realm of reason. " | |
1294 | "\n\n" | |
1295 | "163 The serious workman. Do not talk about giftedness, inborn talents! One can " | |
1296 | "name great men of all kinds who were very little gifted. The acquired " | |
1297 | "greatness, became \"geniuses\" (as we put it), through qualities the lack of " | |
1298 | "which no one who knew what they were would boast of: they all possessed that " | |
1299 | "seriousness of the efficient workman which first learns to construct the parts " | |
1300 | "properly before it ventures to fashion a great whole; they allowed themselves " | |
1301 | "time for it, because they took more pleasure in making the little, secondary " | |
1302 | "things well than in the effect of a dazzling whole. the recipe for becoming a " | |
1303 | "good novelist, for example, is easy to give, but to carry it out presupposes " | |
1304 | "qualities one is accustomed to overlook when one says \"I do not have enough " | |
1305 | "talent.\" One has only to make a hundred or so sketches for novels, none longer " | |
1306 | "than two pages but of such distinctness that every word in them is necessary; " | |
1307 | "one should write down anecdotes each day until one has learned how to give them " | |
1308 | "the most pregnant and effective form; one should be tireless in collecting and " | |
1309 | "describing human types and characters; one should above all relate things to " | |
1310 | "others and listen to others relate, keeping one's eyes and ears open for the " | |
1311 | "effect produced on those present, one should travel like a landscape painter or " | |
1312 | "costume designer; one should excerpt for oneself out of the individual sciences " | |
1313 | "everything that will produce an artistic effect when it is well described, one " | |
1314 | "should, finally, reflect on the motives of human actions, disdain no signpost " | |
1315 | "to instruction about them and be a collector of these things by day and night. " | |
1316 | "One should continue in this many-sided exercise some ten years: what is then " | |
1317 | "created in the workshop, however, will be fit to go out into the world. What, " | |
1318 | "however, do most people do? They begin, not with the parts, but with the whole. " | |
1319 | "Perhaps they chance to strike a right note, excite attention and from then on " | |
1320 | "strike worse and worse notes, for good, natural reasons. Sometimes, when the " | |
1321 | "character and intellect needed to formulate such a life-plan are lacking, fate " | |
1322 | "and need take their place and lead the future master step by step through all " | |
1323 | "the stipulations of his trade. " | |
1324 | "\n\n" | |
1325 | "164 Peril and profit in the cult of the genius. The belief in great, superior, " | |
1326 | "fruitful spirits is not necessarily, yet nonetheless is very frequently " | |
1327 | "associated with that religious or semi-religious superstition that these " | |
1328 | "spirits are of supra-human origin and possess certain miraculous abilities by " | |
1329 | "virtue of which they acquire their knowledge by quite other means than the rest " | |
1330 | "of mankind. One ascribes to them, it seems, a direct view of the nature of the " | |
1331 | "world, as it were a hole in the cloak of appearance, and believes that, by " | |
1332 | "virtue of this miraculous seer's vision, they are able to communicate something " | |
1333 | "conclusive and decisive about man and the world without the toil and " | |
1334 | "rigorousness required by science. As long as there continue to be those who " | |
1335 | "believe in the miraculous in the domain of knowledge one can perhaps concede " | |
1336 | "that these people themselves derive some benefit from their belief, inasmuch as " | |
1337 | "through their unconditional subjection to the great spirits they create for " | |
1338 | "their own spirit during its time of development the finest form of discipline " | |
1339 | "and schooling. On the other hand, it is at least questionable whether the " | |
1340 | "superstitious belief in genius, in its privileges and special abilities, is of " | |
1341 | "benefit to the genius himself if it takes root in him. It is in any event a " | |
1342 | "dangerous sign when a man is assailed by awe of himself, whether it be the " | |
1343 | "celebrated Caesar's awe of Caesar or the awe of one's own genius now under " | |
1344 | "consideration; when the sacrificial incense which is properly rendered only to " | |
1345 | "a god penetrates the brain of the genius, so that his head begins to swim and " | |
1346 | "he comes to regard himself as something supra-human. The consequences that " | |
1347 | "slowly result are: the feeling of irresponsibility, of exceptional rights, the " | |
1348 | "belief that he confers a favor by his mere presence, insane rage when anyone " | |
1349 | "attempts even to compare him with others, let alone to rate him beneath them, " | |
1350 | "or to draw attention to lapses in his work. Because he ceases to practice " | |
1351 | "criticism of himself, at last one pinion after the other falls out of his " | |
1352 | "plumage: that superstitious eats at the roots of his powers and perhaps even " | |
1353 | "turns him into a hypocrite after his powers have fled from him. For the great " | |
1354 | "spirits themselves it is therefore probably more beneficial if they acquire an " | |
1355 | "insight into the nature and origin of their powers, if they grasp, that is to " | |
1356 | "say, what purely human qualities have come together in them and what fortunate " | |
1357 | "circumstances attended them: in the first place undiminished energy, resolute " | |
1358 | "application to individual goals, great personal courage, then the good fortune " | |
1359 | "to receive an upbringing which offered in the early years the finest teachers, " | |
1360 | "models and methods. To be sure, when their goal is the production of the " | |
1361 | "greatest possible effect, unclarity with regard to oneself and that " | |
1362 | "semi-insanity superadded to it has always achieved much; for what has been " | |
1363 | "admired and envied at all times has been that power in them by virtue of which " | |
1364 | "they render men will-less and sweep them away into the delusion that the " | |
1365 | "leaders they are following are supra-natural. Indeed, it elevates and inspires " | |
1366 | "men to believe that someone is in possession of supra-natural powers: to this " | |
1367 | "extent Plato was right to say [Plato: Phaedrus, 244a] that madness has brought " | |
1368 | "the greatest of blessings upon mankind. In rare individual cases this portion " | |
1369 | "of madness may, indeed, actually have been the means by which such a nature, " | |
1370 | "excessive in all directions, was held firmly together: in the life of " | |
1371 | "individuals, too, illusions that are in themselves poisons often play the role " | |
1372 | "of healers; yet, in the end, in the case of every \"genius\" who believes in his " | |
1373 | "own divinity the poison shows itself to the same degree as his \"genius\" grows " | |
1374 | "old: one may recall, for example, the case of Napoleon, whose nature certainly " | |
1375 | "grew into the mighty unity that sets him apart from all men of modern times " | |
1376 | "precisely through his belief in himself and his star and through the contempt " | |
1377 | "for men that flowed from it; until in the end, however, this same belief went " | |
1378 | "over into an almost insane fatalism, robbed him of his acuteness and swiftness " | |
1379 | "of perception, and became the cause of his destruction."; |