]> git.saurik.com Git - wxWidgets.git/blame - samples/scrollsub/scrollsub.cpp
updated and expanded wxLog docs
[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 );
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
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 )
147 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, "test canvas" )
148{
dfd6b52f
RR
149 MyTopLabels *top = new MyTopLabels( this, -1, wxDefaultPosition, wxSize(-1,25) );
150 MyRightLabels *right = new MyRightLabels( this, -1, wxDefaultPosition, wxSize(60,-1) );
151
152 m_canvas = new MyCanvas( this, top, right, -1, wxDefaultPosition, wxDefaultSize );
ecab4dba
RR
153
154 SetTargetWindow( m_canvas );
155
a60b1f5d 156 SetBackgroundColour( wxT("WHEAT") );
ecab4dba
RR
157
158 SetCursor( wxCursor( wxCURSOR_HAND ) );
159
dfd6b52f 160 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
ecab4dba 161
dfd6b52f
RR
162 wxBoxSizer *topsizer = new wxBoxSizer( wxHORIZONTAL );
163 topsizer->Add( 60,25 );
164 topsizer->Add( top, 1 );
165
166 mainsizer->Add( topsizer, 0, wxEXPAND );
167
168 wxBoxSizer *middlesizer = new wxBoxSizer( wxHORIZONTAL );
169 middlesizer->Add( right, 0, wxEXPAND );
170 middlesizer->Add( m_canvas, 1, wxEXPAND );
ecab4dba 171
dfd6b52f 172 mainsizer->Add( middlesizer, 1, wxEXPAND );
ecab4dba
RR
173
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
RR
187 wxSize size( GetClientSize() );
188
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
210void MyTopLabels::OnPaint( wxPaintEvent &event )
211{
212 wxPaintDC dc(this);
213 m_owner->PrepareDC( dc );
8f75cb6c
RR
214 dc.DrawText( "Column 1", 5, 5 );
215 dc.DrawText( "Column 2", 105, 5 );
216 dc.DrawText( "Column 3", 205, 5 );
dfd6b52f
RR
217}
218
219// MyRightLabels
220
221IMPLEMENT_DYNAMIC_CLASS(MyRightLabels,wxWindow)
222
223BEGIN_EVENT_TABLE(MyRightLabels, wxWindow)
224 EVT_PAINT( MyRightLabels::OnPaint)
225END_EVENT_TABLE()
226
227MyRightLabels::MyRightLabels( wxScrolledWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size )
228 : wxWindow( parent, id, pos, size )
229{
230 m_owner = parent;
231}
232
233void MyRightLabels::OnPaint( wxPaintEvent &event )
234{
235 wxPaintDC dc(this);
236 m_owner->PrepareDC( dc );
237 dc.DrawText( "Row 1", 5, 5 );
238 dc.DrawText( "Row 2", 5, 30 );
239 dc.DrawText( "Row 3", 5, 55 );
240 dc.DrawText( "Row 4", 5, 80 );
241 dc.DrawText( "Row 5", 5, 105 );
242 dc.DrawText( "Row 6", 5, 130 );
ecab4dba
RR
243}
244
245// MyCanvas
246
247IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxPanel)
248
249BEGIN_EVENT_TABLE(MyCanvas, wxPanel)
250 EVT_PAINT( MyCanvas::OnPaint)
251END_EVENT_TABLE()
252
dfd6b52f
RR
253MyCanvas::MyCanvas( wxScrolledWindow *parent, MyTopLabels *top, MyRightLabels *right,
254 wxWindowID id, const wxPoint &pos, const wxSize &size )
ecab4dba
RR
255 : wxPanel( parent, id, pos, size, wxSUNKEN_BORDER, "test canvas" )
256{
257 m_owner = parent;
dfd6b52f
RR
258 m_topLabels = top;
259 m_rightLabels = right;
ecab4dba 260
cddfbd9f
RR
261 (void)new wxButton( this, -1, "Hallo I", wxPoint(0,50), wxSize(100,25) );
262 (void)new wxButton( this, -1, "Hallo II", wxPoint(200,50), wxSize(100,25) );
dfd6b52f
RR
263
264 (void)new wxTextCtrl( this, -1, "Text I", wxPoint(0,100), wxSize(100,25) );
265 (void)new wxTextCtrl( this, -1, "Text II", wxPoint(200,100), wxSize(100,25) );
ecab4dba 266
a60b1f5d 267 SetBackgroundColour( wxT("WHEAT") );
ecab4dba
RR
268
269 SetCursor( wxCursor( wxCURSOR_IBEAM ) );
270}
271
272MyCanvas::~MyCanvas()
273{
274}
275
276void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
277{
278 wxPaintDC dc( this );
279 m_owner->PrepareDC( dc );
280
76db86e7
RR
281 dc.SetPen( *wxBLACK_PEN );
282
17800993
RR
283 // OK, let's assume we are a grid control and we have two
284 // grid cells. Here in OnPaint we want to know which cell
285 // to redraw so that we prevent redrawing cells that don't
286 // need to get redrawn. We have one cell at (0,0) and one
287 // more at (200,0), both having a size of (100,25).
288
289 // We can query how much the window has been scrolled
290 // by calling CalcUnscrolledPosition()
291
292 int scroll_x = 0;
293 int scroll_y = 0;
294 m_owner->CalcUnscrolledPosition( scroll_x, scroll_y, &scroll_x, &scroll_y );
295
296 // We also need to know the size of the window to see which
297 // cells are completely hidden and not get redrawn
298
299 int size_x = 0;
300 int size_y = 0;
301 GetClientSize( &size_x, &size_y );
302
303 // First cell: (0,0)(100,25)
304 // It it on screen?
305 if ((0+100-scroll_x > 0) && (0+25-scroll_y > 0) &&
306 (0-scroll_x < size_x) && (0-scroll_y < size_y))
307 {
dfd6b52f
RR
308 // Has the region on screen been exposed?
309 if (IsExposed(0,0,100,25))
310 {
4693b20c 311 wxLogMessage( wxT("Redraw first cell") );
17800993 312 dc.DrawRectangle( 0, 0, 100, 25 );
dfd6b52f
RR
313 dc.DrawText( "First Cell", 5, 5 );
314 }
17800993
RR
315 }
316
ecab4dba 317
17800993
RR
318 // Second cell: (0,200)(100,25)
319 // It it on screen?
320 if ((200+100-scroll_x > 0) && (0+25-scroll_y > 0) &&
321 (200-scroll_x < size_x) && (0-scroll_y < size_y))
322 {
dfd6b52f
RR
323 // Has the region on screen been exposed?
324 if (IsExposed(200,0,100,25))
325 {
4693b20c 326 wxLogMessage( wxT("Redraw second cell") );
17800993 327 dc.DrawRectangle( 200, 0, 100, 25 );
dfd6b52f
RR
328 dc.DrawText( "Second Cell", 205, 5 );
329 }
17800993 330 }
ecab4dba 331
ecab4dba
RR
332}
333
dfd6b52f
RR
334void MyCanvas::ScrollWindow( int dx, int dy, const wxRect *rect )
335{
336 wxPanel::ScrollWindow( dx, dy, rect );
337 m_topLabels->ScrollWindow( dx, 0, rect );
338 m_rightLabels->ScrollWindow( 0, dy, rect );
339}
ecab4dba
RR
340
341// MyFrame
342
343const int ID_QUIT = 108;
3d0c4d2e
RR
344const int ID_FULL = 109;
345const int ID_ABOUT = 110;
ecab4dba
RR
346
347IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
348
349BEGIN_EVENT_TABLE(MyFrame,wxFrame)
350 EVT_MENU (ID_ABOUT, MyFrame::OnAbout)
351 EVT_MENU (ID_QUIT, MyFrame::OnQuit)
3d0c4d2e 352 EVT_MENU (ID_FULL, MyFrame::OnFullScreen)
ecab4dba
RR
353END_EVENT_TABLE()
354
355MyFrame::MyFrame()
356 : wxFrame( (wxFrame *)NULL, -1, "wxScrolledWindow sample",
357 wxPoint(20,20), wxSize(470,500) )
358{
359 wxMenu *file_menu = new wxMenu();
3d0c4d2e
RR
360 file_menu->Append( ID_ABOUT, "&About...");
361 file_menu->Append( ID_FULL, "&Full screen on/off");
ecab4dba
RR
362 file_menu->Append( ID_QUIT, "E&xit\tAlt-X");
363
364 wxMenuBar *menu_bar = new wxMenuBar();
365 menu_bar->Append(file_menu, "&File");
366
367 SetMenuBar( menu_bar );
368
369 CreateStatusBar(2);
370 int widths[] = { -1, 100 };
371 SetStatusWidths( 2, widths );
372
dfd6b52f 373 m_scrolled = new MyScrolledWindow( this, -1, wxDefaultPosition, wxSize(100,100) );
ecab4dba
RR
374 m_scrolled->SetScrollbars( 10, 10, 50, 100 );
375
376 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE );
377 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
378 delete old_log;
379
380 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
ecab4dba
RR
381 topsizer->Add( m_scrolled, 1, wxEXPAND );
382 topsizer->Add( m_log, 0, wxEXPAND );
383
384 SetAutoLayout( TRUE );
385 SetSizer( topsizer );
386}
387
388void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
389{
390 Close( TRUE );
391}
392
3d0c4d2e
RR
393void MyFrame::OnFullScreen( wxCommandEvent &WXUNUSED(event) )
394{
395 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER|wxFULLSCREEN_NOCAPTION );
396}
397
ecab4dba
RR
398void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
399{
400 (void)wxMessageBox( "wxScroll demo II\n"
401 "Robert Roebling (c) 1998",
402 "About wxScroll II Demo", wxICON_INFORMATION | wxOK );
403}
404
405//-----------------------------------------------------------------------------
406// MyApp
407//-----------------------------------------------------------------------------
408
409bool MyApp::OnInit()
410{
411 wxFrame *frame = new MyFrame();
412 frame->Show( TRUE );
413
414 return TRUE;
415}
416