]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/ogl/ogledit/view.cpp
added except sample
[wxWidgets.git] / contrib / samples / ogl / ogledit / view.cpp
CommitLineData
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
12#ifdef __GNUG__
13// #pragma implementation
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
92a19c2e 17#include "wx/wxprec.h"
1fc25a89
JS
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
38IMPLEMENT_DYNAMIC_CLASS(DiagramView, wxView)
39
40BEGIN_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)
44END_EVENT_TABLE()
45
46// What to do when a view is created. Creates actual
47// windows for displaying the view.
1484b5cc 48bool DiagramView::OnCreate(wxDocument *doc, long WXUNUSED(flags))
1fc25a89
JS
49{
50 frame = GetMainFrame();
51 canvas = GetMainFrame()->canvas;
52 canvas->view = this;
53
54 SetFrame(frame);
2ba06d5a 55 Activate(true);
1fc25a89
JS
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
2ba06d5a 66 return true;
1fc25a89
JS
67}
68
2ba06d5a 69#define CENTER false // Place the drawing to the center of the page
1fc25a89
JS
70
71
72// Sneakily gets used for default print/preview
1e00cf2c
JS
73// as well as drawing on the screen.
74void 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
1484b5cc
VS
108 #if 0
109 // center the drawing
1e00cf2c
JS
110 posX = (float) ((w - (200 * actualScale)) / 2.0);
111 posY = (float) ((h - (200 * actualScale)) / 2.0);
1484b5cc
VS
112 #else
113 // Use defined presets
1e00cf2c
JS
114 posX = 10;
115 posY = 35;
1484b5cc 116 #endif
1e00cf2c
JS
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.
1fc25a89 126 wxDiagram *diagram_p=((DiagramDocument*)GetDocument())->GetDiagram(); // Get the current diagram
1e00cf2c
JS
127 if (diagram_p->GetShapeList())
128 {
1484b5cc 129 /* wxCursor *old_cursor = NULL; */
8552e6f0 130 wxNode *current = diagram_p->GetShapeList()->GetFirst();
1fc25a89 131
1e00cf2c 132 while (current) // Loop through the entire list of shapes
1fc25a89 133 {
8552e6f0 134 wxShape *object = (wxShape *)current->GetData();
1fc25a89
JS
135 if (!object->GetParent())
136 {
137 object->Draw(* dc); // Draw the shape onto our printing dc
138 }
8552e6f0 139 current = current->GetNext(); // Procede to the next shape in the list
1fc25a89
JS
140 }
141 }
1e00cf2c 142 dc->EndDrawing(); // Allows optimization of drawing code under MS Windows.
1fc25a89
JS
143}
144
1484b5cc 145void DiagramView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
1fc25a89
JS
146{
147 if (canvas)
148 canvas->Refresh();
149}
150
151// Clean up windows used for displaying the view.
1484b5cc 152bool DiagramView::OnClose(bool WXUNUSED(deleteWindow))
1fc25a89
JS
153{
154 if (!GetDocument()->Close())
2ba06d5a 155 return false;
1fc25a89
JS
156
157 DiagramDocument *diagramDoc = (DiagramDocument *)GetDocument();
158 diagramDoc->GetDiagram()->SetCanvas(NULL);
159
1e00cf2c 160 canvas->ClearBackground();
1fc25a89
JS
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
2ba06d5a 171 Activate(false);
1e00cf2c 172
2ba06d5a 173 return true;
1fc25a89
JS
174}
175
176wxShape *DiagramView::FindSelectedShape(void)
177{
178 DiagramDocument *doc = (DiagramDocument *)GetDocument();
179 wxShape *theShape = NULL;
8552e6f0 180 wxNode *node = doc->GetDiagram()->GetShapeList()->GetFirst();
1fc25a89
JS
181 while (node)
182 {
8552e6f0 183 wxShape *eachShape = (wxShape *)node->GetData();
1fc25a89
JS
184 if ((eachShape->GetParent() == NULL) && eachShape->Selected())
185 {
186 theShape = eachShape;
187 node = NULL;
188 }
8552e6f0 189 else node = node->GetNext();
1fc25a89
JS
190 }
191 return theShape;
192}
193
1484b5cc 194void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
195{
196 DiagramDocument *doc = (DiagramDocument *)GetDocument();
197
198 wxShape *theShape = FindSelectedShape();
199 if (theShape)
2ba06d5a 200 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), OGLEDIT_CUT, doc, NULL, 0.0, 0.0, true, theShape));
1fc25a89
JS
201}
202
1484b5cc 203void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
204{
205 DiagramDocument *doc = (DiagramDocument *)GetDocument();
206
207 wxShape *theShape = FindSelectedShape();
208 if (theShape)
209 {
210 wxColourData data;
2ba06d5a 211 data.SetChooseFull(true);
1fc25a89
JS
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)
1484b5cc 225 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc,
1fc25a89
JS
226 theBrush, theShape));
227 }
228}
229
1484b5cc 230void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
231{
232 wxShape *theShape = FindSelectedShape();
233 if (theShape)
234 {
1484b5cc
VS
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));
1fc25a89
JS
237 }
238}
239
240
241/*
242 * Window implementations
243 */
244
245BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas)
246 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
247 EVT_PAINT(MyCanvas::OnPaint)
248END_EVENT_TABLE()
249
250// Define a constructor for my canvas
251MyCanvas::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
259MyCanvas::~MyCanvas(void)
260{
261}
262
1484b5cc 263void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
1fc25a89
JS
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 {
963a1fcd 294 view->GetDocument()->GetCommandProcessor()->Submit(
1484b5cc 295 new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info,
963a1fcd 296 x, y));
1fc25a89
JS
297 }
298}
299
1484b5cc 300void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
301{
302}
303
1484b5cc 304void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
305{
306}
307
1484b5cc 308void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
309{
310}
311
1484b5cc 312void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
313{
314}
315
1484b5cc 316void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
317{
318}
319
1484b5cc 320void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
321{
322}
323
1484b5cc 324void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
325{
326}
327
328void MyCanvas::OnMouseEvent(wxMouseEvent& event)
329{
330 wxShapeCanvas::OnMouseEvent(event);
331}
332
333void MyCanvas::OnPaint(wxPaintEvent& event)
334{
335// if (GetDiagram())
336 wxShapeCanvas::OnPaint(event);
337}