]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/view.cpp
fix undefined variable color to colour
[wxWidgets.git] / samples / docview / 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 if (!singleWindowMode)
46 {
47 // Multiple windows
48 m_frame = wxGetApp().CreateChildFrame(doc, this, true);
49 m_frame->SetTitle(wxT("DrawingView"));
50
51 m_canvas = GetMainFrame()->CreateCanvas(this, m_frame);
52 #ifdef __X__
53 // X seems to require a forced resize
54 int x, y;
55 m_frame->GetSize(&x, &y);
56 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
57 #endif
58 m_frame->Show(true);
59 }
60 else
61 {
62 // Single-window mode
63 m_frame = GetMainFrame();
64 m_canvas = GetMainFrame()->m_canvas;
65 m_canvas->m_view = this;
66
67 // Associate the appropriate frame with this view.
68 SetFrame(m_frame);
69
70 // Make sure the document manager knows that this is the
71 // current view.
72 Activate(true);
73
74 // Initialize the edit menu Undo and Redo items
75 doc->GetCommandProcessor()->SetEditMenu(((MyFrame*)m_frame)->m_editMenu);
76 doc->GetCommandProcessor()->Initialize();
77 }
78
79 return true;
80 }
81
82 // Sneakily gets used for default print/preview
83 // as well as drawing on the screen.
84 void DrawingView::OnDraw(wxDC *dc)
85 {
86 dc->SetFont(*wxNORMAL_FONT);
87 dc->SetPen(*wxBLACK_PEN);
88
89 wxList::compatibility_iterator node = GetDocument()->GetDoodleSegments().GetFirst();
90 while (node)
91 {
92 DoodleSegment *seg = (DoodleSegment *)node->GetData();
93 seg->Draw(dc);
94 node = node->GetNext();
95 }
96 }
97
98 DrawingDocument* DrawingView::GetDocument()
99 {
100 return wxStaticCast(wxView::GetDocument(), DrawingDocument);
101 }
102
103 void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
104 {
105 if (m_canvas)
106 m_canvas->Refresh();
107
108 /* Is the following necessary?
109 #ifdef __WXMSW__
110 if (canvas)
111 canvas->Refresh();
112 #else
113 if (canvas)
114 {
115 wxClientDC dc(canvas);
116 dc.Clear();
117 OnDraw(& dc);
118 }
119 #endif
120 */
121 }
122
123 // Clean up windows used for displaying the view.
124 bool DrawingView::OnClose(bool deleteWindow)
125 {
126 if (!GetDocument()->Close())
127 return false;
128
129 // Clear the canvas in case we're in single-window mode,
130 // and the canvas stays.
131 m_canvas->ClearBackground();
132 m_canvas->m_view = NULL;
133 m_canvas = NULL;
134
135 wxString s(wxTheApp->GetAppDisplayName());
136 if (m_frame)
137 m_frame->SetTitle(s);
138
139 SetFrame(NULL);
140
141 Activate(false);
142
143 if (deleteWindow && !singleWindowMode)
144 {
145 delete m_frame;
146 return true;
147 }
148 return true;
149 }
150
151 void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
152 {
153 DrawingDocument* doc = GetDocument();
154 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Cut Last Segment"), DOODLE_CUT, doc, NULL));
155 }
156
157 IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
158
159 BEGIN_EVENT_TABLE(TextEditView, wxView)
160 EVT_MENU(wxID_COPY, TextEditView::OnCopy)
161 EVT_MENU(wxID_PASTE, TextEditView::OnPaste)
162 EVT_MENU(wxID_SELECTALL, TextEditView::OnSelectAll)
163 END_EVENT_TABLE()
164
165 bool TextEditView::OnCreate(wxDocument *doc, long WXUNUSED(flags) )
166 {
167 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
168
169 wxSize size = m_frame->GetClientSize();
170 m_textsw = new MyTextWindow(this, m_frame, wxPoint(0, 0), size, wxTE_MULTILINE);
171 m_frame->SetTitle(wxT("TextEditView"));
172
173 #ifdef __X__
174 // X seems to require a forced resize
175 int x, y;
176 m_frame->GetSize(&x, &y);
177 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
178 #endif
179
180 m_frame->Show(true);
181 Activate(true);
182
183 return true;
184 }
185
186 // Handled by wxTextWindow
187 void TextEditView::OnDraw(wxDC *WXUNUSED(dc) )
188 {
189 }
190
191 void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
192 {
193 }
194
195 bool TextEditView::OnClose(bool deleteWindow)
196 {
197 if (!GetDocument()->Close())
198 return false;
199
200 Activate(false);
201
202 if (deleteWindow)
203 {
204 wxDELETE(m_frame);
205 }
206 return true;
207 }
208
209 /*
210 * Window implementations
211 */
212
213 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
214 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
215 END_EVENT_TABLE()
216
217 // Define a constructor for my canvas
218 MyCanvas::MyCanvas(DrawingView* view, wxFrame* frame, const wxPoint& pos, const wxSize& size, const long style):
219 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
220 {
221 m_view = view;
222 }
223
224 // Define the repainting behaviour
225 void MyCanvas::OnDraw(wxDC& dc)
226 {
227 if (m_view)
228 m_view->OnDraw(& dc);
229 }
230
231 // This implements a tiny doodling program. Drag the mouse using
232 // the left button.
233 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
234 {
235 if (!m_view)
236 return;
237
238 static DoodleSegment *currentSegment = NULL;
239
240 wxClientDC dc(this);
241 PrepareDC(dc);
242
243 dc.SetPen(*wxBLACK_PEN);
244
245 wxPoint pt(event.GetLogicalPosition(dc));
246
247 if (currentSegment && event.LeftUp())
248 {
249 if (currentSegment->m_lines.GetCount() == 0)
250 {
251 delete currentSegment;
252 currentSegment = NULL;
253 }
254 else
255 {
256 // We've got a valid segment on mouse left up, so store it.
257 DrawingDocument* doc = m_view->GetDocument();
258
259 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD, doc, currentSegment));
260
261 m_view->GetDocument()->Modify(true);
262 currentSegment = NULL;
263 }
264 }
265
266 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
267 {
268 if (!currentSegment)
269 currentSegment = new DoodleSegment;
270
271 DoodleLine *newLine = new DoodleLine;
272 newLine->x1 = (long)xpos;
273 newLine->y1 = (long)ypos;
274 newLine->x2 = pt.x;
275 newLine->y2 = pt.y;
276 currentSegment->m_lines.Append(newLine);
277
278 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
279 }
280 xpos = pt.x;
281 ypos = pt.y;
282 }
283
284 // Define a constructor for my text subwindow
285 MyTextWindow::MyTextWindow(wxView* view, wxFrame* frame, const wxPoint& pos, const wxSize& size, const long style):
286 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
287 {
288 m_view = view;
289 }
290
291