]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/image/image.cpp
mark the document as modified when its text control is (closes #10826)
[wxWidgets.git] / samples / image / image.cpp
index 9d1d42a7f2c555f187bc4cfd412b7fa21d41570c..b18a797ac681e90305cb642ea3c31f20d173031d 100644 (file)
@@ -6,6 +6,7 @@
 // Created:     1998
 // RCS-ID:      $Id$
 // Copyright:   (c) 1998-2005 Robert Roebling
+//              (c) 2005-2009 Vadim Zeitlin
 // License:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
 
@@ -117,19 +118,41 @@ enum
 class MyImageFrame : public wxFrame
 {
 public:
+    MyImageFrame(wxFrame *parent, const wxString& desc, const wxImage& image)
+    {
+        Create(parent, desc, wxBitmap(image), image.GetImageCount(desc));
+    }
+
     MyImageFrame(wxFrame *parent, const wxString& desc, const wxBitmap& bitmap)
-        : wxFrame(parent, wxID_ANY,
-                wxString::Format(_T("Image from %s"), desc.c_str()),
-                wxDefaultPosition, wxDefaultSize,
-                wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE),
-                m_bitmap(bitmap)
     {
+        Create(parent, desc, bitmap);
+    }
+
+private:
+    bool Create(wxFrame *parent,
+                const wxString& desc,
+                const wxBitmap& bitmap,
+                int numImages = 1)
+    {
+        if ( !wxFrame::Create(parent, wxID_ANY,
+                              wxString::Format(_T("Image from %s"), desc),
+                              wxDefaultPosition, wxDefaultSize,
+                              wxDEFAULT_FRAME_STYLE | wxFULL_REPAINT_ON_RESIZE) )
+            return false;
+
+        m_bitmap = bitmap;
+        m_zoom = 1.;
+
         wxMenu *menu = new wxMenu;
         menu->Append(wxID_SAVE);
         menu->AppendSeparator();
-        m_pClearBgMenu = menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background"));
+        menu->AppendCheckItem(ID_PAINT_BG, _T("&Paint background"),
+                              "Uncheck this for transparent images");
         menu->AppendSeparator();
         menu->Append(ID_RESIZE, _T("&Fit to window\tCtrl-F"));
+        menu->Append(wxID_ZOOM_IN, "Zoom &in\tCtrl-+");
+        menu->Append(wxID_ZOOM_OUT, "Zoom &out\tCtrl--");
+        menu->Append(wxID_ZOOM_100, "Reset zoom to &100%\tCtrl-1");
         menu->AppendSeparator();
         menu->Append(ID_ROTATE_LEFT, _T("Rotate &left\tCtrl-L"));
         menu->Append(ID_ROTATE_RIGHT, _T("Rotate &right\tCtrl-R"));
@@ -138,13 +161,19 @@ public:
         mbar->Append(menu, _T("&Image"));
         SetMenuBar(mbar);
 
-        CreateStatusBar();
+        mbar->Check(ID_PAINT_BG, true);
+
+        CreateStatusBar(2);
+        if ( numImages != 1 )
+            SetStatusText(wxString::Format("%d images", numImages), 1);
 
         SetClientSize(bitmap.GetWidth(), bitmap.GetHeight());
 
         UpdateStatusBar();
 
-//        SetBackgroundColour(*wxWHITE);
+        Show();
+
+        return true;
     }
 
     void OnEraseBackground(wxEraseEvent& WXUNUSED(event))
@@ -156,14 +185,19 @@ public:
     {
         wxPaintDC dc(this);
 
-        if (m_pClearBgMenu->IsChecked())
+        if ( GetMenuBar()->IsChecked(ID_PAINT_BG) )
             ClearBackground();
 
+        dc.SetUserScale(m_zoom, m_zoom);
+
         const wxSize size = GetClientSize();
-        dc.DrawBitmap(m_bitmap,
-                    (size.x - m_bitmap.GetWidth())/2,
-                    (size.y - m_bitmap.GetHeight())/2,
-                    true /* use mask */);
+        dc.DrawBitmap
+           (
+                m_bitmap,
+                dc.DeviceToLogicalX((size.x - m_zoom*m_bitmap.GetWidth())/2),
+                dc.DeviceToLogicalY((size.y - m_zoom*m_bitmap.GetHeight())/2),
+                true /* use mask */
+           );
     }
 
     void OnSave(wxCommandEvent& WXUNUSED(event))
@@ -338,7 +372,18 @@ public:
         m_bitmap = wxBitmap(img);
 
         UpdateStatusBar();
-        Refresh();
+    }
+
+    void OnZoom(wxCommandEvent& event)
+    {
+        if ( event.GetId() == wxID_ZOOM_IN )
+            m_zoom *= 1.2;
+        else if ( event.GetId() == wxID_ZOOM_OUT )
+            m_zoom /= 1.2;
+        else // wxID_ZOOM_100
+            m_zoom = 1.;
+
+        UpdateStatusBar();
     }
 
     void OnRotate(wxCommandEvent& event)
@@ -358,19 +403,19 @@ public:
         m_bitmap = wxBitmap(img);
 
         UpdateStatusBar();
-        Refresh();
     }
 
