vstest.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/vscroll/vstest.cpp
  3. // Purpose: VScroll wxWidgets sample
  4. // Author: Vadim Zeitlin
  5. // Modified by: Brad Anderson
  6. // Created: 04/01/98
  7. // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers (this file is usually all you
  22. // need because it includes almost all "standard" wxWidgets headers)
  23. #ifndef WX_PRECOMP
  24. #include "wx/wx.h"
  25. #include "wx/app.h"
  26. #include "wx/frame.h"
  27. #endif
  28. // we need to include the headers not included from wx/wx.h explicitly anyhow
  29. #include "wx/vscroll.h"
  30. // ----------------------------------------------------------------------------
  31. // resources
  32. // ----------------------------------------------------------------------------
  33. // the application icon (under Windows and OS/2 it is in resources)
  34. #ifndef wxHAS_IMAGES_IN_RESOURCES
  35. #include "../sample.xpm"
  36. #endif
  37. // ----------------------------------------------------------------------------
  38. // definitions
  39. // ----------------------------------------------------------------------------
  40. #define MAX_LINES 10000
  41. // ----------------------------------------------------------------------------
  42. // private classes
  43. // ----------------------------------------------------------------------------
  44. // Define a new application type, each program should derive a class from wxApp
  45. class VarScrollApp : public wxApp
  46. {
  47. public:
  48. // create our main window
  49. virtual bool OnInit();
  50. };
  51. // Define a new frame type: this is going to be our main frame
  52. class VarScrollFrame : public wxFrame
  53. {
  54. public:
  55. // ctor
  56. VarScrollFrame();
  57. // event handlers (these functions should _not_ be virtual)
  58. void OnQuit(wxCommandEvent& event);
  59. void OnModeVScroll(wxCommandEvent& event);
  60. void OnModeHScroll(wxCommandEvent& event);
  61. void OnModeHVScroll(wxCommandEvent& event);
  62. void OnAbout(wxCommandEvent& event);
  63. void OnSize(wxSizeEvent& event)
  64. {
  65. // show current size in the status bar
  66. #if wxUSE_STATUSBAR
  67. if ( m_frameStatusBar )
  68. {
  69. wxSize sz = GetClientSize();
  70. SetStatusText(wxString::Format(wxT("%dx%d"), sz.x, sz.y), 1);
  71. }
  72. #endif // wxUSE_STATUSBAR
  73. event.Skip();
  74. }
  75. private:
  76. // either a wxVScrolledWindow or a wxHVScrolled window, depending on current mode
  77. wxPanel *m_scrollWindow;
  78. // any class wishing to process wxWidgets events must use this macro
  79. wxDECLARE_EVENT_TABLE();
  80. };
  81. class VScrollWindow : public wxVScrolledWindow
  82. {
  83. public:
  84. VScrollWindow(wxFrame *frame) : wxVScrolledWindow(frame, wxID_ANY)
  85. {
  86. m_frame = frame;
  87. SetRowCount(MAX_LINES);
  88. int i;
  89. for ( i = 0; i < MAX_LINES; ++i )
  90. m_heights[i] = rand()%25+16; // low: 16; high: 40
  91. m_changed = true;
  92. }
  93. void OnIdle(wxIdleEvent&)
  94. {
  95. #if wxUSE_STATUSBAR
  96. m_frame->SetStatusText(wxString::Format
  97. (
  98. wxT("Page size = %d, pos = %d, max = %d"),
  99. GetScrollThumb(wxVERTICAL),
  100. GetScrollPos(wxVERTICAL),
  101. GetScrollRange(wxVERTICAL)
  102. ));
  103. #endif // wxUSE_STATUSBAR
  104. m_changed = false;
  105. }
  106. void OnPaint(wxPaintEvent&)
  107. {
  108. wxPaintDC dc(this);
  109. dc.SetPen(*wxBLACK_PEN);
  110. const size_t lineFirst = GetVisibleBegin(),
  111. lineLast = GetVisibleEnd();
  112. const wxCoord hText = dc.GetCharHeight();
  113. wxSize clientSize = GetClientSize();
  114. wxCoord y = 0;
  115. for ( size_t line = lineFirst; line < lineLast; line++ )
  116. {
  117. dc.DrawLine(0, y, clientSize.GetWidth(), y);
  118. wxCoord hLine = OnGetRowHeight(line);
  119. dc.DrawText(wxString::Format(wxT("Line %lu"), (unsigned long)line),
  120. 2, y + (hLine - hText) / 2);
  121. y += hLine;
  122. dc.DrawLine(0, y, 1000, y);
  123. }
  124. }
  125. void OnScroll(wxScrollWinEvent& event)
  126. {
  127. m_changed = true;
  128. event.Skip();
  129. }
  130. void OnMouse(wxMouseEvent& event)
  131. {
  132. if(event.LeftDown())
  133. CaptureMouse();
  134. else if(event.LeftUp())
  135. ReleaseMouse();
  136. event.Skip();
  137. }
  138. virtual wxCoord OnGetRowHeight(size_t n) const
  139. {
  140. wxASSERT( n < GetRowCount() );
  141. return m_heights[n];
  142. }
  143. private:
  144. wxFrame *m_frame;
  145. int m_heights[MAX_LINES];
  146. bool m_changed;
  147. wxDECLARE_EVENT_TABLE();
  148. };
  149. wxBEGIN_EVENT_TABLE(VScrollWindow, wxVScrolledWindow)
  150. EVT_IDLE(VScrollWindow::OnIdle)
  151. EVT_PAINT(VScrollWindow::OnPaint)
  152. EVT_SCROLLWIN(VScrollWindow::OnScroll)
  153. EVT_MOUSE_EVENTS(VScrollWindow::OnMouse)
  154. wxEND_EVENT_TABLE()
  155. class HScrollWindow : public wxHScrolledWindow
  156. {
  157. public:
  158. HScrollWindow(wxFrame *frame) : wxHScrolledWindow(frame, wxID_ANY)
  159. {
  160. m_frame = frame;
  161. SetColumnCount(MAX_LINES);
  162. int i;
  163. for ( i = 0; i < MAX_LINES; ++i )
  164. m_heights[i] = rand()%25+16; // low: 15; high: 40
  165. m_changed = true;
  166. }
  167. void OnIdle(wxIdleEvent&)
  168. {
  169. #if wxUSE_STATUSBAR
  170. m_frame->SetStatusText(wxString::Format
  171. (
  172. wxT("Page size = %d, pos = %d, max = %d"),
  173. GetScrollThumb(wxVERTICAL),
  174. GetScrollPos(wxVERTICAL),
  175. GetScrollRange(wxVERTICAL)
  176. ));
  177. #endif // wxUSE_STATUSBAR
  178. m_changed = false;
  179. }
  180. void OnPaint(wxPaintEvent&)
  181. {
  182. wxPaintDC dc(this);
  183. dc.SetPen(*wxBLACK_PEN);
  184. const size_t lineFirst = GetVisibleBegin(),
  185. lineLast = GetVisibleEnd();
  186. const wxCoord hText = dc.GetCharHeight();
  187. wxSize clientSize = GetClientSize();
  188. wxCoord x = 0;
  189. for ( size_t line = lineFirst; line < lineLast; line++ )
  190. {
  191. dc.DrawLine(x, 0, x, clientSize.GetHeight());
  192. wxCoord wLine = OnGetColumnWidth(line);
  193. dc.DrawRotatedText(wxString::Format(wxT("Line %lu"), (unsigned long)line),
  194. x + (wLine - hText) / 2, clientSize.GetHeight() - 5, 90);
  195. x += wLine;
  196. dc.DrawLine(x, 0, x, 1000);
  197. }
  198. }
  199. void OnScroll(wxScrollWinEvent& event)
  200. {
  201. m_changed = true;
  202. event.Skip();
  203. }
  204. void OnMouse(wxMouseEvent& event)
  205. {
  206. if(event.LeftDown())
  207. CaptureMouse();
  208. else if(event.LeftUp())
  209. ReleaseMouse();
  210. event.Skip();
  211. }
  212. virtual wxCoord OnGetColumnWidth(size_t n) const
  213. {
  214. wxASSERT( n < GetColumnCount() );
  215. return m_heights[n];
  216. }
  217. private:
  218. wxFrame *m_frame;
  219. int m_heights[MAX_LINES];
  220. bool m_changed;
  221. wxDECLARE_EVENT_TABLE();
  222. };
  223. wxBEGIN_EVENT_TABLE(HScrollWindow, wxHScrolledWindow)
  224. EVT_IDLE(HScrollWindow::OnIdle)
  225. EVT_PAINT(HScrollWindow::OnPaint)
  226. EVT_SCROLLWIN(HScrollWindow::OnScroll)
  227. EVT_MOUSE_EVENTS(HScrollWindow::OnMouse)
  228. wxEND_EVENT_TABLE()
  229. class HVScrollWindow : public wxHVScrolledWindow
  230. {
  231. public:
  232. HVScrollWindow(wxFrame *frame) : wxHVScrolledWindow(frame, wxID_ANY)
  233. {
  234. m_frame = frame;
  235. SetRowColumnCount(MAX_LINES, MAX_LINES);
  236. int i;
  237. for ( i = 0; i < MAX_LINES; ++i )
  238. {
  239. m_heights[i] = rand()%30+31; // low: 30; high: 60
  240. m_widths[i] = rand()%30+61; // low: 60; high: 90
  241. }
  242. m_changed = true;
  243. }
  244. void OnIdle(wxIdleEvent&)
  245. {
  246. #if wxUSE_STATUSBAR
  247. m_frame->SetStatusText(wxString::Format
  248. (
  249. wxT("Page size = %d rows %d columns; pos = row: %d, column: %d; max = %d rows, %d columns"),
  250. GetScrollThumb(wxVERTICAL),
  251. GetScrollThumb(wxHORIZONTAL),
  252. GetScrollPos(wxVERTICAL),
  253. GetScrollPos(wxHORIZONTAL),
  254. GetScrollRange(wxVERTICAL),
  255. GetScrollRange(wxHORIZONTAL)
  256. ));
  257. #endif // wxUSE_STATUSBAR
  258. m_changed = false;
  259. }
  260. void OnPaint(wxPaintEvent&)
  261. {
  262. wxPaintDC dc(this);
  263. dc.SetPen(*wxBLACK_PEN);
  264. const size_t rowFirst = GetVisibleRowsBegin(),
  265. rowLast = GetVisibleRowsEnd();
  266. const size_t columnFirst = GetVisibleColumnsBegin(),
  267. columnLast = GetVisibleColumnsEnd();
  268. const wxCoord hText = dc.GetCharHeight();
  269. wxSize clientSize = GetClientSize();
  270. wxCoord y = 0;
  271. wxCoord x = 0;
  272. for ( size_t row = rowFirst; row < rowLast; row++ )
  273. {
  274. wxCoord rowHeight = OnGetRowHeight(row);
  275. dc.DrawLine(0, y, clientSize.GetWidth(), y);
  276. x = 0;
  277. for ( size_t col = columnFirst; col < columnLast; col++ )
  278. {
  279. wxCoord colWidth = OnGetColumnWidth(col);
  280. if ( row == rowFirst )
  281. dc.DrawLine(x, 0, x, clientSize.GetHeight());
  282. dc.DrawText(wxString::Format(wxT("Row %lu"), (unsigned long)row),
  283. x + 2, y + rowHeight / 2 - hText);
  284. dc.DrawText(wxString::Format(wxT("Col %lu"), (unsigned long)col),
  285. x + 2, y + rowHeight / 2);
  286. x += colWidth;
  287. if ( row == rowFirst)
  288. dc.DrawLine(x, 0, x, clientSize.GetHeight());
  289. }
  290. y += rowHeight;
  291. dc.DrawLine(0, y, clientSize.GetWidth(), y);
  292. }
  293. }
  294. void OnScroll(wxScrollWinEvent& event)
  295. {
  296. m_changed = true;
  297. event.Skip();
  298. }
  299. void OnMouse(wxMouseEvent& event)
  300. {
  301. if(event.LeftDown())
  302. CaptureMouse();
  303. else if(event.LeftUp())
  304. ReleaseMouse();
  305. event.Skip();
  306. }
  307. virtual wxCoord OnGetRowHeight(size_t n) const
  308. {
  309. wxASSERT( n < GetRowCount() );
  310. return m_heights[n];
  311. }
  312. virtual wxCoord OnGetColumnWidth(size_t n) const
  313. {
  314. wxASSERT( n < GetColumnCount() );
  315. return m_widths[n];
  316. }
  317. private:
  318. wxFrame *m_frame;
  319. int m_heights[MAX_LINES];
  320. int m_widths[MAX_LINES];
  321. bool m_changed;
  322. wxDECLARE_EVENT_TABLE();
  323. };
  324. wxBEGIN_EVENT_TABLE(HVScrollWindow, wxHVScrolledWindow)
  325. EVT_IDLE(HVScrollWindow::OnIdle)
  326. EVT_PAINT(HVScrollWindow::OnPaint)
  327. EVT_SCROLLWIN(HVScrollWindow::OnScroll)
  328. EVT_MOUSE_EVENTS(HVScrollWindow::OnMouse)
  329. wxEND_EVENT_TABLE()
  330. // ----------------------------------------------------------------------------
  331. // constants
  332. // ----------------------------------------------------------------------------
  333. // IDs for the controls and the menu commands
  334. enum
  335. {
  336. // menu items
  337. VScroll_Quit = wxID_EXIT,
  338. // it is important for the id corresponding to the "About" command to have
  339. // this standard value as otherwise it won't be handled properly under Mac
  340. // (where it is special and put into the "Apple" menu)
  341. VScroll_About = wxID_ABOUT,
  342. VScroll_VScrollMode = wxID_HIGHEST + 1,
  343. VScroll_HScrollMode,
  344. VScroll_HVScrollMode
  345. };
  346. // ----------------------------------------------------------------------------
  347. // event tables and other macros for wxWidgets
  348. // ----------------------------------------------------------------------------
  349. // the event tables connect the wxWidgets events with the functions (event
  350. // handlers) which process them. It can be also done at run-time, but for the
  351. // simple menu events like this the static method is much simpler.
  352. wxBEGIN_EVENT_TABLE(VarScrollFrame, wxFrame)
  353. EVT_MENU(VScroll_Quit, VarScrollFrame::OnQuit)
  354. EVT_MENU(VScroll_VScrollMode, VarScrollFrame::OnModeVScroll)
  355. EVT_MENU(VScroll_HScrollMode, VarScrollFrame::OnModeHScroll)
  356. EVT_MENU(VScroll_HVScrollMode, VarScrollFrame::OnModeHVScroll)
  357. EVT_MENU(VScroll_About, VarScrollFrame::OnAbout)
  358. EVT_SIZE(VarScrollFrame::OnSize)
  359. wxEND_EVENT_TABLE()
  360. // Create a new application object: this macro will allow wxWidgets to create
  361. // the application object during program execution (it's better than using a
  362. // static object for many reasons) and also declares the accessor function
  363. // wxGetApp() which will return the reference of the right type (i.e. VarScrollApp and
  364. // not wxApp)
  365. IMPLEMENT_APP(VarScrollApp)
  366. // ============================================================================
  367. // implementation
  368. // ============================================================================
  369. // ----------------------------------------------------------------------------
  370. // the application class
  371. // ----------------------------------------------------------------------------
  372. // 'Main program' equivalent: the program execution "starts" here
  373. bool VarScrollApp::OnInit()
  374. {
  375. if ( !wxApp::OnInit() )
  376. return false;
  377. // create the main application window
  378. VarScrollFrame *frame = new VarScrollFrame;
  379. // and show it (the frames, unlike simple controls, are not shown when
  380. // created initially)
  381. frame->Show(true);
  382. // ok
  383. return true;
  384. }
  385. // ----------------------------------------------------------------------------
  386. // main frame
  387. // ----------------------------------------------------------------------------
  388. // frame constructor
  389. VarScrollFrame::VarScrollFrame()
  390. : wxFrame(NULL,
  391. wxID_ANY,
  392. wxT("VScroll wxWidgets Sample"),
  393. wxDefaultPosition,
  394. wxSize(400, 350)),
  395. m_scrollWindow(NULL)
  396. {
  397. // set the frame icon
  398. SetIcon(wxICON(sample));
  399. #if wxUSE_MENUS
  400. // create a menu bar
  401. wxMenu *menuFile = new wxMenu;
  402. wxMenu *menuMode = new wxMenu;
  403. // the "About" item should be in the help menu
  404. wxMenu *menuHelp = new wxMenu;
  405. menuHelp->Append(VScroll_About, wxT("&About\tF1"), wxT("Show about dialog"));
  406. #ifdef wxHAS_RADIO_MENU_ITEMS
  407. menuMode->AppendRadioItem(VScroll_VScrollMode, wxT("&Vertical\tAlt-V"),
  408. wxT("Vertical scrolling only"));
  409. menuMode->AppendRadioItem(VScroll_HScrollMode, wxT("&Horizontal\tAlt-H"),
  410. wxT("Horizontal scrolling only"));
  411. menuMode->AppendRadioItem(VScroll_HVScrollMode,
  412. wxT("Hori&zontal/Vertical\tAlt-Z"),
  413. wxT("Horizontal and vertical scrolling"));
  414. menuMode->Check(VScroll_VScrollMode, true);
  415. #else
  416. menuMode->Append(VScroll_VScrollMode, wxT("&Vertical\tAlt-V"),
  417. wxT("Vertical scrolling only"));
  418. menuMode->Append(VScroll_HScrollMode, wxT("&Horizontal\tAlt-H"),
  419. wxT("Horizontal scrolling only"));
  420. menuMode->Append(VScroll_HVScrollMode, wxT("Hori&zontal/Vertical\tAlt-Z"),
  421. wxT("Horizontal and vertical scrolling"));
  422. #endif
  423. menuFile->Append(VScroll_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
  424. // now append the freshly created menu to the menu bar...
  425. wxMenuBar *menuBar = new wxMenuBar;
  426. menuBar->Append(menuFile, wxT("&File"));
  427. menuBar->Append(menuMode, wxT("&Mode"));
  428. menuBar->Append(menuHelp, wxT("&Help"));
  429. // ... and attach this menu bar to the frame
  430. SetMenuBar(menuBar);
  431. #endif // wxUSE_MENUS
  432. #if wxUSE_STATUSBAR
  433. // create a status bar just for fun (by default with 1 pane only)
  434. CreateStatusBar(2);
  435. SetStatusText(wxT("Welcome to wxWidgets!"));
  436. int widths[2];
  437. widths[0] = -1;
  438. widths[1] = 100;
  439. SetStatusWidths(2, widths);
  440. #endif // wxUSE_STATUSBAR
  441. // create our one and only child -- it will take our entire client area
  442. if ( menuMode->IsChecked(VScroll_VScrollMode) )
  443. m_scrollWindow = new VScrollWindow(this);
  444. else if ( menuMode->IsChecked(VScroll_HScrollMode) )
  445. m_scrollWindow = new HScrollWindow(this);
  446. else
  447. m_scrollWindow = new HVScrollWindow(this);
  448. }
  449. // ----------------------------------------------------------------------------
  450. // event handlers
  451. // ----------------------------------------------------------------------------
  452. void VarScrollFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  453. {
  454. // true is to force the frame to close
  455. Close(true);
  456. }
  457. void VarScrollFrame::OnModeVScroll(wxCommandEvent& WXUNUSED(event))
  458. {
  459. if ( m_scrollWindow )
  460. m_scrollWindow->Destroy();
  461. m_scrollWindow = new VScrollWindow(this);
  462. SendSizeEvent();
  463. }
  464. void VarScrollFrame::OnModeHScroll(wxCommandEvent& WXUNUSED(event))
  465. {
  466. if ( m_scrollWindow )
  467. m_scrollWindow->Destroy();
  468. m_scrollWindow = new HScrollWindow(this);
  469. SendSizeEvent();
  470. }
  471. void VarScrollFrame::OnModeHVScroll(wxCommandEvent& WXUNUSED(event))
  472. {
  473. if ( m_scrollWindow )
  474. m_scrollWindow->Destroy();
  475. m_scrollWindow = new HVScrollWindow(this);
  476. SendSizeEvent();
  477. }
  478. void VarScrollFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  479. {
  480. wxMessageBox(wxT("VScroll shows how to implement scrolling with\n")
  481. wxT("variable line widths and heights.\n")
  482. wxT("(c) 2003 Vadim Zeitlin"),
  483. wxT("About VScroll"),
  484. wxOK | wxICON_INFORMATION,
  485. this);
  486. }