]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/view.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/docview/view.cpp
3 // Purpose: View classes implementation
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
7 // Copyright: (c) 1998 Julian Smart
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
31 // ----------------------------------------------------------------------------
32 // DrawingView implementation
33 // ----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(DrawingView
, wxView
)
37 BEGIN_EVENT_TABLE(DrawingView
, wxView
)
38 EVT_MENU(wxID_CUT
, DrawingView::OnCut
)
41 // What to do when a view is created. Creates actual
42 // windows for displaying the view.
43 bool DrawingView::OnCreate(wxDocument
*doc
, long flags
)
45 if ( !wxView::OnCreate(doc
, flags
) )
48 MyApp
& app
= wxGetApp();
49 if ( app
.GetMode() != MyApp::Mode_Single
)
51 // create a new window and canvas inside it
52 wxFrame
* frame
= app
.CreateChildFrame(this, true);
53 wxASSERT(frame
== GetFrame());
54 m_canvas
= new MyCanvas(this);
57 else // single document mode
59 // reuse the existing window and canvas
60 m_canvas
= app
.GetMainWindowCanvas();
61 m_canvas
->SetView(this);
63 // Initialize the edit menu Undo and Redo items
64 doc
->GetCommandProcessor()->SetEditMenu(app
.GetMainWindowEditMenu());
65 doc
->GetCommandProcessor()->Initialize();
71 // Sneakily gets used for default print/preview as well as drawing on the
73 void DrawingView::OnDraw(wxDC
*dc
)
75 dc
->SetPen(*wxBLACK_PEN
);
77 // simply draw all lines of all segments
78 const DoodleSegments
& segments
= GetDocument()->GetSegments();
79 for ( DoodleSegments::const_iterator i
= segments
.begin();
83 const DoodleLines
& lines
= i
->GetLines();
84 for ( DoodleLines::const_iterator j
= lines
.begin();
88 const DoodleLine
& line
= *j
;
90 dc
->DrawLine(line
.x1
, line
.y1
, line
.x2
, line
.y2
);
95 DrawingDocument
* DrawingView::GetDocument()
97 return wxStaticCast(wxView::GetDocument(), DrawingDocument
);
100 void DrawingView::OnUpdate(wxView
* sender
, wxObject
* hint
)
102 wxView::OnUpdate(sender
, hint
);
107 // Clean up windows used for displaying the view.
108 bool DrawingView::OnClose(bool deleteWindow
)
110 if ( !wxView::OnClose(deleteWindow
) )
115 // Clear the canvas in single-window mode in which it stays alive
116 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
118 m_canvas
->ClearBackground();
119 m_canvas
->ResetView();
123 wxStaticCast(GetFrame(), wxFrame
)->SetTitle(wxTheApp
->GetAppDisplayName());
125 else // not single window mode
129 GetFrame()->Destroy();
136 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
138 DrawingDocument
* const doc
= GetDocument();
140 doc
->GetCommandProcessor()->Submit(new DrawingRemoveSegmentCommand(doc
));
143 // ----------------------------------------------------------------------------
144 // TextEditView implementation
145 // ----------------------------------------------------------------------------
147 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
149 BEGIN_EVENT_TABLE(TextEditView
, wxView
)
150 EVT_MENU(wxID_COPY
, TextEditView::OnCopy
)
151 EVT_MENU(wxID_PASTE
, TextEditView::OnPaste
)
152 EVT_MENU(wxID_SELECTALL
, TextEditView::OnSelectAll
)
155 bool TextEditView::OnCreate(wxDocument
*doc
, long flags
)
157 if ( !wxView::OnCreate(doc
, flags
) )
160 wxFrame
* frame
= wxGetApp().CreateChildFrame(this, false);
161 wxASSERT(frame
== GetFrame());
162 m_text
= new wxTextCtrl(frame
, wxID_ANY
, "",
163 wxDefaultPosition
, wxDefaultSize
,
170 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
))
172 // nothing to do here, wxTextCtrl draws itself
175 bool TextEditView::OnClose(bool deleteWindow
)
177 if ( !wxView::OnClose(deleteWindow
) )
182 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
186 else // not single window mode
190 GetFrame()->Destroy();
197 // ----------------------------------------------------------------------------
198 // MyCanvas implementation
199 // ----------------------------------------------------------------------------
201 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
202 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
205 // Define a constructor for my canvas
206 MyCanvas::MyCanvas(wxView
*view
, wxWindow
*parent
)
207 : wxScrolledWindow(parent
? parent
: view
->GetFrame())
210 m_currentSegment
= NULL
;
211 m_lastMousePos
= wxDefaultPosition
;
213 SetCursor(wxCursor(wxCURSOR_PENCIL
));
215 // this is completely arbitrary and is done just for illustration purposes
216 SetVirtualSize(1000, 1000);
217 SetScrollRate(20, 20);
219 SetBackgroundColour(*wxWHITE
);
222 MyCanvas::~MyCanvas()
224 delete m_currentSegment
;
227 // Define the repainting behaviour
228 void MyCanvas::OnDraw(wxDC
& dc
)
231 m_view
->OnDraw(& dc
);
234 // This implements a tiny doodling program. Drag the mouse using the left
236 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
244 dc
.SetPen(*wxBLACK_PEN
);
246 const wxPoint
pt(event
.GetLogicalPosition(dc
));
248 // is this the end of the current segment?
249 if ( m_currentSegment
&& event
.LeftUp() )
251 if ( !m_currentSegment
->IsEmpty() )
253 // We've got a valid segment on mouse left up, so store it.
254 DrawingDocument
* const
255 doc
= wxStaticCast(m_view
->GetDocument(), DrawingDocument
);
257 doc
->GetCommandProcessor()->Submit(
258 new DrawingAddSegmentCommand(doc
, *m_currentSegment
));
263 wxDELETE(m_currentSegment
);
266 // is this the start of a new segment?
267 if ( m_lastMousePos
!= wxDefaultPosition
&& event
.Dragging() )
269 if ( !m_currentSegment
)
270 m_currentSegment
= new DoodleSegment
;
272 m_currentSegment
->AddLine(m_lastMousePos
, pt
);
274 dc
.DrawLine(m_lastMousePos
, pt
);
280 // ----------------------------------------------------------------------------
281 // ImageCanvas implementation
282 // ----------------------------------------------------------------------------
284 // Define a constructor for my canvas
285 ImageCanvas::ImageCanvas(wxView
* view
)
286 : wxScrolledWindow(view
->GetFrame())
289 SetScrollRate( 10, 10 );
292 // Define the repainting behaviour
293 void ImageCanvas::OnDraw(wxDC
& dc
)
296 m_view
->OnDraw(& dc
);
299 // ----------------------------------------------------------------------------
300 // ImageView implementation
301 // ----------------------------------------------------------------------------
303 IMPLEMENT_DYNAMIC_CLASS(ImageView
, wxView
)
305 ImageDocument
* ImageView::GetDocument()
307 return wxStaticCast(wxView::GetDocument(), ImageDocument
);
310 bool ImageView::OnCreate(wxDocument
* doc
, long flags
)
312 if ( !wxView::OnCreate(doc
, flags
) )
315 wxFrame
* frame
= wxGetApp().CreateChildFrame(this, false);
316 wxASSERT(frame
== GetFrame());
317 m_canvas
= new ImageCanvas(this);
323 void ImageView::OnUpdate(wxView
* sender
, wxObject
* hint
)
325 wxView::OnUpdate(sender
, hint
);
326 wxImage image
= GetDocument()->GetImage();
329 m_canvas
->SetVirtualSize(image
.GetWidth(), image
.GetHeight());
333 void ImageView::OnDraw(wxDC
* dc
)
335 wxImage image
= GetDocument()->GetImage();
338 dc
->DrawBitmap(wxBitmap(image
), 0, 0, true /* use mask */);
342 bool ImageView::OnClose(bool deleteWindow
)
344 if ( !wxView::OnClose(deleteWindow
) )
349 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
351 GetDocument()->DeleteContents();
353 else // not single window mode
357 GetFrame()->Destroy();
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 ImageDetailsView::ImageDetailsView(ImageDetailsDocument
*doc
)
373 m_frame
= wxGetApp().CreateChildFrame(this, false);
374 m_frame
->SetTitle("Image Details");
376 wxPanel
* const panel
= new wxPanel(m_frame
);
377 wxFlexGridSizer
* const sizer
= new wxFlexGridSizer(2, wxSize(5, 5));
379 flags
= wxSizerFlags().Align(wxALIGN_CENTRE_VERTICAL
).Border();
381 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, "Image &file:"), flags
);
382 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, doc
->GetFilename()), flags
);
384 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, "Image &type:"), flags
);
386 switch ( doc
->GetType() )
388 case wxBITMAP_TYPE_PNG
:
392 case wxBITMAP_TYPE_JPEG
:
399 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, typeStr
), flags
);
401 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, "Image &size:"), flags
);
402 wxSize size
= doc
->GetSize();
403 sizer
->Add(new wxStaticText(panel
, wxID_ANY
,
404 wxString::Format("%d*%d", size
.x
, size
.y
)),
407 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, "Number of unique &colours:"),
409 sizer
->Add(new wxStaticText(panel
, wxID_ANY
,
410 wxString::Format("%lu", doc
->GetNumColours())),
413 sizer
->Add(new wxStaticText(panel
, wxID_ANY
, "Uses &alpha:"), flags
);
414 sizer
->Add(new wxStaticText(panel
, wxID_ANY
,
415 doc
->HasAlpha() ? "Yes" : "No"), flags
);
417 panel
->SetSizer(sizer
);
418 m_frame
->SetClientSize(panel
->GetBestSize());
422 void ImageDetailsView::OnDraw(wxDC
* WXUNUSED(dc
))
424 // nothing to do here, we use controls to show our information
427 bool ImageDetailsView::OnClose(bool deleteWindow
)
429 if ( wxGetApp().GetMode() != MyApp::Mode_Single
&& deleteWindow
)