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"
22 #include "wx/txtstrm.h"
24 #include "wx/filename.h"
27 #if !wxUSE_DOC_VIEW_ARCHITECTURE
28 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
33 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
35 DrawingDocument::~DrawingDocument(void)
37 WX_CLEAR_LIST(wxList
, doodleSegments
);
40 #if wxUSE_STD_IOSTREAM
41 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
43 wxDocument::SaveObject(stream
);
45 wxInt32 n
= doodleSegments
.GetCount();
48 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
51 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
52 segment
->SaveObject(stream
);
55 node
= node
->GetNext();
61 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
63 wxDocument::SaveObject(stream
);
65 wxTextOutputStream
text_stream( stream
);
67 wxInt32 n
= doodleSegments
.GetCount();
68 text_stream
<< n
<< '\n';
70 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
73 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
74 segment
->SaveObject(stream
);
77 node
= node
->GetNext();
84 #if wxUSE_STD_IOSTREAM
85 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
87 wxDocument::LoadObject(stream
);
92 for (int i
= 0; i
< n
; i
++)
94 DoodleSegment
*segment
= new DoodleSegment
;
95 segment
->LoadObject(stream
);
96 doodleSegments
.Append(segment
);
102 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
104 wxDocument::LoadObject(stream
);
106 wxTextInputStream
text_stream( stream
);
111 for (int i
= 0; i
< n
; i
++)
113 DoodleSegment
*segment
= new DoodleSegment
;
114 segment
->LoadObject(stream
);
115 doodleSegments
.Append(segment
);
122 DoodleSegment::DoodleSegment(const DoodleSegment
& seg
):wxObject()
124 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
127 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
128 DoodleLine
*newLine
= new DoodleLine
;
129 newLine
->x1
= line
->x1
;
130 newLine
->y1
= line
->y1
;
131 newLine
->x2
= line
->x2
;
132 newLine
->y2
= line
->y2
;
134 lines
.Append(newLine
);
136 node
= node
->GetNext();
140 DoodleSegment::~DoodleSegment(void)
142 WX_CLEAR_LIST(wxList
, lines
);
145 #if wxUSE_STD_IOSTREAM
146 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
148 wxInt32 n
= lines
.GetCount();
151 wxList::compatibility_iterator node
= lines
.GetFirst();
154 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
155 stream
<< line
->x1
<< " " <<
159 node
= node
->GetNext();
165 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
167 wxTextOutputStream
text_stream( stream
);
169 wxInt32 n
= lines
.GetCount();
170 text_stream
<< n
<< _T('\n');
172 wxList::compatibility_iterator node
= lines
.GetFirst();
175 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
176 text_stream
<< line
->x1
<< _T(" ") <<
177 line
->y1
<< _T(" ") <<
178 line
->x2
<< _T(" ") <<
179 line
->y2
<< _T("\n");
180 node
= node
->GetNext();
187 #if wxUSE_STD_IOSTREAM
188 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
193 for (int i
= 0; i
< n
; i
++)
195 DoodleLine
*line
= new DoodleLine
;
196 stream
>> line
->x1
>>
206 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
208 wxTextInputStream
text_stream( stream
);
213 for (int i
= 0; i
< n
; i
++)
215 DoodleLine
*line
= new DoodleLine
;
216 text_stream
>> line
->x1
>>
227 void DoodleSegment::Draw(wxDC
*dc
)
229 wxList::compatibility_iterator node
= lines
.GetFirst();
232 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
233 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
234 node
= node
->GetNext();
239 * Implementation of drawing command
242 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
243 wxCommand(true, name
)
250 DrawingCommand::~DrawingCommand(void)
256 bool DrawingCommand::Do(void)
262 // Cut the last segment
263 if (doc
->GetDoodleSegments().GetCount() > 0)
265 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
269 segment
= (DoodleSegment
*)node
->GetData();
270 doc
->GetDoodleSegments().Erase(node
);
273 doc
->UpdateAllViews();
279 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
281 doc
->UpdateAllViews();
288 bool DrawingCommand::Undo(void)
297 doc
->GetDoodleSegments().Append(segment
);
299 doc
->UpdateAllViews();
300 segment
= (DoodleSegment
*) NULL
;
303 doc
->UpdateAllViews();
308 // Cut the last segment
309 if (doc
->GetDoodleSegments().GetCount() > 0)
311 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
312 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
314 doc
->GetDoodleSegments().Erase(node
);
317 doc
->UpdateAllViews();
324 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
326 // Since text windows have their own method for saving to/loading from files,
327 // we override OnSave/OpenDocument instead of Save/LoadObject
328 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
330 TextEditView
*view
= (TextEditView
*)GetFirstView();
332 if (!view
->textsw
->SaveFile(filename
))
336 wxFileName
fn(filename
) ;
337 fn
.MacSetDefaultTypeAndCreator() ;
342 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
344 TextEditView
*view
= (TextEditView
*)GetFirstView();
345 if (!view
->textsw
->LoadFile(filename
))
348 SetFilename(filename
, true);
354 bool TextEditDocument::IsModified(void) const
356 TextEditView
*view
= (TextEditView
*)GetFirstView();
359 return (wxDocument::IsModified() || view
->textsw
->IsModified());
362 return wxDocument::IsModified();
365 void TextEditDocument::Modify(bool mod
)
367 TextEditView
*view
= (TextEditView
*)GetFirstView();
369 wxDocument::Modify(mod
);
371 if (!mod
&& view
&& view
->textsw
)
372 view
->textsw
->DiscardEdits();