]> git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/view.cpp
bb2c0df2973ac33d05cd4abc37935af0c1efff0b
[wxWidgets.git] / samples / docvwmdi / view.cpp
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$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
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
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
25 #endif
26
27 #include "docview.h"
28 #include "doc.h"
29 #include "view.h"
30
31 IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView)
32
33 // For drawing lines in a canvas
34 static float xpos = -1;
35 static float ypos = -1;
36
37 BEGIN_EVENT_TABLE(DrawingView, wxView)
38 EVT_MENU(DOODLE_CUT, DrawingView::OnCut)
39 END_EVENT_TABLE()
40
41 // What to do when a view is created. Creates actual
42 // windows for displaying the view.
43 bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
44 {
45 m_frame = wxGetApp().CreateChildFrame(doc, this, true);
46 m_frame->SetTitle(wxT("DrawingView"));
47
48 m_canvas = GetMainFrame()->CreateCanvas(this, m_frame);
49 #ifdef __X__
50 // X seems to require a forced resize
51 int x, y;
52 m_frame->GetSize(&x, &y);
53 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
54 #endif
55 m_frame->Show(true);
56 Activate(true);
57
58 return true;
59 }
60
61 DrawingDocument* DrawingView::GetDocument()
62 {
63 return wxStaticCast(wxView::GetDocument(), DrawingDocument);
64 }
65
66 // Sneakily gets used for default print/preview
67 // as well as drawing on the screen.
68 void DrawingView::OnDraw(wxDC *dc)
69 {
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 }
80 }
81
82 void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
83 {
84 if (m_canvas)
85 m_canvas->Refresh();
86
87 /* Is the following necessary?
88 #ifdef __WXMSW__
89 if (canvas)
90 canvas->Refresh();
91 #else
92 if (canvas)
93 {
94 wxClientDC dc(canvas);
95 dc.Clear();
96 OnDraw(& dc);
97 }
98 #endif
99 */
100 }
101
102 // Clean up windows used for displaying the view.
103 bool DrawingView::OnClose(bool deleteWindow)
104 {
105 if (!GetDocument()->Close())
106 return false;
107
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;
113
114 wxString s(wxTheApp->GetAppDisplayName());
115 if (m_frame)
116 m_frame->SetTitle(s);
117
118 SetFrame(NULL);
119
120 Activate(false);
121
122 if (deleteWindow)
123 {
124 delete m_frame;
125 return true;
126 }
127 return true;
128 }
129
130 void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
131 {
132 DrawingDocument* doc = GetDocument();
133 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Cut Last Segment"), DOODLE_CUT, doc, NULL));
134 }
135
136 IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
137
138 bool TextEditView::OnCreate(wxDocument* doc, long WXUNUSED(flags) )
139 {
140 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
141
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"));
145
146 #ifdef __X__
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);
151 #endif
152
153 m_frame->Show(true);
154 Activate(true);
155
156 return true;
157 }
158
159 // Handled by wxTextWindow
160 void TextEditView::OnDraw(wxDC *WXUNUSED(dc) )
161 {
162 }
163
164 void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
165 {
166 }
167
168 bool TextEditView::OnClose(bool deleteWindow)
169 {
170 if (!GetDocument()->Close())
171 return false;
172
173 Activate(false);
174
175 if (deleteWindow)
176 {
177 delete m_frame;
178 return true;
179 }
180 return true;
181 }
182
183 bool 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;
196 }
197
198 /*
199 * Window implementations
200 */
201
202 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
203 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
204 END_EVENT_TABLE()
205
206 // Define a constructor for my canvas
207 MyCanvas::MyCanvas(DrawingView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
208 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
209 {
210 m_view = view;
211 }
212
213 // Define the repainting behaviour
214 void MyCanvas::OnDraw(wxDC& dc)
215 {
216 if (m_view)
217 m_view->OnDraw(& dc);
218 }
219
220 // This implements a tiny doodling program. Drag the mouse using
221 // the left button.
222 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
223 {
224 if (!m_view)
225 return;
226
227 static DoodleSegment* currentSegment = NULL;
228
229 wxClientDC dc(this);
230 PrepareDC(dc);
231
232 dc.SetPen(*wxBLACK_PEN);
233
234 wxPoint pt(event.GetLogicalPosition(dc));
235
236 if (currentSegment && event.LeftUp())
237 {
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 }
253 }
254
255 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
256 {
257 if (!currentSegment)
258 currentSegment = new DoodleSegment;
259
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);
266
267 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
268 }
269 xpos = pt.x;
270 ypos = pt.y;
271 }
272
273 // Define a constructor for my text subwindow
274 MyTextWindow::MyTextWindow(wxView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
275 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
276 {
277 m_view = view;
278 }
279