]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/scrollsub/scrollsub.cpp
add support for sorting to grid columns
[wxWidgets.git] / samples / scrollsub / scrollsub.cpp
... / ...
CommitLineData
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
42 void OnPaint( wxPaintEvent &event );
43 void OnSize( wxSizeEvent &event );
44
45protected:
46 virtual wxSize GetSizeAvailableForScrollTarget(const wxSize& size);
47
48private:
49 MyCanvas *m_canvas;
50
51 DECLARE_DYNAMIC_CLASS(MyScrolledWindow)
52 DECLARE_EVENT_TABLE()
53};
54
55// MyTopLabels
56
57class MyTopLabels: public wxWindow
58{
59public:
60 MyTopLabels() {}
61 MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
62
63 void OnPaint( wxPaintEvent &event );
64
65private:
66 wxScrolledWindow *m_owner;
67
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 );
79
80 void OnPaint( wxPaintEvent &event );
81
82private:
83 wxScrolledWindow *m_owner;
84
85 DECLARE_DYNAMIC_CLASS(MyRightLabels)
86 DECLARE_EVENT_TABLE()
87};
88
89// MyCanvas
90
91class MyCanvas: public wxPanel
92{
93public:
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
101private:
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
112class MyFrame: public wxFrame
113{
114public:
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
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)
148 EVT_PAINT( MyScrolledWindow::OnPaint)
149 EVT_SIZE( MyScrolledWindow::OnSize)
150END_EVENT_TABLE()
151
152MyScrolledWindow::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
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
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
208void 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
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
236void 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
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
270void 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
295IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxPanel)
296
297BEGIN_EVENT_TABLE(MyCanvas, wxPanel)
298 EVT_PAINT( MyCanvas::OnPaint)
299END_EVENT_TABLE()
300
301MyCanvas::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
323void 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
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}
387
388// MyFrame
389
390const int ID_QUIT = wxID_EXIT;
391const int ID_FULL = 109;
392const int ID_ABOUT = wxID_ABOUT;
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)
399 EVT_MENU (ID_FULL, MyFrame::OnFullScreen)
400END_EVENT_TABLE()
401
402MyFrame::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
440void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
441{
442 Close( true );
443}
444
445void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) )
446{
447 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION );
448}
449
450void 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
461bool 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