]>
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"
27 #if !USE_DOC_VIEW_ARCHITECTURE
28 #error You must set USE_DOC_VIEW_ARCHITECTURE to 1 in wx_setup.h!
34 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
36 DrawingDocument::DrawingDocument(void)
40 DrawingDocument::~DrawingDocument(void)
42 doodleSegments
.DeleteContents(TRUE
);
45 ostream
& DrawingDocument::SaveObject(ostream
& stream
)
47 wxDocument::SaveObject(stream
);
49 stream
<< doodleSegments
.Number() << '\n';
50 wxNode
*node
= doodleSegments
.First();
53 DoodleSegment
*segment
= (DoodleSegment
*)node
->Data();
54 segment
->SaveObject(stream
);
62 istream
& DrawingDocument::LoadObject(istream
& stream
)
64 wxDocument::LoadObject(stream
);
69 for (int i
= 0; i
< n
; i
++)
71 DoodleSegment
*segment
= new DoodleSegment
;
72 segment
->LoadObject(stream
);
73 doodleSegments
.Append(segment
);
79 DoodleSegment::DoodleSegment(void)
83 DoodleSegment::DoodleSegment(DoodleSegment
& seg
)
85 wxNode
*node
= seg
.lines
.First();
88 DoodleLine
*line
= (DoodleLine
*)node
->Data();
89 DoodleLine
*newLine
= new DoodleLine
;
90 newLine
->x1
= line
->x1
;
91 newLine
->y1
= line
->y1
;
92 newLine
->x2
= line
->x2
;
93 newLine
->y2
= line
->y2
;
95 lines
.Append(newLine
);
101 DoodleSegment::~DoodleSegment(void)
103 lines
.DeleteContents(TRUE
);
106 ostream
& DoodleSegment::SaveObject(ostream
& stream
)
108 stream
<< lines
.Number() << '\n';
109 wxNode
*node
= lines
.First();
112 DoodleLine
*line
= (DoodleLine
*)node
->Data();
113 stream
<< line
->x1
<< " " << line
->y1
<< " " << line
->x2
<< " " << line
->y2
<< "\n";
119 istream
& DoodleSegment::LoadObject(istream
& stream
)
124 for (int i
= 0; i
< n
; i
++)
126 DoodleLine
*line
= new DoodleLine
;
127 stream
>> line
->x1
>> line
->y1
>> line
->x2
>> line
->y2
;
133 void DoodleSegment::Draw(wxDC
*dc
)
135 wxNode
*node
= lines
.First();
138 DoodleLine
*line
= (DoodleLine
*)node
->Data();
139 dc
->DrawLine(line
->x1
, line
->y1
, line
->x2
, line
->y2
);
145 * Implementation of drawing command
148 DrawingCommand::DrawingCommand(const wxString
& name
, int command
, DrawingDocument
*ddoc
, DoodleSegment
*seg
):
149 wxCommand(TRUE
, name
)
156 DrawingCommand::~DrawingCommand(void)
162 bool DrawingCommand::Do(void)
168 // Cut the last segment
169 if (doc
->GetDoodleSegments().Number() > 0)
171 wxNode
*node
= doc
->GetDoodleSegments().Last();
175 segment
= (DoodleSegment
*)node
->Data();
179 doc
->UpdateAllViews();
185 doc
->GetDoodleSegments().Append(new DoodleSegment(*segment
));
187 doc
->UpdateAllViews();
194 bool DrawingCommand::Undo(void)
203 doc
->GetDoodleSegments().Append(segment
);
205 doc
->UpdateAllViews();
209 doc
->UpdateAllViews();
214 // Cut the last segment
215 if (doc
->GetDoodleSegments().Number() > 0)
217 wxNode
*node
= doc
->GetDoodleSegments().Last();
218 DoodleSegment
*seg
= (DoodleSegment
*)node
->Data();
223 doc
->UpdateAllViews();
230 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
232 // Since text windows have their own method for saving to/loading from files,
233 // we override OnSave/OpenDocument instead of Save/LoadObject
234 bool TextEditDocument::OnSaveDocument(const wxString
& filename
)
236 TextEditView
*view
= (TextEditView
*)GetFirstView();
238 if (!view
->textsw
->SaveFile(filename
))
244 bool TextEditDocument::OnOpenDocument(const wxString
& filename
)
246 TextEditView
*view
= (TextEditView
*)GetFirstView();
247 if (!view
->textsw
->LoadFile(filename
))
250 SetFilename(filename
, TRUE
);
256 bool TextEditDocument::IsModified(void) const
258 TextEditView
*view
= (TextEditView
*)GetFirstView();
261 return (wxDocument::IsModified() || view
->textsw
->IsModified());
264 return wxDocument::IsModified();
267 void TextEditDocument::Modify(bool mod
)
269 TextEditView
*view
= (TextEditView
*)GetFirstView();
271 wxDocument::Modify(mod
);
273 if (!mod
&& view
&& view
->textsw
)
274 view
->textsw
->DiscardEdits();