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)
43 DrawingDocument::~DrawingDocument(void)
45 WX_CLEAR_LIST(wxList
, doodleSegments
);
48 #if wxUSE_STD_IOSTREAM
49 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
51 wxDocument::SaveObject(stream
);
53 wxInt32 n
= doodleSegments
.GetCount();
56 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
59 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
60 segment
->SaveObject(stream
);
63 node
= node
->GetNext();
69 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
71 wxDocument::SaveObject(stream
);
73 wxTextOutputStream
text_stream( stream
);
75 wxInt32 n
= doodleSegments
.GetCount();
76 text_stream
<< n
<< '\n';
78 wxList::compatibility_iterator node
= doodleSegments
.GetFirst();
81 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
82 segment
->SaveObject(stream
);
85 node
= node
->GetNext();
92 #if wxUSE_STD_IOSTREAM
93 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
95 wxDocument::LoadObject(stream
);
100 for (int i
= 0; i
< n
; i
++)
102 DoodleSegment
*segment
= new DoodleSegment
;
103 segment
->LoadObject(stream
);
104 doodleSegments
.Append(segment
);
110 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
112 wxDocument::LoadObject(stream
);
114 wxTextInputStream
text_stream( stream
);
119 for (int i
= 0; i
< n
; i
++)
121 DoodleSegment
*segment
= new DoodleSegment
;
122 segment
->LoadObject(stream
);
123 doodleSegments
.Append(segment
);
130 DoodleSegment::DoodleSegment(void)
134 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
136 wxList::compatibility_iterator node
= seg
.lines
.GetFirst();
139 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
140 DoodleLine
*newLine
= new DoodleLine
;
141 newLine
->x1
= line
->x1
;
142 newLine
->y1
= line
->y1
;
143 newLine
->x2
= line
->x2
;
144 newLine
->y2
= line
->y2
;
146 lines
.Append(newLine
);
148 node
= node
->GetNext();
152 DoodleSegment::~DoodleSegment(void)
154 WX_CLEAR_LIST(wxList
, lines
);
157 #if wxUSE_STD_IOSTREAM
158 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
160 wxInt32 n
= lines
.GetCount();
163 wxList::compatibility_iterator node
= lines
.GetFirst();
166 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
167 stream
<< line
->x1
<< " " <<
171 node
= node
->GetNext();
177 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
179 wxTextOutputStream
text_stream( stream
);
181 wxInt32 n
= lines
.GetCount();
182 text_stream
<< n
<< _T('\n');
184 wxList::compatibility_iterator node
= lines
.GetFirst();
187 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
188 text_stream
<< line
->x1
<< _T(" ") <<
189 line
->y1
<< _T(" ") <<
190 line
->x2
<< _T(" ") <<
191 line
->y2
<< _T("\n");
192 node
= node
->GetNext();
199 #if wxUSE_STD_IOSTREAM
200 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
205 for (int i
= 0; i
< n
; i
++)
207 DoodleLine
*line
= new DoodleLine
;
208 stream
>> line
->x1
>>
218 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
220 wxTextInputStream
text_stream( stream
);
225 for (int i
= 0; i
< n
; i
++)
227 DoodleLine
*line
= new DoodleLine
;
228 text_stream
>> line
->x1
>>
239 void DoodleSegment::Draw(wxDC
*dc
)
241 wxList::compatibility_iterator node
= lines
.GetFirst();
244 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
245 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
246 node
= node
->GetNext();
251 * Implementation of drawing command
254 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
255 wxCommand(TRUE
, name
)
262 DrawingCommand::~DrawingCommand(void)
268 bool DrawingCommand::Do(void)
274 // Cut the last segment
275 if (doc
->GetDoodleSegments().GetCount() > 0)
277 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
281 segment
= (DoodleSegment
*)node
->GetData();
282 doc
->GetDoodleSegments().Erase(node
);
285 doc
->UpdateAllViews();
291 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
293 doc
->UpdateAllViews();
300 bool DrawingCommand::Undo(void)
309 doc
->GetDoodleSegments().Append(segment
);
311 doc
->UpdateAllViews();
312 segment
= (DoodleSegment
*) NULL
;
315 doc
->UpdateAllViews();
320 // Cut the last segment
321 if (doc
->GetDoodleSegments().GetCount() > 0)
323 wxList::compatibility_iterator node
= doc
->GetDoodleSegments().GetLast();
324 DoodleSegment
*seg
= (DoodleSegment
*)node
->GetData();
326 doc
->GetDoodleSegments().Erase(node
);
329 doc
->UpdateAllViews();
336 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
338 // Since text windows have their own method for saving to/loading from files,
339 // we override OnSave/OpenDocument instead of Save/LoadObject
340 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
342 TextEditView
*view
= (TextEditView
*)GetFirstView();
344 if (!view
->textsw
->SaveFile(filename
))
348 wxFileName
fn(filename
) ;
349 fn
.MacSetDefaultTypeAndCreator() ;
354 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
356 TextEditView
*view
= (TextEditView
*)GetFirstView();
357 if (!view
->textsw
->LoadFile(filename
))
360 SetFilename(filename
, TRUE
);
366 bool TextEditDocument::IsModified(void) const
368 TextEditView
*view
= (TextEditView
*)GetFirstView();
371 return (wxDocument::IsModified() || view
->textsw
->IsModified());
374 return wxDocument::IsModified();
377 void TextEditDocument::Modify(bool mod
)
379 TextEditView
*view
= (TextEditView
*)GetFirstView();
381 wxDocument::Modify(mod
);
383 if (!mod
&& view
&& view
->textsw
)
384 view
->textsw
->DiscardEdits();