]>
git.saurik.com Git - wxWidgets.git/blob - samples/docview/doc.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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 #if !wxUSE_DOC_VIEW_ARCHITECTURE
29 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
35 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
37 DrawingDocument::DrawingDocument(void)
41 DrawingDocument::~DrawingDocument(void)
43 doodleSegments
.DeleteContents(TRUE
);
46 #if wxUSE_STD_IOSTREAM
47 wxSTD ostream
& DrawingDocument::SaveObject(wxSTD ostream
& stream
)
49 wxDocument::SaveObject(stream
);
51 wxInt32 n
= doodleSegments
.Number();
54 wxNode
*node
= doodleSegments
.First();
57 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
58 segment
->SaveObject(stream
);
67 wxOutputStream
& DrawingDocument::SaveObject(wxOutputStream
& stream
)
69 wxDocument::SaveObject(stream
);
71 wxTextOutputStream
text_stream( stream
);
73 wxInt32 n
= doodleSegments
.Number();
74 text_stream
<< n
<< '\n';
76 wxNode
*node
= doodleSegments
.First();
79 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
80 segment
->SaveObject(stream
);
90 #if wxUSE_STD_IOSTREAM
91 wxSTD istream
& DrawingDocument::LoadObject(wxSTD istream
& stream
)
93 wxDocument::LoadObject(stream
);
98 for (int i
= 0; i
< n
; i
++)
100 DoodleSegment
*segment
= new DoodleSegment
;
101 segment
->LoadObject(stream
);
102 doodleSegments
.Append(segment
);
108 wxInputStream
& DrawingDocument::LoadObject(wxInputStream
& stream
)
110 wxDocument::LoadObject(stream
);
112 wxTextInputStream
text_stream( stream
);
117 for (int i
= 0; i
< n
; i
++)
119 DoodleSegment
*segment
= new DoodleSegment
;
120 segment
->LoadObject(stream
);
121 doodleSegments
.Append(segment
);
128 DoodleSegment::DoodleSegment(void)
132 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
134 wxNode
*node
= seg
.lines
.First();
137 DoodleLine
*line
= (DoodleLine
*)node
->Data();
138 DoodleLine
*newLine
= new DoodleLine
;
139 newLine
->x1
= line
->x1
;
140 newLine
->y1
= line
->y1
;
141 newLine
->x2
= line
->x2
;
142 newLine
->y2
= line
->y2
;
144 lines
.Append(newLine
);
150 DoodleSegment::~DoodleSegment(void)
152 lines
.DeleteContents(TRUE
);
155 #if wxUSE_STD_IOSTREAM
156 wxSTD ostream
& DoodleSegment::SaveObject(wxSTD ostream
& stream
)
158 wxInt32 n
= lines
.Number();
161 wxNode
*node
= lines
.First();
164 DoodleLine
*line
= (DoodleLine
*)node
->Data();
165 stream
<< line
->x1
<< " " <<
175 wxOutputStream
&DoodleSegment::SaveObject(wxOutputStream
& stream
)
177 wxTextOutputStream
text_stream( stream
);
179 wxInt32 n
= lines
.Number();
180 text_stream
<< n
<< '\n';
182 wxNode
*node
= lines
.First();
185 DoodleLine
*line
= (DoodleLine
*)node
->Data();
186 text_stream
<< line
->x1
<< " " <<
197 #if wxUSE_STD_IOSTREAM
198 wxSTD istream
& DoodleSegment::LoadObject(wxSTD istream
& stream
)
203 for (int i
= 0; i
< n
; i
++)
205 DoodleLine
*line
= new DoodleLine
;
206 stream
>> line
->x1
>>
216 wxInputStream
&DoodleSegment::LoadObject(wxInputStream
& stream
)
218 wxTextInputStream
text_stream( stream
);
223 for (int i
= 0; i
< n
; i
++)
225 DoodleLine
*line
= new DoodleLine
;
226 text_stream
>> line
->x1
>>
237 void DoodleSegment::Draw(wxDC
*dc
)
239 wxNode
*node
= lines
.First();
242 DoodleLine
*line
= (DoodleLine
*)node
->Data();
243 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
249 * Implementation of drawing command
252 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
253 wxCommand(TRUE
, name
)
260 DrawingCommand::~DrawingCommand(void)
266 bool DrawingCommand::Do(void)
272 // Cut the last segment
273 if (doc
->GetDoodleSegments().Number() > 0)
275 wxNode
*node
= doc
->GetDoodleSegments().Last();
279 segment
= (DoodleSegment
*)node
->Data();
283 doc
->UpdateAllViews();
289 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
291 doc
->UpdateAllViews();
298 bool DrawingCommand::Undo(void)
307 doc
->GetDoodleSegments().Append(segment
);
309 doc
->UpdateAllViews();
310 segment
= (DoodleSegment
*) NULL
;
313 doc
->UpdateAllViews();
318 // Cut the last segment
319 if (doc
->GetDoodleSegments().Number() > 0)
321 wxNode
*node
= doc
->GetDoodleSegments().Last();
322 DoodleSegment
*seg
= (DoodleSegment
*)node
->Data();
327 doc
->UpdateAllViews();
334 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
336 // Since text windows have their own method for saving to/loading from files,
337 // we override OnSave/OpenDocument instead of Save/LoadObject
338 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
340 TextEditView
*view
= (TextEditView
*)GetFirstView();
342 if (!view
->textsw
->SaveFile(filename
))
348 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
350 TextEditView
*view
= (TextEditView
*)GetFirstView();
351 if (!view
->textsw
->LoadFile(filename
))
354 SetFilename(filename
, TRUE
);
360 bool TextEditDocument::IsModified(void) const
362 TextEditView
*view
= (TextEditView
*)GetFirstView();
365 return (wxDocument::IsModified() || view
->textsw
->IsModified());
368 return wxDocument::IsModified();
371 void TextEditDocument::Modify(bool mod
)
373 TextEditView
*view
= (TextEditView
*)GetFirstView();
375 wxDocument::Modify(mod
);
377 if (!mod
&& view
&& view
->textsw
)
378 view
->textsw
->DiscardEdits();