]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/ogl/ogledit/view.cpp
remove unfinished sample for release
[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; */
36ca94a2 130 wxObjectList::compatibility_iterator 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();
36ca94a2 179 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
1fc25a89
JS
180 while (node)
181 {
8552e6f0 182 wxShape *eachShape = (wxShape *)node->GetData();
1fc25a89
JS
183 if ((eachShape->GetParent() == NULL) && eachShape->Selected())
184 {
5e0dbc8d 185 return eachShape;
1fc25a89 186 }
8552e6f0 187 else node = node->GetNext();
1fc25a89 188 }
5e0dbc8d 189 return NULL;
1fc25a89
JS
190}
191
1484b5cc 192void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
193{
194 DiagramDocument *doc = (DiagramDocument *)GetDocument();
195
196 wxShape *theShape = FindSelectedShape();
197 if (theShape)
2ba06d5a 198 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), OGLEDIT_CUT, doc, NULL, 0.0, 0.0, true, theShape));
1fc25a89
JS
199}
200
1484b5cc 201void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
202{
203 DiagramDocument *doc = (DiagramDocument *)GetDocument();
204
205 wxShape *theShape = FindSelectedShape();
206 if (theShape)
207 {
208 wxColourData data;
2ba06d5a 209 data.SetChooseFull(true);
1fc25a89
JS
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)
1484b5cc 223 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc,
1fc25a89
JS
224 theBrush, theShape));
225 }
226}
227
1484b5cc 228void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
229{
230 wxShape *theShape = FindSelectedShape();
231 if (theShape)
232 {
1484b5cc
VS
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));
1fc25a89
JS
235 }
236}
237
238
239/*
240 * Window implementations
241 */
242
243BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas)
244 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
245 EVT_PAINT(MyCanvas::OnPaint)
246END_EVENT_TABLE()
247
248// Define a constructor for my canvas
249MyCanvas::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
257MyCanvas::~MyCanvas(void)
258{
259}
260
1484b5cc 261void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
1fc25a89
JS
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 {
963a1fcd 292 view->GetDocument()->GetCommandProcessor()->Submit(
1484b5cc 293 new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info,
963a1fcd 294 x, y));
1fc25a89
JS
295 }
296}
297
1484b5cc 298void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
299{
300}
301
1484b5cc 302void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
303{
304}
305
1484b5cc 306void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
307{
308}
309
1484b5cc 310void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
311{
312}
313
1484b5cc 314void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
315{
316}
317
1484b5cc 318void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
319{
320}
321
1484b5cc 322void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
323{
324}
325
326void MyCanvas::OnMouseEvent(wxMouseEvent& event)
327{
328 wxShapeCanvas::OnMouseEvent(event);
329}
330
331void MyCanvas::OnPaint(wxPaintEvent& event)
332{
333// if (GetDiagram())
334 wxShapeCanvas::OnPaint(event);
335}