call wxFlexGridSizer::AddGrowableRow() only when the sizer has enough rows for the...
[wxWidgets.git] / samples / scrollsub / scrollsub.cpp
1 /*
2 * Program: scrollsub
3 *
4 * Author: Robert Roebling
5 *
6 * Copyright: (C) 1999, Robert Roebling
7 *
8 */
9
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20
21 #include "wx/image.h"
22 #include "wx/listctrl.h"
23 #include "wx/sizer.h"
24 #include "wx/log.h"
25
26
27 // derived classes
28
29 class MyFrame;
30 class MyScrolledWindow;
31 class MyCanvas;
32 class MyApp;
33
34 // MyScrolledWindow
35
36 class MyScrolledWindow: public wxScrolledWindow
37 {
38 public:
39 MyScrolledWindow() {}
40 MyScrolledWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
41
42 void OnPaint( wxPaintEvent &event );
43 void OnSize( wxSizeEvent &event );
44
45 protected:
46 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
47
48 private:
49 MyCanvas *m_canvas;
50
51 DECLARE_DYNAMIC_CLASS(MyScrolledWindow)
52 DECLARE_EVENT_TABLE()
53 };
54
55 // MyTopLabels
56
57 class MyTopLabels: public wxWindow
58 {
59 public:
60 MyTopLabels() {}
61 MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
62
63 void OnPaint( wxPaintEvent &event );
64
65 private:
66 wxScrolledWindow *m_owner;
67
68 DECLARE_DYNAMIC_CLASS(MyTopLabels)
69 DECLARE_EVENT_TABLE()
70 };
71
72 // MyRightLabels
73
74 class MyRightLabels: public wxWindow
75 {
76 public:
77 MyRightLabels() {}
78 MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
79
80 void OnPaint( wxPaintEvent &event );
81
82 private:
83 wxScrolledWindow *m_owner;
84
85 DECLARE_DYNAMIC_CLASS(MyRightLabels)
86 DECLARE_EVENT_TABLE()
87 };
88
89 // MyCanvas
90
91 class MyCanvas: public wxPanel
92 {
93 public:
94 MyCanvas(){};
95 MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
96 wxWindowID id, const wxPoint &pos, const wxSize &size );
97 ~MyCanvas(){};
98 void OnPaint( wxPaintEvent &event );
99 void ScrollWindow( int dx, int dy, const wxRect *rect );
100
101 private:
102 wxScrolledWindow *m_owner;
103 MyTopLabels *m_topLabels;
104 MyRightLabels *m_rightLabels;
105
106 DECLARE_DYNAMIC_CLASS(MyCanvas)
107 DECLARE_EVENT_TABLE()
108 };
109
110 // MyFrame
111
112 class MyFrame: public wxFrame
113 {
114 public:
115 MyFrame();
116
117 void OnAbout( wxCommandEvent &event );
118 void OnQuit( wxCommandEvent &event );
119 void OnFullScreen( wxCommandEvent &event );
120
121 wxScrolledWindow *m_scrolled;
122 #if wxUSE_LOG
123 wxTextCtrl *m_log;
124 #endif // wxUSE_LOG
125
126 private:
127 DECLARE_DYNAMIC_CLASS(MyFrame)
128 DECLARE_EVENT_TABLE()
129 };
130
131 // MyApp
132
133 class MyApp: public wxApp
134 {
135 public:
136 virtual bool OnInit();
137 };
138
139 // main program
140
141 IMPLEMENT_APP(MyApp)
142
143 // MyScrolledWindow
144
145 IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow, wxScrolledWindow)
146
147 BEGIN_EVENT_TABLE(MyScrolledWindow, wxScrolledWindow)
148 EVT_PAINT( MyScrolledWindow::OnPaint)
149 EVT_SIZE( MyScrolledWindow::OnSize)
150 END_EVENT_TABLE()
151
152 MyScrolledWindow::MyScrolledWindow( wxWindow *parent, wxWindowID id,
153 const wxPoint &pos, const wxSize &size )
154 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") )
155 {
156 MyTopLabels *top = new MyTopLabels( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,25) );
157 MyRightLabels *right = new MyRightLabels( this, wxID_ANY, wxDefaultPosition, wxSize(60,wxDefaultCoord) );
158
159 m_canvas = new MyCanvas( this, top, right, wxID_ANY, wxDefaultPosition, wxDefaultSize );
160
161 SetTargetWindow( m_canvas );
162
163 SetBackgroundColour( wxT("WHEAT") );
164
165 SetCursor( wxCursor( wxCURSOR_HAND ) );
166
167 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
168
169 wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL );
170 topsizer->Add( 60,25 );
171 topsizer->Add( top, 1, wxEXPAND );
172
173 mainsizer->Add( topsizer, 0, wxEXPAND );
174
175 wxBoxSizer *middlesizer = new wxBoxSizer( wxHORIZONTAL );
176 middlesizer->Add( right, 0, wxEXPAND );
177 middlesizer->Add( m_canvas, 1, wxEXPAND );
178
179 mainsizer->Add( middlesizer, 1, wxEXPAND );
180
181 SetSizer( mainsizer );
182 }
183
184 wxSize MyScrolledWindow::GetSizeAvailableForScrollTarget(const wxSize& size)
185 {
186 // decrease the total size by the size of the non-scrollable parts above/to
187 // the left of the canvas
188 wxSize sizeCanvas(size);
189 sizeCanvas.x -= 60;
190 sizeCanvas.y -= 25;
191 return sizeCanvas;
192 }
193
194 void MyScrolledWindow::OnSize( wxSizeEvent &WXUNUSED(event) )
195 {
196 // We need to override OnSize so that our scrolled
197 // window a) does call Layout() to use sizers for
198 // positioning the controls but b) does not query
199 // the sizer for their size and use that for setting
200 // the scrollable area as set that ourselves by
201 // calling SetScrollbar() further down.
202
203 Layout();
204
205 AdjustScrollbars();
206 }
207
208 void MyScrolledWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
209 {
210 wxPaintDC dc( this );
211
212 /*
213 wxSize size( GetClientSize() );
214
215 long w,h;
216 dc.GetTextExtent( wxT("Headline"), &w, &h );
217
218 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
219 */
220 }
221
222 // MyTopLabels
223
224 IMPLEMENT_DYNAMIC_CLASS(MyTopLabels,wxWindow)
225
226 BEGIN_EVENT_TABLE(MyTopLabels, wxWindow)
227 EVT_PAINT( MyTopLabels::OnPaint)
228 END_EVENT_TABLE()
229
230 MyTopLabels::MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size )
231 : wxWindow( parent, id, pos, size )
232 {
233 m_owner = parent;
234 }
235
236 void MyTopLabels::OnPaint( wxPaintEvent& WXUNUSED(event) )
237 {
238 wxPaintDC dc(this);
239
240 // This is wrong.. it will translate both x and y if the
241 // window is scrolled, the label windows are active in one
242 // direction only. Do the action below instead -- RL.
243 //m_owner->PrepareDC( dc );
244
245 int xScrollUnits, xOrigin;
246
247 m_owner->GetViewStart( &xOrigin, 0 );
248 m_owner->GetScrollPixelsPerUnit( &xScrollUnits, 0 );
249 dc.SetDeviceOrigin( -xOrigin * xScrollUnits, 0 );
250
251 dc.DrawText( _T("Column 1"), 5, 5 );
252 dc.DrawText( _T("Column 2"), 105, 5 );
253 dc.DrawText( _T("Column 3"), 205, 5 );
254 }
255
256 // MyRightLabels
257
258 IMPLEMENT_DYNAMIC_CLASS(MyRightLabels,wxWindow)
259
260 BEGIN_EVENT_TABLE(MyRightLabels, wxWindow)
261 EVT_PAINT( MyRightLabels::OnPaint)
262 END_EVENT_TABLE()
263
264 MyRightLabels::MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size )
265 : wxWindow( parent, id, pos, size )
266 {
267 m_owner = parent;
268 }
269
270 void MyRightLabels::OnPaint( wxPaintEvent& WXUNUSED(event) )
271 {
272 wxPaintDC dc(this);
273
274 // This is wrong.. it will translate both x and y if the
275 // window is scrolled, the label windows are active in one
276 // direction only. Do the action below instead -- RL.
277 //m_owner->PrepareDC( dc );
278
279 int yScrollUnits, yOrigin;
280
281 m_owner->GetViewStart( 0, &yOrigin );
282 m_owner->GetScrollPixelsPerUnit( 0, &yScrollUnits );
283 dc.SetDeviceOrigin( 0, -yOrigin * yScrollUnits );
284
285 dc.DrawText( _T("Row 1"), 5, 5 );
286 dc.DrawText( _T("Row 2"), 5, 30 );
287 dc.DrawText( _T("Row 3"), 5, 55 );
288 dc.DrawText( _T("Row 4"), 5, 80 );
289 dc.DrawText( _T("Row 5"), 5, 105 );
290 dc.DrawText( _T("Row 6"), 5, 130 );
291 }
292
293 // MyCanvas
294
295 IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxPanel)
296
297 BEGIN_EVENT_TABLE(MyCanvas, wxPanel)
298 EVT_PAINT( MyCanvas::OnPaint)
299 END_EVENT_TABLE()
300
301 MyCanvas::MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
302 wxWindowID id, const wxPoint &pos, const wxSize &size )
303 : wxPanel( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") )
304 {
305 m_owner = parent;
306 m_topLabels = top;
307 m_rightLabels = right;
308
309 (void)new wxButton( this, wxID_ANY, _T("Hallo I"), wxPoint(0,50), wxSize(100,25) );
310 (void)new wxButton( this, wxID_ANY, _T("Hallo II"), wxPoint(200,50), wxSize(100,25) );
311
312 (void)new wxTextCtrl( this, wxID_ANY, _T("Text I"), wxPoint(0,100), wxSize(100,25) );
313 (void)new wxTextCtrl( this, wxID_ANY, _T("Text II"), wxPoint(200,100), wxSize(100,25) );
314
315 (void)new wxComboBox( this, wxID_ANY, _T("ComboBox I"), wxPoint(0,150), wxSize(100,25));
316 (void)new wxComboBox( this, wxID_ANY, _T("ComboBox II"), wxPoint(200,150), wxSize(100,25));
317
318 SetBackgroundColour( wxT("WHEAT") );
319
320 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
321 }
322
323 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
324 {
325 wxPaintDC dc( this );
326 m_owner->PrepareDC( dc );
327
328 dc.SetPen( *wxBLACK_PEN );
329
330 // OK, let's assume we are a grid control and we have two
331 // grid cells. Here in OnPaint we want to know which cell
332 // to redraw so that we prevent redrawing cells that don't
333 // need to get redrawn. We have one cell at (0,0) and one
334 // more at (200,0), both having a size of (100,25).
335
336 // We can query how much the window has been scrolled
337 // by calling CalcUnscrolledPosition()
338
339 int scroll_x = 0;
340 int scroll_y = 0;
341 m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y );
342
343 // We also need to know the size of the window to see which
344 // cells are completely hidden and not get redrawn
345
346 int size_x = 0;
347 int size_y = 0;
348 GetClientSize( &size_x, &size_y );
349
350 // First cell: (0,0)(100,25)
351 // It it on screen?
352 if ((0+100-scroll_x > 0) && (0+25-scroll_y > 0) &&
353 (0-scroll_x < size_x) && (0-scroll_y < size_y))
354 {
355 // Has the region on screen been exposed?
356 if (IsExposed(0,0,100,25))
357 {
358 wxLogMessage( wxT("Redraw first cell") );
359 dc.DrawRectangle( 0, 0, 100, 25 );
360 dc.DrawText( _T("First Cell"), 5, 5 );
361 }
362 }
363
364
365 // Second cell: (0,200)(100,25)
366 // It it on screen?
367 if ((200+100-scroll_x > 0) && (0+25-scroll_y > 0) &&
368 (200-scroll_x < size_x) && (0-scroll_y < size_y))
369 {
370 // Has the region on screen been exposed?
371 if (IsExposed(200,0,100,25))
372 {
373 wxLogMessage( wxT("Redraw second cell") );
374 dc.DrawRectangle( 200, 0, 100, 25 );
375 dc.DrawText( _T("Second Cell"), 205, 5 );
376 }
377 }
378
379 }
380
381 void MyCanvas::ScrollWindow( int dx, int dy, const wxRect *rect )
382 {
383 wxPanel::ScrollWindow( dx, dy, rect );
384 m_topLabels->ScrollWindow( dx, 0, rect );
385 m_rightLabels->ScrollWindow( 0, dy, rect );
386 }
387
388 // MyFrame
389
390 const int ID_QUIT = wxID_EXIT;
391 const int ID_FULL = 109;
392 const int ID_ABOUT = wxID_ABOUT;
393
394 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
395
396 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
397 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
398 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
399 EVT_MENU (ID_FULL, MyFrame::OnFullScreen)
400 END_EVENT_TABLE()
401
402 MyFrame::MyFrame()
403 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"),
404 wxPoint(20,20), wxSize(470,500) )
405 {
406 wxMenu *file_menu = new wxMenu();
407 file_menu->Append( ID_ABOUT, _T("&About..."));
408 file_menu->Append( ID_FULL, _T("&Full screen on/off"));
409 file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X"));
410
411 wxMenuBar *menu_bar = new wxMenuBar();
412 menu_bar->Append(file_menu, _T("&File"));
413
414 SetMenuBar( menu_bar );
415
416 #if wxUSE_STATUSBAR
417 CreateStatusBar(2);
418 int widths[] = { -1, 100 };
419 SetStatusWidths( 2, widths );
420 #endif // wxUSE_STATUSBAR
421
422 m_scrolled = new MyScrolledWindow( this, wxID_ANY, wxDefaultPosition, wxSize(100,100) );
423 m_scrolled->SetScrollbars( 10, 10, 50, 50 );
424
425 #if wxUSE_LOG
426 m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
427 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
428 delete old_log;
429 #endif // wxUSE_LOG
430
431 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
432 topsizer->Add( m_scrolled, 1, wxEXPAND );
433 #if wxUSE_LOG
434 topsizer->Add( m_log, 0, wxEXPAND );
435 #endif // wxUSE_LOG
436
437 SetSizer( topsizer );
438 }
439
440 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
441 {
442 Close( true );
443 }
444
445 void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) )
446 {
447 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION );
448 }
449
450 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
451 {
452 (void)wxMessageBox( _T("wxScroll demo II\n")
453 _T("Robert Roebling (c) 1998"),
454 _T("About wxScroll II Demo"), wxICON_INFORMATION | wxOK );
455 }
456
457 //-----------------------------------------------------------------------------
458 // MyApp
459 //-----------------------------------------------------------------------------
460
461 bool MyApp::OnInit()
462 {
463 if ( !wxApp::OnInit() )
464 return false;
465
466 wxFrame *frame = new MyFrame();
467 frame->Show( true );
468
469 return true;
470 }
471