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