X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/49e7b7a26eaeaeb7490389ce2caf907649b7eb17..ea53a6014ada8dafaec620333d00a837315e96db:/samples/scroll/scroll.cpp diff --git a/samples/scroll/scroll.cpp b/samples/scroll/scroll.cpp index 245cf9d735..098c34f1ea 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,13 @@ 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 OnScroll(wxScrollWinEvent& event); + + DECLARE_EVENT_TABLE() }; // ---------------------------------------------------------------------------- @@ -234,7 +334,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 +423,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 +514,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") ); @@ -475,11 +583,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 +672,13 @@ void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) ) bool MyApp::OnInit() { - wxFrame *frame = new MyFrame(); - frame->Show( true ); + wxFrame *frame = new MyFrame(); + frame->Show( true ); + + frame = new MySimpleFrame(); + frame->Show(); - return true; + return true; } // ---------------------------------------------------------------------------- @@ -592,7 +698,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 +727,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; } }