-private:
     void UpdateStatusBar()
     {
-        wxLogStatus(this, _T("Image size: (%d, %d)"),
+        wxLogStatus(this, _T("Image size: (%d, %d), zoom %.2f"),
                     m_bitmap.GetWidth(),
-                    m_bitmap.GetHeight());
+                    m_bitmap.GetHeight(),
+                    m_zoom);
+        Refresh();
     }
 
     wxBitmap m_bitmap;
-    wxMenuItem* m_pClearBgMenu;
+    double m_zoom;
 
     DECLARE_EVENT_TABLE()
 };
@@ -530,6 +575,10 @@ BEGIN_EVENT_TABLE(MyImageFrame, wxFrame)
     EVT_MENU(wxID_SAVE, MyImageFrame::OnSave)
     EVT_MENU_RANGE(ID_ROTATE_LEFT, ID_ROTATE_RIGHT, MyImageFrame::OnRotate)
     EVT_MENU(ID_RESIZE, MyImageFrame::OnResize)
+
+    EVT_MENU(wxID_ZOOM_IN, MyImageFrame::OnZoom)
+    EVT_MENU(wxID_ZOOM_OUT, MyImageFrame::OnZoom)
+    EVT_MENU(wxID_ZOOM_100, MyImageFrame::OnZoom)
 END_EVENT_TABLE()
 
 //-----------------------------------------------------------------------------
@@ -626,9 +675,11 @@ void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
 
 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
 {
-    (void)wxMessageBox( _T("wxImage demo\n")
-                        _T("Robert Roebling (c) 1998,2000"),
-                        _T("About wxImage Demo"), wxICON_INFORMATION | wxOK );
+    (void)wxMessageBox( "wxImage demo\n"
+                        "(c) Robert Roebling 1998-2005"
+                        "(c) Vadim Zeitlin 2005-2009",
+                        "About wxImage Demo",
+                        wxICON_INFORMATION | wxOK );
 }
 
 wxString MyFrame::LoadUserImage(wxImage& image)
@@ -656,7 +707,7 @@ void MyFrame::OnNewFrame( wxCommandEvent &WXUNUSED(event) )
     wxImage image;
     wxString filename = LoadUserImage(image);
     if ( !filename.empty() )
-        (new MyImageFrame(this, filename, wxBitmap(image)))->Show();
+        new MyImageFrame(this, filename, image);
 }
 
 void MyFrame::OnImageInfo( wxCommandEvent &WXUNUSED(event) )
@@ -735,7 +786,7 @@ void MyFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
     }
     else
     {
-        (new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap()))->Show();
+        new MyImageFrame(this, _T("Clipboard"), dobjBmp.GetBitmap());
     }
     wxTheClipboard->Close();
 }
@@ -765,9 +816,7 @@ void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
 
     const long loadTime = sw.Time();
 
-    MyImageFrame * const
-        frame = new MyImageFrame(this, filename, wxBitmap(image));
-    frame->Show();
+    MyImageFrame * const frame = new MyImageFrame(this, filename, image);
     wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
 #else
     wxLogError( _T("Couldn't create file selector dialog") );