]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/view.cpp
b1c289339f085228efd02436200422a3f1d17203
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
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx/wx.h".
14 #include "wx/wxprec.h"
24 #if !wxUSE_DOC_VIEW_ARCHITECTURE
25 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
32 // ----------------------------------------------------------------------------
33 // DrawingView implementation
34 // ----------------------------------------------------------------------------
36 IMPLEMENT_DYNAMIC_CLASS(DrawingView
, wxView
)
38 BEGIN_EVENT_TABLE(DrawingView
, wxView
)
39 EVT_MENU(wxID_CUT
, DrawingView::OnCut
)
42 // What to do when a view is created. Creates actual
43 // windows for displaying the view.
44 bool DrawingView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
) )
46 MyApp
& app
= wxGetApp();
47 if ( app
.GetMode() != MyApp::Mode_Single
)
49 // create a new window and canvas inside it
50 m_frame
= app
.CreateChildFrame(doc
, this, true);
51 m_frame
->SetTitle("Drawing View");
53 m_canvas
= new MyCanvas(this, m_frame
);
56 else // single document mode
58 // reuse the existing window and canvas
59 m_frame
= wxStaticCast(app
.GetTopWindow(), wxFrame
);
60 m_canvas
= app
.GetMainWindowCanvas();
61 m_canvas
->SetView(this);
63 // Associate the appropriate frame with this view.
66 // Make sure the document manager knows that this is the
70 // Initialize the edit menu Undo and Redo items
71 doc
->GetCommandProcessor()->SetEditMenu(app
.GetMainWindowEditMenu());
72 doc
->GetCommandProcessor()->Initialize();
78 // Sneakily gets used for default print/preview as well as drawing on the
80 void DrawingView::OnDraw(wxDC
*dc
)
82 dc
->SetPen(*wxBLACK_PEN
);
84 // simply draw all lines of all segments
85 const DoodleSegments
& segments
= GetDocument()->GetSegments();
86 for ( DoodleSegments::const_iterator i
= segments
.begin();
90 const DoodleLines
& lines
= i
->GetLines();
91 for ( DoodleLines::const_iterator j
= lines
.begin();
95 const DoodleLine
& line
= *j
;
97 dc
->DrawLine(line
.x1
, line
.y1
, line
.x2
, line
.y2
);
102 DrawingDocument
* DrawingView::GetDocument()
104 return wxStaticCast(wxView::GetDocument(), DrawingDocument
);
107 void DrawingView::OnUpdate(wxView
* sender
, wxObject
* hint
)
109 wxView::OnUpdate(sender
, hint
);
114 // Clean up windows used for displaying the view.
115 bool DrawingView::OnClose(bool deleteWindow
)
117 if ( !wxView::OnClose(deleteWindow
) )
122 // Clear the canvas in single-window mode in which it stays alive
123 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
125 m_canvas
->ClearBackground();
126 m_canvas
->ResetView();
130 m_frame
->SetTitle(wxTheApp
->GetAppDisplayName());
132 else // not single window mode
143 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
145 DrawingDocument
* const doc
= GetDocument();
147 doc
->GetCommandProcessor()->Submit(new DrawingRemoveSegmentCommand(doc
));
150 // ----------------------------------------------------------------------------
151 // TextEditView implementation
152 // ----------------------------------------------------------------------------
154 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
156 BEGIN_EVENT_TABLE(TextEditView
, wxView
)
157 EVT_MENU(wxID_COPY
, TextEditView::OnCopy
)
158 EVT_MENU(wxID_PASTE
, TextEditView::OnPaste
)
159 EVT_MENU(wxID_SELECTALL
, TextEditView::OnSelectAll
)
162 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
))
164 m_frame
= wxGetApp().CreateChildFrame(doc
, this, false);
165 m_text
= new wxTextCtrl(m_frame
, wxID_ANY
, "",
166 wxPoint(0, 0), m_frame
->GetClientSize(),
169 m_frame
->SetTitle("Text View");
177 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
))
179 // nothing to do here, wxTextCtrl draws itself
182 bool TextEditView::OnClose(bool deleteWindow
)
184 if ( !wxView::OnClose(deleteWindow
) )
189 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
193 else // not single window mode
202 // ----------------------------------------------------------------------------
203 // MyCanvas implementation
204 // ----------------------------------------------------------------------------
206 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
207 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
210 // Define a constructor for my canvas
211 MyCanvas::MyCanvas(wxView
*view
, wxWindow
*parent
)
212 : wxScrolledWindow(parent
, wxID_ANY
, wxPoint(0, 0), parent
->GetClientSize())
215 m_currentSegment
= NULL
;
216 m_lastMousePos
= wxDefaultPosition
;
218 SetCursor(wxCursor(wxCURSOR_PENCIL
));
220 // this is completely arbitrary and is done just for illustration purposes
221 SetVirtualSize(1000, 1000);
222 SetScrollRate(20, 20);
224 SetBackgroundColour(*wxWHITE
);
227 MyCanvas::~MyCanvas()
229 delete m_currentSegment
;
232 // Define the repainting behaviour
233 void MyCanvas::OnDraw(wxDC
& dc
)
236 m_view
->OnDraw(& dc
);
239 // This implements a tiny doodling program. Drag the mouse using the left
241 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
249 dc
.SetPen(*wxBLACK_PEN
);
251 const wxPoint
pt(event
.GetLogicalPosition(dc
));
253 // is this the end of the current segment?
254 if ( m_currentSegment
&& event
.LeftUp() )
256 if ( !m_currentSegment
->IsEmpty() )
258 // We've got a valid segment on mouse left up, so store it.
259 DrawingDocument
* const
260 doc
= wxStaticCast(m_view
->GetDocument(), DrawingDocument
);
262 doc
->GetCommandProcessor()->Submit(
263 new DrawingAddSegmentCommand(doc
, *m_currentSegment
));
268 wxDELETE(m_currentSegment
);
271 // is this the start of a new segment?
272 if ( m_lastMousePos
!= wxDefaultPosition
&& event
.Dragging() )
274 if ( !m_currentSegment
)
275 m_currentSegment
= new DoodleSegment
;
277 m_currentSegment
->AddLine(m_lastMousePos
, pt
);
279 dc
.DrawLine(m_lastMousePos
, pt
);
285 // ----------------------------------------------------------------------------
286 // ImageCanvas implementation
287 // ----------------------------------------------------------------------------
289 // Define a constructor for my canvas
290 ImageCanvas::ImageCanvas(wxView
* view
, wxWindow
* parent
)
291 : wxScrolledWindow(parent
, wxID_ANY
, wxPoint(0, 0), parent
->GetClientSize())
293 SetScrollRate( 10, 10 );
298 // Define the repainting behaviour
299 void ImageCanvas::OnDraw(wxDC
& dc
)
302 m_view
->OnDraw(& dc
);
305 // ----------------------------------------------------------------------------
306 // ImageView implementation
307 // ----------------------------------------------------------------------------
309 IMPLEMENT_DYNAMIC_CLASS(ImageView
, wxView
)
311 ImageDocument
* ImageView::GetDocument()
313 return wxStaticCast(wxView::GetDocument(), ImageDocument
);
316 bool ImageView::OnCreate(wxDocument
* doc
, long WXUNUSED(flags
))
318 m_frame
= wxGetApp().CreateChildFrame(doc
, this, false);
319 m_frame
->SetTitle("Image View");
320 m_canvas
= new ImageCanvas(this, m_frame
);
326 void ImageView::OnUpdate(wxView
* sender
, wxObject
* hint
)
328 wxView::OnUpdate(sender
, hint
);
329 wxImage image
= GetDocument()->GetImage();
332 m_canvas
->SetVirtualSize(image
.GetWidth(), image
.GetHeight());
336 void ImageView::OnDraw(wxDC
* dc
)
338 wxImage image
= GetDocument()->GetImage();
341 dc
->DrawBitmap(wxBitmap(image
), 0, 0);
345 bool ImageView::OnClose(bool deleteWindow
)
347 if ( !wxView::OnClose(deleteWindow
) )
352 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
354 GetDocument()->DeleteContents();
356 else // not single window mode