]>
git.saurik.com Git - wxWidgets.git/blob - samples/scrollsub/scrollsub.cpp
d1f400d426e8e9216491d53d975f76cb7a933177
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
);
43 void OnSize( wxSizeEvent
&event
);
46 virtual wxSize
GetSizeAvailableForScrollTarget(const wxSize
& size
);
51 DECLARE_DYNAMIC_CLASS(MyScrolledWindow
)
57 class MyTopLabels
: public wxWindow
61 MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
63 void OnPaint( wxPaintEvent
&event
);
66 wxScrolledWindow
*m_owner
;
68 DECLARE_DYNAMIC_CLASS(MyTopLabels
)
74 class MyRightLabels
: public wxWindow
78 MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
80 void OnPaint( wxPaintEvent
&event
);
83 wxScrolledWindow
*m_owner
;
85 DECLARE_DYNAMIC_CLASS(MyRightLabels
)
91 class MyCanvas
: public wxPanel
95 MyCanvas( wxScrolledWindow
*parent
, MyTopLabels
*top
, MyRightLabels
*right
,
96 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
);
98 void OnPaint( wxPaintEvent
&event
);
99 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
102 wxScrolledWindow
*m_owner
;
103 MyTopLabels
*m_topLabels
;
104 MyRightLabels
*m_rightLabels
;
106 DECLARE_DYNAMIC_CLASS(MyCanvas
)
107 DECLARE_EVENT_TABLE()
112 class MyFrame
: public wxFrame
117 void OnAbout( wxCommandEvent
&event
);
118 void OnQuit( wxCommandEvent
&event
);
119 void OnFullScreen( wxCommandEvent
&event
);
121 wxScrolledWindow
*m_scrolled
;
127 DECLARE_DYNAMIC_CLASS(MyFrame
)
128 DECLARE_EVENT_TABLE()
133 class MyApp
: public wxApp
136 virtual bool OnInit();
145 IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow
, wxScrolledWindow
)
147 BEGIN_EVENT_TABLE(MyScrolledWindow
, wxScrolledWindow
)
148 EVT_PAINT( MyScrolledWindow::OnPaint
)
149 EVT_SIZE( MyScrolledWindow::OnSize
)
152 MyScrolledWindow::MyScrolledWindow( wxWindow
*parent
, wxWindowID id
,
153 const wxPoint
&pos
, const wxSize
&size
)
154 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, _T("test canvas") )
156 MyTopLabels
*top
= new MyTopLabels( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,25) );
157 MyRightLabels
*right
= new MyRightLabels( this, wxID_ANY
, wxDefaultPosition
, wxSize(60,wxDefaultCoord
) );
159 m_canvas
= new MyCanvas( this, top
, right
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
161 SetTargetWindow( m_canvas
);
163 SetBackgroundColour( wxT("WHEAT") );
165 SetCursor( wxCursor( wxCURSOR_HAND
) );
167 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
169 wxBoxSizer
*topsizer
= new wxBoxSizer( wxHORIZONTAL
);
170 topsizer
->Add( 60,25 );
171 topsizer
->Add( top
, 1, wxEXPAND
);
173 mainsizer
->Add( topsizer
, 0, wxEXPAND
);
175 wxBoxSizer
*middlesizer
= new wxBoxSizer( wxHORIZONTAL
);
176 middlesizer
->Add( right
, 0, wxEXPAND
);
177 middlesizer
->Add( m_canvas
, 1, wxEXPAND
);
179 mainsizer
->Add( middlesizer
, 1, wxEXPAND
);
181 SetSizer( mainsizer
);
184 wxSize
MyScrolledWindow::GetSizeAvailableForScrollTarget(const wxSize
& size
)
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
);
194 void MyScrolledWindow::OnSize( wxSizeEvent
&WXUNUSED(event
) )
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.
208 void MyScrolledWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
210 wxPaintDC
dc( this );
213 wxSize size( GetClientSize() );
216 dc.GetTextExtent( wxT("Headline"), &w, &h );
218 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
224 IMPLEMENT_DYNAMIC_CLASS(MyTopLabels
,wxWindow
)
226 BEGIN_EVENT_TABLE(MyTopLabels
, wxWindow
)
227 EVT_PAINT( MyTopLabels::OnPaint
)
230 MyTopLabels::MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
231 : wxWindow( parent
, id
, pos
, size
)
236 void MyTopLabels::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
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 );
245 int xScrollUnits
, xOrigin
;
247 m_owner
->GetViewStart( &xOrigin
, 0 );
248 m_owner
->GetScrollPixelsPerUnit( &xScrollUnits
, 0 );
249 dc
.SetDeviceOrigin( -xOrigin
* xScrollUnits
, 0 );
251 dc
.DrawText( _T("Column 1"), 5, 5 );
252 dc
.DrawText( _T("Column 2"), 105, 5 );
253 dc
.DrawText( _T("Column 3"), 205, 5 );
258 IMPLEMENT_DYNAMIC_CLASS(MyRightLabels
,wxWindow
)
260 BEGIN_EVENT_TABLE(MyRightLabels
, wxWindow
)
261 EVT_PAINT( MyRightLabels::OnPaint
)
264 MyRightLabels::MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
265 : wxWindow( parent
, id
, pos
, size
)
270 void MyRightLabels::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
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 );
279 int yScrollUnits
, yOrigin
;
281 m_owner
->GetViewStart( 0, &yOrigin
);
282 m_owner
->GetScrollPixelsPerUnit( 0, &yScrollUnits
);
283 dc
.SetDeviceOrigin( 0, -yOrigin
* yScrollUnits
);
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 );
295 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxPanel
)
297 BEGIN_EVENT_TABLE(MyCanvas
, wxPanel
)
298 EVT_PAINT( MyCanvas::OnPaint
)
301 MyCanvas::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") )
307 m_rightLabels
= right
;
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) );
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) );
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));
318 SetBackgroundColour( wxT("WHEAT") );
320 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
323 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
325 wxPaintDC
dc( this );
326 m_owner
->PrepareDC( dc
);
328 dc
.SetPen( *wxBLACK_PEN
);
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).
336 // We can query how much the window has been scrolled
337 // by calling CalcUnscrolledPosition()
341 m_owner
->CalcUnscrolledPosition( scroll_x
, scroll_y
, &scroll_x
, &scroll_y
);
343 // We also need to know the size of the window to see which
344 // cells are completely hidden and not get redrawn
348 GetClientSize( &size_x
, &size_y
);
350 // First cell: (0,0)(100,25)
352 if ((0+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
353 (0-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
355 // Has the region on screen been exposed?
356 if (IsExposed(0,0,100,25))
358 wxLogMessage( wxT("Redraw first cell") );
359 dc
.DrawRectangle( 0, 0, 100, 25 );
360 dc
.DrawText( _T("First Cell"), 5, 5 );
365 // Second cell: (0,200)(100,25)
367 if ((200+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
368 (200-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
370 // Has the region on screen been exposed?
371 if (IsExposed(200,0,100,25))
373 wxLogMessage( wxT("Redraw second cell") );
374 dc
.DrawRectangle( 200, 0, 100, 25 );
375 dc
.DrawText( _T("Second Cell"), 205, 5 );
381 void MyCanvas::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
383 wxPanel::ScrollWindow( dx
, dy
, rect
);
384 m_topLabels
->ScrollWindow( dx
, 0, rect
);
385 m_rightLabels
->ScrollWindow( 0, dy
, rect
);
390 const int ID_QUIT
= wxID_EXIT
;
391 const int ID_FULL
= 109;
392 const int ID_ABOUT
= wxID_ABOUT
;
394 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
396 BEGIN_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
)
403 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxScrolledWindow sample"),
404 wxPoint(20,20), wxSize(470,500) )
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"));
411 wxMenuBar
*menu_bar
= new wxMenuBar();
412 menu_bar
->Append(file_menu
, _T("&File"));
414 SetMenuBar( menu_bar
);
418 int widths
[] = { -1, 100 };
419 SetStatusWidths( 2, widths
);
420 #endif // wxUSE_STATUSBAR
422 m_scrolled
= new MyScrolledWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(100,100) );
423 m_scrolled
->SetScrollbars( 10, 10, 50, 50 );
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
) );
431 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
432 topsizer
->Add( m_scrolled
, 1, wxEXPAND
);
434 topsizer
->Add( m_log
, 0, wxEXPAND
);
437 SetSizer( topsizer
);
440 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
445 void MyFrame::OnFullScreen( wxCommandEvent
&WXUNUSED(event
) )
447 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER
|wxFULLSCREEN_NOCAPTION
);
450 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
452 (void)wxMessageBox( _T("wxScroll demo II\n")
453 _T("Robert Roebling (c) 1998"),
454 _T("About wxScroll II Demo"), wxICON_INFORMATION
| wxOK
);
457 //-----------------------------------------------------------------------------
459 //-----------------------------------------------------------------------------
463 if ( !wxApp::OnInit() )
466 wxFrame
*frame
= new MyFrame();