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 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
27 #if wxUSE_STD_IOSTREAM
28 #include "wx/ioswrap.h"
30 #include "wx/txtstrm.h"
36 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
38 DrawingDocument::~DrawingDocument(void)
40 WX_CLEAR_LIST(wxList
, m_doodleSegments
)
43 #if wxUSE_STD_IOSTREAM
44 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
46 wxDocument::SaveObject(stream
);
48 wxInt32 n
= m_doodleSegments
.GetCount();
49 stream
<< n
<< wxT('\n');
51 wxList::compatibility_iterator node
= m_doodleSegments
.GetFirst();
54 DoodleSegment
*segment
= (DoodleSegment
*)node
->GetData();
55 segment
->SaveObject(stream
);
58 node
= node
->GetNext();
63 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
65 wxDocument::SaveObject(stream
);
67 wxTextOutputStream
text_stream( stream
);
69 wxInt32 n
= m_doodleSegments
.GetCount();
70 text_stream
<< n
<< wxT('\n');
72 wxList::compatibility_iterator node
= m_doodleSegments
.GetFirst();
75 DoodleSegment
* segment
= (DoodleSegment
*)node
->GetData();
76 segment
->SaveObject(stream
);
77 text_stream
<< wxT('\n');
79 node
= node
->GetNext();
86 #if wxUSE_STD_IOSTREAM
87 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
89 wxDocument::LoadObject(stream
);
94 for (int i
= 0; i
< n
; i
++)
96 DoodleSegment
*segment
= new DoodleSegment
;
97 segment
->LoadObject(stream
);
98 m_doodleSegments
.Append(segment
);
104 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
106 wxDocument::LoadObject(stream
);
108 wxTextInputStream
text_stream( stream
);
113 for (int i
= 0; i
< n
; i
++)
115 DoodleSegment
* segment
= new DoodleSegment
;
116 segment
->LoadObject(stream
);
117 m_doodleSegments
.Append(segment
);
124 DoodleSegment::DoodleSegment(const DoodleSegment
& seg
) : wxObject()
126 wxList::compatibility_iterator node
= seg
.m_lines
.GetFirst();
129 DoodleLine
* line
= (DoodleLine
*)node
->GetData();
130 DoodleLine
* newLine
= new DoodleLine
;
131 newLine
->x1
= line
->x1
;
132 newLine
->y1
= line
->y1
;
133 newLine
->x2
= line
->x2
;
134 newLine
->y2
= line
->y2
;
136 m_lines
.Append(newLine
);
138 node
= node
->GetNext();
142 DoodleSegment::~DoodleSegment(void)
144 WX_CLEAR_LIST(wxList
, m_lines
)
147 #if wxUSE_STD_IOSTREAM
148 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
150 wxInt32 n
= m_lines
.GetCount();
151 stream
<< n
<< wxT('\n');
153 wxList::compatibility_iterator node
= m_lines
.GetFirst();
156 DoodleLine
*line
= (DoodleLine
*)node
->GetData();
157 stream
<< line
->x1
<< wxT(" ") <<
158 line
->y1
<< wxT(" ") <<
159 line
->x2
<< wxT(" ") <<
160 line
->y2
<< wxT("\n");
161 node
= node
->GetNext();
167 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
169 wxTextOutputStream
text_stream( stream
);
171 wxInt32 n
= m_lines
.GetCount();
172 text_stream
<< n
<< wxT('\n');
174 wxList::compatibility_iterator node
= m_lines
.GetFirst();
177 DoodleLine
* line
= (DoodleLine
*)node
->GetData();
178 text_stream
<< line
->x1
<< wxT(" ") <<
179 line
->y1
<< wxT(" ") <<
180 line
->x2
<< wxT(" ") <<
181 line
->y2
<< wxT("\n");
182 node
= node
->GetNext();
189 #if wxUSE_STD_IOSTREAM
190 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
195 for (int i
= 0; i
< n
; i
++)
197 DoodleLine
*line
= new DoodleLine
;
198 stream
>> line
->x1
>>
202 m_lines
.Append(line
);
208 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
210 wxTextInputStream
text_stream( stream
);
215 for (int i
= 0; i
< n
; i
++)
217 DoodleLine
* line
= new DoodleLine
;
218 text_stream
>> line
->x1
>>
222 m_lines
.Append(line
);
228 void DoodleSegment::Draw(wxDC
*dc
)
230 wxList::compatibility_iterator node
= m_lines
.GetFirst();
233 DoodleLine
* line
= (DoodleLine
*)node
->GetData();
234 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
235 node
= node
->GetNext();
240 * Implementation of drawing command
243 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
* doc
, DoodleSegment
* seg
) :
244 wxCommand(true, name
)
251 DrawingCommand::~DrawingCommand(void)
257 bool DrawingCommand::Do(void)
262 // Cut the last segment
263 if (m_doc
->GetDoodleSegments().GetCount() > 0)
265 wxList::compatibility_iterator node
= m_doc
->GetDoodleSegments().GetLast();
269 m_segment
= (DoodleSegment
*)node
->GetData();
270 m_doc
->GetDoodleSegments().Erase(node
);
273 m_doc
->UpdateAllViews();
277 m_doc
->GetDoodleSegments().Append(new DoodleSegment(*m_segment
));
279 m_doc
->UpdateAllViews();
285 bool DrawingCommand::Undo(void)
294 m_doc
->GetDoodleSegments().Append(m_segment
);
296 m_doc
->UpdateAllViews();
300 m_doc
->UpdateAllViews();
305 // Cut the last segment
306 if (m_doc
->GetDoodleSegments().GetCount() > 0)
308 wxList::compatibility_iterator node
= m_doc
->GetDoodleSegments().GetLast();
309 DoodleSegment
* seg
= (DoodleSegment
*)node
->GetData();
311 m_doc
->GetDoodleSegments().Erase(node
);
314 m_doc
->UpdateAllViews();
321 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
323 // Since text windows have their own method for saving to/loading from files,
324 // we override OnSave/OpenDocument instead of Save/LoadObject
325 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
327 TextEditView
* view
= GetFirstView();
329 if (!view
->m_textsw
->SaveFile(filename
))
335 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
337 TextEditView
*view
= GetFirstView();
338 if (!view
->m_textsw
->LoadFile(filename
))
341 SetFilename(filename
, true);
347 bool TextEditDocument::IsModified(void) const
349 TextEditView
* view
= GetFirstView();
350 return (wxDocument::IsModified() || (view
&& view
->m_textsw
->IsModified()));
353 void TextEditDocument::Modify(bool mod
)
355 TextEditView
* view
= GetFirstView();
357 wxDocument::Modify(mod
);
359 if ((!mod
) && view
&& view
->m_textsw
)
361 view
->m_textsw
->DiscardEdits();
365 TextEditView
* TextEditDocument::GetFirstView() const
367 wxView
* view
= wxDocument::GetFirstView();
368 return view
? wxStaticCast(view
, TextEditView
) : NULL
;