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 #include "wx/filename.h"
31 #if !wxUSE_DOC_VIEW_ARCHITECTURE
32 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
37 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
39 DrawingDocument::~DrawingDocument(void)
41 WX_CLEAR_LIST(wxList
, doodleSegments
);
44 #if wxUSE_STD_IOSTREAM
45 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
47 wxDocument::SaveObject(stream
);
49 wxInt32 n
= doodleSegments
.GetCount();
52 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
55 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
56 segment
->SaveObject(stream
);
59 node
= node
->GetNext();
65 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
67 wxDocument::SaveObject(stream
);
69 wxTextOutputStream
text_stream( stream
);
71 wxInt32 n
= doodleSegments
.GetCount();
72 text_stream
<< n
<< '\n';
74 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
77 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
78 segment
->SaveObject(stream
);
81 node
= node
->GetNext();
88 #if wxUSE_STD_IOSTREAM
89 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
91 wxDocument::LoadObject(stream
);
96 for (int i
= 0; i
< n
; i
++)
98 DoodleSegment
*segment
= new DoodleSegment
;
99 segment
->LoadObject(stream
);
100 doodleSegments
.Append(segment
);
106 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
108 wxDocument::LoadObject(stream
);
110 wxTextInputStream
text_stream( stream
);
115 for (int i
= 0; i
< n
; i
++)
117 DoodleSegment
*segment
= new DoodleSegment
;
118 segment
->LoadObject(stream
);
119 doodleSegments
.Append(segment
);
126 DoodleSegment::DoodleSegment(DoodleSegment
& seg
):wxObject()
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();
155 wxList::compatibility_iterator node
= lines
.GetFirst();
158 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
159 stream
<< line
->x1
<< " " <<
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
>>
231 void DoodleSegment::Draw(wxDC
*dc
)
233 wxList::compatibility_iterator node
= lines
.GetFirst();
236 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
237 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
238 node
= node
->GetNext();
243 * Implementation of drawing command
246 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
247 wxCommand(true, name
)
254 DrawingCommand::~DrawingCommand(void)
260 bool DrawingCommand::Do(void)
266 // Cut the last segment
267 if (doc
->GetDoodleSegments().GetCount() > 0)
269 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
273 segment
= (DoodleSegment
*)node
->GetData();
274 doc
->GetDoodleSegments().Erase(node
);
277 doc
->UpdateAllViews();
283 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
285 doc
->UpdateAllViews();
292 bool DrawingCommand::Undo(void)
301 doc
->GetDoodleSegments().Append(segment
);
303 doc
->UpdateAllViews();
304 segment
= (DoodleSegment
*) NULL
;
307 doc
->UpdateAllViews();
312 // Cut the last segment
313 if (doc
->GetDoodleSegments().GetCount() > 0)
315 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
316 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
318 doc
->GetDoodleSegments().Erase(node
);
321 doc
->UpdateAllViews();
328 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
330 // Since text windows have their own method for saving to/loading from files,
331 // we override OnSave/OpenDocument instead of Save/LoadObject
332 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
334 TextEditView
*view
= (TextEditView
*)GetFirstView();
336 if (!view
->textsw
->SaveFile(filename
))
340 wxFileName
fn(filename
) ;
341 fn
.MacSetDefaultTypeAndCreator() ;
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();