]> git.saurik.com Git - wxWidgets.git/commitdiff
Applied #10811: Image in docview sample
authorJulian Smart <julian@anthemion.co.uk>
Thu, 24 Sep 2009 16:04:06 +0000 (16:04 +0000)
committerJulian Smart <julian@anthemion.co.uk>
Thu, 24 Sep 2009 16:04:06 +0000 (16:04 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62089 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

samples/docview/doc.cpp
samples/docview/doc.h
samples/docview/docview.cpp
samples/docview/view.cpp
samples/docview/view.h

index 4892856fe09fbb4203cb849f21431f1a5c70a77a..cf53892ea2403fc326fba2e83d91b8085f312a53 100644 (file)
@@ -34,6 +34,7 @@
 #else
     #include "wx/txtstrm.h"
 #endif
+#include "wx/wfstream.h"
 
 #include "doc.h"
 #include "view.h"
@@ -247,3 +248,58 @@ wxTextCtrl* TextEditDocument::GetTextCtrl() const
     wxView* view = GetFirstView();
     return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
 }
+
+// ----------------------------------------------------------------------------
+// wxImageDocument implementation
+// ----------------------------------------------------------------------------
+
+/////////////////////////////////////////////////////////////////////////////
+// wxImageDocument
+
+IMPLEMENT_DYNAMIC_CLASS(wxImageDocument, wxDocument)
+
+wxImageDocument::wxImageDocument() : wxDocument()
+{
+}
+
+wxImageDocument::~wxImageDocument()
+{
+}
+
+bool wxImageDocument::DeleteContents()
+{
+    bool ok = wxDocument::DeleteContents();
+    if (ok && m_image.IsOk())
+    {
+        m_image.Destroy();
+    }
+    return ok;
+}
+
+bool wxImageDocument::SaveFile(wxOutputStream* stream, wxBitmapType type) const
+{
+    return m_image.IsOk() && m_image.SaveFile(*stream, type);
+}
+
+bool wxImageDocument::DoOpenDocument(const wxString& file)
+{
+    wxFileInputStream stream(file);
+    return stream.IsOk() && DoOpenDocument(&stream);
+}
+
+bool wxImageDocument::DoSaveDocument(const wxString& file)
+{
+    wxFileOutputStream stream(file);
+    return stream.IsOk() && DoSaveDocument(&stream);
+}
+
+bool wxImageDocument::DoOpenDocument(wxInputStream* stream)
+{
+    return m_image.LoadFile(*stream);
+}
+
+bool wxImageDocument::DoSaveDocument(wxOutputStream* stream) const
+{
+    return m_image.IsOk() && SaveFile(stream, m_image.GetType());
+}
+
index dc6dc5e2a33cae559bcf6a4562faf4ef693d5a6d..ecc1453a382488bf1256b45d3cc398e86faec24e 100644 (file)
@@ -16,6 +16,7 @@
 #include "wx/docview.h"
 #include "wx/cmdproc.h"
 #include "wx/vector.h"
+#include "wx/image.h"
 
 // This sample is written to build both with wxUSE_STD_IOSTREAM==0 and 1, which
 // somewhat complicates its code but is necessary in order to support building
@@ -196,4 +197,34 @@ public:
     DECLARE_DYNAMIC_CLASS(TextEditDocument)
 };
 
+// ----------------------------------------------------------------------------
+// A basic image document class
+// ----------------------------------------------------------------------------
+
+class wxImageDocument : public wxDocument
+{
+protected:
+    wxImage m_image;
+public:
+    wxImageDocument();
+
+    wxImage*       GetImage()       { return &m_image; }
+    const wxImage& GetImage() const { return m_image; }
+
+    bool SaveFile(wxOutputStream*, wxBitmapType) const;
+
+public:
+    virtual ~wxImageDocument();
+    virtual bool DeleteContents();
+
+    virtual bool DoOpenDocument(const wxString& file);
+    virtual bool DoSaveDocument(const wxString& file);
+
+    virtual bool DoOpenDocument(wxInputStream*);
+    virtual bool DoSaveDocument(wxOutputStream*) const;
+
+    wxDECLARE_NO_COPY_CLASS(wxImageDocument);
+    DECLARE_DYNAMIC_CLASS(wxImageDocument)
+};
+
 #endif // _WX_SAMPLES_DOCVIEW_DOC_H_
index 5e0c865bae8e2cf244ef9df950261ecadc3a723a..b865b6f42590348df34d8b6659b1a6b493fca9ef 100644 (file)
@@ -148,6 +148,8 @@ bool MyApp::OnInit()
     if ( !wxApp::OnInit() )
         return false;
 
+    ::wxInitAllImageHandlers();
+
     SetAppName("DocView Sample");
 
     //// Create a document manager
@@ -176,6 +178,10 @@ bool MyApp::OnInit()
 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
         wxFileName::MacRegisterDefaultTypeAndCreator("txt" , 'TEXT' , 'WXMA');
 #endif
+        // Create a template relating image documents to their views
+        new wxDocTemplate(docManager, "Image", "*.png;*.jpg", "", "png;jpg",
+                          "Image Doc", "Image View",
+                          CLASSINFO(wxImageDocument), CLASSINFO(wxImageView));
     }
 
     // create the main frame window
