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