]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/view.cpp
4cbb1e6084703a4811a1fcdb4157c5b656385f27
[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 return true;
206 }
207 return true;
208 }
209
210 /*
211 * Window implementations
212 */
213
214 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
215 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
216 END_EVENT_TABLE()
217
218 // Define a constructor for my canvas
219 MyCanvas::MyCanvas(DrawingView* view, wxFrame* frame, const wxPoint& pos, const wxSize& size, const long style):
220 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
221 {
222 m_view = view;
223 }
224
225 // Define the repainting behaviour
226 void MyCanvas::OnDraw(wxDC& dc)
227 {
228 if (m_view)
229 m_view->OnDraw(& dc);
230 }
231
232 // This implements a tiny doodling program. Drag the mouse using
233 // the left button.
234 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
235 {
236 if (!m_view)
237 return;
238
239 static DoodleSegment *currentSegment = NULL;
240
241 wxClientDC dc(this);
242 PrepareDC(dc);
243
244 dc.SetPen(*wxBLACK_PEN);
245
246 wxPoint pt(event.GetLogicalPosition(dc));
247
248 if (currentSegment && event.LeftUp())
249 {
250 if (currentSegment->m_lines.GetCount() == 0)
251 {
252 delete currentSegment;
253 currentSegment = NULL;
254 }
255 else
256 {
257 // We've got a valid segment on mouse left up, so store it.
258 DrawingDocument* doc = m_view->GetDocument();
259
260 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD, doc, currentSegment));
261
262 m_view->GetDocument()->Modify(true);
263 currentSegment = NULL;
264 }
265 }
266
267 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
268 {
269 if (!currentSegment)
270 currentSegment = new DoodleSegment;
271
272 DoodleLine *newLine = new DoodleLine;
273 newLine->x1 = (long)xpos;
274 newLine->y1 = (long)ypos;
275 newLine->x2 = pt.x;
276 newLine->y2 = pt.y;
277 currentSegment->m_lines.Append(newLine);
278
279 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
280 }
281 xpos = pt.x;
282 ypos = pt.y;
283 }
284
285 // Define a constructor for my text subwindow
286 MyTextWindow::MyTextWindow(wxView* view, wxFrame* frame, const wxPoint& pos, const wxSize& size, const long style):
287 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
288 {
289 m_view = view;
290 }
291
292