X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a3e7d24d21f5371c88caffe4a85c8e4a5362db3f..14dd645ee16eb022afb19f35d0c8cfdf2d3cdee9:/samples/scroll/scroll.cpp?ds=sidebyside diff --git a/samples/scroll/scroll.cpp b/samples/scroll/scroll.cpp index d1a03c1ab6..8216846041 100644 --- a/samples/scroll/scroll.cpp +++ b/samples/scroll/scroll.cpp @@ -34,7 +34,7 @@ class MyApp; class MyCanvas: public wxScrolledWindow { public: - MyCanvas() {}; + MyCanvas() {} MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); ~MyCanvas(); void OnPaint( wxPaintEvent &event ); @@ -51,7 +51,54 @@ public: DECLARE_EVENT_TABLE() }; +// ---------------------------------------------------------------------------- +// MyScrolledWindow classes: examples of wxScrolledWindow usage +// ---------------------------------------------------------------------------- + +// base class for both of them +class MyScrolledWindowBase : public wxScrolledWindow +{ +public: + MyScrolledWindowBase(wxWindow *parent) : wxScrolledWindow(parent) + { + m_nLines = 100; + + InitScrollbars(); + } + +protected: + // set the scrollbar params + void InitScrollbars(); + + // the height of one line on screen + wxCoord m_hLine; + + // the number of lines we draw + size_t m_nLines; +}; + +// this class does "stupid" redrawing - it redraws everything each time +class MyScrolledWindowDumb : public MyScrolledWindowBase +{ +public: + MyScrolledWindowDumb(wxWindow *parent) : MyScrolledWindowBase(parent) { } + + virtual void OnDraw(wxDC& dc); +}; + +// this class does "smart" redrawing - only redraws the lines which must be +// redrawn +class MyScrolledWindowSmart : public MyScrolledWindowBase +{ +public: + MyScrolledWindowSmart(wxWindow *parent) : MyScrolledWindowBase(parent) { } + + virtual void OnDraw(wxDC& dc); +}; + +// ---------------------------------------------------------------------------- // MyFrame +// ---------------------------------------------------------------------------- class MyFrame: public wxFrame { @@ -60,6 +107,8 @@ public: void OnAbout( wxCommandEvent &event ); void OnQuit( wxCommandEvent &event ); + void OnDeleteAll( wxCommandEvent &event ); + void OnInsertNew( wxCommandEvent &event ); MyCanvas *m_canvas; wxTextCtrl *m_log; @@ -113,6 +162,8 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, (void) new wxButton( this, ID_MOVEBUTTON, "move button", wxPoint(150,10) ); (void) new wxButton( this, ID_SCROLLWIN, "scroll win", wxPoint(250,10) ); +#if 0 + wxString choices[] = { "This", @@ -121,22 +172,22 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, "wonderful", "examples." }; - + m_button = new wxButton( this, ID_QUERYPOS, "Query position", wxPoint(10,110) ); - + (void) new wxTextCtrl( this, -1, "wxTextCtrl", wxPoint(10,150), wxSize(80,-1) ); - + (void) new wxRadioButton( this, -1, "Disable", wxPoint(10,190) ); - + (void) new wxComboBox( this, -1, "This", wxPoint(10,230), wxDefaultSize, 5, choices ); - + (void) new wxRadioBox( this, -1, "This", wxPoint(10,310), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_COLS ); (void) new wxRadioBox( this, -1, "This", wxPoint(10,440), wxDefaultSize, 5, choices, 2, wxRA_SPECIFY_ROWS ); wxListCtrl *m_listCtrl = new wxListCtrl( - this, -1, wxPoint(200, 110), wxSize(180, 120), - wxLC_REPORT | wxSIMPLE_BORDER | wxLC_SINGLE_SEL ); + this, -1, wxPoint(200, 110), wxSize(180, 120), + wxLC_REPORT | wxSIMPLE_BORDER | wxLC_SINGLE_SEL ); m_listCtrl->InsertColumn(0, "First", wxLIST_FORMAT_LEFT, 90); m_listCtrl->InsertColumn(1, "Last", wxLIST_FORMAT_LEFT, 90); @@ -151,24 +202,31 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, (void) new wxListBox( this, -1, wxPoint(260,280), wxSize(120,120), 5, choices, wxLB_ALWAYS_SB ); - wxWindow *test = new wxWindow( this, -1, wxPoint(10, 530), wxSize(130,120), wxSIMPLE_BORDER | wxTAB_TRAVERSAL ); +#endif + + wxPanel *test = new wxPanel( this, -1, wxPoint(10, 110), wxSize(130,50), wxSIMPLE_BORDER | wxTAB_TRAVERSAL ); test->SetBackgroundColour( "WHEAT" ); + +#if 0 + wxButton *test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) ); - - test = new wxWindow( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER | wxTAB_TRAVERSAL ); + + test = new wxPanel( this, -1, wxPoint(160, 530), wxSize(130,120), wxSUNKEN_BORDER | wxTAB_TRAVERSAL ); test->SetBackgroundColour( "WHEAT" ); test->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) ); test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) ); test2->SetCursor( wxCursor( wxCURSOR_PENCIL ) ); - - test = new wxWindow( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER | wxTAB_TRAVERSAL ); + + test = new wxPanel( this, -1, wxPoint(310, 530), wxSize(130,120), wxRAISED_BORDER | wxTAB_TRAVERSAL ); test->SetBackgroundColour( "WHEAT" ); test->SetCursor( wxCursor( wxCURSOR_PENCIL ) ); test2 = new wxButton( test, -1, "Hallo", wxPoint(10,10) ); test2->SetCursor( wxCursor( wxCURSOR_NO_ENTRY ) ); - SetBackgroundColour( "WHEAT" ); - +#endif + + SetBackgroundColour( "BLUE" ); + SetCursor( wxCursor( wxCURSOR_IBEAM ) ); } @@ -183,13 +241,10 @@ void MyCanvas::OnMouseDown( wxMouseEvent &event ) wxPoint pt( event.GetPosition() ); int x,y; CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); - wxLogMessage( "Mouse down event at: %d %d, scrolled: %d %d", pt.x, pt.y, x, y ); - } - - if (event.LeftIsDown() && - event.LeftDown()) - { - wxLogMessage( "Error: both LeftDown() and LeftIsDown() are TRUE!" ); + wxLogMessage( wxT("Mouse down event at: %d %d, scrolled: %d %d"), pt.x, pt.y, x, y ); + + if ( !event.LeftIsDown() ) + wxLogMessage( wxT("Error: LeftIsDown() should be TRUE if for LeftDown()") ); } } @@ -201,52 +256,52 @@ void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) dc.DrawText( "Press mouse button to test calculations!", 160, 50 ); dc.DrawText( "Some text", 140, 140 ); - + dc.DrawRectangle( 100, 160, 200, 200 ); } void MyCanvas::OnQueryPosition( wxCommandEvent &WXUNUSED(event) ) { wxPoint pt( m_button->GetPosition() ); - wxLogMessage( "Position of ""Query position"" is %d %d", pt.x, pt.y ); + wxLogMessage( wxT("Position of \"Query position\" is %d %d"), pt.x, pt.y ); pt = ClientToScreen( pt ); - wxLogMessage( "Position of ""Query position"" on screen is %d %d", pt.x, pt.y ); + wxLogMessage( wxT("Position of \"Query position\" on screen is %d %d"), pt.x, pt.y ); } void MyCanvas::OnAddButton( wxCommandEvent &WXUNUSED(event) ) { - wxLogMessage( "Inserting button at position 10,70..." ); + wxLogMessage( wxT("Inserting button at position 10,70...") ); wxButton *button = new wxButton( this, ID_NEWBUTTON, "new button", wxPoint(10,70), wxSize(80,25) ); wxPoint pt( button->GetPosition() ); - wxLogMessage( "-> Position after inserting %d %d", pt.x, pt.y ); + wxLogMessage( wxT("-> Position after inserting %d %d"), pt.x, pt.y ); } void MyCanvas::OnDeleteButton( wxCommandEvent &event ) { - wxLogMessage( "Deleting button inserted with ""Add button""..." ); + wxLogMessage( wxT("Deleting button inserted with \"Add button\"...") ); wxWindow *win = FindWindow( ID_NEWBUTTON ); if (win) win->Destroy(); else - wxLogMessage( "-> No window with id = ID_NEWBUTTON found." ); + wxLogMessage( wxT("-> No window with id = ID_NEWBUTTON found.") ); } void MyCanvas::OnMoveButton( wxCommandEvent &event ) { - wxLogMessage( "Moving button 10 pixels downward.." ); + wxLogMessage( wxT("Moving button 10 pixels downward..") ); wxWindow *win = FindWindow( event.GetId() ); wxPoint pt( win->GetPosition() ); - wxLogMessage( "-> Position before move is %d %d", pt.x, pt.y ); + wxLogMessage( wxT("-> Position before move is %d %d"), pt.x, pt.y ); win->Move( -1, pt.y + 10 ); pt = win->GetPosition(); - wxLogMessage( "-> Position after move is %d %d", pt.x, pt.y ); + wxLogMessage( wxT("-> Position after move is %d %d"), pt.x, pt.y ); } void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) ) { - wxLogMessage( "Scrolling 2 units up.\nThe white square and the controls should move equally!" ); + wxLogMessage( wxT("Scrolling 2 units up.\nThe white square and the controls should move equally!") ); int x,y; - ViewStart( &x, &y ); + GetViewStart( &x, &y ); Scroll( -1, y+2 ); } @@ -254,10 +309,14 @@ void MyCanvas::OnScrollWin( wxCommandEvent &WXUNUSED(event) ) const int ID_QUIT = 108; const int ID_ABOUT = 109; +const int ID_DELETE_ALL = 110; +const int ID_INSERT_NEW = 111; IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) BEGIN_EVENT_TABLE(MyFrame,wxFrame) + EVT_MENU (ID_DELETE_ALL, MyFrame::OnDeleteAll) + EVT_MENU (ID_INSERT_NEW, MyFrame::OnInsertNew) EVT_MENU (ID_ABOUT, MyFrame::OnAbout) EVT_MENU (ID_QUIT, MyFrame::OnQuit) END_EVENT_TABLE() @@ -267,6 +326,8 @@ MyFrame::MyFrame() wxPoint(20,20), wxSize(470,500) ) { wxMenu *file_menu = new wxMenu(); + file_menu->Append( ID_DELETE_ALL, "Delete all"); + file_menu->Append( ID_INSERT_NEW, "Insert new"); file_menu->Append( ID_ABOUT, "&About.."); file_menu->Append( ID_QUIT, "E&xit\tAlt-X"); @@ -281,20 +342,30 @@ MyFrame::MyFrame() m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(100,100) ); m_canvas->SetScrollbars( 10, 10, 50, 100 ); - - m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(0,0), wxSize(100,100), wxTE_MULTILINE ); - wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); - delete old_log; - + wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); - + topsizer->Add( m_canvas, 1, wxEXPAND ); - topsizer->Add( m_log, 0, wxEXPAND ); + + wxSizer *sizerBtm = new wxBoxSizer(wxHORIZONTAL); + sizerBtm->Add( new MyScrolledWindowDumb(this), 1, wxEXPAND ); + sizerBtm->Add( new MyScrolledWindowSmart(this), 1, wxEXPAND ); + topsizer->Add( sizerBtm, 1, wxEXPAND ); SetAutoLayout( TRUE ); SetSizer( topsizer ); } +void MyFrame::OnDeleteAll( wxCommandEvent &WXUNUSED(event) ) +{ + m_canvas->DestroyChildren(); +} + +void MyFrame::OnInsertNew( wxCommandEvent &WXUNUSED(event) ) +{ + (void)new wxButton( m_canvas, -1, "Hello", wxPoint(100,100) ); +} + void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) { Close( TRUE ); @@ -319,3 +390,62 @@ bool MyApp::OnInit() return TRUE; } +// ---------------------------------------------------------------------------- +// MyScrolledWindowXXX +// ---------------------------------------------------------------------------- + +void MyScrolledWindowBase::InitScrollbars() +{ + wxClientDC dc(this); + dc.GetTextExtent(_T("Line 17"), NULL, &m_hLine); + + // no horz scrolling + SetScrollbars(0, m_hLine, 0, m_nLines + 1, 0, 0, TRUE /* no refresh */); +} + +void MyScrolledWindowDumb::OnDraw(wxDC& dc) +{ + // this is useful to see which lines are redrawn + static size_t s_redrawCount = 0; + dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE); + + wxCoord y = 0; + for ( size_t line = 0; line < m_nLines; line++ ) + { + wxCoord yPhys; + CalcScrolledPosition(0, y, NULL, &yPhys); + + dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"), + line, y, yPhys), 0, y); + y += m_hLine; + } +} + +void MyScrolledWindowSmart::OnDraw(wxDC& dc) +{ + // this is useful to see which lines are redrawn + static size_t s_redrawCount = 0; + dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE); + + // update region is always in device coords, translate to logical ones + wxRect rectUpdate = GetUpdateRegion().GetBox(); + CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y, + &rectUpdate.x, &rectUpdate.y); + + size_t lineFrom = rectUpdate.y / m_hLine, + lineTo = rectUpdate.GetBottom() / m_hLine; + + if ( lineTo > m_nLines - 1) + lineTo = m_nLines - 1; + + wxCoord y = lineFrom*m_hLine; + for ( size_t line = lineFrom; line <= lineTo; line++ ) + { + wxCoord yPhys; + CalcScrolledPosition(0, y, NULL, &yPhys); + + dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"), + line, y, yPhys), 0, y); + y += m_hLine; + } +}