1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing and clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #include <wx/deprecated/setup.h>
28 #include <wx/deprecated/wxexpr.h>
30 #include <wx/clipbrd.h>
33 #include <wx/metafile.h>
41 IMPLEMENT_DYNAMIC_CLASS(wxDiagramClipboard
, wxDiagram
)
44 bool wxDiagramClipboard::Copy(wxDiagram
* diagram
)
48 return DoCopy(diagram
, this, false, NULL
);
51 // Copy contents to the diagram, with new ids.
53 bool wxDiagramClipboard::Paste(wxDiagram
* diagram
, wxDC
* dc
, int offsetX
, int offsetY
)
55 return DoCopy(this, diagram
, true, dc
, offsetX
, offsetY
);
58 // Universal copy function (to or from clipboard).
60 // Note that this only works for non-composites so far (nested shapes
61 // don't have their old-to-new object mappings stored).
62 // Also, lines don't yet get their attachment points moved to the new offset position
63 // if they have more than 2 points.
64 bool wxDiagramClipboard::DoCopy(wxDiagram
* diagramFrom
, wxDiagram
* diagramTo
, bool newIds
,
65 wxDC
* dc
, int offsetX
, int offsetY
)
67 OnStartCopy(diagramTo
);
69 wxHashTable
mapping(wxKEY_INTEGER
);
71 // First copy all node shapes.
72 wxList
* shapeList
= diagramFrom
->GetShapeList();
73 wxObjectList::compatibility_iterator node
= shapeList
->GetFirst();
76 wxShape
* shape
= (wxShape
*) node
->GetData();
77 if (((diagramFrom
== this) || shape
->Selected()) && !shape
->IsKindOf(CLASSINFO(wxLineShape
)))
79 wxShape
* newShape
= shape
->CreateNewCopy();
80 newShape
->GetLines().Clear();
83 newShape
->AssignNewIds();
85 mapping
.Put((long) shape
, (wxObject
*) newShape
);
87 newShape
->SetX(newShape
->GetX() + offsetX
);
88 newShape
->SetY(newShape
->GetY() + offsetY
);
90 OnAddShape(diagramTo
, newShape
, dc
);
93 node
= node
->GetNext();
96 node
= shapeList
->GetFirst();
99 wxShape
* shape
= (wxShape
*) node
->GetData();
100 if (((diagramFrom
== this) || shape
->Selected()) && shape
->IsKindOf(CLASSINFO(wxLineShape
)))
102 wxLineShape
* lineShape
= (wxLineShape
*) shape
;
103 // Only copy a line if its ends are selected too.
104 if ((diagramFrom
== this) || (lineShape
->GetTo()->Selected() && lineShape
->GetFrom()->Selected()))
106 wxLineShape
* newShape
= (wxLineShape
*) shape
->CreateNewCopy();
107 mapping
.Put((long) shape
, (wxObject
*) newShape
);
110 newShape
->AssignNewIds();
112 wxShape
* fromShape
= (wxShape
*) mapping
.Get((long) lineShape
->GetFrom());
113 wxShape
* toShape
= (wxShape
*) mapping
.Get((long) lineShape
->GetTo());
115 wxASSERT_MSG( (fromShape
!= NULL
), _T("Could not find 'from' shape"));
116 wxASSERT_MSG( (toShape
!= NULL
), _T("Could not find 'to' shape"));
118 fromShape
->AddLine(newShape
, toShape
, newShape
->GetAttachmentFrom(),
119 newShape
->GetAttachmentTo());
121 OnAddShape(diagramTo
, newShape
, dc
);
125 node
= node
->GetNext();
128 // Now make sure line ordering is correct
129 node
= shapeList
->GetFirst();
132 wxShape
* shape
= (wxShape
*) node
->GetData();
133 if (((diagramFrom
== this) || shape
->Selected()) && !shape
->IsKindOf(CLASSINFO(wxLineShape
)))
135 wxShape
* newShape
= (wxShape
*) mapping
.Get((long) shape
);
137 // Make a list of all the new lines, in the same order as the old lines.
138 // Then apply the list of new lines to the shape.
140 wxObjectList::compatibility_iterator lineNode
= shape
->GetLines().GetFirst();
143 wxLineShape
* lineShape
= (wxLineShape
*) lineNode
->GetData();
144 if ((diagramFrom
== this) || (lineShape
->GetTo()->Selected() && lineShape
->GetFrom()->Selected()))
146 wxLineShape
* newLineShape
= (wxLineShape
*) mapping
.Get((long) lineShape
);
148 wxASSERT_MSG( (newLineShape
!= NULL
), _T("Could not find new line shape"));
150 newLines
.Append(newLineShape
);
153 lineNode
= lineNode
->GetNext();
156 if (newLines
.GetCount() > 0)
157 newShape
->ApplyAttachmentOrdering(newLines
);
159 node
= node
->GetNext();
162 OnEndCopy(diagramTo
);
168 // Draw contents to a Windows metafile device context and a bitmap, and copy
169 // these to the Windows clipboard
170 bool wxDiagramClipboard::CopyToClipboard(double scale
)
173 // Make a metafile DC
177 mfDC
.SetUserScale(scale
, scale
);
179 // Draw on metafile DC
182 // int printWidth = mfDC.MaxX() - mfDC.MinX();
183 // int printHeight = mfDC.MaxY() - mfDC.MinY();
184 int maxX
= (int)mfDC
.MaxX();
185 int maxY
= (int)mfDC
.MaxY();
186 wxMetaFile
*mf
= mfDC
.Close();
188 // Set to a bitmap memory DC
189 wxBitmap
*newBitmap
= new wxBitmap((int)(maxX
+ 10), (int)(maxY
+ 10));
190 if (!newBitmap
->Ok())
195 wxSprintf(buf
, _T("Sorry, could not allocate clipboard bitmap (%dx%d)"), (maxX
+10), (maxY
+10));
196 wxMessageBox(buf
, _T("Clipboard copy problem"));
201 memDC
.SelectObject(*newBitmap
);
204 // Now draw on memory bitmap DC
207 memDC
.SelectObject(wxNullBitmap
);
209 // Open clipboard and set the data
210 if (wxOpenClipboard())
214 // Copy the bitmap to the clipboard
215 wxSetClipboardData(wxDF_BITMAP
, newBitmap
, 0, 0);
217 #if 0 // TODO: replace this code (wxEnhMetaFile doesn't have SetClipboard)
220 // Copy the metafile to the clipboard
221 // Allow a small margin
222 bool success
= mf
->SetClipboard((int)(mfDC
.MaxX() + 15), (int)(mfDC
.MaxY() + 15));
236 wxMessageBox("wxUSE_METAFILE in build required to use Clipboard", _T("Clipboard copy problem"));
243 // Override this to e.g. have the shape added through a Do/Undo command system.
244 // By default, we'll just add it directly to the destination diagram.
245 bool wxDiagramClipboard::OnAddShape(wxDiagram
* diagramTo
, wxShape
* newShape
, wxDC
* dc
)
247 diagramTo
->AddShape(newShape
);
249 if (dc
&& (diagramTo
!= this))
251 newShape
->Select(true, dc
);
261 IMPLEMENT_DYNAMIC_CLASS(csDiagramClipboard
, wxDiagramClipboard
)
264 bool csDiagramClipboard::OnStartCopy(wxDiagram
* diagramTo
)
266 // Do nothing if copying to the clipboard
267 if (diagramTo
== this)
270 // Deselect all objects initially.
272 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
273 csDiagramDocument
* doc
= diagram
->GetDocument();
274 ((csDiagramView
*)doc
->GetFirstView())->SelectAll(false);
276 m_currentCmd
= new csDiagramCommand(_T("Paste"), doc
);
281 bool csDiagramClipboard::OnEndCopy(wxDiagram
* diagramTo
)
283 // Do nothing if copying to the clipboard
284 if (diagramTo
== this)
287 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
288 csDiagramDocument
* doc
= diagram
->GetDocument();
292 if (m_currentCmd
->GetStates().GetCount() == 0)
298 doc
->GetCommandProcessor()->Submit(m_currentCmd
);
305 // Use the command framework to add the shapes, if we're copying to a diagram and
306 // not the clipboard.
307 bool csDiagramClipboard::OnAddShape(wxDiagram
* diagramTo
, wxShape
* newShape
, wxDC
* WXUNUSED(dc
))
309 if (diagramTo
== this)
311 diagramTo
->AddShape(newShape
);
315 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
316 /* csDiagramDocument* doc = */ diagram
->GetDocument();
318 if (newShape
->IsKindOf(CLASSINFO(wxLineShape
)))
319 m_currentCmd
->AddState(new csCommandState(ID_CS_ADD_LINE_SELECT
, newShape
, NULL
));
321 m_currentCmd
->AddState(new csCommandState(ID_CS_ADD_SHAPE_SELECT
, newShape
, NULL
));