]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/scrollsub/scrollsub.cpp
descriptiveLabel clarification
[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 ~MyScrolledWindow();
42 void OnPaint( wxPaintEvent &event );
43
44private:
45 MyCanvas *m_canvas;
46
47 DECLARE_DYNAMIC_CLASS(MyScrolledWindow)
48 DECLARE_EVENT_TABLE()
49};
50
51// MyTopLabels
52
53class MyTopLabels: public wxWindow
54{
55public:
56 MyTopLabels() {}
57 MyTopLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size );
58
59 void OnPaint( wxPaintEvent &event );
60
61private:
62 wxScrolledWindow *m_owner;
63
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 );
75
76 void OnPaint( wxPaintEvent &event );
77
78private:
79 wxScrolledWindow *m_owner;
80
81 DECLARE_DYNAMIC_CLASS(MyRightLabels)
82 DECLARE_EVENT_TABLE()
83};
84
85// MyCanvas
86
87class MyCanvas: public wxPanel
88{
89public:
90 MyCanvas() {}
91 MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
92 wxWindowID id, const wxPoint &pos, const wxSize &size );
93 ~MyCanvas();
94 void OnPaint( wxPaintEvent &event );
95 void ScrollWindow( int dx, int dy, const wxRect *rect );
96
97private:
98 wxScrolledWindow *m_owner;
99 MyTopLabels *m_topLabels;
100 MyRightLabels *m_rightLabels;
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 );
115 void OnFullScreen( wxCommandEvent &event );
116
117 wxScrolledWindow *m_scrolled;
118#if wxUSE_LOG
119 wxTextCtrl *m_log;
120#endif // wxUSE_LOG
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 )
149 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") )
150{
151 MyTopLabels *top = new MyTopLabels( this, wxID_ANY, wxDefaultPosition, wxSize(wxDefaultCoord,25) );
152 MyRightLabels *right = new MyRightLabels( this, wxID_ANY, wxDefaultPosition, wxSize(60,wxDefaultCoord) );
153
154 m_canvas = new MyCanvas( this, top, right, wxID_ANY, wxDefaultPosition, wxDefaultSize );
155
156 SetTargetWindow( m_canvas );
157
158 SetBackgroundColour( wxT("WHEAT") );
159
160 SetCursor( wxCursor( wxCURSOR_HAND ) );
161
162 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
163
164 wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL );
165 topsizer->Add( 60,25 );
166 topsizer->Add( top, 1, wxEXPAND );
167
168 mainsizer->Add( topsizer, 0, wxEXPAND );
169
170 wxBoxSizer *middlesizer = new wxBoxSizer( wxHORIZONTAL );
171 middlesizer->Add( right, 0, wxEXPAND );
172 middlesizer->Add( m_canvas, 1, wxEXPAND );
173
174 mainsizer->Add( middlesizer, 1, wxEXPAND );
175
176 SetAutoLayout( true );
177 SetSizer( mainsizer );
178}
179
180MyScrolledWindow::~MyScrolledWindow()
181{
182}
183
184void MyScrolledWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
185{
186 wxPaintDC dc( this );
187
188/*
189 wxSize size( GetClientSize() );
190
191 long w,h;
192 dc.GetTextExtent( wxT("Headline"), &w, &h );
193
194 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
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
212void MyTopLabels::OnPaint( wxPaintEvent& WXUNUSED(event) )
213{
214 wxPaintDC dc(this);
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
227 dc.DrawText( _T("Column 1"), 5, 5 );
228 dc.DrawText( _T("Column 2"), 105, 5 );
229 dc.DrawText( _T("Column 3"), 205, 5 );
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
246void MyRightLabels::OnPaint( wxPaintEvent& WXUNUSED(event) )
247{
248 wxPaintDC dc(this);
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
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 );
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
277MyCanvas::MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
278 wxWindowID id, const wxPoint &pos, const wxSize &size )
279 : wxPanel( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") )
280{
281 m_owner = parent;
282 m_topLabels = top;
283 m_rightLabels = right;
284
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) );
287
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) );
290
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));
293
294 SetBackgroundColour( wxT("WHEAT") );
295
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
308 dc.SetPen( *wxBLACK_PEN );
309
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).
315
316 // We can query how much the window has been scrolled
317 // by calling CalcUnscrolledPosition()
318
319 int scroll_x = 0;
320 int scroll_y = 0;
321 m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y );
322
323 // We also need to know the size of the window to see which
324 // cells are completely hidden and not get redrawn
325
326 int size_x = 0;
327 int size_y = 0;
328 GetClientSize( &size_x, &size_y );
329
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 {
335 // Has the region on screen been exposed?
336 if (IsExposed(0,0,100,25))
337 {
338 wxLogMessage( wxT("Redraw first cell") );
339 dc.DrawRectangle( 0, 0, 100, 25 );
340 dc.DrawText( _T("First Cell"), 5, 5 );
341 }
342 }
343
344
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 {
350 // Has the region on screen been exposed?
351 if (IsExposed(200,0,100,25))
352 {
353 wxLogMessage( wxT("Redraw second cell") );
354 dc.DrawRectangle( 200, 0, 100, 25 );
355 dc.DrawText( _T("Second Cell"), 205, 5 );
356 }
357 }
358
359}
360
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}
367
368// MyFrame
369
370const int ID_QUIT = 108;
371const int ID_FULL = 109;
372const int ID_ABOUT = 110;
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)
379 EVT_MENU (ID_FULL, MyFrame::OnFullScreen)
380END_EVENT_TABLE()
381
382MyFrame::MyFrame()
383 : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"),
384 wxPoint(20,20), wxSize(470,500) )
385{
386 wxMenu *file_menu = new wxMenu();
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"));
390
391 wxMenuBar *menu_bar = new wxMenuBar();
392 menu_bar->Append(file_menu, _T("&File"));
393
394 SetMenuBar( menu_bar );
395
396#if wxUSE_STATUSBAR
397 CreateStatusBar(2);
398 int widths[] = { -1, 100 };
399 SetStatusWidths( 2, widths );
400#endif // wxUSE_STATUSBAR
401
402 m_scrolled = new MyScrolledWindow( this, wxID_ANY, wxDefaultPosition, wxSize(100,100) );
403 m_scrolled->SetScrollbars( 10, 10, 50, 50 );
404
405#if wxUSE_LOG
406 m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
407 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
408 delete old_log;
409#endif // wxUSE_LOG
410
411 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
412 topsizer->Add( m_scrolled, 1, wxEXPAND );
413#if wxUSE_LOG
414 topsizer->Add( m_log, 0, wxEXPAND );
415#endif // wxUSE_LOG
416
417 SetAutoLayout( true );
418 SetSizer( topsizer );
419}
420
421void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
422{
423 Close( true );
424}
425
426void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) )
427{
428 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION );
429}
430
431void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
432{
433 (void)wxMessageBox( _T("wxScroll demo II\n")
434 _T("Robert Roebling (c) 1998"),
435 _T("About wxScroll II Demo"), wxICON_INFORMATION | wxOK );
436}
437
438//-----------------------------------------------------------------------------
439// MyApp
440//-----------------------------------------------------------------------------
441
442bool MyApp::OnInit()
443{
444 wxFrame *frame = new MyFrame();
445 frame->Show( true );
446
447 return true;
448}
449