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