1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
27 #if wxUSE_STD_IOSTREAM
28 #include "wx/ioswrap.h"
30 #include "wx/txtstrm.h"
36 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
38 DrawingDocument::~DrawingDocument(void)
40 WX_CLEAR_LIST(wxList
, doodleSegments
);
43 #if wxUSE_STD_IOSTREAM
44 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
46 wxDocument::SaveObject(stream
);
48 wxInt32 n
= doodleSegments
.GetCount();
49 stream
<< n
<< _T('\n');
51 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
54 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
55 segment
->SaveObject(stream
);
58 node
= node
->GetNext();
64 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
66 wxDocument::SaveObject(stream
);
68 wxTextOutputStream
text_stream( stream
);
70 wxInt32 n
= doodleSegments
.GetCount();
71 text_stream
<< n
<< _T('\n');
73 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
76 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
77 segment
->SaveObject(stream
);
78 text_stream
<< _T('\n');
80 node
= node
->GetNext();
87 #if wxUSE_STD_IOSTREAM
88 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
90 wxDocument::LoadObject(stream
);
95 for (int i
= 0; i
< n
; i
++)
97 DoodleSegment
*segment
= new DoodleSegment
;
98 segment
->LoadObject(stream
);
99 doodleSegments
.Append(segment
);
105 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
107 wxDocument::LoadObject(stream
);
109 wxTextInputStream
text_stream( stream
);
114 for (int i
= 0; i
< n
; i
++)
116 DoodleSegment
*segment
= new DoodleSegment
;
117 segment
->LoadObject(stream
);
118 doodleSegments
.Append(segment
);
125 DoodleSegment::DoodleSegment(const DoodleSegment
& seg
)
128 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
131 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
132 DoodleLine
*newLine
= new DoodleLine
;
133 newLine
->x1
= line
->x1
;
134 newLine
->y1
= line
->y1
;
135 newLine
->x2
= line
->x2
;
136 newLine
->y2
= line
->y2
;
138 lines
.Append(newLine
);
140 node
= node
->GetNext();
144 DoodleSegment::~DoodleSegment(void)
146 WX_CLEAR_LIST(wxList
, lines
);
149 #if wxUSE_STD_IOSTREAM
150 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
152 wxInt32 n
= lines
.GetCount();
153 stream
<< n
<< _T('\n');
155 wxList::compatibility_iterator node
= lines
.GetFirst();
158 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
159 stream
<< line
->x1
<< _T(" ") <<
160 line
->y1
<< _T(" ") <<
161 line
->x2
<< _T(" ") <<
162 line
->y2
<< _T("\n");
163 node
= node
->GetNext();
169 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
171 wxTextOutputStream
text_stream( stream
);
173 wxInt32 n
= lines
.GetCount();
174 text_stream
<< n
<< _T('\n');
176 wxList::compatibility_iterator node
= lines
.GetFirst();
179 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
180 text_stream
<< line
->x1
<< _T(" ") <<
181 line
->y1
<< _T(" ") <<
182 line
->x2
<< _T(" ") <<
183 line
->y2
<< _T("\n");
184 node
= node
->GetNext();
191 #if wxUSE_STD_IOSTREAM
192 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
197 for (int i
= 0; i
< n
; i
++)
199 DoodleLine
*line
= new DoodleLine
;
200 stream
>> line
->x1
>>
210 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
212 wxTextInputStream
text_stream( stream
);
217 for (int i
= 0; i
< n
; i
++)
219 DoodleLine
*line
= new DoodleLine
;
220 text_stream
>> line
->x1
>>
230 void DoodleSegment::Draw(wxDC
*dc
)
232 wxList::compatibility_iterator node
= lines
.GetFirst();
235 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
236 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
237 node
= node
->GetNext();
242 * Implementation of drawing command
245 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
246 wxCommand(true, name
)
253 DrawingCommand::~DrawingCommand(void)
259 bool DrawingCommand::Do(void)
265 // Cut the last segment
266 if (doc
->GetDoodleSegments().GetCount() > 0)
268 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
272 segment
= (DoodleSegment
*)node
->GetData();
273 doc
->GetDoodleSegments().Erase(node
);
276 doc
->UpdateAllViews();
282 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
284 doc
->UpdateAllViews();
291 bool DrawingCommand::Undo(void)
300 doc
->GetDoodleSegments().Append(segment
);
302 doc
->UpdateAllViews();
303 segment
= (DoodleSegment
*) NULL
;
306 doc
->UpdateAllViews();
311 // Cut the last segment
312 if (doc
->GetDoodleSegments().GetCount() > 0)
314 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
315 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
317 doc
->GetDoodleSegments().Erase(node
);
320 doc
->UpdateAllViews();
327 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
329 // Since text windows have their own method for saving to/loading from files,
330 // we override OnSave/OpenDocument instead of Save/LoadObject
331 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
333 TextEditView
*view
= (TextEditView
*)GetFirstView();
335 if (!view
->textsw
->SaveFile(filename
))
341 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
343 TextEditView
*view
= (TextEditView
*)GetFirstView();
344 if (!view
->textsw
->LoadFile(filename
))
347 SetFilename(filename
, true);
353 bool TextEditDocument::IsModified(void) const
355 TextEditView
*view
= (TextEditView
*)GetFirstView();
358 return (wxDocument::IsModified() || view
->textsw
->IsModified());
361 return wxDocument::IsModified();
364 void TextEditDocument::Modify(bool mod
)
366 TextEditView
*view
= (TextEditView
*)GetFirstView();
368 wxDocument::Modify(mod
);
370 if (!mod
&& view
&& view
->textsw
)
371 view
->textsw
->DiscardEdits();