]>
git.saurik.com Git - wxWidgets.git/blob - samples/scroll/scroll.cpp
4 * Author: Robert Roebling
6 * Copyright: (C) 1998, Robert Roebling
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
23 #include "wx/listctrl.h"
35 class MyCanvas
: public wxScrolledWindow
39 MyCanvas( wxWindow
*parent
, wxWindowID
, const wxPoint
&pos
, const wxSize
&size
);
41 void OnPaint( wxPaintEvent
&event
);
42 void OnQueryPosition( wxCommandEvent
&event
);
43 void OnAddButton( wxCommandEvent
&event
);
44 void OnDeleteButton( wxCommandEvent
&event
);
45 void OnMoveButton( wxCommandEvent
&event
);
46 void OnScrollWin( wxCommandEvent
&event
);
47 void OnMouseDown( wxMouseEvent
&event
);
51 DECLARE_DYNAMIC_CLASS(MyCanvas
)
56 // ----------------------------------------------------------------------------
57 // Autoscrolling example.
58 // ----------------------------------------------------------------------------
60 // this class uses the 'virtual' size attribute along with an internal
61 // sizer to automatically set up scrollbars as needed
63 class MyAutoScrollWindow
: public wxScrolledWindow
71 MyAutoScrollWindow( wxWindow
*parent
);
73 void OnResizeClick( wxCommandEvent
&WXUNUSED( event
) );
79 // ----------------------------------------------------------------------------
80 // MyScrolledWindow classes: examples of wxScrolledWindow usage
81 // ----------------------------------------------------------------------------
83 // base class for both of them
84 class MyScrolledWindowBase
: public wxScrolledWindow
87 MyScrolledWindowBase(wxWindow
*parent
)
88 : wxScrolledWindow(parent
)
92 dc
.GetTextExtent(_T("Line 17"), NULL
, &m_hLine
);
96 // the height of one line on screen
99 // the number of lines we draw
103 // this class does "stupid" redrawing - it redraws everything each time
104 // and sets the scrollbar extent directly.
106 class MyScrolledWindowDumb
: public MyScrolledWindowBase
109 MyScrolledWindowDumb(wxWindow
*parent
) : MyScrolledWindowBase(parent
)
112 SetScrollbars(0, m_hLine
, 0, m_nLines
+ 1, 0, 0, TRUE
/* no refresh */);
115 virtual void OnDraw(wxDC
& dc
);
118 // this class does "smart" redrawing - only redraws the lines which must be
119 // redrawn and sets the scroll rate and virtual size to affect the
122 // Note that this class should produce identical results to the one above.
124 class MyScrolledWindowSmart
: public MyScrolledWindowBase
127 MyScrolledWindowSmart(wxWindow
*parent
) : MyScrolledWindowBase(parent
)
130 SetScrollRate( 0, m_hLine
);
131 SetVirtualSize( -1, ( m_nLines
+ 1 ) * m_hLine
);
134 virtual void OnDraw(wxDC
& dc
);
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 class MyFrame
: public wxFrame
147 void OnAbout( wxCommandEvent
&event
);
148 void OnQuit( wxCommandEvent
&event
);
149 void OnDeleteAll( wxCommandEvent
&event
);
150 void OnInsertNew( wxCommandEvent
&event
);
155 DECLARE_DYNAMIC_CLASS(MyFrame
)
156 DECLARE_EVENT_TABLE()
161 class MyApp
: public wxApp
164 virtual bool OnInit();
173 const long ID_ADDBUTTON
= wxNewId();
174 const long ID_DELBUTTON
= wxNewId();
175 const long ID_MOVEBUTTON
= wxNewId();
176 const long ID_SCROLLWIN
= wxNewId();
177 const long ID_QUERYPOS
= wxNewId();
179 const long ID_NEWBUTTON
= wxNewId();
183 IMPLEMENT_DYNAMIC_CLASS(MyCanvas
, wxScrolledWindow
)
185 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
186 EVT_PAINT( MyCanvas::OnPaint
)
187 EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown
)
188 EVT_BUTTON( ID_QUERYPOS
, MyCanvas::OnQueryPosition
)
189 EVT_BUTTON( ID_ADDBUTTON
, MyCanvas::OnAddButton
)
190 EVT_BUTTON( ID_DELBUTTON
, MyCanvas::OnDeleteButton
)
191 EVT_BUTTON( ID_MOVEBUTTON
, MyCanvas::OnMoveButton
)
192 EVT_BUTTON( ID_SCROLLWIN
, MyCanvas::OnScrollWin
)
195 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
196 const wxPoint
&pos
, const wxSize
&size
)
197 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
, _T("test canvas") )
199 SetScrollRate( 10, 10 );
200 SetVirtualSize( 500, 1000 );
202 (void) new wxButton( this, ID_ADDBUTTON
, _T("add button"), wxPoint(10,10) );
203 (void) new wxButton( this, ID_DELBUTTON
, _T("del button"), wxPoint(10,40) );
204 (void) new wxButton( this, ID_MOVEBUTTON
, _T("move button"), wxPoint(150,10) );
205 (void) new wxButton( this, ID_SCROLLWIN
, _T("scroll win"), wxPoint(250,10) );
218 m_button
= new wxButton( this, ID_QUERYPOS
, "Query position", wxPoint(10,110) );
220 (void) new wxTextCtrl( this, -1, "wxTextCtrl", wxPoint(10,150), wxSize(80,-1) );
222 (void) new wxRadioButton( this, -1, "Disable", wxPoint(10,190) );
224 (void) new wxComboBox( this, -1, "This", wxPoint(10,230), wxDefaultSize
, 5, choices
);
226 (void) new wxRadioBox( this, -1, "This", wxPoint(10,310), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_COLS
);
228 (void) new wxRadioBox( this, -1, "This", wxPoint(10,440), wxDefaultSize
, 5, choices
, 2, wxRA_SPECIFY_ROWS
);
230 wxListCtrl
*m_listCtrl
= new wxListCtrl(
231 this, -1, wxPoint(200, 110), wxSize(180, 120),
232 wxLC_REPORT
| wxSIMPLE_BORDER
| wxLC_SINGLE_SEL
);
234 m_listCtrl
->InsertColumn(0, "First", wxLIST_FORMAT_LEFT
, 90);
235 m_listCtrl
->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT
, 90);
237 for ( int i
=0; i
< 30; i
++)
240 sprintf(buf
, "Item %d", i
);
241 m_listCtrl
->InsertItem(i
, buf
);
243 m_listCtrl
->SetItemState( 3, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
245 (void) new wxListBox( this, -1, wxPoint(260,280), wxSize(120,120), 5, choices
, wxLB_ALWAYS_SB
);
249 wxPanel
*test
= new wxPanel( this, -1, wxPoint(10, 110), wxSize(130,50), wxSIMPLE_BORDER
| wxTAB_TRAVERSAL
);
250 test
->SetBackgroundColour( wxT("WHEAT") );
254 wxButton
*test2
= new wxButton( test
, -1, "Hallo", wxPoint(10,10) );
256 test
= new wxPanel( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
257 test
->SetBackgroundColour( wxT("WHEAT") );
258 test
->SetCursor( wxCursor( wxCURSOR_NO_ENTRY
) );
259 test2
= new wxButton( test
, -1, "Hallo", wxPoint(10,10) );
260 test2
->SetCursor( wxCursor( wxCURSOR_PENCIL
) );
262 test
= new wxPanel( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER
| wxTAB_TRAVERSAL
);
263 test
->SetBackgroundColour( wxT("WHEAT") );
264 test
->SetCursor( wxCursor( wxCURSOR_PENCIL
) );
265 test2
= new wxButton( test
, -1, "Hallo", wxPoint(10,10) );
266 test2
->SetCursor( wxCursor( wxCURSOR_NO_ENTRY
) );
270 SetBackgroundColour( wxT("BLUE") );
272 SetCursor( wxCursor( wxCURSOR_IBEAM
) );
275 MyCanvas::~MyCanvas()
279 void MyCanvas::OnMouseDown( wxMouseEvent
&event
)
281 if (event
.LeftDown())
283 wxPoint
pt( event
.GetPosition() );
285 CalcUnscrolledPosition( pt
.x
, pt
.y
, &x
, &y
);
286 wxLogMessage( wxT("Mouse down event at: %d %d, scrolled: %d %d"), pt
.x
, pt
.y
, x
, y
);
288 if ( !event
.LeftIsDown() )
289 wxLogMessage( wxT("Error: LeftIsDown() should be TRUE if for LeftDown()") );
293 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
295 wxPaintDC
dc( this );
298 dc
.DrawText( _T("Press mouse button to test calculations!"), 160, 50 );
300 dc
.DrawText( _T("Some text"), 140, 140 );
302 dc
.DrawRectangle( 100, 160, 200, 200 );
305 void MyCanvas::OnQueryPosition( wxCommandEvent
&WXUNUSED(event
) )
307 wxPoint
pt( m_button
->GetPosition() );
308 wxLogMessage( wxT("Position of \"Query position\" is %d %d"), pt
.x
, pt
.y
);
309 pt
= ClientToScreen( pt
);
310 wxLogMessage( wxT("Position of \"Query position\" on screen is %d %d"), pt
.x
, pt
.y
);
313 void MyCanvas::OnAddButton( wxCommandEvent
&WXUNUSED(event
) )
315 wxLogMessage( wxT("Inserting button at position 10,70...") );
316 wxButton
*button
= new wxButton( this, ID_NEWBUTTON
, wxT("new button"), wxPoint(10,70), wxSize(80,25) );
317 wxPoint
pt( button
->GetPosition() );
318 wxLogMessage( wxT("-> Position after inserting %d %d"), pt
.x
, pt
.y
);
321 void MyCanvas::OnDeleteButton( wxCommandEvent
&WXUNUSED(event
) )
323 wxLogMessage( wxT("Deleting button inserted with \"Add button\"...") );
324 wxWindow
*win
= FindWindow( ID_NEWBUTTON
);
328 wxLogMessage( wxT("-> No window with id = ID_NEWBUTTON found.") );
331 void MyCanvas::OnMoveButton( wxCommandEvent
&event
)
333 wxLogMessage( wxT("Moving button 10 pixels downward..") );
334 wxWindow
*win
= FindWindow( event
.GetId() );
335 wxPoint
pt( win
->GetPosition() );
336 wxLogMessage( wxT("-> Position before move is %d %d"), pt
.x
, pt
.y
);
337 win
->Move( -1, pt
.y
+ 10 );
338 pt
= win
->GetPosition();
339 wxLogMessage( wxT("-> Position after move is %d %d"), pt
.x
, pt
.y
);
342 void MyCanvas::OnScrollWin( wxCommandEvent
&WXUNUSED(event
) )
344 wxLogMessage( wxT("Scrolling 2 units up.\nThe white square and the controls should move equally!") );
346 GetViewStart( &x
, &y
);
350 // MyAutoScrollWindow
352 const long ID_RESIZEBUTTON
= wxNewId();
353 const wxSize
SMALL_BUTTON( 100, 50 );
354 const wxSize
LARGE_BUTTON( 300, 100 );
356 BEGIN_EVENT_TABLE( MyAutoScrollWindow
, wxScrolledWindow
)
357 EVT_BUTTON( ID_RESIZEBUTTON
, MyAutoScrollWindow::OnResizeClick
)
360 MyAutoScrollWindow::MyAutoScrollWindow( wxWindow
*parent
)
361 : wxScrolledWindow( parent
)
363 SetBackgroundColour( wxT("GREEN") );
365 // Set the rate we'd like for scrolling.
367 SetScrollRate( 5, 5 );
369 // Populate a sizer with a 'resizing' button and some
370 // other static decoration
372 wxFlexGridSizer
*innersizer
= new wxFlexGridSizer( 2, 2 );
374 m_button
= new wxButton( this,
380 // We need to do this here, because wxADJUST_MINSIZE below
381 // will cause the initial size to be ignored for Best/Min size.
382 // It would be nice to fix the sizers to handle this a little
385 m_button
->SetSizeHints( SMALL_BUTTON
.GetWidth(), SMALL_BUTTON
.GetHeight() );
387 innersizer
->Add( m_button
,
389 wxALIGN_CENTER
| wxALL
| wxADJUST_MINSIZE
,
392 innersizer
->Add( new wxStaticText( this, -1, _T("This is just") ),
396 innersizer
->Add( new wxStaticText( this, -1, _T("some decoration") ),
400 innersizer
->Add( new wxStaticText( this, -1, _T("for you to scroll...") ),
404 // Then use the sizer to set the scrolled region size.
406 SetSizer( innersizer
);
409 void MyAutoScrollWindow::OnResizeClick( wxCommandEvent
&WXUNUSED( event
) )
411 // Arbitrarily resize the button to change the minimum size of
412 // the (scrolled) sizer.
414 if( m_button
->GetSize() == SMALL_BUTTON
)
415 m_button
->SetSizeHints( LARGE_BUTTON
.GetWidth(), LARGE_BUTTON
.GetHeight() );
417 m_button
->SetSizeHints( SMALL_BUTTON
.GetWidth(), SMALL_BUTTON
.GetHeight() );
419 // Force update layout and scrollbars, since nothing we do here
420 // necessarily generates a size event which would do it for us.
427 const long ID_QUIT
= wxNewId();
428 const long ID_ABOUT
= wxNewId();
429 const long ID_DELETE_ALL
= wxNewId();
430 const long ID_INSERT_NEW
= wxNewId();
432 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
434 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
435 EVT_MENU (ID_DELETE_ALL
, MyFrame::OnDeleteAll
)
436 EVT_MENU (ID_INSERT_NEW
, MyFrame::OnInsertNew
)
437 EVT_MENU (ID_ABOUT
, MyFrame::OnAbout
)
438 EVT_MENU (ID_QUIT
, MyFrame::OnQuit
)
442 : wxFrame( (wxFrame
*)NULL
, -1, _T("wxScrolledWindow sample"),
443 wxPoint(20,20), wxSize(470,500) )
445 wxMenu
*file_menu
= new wxMenu();
446 file_menu
->Append( ID_DELETE_ALL
, _T("Delete all"));
447 file_menu
->Append( ID_INSERT_NEW
, _T("Insert new"));
448 file_menu
->Append( ID_ABOUT
, _T("&About.."));
449 file_menu
->Append( ID_QUIT
, _T("E&xit\tAlt-X"));
451 wxMenuBar
*menu_bar
= new wxMenuBar();
452 menu_bar
->Append(file_menu
, _T("&File"));
454 SetMenuBar( menu_bar
);
457 int widths
[] = { -1, 100 };
458 SetStatusWidths( 2, widths
);
460 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
462 // Setting an explicit size here is superfluous, it will be overridden
463 // by the sizer in any case.
464 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(100,100) );
466 // This is done with ScrollRate/VirtualSize in MyCanvas ctor now,
467 // both should produce identical results.
468 //m_canvas->SetScrollbars( 10, 10, 50, 100 );
470 topsizer
->Add( m_canvas
, 1, wxEXPAND
);
471 topsizer
->Add( new MyAutoScrollWindow( this ), 1, wxEXPAND
);
473 wxSizer
*sizerBtm
= new wxBoxSizer(wxHORIZONTAL
);
474 sizerBtm
->Add( new MyScrolledWindowDumb(this), 1, wxEXPAND
);
475 sizerBtm
->Add( new MyScrolledWindowSmart(this), 1, wxEXPAND
);
476 topsizer
->Add( sizerBtm
, 1, wxEXPAND
);
478 SetSizer( topsizer
);
481 void MyFrame::OnDeleteAll( wxCommandEvent
&WXUNUSED(event
) )
483 m_canvas
->DestroyChildren();
486 void MyFrame::OnInsertNew( wxCommandEvent
&WXUNUSED(event
) )
488 (void)new wxButton( m_canvas
, -1, _T("Hello"), wxPoint(100,100) );
491 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
496 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
498 (void)wxMessageBox( _T("wxScroll demo\n"
499 "Robert Roebling (c) 1998\n"
500 "Autoscrolling examples\n"
502 _T("About wxScroll Demo"),
503 wxICON_INFORMATION
| wxOK
);
506 //-----------------------------------------------------------------------------
508 //-----------------------------------------------------------------------------
512 wxFrame
*frame
= new MyFrame();
518 // ----------------------------------------------------------------------------
519 // MyScrolledWindowXXX
520 // ----------------------------------------------------------------------------
522 void MyScrolledWindowDumb::OnDraw(wxDC
& dc
)
524 // this is useful to see which lines are redrawn
525 static size_t s_redrawCount
= 0;
526 dc
.SetTextForeground(s_redrawCount
++ % 2 ? *wxRED
: *wxBLUE
);
529 for ( size_t line
= 0; line
< m_nLines
; line
++ )
532 CalcScrolledPosition(0, y
, NULL
, &yPhys
);
534 dc
.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
535 line
, y
, yPhys
), 0, y
);
540 void MyScrolledWindowSmart::OnDraw(wxDC
& dc
)
542 // this is useful to see which lines are redrawn
543 static size_t s_redrawCount
= 0;
544 dc
.SetTextForeground(s_redrawCount
++ % 2 ? *wxRED
: *wxBLUE
);
546 // update region is always in device coords, translate to logical ones
547 wxRect rectUpdate
= GetUpdateRegion().GetBox();
548 CalcUnscrolledPosition(rectUpdate
.x
, rectUpdate
.y
,
549 &rectUpdate
.x
, &rectUpdate
.y
);
551 size_t lineFrom
= rectUpdate
.y
/ m_hLine
,
552 lineTo
= rectUpdate
.GetBottom() / m_hLine
;
554 if ( lineTo
> m_nLines
- 1)
555 lineTo
= m_nLines
- 1;
557 wxCoord y
= lineFrom
*m_hLine
;
558 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
561 CalcScrolledPosition(0, y
, NULL
, &yPhys
);
563 dc
.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
564 line
, y
, yPhys
), 0, y
);