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