scroll.cpp 51 KB

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