1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
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)
39 WX_CLEAR_LIST(wxList
, doodleSegments
);
42 #if wxUSE_STD_IOSTREAM
43 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
45 wxDocument::SaveObject(stream
);
47 wxInt32 n
= doodleSegments
.GetCount();
48 stream
<< n
<< _T('\n');
50 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
53 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
54 segment
->SaveObject(stream
);
57 node
= node
->GetNext();
63 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
65 wxDocument::SaveObject(stream
);
67 wxTextOutputStream
text_stream( stream
);
69 wxInt32 n
= doodleSegments
.GetCount();
70 text_stream
<< n
<< _T('\n');
72 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
75 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
76 segment
->SaveObject(stream
);
77 text_stream
<< _T('\n');
79 node
= node
->GetNext();
86 #if wxUSE_STD_IOSTREAM
87 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
89 wxDocument::LoadObject(stream
);
94 for (int i
= 0; i
< n
; i
++)
96 DoodleSegment
*segment
= new DoodleSegment
;
97 segment
->LoadObject(stream
);
98 doodleSegments
.Append(segment
);
104 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
106 wxDocument::LoadObject(stream
);
108 wxTextInputStream
text_stream( stream
);
113 for (int i
= 0; i
< n
; i
++)
115 DoodleSegment
*segment
= new DoodleSegment
;
116 segment
->LoadObject(stream
);
117 doodleSegments
.Append(segment
);
124 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
126 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
129 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
130 DoodleLine
*newLine
= new DoodleLine
;
131 newLine
->x1
= line
->x1
;
132 newLine
->y1
= line
->y1
;
133 newLine
->x2
= line
->x2
;
134 newLine
->y2
= line
->y2
;
136 lines
.Append(newLine
);
138 node
= node
->GetNext();
142 DoodleSegment::~DoodleSegment(void)
144 WX_CLEAR_LIST(wxList
, lines
);
147 #if wxUSE_STD_IOSTREAM
148 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
150 wxInt32 n
= lines
.GetCount();
151 stream
<< n
<< _T('\n');
153 wxList::compatibility_iterator node
= lines
.GetFirst();
156 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
157 stream
<< line
->x1
<< _T(" ") <<
158 line
->y1
<< _T(" ") <<
159 line
->x2
<< _T(" ") <<
160 line
->y2
<< _T("\n");
161 node
= node
->GetNext();
167 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
169 wxTextOutputStream
text_stream( stream
);
171 wxInt32 n
= lines
.GetCount();
172 text_stream
<< n
<< _T('\n');
174 wxList::compatibility_iterator node
= lines
.GetFirst();
177 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
178 text_stream
<< line
->x1
<< _T(" ") <<
179 line
->y1
<< _T(" ") <<
180 line
->x2
<< _T(" ") <<
181 line
->y2
<< _T("\n");
182 node
= node
->GetNext();
189 #if wxUSE_STD_IOSTREAM
190 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
195 for (int i
= 0; i
< n
; i
++)
197 DoodleLine
*line
= new DoodleLine
;
198 stream
>> line
->x1
>>
208 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
210 wxTextInputStream
text_stream( stream
);
215 for (int i
= 0; i
< n
; i
++)
217 DoodleLine
*line
= new DoodleLine
;
218 text_stream
>> line
->x1
>>
228 void DoodleSegment::Draw(wxDC
*dc
)
230 wxList::compatibility_iterator node
= lines
.GetFirst();
233 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
234 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
235 node
= node
->GetNext();
240 * Implementation of drawing command
243 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
244 wxCommand(true, name
)
251 DrawingCommand::~DrawingCommand(void)
257 bool DrawingCommand::Do(void)
263 // Cut the last segment
264 if (doc
->GetDoodleSegments().GetCount() > 0)
266 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
270 segment
= (DoodleSegment
*)node
->GetData();
271 doc
->GetDoodleSegments().Erase(node
);
274 doc
->UpdateAllViews();
280 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
282 doc
->UpdateAllViews();
289 bool DrawingCommand::Undo(void)
298 doc
->GetDoodleSegments().Append(segment
);
300 doc
->UpdateAllViews();
301 segment
= (DoodleSegment
*) NULL
;
304 doc
->UpdateAllViews();
309 // Cut the last segment
310 if (doc
->GetDoodleSegments().GetCount() > 0)
312 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
313 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
315 doc
->GetDoodleSegments().Erase(node
);
318 doc
->UpdateAllViews();
325 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
327 // Since text windows have their own method for saving to/loading from files,
328 // we override OnSave/OpenDocument instead of Save/LoadObject
329 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
331 TextEditView
*view
= (TextEditView
*)GetFirstView();
333 if (!view
->textsw
->SaveFile(filename
))
339 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
341 TextEditView
*view
= (TextEditView
*)GetFirstView();
342 if (!view
->textsw
->LoadFile(filename
))
345 SetFilename(filename
, true);
351 bool TextEditDocument::IsModified(void) const
353 TextEditView
*view
= (TextEditView
*)GetFirstView();
356 return (wxDocument::IsModified() || view
->textsw
->IsModified());
359 return wxDocument::IsModified();
362 void TextEditDocument::Modify(bool mod
)
364 TextEditView
*view
= (TextEditView
*)GetFirstView();
366 wxDocument::Modify(mod
);
368 if (!mod
&& view
&& view
->textsw
)
369 view
->textsw
->DiscardEdits();