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