1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Printing and clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
23 #include <wx/ogl/ogl.h> // base header of OGL, includes and adjusts wx/deprecated/setup.h
25 #include <wx/clipbrd.h>
28 #include <wx/metafile.h>
36 IMPLEMENT_DYNAMIC_CLASS(wxDiagramClipboard
, wxDiagram
)
39 bool wxDiagramClipboard::Copy(wxDiagram
* diagram
)
43 return DoCopy(diagram
, this, false, NULL
);
46 // Copy contents to the diagram, with new ids.
48 bool wxDiagramClipboard::Paste(wxDiagram
* diagram
, wxDC
* dc
, int offsetX
, int offsetY
)
50 return DoCopy(this, diagram
, true, dc
, offsetX
, offsetY
);
53 // Universal copy function (to or from clipboard).
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.
59 bool wxDiagramClipboard::DoCopy(wxDiagram
* diagramFrom
, wxDiagram
* diagramTo
, bool newIds
,
60 wxDC
* dc
, int offsetX
, int offsetY
)
62 OnStartCopy(diagramTo
);
64 wxHashTable
mapping(wxKEY_INTEGER
);
66 // First copy all node shapes.
67 wxList
* shapeList
= diagramFrom
->GetShapeList();
68 wxObjectList::compatibility_iterator node
= shapeList
->GetFirst();
71 wxShape
* shape
= (wxShape
*) node
->GetData();
72 if (((diagramFrom
== this) || shape
->Selected()) && !shape
->IsKindOf(CLASSINFO(wxLineShape
)))
74 wxShape
* newShape
= shape
->CreateNewCopy();
75 newShape
->GetLines().Clear();
78 newShape
->AssignNewIds();
80 mapping
.Put((long) shape
, (wxObject
*) newShape
);
82 newShape
->SetX(newShape
->GetX() + offsetX
);
83 newShape
->SetY(newShape
->GetY() + offsetY
);
85 OnAddShape(diagramTo
, newShape
, dc
);
88 node
= node
->GetNext();
91 node
= shapeList
->GetFirst();
94 wxShape
* shape
= (wxShape
*) node
->GetData();
95 if (((diagramFrom
== this) || shape
->Selected()) && shape
->IsKindOf(CLASSINFO(wxLineShape
)))
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()))
101 wxLineShape
* newShape
= (wxLineShape
*) shape
->CreateNewCopy();
102 mapping
.Put((long) shape
, (wxObject
*) newShape
);
105 newShape
->AssignNewIds();
107 wxShape
* fromShape
= (wxShape
*) mapping
.Get((long) lineShape
->GetFrom());
108 wxShape
* toShape
= (wxShape
*) mapping
.Get((long) lineShape
->GetTo());
110 wxASSERT_MSG( (fromShape
!= NULL
), _T("Could not find 'from' shape"));
111 wxASSERT_MSG( (toShape
!= NULL
), _T("Could not find 'to' shape"));
113 fromShape
->AddLine(newShape
, toShape
, newShape
->GetAttachmentFrom(),
114 newShape
->GetAttachmentTo());
116 OnAddShape(diagramTo
, newShape
, dc
);
120 node
= node
->GetNext();
123 // Now make sure line ordering is correct
124 node
= shapeList
->GetFirst();
127 wxShape
* shape
= (wxShape
*) node
->GetData();
128 if (((diagramFrom
== this) || shape
->Selected()) && !shape
->IsKindOf(CLASSINFO(wxLineShape
)))
130 wxShape
* newShape
= (wxShape
*) mapping
.Get((long) shape
);
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.
135 wxObjectList::compatibility_iterator lineNode
= shape
->GetLines().GetFirst();
138 wxLineShape
* lineShape
= (wxLineShape
*) lineNode
->GetData();
139 if ((diagramFrom
== this) || (lineShape
->GetTo()->Selected() && lineShape
->GetFrom()->Selected()))
141 wxLineShape
* newLineShape
= (wxLineShape
*) mapping
.Get((long) lineShape
);
143 wxASSERT_MSG( (newLineShape
!= NULL
), _T("Could not find new line shape"));
145 newLines
.Append(newLineShape
);
148 lineNode
= lineNode
->GetNext();
151 if (newLines
.GetCount() > 0)
152 newShape
->ApplyAttachmentOrdering(newLines
);
154 node
= node
->GetNext();
157 OnEndCopy(diagramTo
);
163 // Draw contents to a Windows metafile device context and a bitmap, and copy
164 // these to the Windows clipboard
165 bool wxDiagramClipboard::CopyToClipboard(double scale
)
168 // Make a metafile DC
172 mfDC
.SetUserScale(scale
, scale
);
174 // Draw on metafile DC
177 // int printWidth = mfDC.MaxX() - mfDC.MinX();
178 // int printHeight = mfDC.MaxY() - mfDC.MinY();
179 int maxX
= (int)mfDC
.MaxX();
180 int maxY
= (int)mfDC
.MaxY();
181 wxMetaFile
*mf
= mfDC
.Close();
183 // Set to a bitmap memory DC
184 wxBitmap
*newBitmap
= new wxBitmap((int)(maxX
+ 10), (int)(maxY
+ 10));
185 if (!newBitmap
->Ok())
190 wxSprintf(buf
, _T("Sorry, could not allocate clipboard bitmap (%dx%d)"), (maxX
+10), (maxY
+10));
191 wxMessageBox(buf
, _T("Clipboard copy problem"));
196 memDC
.SelectObject(*newBitmap
);
199 // Now draw on memory bitmap DC
202 memDC
.SelectObject(wxNullBitmap
);
204 // Open clipboard and set the data
205 if (wxOpenClipboard())
209 // Copy the bitmap to the clipboard
210 wxSetClipboardData(wxDF_BITMAP
, newBitmap
, 0, 0);
212 #if 0 // TODO: replace this code (wxEnhMetaFile doesn't have SetClipboard)
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));
231 wxMessageBox("wxUSE_METAFILE in build required to use Clipboard", _T("Clipboard copy problem"));
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.
240 bool wxDiagramClipboard::OnAddShape(wxDiagram
* diagramTo
, wxShape
* newShape
, wxDC
* dc
)
242 diagramTo
->AddShape(newShape
);
244 if (dc
&& (diagramTo
!= this))
246 newShape
->Select(true, dc
);
256 IMPLEMENT_DYNAMIC_CLASS(csDiagramClipboard
, wxDiagramClipboard
)
259 bool csDiagramClipboard::OnStartCopy(wxDiagram
* diagramTo
)
261 // Do nothing if copying to the clipboard
262 if (diagramTo
== this)
265 // Deselect all objects initially.
267 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
268 csDiagramDocument
* doc
= diagram
->GetDocument();
269 ((csDiagramView
*)doc
->GetFirstView())->SelectAll(false);
271 m_currentCmd
= new csDiagramCommand(_T("Paste"), doc
);
276 bool csDiagramClipboard::OnEndCopy(wxDiagram
* diagramTo
)
278 // Do nothing if copying to the clipboard
279 if (diagramTo
== this)
282 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
283 csDiagramDocument
* doc
= diagram
->GetDocument();
287 if (m_currentCmd
->GetStates().GetCount() == 0)
293 doc
->GetCommandProcessor()->Submit(m_currentCmd
);
300 // Use the command framework to add the shapes, if we're copying to a diagram and
301 // not the clipboard.
302 bool csDiagramClipboard::OnAddShape(wxDiagram
* diagramTo
, wxShape
* newShape
, wxDC
* WXUNUSED(dc
))
304 if (diagramTo
== this)
306 diagramTo
->AddShape(newShape
);
310 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
311 /* csDiagramDocument* doc = */ diagram
->GetDocument();
313 if (newShape
->IsKindOf(CLASSINFO(wxLineShape
)))
314 m_currentCmd
->AddState(new csCommandState(ID_CS_ADD_LINE_SELECT
, newShape
, NULL
));
316 m_currentCmd
->AddState(new csCommandState(ID_CS_ADD_SHAPE_SELECT
, newShape
, NULL
));