]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/ogl/ogledit/view.cpp
Use 'wx/' for contrib samples and make wxTinderbox properly rebuilded after commits...
[wxWidgets.git] / contrib / samples / ogl / ogledit / view.cpp
CommitLineData
1fc25a89 1/////////////////////////////////////////////////////////////////////////////
f4ec6bd2 2// Name: contrib/samples/ogl/ogledit/view.cpp
1fc25a89
JS
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
1fc25a89 12// For compilers that support precompilation, includes "wx.h".
92a19c2e 13#include "wx/wxprec.h"
1fc25a89
JS
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
f4ec6bd2 20#include "wx/wx.h"
1fc25a89
JS
21#endif
22
f4ec6bd2 23#include "wx/colordlg.h"
1fc25a89
JS
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
34IMPLEMENT_DYNAMIC_CLASS(DiagramView, wxView)
35
36BEGIN_EVENT_TABLE(DiagramView, wxView)
cecdcad1 37 EVT_MENU(wxID_CUT, DiagramView::OnCut)
1fc25a89
JS
38 EVT_MENU(OGLEDIT_CHANGE_BACKGROUND_COLOUR, DiagramView::OnChangeBackgroundColour)
39 EVT_MENU(OGLEDIT_EDIT_LABEL, DiagramView::OnEditLabel)
40END_EVENT_TABLE()
41
42// What to do when a view is created. Creates actual
43// windows for displaying the view.
1484b5cc 44bool DiagramView::OnCreate(wxDocument *doc, long WXUNUSED(flags))
1fc25a89
JS
45{
46 frame = GetMainFrame();
47 canvas = GetMainFrame()->canvas;
48 canvas->view = this;
49
50 SetFrame(frame);
2ba06d5a 51 Activate(true);
1fc25a89
JS
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
2ba06d5a 62 return true;
1fc25a89
JS
63}
64
2ba06d5a 65#define CENTER false // Place the drawing to the center of the page
1fc25a89
JS
66
67
68// Sneakily gets used for default print/preview
1e00cf2c
JS
69// as well as drawing on the screen.
70void 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
1484b5cc
VS
104 #if 0
105 // center the drawing
1e00cf2c
JS
106 posX = (float) ((w - (200 * actualScale)) / 2.0);
107 posY = (float) ((h - (200 * actualScale)) / 2.0);
1484b5cc
VS
108 #else
109 // Use defined presets
1e00cf2c
JS
110 posX = 10;
111 posY = 35;
1484b5cc 112 #endif
1e00cf2c
JS
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
1fc25a89 121 wxDiagram *diagram_p=((DiagramDocument*)GetDocument())->GetDiagram(); // Get the current diagram
1e00cf2c
JS
122 if (diagram_p->GetShapeList())
123 {
1484b5cc 124 /* wxCursor *old_cursor = NULL; */
36ca94a2 125 wxObjectList::compatibility_iterator current = diagram_p->GetShapeList()->GetFirst();
1fc25a89 126
1e00cf2c 127 while (current) // Loop through the entire list of shapes
1fc25a89 128 {
8552e6f0 129 wxShape *object = (wxShape *)current->GetData();
1fc25a89
JS
130 if (!object->GetParent())
131 {
132 object->Draw(* dc); // Draw the shape onto our printing dc
133 }
8552e6f0 134 current = current->GetNext(); // Procede to the next shape in the list
1fc25a89
JS
135 }
136 }
1fc25a89
JS
137}
138
1484b5cc 139void DiagramView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
1fc25a89
JS
140{
141 if (canvas)
142 canvas->Refresh();
143}
144
145// Clean up windows used for displaying the view.
1484b5cc 146bool DiagramView::OnClose(bool WXUNUSED(deleteWindow))
1fc25a89
JS
147{
148 if (!GetDocument()->Close())
2ba06d5a 149 return false;
1fc25a89
JS
150
151 DiagramDocument *diagramDoc = (DiagramDocument *)GetDocument();
152 diagramDoc->GetDiagram()->SetCanvas(NULL);
153
1e00cf2c 154 canvas->ClearBackground();
1fc25a89
JS
155 canvas->SetDiagram(NULL);
156 canvas->view = NULL;
157 canvas = NULL;
158
159 wxString s = wxTheApp->GetAppName();
160 if (frame)
161 frame->SetTitle(s);
162
163 SetFrame(NULL);
164
2ba06d5a 165 Activate(false);
1e00cf2c 166
2ba06d5a 167 return true;
1fc25a89
JS
168}
169
170wxShape *DiagramView::FindSelectedShape(void)
171{
172 DiagramDocument *doc = (DiagramDocument *)GetDocument();
36ca94a2 173 wxObjectList::compatibility_iterator node = doc->GetDiagram()->GetShapeList()->GetFirst();
1fc25a89
JS
174 while (node)
175 {
8552e6f0 176 wxShape *eachShape = (wxShape *)node->GetData();
1fc25a89
JS
177 if ((eachShape->GetParent() == NULL) && eachShape->Selected())
178 {
5e0dbc8d 179 return eachShape;
1fc25a89 180 }
8552e6f0 181 else node = node->GetNext();
1fc25a89 182 }
5e0dbc8d 183 return NULL;
1fc25a89
JS
184}
185
1484b5cc 186void DiagramView::OnCut(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
187{
188 DiagramDocument *doc = (DiagramDocument *)GetDocument();
189
190 wxShape *theShape = FindSelectedShape();
191 if (theShape)
cecdcad1 192 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Cut"), wxID_CUT, doc, NULL, 0.0, 0.0, true, theShape));
1fc25a89
JS
193}
194
1484b5cc 195void DiagramView::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
196{
197 DiagramDocument *doc = (DiagramDocument *)GetDocument();
198
199 wxShape *theShape = FindSelectedShape();
200 if (theShape)
201 {
202 wxColourData data;
2ba06d5a 203 data.SetChooseFull(true);
1fc25a89
JS
204 data.SetColour(theShape->GetBrush()->GetColour());
205
206 wxColourDialog *dialog = new wxColourDialog(frame, &data);
207 wxBrush *theBrush = NULL;
208 if (dialog->ShowModal() == wxID_OK)
209 {
210 wxColourData retData = dialog->GetColourData();
211 wxColour col = retData.GetColour();
212 theBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID);
213 }
214 dialog->Close();
215
216 if (theBrush)
1484b5cc 217 doc->GetCommandProcessor()->Submit(new DiagramCommand(_T("Change colour"), OGLEDIT_CHANGE_BACKGROUND_COLOUR, doc,
1fc25a89
JS
218 theBrush, theShape));
219 }
220}
221
1484b5cc 222void DiagramView::OnEditLabel(wxCommandEvent& WXUNUSED(event))
1fc25a89
JS
223{
224 wxShape *theShape = FindSelectedShape();
225 if (theShape)
226 {
1484b5cc
VS
227 wxString newLabel = wxGetTextFromUser(_T("Enter new label"), _T("Shape Label"), ((MyEvtHandler *)theShape->GetEventHandler())->label);
228 GetDocument()->GetCommandProcessor()->Submit(new DiagramCommand(_T("Edit label"), OGLEDIT_EDIT_LABEL, (DiagramDocument*) GetDocument(), newLabel, theShape));
1fc25a89
JS
229 }
230}
231
232
233/*
234 * Window implementations
235 */
236
237BEGIN_EVENT_TABLE(MyCanvas, wxShapeCanvas)
238 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
239 EVT_PAINT(MyCanvas::OnPaint)
240END_EVENT_TABLE()
241
242// Define a constructor for my canvas
243MyCanvas::MyCanvas(wxView *v, wxWindow *parent, wxWindowID id, const wxPoint& pos,
244 const wxSize& size, long style):
245 wxShapeCanvas(parent, id, pos, size, style)
246{
247 SetBackgroundColour(*wxWHITE);
248 view = v;
249}
250
251MyCanvas::~MyCanvas(void)
252{
253}
254
1484b5cc 255void MyCanvas::OnLeftClick(double x, double y, int WXUNUSED(keys))
1fc25a89
JS
256{
257 EditorToolPalette *palette = wxGetApp().frame->palette;
258 wxClassInfo *info = NULL;
259 switch (palette->currentlySelected)
260 {
261 case PALETTE_TOOL1:
262 {
263 info = CLASSINFO(wxRectangleShape);
264 break;
265 }
266 case PALETTE_TOOL2:
267 {
268 info = CLASSINFO(wxRoundedRectangleShape);
269 break;
270 }
271 case PALETTE_TOOL3:
272 {
273 info = CLASSINFO(wxEllipseShape);
274 break;
275 }
276 case PALETTE_TOOL4:
277 {
278 info = CLASSINFO(wxDiamondShape);
279 break;
280 }
281 default:
282 break;
283 }
284 if (info)
285 {
963a1fcd 286 view->GetDocument()->GetCommandProcessor()->Submit(
1484b5cc 287 new DiagramCommand( info->GetClassName(), OGLEDIT_ADD_SHAPE, (DiagramDocument *)view->GetDocument(), info,
963a1fcd 288 x, y));
1fc25a89
JS
289 }
290}
291
1484b5cc 292void MyCanvas::OnRightClick(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
293{
294}
295
1484b5cc 296void MyCanvas::OnDragLeft(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
297{
298}
299
1484b5cc 300void MyCanvas::OnBeginDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
301{
302}
303
1484b5cc 304void MyCanvas::OnEndDragLeft(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
305{
306}
307
1484b5cc 308void MyCanvas::OnDragRight(bool WXUNUSED(draw), double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
309{
310}
311
1484b5cc 312void MyCanvas::OnBeginDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
313{
314}
315
1484b5cc 316void MyCanvas::OnEndDragRight(double WXUNUSED(x), double WXUNUSED(y), int WXUNUSED(keys))
1fc25a89
JS
317{
318}
319
320void MyCanvas::OnMouseEvent(wxMouseEvent& event)
321{
322 wxShapeCanvas::OnMouseEvent(event);
323}
324
325void MyCanvas::OnPaint(wxPaintEvent& event)
326{
327// if (GetDiagram())
328 wxShapeCanvas::OnPaint(event);
329}