X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/f37f49b652601744a8576d94568985cba49fc784..3717eeaf39f7145ab6d09a326dedb2c09b1219e8:/samples/docview/view.cpp diff --git a/samples/docview/view.cpp b/samples/docview/view.cpp index d454e56554..5b3743fd90 100644 --- a/samples/docview/view.cpp +++ b/samples/docview/view.cpp @@ -7,7 +7,7 @@ // RCS-ID: $Id$ // Copyright: (c) 1998 Julian Smart // (c) 2008 Vadim Zeitlin -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes "wx/wx.h". @@ -41,32 +41,26 @@ END_EVENT_TABLE() // What to do when a view is created. Creates actual // windows for displaying the view. -bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) ) +bool DrawingView::OnCreate(wxDocument *doc, long flags) { + if ( !wxView::OnCreate(doc, flags) ) + return false; + MyApp& app = wxGetApp(); if ( app.GetMode() != MyApp::Mode_Single ) { // create a new window and canvas inside it - m_frame = app.CreateChildFrame(doc, this, true); - m_frame->SetTitle("Drawing View"); - - m_canvas = new MyCanvas(this, m_frame); - m_frame->Show(true); + wxFrame* frame = app.CreateChildFrame(this, true); + wxASSERT(frame == GetFrame()); + m_canvas = new MyCanvas(this); + frame->Show(); } else // single document mode { // reuse the existing window and canvas - m_frame = wxStaticCast(app.GetTopWindow(), wxFrame); m_canvas = app.GetMainWindowCanvas(); m_canvas->SetView(this); - // Associate the appropriate frame with this view. - SetFrame(m_frame); - - // Make sure the document manager knows that this is the - // current view. - Activate(true); - // Initialize the edit menu Undo and Redo items doc->GetCommandProcessor()->SetEditMenu(app.GetMainWindowEditMenu()); doc->GetCommandProcessor()->Initialize(); @@ -88,7 +82,7 @@ void DrawingView::OnDraw(wxDC *dc) ++i ) { const DoodleLines& lines = i->GetLines(); - for ( DoodleLines::const_iterator j = lines.begin(); + for ( DoodleLines::const_iterator j = lines.begin(); j != lines.end(); ++j ) { @@ -114,7 +108,7 @@ void DrawingView::OnUpdate(wxView* sender, wxObject* hint) // Clean up windows used for displaying the view. bool DrawingView::OnClose(bool deleteWindow) { - if ( !GetDocument()->Close() ) + if ( !wxView::OnClose(deleteWindow) ) return false; Activate(false); @@ -126,17 +120,17 @@ bool DrawingView::OnClose(bool deleteWindow) m_canvas->ResetView(); m_canvas = NULL; - if ( m_frame ) - m_frame->SetTitle(wxTheApp->GetAppDisplayName()); + if (GetFrame()) + wxStaticCast(GetFrame(), wxFrame)->SetTitle(wxTheApp->GetAppDisplayName()); } else // not single window mode { if ( deleteWindow ) - wxDELETE(m_frame); + { + GetFrame()->Destroy(); + SetFrame(NULL); + } } - - SetFrame(NULL); - return true; } @@ -159,17 +153,17 @@ BEGIN_EVENT_TABLE(TextEditView, wxView) EVT_MENU(wxID_SELECTALL, TextEditView::OnSelectAll) END_EVENT_TABLE() -bool TextEditView::OnCreate(wxDocument *doc, long WXUNUSED(flags)) +bool TextEditView::OnCreate(wxDocument *doc, long flags) { - m_frame = wxGetApp().CreateChildFrame(doc, this, false); - m_text = new wxTextCtrl(m_frame, wxID_ANY, "", - wxPoint(0, 0), m_frame->GetClientSize(), - wxTE_MULTILINE); - - m_frame->SetTitle("Text View"); - m_frame->Show(true); + if ( !wxView::OnCreate(doc, flags) ) + return false; - Activate(true); + wxFrame* frame = wxGetApp().CreateChildFrame(this, false); + wxASSERT(frame == GetFrame()); + m_text = new wxTextCtrl(frame, wxID_ANY, "", + wxDefaultPosition, wxDefaultSize, + wxTE_MULTILINE); + frame->Show(); return true; } @@ -181,7 +175,7 @@ void TextEditView::OnDraw(wxDC *WXUNUSED(dc)) bool TextEditView::OnClose(bool deleteWindow) { - if ( !GetDocument()->Close() ) + if ( !wxView::OnClose(deleteWindow) ) return false; Activate(false); @@ -193,9 +187,11 @@ bool TextEditView::OnClose(bool deleteWindow) else // not single window mode { if ( deleteWindow ) - wxDELETE(m_frame); + { + GetFrame()->Destroy(); + SetFrame(NULL); + } } - return true; } @@ -209,7 +205,7 @@ END_EVENT_TABLE() // Define a constructor for my canvas MyCanvas::MyCanvas(wxView *view, wxWindow *parent) - : wxScrolledWindow(parent, wxID_ANY, wxPoint(0, 0), parent->GetClientSize()) + : wxScrolledWindow(parent ? parent : view->GetFrame()) { m_view = view; m_currentSegment = NULL; @@ -265,8 +261,7 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event) doc->Modify(true); } - delete m_currentSegment; - m_currentSegment = NULL; + wxDELETE(m_currentSegment); } // is this the start of a new segment? @@ -284,72 +279,70 @@ void MyCanvas::OnMouseEvent(wxMouseEvent& event) } // ---------------------------------------------------------------------------- -// wxImageCanvas implementation +// ImageCanvas 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()) +ImageCanvas::ImageCanvas(wxView* view) + : wxScrolledWindow(view->GetFrame()) { m_view = view; + SetScrollRate( 10, 10 ); } // Define the repainting behaviour -void wxImageCanvas::OnDraw(wxDC& dc) +void ImageCanvas::OnDraw(wxDC& dc) { if ( m_view ) m_view->OnDraw(& dc); } // ---------------------------------------------------------------------------- -// wxImageView implementation +// ImageView implementation // ---------------------------------------------------------------------------- -IMPLEMENT_DYNAMIC_CLASS(wxImageView, wxView) +IMPLEMENT_DYNAMIC_CLASS(ImageView, wxView) -BEGIN_EVENT_TABLE(wxImageView, wxView) -END_EVENT_TABLE() - -wxImageDocument* wxImageView::GetDocument() +ImageDocument* ImageView::GetDocument() { - return wxStaticCast(wxView::GetDocument(), wxImageDocument); + return wxStaticCast(wxView::GetDocument(), ImageDocument); } -bool wxImageView::OnCreate(wxDocument* doc, long WXUNUSED(flags)) +bool ImageView::OnCreate(wxDocument* doc, long 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); + if ( !wxView::OnCreate(doc, flags) ) + return false; + + wxFrame* frame = wxGetApp().CreateChildFrame(this, false); + wxASSERT(frame == GetFrame()); + m_canvas = new ImageCanvas(this); + frame->Show(); + return true; } -void wxImageView::OnUpdate(wxView* sender, wxObject* hint) +void ImageView::OnUpdate(wxView* sender, wxObject* hint) { wxView::OnUpdate(sender, hint); - const wxImage* image = GetDocument()->GetImage(); - if (image->IsOk()) + wxImage image = GetDocument()->GetImage(); + if ( image.IsOk() ) { - m_canvas->SetScrollbars( 1, 1, image->GetWidth(), image->GetHeight() ); + m_canvas->SetVirtualSize(image.GetWidth(), image.GetHeight()); } } -void wxImageView::OnDraw(wxDC* dc) +void ImageView::OnDraw(wxDC* dc) { - const wxImage* image = GetDocument()->GetImage(); - if (image->IsOk()) + wxImage image = GetDocument()->GetImage(); + if ( image.IsOk() ) { - dc->DrawBitmap(wxBitmap(*image), 0, 0); + dc->DrawBitmap(wxBitmap(image), 0, 0, true /* use mask */); } } -bool wxImageView::OnClose(bool deleteWindow) +bool ImageView::OnClose(bool deleteWindow) { - if ( !GetDocument()->Close() ) + if ( !wxView::OnClose(deleteWindow) ) return false; Activate(false); @@ -361,8 +354,84 @@ bool wxImageView::OnClose(bool deleteWindow) else // not single window mode { if ( deleteWindow ) - wxDELETE(m_frame); + { + GetFrame()->Destroy(); + SetFrame(NULL); + } } return true; } +// ---------------------------------------------------------------------------- +// ImageDetailsView +// ---------------------------------------------------------------------------- + +ImageDetailsView::ImageDetailsView(ImageDetailsDocument *doc) + : wxView() +{ + SetDocument(doc); + + m_frame = wxGetApp().CreateChildFrame(this, false); + m_frame->SetTitle("Image Details"); + + wxPanel * const panel = new wxPanel(m_frame); + wxFlexGridSizer * const sizer = new wxFlexGridSizer(2, wxSize(5, 5)); + const wxSizerFlags + flags = wxSizerFlags().Align(wxALIGN_CENTRE_VERTICAL).Border(); + + sizer->Add(new wxStaticText(panel, wxID_ANY, "Image &file:"), flags); + sizer->Add(new wxStaticText(panel, wxID_ANY, doc->GetFilename()), flags); + + sizer->Add(new wxStaticText(panel, wxID_ANY, "Image &type:"), flags); + wxString typeStr; + switch ( doc->GetType() ) + { + case wxBITMAP_TYPE_PNG: + typeStr = "PNG"; + break; + + case wxBITMAP_TYPE_JPEG: + typeStr = "JPEG"; + break; + + default: + typeStr = "Unknown"; + } + sizer->Add(new wxStaticText(panel, wxID_ANY, typeStr), flags); + + sizer->Add(new wxStaticText(panel, wxID_ANY, "Image &size:"), flags); + wxSize size = doc->GetSize(); + sizer->Add(new wxStaticText(panel, wxID_ANY, + wxString::Format("%d*%d", size.x, size.y)), + flags); + + sizer->Add(new wxStaticText(panel, wxID_ANY, "Number of unique &colours:"), + flags); + sizer->Add(new wxStaticText(panel, wxID_ANY, + wxString::Format("%lu", doc->GetNumColours())), + flags); + + sizer->Add(new wxStaticText(panel, wxID_ANY, "Uses &alpha:"), flags); + sizer->Add(new wxStaticText(panel, wxID_ANY, + doc->HasAlpha() ? "Yes" : "No"), flags); + + panel->SetSizer(sizer); + m_frame->SetClientSize(panel->GetBestSize()); + m_frame->Show(true); +} + +void ImageDetailsView::OnDraw(wxDC * WXUNUSED(dc)) +{ + // nothing to do here, we use controls to show our information +} + +bool ImageDetailsView::OnClose(bool deleteWindow) +{ + if ( wxGetApp().GetMode() != MyApp::Mode_Single && deleteWindow ) + { + delete m_frame; + m_frame = NULL; + } + + return true; +}