index d27e3f6a20f7a0a67a3ecc768f7398508c733f02..d454e56554600d29ff16b74322ad3f121768fa97 100644 (file)
@@ -104,8 +104,9 @@ DrawingDocument* DrawingView::GetDocument()
     return wxStaticCast(wxView::GetDocument(), DrawingDocument);
 }
 
-void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
+void DrawingView::OnUpdate(wxView* sender, wxObject* hint)
 {
+    wxView::OnUpdate(sender, hint);
     if ( m_canvas )
         m_canvas->Refresh();
 }
@@ -282,3 +283,86 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event)
     m_lastMousePos = pt;
 }
 
+// ----------------------------------------------------------------------------
+// wxImageCanvas implementation
+// ----------------------------------------------------------------------------
+
+BEGIN_EVENT_TABLE(wxImageCanvas, wxScrolledWindow)
+END_EVENT_TABLE()
+
+// Define a constructor for my canvas
+wxImageCanvas::wxImageCanvas(wxView* view, wxWindow* parent)
+    : wxScrolledWindow(parent, wxID_ANY, wxPoint(0, 0), parent->GetClientSize())
+{
+    m_view = view;
+}
+
+// Define the repainting behaviour
+void wxImageCanvas::OnDraw(wxDC& dc)
+{
+    if ( m_view )
+        m_view->OnDraw(& dc);
+}
+
+// ----------------------------------------------------------------------------
+// wxImageView implementation
+// ----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxImageView, wxView)
+
+BEGIN_EVENT_TABLE(wxImageView, wxView)
+END_EVENT_TABLE()
+
+wxImageDocument* wxImageView::GetDocument()
+{
+    return wxStaticCast(wxView::GetDocument(), wxImageDocument);
+}
+
+bool wxImageView::OnCreate(wxDocument* doc, long WXUNUSED(flags))
+{
+    m_frame = wxGetApp().CreateChildFrame(doc, this, false);
+    m_frame->SetTitle("Image View");
+    m_canvas = new wxImageCanvas(this, m_frame);
+    m_frame->Show(true);
+    Activate(true);
+    return true;
+}
+
+void wxImageView::OnUpdate(wxView* sender, wxObject* hint)
+{
+    wxView::OnUpdate(sender, hint);
+    const wxImage* image = GetDocument()->GetImage();
+    if (image->IsOk())
+    {
+        m_canvas->SetScrollbars( 1, 1, image->GetWidth(), image->GetHeight() );
+    }
+}
+
+void wxImageView::OnDraw(wxDC* dc)
+{
+    const wxImage* image = GetDocument()->GetImage();
+    if (image->IsOk())
+    {
+        dc->DrawBitmap(wxBitmap(*image), 0, 0);
+    }
+}
+
+bool wxImageView::OnClose(bool deleteWindow)
+{
+    if ( !GetDocument()->Close() )
+        return false;
+
+    Activate(false);
+
+    if ( wxGetApp().GetMode() == MyApp::Mode_Single )
+    {
+        GetDocument()->DeleteContents();
+    }
+    else // not single window mode
+    {
+        if ( deleteWindow )
+            wxDELETE(m_frame);
+    }
+    return true;
+}
+
index c7209f28fae2e624c7559adca6e76a7eb10744e8..f2df562f7f8b80112963e27f30470703b135f397 100644 (file)
@@ -114,4 +114,65 @@ private:
     DECLARE_DYNAMIC_CLASS(TextEditView)
 };
 
+// ----------------------------------------------------------------------------
+// wxImageCanvas
+// ----------------------------------------------------------------------------
+
+class wxImageCanvas : public wxScrolledWindow
+{
+public:
+    wxImageCanvas(wxView*, wxWindow* parent);
+
+    virtual void OnDraw(wxDC& dc);
+
+    // in a normal multiple document application a canvas is associated with
+    // one view from the beginning until the end, but to support the single
+    // document mode in which all documents reuse the same MyApp::GetCanvas()
+    // we need to allow switching the canvas from one view to another one
+
+    void SetView(wxView* view)
+    {
+        wxASSERT_MSG( !m_view, "shouldn't be already associated with a view" );
+
+        m_view = view;
+    }
+
+    void ResetView()
+    {
+        wxASSERT_MSG( m_view, "should be associated with a view" );
+
+        m_view = NULL;
+    }
+
+protected:
+    wxView *m_view;
+
+    DECLARE_EVENT_TABLE()
+};
+
+// ----------------------------------------------------------------------------
+// wxImageView
+// ----------------------------------------------------------------------------
+
+class wxImageDocument;
+class wxImageView : public wxView
+{
+public:
+    wxImageView() : wxView(), m_frame(NULL) {}
+
+    virtual bool OnCreate(wxDocument*, long flags);
+    virtual void OnDraw(wxDC*);
+    virtual bool OnClose(bool deleteWindow = true);
+    virtual void OnUpdate(wxView *sender, wxObject *hint = NULL);
+
+    wxImageDocument* GetDocument();
+
+protected:
+    wxFrame* m_frame;
+    wxImageCanvas* m_canvas;
+
+    DECLARE_EVENT_TABLE()
+    DECLARE_DYNAMIC_CLASS(wxImageView)
+};
+
 #endif // _WX_SAMPLES_DOCVIEW_VIEW_H_