]>
git.saurik.com Git - wxWidgets.git/blob - samples/docvwmdi/doc.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 // #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/txtstrm.h"
28 #if !wxUSE_DOC_VIEW_ARCHITECTURE
29 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
35 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
37 DrawingDocument::DrawingDocument(void)
41 DrawingDocument::~DrawingDocument(void)
43 doodleSegments
.DeleteContents(TRUE
);
46 #if wxUSE_STD_IOSTREAM
47 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
49 wxDocument::SaveObject(stream
);
51 wxInt32 n
= doodleSegments
.GetCount();
52 stream
<< n
<< _T('\n');
54 wxNode
*node
= doodleSegments
.GetFirst();
57 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
58 segment
->SaveObject(stream
);
61 node
= node
->GetNext();
67 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
69 wxDocument::SaveObject(stream
);
71 wxTextOutputStream
text_stream( stream
);
73 wxInt32 n
= doodleSegments
.GetCount();
74 text_stream
<< n
<< _T('\n');
76 wxNode
*node
= doodleSegments
.GetFirst();
79 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
80 segment
->SaveObject(stream
);
81 text_stream
<< _T('\n');
83 node
= node
->GetNext();
90 #if wxUSE_STD_IOSTREAM
91 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
93 wxDocument::LoadObject(stream
);
98 for (int i
= 0; i
< n
; i
++)
100 DoodleSegment
*segment
= new DoodleSegment
;
101 segment
->LoadObject(stream
);
102 doodleSegments
.Append(segment
);
108 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
110 wxDocument::LoadObject(stream
);
112 wxTextInputStream
text_stream( stream
);
117 for (int i
= 0; i
< n
; i
++)
119 DoodleSegment
*segment
= new DoodleSegment
;
120 segment
->LoadObject(stream
);
121 doodleSegments
.Append(segment
);
127 DoodleSegment::DoodleSegment(void)
131 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
133 wxNode
*node
= seg
.lines
.GetFirst();
136 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
137 DoodleLine
*newLine
= new DoodleLine
;
138 newLine
->x1
= line
->x1
;
139 newLine
->y1
= line
->y1
;
140 newLine
->x2
= line
->x2
;
141 newLine
->y2
= line
->y2
;
143 lines
.Append(newLine
);
145 node
= node
->GetNext();
149 DoodleSegment::~DoodleSegment(void)
151 lines
.DeleteContents(TRUE
);
154 #if wxUSE_STD_IOSTREAM
155 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
157 wxInt32 n
= lines
.GetCount();
158 stream
<< n
<< _T('\n');
160 wxNode
*node
= lines
.GetFirst();
163 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
164 stream
<< line
->x1
<< _T(" ") <<
165 line
->y1
<< _T(" ") <<
166 line
->x2
<< _T(" ") <<
167 line
->y2
<< _T("\n");
168 node
= node
->GetNext();
174 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
176 wxTextOutputStream
text_stream( stream
);
178 wxInt32 n
= lines
.GetCount();
179 text_stream
<< n
<< _T('\n');
181 wxNode
*node
= lines
.GetFirst();
184 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
185 text_stream
<< line
->x1
<< _T(" ") <<
186 line
->y1
<< _T(" ") <<
187 line
->x2
<< _T(" ") <<
188 line
->y2
<< _T("\n");
189 node
= node
->GetNext();
196 #if wxUSE_STD_IOSTREAM
197 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
202 for (int i
= 0; i
< n
; i
++)
204 DoodleLine
*line
= new DoodleLine
;
205 stream
>> line
->x1
>>
215 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
217 wxTextInputStream
text_stream( stream
);
222 for (int i
= 0; i
< n
; i
++)
224 DoodleLine
*line
= new DoodleLine
;
225 text_stream
>> line
->x1
>>
235 void DoodleSegment::Draw(wxDC
*dc
)
237 wxNode
*node
= lines
.GetFirst();
240 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
241 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
242 node
= node
->GetNext();
247 * Implementation of drawing command
250 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
251 wxCommand(TRUE
, name
)
258 DrawingCommand::~DrawingCommand(void)
264 bool DrawingCommand::Do(void)
270 // Cut the last segment
271 if (doc
->GetDoodleSegments().GetCount() > 0)
273 wxNode
*node
= doc
->GetDoodleSegments().GetLast();
277 segment
= (DoodleSegment
*)node
->GetData();
281 doc
->UpdateAllViews();
287 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
289 doc
->UpdateAllViews();
296 bool DrawingCommand::Undo(void)
305 doc
->GetDoodleSegments().Append(segment
);
307 doc
->UpdateAllViews();
308 segment
= (DoodleSegment
*) NULL
;
311 doc
->UpdateAllViews();
316 // Cut the last segment
317 if (doc
->GetDoodleSegments().GetCount() > 0)
319 wxNode
*node
= doc
->GetDoodleSegments().GetLast();
320 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
325 doc
->UpdateAllViews();
332 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
334 // Since text windows have their own method for saving to/loading from files,
335 // we override OnSave/OpenDocument instead of Save/LoadObject
336 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
338 TextEditView
*view
= (TextEditView
*)GetFirstView();
340 if (!view
->textsw
->SaveFile(filename
))
346 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
348 TextEditView
*view
= (TextEditView
*)GetFirstView();
349 if (!view
->textsw
->LoadFile(filename
))
352 SetFilename(filename
, TRUE
);
358 bool TextEditDocument::IsModified(void) const
360 TextEditView
*view
= (TextEditView
*)GetFirstView();
363 return (wxDocument::IsModified() || view
->textsw
->IsModified());
366 return wxDocument::IsModified();
369 void TextEditDocument::Modify(bool mod
)
371 TextEditView
*view
= (TextEditView
*)GetFirstView();
373 wxDocument::Modify(mod
);
375 if (!mod
&& view
&& view
->textsw
)
376 view
->textsw
->DiscardEdits();