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 #include "wx/filename.h"
26 #if wxUSE_STD_IOSTREAM
27 #include "wx/ioswrap.h"
29 #include "wx/txtstrm.h"
32 #if !wxUSE_DOC_VIEW_ARCHITECTURE
33 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
38 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
40 DrawingDocument::~DrawingDocument(void)
42 WX_CLEAR_LIST(wxList
, doodleSegments
);
45 #if wxUSE_STD_IOSTREAM
46 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
48 wxDocument::SaveObject(stream
);
50 wxInt32 n
= doodleSegments
.GetCount();
53 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
56 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
57 segment
->SaveObject(stream
);
60 node
= node
->GetNext();
66 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
68 wxDocument::SaveObject(stream
);
70 wxTextOutputStream
text_stream( stream
);
72 wxInt32 n
= doodleSegments
.GetCount();
73 text_stream
<< n
<< '\n';
75 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
78 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
79 segment
->SaveObject(stream
);
82 node
= node
->GetNext();
89 #if wxUSE_STD_IOSTREAM
90 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
92 wxDocument::LoadObject(stream
);
97 for (int i
= 0; i
< n
; i
++)
99 DoodleSegment
*segment
= new DoodleSegment
;
100 segment
->LoadObject(stream
);
101 doodleSegments
.Append(segment
);
107 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
109 wxDocument::LoadObject(stream
);
111 wxTextInputStream
text_stream( stream
);
116 for (int i
= 0; i
< n
; i
++)
118 DoodleSegment
*segment
= new DoodleSegment
;
119 segment
->LoadObject(stream
);
120 doodleSegments
.Append(segment
);
127 DoodleSegment::DoodleSegment(const DoodleSegment
& seg
):wxObject()
129 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
132 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
133 DoodleLine
*newLine
= new DoodleLine
;
134 newLine
->x1
= line
->x1
;
135 newLine
->y1
= line
->y1
;
136 newLine
->x2
= line
->x2
;
137 newLine
->y2
= line
->y2
;
139 lines
.Append(newLine
);
141 node
= node
->GetNext();
145 DoodleSegment::~DoodleSegment(void)
147 WX_CLEAR_LIST(wxList
, lines
);
150 #if wxUSE_STD_IOSTREAM
151 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
153 wxInt32 n
= lines
.GetCount();
156 wxList::compatibility_iterator node
= lines
.GetFirst();
159 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
160 stream
<< line
->x1
<< " " <<
164 node
= node
->GetNext();
170 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
172 wxTextOutputStream
text_stream( stream
);
174 wxInt32 n
= lines
.GetCount();
175 text_stream
<< n
<< _T("\n");
177 wxList::compatibility_iterator node
= lines
.GetFirst();
180 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
181 text_stream
<< line
->x1
<< _T(" ") <<
182 line
->y1
<< _T(" ") <<
183 line
->x2
<< _T(" ") <<
184 line
->y2
<< _T("\n");
185 node
= node
->GetNext();
192 #if wxUSE_STD_IOSTREAM
193 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
198 for (int i
= 0; i
< n
; i
++)
200 DoodleLine
*line
= new DoodleLine
;
201 stream
>> line
->x1
>>
211 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
213 wxTextInputStream
text_stream( stream
);
218 for (int i
= 0; i
< n
; i
++)
220 DoodleLine
*line
= new DoodleLine
;
221 text_stream
>> line
->x1
>>
232 void DoodleSegment::Draw(wxDC
*dc
)
234 wxList::compatibility_iterator node
= lines
.GetFirst();
237 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
238 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
239 node
= node
->GetNext();
244 * Implementation of drawing command
247 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
248 wxCommand(true, name
)
255 DrawingCommand::~DrawingCommand(void)
261 bool DrawingCommand::Do(void)
267 // Cut the last segment
268 if (doc
->GetDoodleSegments().GetCount() > 0)
270 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
274 segment
= (DoodleSegment
*)node
->GetData();
275 doc
->GetDoodleSegments().Erase(node
);
278 doc
->UpdateAllViews();
284 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
286 doc
->UpdateAllViews();
293 bool DrawingCommand::Undo(void)
302 doc
->GetDoodleSegments().Append(segment
);
304 doc
->UpdateAllViews();
305 segment
= (DoodleSegment
*) NULL
;
308 doc
->UpdateAllViews();
313 // Cut the last segment
314 if (doc
->GetDoodleSegments().GetCount() > 0)
316 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
317 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
319 doc
->GetDoodleSegments().Erase(node
);
322 doc
->UpdateAllViews();
329 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
331 // Since text windows have their own method for saving to/loading from files,
332 // we override OnSave/OpenDocument instead of Save/LoadObject
333 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
335 TextEditView
*view
= (TextEditView
*)GetFirstView();
337 if (!view
->textsw
->SaveFile(filename
))
341 wxFileName
fn(filename
) ;
342 fn
.MacSetDefaultTypeAndCreator() ;
347 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
349 TextEditView
*view
= (TextEditView
*)GetFirstView();
350 if (!view
->textsw
->LoadFile(filename
))
353 SetFilename(filename
, true);
359 bool TextEditDocument::IsModified(void) const
361 TextEditView
*view
= (TextEditView
*)GetFirstView();
364 return (wxDocument::IsModified() || view
->textsw
->IsModified());
367 return wxDocument::IsModified();
370 void TextEditDocument::Modify(bool mod
)
372 TextEditView
*view
= (TextEditView
*)GetFirstView();
374 wxDocument::Modify(mod
);
376 if (!mod
&& view
&& view
->textsw
)
377 view
->textsw
->DiscardEdits();