]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/ogl/ogledit/view.cpp
wxUSE_STL fixes.
[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 wxShape *theShape = NULL;
180 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
181 while (node)
182 {
183 wxShape *eachShape = (wxShape *)node->GetData();
184 if ((eachShape->GetParent() == NULL) && eachShape->Selected())
185 {
186 theShape = eachShape;
187 node = NULL;
188 }
189 else node = node->GetNext();
190 }
191 return theShape;
192 }
193
194 void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
195 {
196 DiagramDocument *doc = (DiagramDocument *)GetDocument();
197
198 wxShape *theShape = FindSelectedShape();
199 if (theShape)
200 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), OGLEDIT_CUT, doc, NULL, 0.0, 0.0, true, theShape));
201 }
202
203 void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
204 {
205 DiagramDocument *doc = (DiagramDocument *)GetDocument();
206
207 wxShape *theShape = FindSelectedShape();
208 if (theShape)
209 {
210 wxColourData data;
211 data.SetChooseFull(true);
212 data.SetColour(theShape->GetBrush()->GetColour());
213
214 wxColourDialog *dialog = new wxColourDialog(frame, &data);
215 wxBrush *theBrush = NULL;
216 if (dialog->ShowModal() == wxID_OK)
217 {
218 wxColourData retData = dialog->GetColourData();
219 wxColour col = retData.GetColour();
220 theBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID);
221 }
222 dialog->Close();
223
224 if (theBrush)
225 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc,
226 theBrush, theShape));
227 }
228 }
229
230 void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event))
231 {
232 wxShape *theShape = FindSelectedShape();
233 if (theShape)
234 {
235 wxString newLabel = wxGetTextFromUser(_T("Enter new label"), _T("Shape Label"), ((MyEvtHandler *)theShape->GetEventHandler())->label);
236 GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(_T("Edit label"), OGLEDIT_EDIT_LABEL, (DiagramDocument*) GetDocument(), newLabel, theShape));
237 }
238 }
239
240
241 /*
242 * Window implementations
243 */
244
245 BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas)
246 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
247 EVT_PAINT(MyCanvas::OnPaint)
248 END_EVENT_TABLE()
249
250 // Define a constructor for my canvas
251 MyCanvas::MyCanvas(wxView *v, wxWindow *parent, wxWindowID id, const wxPoint& pos,
252 const wxSize& size, long style):
253 wxShapeCanvas(parent, id, pos, size, style)
254 {
255 SetBackgroundColour(*wxWHITE);
256 view = v;
257 }
258
259 MyCanvas::~MyCanvas(void)
260 {
261 }
262
263 void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
264 {
265 EditorToolPalette *palette = wxGetApp().frame->palette;
266 wxClassInfo *info = NULL;
267 switch (palette->currentlySelected)
268 {
269 case PALETTE_TOOL1:
270 {
271 info = CLASSINFO(wxRectangleShape);
272 break;
273 }
274 case PALETTE_TOOL2:
275 {
276 info = CLASSINFO(wxRoundedRectangleShape);
277 break;
278 }
279 case PALETTE_TOOL3:
280 {
281 info = CLASSINFO(wxEllipseShape);
282 break;
283 }
284 case PALETTE_TOOL4:
285 {
286 info = CLASSINFO(wxDiamondShape);
287 break;
288 }
289 default:
290 break;
291 }
292 if (info)
293 {
294 view->GetDocument()->GetCommandProcessor()->Submit(
295 new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info,
296 x, y));
297 }
298 }
299
300 void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
301 {
302 }
303
304 void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
305 {
306 }
307
308 void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
309 {
310 }
311
312 void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
313 {
314 }
315
316 void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
317 {
318 }
319
320 void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
321 {
322 }
323
324 void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
325 {
326 }
327
328 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
329 {
330 wxShapeCanvas::OnMouseEvent(event);
331 }
332
333 void MyCanvas::OnPaint(wxPaintEvent& event)
334 {
335 // if (GetDiagram())
336 wxShapeCanvas::OnPaint(event);
337 }