merged docvwmdi sample into docview one to avoid having 2 almost identical samples...
[wxWidgets.git] / samples / docview / doc.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/docview/doc.cpp
3 // Purpose: Implements document functionality
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
19
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
23
24 #if !wxUSE_DOC_VIEW_ARCHITECTURE
25 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
26 #endif
27
28 #ifndef WX_PRECOMP
29 #include "wx/wx.h"
30 #endif
31
32 #if wxUSE_STD_IOSTREAM
33 #include "wx/ioswrap.h"
34 #else
35 #include "wx/txtstrm.h"
36 #endif
37
38 #include "doc.h"
39 #include "view.h"
40
41 // ----------------------------------------------------------------------------
42 // DrawingDocument implementation
43 // ----------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
46
47 DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& stream)
48 {
49 wxDocument::SaveObject(stream);
50
51 const wxInt32 count = m_doodleSegments.size();
52 stream << count << '\n';
53
54 for ( int n = 0; n < count; n++ )
55 {
56 m_doodleSegments[n].SaveObject(stream);
57 stream << '\n';
58 }
59
60 return stream;
61 }
62
63 DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& stream)
64 {
65 wxDocument::LoadObject(stream);
66
67 wxInt32 count = 0;
68 stream >> count;
69
70 for ( int n = 0; n < count; n++ )
71 {
72 DoodleSegment segment;
73 segment.LoadObject(stream);
74 m_doodleSegments.push_back(segment);
75 }
76
77 return stream;
78 }
79
80 void DrawingDocument::DoUpdate()
81 {
82 Modify(true);
83 UpdateAllViews();
84 }
85
86 void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
87 {
88 m_doodleSegments.push_back(segment);
89
90 DoUpdate();
91 }
92
93 bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
94 {
95 if ( m_doodleSegments.empty() )
96 return false;
97
98 if ( segment )
99 *segment = m_doodleSegments.back();
100
101 m_doodleSegments.pop_back();
102
103 DoUpdate();
104
105 return true;
106 }
107
108 // ----------------------------------------------------------------------------
109 // DoodleSegment implementation
110 // ----------------------------------------------------------------------------
111
112 DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
113 {
114 #if wxUSE_STD_IOSTREAM
115 DocumentOstream& stream = ostream;
116 #else
117 wxTextOutputStream stream(ostream);
118 #endif
119
120 const wxInt32 count = m_lines.size();
121 stream << count << '\n';
122
123 for ( int n = 0; n < count; n++ )
124 {
125 const DoodleLine& line = m_lines[n];
126 stream
127 << line.x1 << ' '
128 << line.y1 << ' '
129 << line.x2 << ' '
130 << line.y2 << '\n';
131 }
132
133 return stream;
134 }
135
136 DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
137 {
138 #if wxUSE_STD_IOSTREAM
139 DocumentIstream& stream = istream;
140 #else
141 wxTextInputStream stream(istream);
142 #endif
143
144 wxInt32 count = 0;
145 stream >> count;
146
147 for ( int n = 0; n < count; n++ )
148 {
149 DoodleLine line;
150 stream
151 >> line.x1
152 >> line.y1
153 >> line.x2
154 >> line.y2;
155 m_lines.push_back(line);
156 }
157
158 return stream;
159 }
160
161 // ----------------------------------------------------------------------------
162 // TextEditDocument implementation
163 // ----------------------------------------------------------------------------
164
165 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
166
167 // Since text windows have their own method for saving to/loading from files,
168 // we override DoSave/OpenDocument instead of Save/LoadObject
169 bool TextEditDocument::DoSaveDocument(const wxString& filename)
170 {
171 return GetFirstView()->GetText()->SaveFile(filename);
172 }
173
174 bool TextEditDocument::DoOpenDocument(const wxString& filename)
175 {
176 return GetFirstView()->GetText()->LoadFile(filename);
177 }
178
179 bool TextEditDocument::IsModified() const
180 {
181 TextEditView* view = GetFirstView();
182 return wxDocument::IsModified() || (view && view->GetText()->IsModified());
183 }
184
185 void TextEditDocument::Modify(bool modified)
186 {
187 TextEditView* view = GetFirstView();
188
189 wxDocument::Modify(modified);
190
191 if ( !modified && view && view->GetText() )
192 view->GetText()->DiscardEdits();
193 }
194
195 TextEditView* TextEditDocument::GetFirstView() const
196 {
197 wxView* view = wxDocument::GetFirstView();
198 return view ? wxStaticCast(view, TextEditView) : NULL;
199 }
200