]> git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/view.cpp
compilation fix for wxX11
[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 BEGIN_EVENT_TABLE(TextEditView, wxView)
139 EVT_MENU(wxID_COPY, TextEditView::OnCopy)
140 EVT_MENU(wxID_PASTE, TextEditView::OnPaste)
141 EVT_MENU(wxID_SELECTALL, TextEditView::OnSelectAll)
142 END_EVENT_TABLE()
143
144 bool TextEditView::OnCreate(wxDocument* doc, long WXUNUSED(flags) )
145 {
146 m_frame = wxGetApp().CreateChildFrame(doc, this, false);
147
148 wxSize size = m_frame->GetClientSize();
149 m_textsw = new MyTextWindow(this, m_frame, wxPoint(0, 0), size, wxTE_MULTILINE);
150 m_frame->SetTitle(wxT("TextEditView"));
151
152 #ifdef __X__
153 // X seems to require a forced resize
154 int x, y;
155 m_frame->GetSize(&x, &y);
156 m_frame->SetSize(wxDefaultCoord, wxDefaultCoord, x, y);
157 #endif
158
159 m_frame->Show(true);
160 Activate(true);
161
162 return true;
163 }
164
165 // Handled by wxTextWindow
166 void TextEditView::OnDraw(wxDC *WXUNUSED(dc) )
167 {
168 }
169
170 void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) )
171 {
172 }
173
174 bool TextEditView::OnClose(bool deleteWindow)
175 {
176 if (!GetDocument()->Close())
177 return false;
178
179 Activate(false);
180
181 if (deleteWindow)
182 {
183 delete m_frame;
184 return true;
185 }
186 return true;
187 }
188
189 bool TextEditView::ProcessEvent(wxEvent& event)
190 {
191 bool processed = false;
192 if (!processed) switch (event.GetId())
193 {
194 case wxID_COPY:
195 case wxID_PASTE:
196 case wxID_SELECTALL:
197 processed = m_textsw->ProcessEvent(event);
198 break;
199 }
200 if (!processed) processed = wxView::ProcessEvent(event);
201 return processed;
202 }
203
204 /*
205 * Window implementations
206 */
207
208 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
209 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
210 END_EVENT_TABLE()
211
212 // Define a constructor for my canvas
213 MyCanvas::MyCanvas(DrawingView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
214 wxScrolledWindow(frame, wxID_ANY, pos, size, style)
215 {
216 m_view = view;
217 }
218
219 // Define the repainting behaviour
220 void MyCanvas::OnDraw(wxDC& dc)
221 {
222 if (m_view)
223 m_view->OnDraw(& dc);
224 }
225
226 // This implements a tiny doodling program. Drag the mouse using
227 // the left button.
228 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
229 {
230 if (!m_view)
231 return;
232
233 static DoodleSegment* currentSegment = NULL;
234
235 wxClientDC dc(this);
236 PrepareDC(dc);
237
238 dc.SetPen(*wxBLACK_PEN);
239
240 wxPoint pt(event.GetLogicalPosition(dc));
241
242 if (currentSegment && event.LeftUp())
243 {
244 if (currentSegment->m_lines.GetCount() == 0)
245 {
246 delete currentSegment;
247 currentSegment = NULL;
248 }
249 else
250 {
251 // We've got a valid segment on mouse left up, so store it.
252 DrawingDocument* doc = m_view->GetDocument();
253
254 doc->GetCommandProcessor()->Submit(new DrawingCommand(wxT("Add Segment"), DOODLE_ADD, doc, currentSegment));
255
256 m_view->GetDocument()->Modify(true);
257 currentSegment = NULL;
258 }
259 }
260
261 if ( (xpos > -1) && (ypos > -1) && event.Dragging())
262 {
263 if (!currentSegment)
264 currentSegment = new DoodleSegment;
265
266 DoodleLine *newLine = new DoodleLine;
267 newLine->x1 = (long)xpos;
268 newLine->y1 = (long)ypos;
269 newLine->x2 = pt.x;
270 newLine->y2 = pt.y;
271 currentSegment->m_lines.Append(newLine);
272
273 dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y);
274 }
275 xpos = pt.x;
276 ypos = pt.y;
277 }
278
279 // Define a constructor for my text subwindow
280 MyTextWindow::MyTextWindow(wxView* view, wxMDIChildFrame* frame, const wxPoint& pos, const wxSize& size, long style):
281 wxTextCtrl(frame, wxID_ANY, wxEmptyString, pos, size, style)
282 {
283 m_view = view;
284 }
285