]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/ogl/ogledit/view.cpp
offset version by 1 to avoid having compatibility_version of 0.0.0 under Darwin:...
[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 // 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 dc->BeginDrawing(); // Allows optimization of drawing code under MS Windows.
122 wxDiagram *diagram_p=((DiagramDocument*)GetDocument())->GetDiagram(); // Get the current diagram
123 if (diagram_p->GetShapeList())
124 {
125 /* wxCursor *old_cursor = NULL; */
126 wxObjectList::compatibility_iterator current = diagram_p->GetShapeList()->GetFirst();
127
128 while (current) // Loop through the entire list of shapes
129 {
130 wxShape *object = (wxShape *)current->GetData();
131 if (!object->GetParent())
132 {
133 object->Draw(* dc); // Draw the shape onto our printing dc
134 }
135 current = current->GetNext(); // Procede to the next shape in the list
136 }
137 }
138 dc->EndDrawing(); // Allows optimization of drawing code under MS Windows.
139 }
140
141 void DiagramView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
142 {
143 if (canvas)
144 canvas->Refresh();
145 }
146
147 // Clean up windows used for displaying the view.
148 bool DiagramView::OnClose(bool WXUNUSED(deleteWindow))
149 {
150 if (!GetDocument()->Close())
151 return false;
152
153 DiagramDocument *diagramDoc = (DiagramDocument *)GetDocument();
154 diagramDoc->GetDiagram()->SetCanvas(NULL);
155
156 canvas->ClearBackground();
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
167 Activate(false);
168
169 return true;
170 }
171
172 wxShape *DiagramView::FindSelectedShape(void)
173 {
174 DiagramDocument *doc = (DiagramDocument *)GetDocument();
175 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
176 while (node)
177 {
178 wxShape *eachShape = (wxShape *)node->GetData();
179 if ((eachShape->GetParent() == NULL) && eachShape->Selected())
180 {
181 return eachShape;
182 }
183 else node = node->GetNext();
184 }
185 return NULL;
186 }
187
188 void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
189 {
190 DiagramDocument *doc = (DiagramDocument *)GetDocument();
191
192 wxShape *theShape = FindSelectedShape();
193 if (theShape)
194 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), wxID_CUT, doc, NULL, 0.0, 0.0, true, theShape));
195 }
196
197 void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
198 {
199 DiagramDocument *doc = (DiagramDocument *)GetDocument();
200
201 wxShape *theShape = FindSelectedShape();
202 if (theShape)
203 {
204 wxColourData data;
205 data.SetChooseFull(true);
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)
219 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc,
220 theBrush, theShape));
221 }
222 }
223
224 void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event))
225 {
226 wxShape *theShape = FindSelectedShape();
227 if (theShape)
228 {
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));
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
257 void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
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 {
288 view->GetDocument()->GetCommandProcessor()->Submit(
289 new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info,
290 x, y));
291 }
292 }
293
294 void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
295 {
296 }
297
298 void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
299 {
300 }
301
302 void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
303 {
304 }
305
306 void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
307 {
308 }
309
310 void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
311 {
312 }
313
314 void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
315 {
316 }
317
318 void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
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 }