]>
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
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
113 // Clean up windows used for displaying the view.
114 bool DrawingView::OnClose(bool deleteWindow
)
116 if ( !GetDocument()->Close() )
121 // Clear the canvas in single-window mode in which it stays alive
122 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
124 m_canvas
->ClearBackground();
125 m_canvas
->ResetView();
129 m_frame
->SetTitle(wxTheApp
->GetAppDisplayName());
131 else // not single window mode
142 void DrawingView::OnCut(wxCommandEvent
& WXUNUSED(event
) )
144 DrawingDocument
* const doc
= GetDocument();
146 doc
->GetCommandProcessor()->Submit(new DrawingRemoveSegmentCommand(doc
));
149 // ----------------------------------------------------------------------------
150 // TextEditView implementation
151 // ----------------------------------------------------------------------------
153 IMPLEMENT_DYNAMIC_CLASS(TextEditView
, wxView
)
155 BEGIN_EVENT_TABLE(TextEditView
, wxView
)
156 EVT_MENU(wxID_COPY
, TextEditView::OnCopy
)
157 EVT_MENU(wxID_PASTE
, TextEditView::OnPaste
)
158 EVT_MENU(wxID_SELECTALL
, TextEditView::OnSelectAll
)
161 bool TextEditView::OnCreate(wxDocument
*doc
, long WXUNUSED(flags
))
163 m_frame
= wxGetApp().CreateChildFrame(doc
, this, false);
164 m_text
= new wxTextCtrl(m_frame
, wxID_ANY
, "",
165 wxPoint(0, 0), m_frame
->GetClientSize(),
168 m_frame
->SetTitle("Text View");
176 void TextEditView::OnDraw(wxDC
*WXUNUSED(dc
))
178 // nothing to do here, wxTextCtrl draws itself
181 bool TextEditView::OnClose(bool deleteWindow
)
183 if ( !GetDocument()->Close() )
188 if ( wxGetApp().GetMode() == MyApp::Mode_Single
)
192 else // not single window mode
201 // ----------------------------------------------------------------------------
202 // MyCanvas implementation
203 // ----------------------------------------------------------------------------
205 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
206 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
209 // Define a constructor for my canvas
210 MyCanvas::MyCanvas(wxView
*view
, wxWindow
*parent
)
211 : wxScrolledWindow(parent
, wxID_ANY
, wxPoint(0, 0), parent
->GetClientSize())
214 m_currentSegment
= NULL
;
215 m_lastMousePos
= wxDefaultPosition
;
217 SetCursor(wxCursor(wxCURSOR_PENCIL
));
219 // this is completely arbitrary and is done just for illustration purposes
220 SetVirtualSize(1000, 1000);
221 SetScrollRate(20, 20);
223 SetBackgroundColour(*wxWHITE
);
226 MyCanvas::~MyCanvas()
228 delete m_currentSegment
;
231 // Define the repainting behaviour
232 void MyCanvas::OnDraw(wxDC
& dc
)
235 m_view
->OnDraw(& dc
);
238 // This implements a tiny doodling program. Drag the mouse using the left
240 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
248 dc
.SetPen(*wxBLACK_PEN
);
250 const wxPoint
pt(event
.GetLogicalPosition(dc
));
252 // is this the end of the current segment?
253 if ( m_currentSegment
&& event
.LeftUp() )
255 if ( !m_currentSegment
->IsEmpty() )
257 // We've got a valid segment on mouse left up, so store it.
258 DrawingDocument
* const
259 doc
= wxStaticCast(m_view
->GetDocument(), DrawingDocument
);
261 doc
->GetCommandProcessor()->Submit(
262 new DrawingAddSegmentCommand(doc
, *m_currentSegment
));
267 delete m_currentSegment
;
268 m_currentSegment
= NULL
;
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
);