]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/view.cpp
fixing multilib build
[wxWidgets.git] / samples / docview / view.cpp
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
2d1df0fc
VZ
2// Name: samples/docview/view.cpp
3// Purpose: View classes implementation
457814b5 4// Author: Julian Smart
2d1df0fc 5// Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
457814b5
JS
6// Created: 04/01/98
7// RCS-ID: $Id$
2d1df0fc
VZ
8// Copyright: (c) 1998 Julian Smart
9// (c) 2008 Vadim Zeitlin
2f6c54eb 10// Licence: wxWindows license
457814b5
JS
11/////////////////////////////////////////////////////////////////////////////
12
457814b5
JS
13// For compilers that support precompilation, includes "wx/wx.h".
14#include "wx/wxprec.h"
15
16#ifdef __BORLANDC__
2d1df0fc 17 #pragma hdrstop
457814b5
JS
18#endif
19
20#ifndef WX_PRECOMP
2d1df0fc 21 #include "wx/wx.h"
457814b5
JS
22#endif
23
e4b19d9b 24#if !wxUSE_DOC_VIEW_ARCHITECTURE
2d1df0fc 25 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5
JS
26#endif
27
28#include "docview.h"
29#include "doc.h"
30#include "view.h"
31
2d1df0fc
VZ
32// ----------------------------------------------------------------------------
33// DrawingView implementation
34// ----------------------------------------------------------------------------
457814b5 35
2d1df0fc 36IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView)
457814b5
JS
37
38BEGIN_EVENT_TABLE(DrawingView, wxView)
2d1df0fc 39 EVT_MENU(wxID_CUT, DrawingView::OnCut)
457814b5
JS
40END_EVENT_TABLE()
41
42// What to do when a view is created. Creates actual
43// windows for displaying the view.
e3e65dac 44bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
457814b5 45{
2d1df0fc
VZ
46 MyApp& app = wxGetApp();
47 if ( app.GetMode() != MyApp::Mode_Single )
f6bcfd97 48 {
2d1df0fc
VZ
49 // create a new window and canvas inside it
50 m_frame = app.CreateChildFrame(doc, this, true);
51 m_frame->SetTitle("Drawing View");
52
53 m_canvas = new MyCanvas(this, m_frame);
6bdf5153 54 m_frame->Show(true);
f6bcfd97 55 }
2d1df0fc 56 else // single document mode
f6bcfd97 57 {
2d1df0fc
VZ
58 // reuse the existing window and canvas
59 m_frame = wxStaticCast(app.GetTopWindow(), wxFrame);
60 m_canvas = app.GetMainWindowCanvas();
61 m_canvas->SetView(this);
6bdf5153 62
f6bcfd97 63 // Associate the appropriate frame with this view.
6bdf5153
VZ
64 SetFrame(m_frame);
65
f6bcfd97
BP
66 // Make sure the document manager knows that this is the
67 // current view.
f2aea0d1 68 Activate(true);
6bdf5153 69
f6bcfd97 70 // Initialize the edit menu Undo and Redo items
2d1df0fc 71 doc->GetCommandProcessor()->SetEditMenu(app.GetMainWindowEditMenu());
f6bcfd97
BP
72 doc->GetCommandProcessor()->Initialize();
73 }
6bdf5153 74
f2aea0d1 75 return true;
457814b5
JS
76}
77
2d1df0fc
VZ
78// Sneakily gets used for default print/preview as well as drawing on the
79// screen.
457814b5
JS
80void DrawingView::OnDraw(wxDC *dc)
81{
f6bcfd97 82 dc->SetPen(*wxBLACK_PEN);
6bdf5153 83
2d1df0fc
VZ
84 // simply draw all lines of all segments
85 const DoodleSegments& segments = GetDocument()->GetSegments();
86 for ( DoodleSegments::const_iterator i = segments.begin();
87 i != segments.end();
88 ++i )
f6bcfd97 89 {
2d1df0fc
VZ
90 const DoodleLines& lines = i->GetLines();
91 for ( DoodleLines::const_iterator j = lines.begin();
92 j != lines.end();
93 ++j )
94 {
95 const DoodleLine& line = *j;
96
97 dc->DrawLine(line.x1, line.y1, line.x2, line.y2);
98 }
f6bcfd97 99 }
457814b5
JS
100}
101
6bdf5153
VZ
102DrawingDocument* DrawingView::GetDocument()
103{
104 return wxStaticCast(wxView::GetDocument(), DrawingDocument);
105}
106
f37f49b6 107void DrawingView::OnUpdate(wxView* sender, wxObject* hint)
457814b5 108{
f37f49b6 109 wxView::OnUpdate(sender, hint);
2d1df0fc 110 if ( m_canvas )
6bdf5153 111 m_canvas->Refresh();
457814b5
JS
112}
113
114// Clean up windows used for displaying the view.
115bool DrawingView::OnClose(bool deleteWindow)
116{
2d1df0fc 117 if ( !GetDocument()->Close() )
f2aea0d1 118 return false;
6bdf5153 119
f2aea0d1 120 Activate(false);
6bdf5153 121
2d1df0fc
VZ
122 // Clear the canvas in single-window mode in which it stays alive
123 if ( wxGetApp().GetMode() == MyApp::Mode_Single )
124 {
125 m_canvas->ClearBackground();
126 m_canvas->ResetView();
127 m_canvas = NULL;
128
129 if ( m_frame )
130 m_frame->SetTitle(wxTheApp->GetAppDisplayName());
131 }
132 else // not single window mode
f6bcfd97 133 {
2d1df0fc
VZ
134 if ( deleteWindow )
135 wxDELETE(m_frame);
f6bcfd97 136 }
2d1df0fc
VZ
137
138 SetFrame(NULL);
139
f2aea0d1 140 return true;
457814b5
JS
141}
142
e3e65dac 143void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
457814b5 144{
2d1df0fc
VZ
145 DrawingDocument * const doc = GetDocument();
146
147 doc->GetCommandProcessor()->Submit(new DrawingRemoveSegmentCommand(doc));
457814b5
JS
148}
149
2d1df0fc
VZ
150// ----------------------------------------------------------------------------
151// TextEditView implementation
152// ----------------------------------------------------------------------------
153
457814b5
JS
154IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
155
4e553af1
VZ
156BEGIN_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)
160END_EVENT_TABLE()
161
2d1df0fc 162bool TextEditView::OnCreate(wxDocument *doc, long WXUNUSED(flags))
457814b5 163{
6bdf5153 164 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
2d1df0fc
VZ
165 m_text = new wxTextCtrl(m_frame, wxID_ANY, "",
166 wxPoint(0, 0), m_frame->GetClientSize(),
167 wxTE_MULTILINE);
6bdf5153 168
2d1df0fc 169 m_frame->SetTitle("Text View");
6bdf5153 170 m_frame->Show(true);
2d1df0fc 171
f2aea0d1 172 Activate(true);
6bdf5153 173
f2aea0d1 174 return true;
457814b5
JS
175}
176
2d1df0fc 177void TextEditView::OnDraw(wxDC *WXUNUSED(dc))
457814b5 178{
2d1df0fc 179 // nothing to do here, wxTextCtrl draws itself
457814b5
JS
180}
181
182bool TextEditView::OnClose(bool deleteWindow)
183{
2d1df0fc 184 if ( !GetDocument()->Close() )
f2aea0d1 185 return false;
6bdf5153 186
f2aea0d1 187 Activate(false);
6bdf5153 188
2d1df0fc 189 if ( wxGetApp().GetMode() == MyApp::Mode_Single )
f6bcfd97 190 {
2d1df0fc 191 m_text->Clear();
f6bcfd97 192 }
2d1df0fc
VZ
193 else // not single window mode
194 {
195 if ( deleteWindow )
196 wxDELETE(m_frame);
197 }
198
f2aea0d1 199 return true;
457814b5
JS
200}
201
2d1df0fc
VZ
202// ----------------------------------------------------------------------------
203// MyCanvas implementation
204// ----------------------------------------------------------------------------
457814b5
JS
205
206BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
207 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
208END_EVENT_TABLE()
209
210// Define a constructor for my canvas
2d1df0fc
VZ
211MyCanvas::MyCanvas(wxView *view, wxWindow *parent)
212 : wxScrolledWindow(parent, wxID_ANY, wxPoint(0, 0), parent->GetClientSize())
457814b5 213{
6bdf5153 214 m_view = view;
2d1df0fc
VZ
215 m_currentSegment = NULL;
216 m_lastMousePos = wxDefaultPosition;
217
218 SetCursor(wxCursor(wxCURSOR_PENCIL));
219
220 // this is completely arbitrary and is done just for illustration purposes
221 SetVirtualSize(1000, 1000);
222 SetScrollRate(20, 20);
223
224 SetBackgroundColour(*wxWHITE);
225}
226
227MyCanvas::~MyCanvas()
228{
229 delete m_currentSegment;
457814b5
JS
230}
231
232// Define the repainting behaviour
233void MyCanvas::OnDraw(wxDC& dc)
234{
2d1df0fc 235 if ( m_view )
6bdf5153 236 m_view->OnDraw(& dc);
457814b5
JS
237}
238
2d1df0fc
VZ
239// This implements a tiny doodling program. Drag the mouse using the left
240// button.
457814b5
JS
241void MyCanvas::OnMouseEvent(wxMouseEvent& event)
242{
2d1df0fc 243 if ( !m_view )
f6bcfd97 244 return;
6bdf5153 245
f6bcfd97
BP
246 wxClientDC dc(this);
247 PrepareDC(dc);
6bdf5153 248
f6bcfd97 249 dc.SetPen(*wxBLACK_PEN);
6bdf5153 250
2d1df0fc 251 const wxPoint pt(event.GetLogicalPosition(dc));
6bdf5153 252
2d1df0fc
VZ
253 // is this the end of the current segment?
254 if ( m_currentSegment && event.LeftUp() )
457814b5 255 {
2d1df0fc 256 if ( !m_currentSegment->IsEmpty() )
f6bcfd97
BP
257 {
258 // We've got a valid segment on mouse left up, so store it.
2d1df0fc
VZ
259 DrawingDocument * const
260 doc = wxStaticCast(m_view->GetDocument(), DrawingDocument);
6bdf5153 261
2d1df0fc
VZ
262 doc->GetCommandProcessor()->Submit(
263 new DrawingAddSegmentCommand(doc, *m_currentSegment));
6bdf5153 264
2d1df0fc 265 doc->Modify(true);
f6bcfd97 266 }
2d1df0fc
VZ
267
268 delete m_currentSegment;
269 m_currentSegment = NULL;
457814b5 270 }
6bdf5153 271
2d1df0fc
VZ
272 // is this the start of a new segment?
273 if ( m_lastMousePos != wxDefaultPosition && event.Dragging() )
457814b5 274 {
2d1df0fc
VZ
275 if ( !m_currentSegment )
276 m_currentSegment = new DoodleSegment;
6bdf5153 277
2d1df0fc 278 m_currentSegment->AddLine(m_lastMousePos, pt);
6bdf5153 279
2d1df0fc 280 dc.DrawLine(m_lastMousePos, pt);
457814b5 281 }
457814b5 282
2d1df0fc 283 m_lastMousePos = pt;
457814b5
JS
284}
285
f37f49b6
JS
286// ----------------------------------------------------------------------------
287// wxImageCanvas implementation
288// ----------------------------------------------------------------------------
289
290BEGIN_EVENT_TABLE(wxImageCanvas, wxScrolledWindow)
291END_EVENT_TABLE()
292
293// Define a constructor for my canvas
294wxImageCanvas::wxImageCanvas(wxView* view, wxWindow* parent)
295 : wxScrolledWindow(parent, wxID_ANY, wxPoint(0, 0), parent->GetClientSize())
296{
297 m_view = view;
298}
299
300// Define the repainting behaviour
301void wxImageCanvas::OnDraw(wxDC& dc)
302{
303 if ( m_view )
304 m_view->OnDraw(& dc);
305}
306
307// ----------------------------------------------------------------------------
308// wxImageView implementation
309// ----------------------------------------------------------------------------
310
311IMPLEMENT_DYNAMIC_CLASS(wxImageView, wxView)
312
313BEGIN_EVENT_TABLE(wxImageView, wxView)
314END_EVENT_TABLE()
315
316wxImageDocument* wxImageView::GetDocument()
317{
318 return wxStaticCast(wxView::GetDocument(), wxImageDocument);
319}
320
321bool wxImageView::OnCreate(wxDocument* doc, long WXUNUSED(flags))
322{
323 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
324 m_frame->SetTitle("Image View");
325 m_canvas = new wxImageCanvas(this, m_frame);
326 m_frame->Show(true);
327 Activate(true);
328 return true;
329}
330
331void wxImageView::OnUpdate(wxView* sender, wxObject* hint)
332{
333 wxView::OnUpdate(sender, hint);
334 const wxImage* image = GetDocument()->GetImage();
335 if (image->IsOk())
336 {
337 m_canvas->SetScrollbars( 1, 1, image->GetWidth(), image->GetHeight() );
338 }
339}
340
341void wxImageView::OnDraw(wxDC* dc)
342{
343 const wxImage* image = GetDocument()->GetImage();
344 if (image->IsOk())
345 {
346 dc->DrawBitmap(wxBitmap(*image), 0, 0);
347 }
348}
349
350bool wxImageView::OnClose(bool deleteWindow)
351{
352 if ( !GetDocument()->Close() )
353 return false;
354
355 Activate(false);
356
357 if ( wxGetApp().GetMode() == MyApp::Mode_Single )
358 {
359 GetDocument()->DeleteContents();
360 }
361 else // not single window mode
362 {
363 if ( deleteWindow )
364 wxDELETE(m_frame);
365 }
366 return true;
367}
368