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