]>
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
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows license
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 ( !GetDocument()->Close() )
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 ( !GetDocument()->Close() )
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 delete m_currentSegment
;
269 m_currentSegment
= NULL
;
272 // is this the start of a new segment?
273 if ( m_lastMousePos
!= wxDefaultPosition
&& event
.Dragging() )
275 if ( !m_currentSegment
)
276 m_currentSegment
= new DoodleSegment
;
278 m_currentSegment
->AddLine(m_lastMousePos
, pt
);
280 dc
.DrawLine(m_lastMousePos
, pt
);
286 // ----------------------------------------------------------------------------
287 // ImageCanvas implementation
288 // ----------------------------------------------------------------------------
290 // Define a constructor for my canvas
291 ImageCanvas::ImageCanvas(wxView
* view
, wxWindow
* parent
)
292 : wxScrolledWindow(parent
, wxID_ANY
, wxPoint(0, 0), parent
->GetClientSize())
294 SetScrollRate( 10, 10 );
299 // Define the repainting behaviour
300 void ImageCanvas::OnDraw(wxDC
& dc
)
303 m_view
->OnDraw(& dc
);
306 // ----------------------------------------------------------------------------
307 // ImageView implementation
308 // ----------------------------------------------------------------------------
310 IMPLEMENT_DYNAMIC_CLASS(ImageView
, wxView
)
312 ImageDocument
* ImageView::GetDocument()
314 return wxStaticCast(wxView::GetDocument(), ImageDocument
);
317 bool ImageView::OnCreate(wxDocument
* doc
, long WXUNUSED(flags
))
319 m_frame
= wxGetApp().CreateChildFrame(doc
, this, false);
320 m_frame
->SetTitle("Image View");
321 m_canvas
= new ImageCanvas(this, m_frame
);
327 void ImageView::OnUpdate(wxView
* sender
, wxObject
* hint
)
329 wxView::OnUpdate(sender
, hint
);
330 wxImage image
= GetDocument()->GetImage();
333 m_canvas
->SetVirtualSize(image
.GetWidth(), image
.GetHeight());
337 void ImageView::OnDraw(wxDC
* dc
)
339 wxImage image
= GetDocument()->GetImage();
342 dc
->DrawBitmap(wxBitmap(image
), 0, 0);
346 bool ImageView::OnClose(bool deleteWindow
)
348 if ( !GetDocument()->Close() )
353 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
355 GetDocument()->DeleteContents();
357 else // not single window mode