]> git.saurik.com Git - wxWidgets.git/blame - samples/docvwmdi/view.cpp
no changes, just some cleanup (patch 1918720)
[wxWidgets.git] / samples / docvwmdi / view.cpp
CommitLineData
2108f33a
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: view.cpp
3// Purpose: View classes
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6aa89a22 8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows license
2108f33a
JS
10/////////////////////////////////////////////////////////////////////////////
11
2108f33a
JS
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
e4b19d9b 23#if !wxUSE_DOC_VIEW_ARCHITECTURE
ad813b00 24#error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
2108f33a
JS
25#endif
26
27#include "docview.h"
28#include "doc.h"
29#include "view.h"
30
31IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView)
32
33// For drawing lines in a canvas
6bdf5153
VZ
34static float xpos = -1;
35static float ypos = -1;
2108f33a
JS
36
37BEGIN_EVENT_TABLE(DrawingView, wxView)
38 EVT_MENU(DOODLE_CUT, DrawingView::OnCut)
39END_EVENT_TABLE()
40
41// What to do when a view is created. Creates actual
42// windows for displaying the view.
43bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
44{
6bdf5153
VZ
45 m_frame = wxGetApp().CreateChildFrame(doc, this, true);
46 m_frame->SetTitle(wxT("DrawingView"));
2108f33a 47
6bdf5153 48 m_canvas = GetMainFrame()->CreateCanvas(this, m_frame);
2108f33a
JS
49#ifdef __X__
50 // X seems to require a forced resize
51 int x, y;
6bdf5153
VZ
52 m_frame->GetSize(&x, &y);
53 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
2108f33a 54#endif
6bdf5153 55 m_frame->Show(true);
691d944f 56 Activate(true);
2108f33a 57
691d944f 58 return true;
2108f33a
JS
59}
60
6bdf5153
VZ
61DrawingDocument* DrawingView::GetDocument()
62{
63 return wxStaticCast(wxView::GetDocument(), DrawingDocument);
64}
65
2108f33a
JS
66// Sneakily gets used for default print/preview
67// as well as drawing on the screen.
68void DrawingView::OnDraw(wxDC *dc)
69{
6bdf5153
VZ
70 dc->SetFont(*wxNORMAL_FONT);
71 dc->SetPen(*wxBLACK_PEN);
72
73 wxList::compatibility_iterator node = GetDocument()->GetDoodleSegments().GetFirst();
74 while (node)
75 {
76 DoodleSegment* seg = (DoodleSegment*)node->GetData();
77 seg->Draw(dc);
78 node = node->GetNext();
79 }
2108f33a
JS
80}
81
82void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
83{
6bdf5153
VZ
84 if (m_canvas)
85 m_canvas->Refresh();
2108f33a
JS
86
87/* Is the following necessary?
88#ifdef __WXMSW__
6bdf5153
VZ
89 if (canvas)
90 canvas->Refresh();
2108f33a 91#else
6bdf5153 92 if (canvas)
2108f33a 93 {
6bdf5153
VZ
94 wxClientDC dc(canvas);
95 dc.Clear();
96 OnDraw(& dc);
2108f33a
JS
97 }
98#endif
99*/
100}
101
102// Clean up windows used for displaying the view.
103bool DrawingView::OnClose(bool deleteWindow)
104{
6bdf5153
VZ
105 if (!GetDocument()->Close())
106 return false;
2108f33a 107
6bdf5153
VZ
108 // Clear the canvas in case we're in single-window mode,
109 // and the canvas stays.
110 m_canvas->ClearBackground();
111 m_canvas->m_view = NULL;
112 m_canvas = NULL;
2108f33a 113
6bdf5153
VZ
114 wxString s(wxTheApp->GetAppDisplayName());
115 if (m_frame)
116 m_frame->SetTitle(s);
2108f33a 117
6bdf5153 118 SetFrame(NULL);
2108f33a 119
6bdf5153 120 Activate(false);
1d14c005 121
6bdf5153
VZ
122 if (deleteWindow)
123 {
124 delete m_frame;
125 return true;
126 }
691d944f 127 return true;
2108f33a
JS
128}
129
130void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
131{
6bdf5153
VZ
132 DrawingDocument* doc = GetDocument();
133 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Cut Last Segment"), DOODLE_CUT, doc, NULL));
2108f33a
JS
134}
135
136IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
137
6bdf5153 138bool TextEditView::OnCreate(wxDocument* doc, long WXUNUSED(flags) )
2108f33a 139{
6bdf5153 140 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
2108f33a 141
6bdf5153
VZ
142 wxSize size = m_frame->GetClientSize();
143 m_textsw = new MyTextWindow(this, m_frame, wxPoint(0, 0), size, wxTE_MULTILINE);
144 m_frame->SetTitle(wxT("TextEditView"));
2108f33a
JS
145
146#ifdef __X__
6bdf5153
VZ
147 // X seems to require a forced resize
148 int x, y;
149 m_frame->GetSize(&x, &y);
150 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
2108f33a
JS
151#endif
152
6bdf5153
VZ
153 m_frame->Show(true);
154 Activate(true);
1d14c005 155
6bdf5153 156 return true;
2108f33a
JS
157}
158
159// Handled by wxTextWindow
160void TextEditView::OnDraw(wxDC *WXUNUSED(dc) )
161{
162}
163
164void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
165{
166}
167
168bool TextEditView::OnClose(bool deleteWindow)
169{
6bdf5153
VZ
170 if (!GetDocument()->Close())
171 return false;
1d14c005 172
6bdf5153 173 Activate(false);
2108f33a 174
6bdf5153
VZ
175 if (deleteWindow)
176 {
177 delete m_frame;
178 return true;
179 }
691d944f 180 return true;
6bdf5153
VZ
181}
182
183bool TextEditView::ProcessEvent(wxEvent& event)
184{
185 bool processed = false;
186 if (!processed) switch (event.GetId())
187 {
188 case wxID_COPY:
189 case wxID_PASTE:
190 case wxID_SELECTALL:
191 processed = m_textsw->ProcessEvent(event);
192 break;
193 }
194 if (!processed) processed = wxView::ProcessEvent(event);
195 return processed;
2108f33a
JS
196}
197
198/*
199 * Window implementations
200 */
201
202BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
203 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
204END_EVENT_TABLE()
205
206// Define a constructor for my canvas
6bdf5153 207MyCanvas::MyCanvas(DrawingView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
691d944f 208 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
2108f33a 209{
6bdf5153 210 m_view = view;
2108f33a
JS
211}
212
213// Define the repainting behaviour
214void MyCanvas::OnDraw(wxDC& dc)
215{
6bdf5153
VZ
216 if (m_view)
217 m_view->OnDraw(& dc);
2108f33a
JS
218}
219
220// This implements a tiny doodling program. Drag the mouse using
221// the left button.
222void MyCanvas::OnMouseEvent(wxMouseEvent& event)
223{
6bdf5153
VZ
224 if (!m_view)
225 return;
1d14c005 226
6bdf5153 227 static DoodleSegment* currentSegment = NULL;
2108f33a 228
6bdf5153
VZ
229 wxClientDC dc(this);
230 PrepareDC(dc);
2108f33a 231
6bdf5153 232 dc.SetPen(*wxBLACK_PEN);
2108f33a 233
6bdf5153 234 wxPoint pt(event.GetLogicalPosition(dc));
2108f33a 235
6bdf5153 236 if (currentSegment && event.LeftUp())
2108f33a 237 {
6bdf5153
VZ
238 if (currentSegment->m_lines.GetCount() == 0)
239 {
240 delete currentSegment;
241 currentSegment = NULL;
242 }
243 else
244 {
245 // We've got a valid segment on mouse left up, so store it.
246 DrawingDocument* doc = m_view->GetDocument();
247
248 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD, doc, currentSegment));
249
250 m_view->GetDocument()->Modify(true);
251 currentSegment = NULL;
252 }
2108f33a 253 }
6bdf5153
VZ
254
255 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
2108f33a 256 {
6bdf5153
VZ
257 if (!currentSegment)
258 currentSegment = new DoodleSegment;
2108f33a 259
6bdf5153
VZ
260 DoodleLine *newLine = new DoodleLine;
261 newLine->x1 = (long)xpos;
262 newLine->y1 = (long)ypos;
263 newLine->x2 = pt.x;
264 newLine->y2 = pt.y;
265 currentSegment->m_lines.Append(newLine);
2108f33a 266
6bdf5153 267 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
2108f33a 268 }
6bdf5153
VZ
269 xpos = pt.x;
270 ypos = pt.y;
2108f33a
JS
271}
272
273// Define a constructor for my text subwindow
6bdf5153
VZ
274MyTextWindow::MyTextWindow(wxView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
275 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
2108f33a 276{
6bdf5153 277 m_view = view;
2108f33a
JS
278}
279