compilation fix for wxUSE_STD_IOSTREAM==0
[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& ostream)
48 {
49 #if wxUSE_STD_IOSTREAM
50 DocumentOstream& stream = ostream;
51 #else
52 wxTextOutputStream stream(ostream);
53 #endif
54
55 wxDocument::SaveObject(ostream);
56
57 const wxInt32 count = m_doodleSegments.size();
58 stream << count << '\n';
59
60 for ( int n = 0; n < count; n++ )
61 {
62 m_doodleSegments[n].SaveObject(ostream);
63 stream << '\n';
64 }
65
66 return ostream;
67 }
68
69 DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream)
70 {
71 #if wxUSE_STD_IOSTREAM
72 DocumentIstream& stream = istream;
73 #else
74 wxTextInputStream stream(istream);
75 #endif
76
77 wxDocument::LoadObject(istream);
78
79 wxInt32 count = 0;
80 stream >> count;
81
82 for ( int n = 0; n < count; n++ )
83 {
84 DoodleSegment segment;
85 segment.LoadObject(istream);
86 m_doodleSegments.push_back(segment);
87 }
88
89 return istream;
90 }
91
92 void DrawingDocument::DoUpdate()
93 {
94 Modify(true);
95 UpdateAllViews();
96 }
97
98 void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
99 {
100 m_doodleSegments.push_back(segment);
101
102 DoUpdate();
103 }
104
105 bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
106 {
107 if ( m_doodleSegments.empty() )
108 return false;
109
110 if ( segment )
111 *segment = m_doodleSegments.back();
112
113 m_doodleSegments.pop_back();
114
115 DoUpdate();
116
117 return true;
118 }
119
120 // ----------------------------------------------------------------------------
121 // DoodleSegment implementation
122 // ----------------------------------------------------------------------------
123
124 DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
125 {
126 #if wxUSE_STD_IOSTREAM
127 DocumentOstream& stream = ostream;
128 #else
129 wxTextOutputStream stream(ostream);
130 #endif
131
132 const wxInt32 count = m_lines.size();
133 stream << count << '\n';
134
135 for ( int n = 0; n < count; n++ )
136 {
137 const DoodleLine& line = m_lines[n];
138 stream
139 << line.x1 << ' '
140 << line.y1 << ' '
141 << line.x2 << ' '
142 << line.y2 << '\n';
143 }
144
145 return ostream;
146 }
147
148 DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
149 {
150 #if wxUSE_STD_IOSTREAM
151 DocumentIstream& stream = istream;
152 #else
153 wxTextInputStream stream(istream);
154 #endif
155
156 wxInt32 count = 0;
157 stream >> count;
158
159 for ( int n = 0; n < count; n++ )
160 {
161 DoodleLine line;
162 stream
163 >> line.x1
164 >> line.y1
165 >> line.x2
166 >> line.y2;
167 m_lines.push_back(line);
168 }
169
170 return istream;
171 }
172
173 // ----------------------------------------------------------------------------
174 // TextEditDocument implementation
175 // ----------------------------------------------------------------------------
176
177 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
178
179 // Since text windows have their own method for saving to/loading from files,
180 // we override DoSave/OpenDocument instead of Save/LoadObject
181 bool TextEditDocument::DoSaveDocument(const wxString& filename)
182 {
183 return GetFirstView()->GetText()->SaveFile(filename);
184 }
185
186 bool TextEditDocument::DoOpenDocument(const wxString& filename)
187 {
188 return GetFirstView()->GetText()->LoadFile(filename);
189 }
190
191 bool TextEditDocument::IsModified() const
192 {
193 TextEditView* view = GetFirstView();
194 return wxDocument::IsModified() || (view && view->GetText()->IsModified());
195 }
196
197 void TextEditDocument::Modify(bool modified)
198 {
199 TextEditView* view = GetFirstView();
200
201 wxDocument::Modify(modified);
202
203 if ( !modified && view && view->GetText() )
204 view->GetText()->DiscardEdits();
205 }
206
207 TextEditView* TextEditDocument::GetFirstView() const
208 {
209 wxView* view = wxDocument::GetFirstView();
210 return view ? wxStaticCast(view, TextEditView) : NULL;
211 }
212