]>
git.saurik.com Git - wxWidgets.git/blob - samples/scrollsub/scrollsub.cpp
4 * Author: Robert Roebling
6 * Copyright: (C) 1999, Robert Roebling
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/listctrl.h"
30 class MyScrolledWindow
;
36 class MyScrolledWindow
: public wxScrolledWindow
40 MyScrolledWindow( wxWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
42 void OnPaint( wxPaintEvent
&event
);
47 DECLARE_DYNAMIC_CLASS(MyScrolledWindow
)
53 class MyTopLabels
: public wxWindow
57 MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
59 void OnPaint( wxPaintEvent
&event
);
62 wxScrolledWindow
*m_owner
;
64 DECLARE_DYNAMIC_CLASS(MyTopLabels
)
70 class MyRightLabels
: public wxWindow
74 MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
76 void OnPaint( wxPaintEvent
&event
);
79 wxScrolledWindow
*m_owner
;
81 DECLARE_DYNAMIC_CLASS(MyRightLabels
)
87 class MyCanvas
: public wxPanel
91 MyCanvas( wxScrolledWindow
*parent
, MyTopLabels
*top
, MyRightLabels
*right
,
92 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
94 void OnPaint( wxPaintEvent
&event
);
95 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
98 wxScrolledWindow
*m_owner
;
99 MyTopLabels
*m_topLabels
;
100 MyRightLabels
*m_rightLabels
;
102 DECLARE_DYNAMIC_CLASS(MyCanvas
)
103 DECLARE_EVENT_TABLE()
108 class MyFrame
: public wxFrame
113 void OnAbout( wxCommandEvent
&event
);
114 void OnQuit( wxCommandEvent
&event
);
115 void OnFullScreen( wxCommandEvent
&event
);
117 wxScrolledWindow
*m_scrolled
;
121 DECLARE_DYNAMIC_CLASS(MyFrame
)
122 DECLARE_EVENT_TABLE()
127 class MyApp
: public wxApp
130 virtual bool OnInit();
139 IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow
, wxScrolledWindow
)
141 BEGIN_EVENT_TABLE(MyScrolledWindow
, wxScrolledWindow
)
142 EVT_PAINT( MyScrolledWindow::OnPaint
)
145 MyScrolledWindow::MyScrolledWindow( wxWindow
*parent
, wxWindowID id
,
146 const wxPoint
&pos
, const wxSize
&size
)
147 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, "test canvas" )
149 MyTopLabels
*top
= new MyTopLabels( this, -1, wxDefaultPosition
, wxSize(-1,25) );
150 MyRightLabels
*right
= new MyRightLabels( this, -1, wxDefaultPosition
, wxSize(60,-1) );
152 m_canvas
= new MyCanvas( this, top
, right
, -1, wxDefaultPosition
, wxDefaultSize
);
154 SetTargetWindow( m_canvas
);
156 SetBackgroundColour( "WHEAT" );
158 SetCursor( wxCursor( wxCURSOR_HAND
) );
160 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
162 wxBoxSizer
*topsizer
= new wxBoxSizer( wxHORIZONTAL
);
163 topsizer
->Add( 60,25 );
164 topsizer
->Add( top
, 1 );
166 mainsizer
->Add( topsizer
, 0, wxEXPAND
);
168 wxBoxSizer
*middlesizer
= new wxBoxSizer( wxHORIZONTAL
);
169 middlesizer
->Add( right
, 0, wxEXPAND
);
170 middlesizer
->Add( m_canvas
, 1, wxEXPAND
);
172 mainsizer
->Add( middlesizer
, 1, wxEXPAND
);
174 SetAutoLayout( TRUE
);
175 SetSizer( mainsizer
);
178 MyScrolledWindow::~MyScrolledWindow()
182 void MyScrolledWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
184 wxPaintDC
dc( this );
187 wxSize size( GetClientSize() );
190 dc.GetTextExtent( wxT("Headline"), &w, &h );
192 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
198 IMPLEMENT_DYNAMIC_CLASS(MyTopLabels
,wxWindow
)
200 BEGIN_EVENT_TABLE(MyTopLabels
, wxWindow
)
201 EVT_PAINT( MyTopLabels::OnPaint
)
204 MyTopLabels::MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
205 : wxWindow( parent
, id
, pos
, size
)
210 void MyTopLabels::OnPaint( wxPaintEvent
&event
)
213 m_owner
->PrepareDC( dc
);
214 dc
.DrawText( "Column 1", 5, 5 );
215 dc
.DrawText( "Column 2", 105, 5 );
216 dc
.DrawText( "Column 3", 205, 5 );
221 IMPLEMENT_DYNAMIC_CLASS(MyRightLabels
,wxWindow
)
223 BEGIN_EVENT_TABLE(MyRightLabels
, wxWindow
)
224 EVT_PAINT( MyRightLabels::OnPaint
)
227 MyRightLabels::MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
228 : wxWindow( parent
, id
, pos
, size
)
233 void MyRightLabels::OnPaint( wxPaintEvent
&event
)
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 );
247 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxPanel
)
249 BEGIN_EVENT_TABLE(MyCanvas
, wxPanel
)
250 EVT_PAINT( MyCanvas::OnPaint
)
253 MyCanvas::MyCanvas( wxScrolledWindow
*parent
, MyTopLabels
*top
, MyRightLabels
*right
,
254 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
255 : wxPanel( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, "test canvas" )
259 m_rightLabels
= right
;
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) );
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) );
267 SetBackgroundColour( "WHEAT" );
269 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
272 MyCanvas::~MyCanvas()
276 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
278 wxPaintDC
dc( this );
279 m_owner
->PrepareDC( dc
);
281 dc
.SetPen( *wxBLACK_PEN
);
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).
289 // We can query how much the window has been scrolled
290 // by calling CalcUnscrolledPosition()
294 m_owner
->CalcUnscrolledPosition( scroll_x
, scroll_y
, &scroll_x
, &scroll_y
);
296 // We also need to know the size of the window to see which
297 // cells are completely hidden and not get redrawn
301 GetClientSize( &size_x
, &size_y
);
303 // First cell: (0,0)(100,25)
305 if ((0+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
306 (0-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
308 // Has the region on screen been exposed?
309 if (IsExposed(0,0,100,25))
311 wxLogMessage( "Redraw first cell" );
312 dc
.DrawRectangle( 0, 0, 100, 25 );
313 dc
.DrawText( "First Cell", 5, 5 );
318 // Second cell: (0,200)(100,25)
320 if ((200+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
321 (200-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
323 // Has the region on screen been exposed?
324 if (IsExposed(200,0,100,25))
326 wxLogMessage( "Redraw second cell" );
327 dc
.DrawRectangle( 200, 0, 100, 25 );
328 dc
.DrawText( "Second Cell", 205, 5 );
334 void MyCanvas::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
336 wxPanel::ScrollWindow( dx
, dy
, rect
);
337 m_topLabels
->ScrollWindow( dx
, 0, rect
);
338 m_rightLabels
->ScrollWindow( 0, dy
, rect
);
343 const int ID_QUIT
= 108;
344 const int ID_FULL
= 109;
345 const int ID_ABOUT
= 110;
347 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
349 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
350 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
351 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
352 EVT_MENU (ID_FULL
, MyFrame::OnFullScreen
)
356 : wxFrame( (wxFrame
*)NULL
, -1, "wxScrolledWindow sample",
357 wxPoint(20,20), wxSize(470,500) )
359 wxMenu
*file_menu
= new wxMenu();
360 file_menu
->Append( ID_ABOUT
, "&About...");
361 file_menu
->Append( ID_FULL
, "&Full screen on/off");
362 file_menu
->Append( ID_QUIT
, "E&xit\tAlt-X");
364 wxMenuBar
*menu_bar
= new wxMenuBar();
365 menu_bar
->Append(file_menu
, "&File");
367 SetMenuBar( menu_bar
);
370 int widths
[] = { -1, 100 };
371 SetStatusWidths( 2, widths
);
373 m_scrolled
= new MyScrolledWindow( this, -1, wxDefaultPosition
, wxSize(100,100) );
374 m_scrolled
->SetScrollbars( 10, 10, 50, 100 );
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
) );
380 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
381 topsizer
->Add( m_scrolled
, 1, wxEXPAND
);
382 topsizer
->Add( m_log
, 0, wxEXPAND
);
384 SetAutoLayout( TRUE
);
385 SetSizer( topsizer
);
388 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
393 void MyFrame::OnFullScreen( wxCommandEvent
&WXUNUSED(event
) )
395 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER
|wxFULLSCREEN_NOCAPTION
);
398 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
400 (void)wxMessageBox( "wxScroll demo II\n"
401 "Robert Roebling (c) 1998",
402 "About wxScroll II Demo", wxICON_INFORMATION
| wxOK
);
405 //-----------------------------------------------------------------------------
407 //-----------------------------------------------------------------------------
411 wxFrame
*frame
= new MyFrame();