]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/scroll/scroll.cpp
Copy headers and get MDI and glcanvas fully building for OS X Cocoa port.
[wxWidgets.git] / samples / scroll / scroll.cpp
index 442d5f502162ea497b0c8eea14f4ae97608c0ed5..a22c23e2a907ec74568992ec2a2e64ba7b7811ef 100644 (file)
 #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
+// ----------------------------------------------------------------------
+
+// MySimpleCanvas: a scrolled window which draws a simple rectangle
+class MySimpleCanvas: public wxScrolledWindow
+{
+public:
+    MySimpleCanvas() { }
+    MySimpleCanvas(wxWindow *parent);
+
+private:
+    void OnPaint(wxPaintEvent& event);
+
+    enum
+    {
+        CANVAS_WIDTH = 292,
+        CANVAS_HEIGHT = 297
+    };
+
+    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)
+              : wxScrolledWindow(parent, wxID_ANY,
+                                 wxDefaultPosition,
+                                 wxDefaultSize,
+                                 wxSUNKEN_BORDER)
+{
+    SetScrollRate( 10, 10 );
+    SetVirtualSize( CANVAS_WIDTH, CANVAS_HEIGHT );
+    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, CANVAS_WIDTH, CANVAS_HEIGHT );
+}
+
+// MySimpleFrame: a frame which contains a MySimpleCanvas
+class MySimpleFrame: public wxFrame
+{
+public:
+    MySimpleFrame();
+
+private:
+    void OnClose(wxCommandEvent& WXUNUSED(event)) { Close(true); }
+
+    DECLARE_DYNAMIC_CLASS(MySimpleFrame)
+    DECLARE_EVENT_TABLE()
+};
+
+
+IMPLEMENT_DYNAMIC_CLASS( MySimpleFrame, wxFrame )
+
+BEGIN_EVENT_TABLE(MySimpleFrame,wxFrame)
+  EVT_MENU(wxID_CLOSE, MySimpleFrame::OnClose)
+END_EVENT_TABLE()
+
+MySimpleFrame::MySimpleFrame()
+             : wxFrame(NULL, wxID_ANY, _T("wxScrolledWindow sample"),
+                       wxDefaultPosition, wxSize(200, 200))
+{
+    wxMenu *file_menu = new wxMenu();
+    file_menu->Append(wxID_CLOSE);
+
+    wxMenuBar *menu_bar = new wxMenuBar();
+    menu_bar->Append(file_menu, _T("&File"));
+
+    SetMenuBar( menu_bar );
+
+    new MySimpleCanvas(this);
+}
+
+// ----------------------------------------------------------------------
+// a complex example
+// ----------------------------------------------------------------------
 
 // derived classes
 
@@ -175,6 +269,7 @@ protected: // event stuff
     void OnMouseLeftDown(wxMouseEvent& event);
     void OnMouseLeftUp(wxMouseEvent& event);
     void OnMouseMove(wxMouseEvent& event);
+    void OnMouseCaptureLost(wxMouseCaptureLostEvent& event);
     void OnScroll(wxScrollWinEvent& event);
 
     DECLARE_EVENT_TABLE()
@@ -416,7 +511,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") );
@@ -436,16 +531,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") ),
@@ -485,11 +575,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)
@@ -579,10 +664,16 @@ void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
 
 bool MyApp::OnInit()
 {
-  wxFrame *frame = new MyFrame();
-  frame->Show( true );
+    if ( !wxApp::OnInit() )
+        return false;
+
+    wxFrame *frame = new MyFrame();
+    frame->Show( true );
+
+    frame = new MySimpleFrame();
+    frame->Show();
 
-  return true;
+    return true;
 }
 
 // ----------------------------------------------------------------------------
@@ -602,7 +693,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;
     }
 }
@@ -631,7 +722,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;
     }
 }
@@ -644,6 +735,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()
 
@@ -838,7 +930,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
@@ -849,6 +943,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) {
@@ -870,10 +965,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;
                 }
             }
         }
@@ -914,6 +1014,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