]>
Commit | Line | Data |
---|---|---|
fdd3ed7a RR |
1 | /* |
2 | * Program: scroll | |
3 | * | |
4 | * Author: Robert Roebling | |
5 | * | |
6 | * Copyright: (C) 1998, Robert Roebling | |
2b5f62a0 | 7 | * 2002, Ron Lee |
fdd3ed7a RR |
8 | * |
9 | */ | |
10 | ||
11 | // For compilers that support precompilation, includes "wx/wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #ifndef WX_PRECOMP | |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/image.h" | |
053f9cc1 | 23 | #include "wx/listctrl.h" |
ed673c6a RR |
24 | #include "wx/sizer.h" |
25 | #include "wx/log.h" | |
26 | ||
fdd3ed7a RR |
27 | |
28 | // derived classes | |
29 | ||
30 | class MyFrame; | |
31 | class MyApp; | |
32 | ||
33 | // MyCanvas | |
34 | ||
35 | class MyCanvas: public wxScrolledWindow | |
36 | { | |
37 | public: | |
8a73bf3d | 38 | MyCanvas() {} |
fdd3ed7a | 39 | MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); |
925e9792 | 40 | ~MyCanvas(){}; |
fdd3ed7a | 41 | void OnPaint( wxPaintEvent &event ); |
307f16e8 | 42 | void OnQueryPosition( wxCommandEvent &event ); |
ed673c6a RR |
43 | void OnAddButton( wxCommandEvent &event ); |
44 | void OnDeleteButton( wxCommandEvent &event ); | |
45 | void OnMoveButton( wxCommandEvent &event ); | |
46 | void OnScrollWin( wxCommandEvent &event ); | |
bf0c00c6 | 47 | void OnMouseDown( wxMouseEvent &event ); |
ed673c6a RR |
48 | |
49 | wxButton *m_button; | |
fdd3ed7a RR |
50 | |
51 | DECLARE_DYNAMIC_CLASS(MyCanvas) | |
52 | DECLARE_EVENT_TABLE() | |
53 | }; | |
54 | ||
2b5f62a0 VZ |
55 | |
56 | // ---------------------------------------------------------------------------- | |
57 | // Autoscrolling example. | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | // this class uses the 'virtual' size attribute along with an internal | |
61 | // sizer to automatically set up scrollbars as needed | |
62 | ||
63 | class MyAutoScrollWindow : public wxScrolledWindow | |
64 | { | |
65 | private: | |
66 | ||
67 | wxButton *m_button; | |
68 | ||
69 | public: | |
70 | ||
71 | MyAutoScrollWindow( wxWindow *parent ); | |
72 | ||
73 | void OnResizeClick( wxCommandEvent &WXUNUSED( event ) ); | |
74 | ||
75 | DECLARE_EVENT_TABLE() | |
76 | }; | |
77 | ||
78 | ||
8a73bf3d VZ |
79 | // ---------------------------------------------------------------------------- |
80 | // MyScrolledWindow classes: examples of wxScrolledWindow usage | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | // base class for both of them | |
84 | class MyScrolledWindowBase : public wxScrolledWindow | |
85 | { | |
86 | public: | |
2b5f62a0 VZ |
87 | MyScrolledWindowBase(wxWindow *parent) |
88 | : wxScrolledWindow(parent) | |
89 | , m_nLines( 100 ) | |
8a73bf3d | 90 | { |
2b5f62a0 VZ |
91 | wxClientDC dc(this); |
92 | dc.GetTextExtent(_T("Line 17"), NULL, &m_hLine); | |
8a73bf3d VZ |
93 | } |
94 | ||
95 | protected: | |
8a73bf3d VZ |
96 | // the height of one line on screen |
97 | wxCoord m_hLine; | |
98 | ||
99 | // the number of lines we draw | |
100 | size_t m_nLines; | |
101 | }; | |
102 | ||
103 | // this class does "stupid" redrawing - it redraws everything each time | |
2b5f62a0 VZ |
104 | // and sets the scrollbar extent directly. |
105 | ||
8a73bf3d VZ |
106 | class MyScrolledWindowDumb : public MyScrolledWindowBase |
107 | { | |
108 | public: | |
2b5f62a0 VZ |
109 | MyScrolledWindowDumb(wxWindow *parent) : MyScrolledWindowBase(parent) |
110 | { | |
111 | // no horz scrolling | |
b62ca03d | 112 | SetScrollbars(0, m_hLine, 0, m_nLines + 1, 0, 0, true /* no refresh */); |
2b5f62a0 | 113 | } |
8a73bf3d VZ |
114 | |
115 | virtual void OnDraw(wxDC& dc); | |
116 | }; | |
117 | ||
118 | // this class does "smart" redrawing - only redraws the lines which must be | |
2b5f62a0 VZ |
119 | // redrawn and sets the scroll rate and virtual size to affect the |
120 | // scrollbars. | |
121 | // | |
122 | // Note that this class should produce identical results to the one above. | |
123 | ||
8a73bf3d VZ |
124 | class MyScrolledWindowSmart : public MyScrolledWindowBase |
125 | { | |
126 | public: | |
2b5f62a0 VZ |
127 | MyScrolledWindowSmart(wxWindow *parent) : MyScrolledWindowBase(parent) |
128 | { | |
129 | // no horz scrolling | |
130 | SetScrollRate( 0, m_hLine ); | |
131 | SetVirtualSize( -1, ( m_nLines + 1 ) * m_hLine ); | |
132 | } | |
8a73bf3d VZ |
133 | |
134 | virtual void OnDraw(wxDC& dc); | |
135 | }; | |
136 | ||
2b5f62a0 | 137 | |
8a73bf3d | 138 | // ---------------------------------------------------------------------------- |
fdd3ed7a | 139 | // MyFrame |
8a73bf3d | 140 | // ---------------------------------------------------------------------------- |
fdd3ed7a RR |
141 | |
142 | class MyFrame: public wxFrame | |
143 | { | |
144 | public: | |
145 | MyFrame(); | |
146 | ||
147 | void OnAbout( wxCommandEvent &event ); | |
148 | void OnQuit( wxCommandEvent &event ); | |
8e217128 RR |
149 | void OnDeleteAll( wxCommandEvent &event ); |
150 | void OnInsertNew( wxCommandEvent &event ); | |
fdd3ed7a RR |
151 | |
152 | MyCanvas *m_canvas; | |
ed673c6a | 153 | wxTextCtrl *m_log; |
fdd3ed7a RR |
154 | |
155 | DECLARE_DYNAMIC_CLASS(MyFrame) | |
156 | DECLARE_EVENT_TABLE() | |
157 | }; | |
158 | ||
159 | // MyApp | |
160 | ||
161 | class MyApp: public wxApp | |
162 | { | |
163 | public: | |
164 | virtual bool OnInit(); | |
165 | }; | |
166 | ||
167 | // main program | |
168 | ||
169 | IMPLEMENT_APP(MyApp) | |
170 | ||
ed673c6a RR |
171 | // ids |
172 | ||
2b5f62a0 VZ |
173 | const long ID_ADDBUTTON = wxNewId(); |
174 | const long ID_DELBUTTON = wxNewId(); | |
175 | const long ID_MOVEBUTTON = wxNewId(); | |
176 | const long ID_SCROLLWIN = wxNewId(); | |
177 | const long ID_QUERYPOS = wxNewId(); | |
ed673c6a | 178 | |
2b5f62a0 | 179 | const long ID_NEWBUTTON = wxNewId(); |
ed673c6a | 180 | |
fdd3ed7a RR |
181 | // MyCanvas |
182 | ||
183 | IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) | |
184 | ||
185 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
307f16e8 | 186 | EVT_PAINT( MyCanvas::OnPaint) |
2b5f62a0 | 187 | EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown) |
307f16e8 | 188 | EVT_BUTTON( ID_QUERYPOS, MyCanvas::OnQueryPosition) |
ed673c6a RR |
189 | EVT_BUTTON( ID_ADDBUTTON, MyCanvas::OnAddButton) |
190 | EVT_BUTTON( ID_DELBUTTON, MyCanvas::OnDeleteButton) | |
191 | EVT_BUTTON( ID_MOVEBUTTON, MyCanvas::OnMoveButton) | |
192 | EVT_BUTTON( ID_SCROLLWIN, MyCanvas::OnScrollWin) | |
fdd3ed7a RR |
193 | END_EVENT_TABLE() |
194 | ||
195 | MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, | |
196 | const wxPoint &pos, const wxSize &size ) | |
2b5f62a0 | 197 | : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER | wxTAB_TRAVERSAL, _T("test canvas") ) |
fdd3ed7a | 198 | { |
2b5f62a0 VZ |
199 | SetScrollRate( 10, 10 ); |
200 | SetVirtualSize( 500, 1000 ); | |
201 | ||
202 | (void) new wxButton( this, ID_ADDBUTTON, _T("add button"), wxPoint(10,10) ); | |
203 | (void) new wxButton( this, ID_DELBUTTON, _T("del button"), wxPoint(10,40) ); | |
204 | (void) new wxButton( this, ID_MOVEBUTTON, _T("move button"), wxPoint(150,10) ); | |
205 | (void) new wxButton( this, ID_SCROLLWIN, _T("scroll win"), wxPoint(250,10) ); | |
ed673c6a | 206 | |
aa06a8fd DW |
207 | #if 0 |
208 | ||
fdd3ed7a RR |
209 | wxString choices[] = |
210 | { | |
211 | "This", | |
212 | "is one of my", | |
213 | "really", | |
214 | "wonderful", | |
215 | "examples." | |
216 | }; | |
aa06a8fd | 217 | |
307f16e8 | 218 | m_button = new wxButton( this, ID_QUERYPOS, "Query position", wxPoint(10,110) ); |
aa06a8fd | 219 | |
422d0ff0 | 220 | (void) new wxTextCtrl( this, wxID_ANY, "wxTextCtrl", wxPoint(10,150), wxSize(80,wxDefaultCoord) ); |
aa06a8fd | 221 | |
b62ca03d | 222 | (void) new wxRadioButton( this, wxID_ANY, "Disable", wxPoint(10,190) ); |
aa06a8fd | 223 | |
b62ca03d | 224 | (void) new wxComboBox( this, wxID_ANY, "This", wxPoint(10,230), wxDefaultSize, 5, choices ); |
aa06a8fd | 225 | |
b62ca03d | 226 | (void) new wxRadioBox( this, wxID_ANY, "This", wxPoint(10,310), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_COLS ); |
053f9cc1 | 227 | |
b62ca03d | 228 | (void) new wxRadioBox( this, wxID_ANY, "This", wxPoint(10,440), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_ROWS ); |
e9158f7d | 229 | |
ed673c6a | 230 | wxListCtrl *m_listCtrl = new wxListCtrl( |
b62ca03d | 231 | this, wxID_ANY, wxPoint(200, 110), wxSize(180, 120), |
8a73bf3d | 232 | wxLC_REPORT | wxSIMPLE_BORDER | wxLC_SINGLE_SEL ); |
053f9cc1 | 233 | |
ed673c6a RR |
234 | m_listCtrl->InsertColumn(0, "First", wxLIST_FORMAT_LEFT, 90); |
235 | m_listCtrl->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT, 90); | |
053f9cc1 | 236 | |
ed673c6a RR |
237 | for ( int i=0; i < 30; i++) |
238 | { | |
239 | char buf[20]; | |
240 | sprintf(buf, "Item %d", i); | |
241 | m_listCtrl->InsertItem(i, buf); | |
242 | } | |
243 | m_listCtrl->SetItemState( 3, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
053f9cc1 | 244 | |
b62ca03d | 245 | (void) new wxListBox( this, wxID_ANY, wxPoint(260,280), wxSize(120,120), 5, choices, wxLB_ALWAYS_SB ); |
5e014a0c | 246 | |
aa06a8fd DW |
247 | #endif |
248 | ||
b62ca03d | 249 | wxPanel *test = new wxPanel( this, wxID_ANY, wxPoint(10, 110), wxSize(130,50), wxSIMPLE_BORDER | wxTAB_TRAVERSAL ); |
a60b1f5d | 250 | test->SetBackgroundColour( wxT("WHEAT") ); |
aa06a8fd DW |
251 | |
252 | #if 0 | |
253 | ||
b62ca03d | 254 | wxButton *test2 = new wxButton( test, wxID_ANY, "Hallo", wxPoint(10,10) ); |
aa06a8fd | 255 | |
b62ca03d | 256 | test = new wxPanel( this, wxID_ANY, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER | wxTAB_TRAVERSAL ); |
a60b1f5d | 257 | test->SetBackgroundColour( wxT("WHEAT") ); |
ed673c6a | 258 | test->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) ); |
b62ca03d | 259 | test2 = new wxButton( test, wxID_ANY, "Hallo", wxPoint(10,10) ); |
ed673c6a | 260 | test2->SetCursor( wxCursor( wxCURSOR_PENCIL ) ); |
aa06a8fd | 261 | |
b62ca03d | 262 | test = new wxPanel( this, wxID_ANY, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER | wxTAB_TRAVERSAL ); |
a60b1f5d | 263 | test->SetBackgroundColour( wxT("WHEAT") ); |
ed673c6a | 264 | test->SetCursor( wxCursor( wxCURSOR_PENCIL ) ); |
b62ca03d | 265 | test2 = new wxButton( test, wxID_ANY, "Hallo", wxPoint(10,10) ); |
ed673c6a | 266 | test2->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) ); |
5e014a0c | 267 | |
aa06a8fd DW |
268 | #endif |
269 | ||
a60b1f5d | 270 | SetBackgroundColour( wxT("BLUE") ); |
aa06a8fd | 271 | |
ed673c6a | 272 | SetCursor( wxCursor( wxCURSOR_IBEAM ) ); |
fdd3ed7a RR |
273 | } |
274 | ||
bf0c00c6 RR |
275 | void MyCanvas::OnMouseDown( wxMouseEvent &event ) |
276 | { | |
a3e7d24d RR |
277 | if (event.LeftDown()) |
278 | { | |
279 | wxPoint pt( event.GetPosition() ); | |
280 | int x,y; | |
281 | CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); | |
4693b20c | 282 | wxLogMessage( wxT("Mouse down event at: %d %d, scrolled: %d %d"), pt.x, pt.y, x, y ); |
f6bcfd97 BP |
283 | |
284 | if ( !event.LeftIsDown() ) | |
b62ca03d | 285 | wxLogMessage( wxT("Error: LeftIsDown() should be true if for LeftDown()") ); |
a3e7d24d | 286 | } |
bf0c00c6 RR |
287 | } |
288 | ||
289 | void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) | |
290 | { | |
291 | wxPaintDC dc( this ); | |
292 | PrepareDC( dc ); | |
293 | ||
2b5f62a0 | 294 | dc.DrawText( _T("Press mouse button to test calculations!"), 160, 50 ); |
bf0c00c6 | 295 | |
2b5f62a0 | 296 | dc.DrawText( _T("Some text"), 140, 140 ); |
aa06a8fd | 297 | |
bf0c00c6 RR |
298 | dc.DrawRectangle( 100, 160, 200, 200 ); |
299 | } | |
300 | ||
307f16e8 RR |
301 | void MyCanvas::OnQueryPosition( wxCommandEvent &WXUNUSED(event) ) |
302 | { | |
303 | wxPoint pt( m_button->GetPosition() ); | |
4693b20c | 304 | wxLogMessage( wxT("Position of \"Query position\" is %d %d"), pt.x, pt.y ); |
bf0c00c6 | 305 | pt = ClientToScreen( pt ); |
4693b20c | 306 | wxLogMessage( wxT("Position of \"Query position\" on screen is %d %d"), pt.x, pt.y ); |
307f16e8 RR |
307 | } |
308 | ||
ed673c6a RR |
309 | void MyCanvas::OnAddButton( wxCommandEvent &WXUNUSED(event) ) |
310 | { | |
4693b20c | 311 | wxLogMessage( wxT("Inserting button at position 10,70...") ); |
2b5f62a0 | 312 | wxButton *button = new wxButton( this, ID_NEWBUTTON, wxT("new button"), wxPoint(10,70), wxSize(80,25) ); |
bf0c00c6 | 313 | wxPoint pt( button->GetPosition() ); |
4693b20c | 314 | wxLogMessage( wxT("-> Position after inserting %d %d"), pt.x, pt.y ); |
ed673c6a RR |
315 | } |
316 | ||
256b8649 | 317 | void MyCanvas::OnDeleteButton( wxCommandEvent &WXUNUSED(event) ) |
ed673c6a | 318 | { |
4693b20c | 319 | wxLogMessage( wxT("Deleting button inserted with \"Add button\"...") ); |
ed673c6a RR |
320 | wxWindow *win = FindWindow( ID_NEWBUTTON ); |
321 | if (win) | |
322 | win->Destroy(); | |
323 | else | |
4693b20c | 324 | wxLogMessage( wxT("-> No window with id = ID_NEWBUTTON found.") ); |
ed673c6a RR |
325 | } |
326 | ||
327 | void MyCanvas::OnMoveButton( wxCommandEvent &event ) | |
328 | { | |
4693b20c | 329 | wxLogMessage( wxT("Moving button 10 pixels downward..") ); |
ed673c6a | 330 | wxWindow *win = FindWindow( event.GetId() ); |
bf0c00c6 | 331 | wxPoint pt( win->GetPosition() ); |
4693b20c | 332 | wxLogMessage( wxT("-> Position before move is %d %d"), pt.x, pt.y ); |
422d0ff0 | 333 | win->Move( wxDefaultCoord, pt.y + 10 ); |
bf0c00c6 | 334 | pt = win->GetPosition(); |
4693b20c | 335 | wxLogMessage( wxT("-> Position after move is %d %d"), pt.x, pt.y ); |
ed673c6a RR |
336 | } |
337 | ||
338 | void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) ) | |
339 | { | |
4693b20c | 340 | wxLogMessage( wxT("Scrolling 2 units up.\nThe white square and the controls should move equally!") ); |
ed673c6a | 341 | int x,y; |
8073eb40 | 342 | GetViewStart( &x, &y ); |
ed673c6a RR |
343 | Scroll( -1, y+2 ); |
344 | } | |
345 | ||
2b5f62a0 VZ |
346 | // MyAutoScrollWindow |
347 | ||
348 | const long ID_RESIZEBUTTON = wxNewId(); | |
349 | const wxSize SMALL_BUTTON( 100, 50 ); | |
350 | const wxSize LARGE_BUTTON( 300, 100 ); | |
351 | ||
352 | BEGIN_EVENT_TABLE( MyAutoScrollWindow, wxScrolledWindow) | |
353 | EVT_BUTTON( ID_RESIZEBUTTON, MyAutoScrollWindow::OnResizeClick) | |
354 | END_EVENT_TABLE() | |
355 | ||
356 | MyAutoScrollWindow::MyAutoScrollWindow( wxWindow *parent ) | |
357 | : wxScrolledWindow( parent ) | |
358 | { | |
359 | SetBackgroundColour( wxT("GREEN") ); | |
360 | ||
361 | // Set the rate we'd like for scrolling. | |
362 | ||
363 | SetScrollRate( 5, 5 ); | |
364 | ||
365 | // Populate a sizer with a 'resizing' button and some | |
366 | // other static decoration | |
367 | ||
368 | wxFlexGridSizer *innersizer = new wxFlexGridSizer( 2, 2 ); | |
369 | ||
370 | m_button = new wxButton( this, | |
371 | ID_RESIZEBUTTON, | |
372 | _T("Press me"), | |
373 | wxDefaultPosition, | |
374 | SMALL_BUTTON ); | |
375 | ||
376 | // We need to do this here, because wxADJUST_MINSIZE below | |
377 | // will cause the initial size to be ignored for Best/Min size. | |
378 | // It would be nice to fix the sizers to handle this a little | |
379 | // more cleanly. | |
380 | ||
381 | m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() ); | |
382 | ||
383 | innersizer->Add( m_button, | |
384 | 0, | |
385 | wxALIGN_CENTER | wxALL | wxADJUST_MINSIZE, | |
386 | 20 ); | |
387 | ||
b62ca03d | 388 | innersizer->Add( new wxStaticText( this, wxID_ANY, _T("This is just") ), |
2b5f62a0 VZ |
389 | 0, |
390 | wxALIGN_CENTER ); | |
391 | ||
b62ca03d | 392 | innersizer->Add( new wxStaticText( this, wxID_ANY, _T("some decoration") ), |
2b5f62a0 VZ |
393 | 0, |
394 | wxALIGN_CENTER ); | |
395 | ||
b62ca03d | 396 | innersizer->Add( new wxStaticText( this, wxID_ANY, _T("for you to scroll...") ), |
2b5f62a0 VZ |
397 | 0, |
398 | wxALIGN_CENTER ); | |
399 | ||
400 | // Then use the sizer to set the scrolled region size. | |
401 | ||
402 | SetSizer( innersizer ); | |
403 | } | |
404 | ||
405 | void MyAutoScrollWindow::OnResizeClick( wxCommandEvent &WXUNUSED( event ) ) | |
406 | { | |
407 | // Arbitrarily resize the button to change the minimum size of | |
408 | // the (scrolled) sizer. | |
409 | ||
410 | if( m_button->GetSize() == SMALL_BUTTON ) | |
411 | m_button->SetSizeHints( LARGE_BUTTON.GetWidth(), LARGE_BUTTON.GetHeight() ); | |
412 | else | |
413 | m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() ); | |
414 | ||
415 | // Force update layout and scrollbars, since nothing we do here | |
416 | // necessarily generates a size event which would do it for us. | |
417 | ||
418 | FitInside(); | |
419 | } | |
420 | ||
fdd3ed7a RR |
421 | // MyFrame |
422 | ||
2b5f62a0 VZ |
423 | const long ID_QUIT = wxNewId(); |
424 | const long ID_ABOUT = wxNewId(); | |
425 | const long ID_DELETE_ALL = wxNewId(); | |
426 | const long ID_INSERT_NEW = wxNewId(); | |
fdd3ed7a RR |
427 | |
428 | IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) | |
429 | ||
430 | BEGIN_EVENT_TABLE(MyFrame,wxFrame) | |
8e217128 RR |
431 | EVT_MENU (ID_DELETE_ALL, MyFrame::OnDeleteAll) |
432 | EVT_MENU (ID_INSERT_NEW, MyFrame::OnInsertNew) | |
fdd3ed7a RR |
433 | EVT_MENU (ID_ABOUT, MyFrame::OnAbout) |
434 | EVT_MENU (ID_QUIT, MyFrame::OnQuit) | |
435 | END_EVENT_TABLE() | |
436 | ||
437 | MyFrame::MyFrame() | |
b62ca03d | 438 | : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"), |
ed673c6a | 439 | wxPoint(20,20), wxSize(470,500) ) |
fdd3ed7a | 440 | { |
ed673c6a | 441 | wxMenu *file_menu = new wxMenu(); |
2b5f62a0 VZ |
442 | file_menu->Append( ID_DELETE_ALL, _T("Delete all")); |
443 | file_menu->Append( ID_INSERT_NEW, _T("Insert new")); | |
444 | file_menu->Append( ID_ABOUT, _T("&About..")); | |
445 | file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X")); | |
fdd3ed7a | 446 | |
ed673c6a | 447 | wxMenuBar *menu_bar = new wxMenuBar(); |
2b5f62a0 | 448 | menu_bar->Append(file_menu, _T("&File")); |
fdd3ed7a | 449 | |
ed673c6a | 450 | SetMenuBar( menu_bar ); |
fdd3ed7a | 451 | |
8520f137 | 452 | #if wxUSE_STATUSBAR |
ed673c6a RR |
453 | CreateStatusBar(2); |
454 | int widths[] = { -1, 100 }; | |
455 | SetStatusWidths( 2, widths ); | |
8520f137 | 456 | #endif // wxUSE_STATUSBAR |
fdd3ed7a | 457 | |
2b5f62a0 VZ |
458 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
459 | ||
460 | // Setting an explicit size here is superfluous, it will be overridden | |
461 | // by the sizer in any case. | |
b62ca03d | 462 | m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(100,100) ); |
8a73bf3d | 463 | |
2b5f62a0 VZ |
464 | // This is done with ScrollRate/VirtualSize in MyCanvas ctor now, |
465 | // both should produce identical results. | |
466 | //m_canvas->SetScrollbars( 10, 10, 50, 100 ); | |
aa06a8fd | 467 | |
ed673c6a | 468 | topsizer->Add( m_canvas, 1, wxEXPAND ); |
2b5f62a0 | 469 | topsizer->Add( new MyAutoScrollWindow( this ), 1, wxEXPAND ); |
8a73bf3d VZ |
470 | |
471 | wxSizer *sizerBtm = new wxBoxSizer(wxHORIZONTAL); | |
472 | sizerBtm->Add( new MyScrolledWindowDumb(this), 1, wxEXPAND ); | |
473 | sizerBtm->Add( new MyScrolledWindowSmart(this), 1, wxEXPAND ); | |
474 | topsizer->Add( sizerBtm, 1, wxEXPAND ); | |
ed673c6a | 475 | |
ed673c6a | 476 | SetSizer( topsizer ); |
fdd3ed7a RR |
477 | } |
478 | ||
8e217128 RR |
479 | void MyFrame::OnDeleteAll( wxCommandEvent &WXUNUSED(event) ) |
480 | { | |
481 | m_canvas->DestroyChildren(); | |
482 | } | |
483 | ||
484 | void MyFrame::OnInsertNew( wxCommandEvent &WXUNUSED(event) ) | |
485 | { | |
b62ca03d | 486 | (void)new wxButton( m_canvas, wxID_ANY, _T("Hello"), wxPoint(100,100) ); |
8e217128 RR |
487 | } |
488 | ||
fdd3ed7a RR |
489 | void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) |
490 | { | |
b62ca03d | 491 | Close( true ); |
fdd3ed7a RR |
492 | } |
493 | ||
494 | void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) | |
495 | { | |
b62ca03d WS |
496 | (void)wxMessageBox( _T("wxScroll demo\n") |
497 | _T("Robert Roebling (c) 1998\n") | |
498 | _T("Autoscrolling examples\n") | |
499 | _T("Ron Lee (c) 2002"), | |
2b5f62a0 VZ |
500 | _T("About wxScroll Demo"), |
501 | wxICON_INFORMATION | wxOK ); | |
fdd3ed7a RR |
502 | } |
503 | ||
504 | //----------------------------------------------------------------------------- | |
505 | // MyApp | |
506 | //----------------------------------------------------------------------------- | |
507 | ||
508 | bool MyApp::OnInit() | |
509 | { | |
510 | wxFrame *frame = new MyFrame(); | |
b62ca03d | 511 | frame->Show( true ); |
fdd3ed7a | 512 | |
b62ca03d | 513 | return true; |
fdd3ed7a RR |
514 | } |
515 | ||
8a73bf3d VZ |
516 | // ---------------------------------------------------------------------------- |
517 | // MyScrolledWindowXXX | |
518 | // ---------------------------------------------------------------------------- | |
519 | ||
8a73bf3d VZ |
520 | void MyScrolledWindowDumb::OnDraw(wxDC& dc) |
521 | { | |
522 | // this is useful to see which lines are redrawn | |
523 | static size_t s_redrawCount = 0; | |
524 | dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE); | |
525 | ||
526 | wxCoord y = 0; | |
527 | for ( size_t line = 0; line < m_nLines; line++ ) | |
528 | { | |
529 | wxCoord yPhys; | |
530 | CalcScrolledPosition(0, y, NULL, &yPhys); | |
531 | ||
532 | dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"), | |
533 | line, y, yPhys), 0, y); | |
534 | y += m_hLine; | |
535 | } | |
536 | } | |
537 | ||
538 | void MyScrolledWindowSmart::OnDraw(wxDC& dc) | |
539 | { | |
540 | // this is useful to see which lines are redrawn | |
541 | static size_t s_redrawCount = 0; | |
542 | dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE); | |
543 | ||
544 | // update region is always in device coords, translate to logical ones | |
545 | wxRect rectUpdate = GetUpdateRegion().GetBox(); | |
546 | CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y, | |
547 | &rectUpdate.x, &rectUpdate.y); | |
548 | ||
549 | size_t lineFrom = rectUpdate.y / m_hLine, | |
550 | lineTo = rectUpdate.GetBottom() / m_hLine; | |
551 | ||
552 | if ( lineTo > m_nLines - 1) | |
553 | lineTo = m_nLines - 1; | |
554 | ||
555 | wxCoord y = lineFrom*m_hLine; | |
556 | for ( size_t line = lineFrom; line <= lineTo; line++ ) | |
557 | { | |
558 | wxCoord yPhys; | |
559 | CalcScrolledPosition(0, y, NULL, &yPhys); | |
560 | ||
561 | dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"), | |
562 | line, y, yPhys), 0, y); | |
563 | y += m_hLine; | |
564 | } | |
565 | } |