]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: view.cpp | |
3 | // Purpose: Implements view functionality in OGLEdit | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 12/07/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | // #pragma implementation | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "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 | #include <wx/colordlg.h> | |
28 | ||
29 | #if !wxUSE_DOC_VIEW_ARCHITECTURE | |
30 | #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h! | |
31 | #endif | |
32 | ||
33 | #include "ogledit.h" | |
34 | #include "doc.h" | |
35 | #include "view.h" | |
36 | #include "palette.h" | |
37 | ||
38 | IMPLEMENT_DYNAMIC_CLASS(DiagramView, wxView) | |
39 | ||
40 | BEGIN_EVENT_TABLE(DiagramView, wxView) | |
41 | EVT_MENU(OGLEDIT_CUT, DiagramView::OnCut) | |
42 | EVT_MENU(OGLEDIT_CHANGE_BACKGROUND_COLOUR, DiagramView::OnChangeBackgroundColour) | |
43 | EVT_MENU(OGLEDIT_EDIT_LABEL, DiagramView::OnEditLabel) | |
44 | END_EVENT_TABLE() | |
45 | ||
46 | // What to do when a view is created. Creates actual | |
47 | // windows for displaying the view. | |
48 | bool DiagramView::OnCreate(wxDocument *doc, long flags) | |
49 | { | |
50 | frame = GetMainFrame(); | |
51 | canvas = GetMainFrame()->canvas; | |
52 | canvas->view = this; | |
53 | ||
54 | SetFrame(frame); | |
55 | Activate(TRUE); | |
56 | ||
57 | // Initialize the edit menu Undo and Redo items | |
58 | doc->GetCommandProcessor()->SetEditMenu(((MyFrame *)frame)->editMenu); | |
59 | doc->GetCommandProcessor()->Initialize(); | |
60 | ||
61 | wxShapeCanvas *shapeCanvas = (wxShapeCanvas *)canvas; | |
62 | DiagramDocument *diagramDoc = (DiagramDocument *)doc; | |
63 | shapeCanvas->SetDiagram(diagramDoc->GetDiagram()); | |
64 | diagramDoc->GetDiagram()->SetCanvas(shapeCanvas); | |
65 | ||
66 | return TRUE; | |
67 | } | |
68 | ||
69 | #define CENTER FALSE // Place the drawing to the center of the page | |
70 | ||
71 | ||
72 | // Sneakily gets used for default print/preview | |
73 | // as well as drawing on the screen. | |
74 | void DiagramView::OnDraw(wxDC *dc) | |
75 | { | |
76 | ||
77 | /* You might use THIS code if you were scaling | |
78 | * graphics of known size to fit on the page. | |
79 | */ | |
80 | int w, h; | |
81 | ||
82 | // We need to adjust for the graphic size, a formula will be added | |
83 | float maxX = 900; | |
84 | float maxY = 700; | |
85 | // A better way of find the maxium values would be to search through | |
86 | // the linked list | |
87 | ||
88 | // Let's have at least 10 device units margin | |
89 | float marginX = 10; | |
90 | float marginY = 10; | |
91 | ||
92 | // Add the margin to the graphic size | |
93 | maxX += (2 * marginX); | |
94 | maxY += (2 * marginY); | |
95 | ||
96 | // Get the size of the DC in pixels | |
97 | dc->GetSize (&w, &h); | |
98 | ||
99 | // Calculate a suitable scaling factor | |
100 | float scaleX = (float) (w / maxX); | |
101 | float scaleY = (float) (h / maxY); | |
102 | ||
103 | // Use x or y scaling factor, whichever fits on the DC | |
104 | float actualScale = wxMin (scaleX, scaleY); | |
105 | ||
106 | float posX, posY; | |
107 | // Calculate the position on the DC for centring the graphic | |
108 | if (CENTER == TRUE) // center the drawing | |
109 | { | |
110 | posX = (float) ((w - (200 * actualScale)) / 2.0); | |
111 | posY = (float) ((h - (200 * actualScale)) / 2.0); | |
112 | } | |
113 | else // Use defined presets | |
114 | { | |
115 | posX = 10; | |
116 | posY = 35; | |
117 | } | |
118 | ||
119 | ||
120 | // Set the scale and origin | |
121 | dc->SetUserScale (actualScale, actualScale); | |
122 | dc->SetDeviceOrigin ((long) posX, (long) posY); | |
123 | ||
124 | // This part was added to preform the print preview and printing functions | |
125 | ||
126 | dc->BeginDrawing(); // Allows optimization of drawing code under MS Windows. | |
127 | wxDiagram *diagram_p=((DiagramDocument*)GetDocument())->GetDiagram(); // Get the current diagram | |
128 | if (diagram_p->GetShapeList()) | |
129 | { | |
130 | wxCursor *old_cursor = NULL; | |
131 | wxNode *current = diagram_p->GetShapeList()->First(); | |
132 | ||
133 | while (current) // Loop through the entire list of shapes | |
134 | { | |
135 | wxShape *object = (wxShape *)current->Data(); | |
136 | if (!object->GetParent()) | |
137 | { | |
138 | object->Draw(* dc); // Draw the shape onto our printing dc | |
139 | } | |
140 | current = current->Next(); // Procede to the next shape in the list | |
141 | } | |
142 | } | |
143 | dc->EndDrawing(); // Allows optimization of drawing code under MS Windows. | |
144 | } | |
145 | ||
146 | void DiagramView::OnUpdate(wxView *sender, wxObject *hint) | |
147 | { | |
148 | if (canvas) | |
149 | canvas->Refresh(); | |
150 | } | |
151 | ||
152 | // Clean up windows used for displaying the view. | |
153 | bool DiagramView::OnClose(bool deleteWindow) | |
154 | { | |
155 | if (!GetDocument()->Close()) | |
156 | return FALSE; | |
157 | ||
158 | DiagramDocument *diagramDoc = (DiagramDocument *)GetDocument(); | |
159 | diagramDoc->GetDiagram()->SetCanvas(NULL); | |
160 | ||
161 | canvas->ClearBackground(); | |
162 | canvas->SetDiagram(NULL); | |
163 | canvas->view = NULL; | |
164 | canvas = NULL; | |
165 | ||
166 | wxString s = wxTheApp->GetAppName(); | |
167 | if (frame) | |
168 | frame->SetTitle(s); | |
169 | ||
170 | SetFrame(NULL); | |
171 | ||
172 | Activate(FALSE); | |
173 | ||
174 | return TRUE; | |
175 | } | |
176 | ||
177 | wxShape *DiagramView::FindSelectedShape(void) | |
178 | { | |
179 | DiagramDocument *doc = (DiagramDocument *)GetDocument(); | |
180 | wxShape *theShape = NULL; | |
181 | wxNode *node = doc->GetDiagram()->GetShapeList()->First(); | |
182 | while (node) | |
183 | { | |
184 | wxShape *eachShape = (wxShape *)node->Data(); | |
185 | if ((eachShape->GetParent() == NULL) && eachShape->Selected()) | |
186 | { | |
187 | theShape = eachShape; | |
188 | node = NULL; | |
189 | } | |
190 | else node = node->Next(); | |
191 | } | |
192 | return theShape; | |
193 | } | |
194 | ||
195 | void DiagramView::OnCut(wxCommandEvent& event) | |
196 | { | |
197 | DiagramDocument *doc = (DiagramDocument *)GetDocument(); | |
198 | ||
199 | wxShape *theShape = FindSelectedShape(); | |
200 | if (theShape) | |
201 | doc->GetCommandProcessor()->Submit(new DiagramCommand("Cut", OGLEDIT_CUT, doc, NULL, 0.0, 0.0, TRUE, theShape)); | |
202 | } | |
203 | ||
204 | void DiagramView::OnChangeBackgroundColour(wxCommandEvent& event) | |
205 | { | |
206 | DiagramDocument *doc = (DiagramDocument *)GetDocument(); | |
207 | ||
208 | wxShape *theShape = FindSelectedShape(); | |
209 | if (theShape) | |
210 | { | |
211 | wxColourData data; | |
212 | data.SetChooseFull(TRUE); | |
213 | data.SetColour(theShape->GetBrush()->GetColour()); | |
214 | ||
215 | wxColourDialog *dialog = new wxColourDialog(frame, &data); | |
216 | wxBrush *theBrush = NULL; | |
217 | if (dialog->ShowModal() == wxID_OK) | |
218 | { | |
219 | wxColourData retData = dialog->GetColourData(); | |
220 | wxColour col = retData.GetColour(); | |
221 | theBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID); | |
222 | } | |
223 | dialog->Close(); | |
224 | ||
225 | if (theBrush) | |
226 | doc->GetCommandProcessor()->Submit(new DiagramCommand("Change colour", OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc, | |
227 | theBrush, theShape)); | |
228 | } | |
229 | } | |
230 | ||
231 | void DiagramView::OnEditLabel(wxCommandEvent& event) | |
232 | { | |
233 | wxShape *theShape = FindSelectedShape(); | |
234 | if (theShape) | |
235 | { | |
236 | wxString newLabel = wxGetTextFromUser("Enter new label", "Shape Label", ((MyEvtHandler *)theShape->GetEventHandler())->label); | |
237 | GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand("Edit label", OGLEDIT_EDIT_LABEL, (DiagramDocument*) GetDocument(), newLabel, theShape)); | |
238 | } | |
239 | } | |
240 | ||
241 | ||
242 | /* | |
243 | * Window implementations | |
244 | */ | |
245 | ||
246 | BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas) | |
247 | EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent) | |
248 | EVT_PAINT(MyCanvas::OnPaint) | |
249 | END_EVENT_TABLE() | |
250 | ||
251 | // Define a constructor for my canvas | |
252 | MyCanvas::MyCanvas(wxView *v, wxWindow *parent, wxWindowID id, const wxPoint& pos, | |
253 | const wxSize& size, long style): | |
254 | wxShapeCanvas(parent, id, pos, size, style) | |
255 | { | |
256 | SetBackgroundColour(*wxWHITE); | |
257 | view = v; | |
258 | } | |
259 | ||
260 | MyCanvas::~MyCanvas(void) | |
261 | { | |
262 | } | |
263 | ||
264 | void MyCanvas::OnLeftClick(double x, double y, int keys) | |
265 | { | |
266 | EditorToolPalette *palette = wxGetApp().frame->palette; | |
267 | wxClassInfo *info = NULL; | |
268 | switch (palette->currentlySelected) | |
269 | { | |
270 | case PALETTE_TOOL1: | |
271 | { | |
272 | info = CLASSINFO(wxRectangleShape); | |
273 | break; | |
274 | } | |
275 | case PALETTE_TOOL2: | |
276 | { | |
277 | info = CLASSINFO(wxRoundedRectangleShape); | |
278 | break; | |
279 | } | |
280 | case PALETTE_TOOL3: | |
281 | { | |
282 | info = CLASSINFO(wxEllipseShape); | |
283 | break; | |
284 | } | |
285 | case PALETTE_TOOL4: | |
286 | { | |
287 | info = CLASSINFO(wxDiamondShape); | |
288 | break; | |
289 | } | |
290 | default: | |
291 | break; | |
292 | } | |
293 | if (info) | |
294 | { | |
295 | view->GetDocument()->GetCommandProcessor()->Submit( | |
296 | new DiagramCommand((char*) info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info, | |
297 | x, y)); | |
298 | } | |
299 | } | |
300 | ||
301 | void MyCanvas::OnRightClick(double x, double y, int keys) | |
302 | { | |
303 | } | |
304 | ||
305 | void MyCanvas::OnDragLeft(bool draw, double x, double y, int keys) | |
306 | { | |
307 | } | |
308 | ||
309 | void MyCanvas::OnBeginDragLeft(double x, double y, int keys) | |
310 | { | |
311 | } | |
312 | ||
313 | void MyCanvas::OnEndDragLeft(double x, double y, int keys) | |
314 | { | |
315 | } | |
316 | ||
317 | void MyCanvas::OnDragRight(bool draw, double x, double y, int keys) | |
318 | { | |
319 | } | |
320 | ||
321 | void MyCanvas::OnBeginDragRight(double x, double y, int keys) | |
322 | { | |
323 | } | |
324 | ||
325 | void MyCanvas::OnEndDragRight(double x, double y, int keys) | |
326 | { | |
327 | } | |
328 | ||
329 | void MyCanvas::OnMouseEvent(wxMouseEvent& event) | |
330 | { | |
331 | wxShapeCanvas::OnMouseEvent(event); | |
332 | } | |
333 | ||
334 | void MyCanvas::OnPaint(wxPaintEvent& event) | |
335 | { | |
336 | // if (GetDiagram()) | |
337 | wxShapeCanvas::OnPaint(event); | |
338 | } |