]>
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 ostream
& DrawingDocument::SaveObject(ostream
& stream
)
49 wxDocument::SaveObject(stream
);
51 stream
<< doodleSegments
.Number() << '\n';
52 wxNode
*node
= doodleSegments
.First();
55 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
56 segment
->SaveObject(stream
);
64 bool DrawingDocument::SaveObject(wxOutputStream
&stream
)
66 wxDocument::SaveObject(stream
);
68 wxTextOutputStream
text_stream( stream
);
70 wxInt32 n
= doodleSegments
.Number();
71 text_stream
<< n
<< "\n";
73 wxNode
*node
= doodleSegments
.First();
76 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
77 segment
->SaveObject(stream
);
86 #if wxUSE_STD_IOSTREAM
87 istream
& DrawingDocument::LoadObject(istream
& stream
)
89 wxDocument::LoadObject(stream
);
94 for (int i
= 0; i
< n
; i
++)
96 DoodleSegment
*segment
= new DoodleSegment
;
97 segment
->LoadObject(stream
);
98 doodleSegments
.Append(segment
);
104 bool 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 doodleSegments
.Append(segment
);
124 DoodleSegment::DoodleSegment(void)
128 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
130 wxNode
*node
= seg
.lines
.First();
133 DoodleLine
*line
= (DoodleLine
*)node
->Data();
134 DoodleLine
*newLine
= new DoodleLine
;
135 newLine
->x1
= line
->x1
;
136 newLine
->y1
= line
->y1
;
137 newLine
->x2
= line
->x2
;
138 newLine
->y2
= line
->y2
;
140 lines
.Append(newLine
);
146 DoodleSegment::~DoodleSegment(void)
148 lines
.DeleteContents(TRUE
);
151 #if wxUSE_STD_IOSTREAM
152 ostream
& DoodleSegment::SaveObject(ostream
& stream
)
154 stream
<< lines
.Number() << '\n';
155 wxNode
*node
= lines
.First();
158 DoodleLine
*line
= (DoodleLine
*)node
->Data();
159 stream
<< line
->x1
<< " " << line
->y1
<< " " << line
->x2
<< " " << line
->y2
<< "\n";
165 bool DoodleSegment::SaveObject(wxOutputStream
& stream
)
167 wxTextOutputStream
text_stream( stream
);
169 wxInt32 n
= lines
.Number();
170 text_stream
<< n
<< "\n";
172 wxNode
*node
= lines
.First();
175 DoodleLine
*line
= (DoodleLine
*)node
->Data();
176 text_stream
<< line
->x1
<< " " <<
186 #if wxUSE_STD_IOSTREAM
187 istream
& DoodleSegment::LoadObject(istream
& stream
)
192 for (int i
= 0; i
< n
; i
++)
194 DoodleLine
*line
= new DoodleLine
;
195 stream
>> line
->x1
>> line
->y1
>> line
->x2
>> line
->y2
;
201 bool DoodleSegment::LoadObject(wxInputStream
& stream
)
203 wxTextInputStream
text_stream( stream
);
208 for (int i
= 0; i
< n
; i
++)
210 DoodleLine
*line
= new DoodleLine
;
211 text_stream
>> line
->x1
>>
221 void DoodleSegment::Draw(wxDC
*dc
)
223 wxNode
*node
= lines
.First();
226 DoodleLine
*line
= (DoodleLine
*)node
->Data();
227 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
233 * Implementation of drawing command
236 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
237 wxCommand(TRUE
, name
)
244 DrawingCommand::~DrawingCommand(void)
250 bool DrawingCommand::Do(void)
256 // Cut the last segment
257 if (doc
->GetDoodleSegments().Number() > 0)
259 wxNode
*node
= doc
->GetDoodleSegments().Last();
263 segment
= (DoodleSegment
*)node
->Data();
267 doc
->UpdateAllViews();
273 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
275 doc
->UpdateAllViews();
282 bool DrawingCommand::Undo(void)
291 doc
->GetDoodleSegments().Append(segment
);
293 doc
->UpdateAllViews();
294 segment
= (DoodleSegment
*) NULL
;
297 doc
->UpdateAllViews();
302 // Cut the last segment
303 if (doc
->GetDoodleSegments().Number() > 0)
305 wxNode
*node
= doc
->GetDoodleSegments().Last();
306 DoodleSegment
*seg
= (DoodleSegment
*)node
->Data();
311 doc
->UpdateAllViews();
318 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
320 // Since text windows have their own method for saving to/loading from files,
321 // we override OnSave/OpenDocument instead of Save/LoadObject
322 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
324 TextEditView
*view
= (TextEditView
*)GetFirstView();
326 if (!view
->textsw
->SaveFile(filename
))
332 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
334 TextEditView
*view
= (TextEditView
*)GetFirstView();
335 if (!view
->textsw
->LoadFile(filename
))
338 SetFilename(filename
, TRUE
);
344 bool TextEditDocument::IsModified(void) const
346 TextEditView
*view
= (TextEditView
*)GetFirstView();
349 return (wxDocument::IsModified() || view
->textsw
->IsModified());
352 return wxDocument::IsModified();
355 void TextEditDocument::Modify(bool mod
)
357 TextEditView
*view
= (TextEditView
*)GetFirstView();
359 wxDocument::Modify(mod
);
361 if (!mod
&& view
&& view
->textsw
)
362 view
->textsw
->DiscardEdits();