]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: contrib/samples/ogl/ogledit/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 | // For compilers that support precompilation, includes "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 | #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) | |
37 | EVT_MENU(wxID_CUT, DiagramView::OnCut) | |
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. | |
44 | bool DiagramView::OnCreate(wxDocument *doc, long WXUNUSED(flags)) | |
45 | { | |
46 | frame = GetMainFrame(); | |
47 | canvas = GetMainFrame()->canvas; | |
48 | canvas->view = this; | |
49 | ||
50 | SetFrame(frame); | |
51 | Activate(true); | |
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 | ||
62 | return true; | |
63 | } | |
64 | ||
65 | #define CENTER false // Place the drawing to the center of the page | |
66 | ||
67 | ||
68 | // Sneakily gets used for default print/preview | |
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 | |
104 | #if 0 | |
105 | // center the drawing | |
106 | posX = (float) ((w - (200 * actualScale)) / 2.0); | |
107 | posY = (float) ((h - (200 * actualScale)) / 2.0); | |
108 | #else | |
109 | // Use defined presets | |
110 | posX = 10; | |
111 | posY = 35; | |
112 | #endif | |
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 | wxDiagram *diagram_p=((DiagramDocument*)GetDocument())->GetDiagram(); // Get the current diagram | |
122 | if (diagram_p->GetShapeList()) | |
123 | { | |
124 | /* wxCursor *old_cursor = NULL; */ | |
125 | wxObjectList::compatibility_iterator current = diagram_p->GetShapeList()->GetFirst(); | |
126 | ||
127 | while (current) // Loop through the entire list of shapes | |
128 | { | |
129 | wxShape *object = (wxShape *)current->GetData(); | |
130 | if (!object->GetParent()) | |
131 | { | |
132 | object->Draw(* dc); // Draw the shape onto our printing dc | |
133 | } | |
134 | current = current->GetNext(); // Procede to the next shape in the list | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | void DiagramView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint)) | |
140 | { | |
141 | if (canvas) | |
142 | canvas->Refresh(); | |
143 | } | |
144 | ||
145 | // Clean up windows used for displaying the view. | |
146 | bool DiagramView::OnClose(bool WXUNUSED(deleteWindow)) | |
147 | { | |
148 | if (!GetDocument()->Close()) | |
149 | return false; | |
150 | ||
151 | DiagramDocument *diagramDoc = (DiagramDocument *)GetDocument(); | |
152 | diagramDoc->GetDiagram()->SetCanvas(NULL); | |
153 | ||
154 | canvas->ClearBackground(); | |
155 | canvas->SetDiagram(NULL); | |
156 | canvas->view = NULL; | |
157 | canvas = NULL; | |
158 | ||
159 | wxString s = wxTheApp->GetAppName(); | |
160 | if (frame) | |
161 | frame->SetTitle(s); | |
162 | ||
163 | SetFrame(NULL); | |
164 | ||
165 | Activate(false); | |
166 | ||
167 | return true; | |
168 | } | |
169 | ||
170 | wxShape *DiagramView::FindSelectedShape(void) | |
171 | { | |
172 | DiagramDocument *doc = (DiagramDocument *)GetDocument(); | |
173 | wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst(); | |
174 | while (node) | |
175 | { | |
176 | wxShape *eachShape = (wxShape *)node->GetData(); | |
177 | if ((eachShape->GetParent() == NULL) && eachShape->Selected()) | |
178 | { | |
179 | return eachShape; | |
180 | } | |
181 | else node = node->GetNext(); | |
182 | } | |
183 | return NULL; | |
184 | } | |
185 | ||
186 | void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event)) | |
187 | { | |
188 | DiagramDocument *doc = (DiagramDocument *)GetDocument(); | |
189 | ||
190 | wxShape *theShape = FindSelectedShape(); | |
191 | if (theShape) | |
192 | doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), wxID_CUT, doc, NULL, 0.0, 0.0, true, theShape)); | |
193 | } | |
194 | ||
195 | void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event)) | |
196 | { | |
197 | DiagramDocument *doc = (DiagramDocument *)GetDocument(); | |
198 | ||
199 | wxShape *theShape = FindSelectedShape(); | |
200 | if (theShape) | |
201 | { | |
202 | wxColourData data; | |
203 | data.SetChooseFull(true); | |
204 | data.SetColour(theShape->GetBrush()->GetColour()); | |
205 | ||
206 | wxColourDialog *dialog = new wxColourDialog(frame, &data); | |
207 | wxBrush *theBrush = NULL; | |
208 | if (dialog->ShowModal() == wxID_OK) | |
209 | { | |
210 | wxColourData retData = dialog->GetColourData(); | |
211 | wxColour col = retData.GetColour(); | |
212 | theBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID); | |
213 | } | |
214 | dialog->Close(); | |
215 | ||
216 | if (theBrush) | |
217 | doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc, | |
218 | theBrush, theShape)); | |
219 | } | |
220 | } | |
221 | ||
222 | void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event)) | |
223 | { | |
224 | wxShape *theShape = FindSelectedShape(); | |
225 | if (theShape) | |
226 | { | |
227 | wxString newLabel = wxGetTextFromUser(_T("Enter new label"), _T("Shape Label"), ((MyEvtHandler *)theShape->GetEventHandler())->label); | |
228 | GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(_T("Edit label"), OGLEDIT_EDIT_LABEL, (DiagramDocument*) GetDocument(), newLabel, theShape)); | |
229 | } | |
230 | } | |
231 | ||
232 | ||
233 | /* | |
234 | * Window implementations | |
235 | */ | |
236 | ||
237 | BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas) | |
238 | EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent) | |
239 | EVT_PAINT(MyCanvas::OnPaint) | |
240 | END_EVENT_TABLE() | |
241 | ||
242 | // Define a constructor for my canvas | |
243 | MyCanvas::MyCanvas(wxView *v, wxWindow *parent, wxWindowID id, const wxPoint& pos, | |
244 | const wxSize& size, long style): | |
245 | wxShapeCanvas(parent, id, pos, size, style) | |
246 | { | |
247 | SetBackgroundColour(*wxWHITE); | |
248 | view = v; | |
249 | } | |
250 | ||
251 | MyCanvas::~MyCanvas(void) | |
252 | { | |
253 | } | |
254 | ||
255 | void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys)) | |
256 | { | |
257 | EditorToolPalette *palette = wxGetApp().frame->palette; | |
258 | wxClassInfo *info = NULL; | |
259 | switch (palette->currentlySelected) | |
260 | { | |
261 | case PALETTE_TOOL1: | |
262 | { | |
263 | info = CLASSINFO(wxRectangleShape); | |
264 | break; | |
265 | } | |
266 | case PALETTE_TOOL2: | |
267 | { | |
268 | info = CLASSINFO(wxRoundedRectangleShape); | |
269 | break; | |
270 | } | |
271 | case PALETTE_TOOL3: | |
272 | { | |
273 | info = CLASSINFO(wxEllipseShape); | |
274 | break; | |
275 | } | |
276 | case PALETTE_TOOL4: | |
277 | { | |
278 | info = CLASSINFO(wxDiamondShape); | |
279 | break; | |
280 | } | |
281 | default: | |
282 | break; | |
283 | } | |
284 | if (info) | |
285 | { | |
286 | view->GetDocument()->GetCommandProcessor()->Submit( | |
287 | new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info, | |
288 | x, y)); | |
289 | } | |
290 | } | |
291 | ||
292 | void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
293 | { | |
294 | } | |
295 | ||
296 | void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
297 | { | |
298 | } | |
299 | ||
300 | void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
301 | { | |
302 | } | |
303 | ||
304 | void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
305 | { | |
306 | } | |
307 | ||
308 | void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
309 | { | |
310 | } | |
311 | ||
312 | void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
313 | { | |
314 | } | |
315 | ||
316 | void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys)) | |
317 | { | |
318 | } | |
319 | ||
320 | void MyCanvas::OnMouseEvent(wxMouseEvent& event) | |
321 | { | |
322 | wxShapeCanvas::OnMouseEvent(event); | |
323 | } | |
324 | ||
325 | void MyCanvas::OnPaint(wxPaintEvent& event) | |
326 | { | |
327 | // if (GetDiagram()) | |
328 | wxShapeCanvas::OnPaint(event); | |
329 | } |