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