]>
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 ostream
& DrawingDocument::SaveObject(ostream
& stream
)
49 wxDocument::SaveObject(stream
);
51 wxInt32 n
= doodleSegments
.Number();
54 wxNode
*node
= doodleSegments
.First();
57 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
58 segment
->SaveObject(stream
);
67 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
69 wxDocument::SaveObject(stream
);
71 wxTextOutputStream
text_stream( stream
);
73 wxInt32 n
= doodleSegments
.Number();
74 text_stream
<< n
<< '\n';
76 wxNode
*node
= doodleSegments
.First();
79 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
80 segment
->SaveObject(stream
);
90 #if wxUSE_STD_IOSTREAM
91 istream
& DrawingDocument::LoadObject(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
.First();
136 DoodleLine
*line
= (DoodleLine
*)node
->Data();
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
);
149 DoodleSegment::~DoodleSegment(void)
151 lines
.DeleteContents(TRUE
);
154 #if wxUSE_STD_IOSTREAM
155 ostream
& DoodleSegment::SaveObject(ostream
& stream
)
157 wxInt32 n
= lines
.Number();
160 wxNode
*node
= lines
.First();
163 DoodleLine
*line
= (DoodleLine
*)node
->Data();
164 stream
<< line
->x1
<< " " <<
174 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
176 wxTextOutputStream
text_stream( stream
);
178 wxInt32 n
= lines
.Number();
179 text_stream
<< n
<< '\n';
181 wxNode
*node
= lines
.First();
184 DoodleLine
*line
= (DoodleLine
*)node
->Data();
185 text_stream
<< line
->x1
<< " " <<
196 #if wxUSE_STD_IOSTREAM
197 istream
& DoodleSegment::LoadObject(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
.First();
240 DoodleLine
*line
= (DoodleLine
*)node
->Data();
241 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
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().Number() > 0)
273 wxNode
*node
= doc
->GetDoodleSegments().Last();
277 segment
= (DoodleSegment
*)node
->Data();
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().Number() > 0)
319 wxNode
*node
= doc
->GetDoodleSegments().Last();
320 DoodleSegment
*seg
= (DoodleSegment
*)node
->Data();
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();