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