]> git.saurik.com Git - wxWidgets.git/blame - samples/scrollsub/scrollsub.cpp
port updates for newer Intel compiler support from bakefile svn and rebake
[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:
fa05a943 39 MyScrolledWindow() {}
ecab4dba 40 MyScrolledWindow( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
fa05a943 41
ecab4dba 42 void OnPaint( wxPaintEvent &event );
c376d80f 43 void OnSize( wxSizeEvent &event );
fa05a943
VZ
44
45protected:
46 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
47
ecab4dba
RR
48private:
49 MyCanvas *m_canvas;
50
51 DECLARE_DYNAMIC_CLASS(MyScrolledWindow)
52 DECLARE_EVENT_TABLE()
53};
54
dfd6b52f
RR
55// MyTopLabels
56
57class MyTopLabels: public wxWindow
58{
59public:
60 MyTopLabels() {}
61 MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
5f3286d1 62
dfd6b52f 63 void OnPaint( wxPaintEvent &event );
5f3286d1 64
dfd6b52f
RR
65private:
66 wxScrolledWindow *m_owner;
5f3286d1 67
dfd6b52f
RR
68 DECLARE_DYNAMIC_CLASS(MyTopLabels)
69 DECLARE_EVENT_TABLE()
70};
71
72// MyRightLabels
73
74class MyRightLabels: public wxWindow
75{
76public:
77 MyRightLabels() {}
78 MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
5f3286d1 79
dfd6b52f 80 void OnPaint( wxPaintEvent &event );
5f3286d1 81
dfd6b52f
RR
82private:
83 wxScrolledWindow *m_owner;
5f3286d1 84
dfd6b52f
RR
85 DECLARE_DYNAMIC_CLASS(MyRightLabels)
86 DECLARE_EVENT_TABLE()
87};
88
ecab4dba
RR
89// MyCanvas
90
91class MyCanvas: public wxPanel
92{
93public:
925e9792 94 MyCanvas(){};
dfd6b52f
RR
95 MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
96 wxWindowID id, const wxPoint &pos, const wxSize &size );
925e9792 97 ~MyCanvas(){};
ecab4dba 98 void OnPaint( wxPaintEvent &event );
dfd6b52f 99 void ScrollWindow( int dx, int dy, const wxRect *rect );
ecab4dba
RR
100
101private:
102 wxScrolledWindow *m_owner;
dfd6b52f
RR
103 MyTopLabels *m_topLabels;
104 MyRightLabels *m_rightLabels;
ecab4dba
RR
105
106 DECLARE_DYNAMIC_CLASS(MyCanvas)
107 DECLARE_EVENT_TABLE()
108};
109
110// MyFrame
111
112class MyFrame: public wxFrame
113{
114public:
115 MyFrame();
116
117 void OnAbout( wxCommandEvent &event );
118 void OnQuit( wxCommandEvent &event );
3d0c4d2e 119 void OnFullScreen( wxCommandEvent &event );
ecab4dba
RR
120
121 wxScrolledWindow *m_scrolled;
b29903d4 122#if wxUSE_LOG
ecab4dba 123 wxTextCtrl *m_log;
b29903d4 124#endif // wxUSE_LOG
ecab4dba
RR
125
126private:
127 DECLARE_DYNAMIC_CLASS(MyFrame)
128 DECLARE_EVENT_TABLE()
129};
130
131// MyApp
132
133class MyApp: public wxApp
134{
135public:
136 virtual bool OnInit();
137};
138
139// main program
140
141IMPLEMENT_APP(MyApp)
142
143// MyScrolledWindow
144
145IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow, wxScrolledWindow)
146
147BEGIN_EVENT_TABLE(MyScrolledWindow, wxScrolledWindow)
c376d80f
RR
148 EVT_PAINT( MyScrolledWindow::OnPaint)
149 EVT_SIZE( MyScrolledWindow::OnSize)
ecab4dba
RR
150END_EVENT_TABLE()
151
152MyScrolledWindow::MyScrolledWindow( wxWindow *parent, wxWindowID id,
153 const wxPoint &pos, const wxSize &size )
600683ca 154 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") )
ecab4dba 155{
422d0ff0
WS
156 MyTopLabels *top = new MyTopLabels( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,25) );
157 MyRightLabels *right = new MyRightLabels( this, wxID_ANY, wxDefaultPosition, wxSize(60,wxDefaultCoord) );
dfd6b52f 158
b62ca03d 159 m_canvas = new MyCanvas( this, top, right, wxID_ANY, wxDefaultPosition, wxDefaultSize );
5f3286d1 160
ecab4dba
RR
161 SetTargetWindow( m_canvas );
162
a60b1f5d 163 SetBackgroundColour( wxT("WHEAT") );
5f3286d1 164
ecab4dba 165 SetCursor( wxCursor( wxCURSOR_HAND ) );
5f3286d1 166
dfd6b52f 167 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
5f3286d1 168
dfd6b52f
RR
169 wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL );
170 topsizer->Add( 60,25 );
5f3286d1
VZ
171 topsizer->Add( top, 1, wxEXPAND );
172
dfd6b52f 173 mainsizer->Add( topsizer, 0, wxEXPAND );
5f3286d1 174
dfd6b52f
RR
175 wxBoxSizer *middlesizer = new wxBoxSizer( wxHORIZONTAL );
176 middlesizer->Add( right, 0, wxEXPAND );
177 middlesizer->Add( m_canvas, 1, wxEXPAND );
5f3286d1 178
dfd6b52f 179 mainsizer->Add( middlesizer, 1, wxEXPAND );
ecab4dba 180
dfd6b52f 181 SetSizer( mainsizer );
ecab4dba
RR
182}
183
fa05a943
VZ
184wxSize 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
c376d80f
RR
194void 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
ecab4dba
RR
208void MyScrolledWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
209{
210 wxPaintDC dc( this );
211
dfd6b52f 212/*
ecab4dba 213 wxSize size( GetClientSize() );
5f3286d1 214
ecab4dba
RR
215 long w,h;
216 dc.GetTextExtent( wxT("Headline"), &w, &h );
217
218 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
dfd6b52f
RR
219*/
220}
221
222// MyTopLabels
223
224IMPLEMENT_DYNAMIC_CLASS(MyTopLabels,wxWindow)
225
226BEGIN_EVENT_TABLE(MyTopLabels, wxWindow)
227 EVT_PAINT( MyTopLabels::OnPaint)
228END_EVENT_TABLE()
229
230MyTopLabels::MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size )
231 : wxWindow( parent, id, pos, size )
232{
233 m_owner = parent;
234}
235
4547faae 236void MyTopLabels::OnPaint( wxPaintEvent& WXUNUSED(event) )
dfd6b52f
RR
237{
238 wxPaintDC dc(this);
566d84a7
RL
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
600683ca
MB
251 dc.DrawText( _T("Column 1"), 5, 5 );
252 dc.DrawText( _T("Column 2"), 105, 5 );
253 dc.DrawText( _T("Column 3"), 205, 5 );
dfd6b52f
RR
254}
255
256// MyRightLabels
257
258IMPLEMENT_DYNAMIC_CLASS(MyRightLabels,wxWindow)
259
260BEGIN_EVENT_TABLE(MyRightLabels, wxWindow)
261 EVT_PAINT( MyRightLabels::OnPaint)
262END_EVENT_TABLE()
263
264MyRightLabels::MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size )
265 : wxWindow( parent, id, pos, size )
266{
267 m_owner = parent;
268}
269
4547faae 270void MyRightLabels::OnPaint( wxPaintEvent& WXUNUSED(event) )
dfd6b52f
RR
271{
272 wxPaintDC dc(this);
566d84a7
RL
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
600683ca
MB
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 );
ecab4dba
RR
291}
292
293// MyCanvas
294
295IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxPanel)
296
297BEGIN_EVENT_TABLE(MyCanvas, wxPanel)
298 EVT_PAINT( MyCanvas::OnPaint)
299END_EVENT_TABLE()
300
dfd6b52f
RR
301MyCanvas::MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
302 wxWindowID id, const wxPoint &pos, const wxSize &size )
600683ca 303 : wxPanel( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") )
ecab4dba
RR
304{
305 m_owner = parent;
dfd6b52f
RR
306 m_topLabels = top;
307 m_rightLabels = right;
5f3286d1 308
b62ca03d
WS
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) );
5f3286d1 311
b62ca03d
WS
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) );
ecab4dba 314
b62ca03d
WS
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));
2a0e49a4 317
a60b1f5d 318 SetBackgroundColour( wxT("WHEAT") );
5f3286d1 319
ecab4dba
RR
320 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
321}
322
ecab4dba
RR
323void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
324{
325 wxPaintDC dc( this );
326 m_owner->PrepareDC( dc );
327
76db86e7 328 dc.SetPen( *wxBLACK_PEN );
5f3286d1 329
17800993
RR
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).
5f3286d1
VZ
335
336 // We can query how much the window has been scrolled
17800993 337 // by calling CalcUnscrolledPosition()
5f3286d1 338
17800993
RR
339 int scroll_x = 0;
340 int scroll_y = 0;
341 m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y );
5f3286d1 342
17800993
RR
343 // We also need to know the size of the window to see which
344 // cells are completely hidden and not get redrawn
5f3286d1 345
17800993
RR
346 int size_x = 0;
347 int size_y = 0;
348 GetClientSize( &size_x, &size_y );
5f3286d1 349
17800993
RR
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 {
dfd6b52f 355 // Has the region on screen been exposed?
2f6c54eb
VZ
356 if (IsExposed(0,0,100,25))
357 {
358 wxLogMessage( wxT("Redraw first cell") );
17800993 359 dc.DrawRectangle( 0, 0, 100, 25 );
600683ca 360 dc.DrawText( _T("First Cell"), 5, 5 );
2f6c54eb 361 }
17800993 362 }
5f3286d1
VZ
363
364
17800993
RR
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 {
dfd6b52f 370 // Has the region on screen been exposed?
2f6c54eb
VZ
371 if (IsExposed(200,0,100,25))
372 {
373 wxLogMessage( wxT("Redraw second cell") );
17800993 374 dc.DrawRectangle( 200, 0, 100, 25 );
600683ca 375 dc.DrawText( _T("Second Cell"), 205, 5 );
2f6c54eb 376 }
17800993 377 }
5f3286d1 378
ecab4dba
RR
379}
380
dfd6b52f
RR
381void 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}
ecab4dba
RR
387
388// MyFrame
389
91b07357 390const int ID_QUIT = wxID_EXIT;
3d0c4d2e 391const int ID_FULL = 109;
91b07357 392const int ID_ABOUT = wxID_ABOUT;
ecab4dba
RR
393
394IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
395
396BEGIN_EVENT_TABLE(MyFrame,wxFrame)
397 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
398 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
3d0c4d2e 399 EVT_MENU (ID_FULL, MyFrame::OnFullScreen)
ecab4dba
RR
400END_EVENT_TABLE()
401
402MyFrame::MyFrame()
b62ca03d 403 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"),
ecab4dba
RR
404 wxPoint(20,20), wxSize(470,500) )
405{
406 wxMenu *file_menu = new wxMenu();
600683ca
MB
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"));
ecab4dba
RR
410
411 wxMenuBar *menu_bar = new wxMenuBar();
600683ca 412 menu_bar->Append(file_menu, _T("&File"));
ecab4dba
RR
413
414 SetMenuBar( menu_bar );
415
8520f137 416#if wxUSE_STATUSBAR
ecab4dba
RR
417 CreateStatusBar(2);
418 int widths[] = { -1, 100 };
419 SetStatusWidths( 2, widths );
8520f137 420#endif // wxUSE_STATUSBAR
ecab4dba 421
b62ca03d 422 m_scrolled = new MyScrolledWindow( this, wxID_ANY, wxDefaultPosition, wxSize(100,100) );
4547faae 423 m_scrolled->SetScrollbars( 10, 10, 50, 50 );
5f3286d1 424
b29903d4 425#if wxUSE_LOG
b62ca03d 426 m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
ecab4dba
RR
427 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
428 delete old_log;
b29903d4 429#endif // wxUSE_LOG
5f3286d1 430
ecab4dba 431 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
ecab4dba 432 topsizer->Add( m_scrolled, 1, wxEXPAND );
b29903d4 433#if wxUSE_LOG
ecab4dba 434 topsizer->Add( m_log, 0, wxEXPAND );
b29903d4 435#endif // wxUSE_LOG
ecab4dba 436
ecab4dba
RR
437 SetSizer( topsizer );
438}
439
440void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
441{
b62ca03d 442 Close( true );
ecab4dba
RR
443}
444
3d0c4d2e
RR
445void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) )
446{
447 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION );
448}
449
ecab4dba
RR
450void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
451{
600683ca
MB
452 (void)wxMessageBox( _T("wxScroll demo II\n")
453 _T("Robert Roebling (c) 1998"),
454 _T("About wxScroll II Demo"), wxICON_INFORMATION | wxOK );
ecab4dba
RR
455}
456
457//-----------------------------------------------------------------------------
458// MyApp
459//-----------------------------------------------------------------------------
460
461bool MyApp::OnInit()
462{
45e6e6f8
VZ
463 if ( !wxApp::OnInit() )
464 return false;
465
ecab4dba 466 wxFrame *frame = new MyFrame();
b62ca03d 467 frame->Show( true );
ecab4dba 468
b62ca03d 469 return true;
ecab4dba
RR
470}
471