]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/ogl/studio/csprint.cpp
Add libs html and xml to bakefiles of samples/contribs/demos using lib adv
[wxWidgets.git] / contrib / samples / ogl / studio / csprint.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include <wx/wx.h>
21 #endif
22
23 #include <wx/ogl/ogl.h> // base header of OGL, includes and adjusts wx/deprecated/setup.h
24
25 #include <wx/clipbrd.h>
26
27 #ifdef __WXMSW__
28 #include <wx/metafile.h>
29 #endif
30
31 #include "studio.h"
32 #include "doc.h"
33 #include "shapes.h"
34 #include "view.h"
35
36 IMPLEMENT_DYNAMIC_CLASS(wxDiagramClipboard, wxDiagram)
37
38 // Copy selection
39 bool wxDiagramClipboard::Copy(wxDiagram* diagram)
40 {
41 DeleteAllShapes();
42
43 return DoCopy(diagram, this, false, NULL);
44 }
45
46 // Copy contents to the diagram, with new ids.
47
48 bool wxDiagramClipboard::Paste(wxDiagram* diagram, wxDC* dc, int offsetX, int offsetY)
49 {
50 return DoCopy(this, diagram, true, dc, offsetX, offsetY);
51 }
52
53 // Universal copy function (to or from clipboard).
54 // TODO:
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)
61 {
62 OnStartCopy(diagramTo);
63
64 wxHashTable mapping(wxKEY_INTEGER);
65
66 // First copy all node shapes.
67 wxList* shapeList = diagramFrom->GetShapeList();
68 wxObjectList::compatibility_iterator node = shapeList->GetFirst();
69 while (node)
70 {
71 wxShape* shape = (wxShape*) node->GetData();
72 if (((diagramFrom == this) || shape->Selected()) && !shape->IsKindOf(CLASSINFO(wxLineShape)))
73 {
74 wxShape* newShape = shape->CreateNewCopy();
75 newShape->GetLines().Clear();
76 if (newIds)
77 {
78 newShape->AssignNewIds();
79 }
80 mapping.Put((long) shape, (wxObject*) newShape);
81
82 newShape->SetX(newShape->GetX() + offsetX);
83 newShape->SetY(newShape->GetY() + offsetY);
84
85 OnAddShape(diagramTo, newShape, dc);
86
87 }
88 node = node->GetNext();
89 }
90
91 node = shapeList->GetFirst();
92 while (node)
93 {
94 wxShape* shape = (wxShape*) node->GetData();
95 if (((diagramFrom == this) || shape->Selected()) && shape->IsKindOf(CLASSINFO(wxLineShape)))
96 {
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()))
100 {
101 wxLineShape* newShape = (wxLineShape*) shape->CreateNewCopy();
102 mapping.Put((long) shape, (wxObject*) newShape);
103
104 if (newIds)
105 newShape->AssignNewIds();
106
107 wxShape* fromShape = (wxShape*) mapping.Get((long) lineShape->GetFrom());
108 wxShape* toShape = (wxShape*) mapping.Get((long) lineShape->GetTo());
109
110 wxASSERT_MSG( (fromShape != NULL), _T("Could not find 'from' shape"));
111 wxASSERT_MSG( (toShape != NULL), _T("Could not find 'to' shape"));
112
113 fromShape->AddLine(newShape, toShape, newShape->GetAttachmentFrom(),
114 newShape->GetAttachmentTo());
115
116 OnAddShape(diagramTo, newShape, dc);
117
118 }
119 }
120 node = node->GetNext();
121 }
122
123 // Now make sure line ordering is correct
124 node = shapeList->GetFirst();
125 while (node)
126 {
127 wxShape* shape = (wxShape*) node->GetData();
128 if (((diagramFrom == this) || shape->Selected()) && !shape->IsKindOf(CLASSINFO(wxLineShape)))
129 {
130 wxShape* newShape = (wxShape*) mapping.Get((long) shape);
131
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.
134 wxList newLines;
135 wxObjectList::compatibility_iterator lineNode = shape->GetLines().GetFirst();
136 while (lineNode)
137 {
138 wxLineShape* lineShape = (wxLineShape*) lineNode->GetData();
139 if ((diagramFrom == this) || (lineShape->GetTo()->Selected() && lineShape->GetFrom()->Selected()))
140 {
141 wxLineShape* newLineShape = (wxLineShape*) mapping.Get((long) lineShape);
142
143 wxASSERT_MSG( (newLineShape != NULL), _T("Could not find new line shape"));
144
145 newLines.Append(newLineShape);
146 }
147
148 lineNode = lineNode->GetNext();
149 }
150
151 if (newLines.GetCount() > 0)
152 newShape->ApplyAttachmentOrdering(newLines);
153 }
154 node = node->GetNext();
155 }
156
157 OnEndCopy(diagramTo);
158
159 return true;
160 }
161
162 #ifdef __WXMSW__
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)
166 {
167 #if wxUSE_METAFILE
168 // Make a metafile DC
169 wxMetaFileDC mfDC;
170 if (mfDC.Ok())
171 {
172 mfDC.SetUserScale(scale, scale);
173
174 // Draw on metafile DC
175 Redraw(mfDC);
176
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();
182
183 // Set to a bitmap memory DC
184 wxBitmap *newBitmap = new wxBitmap((int)(maxX + 10), (int)(maxY + 10));
185 if (!newBitmap->Ok())
186 {
187 delete newBitmap;
188
189 wxChar buf[200];
190 wxSprintf(buf, _T("Sorry, could not allocate clipboard bitmap (%dx%d)"), (maxX+10), (maxY+10));
191 wxMessageBox(buf, _T("Clipboard copy problem"));
192 return false;
193 }
194
195 wxMemoryDC memDC;
196 memDC.SelectObject(*newBitmap);
197 memDC.Clear();
198
199 // Now draw on memory bitmap DC
200 Redraw(memDC);
201
202 memDC.SelectObject(wxNullBitmap);
203
204 // Open clipboard and set the data
205 if (wxOpenClipboard())
206 {
207 wxEmptyClipboard();
208
209 // Copy the bitmap to the clipboard
210 wxSetClipboardData(wxDF_BITMAP, newBitmap, 0, 0);
211
212 #if 0 // TODO: replace this code (wxEnhMetaFile doesn't have SetClipboard)
213 if (mf)
214 {
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));
218 }
219 #endif
220
221 // Close clipboard
222 wxCloseClipboard();
223 }
224
225 delete newBitmap;
226 delete mf;
227
228 }
229 return true;
230 #else
231 wxMessageBox("wxUSE_METAFILE in build required to use Clipboard", _T("Clipboard copy problem"));
232 return false;
233 #endif
234 }
235 #endif
236 // __WXMSW__
237
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)
241 {
242 diagramTo->AddShape(newShape);
243
244 if (dc && (diagramTo != this))
245 {
246 newShape->Select(true, dc);
247 }
248
249 return true;
250 }
251
252 /*
253 * csDiagramClipboard
254 */
255
256 IMPLEMENT_DYNAMIC_CLASS(csDiagramClipboard, wxDiagramClipboard)
257
258 // Start/end copying
259 bool csDiagramClipboard::OnStartCopy(wxDiagram* diagramTo)
260 {
261 // Do nothing if copying to the clipboard
262 if (diagramTo == this)
263 return true;
264
265 // Deselect all objects initially.
266
267 csDiagram* diagram = (csDiagram*) diagramTo;
268 csDiagramDocument* doc = diagram->GetDocument();
269 ((csDiagramView*)doc->GetFirstView())->SelectAll(false);
270
271 m_currentCmd = new csDiagramCommand(_T("Paste"), doc);
272
273 return true;
274 }
275
276 bool csDiagramClipboard::OnEndCopy(wxDiagram* diagramTo)
277 {
278 // Do nothing if copying to the clipboard
279 if (diagramTo == this)
280 return true;
281
282 csDiagram* diagram = (csDiagram*) diagramTo;
283 csDiagramDocument* doc = diagram->GetDocument();
284
285 if (m_currentCmd)
286 {
287 if (m_currentCmd->GetStates().GetCount() == 0)
288 {
289 delete m_currentCmd;
290 }
291 else
292 {
293 doc->GetCommandProcessor()->Submit(m_currentCmd);
294 m_currentCmd = NULL;
295 }
296 }
297 return true;
298 }
299
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))
303 {
304 if (diagramTo == this)
305 {
306 diagramTo->AddShape(newShape);
307 }
308 else
309 {
310 csDiagram* diagram = (csDiagram*) diagramTo;
311 /* csDiagramDocument* doc = */ diagram->GetDocument();
312
313 if (newShape->IsKindOf(CLASSINFO(wxLineShape)))
314 m_currentCmd->AddState(new csCommandState(ID_CS_ADD_LINE_SELECT, newShape, NULL));
315 else
316 m_currentCmd->AddState(new csCommandState(ID_CS_ADD_SHAPE_SELECT, newShape, NULL));
317 }
318
319 return true;
320 }
321
322