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