]>
git.saurik.com Git - wxWidgets.git/blob - contrib/samples/ogl/studio/csprint.cpp
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 wxNode
* node
= shapeList
->First();
76 wxShape
* shape
= (wxShape
*) node
->Data();
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
);
96 node
= shapeList
->First();
99 wxShape
* shape
= (wxShape
*) node
->Data();
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
);
128 // Now make sure line ordering is correct
129 node
= shapeList
->First();
132 wxShape
* shape
= (wxShape
*) node
->Data();
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 wxNode
* lineNode
= shape
->GetLines().First();
143 wxLineShape
* lineShape
= (wxLineShape
*) lineNode
->Data();
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
->Next();
156 if (newLines
.Number() > 0)
157 newShape
->ApplyAttachmentOrdering(newLines
);
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
)
172 // Make a metafile DC
176 mfDC
.SetUserScale(scale
, scale
);
178 // Draw on metafile DC
181 /* int printWidth = */ mfDC
.MaxX() - mfDC
.MinX();
182 /* int printHeight = */ mfDC
.MaxY() - mfDC
.MinY();
183 int maxX
= (int)mfDC
.MaxX();
184 int maxY
= (int)mfDC
.MaxY();
185 wxMetaFile
*mf
= mfDC
.Close();
187 // Set to a bitmap memory DC
188 wxBitmap
*newBitmap
= new wxBitmap((int)(maxX
+ 10), (int)(maxY
+ 10));
189 if (!newBitmap
->Ok())
194 sprintf(buf
, "Sorry, could not allocate clipboard bitmap (%dx%d)", (maxX
+10), (maxY
+10));
195 wxMessageBox(buf
, _T("Clipboard copy problem"));
200 memDC
.SelectObject(*newBitmap
);
203 // Now draw on memory bitmap DC
206 memDC
.SelectObject(wxNullBitmap
);
208 // Open clipboard and set the data
209 if (wxOpenClipboard())
213 // Copy the bitmap to the clipboard
214 wxSetClipboardData(wxDF_BITMAP
, newBitmap
, 0, 0);
216 #if 0 // TODO: replace this code (wxEnhMetaFile doesn't have SetClipboard)
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));
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().Number() == 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
));