]>
Commit | Line | Data |
---|---|---|
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 and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | // #pragma implementation | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #if !USE_DOC_VIEW_ARCHITECTURE | |
28 | #error You must set USE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h! | |
29 | #endif | |
30 | ||
31 | #include "docview.h" | |
32 | #include "doc.h" | |
33 | #include "view.h" | |
34 | ||
35 | IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView) | |
36 | ||
37 | // For drawing lines in a canvas | |
38 | float xpos = -1; | |
39 | float ypos = -1; | |
40 | ||
41 | BEGIN_EVENT_TABLE(DrawingView, wxView) | |
42 | EVT_MENU(DOODLE_CUT, DrawingView::OnCut) | |
43 | END_EVENT_TABLE() | |
44 | ||
45 | // What to do when a view is created. Creates actual | |
46 | // windows for displaying the view. | |
47 | bool DrawingView::OnCreate(wxDocument *doc, long WXUNUSED(flags) ) | |
48 | { | |
49 | frame = wxGetApp().CreateChildFrame(doc, this, TRUE); | |
50 | frame->SetTitle("DrawingView"); | |
51 | ||
52 | canvas = GetMainFrame()->CreateCanvas(this, frame); | |
53 | #ifdef __X__ | |
54 | // X seems to require a forced resize | |
55 | int x, y; | |
56 | frame->GetSize(&x, &y); | |
57 | frame->SetSize(x, y); | |
58 | #endif | |
59 | frame->Show(TRUE); | |
60 | ||
61 | return TRUE; | |
62 | } | |
63 | ||
64 | // Sneakily gets used for default print/preview | |
65 | // as well as drawing on the screen. | |
66 | void DrawingView::OnDraw(wxDC *dc) | |
67 | { | |
68 | dc->SetFont(*wxNORMAL_FONT); | |
69 | dc->SetPen(*wxBLACK_PEN); | |
70 | ||
71 | wxNode *node = ((DrawingDocument *)GetDocument())->GetDoodleSegments().First(); | |
72 | while (node) | |
73 | { | |
74 | DoodleSegment *seg = (DoodleSegment *)node->Data(); | |
75 | seg->Draw(dc); | |
76 | node = node->Next(); | |
77 | } | |
78 | } | |
79 | ||
80 | void DrawingView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint)) | |
81 | { | |
82 | if (canvas) | |
83 | canvas->Refresh(); | |
84 | ||
85 | /* Is the following necessary? | |
86 | #ifdef __WXMSW__ | |
87 | if (canvas) | |
88 | canvas->Refresh(); | |
89 | #else | |
90 | if (canvas) | |
91 | { | |
92 | wxClientDC dc(canvas); | |
93 | dc.Clear(); | |
94 | OnDraw(& dc); | |
95 | } | |
96 | #endif | |
97 | */ | |
98 | } | |
99 | ||
100 | // Clean up windows used for displaying the view. | |
101 | bool DrawingView::OnClose(bool deleteWindow) | |
102 | { | |
103 | if (!GetDocument()->Close()) | |
104 | return FALSE; | |
105 | ||
106 | // Clear the canvas in case we're in single-window mode, | |
107 | // and the canvas stays. | |
108 | canvas->Clear(); | |
109 | canvas->view = (wxView *) NULL; | |
110 | canvas = (MyCanvas *) NULL; | |
111 | ||
112 | wxString s(wxTheApp->GetAppName()); | |
113 | if (frame) | |
114 | frame->SetTitle(s); | |
115 | ||
116 | SetFrame((wxFrame*)NULL); | |
117 | ||
118 | Activate(FALSE); | |
119 | ||
120 | if (deleteWindow) | |
121 | { | |
122 | delete frame; | |
123 | return TRUE; | |
124 | } | |
125 | return TRUE; | |
126 | } | |
127 | ||
128 | void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) ) | |
129 | { | |
130 | DrawingDocument *doc = (DrawingDocument *)GetDocument(); | |
131 | doc->GetCommandProcessor()->Submit(new DrawingCommand((const wxString) "Cut Last Segment", DOODLE_CUT, doc, (DoodleSegment *) NULL)); | |
132 | } | |
133 | ||
134 | IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView) | |
135 | ||
136 | bool TextEditView::OnCreate(wxDocument *doc, long WXUNUSED(flags) ) | |
137 | { | |
138 | frame = wxGetApp().CreateChildFrame(doc, this, FALSE); | |
139 | ||
140 | int width, height; | |
141 | frame->GetClientSize(&width, &height); | |
142 | textsw = new MyTextWindow(this, frame, wxPoint(0, 0), wxSize(width, height), wxTE_MULTILINE); | |
143 | frame->SetTitle("TextEditView"); | |
144 | ||
145 | #ifdef __X__ | |
146 | // X seems to require a forced resize | |
147 | int x, y; | |
148 | frame->GetSize(&x, &y); | |
149 | frame->SetSize(x, y); | |
150 | #endif | |
151 | ||
152 | frame->Show(TRUE); | |
153 | Activate(TRUE); | |
154 | ||
155 | return TRUE; | |
156 | } | |
157 | ||
158 | // Handled by wxTextWindow | |
159 | void TextEditView::OnDraw(wxDC *WXUNUSED(dc) ) | |
160 | { | |
161 | } | |
162 | ||
163 | void TextEditView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint) ) | |
164 | { | |
165 | } | |
166 | ||
167 | bool TextEditView::OnClose(bool deleteWindow) | |
168 | { | |
169 | if (!GetDocument()->Close()) | |
170 | return FALSE; | |
171 | ||
172 | Activate(FALSE); | |
173 | ||
174 | if (deleteWindow) | |
175 | { | |
176 | delete frame; | |
177 | return TRUE; | |
178 | } | |
179 | return TRUE; | |
180 | } | |
181 | ||
182 | /* | |
183 | * Window implementations | |
184 | */ | |
185 | ||
186 | BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow) | |
187 | EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent) | |
188 | END_EVENT_TABLE() | |
189 | ||
190 | // Define a constructor for my canvas | |
191 | MyCanvas::MyCanvas(wxView *v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style): | |
192 | wxScrolledWindow(frame, -1, pos, size, style) | |
193 | { | |
194 | view = v; | |
195 | } | |
196 | ||
197 | // Define the repainting behaviour | |
198 | void MyCanvas::OnDraw(wxDC& dc) | |
199 | { | |
200 | if (view) | |
201 | view->OnDraw(& dc); | |
202 | } | |
203 | ||
204 | // This implements a tiny doodling program. Drag the mouse using | |
205 | // the left button. | |
206 | void MyCanvas::OnMouseEvent(wxMouseEvent& event) | |
207 | { | |
208 | if (!view) | |
209 | return; | |
210 | ||
211 | static DoodleSegment *currentSegment = (DoodleSegment *) NULL; | |
212 | ||
213 | wxClientDC dc(this); | |
214 | PrepareDC(dc); | |
215 | ||
216 | dc.SetPen(*wxBLACK_PEN); | |
217 | ||
218 | wxPoint pt(event.GetLogicalPosition(dc)); | |
219 | ||
220 | if (currentSegment && event.LeftUp()) | |
221 | { | |
222 | if (currentSegment->lines.Number() == 0) | |
223 | { | |
224 | delete currentSegment; | |
225 | currentSegment = (DoodleSegment *) NULL; | |
226 | } | |
227 | else | |
228 | { | |
229 | // We've got a valid segment on mouse left up, so store it. | |
230 | DrawingDocument *doc = (DrawingDocument *)view->GetDocument(); | |
231 | ||
232 | doc->GetCommandProcessor()->Submit(new DrawingCommand("Add Segment", DOODLE_ADD, doc, currentSegment)); | |
233 | ||
234 | view->GetDocument()->Modify(TRUE); | |
235 | currentSegment = (DoodleSegment *) NULL; | |
236 | } | |
237 | } | |
238 | ||
239 | if (xpos > -1 && ypos > -1 && event.Dragging()) | |
240 | { | |
241 | if (!currentSegment) | |
242 | currentSegment = new DoodleSegment; | |
243 | ||
244 | DoodleLine *newLine = new DoodleLine; | |
245 | newLine->x1 = (long)xpos; | |
246 | newLine->y1 = (long)ypos; | |
247 | newLine->x2 = pt.x; | |
248 | newLine->y2 = pt.y; | |
249 | currentSegment->lines.Append(newLine); | |
250 | ||
251 | dc.DrawLine( (long)xpos, (long)ypos, pt.x, pt.y); | |
252 | } | |
253 | xpos = pt.x; | |
254 | ypos = pt.y; | |
255 | } | |
256 | ||
257 | // Define a constructor for my text subwindow | |
258 | MyTextWindow::MyTextWindow(wxView *v, wxFrame *frame, const wxPoint& pos, const wxSize& size, const long style): | |
259 | wxTextCtrl(frame, -1, "", pos, size, style) | |
260 | { | |
261 | view = v; | |
262 | } | |
263 | ||
264 |