]>
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
);
41 ~MyScrolledWindow(){};
42 void OnPaint( wxPaintEvent
&event
);
43 void OnSize( wxSizeEvent
&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
;
123 DECLARE_DYNAMIC_CLASS(MyFrame
)
124 DECLARE_EVENT_TABLE()
129 class MyApp
: public wxApp
132 virtual bool OnInit();
141 IMPLEMENT_DYNAMIC_CLASS(MyScrolledWindow
, wxScrolledWindow
)
143 BEGIN_EVENT_TABLE(MyScrolledWindow
, wxScrolledWindow
)
144 EVT_PAINT( MyScrolledWindow::OnPaint
)
145 EVT_SIZE( MyScrolledWindow::OnSize
)
148 MyScrolledWindow::MyScrolledWindow( wxWindow
*parent
, wxWindowID id
,
149 const wxPoint
&pos
, const wxSize
&size
)
150 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, _T("test canvas") )
152 MyTopLabels
*top
= new MyTopLabels( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,25) );
153 MyRightLabels
*right
= new MyRightLabels( this, wxID_ANY
, wxDefaultPosition
, wxSize(60,wxDefaultCoord
) );
155 m_canvas
= new MyCanvas( this, top
, right
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
);
157 SetTargetWindow( m_canvas
);
159 SetBackgroundColour( wxT("WHEAT") );
161 SetCursor( wxCursor( wxCURSOR_HAND
) );
163 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
165 wxBoxSizer
*topsizer
= new wxBoxSizer( wxHORIZONTAL
);
166 topsizer
->Add( 60,25 );
167 topsizer
->Add( top
, 1, wxEXPAND
);
169 mainsizer
->Add( topsizer
, 0, wxEXPAND
);
171 wxBoxSizer
*middlesizer
= new wxBoxSizer( wxHORIZONTAL
);
172 middlesizer
->Add( right
, 0, wxEXPAND
);
173 middlesizer
->Add( m_canvas
, 1, wxEXPAND
);
175 mainsizer
->Add( middlesizer
, 1, wxEXPAND
);
177 SetAutoLayout( true );
178 SetSizer( mainsizer
);
181 void MyScrolledWindow::OnSize( wxSizeEvent
&WXUNUSED(event
) )
183 // We need to override OnSize so that our scrolled
184 // window a) does call Layout() to use sizers for
185 // positioning the controls but b) does not query
186 // the sizer for their size and use that for setting
187 // the scrollable area as set that ourselves by
188 // calling SetScrollbar() further down.
195 void MyScrolledWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
197 wxPaintDC
dc( this );
200 wxSize size( GetClientSize() );
203 dc.GetTextExtent( wxT("Headline"), &w, &h );
205 dc.DrawText( wxT("Headline"), long (size.x / 2 - w / 2), 10 );
211 IMPLEMENT_DYNAMIC_CLASS(MyTopLabels
,wxWindow
)
213 BEGIN_EVENT_TABLE(MyTopLabels
, wxWindow
)
214 EVT_PAINT( MyTopLabels::OnPaint
)
217 MyTopLabels::MyTopLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
218 : wxWindow( parent
, id
, pos
, size
)
223 void MyTopLabels::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
227 // This is wrong.. it will translate both x and y if the
228 // window is scrolled, the label windows are active in one
229 // direction only. Do the action below instead -- RL.
230 //m_owner->PrepareDC( dc );
232 int xScrollUnits
, xOrigin
;
234 m_owner
->GetViewStart( &xOrigin
, 0 );
235 m_owner
->GetScrollPixelsPerUnit( &xScrollUnits
, 0 );
236 dc
.SetDeviceOrigin( -xOrigin
* xScrollUnits
, 0 );
238 dc
.DrawText( _T("Column 1"), 5, 5 );
239 dc
.DrawText( _T("Column 2"), 105, 5 );
240 dc
.DrawText( _T("Column 3"), 205, 5 );
245 IMPLEMENT_DYNAMIC_CLASS(MyRightLabels
,wxWindow
)
247 BEGIN_EVENT_TABLE(MyRightLabels
, wxWindow
)
248 EVT_PAINT( MyRightLabels::OnPaint
)
251 MyRightLabels::MyRightLabels( wxScrolledWindow
*parent
, wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
252 : wxWindow( parent
, id
, pos
, size
)
257 void MyRightLabels::OnPaint( wxPaintEvent
& WXUNUSED(event
) )
261 // This is wrong.. it will translate both x and y if the
262 // window is scrolled, the label windows are active in one
263 // direction only. Do the action below instead -- RL.
264 //m_owner->PrepareDC( dc );
266 int yScrollUnits
, yOrigin
;
268 m_owner
->GetViewStart( 0, &yOrigin
);
269 m_owner
->GetScrollPixelsPerUnit( 0, &yScrollUnits
);
270 dc
.SetDeviceOrigin( 0, -yOrigin
* yScrollUnits
);
272 dc
.DrawText( _T("Row 1"), 5, 5 );
273 dc
.DrawText( _T("Row 2"), 5, 30 );
274 dc
.DrawText( _T("Row 3"), 5, 55 );
275 dc
.DrawText( _T("Row 4"), 5, 80 );
276 dc
.DrawText( _T("Row 5"), 5, 105 );
277 dc
.DrawText( _T("Row 6"), 5, 130 );
282 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxPanel
)
284 BEGIN_EVENT_TABLE(MyCanvas
, wxPanel
)
285 EVT_PAINT( MyCanvas::OnPaint
)
288 MyCanvas::MyCanvas( wxScrolledWindow
*parent
, MyTopLabels
*top
, MyRightLabels
*right
,
289 wxWindowID id
, const wxPoint
&pos
, const wxSize
&size
)
290 : wxPanel( parent
, id
, pos
, size
, wxSUNKEN_BORDER
, _T("test canvas") )
294 m_rightLabels
= right
;
296 (void)new wxButton( this, wxID_ANY
, _T("Hallo I"), wxPoint(0,50), wxSize(100,25) );
297 (void)new wxButton( this, wxID_ANY
, _T("Hallo II"), wxPoint(200,50), wxSize(100,25) );
299 (void)new wxTextCtrl( this, wxID_ANY
, _T("Text I"), wxPoint(0,100), wxSize(100,25) );
300 (void)new wxTextCtrl( this, wxID_ANY
, _T("Text II"), wxPoint(200,100), wxSize(100,25) );
302 (void)new wxComboBox( this, wxID_ANY
, _T("ComboBox I"), wxPoint(0,150), wxSize(100,25));
303 (void)new wxComboBox( this, wxID_ANY
, _T("ComboBox II"), wxPoint(200,150), wxSize(100,25));
305 SetBackgroundColour( wxT("WHEAT") );
307 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
310 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
312 wxPaintDC
dc( this );
313 m_owner
->PrepareDC( dc
);
315 dc
.SetPen( *wxBLACK_PEN
);
317 // OK, let's assume we are a grid control and we have two
318 // grid cells. Here in OnPaint we want to know which cell
319 // to redraw so that we prevent redrawing cells that don't
320 // need to get redrawn. We have one cell at (0,0) and one
321 // more at (200,0), both having a size of (100,25).
323 // We can query how much the window has been scrolled
324 // by calling CalcUnscrolledPosition()
328 m_owner
->CalcUnscrolledPosition( scroll_x
, scroll_y
, &scroll_x
, &scroll_y
);
330 // We also need to know the size of the window to see which
331 // cells are completely hidden and not get redrawn
335 GetClientSize( &size_x
, &size_y
);
337 // First cell: (0,0)(100,25)
339 if ((0+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
340 (0-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
342 // Has the region on screen been exposed?
343 if (IsExposed(0,0,100,25))
345 wxLogMessage( wxT("Redraw first cell") );
346 dc
.DrawRectangle( 0, 0, 100, 25 );
347 dc
.DrawText( _T("First Cell"), 5, 5 );
352 // Second cell: (0,200)(100,25)
354 if ((200+100-scroll_x
> 0) && (0+25-scroll_y
> 0) &&
355 (200-scroll_x
< size_x
) && (0-scroll_y
< size_y
))
357 // Has the region on screen been exposed?
358 if (IsExposed(200,0,100,25))
360 wxLogMessage( wxT("Redraw second cell") );
361 dc
.DrawRectangle( 200, 0, 100, 25 );
362 dc
.DrawText( _T("Second Cell"), 205, 5 );
368 void MyCanvas::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
370 wxPanel::ScrollWindow( dx
, dy
, rect
);
371 m_topLabels
->ScrollWindow( dx
, 0, rect
);
372 m_rightLabels
->ScrollWindow( 0, dy
, rect
);
377 const int ID_QUIT
= wxID_EXIT
;
378 const int ID_FULL
= 109;
379 const int ID_ABOUT
= wxID_ABOUT
;
381 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
383 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
384 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
385 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
386 EVT_MENU (ID_FULL
, MyFrame::OnFullScreen
)
390 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxScrolledWindow sample"),
391 wxPoint(20,20), wxSize(470,500) )
393 wxMenu
*file_menu
= new wxMenu();
394 file_menu
->Append( ID_ABOUT
, _T("&About..."));
395 file_menu
->Append( ID_FULL
, _T("&Full screen on/off"));
396 file_menu
->Append( ID_QUIT
, _T("E&xit\tAlt-X"));
398 wxMenuBar
*menu_bar
= new wxMenuBar();
399 menu_bar
->Append(file_menu
, _T("&File"));
401 SetMenuBar( menu_bar
);
405 int widths
[] = { -1, 100 };
406 SetStatusWidths( 2, widths
);
407 #endif // wxUSE_STATUSBAR
409 m_scrolled
= new MyScrolledWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(100,100) );
410 m_scrolled
->SetScrollbars( 10, 10, 50, 50 );
413 m_log
= new wxTextCtrl( this, wxID_ANY
, _T("This is the log window.\n"), wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE
);
414 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
418 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
419 topsizer
->Add( m_scrolled
, 1, wxEXPAND
);
421 topsizer
->Add( m_log
, 0, wxEXPAND
);
424 SetAutoLayout( true );
425 SetSizer( topsizer
);
428 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
433 void MyFrame::OnFullScreen( wxCommandEvent
&WXUNUSED(event
) )
435 ShowFullScreen( !IsFullScreen(), wxFULLSCREEN_NOBORDER
|wxFULLSCREEN_NOCAPTION
);
438 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
440 (void)wxMessageBox( _T("wxScroll demo II\n")
441 _T("Robert Roebling (c) 1998"),
442 _T("About wxScroll II Demo"), wxICON_INFORMATION
| wxOK
);
445 //-----------------------------------------------------------------------------
447 //-----------------------------------------------------------------------------
451 wxFrame
*frame
= new MyFrame();