X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/91b073576e8fd216c71acacf5c1495e1a56dd39f..16bf3190407e402c8c40d3eed43c42f52f297684:/samples/scroll/scroll.cpp diff --git a/samples/scroll/scroll.cpp b/samples/scroll/scroll.cpp index bf5327cd11..c879b87686 100644 --- a/samples/scroll/scroll.cpp +++ b/samples/scroll/scroll.cpp @@ -25,6 +25,104 @@ #include "wx/sizer.h" #include "wx/log.h" +const long ID_QUIT = wxID_EXIT; +const long ID_ABOUT = wxID_ABOUT; +const long ID_DELETE_ALL = 100; +const long ID_INSERT_NEW = 101; + +// ---------------------------------------------------------------------- +// a trivial example +// ---------------------------------------------------------------------- + +class MySimpleFrame; +class MySimpleCanvas; + +// MySimpleCanvas + +class MySimpleCanvas: public wxScrolledWindow +{ +public: + MySimpleCanvas() { } + MySimpleCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size ); + + void OnPaint( wxPaintEvent &event ); + +private: + DECLARE_DYNAMIC_CLASS(MyCanvas) + DECLARE_EVENT_TABLE() +}; + +IMPLEMENT_DYNAMIC_CLASS(MySimpleCanvas, wxScrolledWindow) + +BEGIN_EVENT_TABLE(MySimpleCanvas, wxScrolledWindow) + EVT_PAINT( MySimpleCanvas::OnPaint) +END_EVENT_TABLE() + +MySimpleCanvas::MySimpleCanvas( wxWindow *parent, wxWindowID id, + const wxPoint &pos, const wxSize &size ) + : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER, _T("test canvas") ) +{ + SetScrollRate( 10, 10 ); + SetVirtualSize( 92, 97 ); + SetBackgroundColour( *wxWHITE ); +} + +void MySimpleCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) +{ + wxPaintDC dc(this); + PrepareDC( dc ); + + dc.SetPen( *wxRED_PEN ); + dc.SetBrush( *wxTRANSPARENT_BRUSH ); + dc.DrawRectangle( 0,0,92,97 ); +} + +// MySimpleFrame + +class MySimpleFrame: public wxFrame +{ +public: + MySimpleFrame(); + + void OnQuit( wxCommandEvent &event ); + + MySimpleCanvas *m_canvas; + +private: + DECLARE_DYNAMIC_CLASS(MySimpleFrame) + DECLARE_EVENT_TABLE() +}; + + +IMPLEMENT_DYNAMIC_CLASS( MySimpleFrame, wxFrame ) + +BEGIN_EVENT_TABLE(MySimpleFrame,wxFrame) + EVT_MENU (ID_QUIT, MySimpleFrame::OnQuit) +END_EVENT_TABLE() + +MySimpleFrame::MySimpleFrame() + : wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxScrolledWindow sample"), + wxPoint(120,120), wxSize(150,150) ) +{ + wxMenu *file_menu = new wxMenu(); + file_menu->Append( ID_QUIT, _T("E&xit\tAlt-X")); + + wxMenuBar *menu_bar = new wxMenuBar(); + menu_bar->Append(file_menu, _T("&File")); + + SetMenuBar( menu_bar ); + + m_canvas = new MySimpleCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(100,100) ); +} + +void MySimpleFrame::OnQuit( wxCommandEvent &WXUNUSED(event) ) +{ + Close( true ); +} + +// ---------------------------------------------------------------------- +// a complex example +// ---------------------------------------------------------------------- // derived classes @@ -45,7 +143,8 @@ public: void OnDeleteButton( wxCommandEvent &event ); void OnMoveButton( wxCommandEvent &event ); void OnScrollWin( wxCommandEvent &event ); - void OnMouseDown( wxMouseEvent &event ); + void OnMouseRightDown( wxMouseEvent &event ); + void OnMouseWheel( wxMouseEvent &event ); wxButton *m_button; @@ -170,12 +269,14 @@ public: // interface static wxRect DCNormalize(wxCoord x, wxCoord y, wxCoord w, wxCoord h); protected: // event stuff - DECLARE_EVENT_TABLE() void OnDraw(wxDC& dc); void OnMouseLeftDown(wxMouseEvent& event); void OnMouseLeftUp(wxMouseEvent& event); void OnMouseMove(wxMouseEvent& event); + void OnMouseCaptureLost(wxMouseCaptureLostEvent& event); void OnScroll(wxScrollWinEvent& event); + + DECLARE_EVENT_TABLE() }; // ---------------------------------------------------------------------------- @@ -234,7 +335,8 @@ IMPLEMENT_DYNAMIC_CLASS(MyCanvas, wxScrolledWindow) BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) EVT_PAINT( MyCanvas::OnPaint) - EVT_MOUSE_EVENTS( MyCanvas::OnMouseDown) + EVT_RIGHT_DOWN( MyCanvas::OnMouseRightDown) + EVT_MOUSEWHEEL( MyCanvas::OnMouseWheel) EVT_BUTTON( ID_QUERYPOS, MyCanvas::OnQueryPosition) EVT_BUTTON( ID_ADDBUTTON, MyCanvas::OnAddButton) EVT_BUTTON( ID_DELBUTTON, MyCanvas::OnDeleteButton) @@ -322,18 +424,25 @@ MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id, SetCursor( wxCursor( wxCURSOR_IBEAM ) ); } -void MyCanvas::OnMouseDown( wxMouseEvent &event ) +void MyCanvas::OnMouseRightDown( wxMouseEvent &event ) { - if (event.LeftDown()) - { - wxPoint pt( event.GetPosition() ); - int x,y; - CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); - wxLogMessage( wxT("Mouse down event at: %d %d, scrolled: %d %d"), pt.x, pt.y, x, y ); + wxPoint pt( event.GetPosition() ); + int x,y; + CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); + 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()") ); - } +void MyCanvas::OnMouseWheel( wxMouseEvent &event ) +{ + wxPoint pt( event.GetPosition() ); + int x,y; + CalcUnscrolledPosition( pt.x, pt.y, &x, &y ); + wxLogMessage( wxT("Mouse wheel event at: %d %d, scrolled: %d %d\n") + wxT("Rotation: %d, delta = %d"), + pt.x, pt.y, x, y, + event.GetWheelRotation(), event.GetWheelDelta() ); + + event.Skip(); } void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) ) @@ -406,7 +515,7 @@ BEGIN_EVENT_TABLE( MyAutoScrollWindow, wxScrolledWindow) END_EVENT_TABLE() MyAutoScrollWindow::MyAutoScrollWindow( wxWindow *parent ) - : wxScrolledWindow( parent, -1, wxDefaultPosition, wxDefaultSize, + : wxScrolledWindow( parent, -1, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxScrolledWindowStyle ) { SetBackgroundColour( wxT("GREEN") ); @@ -426,16 +535,11 @@ MyAutoScrollWindow::MyAutoScrollWindow( wxWindow *parent ) wxDefaultPosition, SMALL_BUTTON ); - // We need to do this here, because wxADJUST_MINSIZE below - // will cause the initial size to be ignored for Best/Min size. - // It would be nice to fix the sizers to handle this a little - // more cleanly. - m_button->SetSizeHints( SMALL_BUTTON.GetWidth(), SMALL_BUTTON.GetHeight() ); innersizer->Add( m_button, 0, - wxALIGN_CENTER | wxALL | wxADJUST_MINSIZE, + wxALIGN_CENTER | wxALL, 20 ); innersizer->Add( new wxStaticText( this, wxID_ANY, _T("This is just") ), @@ -475,11 +579,6 @@ void MyAutoScrollWindow::OnResizeClick( wxCommandEvent &WXUNUSED( event ) ) // MyFrame // ---------------------------------------------------------------------------- -const long ID_QUIT = wxID_EXIT; -const long ID_ABOUT = wxID_ABOUT; -const long ID_DELETE_ALL = 100; -const long ID_INSERT_NEW = 101; - IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame ) BEGIN_EVENT_TABLE(MyFrame,wxFrame) @@ -569,10 +668,16 @@ void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) bool MyApp::OnInit() { - wxFrame *frame = new MyFrame(); - frame->Show( true ); + if ( !wxApp::OnInit() ) + return false; - return true; + wxFrame *frame = new MyFrame(); + frame->Show( true ); + + frame = new MySimpleFrame(); + frame->Show(); + + return true; } // ---------------------------------------------------------------------------- @@ -592,7 +697,7 @@ void MyScrolledWindowDumb::OnDraw(wxDC& dc) CalcScrolledPosition(0, y, NULL, &yPhys); dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"), - line, y, yPhys), 0, y); + unsigned(line), y, yPhys), 0, y); y += m_hLine; } } @@ -621,7 +726,7 @@ void MyScrolledWindowSmart::OnDraw(wxDC& dc) CalcScrolledPosition(0, y, NULL, &yPhys); dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"), - line, y, yPhys), 0, y); + unsigned(line), y, yPhys), 0, y); y += m_hLine; } } @@ -634,6 +739,7 @@ BEGIN_EVENT_TABLE(MyAutoTimedScrollingWindow, wxScrolledWindow) EVT_LEFT_DOWN(MyAutoTimedScrollingWindow::OnMouseLeftDown) EVT_LEFT_UP(MyAutoTimedScrollingWindow::OnMouseLeftUp) EVT_MOTION(MyAutoTimedScrollingWindow::OnMouseMove) + EVT_MOUSE_CAPTURE_LOST(MyAutoTimedScrollingWindow::OnMouseCaptureLost) EVT_SCROLLWIN(MyAutoTimedScrollingWindow::OnScroll) END_EVENT_TABLE() @@ -828,7 +934,9 @@ void MyAutoTimedScrollingWindow::OnDraw(wxDC& dc) wxBrush selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT) , wxSOLID); dc.SetPen(*wxTRANSPARENT_PEN); - wxString str = sm_testData; + const wxString str = sm_testData; + size_t strLength = str.length(); + wxString::const_iterator str_i; // draw the characters // 1. for each update region @@ -839,6 +947,7 @@ void MyAutoTimedScrollingWindow::OnDraw(wxDC& dc) for (int chY = updRectInGChars.y ; chY <= updRectInGChars.y + updRectInGChars.height; ++chY) { // 3. for each character in the row + bool isFirstX = true; for (int chX = updRectInGChars.x ; chX <= updRectInGChars.x + updRectInGChars.width ; ++chX) { @@ -860,10 +969,15 @@ void MyAutoTimedScrollingWindow::OnDraw(wxDC& dc) size_t charIndex = chY * sm_lineLen + chX; if (chY < sm_lineCnt && chX < sm_lineLen && - charIndex < str.Length()) + charIndex < strLength) { - dc.DrawText(str.Mid(charIndex,1), - charPos.x, charPos.y); + if (isFirstX) + { + str_i = str.begin() + charIndex; + isFirstX = false; + } + dc.DrawText(*str_i, charPos.x, charPos.y); + ++str_i; } } } @@ -904,6 +1018,12 @@ void MyAutoTimedScrollingWindow::OnMouseMove(wxMouseEvent& event) } } +void MyAutoTimedScrollingWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event)) +{ + // we only capture mouse for timed scrolling, so nothing is needed here + // other than making sure to not call event.Skip() +} + void MyAutoTimedScrollingWindow::OnScroll(wxScrollWinEvent& event) { // need to move the cursor when autoscrolling @@ -930,7 +1050,7 @@ void MyAutoTimedScrollingWindow::OnScroll(wxScrollWinEvent& event) const int MyAutoTimedScrollingWindow::sm_lineCnt = 125; const int MyAutoTimedScrollingWindow::sm_lineLen = 79; const wxChar* MyAutoTimedScrollingWindow::sm_testData = -_T("162 Cult of the genius out of vanity.— Because we think well of ourselves, but ") +_T("162 Cult of the genius out of vanity. Because we think well of ourselves, but ") _T("nonetheless never suppose ourselves capable of producing a painting like one of ") _T("Raphael's or a dramatic scene like one of Shakespeare's, we convince ourselves ") _T("that the capacity to do so is quite extraordinarily marvelous, a wholly ") @@ -938,7 +1058,7 @@ _T("uncommon accident, or, if we are still religiously inclined, a mercy from on _T("high. Thus our vanity, our self-love, promotes the cult of the genius: for only ") _T("if we think of him as being very remote from us, as a miraculum, does he not ") _T("aggrieve us (even Goethe, who was without envy, called Shakespeare his star of ") -_T("the most distant heights [\"William! Stern der schönsten Ferne\": from Goethe's, ") +_T("the most distant heights [\"William! Stern der schonsten Ferne\": from Goethe's, ") _T("\"Between Two Worlds\"]; in regard to which one might recall the lines: \"the ") _T("stars, these we do not desire\" [from Goethe's, \"Comfort in Tears\"]). But, aside ") _T("from these suggestions of our vanity, the activity of the genius seems in no ") @@ -951,7 +1071,7 @@ _T("incentives, who never tire of combining together the means available to them _T("Genius too does nothing except learn first how to lay bricks then how to build, ") _T("except continually seek for material and continually form itself around it. ") _T("Every activity of man is amazingly complicated, not only that of the genius: ") -_T("but none is a \"miracle.\"— Whence, then, the belief that genius exists only in ") +_T("but none is a \"miracle.\" Whence, then, the belief that genius exists only in ") _T("the artist, orator and philosopher? that only they have \"intuition\"? (Whereby ") _T("they are supposed to possess a kind of miraculous eyeglass with which they can ") _T("see directly into \"the essence of the thing\"!) It is clear that people speak of ") @@ -968,7 +1088,7 @@ _T("with genius and why men of science do not. In reality, this evaluation of th _T("former and undervaluation of the latter is only a piece of childishness in the ") _T("realm of reason. ") _T("\n\n") -_T("163 The serious workman.— Do not talk about giftedness, inborn talents! One can ") +_T("163 The serious workman. Do not talk about giftedness, inborn talents! One can ") _T("name great men of all kinds who were very little gifted. The acquired ") _T("greatness, became \"geniuses\" (as we put it), through qualities the lack of ") _T("which no one who knew what they were would boast of: they all possessed that ") @@ -990,15 +1110,15 @@ _T("everything that will produce an artistic effect when it is well described, o _T("should, finally, reflect on the motives of human actions, disdain no signpost ") _T("to instruction about them and be a collector of these things by day and night. ") _T("One should continue in this many-sided exercise some ten years: what is then ") -_T("created in the workshop, however, will be fit to go out into the world.— What, ") +_T("created in the workshop, however, will be fit to go out into the world. What, ") _T("however, do most people do? They begin, not with the parts, but with the whole. ") _T("Perhaps they chance to strike a right note, excite attention and from then on ") -_T("strike worse and worse notes, for good, natural reasons.— Sometimes, when the ") +_T("strike worse and worse notes, for good, natural reasons. Sometimes, when the ") _T("character and intellect needed to formulate such a life-plan are lacking, fate ") _T("and need take their place and lead the future master step by step through all ") _T("the stipulations of his trade. ") _T("\n\n") -_T("164 Peril and profit in the cult of the genius.— The belief in great, superior, ") +_T("164 Peril and profit in the cult of the genius. The belief in great, superior, ") _T("fruitful spirits is not necessarily, yet nonetheless is very frequently ") _T("associated with that religious or semi-religious superstition that these ") _T("spirits are of supra-human origin and possess certain miraculous abilities by ") @@ -1041,7 +1161,7 @@ _T("they render men will-less and sweep them away into the delusion that the ") _T("leaders they are following are supra-natural. Indeed, it elevates and inspires ") _T("men to believe that someone is in possession of supra-natural powers: to this ") _T("extent Plato was right to say [Plato: Phaedrus, 244a] that madness has brought ") -_T("the greatest of blessings upon mankind.— In rare individual cases this portion ") +_T("the greatest of blessings upon mankind. In rare individual cases this portion ") _T("of madness may, indeed, actually have been the means by which such a nature, ") _T("excessive in all directions, was held firmly together: in the life of ") _T("individuals, too, illusions that are in themselves poisons often play the role ")