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
, m_doodleSegments
)
45 #if wxUSE_STD_IOSTREAM
46 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
48 wxDocument::SaveObject(stream
);
50 wxInt32 n
= m_doodleSegments
.GetCount();
53 wxList::compatibility_iterator node
= m_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
= m_doodleSegments
.GetCount();
73 text_stream
<< n
<< '\n';
75 wxList::compatibility_iterator node
= m_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 m_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 m_doodleSegments
.Append(segment
);
127 DoodleSegment::DoodleSegment(const DoodleSegment
& seg
):wxObject()
129 wxList::compatibility_iterator node
= seg
.m_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 m_lines
.Append(newLine
);
141 node
= node
->GetNext();
145 DoodleSegment::~DoodleSegment(void)
147 WX_CLEAR_LIST(wxList
, m_lines
)
150 #if wxUSE_STD_IOSTREAM
151 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
153 wxInt32 n
= m_lines
.GetCount();
156 wxList::compatibility_iterator node
= m_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
= m_lines
.GetCount();
175 text_stream
<< n
<< wxT("\n");
177 wxList::compatibility_iterator node
= m_lines
.GetFirst();
180 DoodleLine
* line
= (DoodleLine
*)node
->GetData();
181 text_stream
<< line
->x1
<< wxT(" ") <<
182 line
->y1
<< wxT(" ") <<
183 line
->x2
<< wxT(" ") <<
184 line
->y2
<< wxT("\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
>>
205 m_lines
.Append(line
);
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
>>
225 m_lines
.Append(line
);
232 void DoodleSegment::Draw(wxDC
*dc
)
234 wxList::compatibility_iterator node
= m_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
* doc
, DoodleSegment
* seg
) :
248 wxCommand(true, name
)
255 DrawingCommand::~DrawingCommand(void)
261 bool DrawingCommand::Do(void)
267 // Cut the last segment
268 if (m_doc
->GetDoodleSegments().GetCount() > 0)
270 wxList::compatibility_iterator node
= m_doc
->GetDoodleSegments().GetLast();
274 m_segment
= (DoodleSegment
*)node
->GetData();
275 m_doc
->GetDoodleSegments().Erase(node
);
278 m_doc
->UpdateAllViews();
284 m_doc
->GetDoodleSegments().Append(new DoodleSegment(*m_segment
));
286 m_doc
->UpdateAllViews();
293 bool DrawingCommand::Undo(void)
302 m_doc
->GetDoodleSegments().Append(m_segment
);
304 m_doc
->UpdateAllViews();
308 m_doc
->UpdateAllViews();
313 // Cut the last segment
314 if (m_doc
->GetDoodleSegments().GetCount() > 0)
316 wxList::compatibility_iterator node
= m_doc
->GetDoodleSegments().GetLast();
317 DoodleSegment
* seg
= (DoodleSegment
*)node
->GetData();
319 m_doc
->GetDoodleSegments().Erase(node
);
322 m_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
= GetFirstView();
337 if (!view
->m_textsw
->SaveFile(filename
))
341 wxFileName
fn(filename
) ;
342 fn
.MacSetDefaultTypeAndCreator() ;
347 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
349 TextEditView
* view
= GetFirstView();
350 if (!view
->m_textsw
->LoadFile(filename
))
353 SetFilename(filename
, true);
359 bool TextEditDocument::IsModified(void) const
361 TextEditView
* view
= GetFirstView();
362 return (wxDocument::IsModified() || (view
&& view
->m_textsw
->IsModified()));
365 void TextEditDocument::Modify(bool mod
)
367 TextEditView
* view
= GetFirstView();
369 wxDocument::Modify(mod
);
371 if (!mod
&& view
&& view
->m_textsw
)
372 view
->m_textsw
->DiscardEdits();
375 TextEditView
* TextEditDocument::GetFirstView() const
377 wxView
* view
= wxDocument::GetFirstView();
378 return view
? wxStaticCast(view
, TextEditView
) : NULL
;