]>
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/wxexpr.h>
28 #include <wx/clipbrd.h>
31 #include <wx/metafile.h>
39 IMPLEMENT_DYNAMIC_CLASS(wxDiagramClipboard
, wxDiagram
)
42 bool wxDiagramClipboard::Copy(wxDiagram
* diagram
)
46 return DoCopy(diagram
, this, FALSE
, NULL
);
49 // Copy contents to the diagram, with new ids.
51 bool wxDiagramClipboard::Paste(wxDiagram
* diagram
, wxDC
* dc
, int offsetX
, int offsetY
)
53 return DoCopy(this, diagram
, TRUE
, dc
, offsetX
, offsetY
);
56 // Universal copy function (to or from clipboard).
58 // Note that this only works for non-composites so far (nested shapes
59 // don't have their old-to-new object mappings stored).
60 // Also, lines don't yet get their attachment points moved to the new offset position
61 // if they have more than 2 points.
62 bool wxDiagramClipboard::DoCopy(wxDiagram
* diagramFrom
, wxDiagram
* diagramTo
, bool newIds
,
63 wxDC
* dc
, int offsetX
, int offsetY
)
65 OnStartCopy(diagramTo
);
67 wxHashTable
mapping(wxKEY_INTEGER
);
69 // First copy all node shapes.
70 wxList
* shapeList
= diagramFrom
->GetShapeList();
71 wxNode
* node
= shapeList
->First();
74 wxShape
* shape
= (wxShape
*) node
->Data();
75 if (((diagramFrom
== this) || shape
->Selected()) && !shape
->IsKindOf(CLASSINFO(wxLineShape
)))
77 wxShape
* newShape
= shape
->CreateNewCopy();
78 newShape
->GetLines().Clear();
81 newShape
->AssignNewIds();
83 mapping
.Put((long) shape
, (wxObject
*) newShape
);
85 newShape
->SetX(newShape
->GetX() + offsetX
);
86 newShape
->SetY(newShape
->GetY() + offsetY
);
88 OnAddShape(diagramTo
, newShape
, dc
);
94 node
= shapeList
->First();
97 wxShape
* shape
= (wxShape
*) node
->Data();
98 if (((diagramFrom
== this) || shape
->Selected()) && shape
->IsKindOf(CLASSINFO(wxLineShape
)))
100 wxLineShape
* lineShape
= (wxLineShape
*) shape
;
101 // Only copy a line if its ends are selected too.
102 if ((diagramFrom
== this) || (lineShape
->GetTo()->Selected() && lineShape
->GetFrom()->Selected()))
104 wxLineShape
* newShape
= (wxLineShape
*) shape
->CreateNewCopy();
105 mapping
.Put((long) shape
, (wxObject
*) newShape
);
108 newShape
->AssignNewIds();
110 wxShape
* fromShape
= (wxShape
*) mapping
.Get((long) lineShape
->GetFrom());
111 wxShape
* toShape
= (wxShape
*) mapping
.Get((long) lineShape
->GetTo());
113 wxASSERT_MSG( (fromShape
!= NULL
), "Could not find 'from' shape");
114 wxASSERT_MSG( (toShape
!= NULL
), "Could not find 'to' shape");
116 fromShape
->AddLine(newShape
, toShape
, newShape
->GetAttachmentFrom(),
117 newShape
->GetAttachmentTo());
119 OnAddShape(diagramTo
, newShape
, dc
);
126 // Now make sure line ordering is correct
127 node
= shapeList
->First();
130 wxShape
* shape
= (wxShape
*) node
->Data();
131 if (((diagramFrom
== this) || shape
->Selected()) && !shape
->IsKindOf(CLASSINFO(wxLineShape
)))
133 wxShape
* newShape
= (wxShape
*) mapping
.Get((long) shape
);
135 // Make a list of all the new lines, in the same order as the old lines.
136 // Then apply the list of new lines to the shape.
138 wxNode
* lineNode
= shape
->GetLines().First();
141 wxLineShape
* lineShape
= (wxLineShape
*) lineNode
->Data();
142 if ((diagramFrom
== this) || (lineShape
->GetTo()->Selected() && lineShape
->GetFrom()->Selected()))
144 wxLineShape
* newLineShape
= (wxLineShape
*) mapping
.Get((long) lineShape
);
146 wxASSERT_MSG( (newLineShape
!= NULL
), "Could not find new line shape");
148 newLines
.Append(newLineShape
);
151 lineNode
= lineNode
->Next();
154 if (newLines
.Number() > 0)
155 newShape
->ApplyAttachmentOrdering(newLines
);
160 OnEndCopy(diagramTo
);
166 // Draw contents to a Windows metafile device context and a bitmap, and copy
167 // these to the Windows clipboard
168 bool wxDiagramClipboard::CopyToClipboard(double scale
)
170 // Make a metafile DC
174 mfDC
.SetUserScale(scale
, scale
);
176 // Draw on metafile DC
179 int printWidth
= mfDC
.MaxX() - mfDC
.MinX();
180 int printHeight
= mfDC
.MaxY() - mfDC
.MinY();
181 int maxX
= (int)mfDC
.MaxX();
182 int maxY
= (int)mfDC
.MaxY();
183 wxMetaFile
*mf
= mfDC
.Close();
185 // Set to a bitmap memory DC
186 wxBitmap
*newBitmap
= new wxBitmap((int)(maxX
+ 10), (int)(maxY
+ 10));
187 if (!newBitmap
->Ok())
192 sprintf(buf
, "Sorry, could not allocate clipboard bitmap (%dx%d)", (maxX
+10), (maxY
+10));
193 wxMessageBox(buf
, "Clipboard copy problem");
198 memDC
.SelectObject(*newBitmap
);
201 // Now draw on memory bitmap DC
204 memDC
.SelectObject(wxNullBitmap
);
206 // Open clipboard and set the data
207 if (wxOpenClipboard())
211 // Copy the bitmap to the clipboard
212 wxSetClipboardData(wxDF_BITMAP
, newBitmap
, 0, 0);
214 #if 0 // TODO: replace this code (wxEnhMetaFile doesn't have SetClipboard)
217 // Copy the metafile to the clipboard
218 // Allow a small margin
219 bool success
= mf
->SetClipboard((int)(mfDC
.MaxX() + 15), (int)(mfDC
.MaxY() + 15));
236 // Override this to e.g. have the shape added through a Do/Undo command system.
237 // By default, we'll just add it directly to the destination diagram.
238 bool wxDiagramClipboard::OnAddShape(wxDiagram
* diagramTo
, wxShape
* newShape
, wxDC
* dc
)
240 diagramTo
->AddShape(newShape
);
242 if (dc
&& (diagramTo
!= this))
244 newShape
->Select(TRUE
, dc
);
254 IMPLEMENT_DYNAMIC_CLASS(csDiagramClipboard
, wxDiagramClipboard
)
257 bool csDiagramClipboard::OnStartCopy(wxDiagram
* diagramTo
)
259 // Do nothing if copying to the clipboard
260 if (diagramTo
== this)
263 // Deselect all objects initially.
265 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
266 csDiagramDocument
* doc
= diagram
->GetDocument();
267 ((csDiagramView
*)doc
->GetFirstView())->SelectAll(FALSE
);
269 m_currentCmd
= new csDiagramCommand("Paste", doc
);
274 bool csDiagramClipboard::OnEndCopy(wxDiagram
* diagramTo
)
276 // Do nothing if copying to the clipboard
277 if (diagramTo
== this)
280 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
281 csDiagramDocument
* doc
= diagram
->GetDocument();
285 if (m_currentCmd
->GetStates().Number() == 0)
291 doc
->GetCommandProcessor()->Submit(m_currentCmd
);
298 // Use the command framework to add the shapes, if we're copying to a diagram and
299 // not the clipboard.
300 bool csDiagramClipboard::OnAddShape(wxDiagram
* diagramTo
, wxShape
* newShape
, wxDC
* dc
)
302 if (diagramTo
== this)
304 diagramTo
->AddShape(newShape
);
308 csDiagram
* diagram
= (csDiagram
*) diagramTo
;
309 csDiagramDocument
* doc
= diagram
->GetDocument();
311 if (newShape
->IsKindOf(CLASSINFO(wxLineShape
)))
312 m_currentCmd
->AddState(new csCommandState(ID_CS_ADD_LINE_SELECT
, newShape
, NULL
));
314 m_currentCmd
->AddState(new csCommandState(ID_CS_ADD_SHAPE_SELECT
, newShape
, NULL
));