]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/ogl/studio/csprint.cpp
Added SWIG patches to allow compiling the generated code with
[wxWidgets.git] / contrib / samples / ogl / studio / csprint.cpp
CommitLineData
1fc25a89 1/////////////////////////////////////////////////////////////////////////////
f4ec6bd2 2// Name: contrib/samples/ogl/studio/csprint.cpp
1fc25a89
JS
3// Purpose: Printing and clipboard functionality
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/ogl/ogl.h" // base header of OGL, includes and adjusts wx/deprecated/setup.h
7c9955d1 24
f4ec6bd2 25#include "wx/clipbrd.h"
1fc25a89
JS
26
27#ifdef __WXMSW__
f4ec6bd2 28#include "wx/metafile.h"
1fc25a89
JS
29#endif
30
31#include "studio.h"
32#include "doc.h"
33#include "shapes.h"
34#include "view.h"
35
36IMPLEMENT_DYNAMIC_CLASS(wxDiagramClipboard, wxDiagram)
37
38// Copy selection
39bool wxDiagramClipboard::Copy(wxDiagram* diagram)
40{
41 DeleteAllShapes();
42
2ba06d5a 43 return DoCopy(diagram, this, false, NULL);
1fc25a89
JS
44}
45
46// Copy contents to the diagram, with new ids.
47
48bool wxDiagramClipboard::Paste(wxDiagram* diagram, wxDC* dc, int offsetX, int offsetY)
49{
2ba06d5a 50 return DoCopy(this, diagram, true, dc, offsetX, offsetY);
1fc25a89
JS
51}
52
53// Universal copy function (to or from clipboard).
54// TODO:
55// Note that this only works for non-composites so far (nested shapes
56// don't have their old-to-new object mappings stored).
57// Also, lines don't yet get their attachment points moved to the new offset position
58// if they have more than 2 points.
59bool wxDiagramClipboard::DoCopy(wxDiagram* diagramFrom, wxDiagram* diagramTo, bool newIds,
60 wxDC* dc, int offsetX, int offsetY)
61{
62 OnStartCopy(diagramTo);
63
64 wxHashTable mapping(wxKEY_INTEGER);
65
66 // First copy all node shapes.
67 wxList* shapeList = diagramFrom->GetShapeList();
5e0dbc8d 68 wxObjectList::compatibility_iterator node = shapeList->GetFirst();
1fc25a89
JS
69 while (node)
70 {
8552e6f0 71 wxShape* shape = (wxShape*) node->GetData();
1fc25a89
JS
72 if (((diagramFrom == this) || shape->Selected()) && !shape->IsKindOf(CLASSINFO(wxLineShape)))
73 {
74 wxShape* newShape = shape->CreateNewCopy();
75 newShape->GetLines().Clear();
76 if (newIds)
77 {
78 newShape->AssignNewIds();
79 }
80 mapping.Put((long) shape, (wxObject*) newShape);
81
82 newShape->SetX(newShape->GetX() + offsetX);
83 newShape->SetY(newShape->GetY() + offsetY);
84
85 OnAddShape(diagramTo, newShape, dc);
86
87 }
8552e6f0 88 node = node->GetNext();
1fc25a89
JS
89 }
90
8552e6f0 91 node = shapeList->GetFirst();
1fc25a89
JS
92 while (node)
93 {
8552e6f0 94 wxShape* shape = (wxShape*) node->GetData();
1fc25a89
JS
95 if (((diagramFrom == this) || shape->Selected()) && shape->IsKindOf(CLASSINFO(wxLineShape)))
96 {
97 wxLineShape* lineShape = (wxLineShape*) shape;
98 // Only copy a line if its ends are selected too.
99 if ((diagramFrom == this) || (lineShape->GetTo()->Selected() && lineShape->GetFrom()->Selected()))
100 {
101 wxLineShape* newShape = (wxLineShape*) shape->CreateNewCopy();
102 mapping.Put((long) shape, (wxObject*) newShape);
103
104 if (newIds)
105 newShape->AssignNewIds();
106
107 wxShape* fromShape = (wxShape*) mapping.Get((long) lineShape->GetFrom());
108 wxShape* toShape = (wxShape*) mapping.Get((long) lineShape->GetTo());
109
1484b5cc
VS
110 wxASSERT_MSG( (fromShape != NULL), _T("Could not find 'from' shape"));
111 wxASSERT_MSG( (toShape != NULL), _T("Could not find 'to' shape"));
1fc25a89
JS
112
113 fromShape->AddLine(newShape, toShape, newShape->GetAttachmentFrom(),
114 newShape->GetAttachmentTo());
115
116 OnAddShape(diagramTo, newShape, dc);
117
118 }
119 }
8552e6f0 120 node = node->GetNext();
1fc25a89
JS
121 }
122
123 // Now make sure line ordering is correct
8552e6f0 124 node = shapeList->GetFirst();
1fc25a89
JS
125 while (node)
126 {
8552e6f0 127 wxShape* shape = (wxShape*) node->GetData();
1fc25a89
JS
128 if (((diagramFrom == this) || shape->Selected()) && !shape->IsKindOf(CLASSINFO(wxLineShape)))
129 {
130 wxShape* newShape = (wxShape*) mapping.Get((long) shape);
131
132 // Make a list of all the new lines, in the same order as the old lines.
133 // Then apply the list of new lines to the shape.
134 wxList newLines;
5e0dbc8d 135 wxObjectList::compatibility_iterator lineNode = shape->GetLines().GetFirst();
1fc25a89
JS
136 while (lineNode)
137 {
8552e6f0 138 wxLineShape* lineShape = (wxLineShape*) lineNode->GetData();
1fc25a89
JS
139 if ((diagramFrom == this) || (lineShape->GetTo()->Selected() && lineShape->GetFrom()->Selected()))
140 {
141 wxLineShape* newLineShape = (wxLineShape*) mapping.Get((long) lineShape);
142
1484b5cc 143 wxASSERT_MSG( (newLineShape != NULL), _T("Could not find new line shape"));
1fc25a89
JS
144
145 newLines.Append(newLineShape);
146 }
147
8552e6f0 148 lineNode = lineNode->GetNext();
1fc25a89
JS
149 }
150
8552e6f0 151 if (newLines.GetCount() > 0)
1fc25a89
JS
152 newShape->ApplyAttachmentOrdering(newLines);
153 }
8552e6f0 154 node = node->GetNext();
1fc25a89
JS
155 }
156
157 OnEndCopy(diagramTo);
158
2ba06d5a 159 return true;
1fc25a89
JS
160}
161
162#ifdef __WXMSW__
163// Draw contents to a Windows metafile device context and a bitmap, and copy
164// these to the Windows clipboard
165bool wxDiagramClipboard::CopyToClipboard(double scale)
166{
93210c68 167#if wxUSE_METAFILE
1fc25a89
JS
168 // Make a metafile DC
169 wxMetaFileDC mfDC;
170 if (mfDC.Ok())
171 {
172 mfDC.SetUserScale(scale, scale);
173
174 // Draw on metafile DC
175 Redraw(mfDC);
176
b1c1e25b
JS
177 // int printWidth = mfDC.MaxX() - mfDC.MinX();
178 // int printHeight = mfDC.MaxY() - mfDC.MinY();
1fc25a89
JS
179 int maxX = (int)mfDC.MaxX();
180 int maxY = (int)mfDC.MaxY();
181 wxMetaFile *mf = mfDC.Close();
182
183 // Set to a bitmap memory DC
184 wxBitmap *newBitmap = new wxBitmap((int)(maxX + 10), (int)(maxY + 10));
185 if (!newBitmap->Ok())
186 {
187 delete newBitmap;
cecdcad1 188
42c37dec
VS
189 wxChar buf[200];
190 wxSprintf(buf, _T("Sorry, could not allocate clipboard bitmap (%dx%d)"), (maxX+10), (maxY+10));
1484b5cc 191 wxMessageBox(buf, _T("Clipboard copy problem"));
2ba06d5a 192 return false;
1fc25a89
JS
193 }
194
195 wxMemoryDC memDC;
196 memDC.SelectObject(*newBitmap);
197 memDC.Clear();
198
199 // Now draw on memory bitmap DC
200 Redraw(memDC);
201
202 memDC.SelectObject(wxNullBitmap);
203
204 // Open clipboard and set the data
205 if (wxOpenClipboard())
206 {
207 wxEmptyClipboard();
208
209 // Copy the bitmap to the clipboard
210 wxSetClipboardData(wxDF_BITMAP, newBitmap, 0, 0);
211
212#if 0 // TODO: replace this code (wxEnhMetaFile doesn't have SetClipboard)
213 if (mf)
214 {
215 // Copy the metafile to the clipboard
216 // Allow a small margin
217 bool success = mf->SetClipboard((int)(mfDC.MaxX() + 15), (int)(mfDC.MaxY() + 15));
218 }
219#endif
220
221 // Close clipboard
222 wxCloseClipboard();
223 }
cecdcad1 224
1fc25a89
JS
225 delete newBitmap;
226 delete mf;
227
228 }
93210c68
JS
229 return true;
230#else
231 wxMessageBox("wxUSE_METAFILE in build required to use Clipboard", _T("Clipboard copy problem"));
232 return false;
233#endif
1fc25a89
JS
234}
235#endif
236 // __WXMSW__
237
238// Override this to e.g. have the shape added through a Do/Undo command system.
239// By default, we'll just add it directly to the destination diagram.
240bool wxDiagramClipboard::OnAddShape(wxDiagram* diagramTo, wxShape* newShape, wxDC* dc)
241{
242 diagramTo->AddShape(newShape);
243
244 if (dc && (diagramTo != this))
245 {
2ba06d5a 246 newShape->Select(true, dc);
1fc25a89
JS
247 }
248
2ba06d5a 249 return true;
1fc25a89
JS
250}
251
252/*
253 * csDiagramClipboard
254 */
255
256IMPLEMENT_DYNAMIC_CLASS(csDiagramClipboard, wxDiagramClipboard)
257
258// Start/end copying
259bool csDiagramClipboard::OnStartCopy(wxDiagram* diagramTo)
260{
261 // Do nothing if copying to the clipboard
262 if (diagramTo == this)
2ba06d5a 263 return true;
1fc25a89
JS
264
265 // Deselect all objects initially.
266
267 csDiagram* diagram = (csDiagram*) diagramTo;
268 csDiagramDocument* doc = diagram->GetDocument();
2ba06d5a 269 ((csDiagramView*)doc->GetFirstView())->SelectAll(false);
1fc25a89 270
1484b5cc 271 m_currentCmd = new csDiagramCommand(_T("Paste"), doc);
1fc25a89 272
2ba06d5a 273 return true;
1fc25a89
JS
274}
275
276bool csDiagramClipboard::OnEndCopy(wxDiagram* diagramTo)
277{
278 // Do nothing if copying to the clipboard
279 if (diagramTo == this)
2ba06d5a 280 return true;
1fc25a89
JS
281
282 csDiagram* diagram = (csDiagram*) diagramTo;
283 csDiagramDocument* doc = diagram->GetDocument();
284
285 if (m_currentCmd)
286 {
8552e6f0 287 if (m_currentCmd->GetStates().GetCount() == 0)
1fc25a89
JS
288 {
289 delete m_currentCmd;
290 }
291 else
292 {
293 doc->GetCommandProcessor()->Submit(m_currentCmd);
294 m_currentCmd = NULL;
295 }
296 }
2ba06d5a 297 return true;
1fc25a89
JS
298}
299
300// Use the command framework to add the shapes, if we're copying to a diagram and
301// not the clipboard.
1484b5cc 302bool csDiagramClipboard::OnAddShape(wxDiagram* diagramTo, wxShape* newShape, wxDC* WXUNUSED(dc))
1fc25a89
JS
303{
304 if (diagramTo == this)
305 {
306 diagramTo->AddShape(newShape);
307 }
308 else
309 {
310 csDiagram* diagram = (csDiagram*) diagramTo;
1484b5cc 311 /* csDiagramDocument* doc = */ diagram->GetDocument();
1fc25a89
JS
312
313 if (newShape->IsKindOf(CLASSINFO(wxLineShape)))
314 m_currentCmd->AddState(new csCommandState(ID_CS_ADD_LINE_SELECT, newShape, NULL));
315 else
316 m_currentCmd->AddState(new csCommandState(ID_CS_ADD_SHAPE_SELECT, newShape, NULL));
317 }
318
2ba06d5a 319 return true;
1fc25a89 320}