]>
Commit | Line | Data |
---|---|---|
457814b5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
2d1df0fc | 2 | // Name: samples/docview/doc.cpp |
457814b5 JS |
3 | // Purpose: Implements document functionality |
4 | // Author: Julian Smart | |
2d1df0fc | 5 | // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup |
457814b5 JS |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
2d1df0fc VZ |
8 | // Copyright: (c) 1998 Julian Smart |
9 | // (c) 2008 Vadim Zeitlin | |
2f6c54eb | 10 | // Licence: wxWindows license |
457814b5 JS |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
2d1df0fc VZ |
13 | // ---------------------------------------------------------------------------- |
14 | // headers | |
15 | // ---------------------------------------------------------------------------- | |
16 | ||
457814b5 JS |
17 | // For compilers that support precompilation, includes "wx/wx.h". |
18 | #include "wx/wxprec.h" | |
19 | ||
20 | #ifdef __BORLANDC__ | |
2d1df0fc | 21 | #pragma hdrstop |
457814b5 JS |
22 | #endif |
23 | ||
2d1df0fc VZ |
24 | #if !wxUSE_DOC_VIEW_ARCHITECTURE |
25 | #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h! | |
457814b5 | 26 | #endif |
2d1df0fc VZ |
27 | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/wx.h" | |
450a5bdd | 30 | #endif |
457814b5 | 31 | |
76cf603e VZ |
32 | #if wxUSE_STD_IOSTREAM |
33 | #include "wx/ioswrap.h" | |
34 | #else | |
35 | #include "wx/txtstrm.h" | |
36 | #endif | |
f37f49b6 | 37 | #include "wx/wfstream.h" |
76cf603e | 38 | |
457814b5 JS |
39 | #include "doc.h" |
40 | #include "view.h" | |
457814b5 | 41 | |
2d1df0fc VZ |
42 | // ---------------------------------------------------------------------------- |
43 | // DrawingDocument implementation | |
44 | // ---------------------------------------------------------------------------- | |
457814b5 | 45 | |
2d1df0fc VZ |
46 | IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument) |
47 | ||
00a1c49a | 48 | DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& ostream) |
457814b5 | 49 | { |
00a1c49a VZ |
50 | #if wxUSE_STD_IOSTREAM |
51 | DocumentOstream& stream = ostream; | |
52 | #else | |
53 | wxTextOutputStream stream(ostream); | |
54 | #endif | |
55 | ||
56 | wxDocument::SaveObject(ostream); | |
958d3a7e | 57 | |
2d1df0fc VZ |
58 | const wxInt32 count = m_doodleSegments.size(); |
59 | stream << count << '\n'; | |
958d3a7e | 60 | |
2d1df0fc | 61 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 62 | { |
00a1c49a | 63 | m_doodleSegments[n].SaveObject(ostream); |
f6bcfd97 | 64 | stream << '\n'; |
f6bcfd97 | 65 | } |
958d3a7e | 66 | |
00a1c49a | 67 | return ostream; |
457814b5 | 68 | } |
958d3a7e | 69 | |
00a1c49a | 70 | DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream) |
2d1df0fc | 71 | { |
00a1c49a VZ |
72 | #if wxUSE_STD_IOSTREAM |
73 | DocumentIstream& stream = istream; | |
74 | #else | |
75 | wxTextInputStream stream(istream); | |
76 | #endif | |
77 | ||
78 | wxDocument::LoadObject(istream); | |
958d3a7e | 79 | |
2d1df0fc VZ |
80 | wxInt32 count = 0; |
81 | stream >> count; | |
958d3a7e | 82 | |
2d1df0fc | 83 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 84 | { |
2d1df0fc | 85 | DoodleSegment segment; |
00a1c49a | 86 | segment.LoadObject(istream); |
2d1df0fc | 87 | m_doodleSegments.push_back(segment); |
f6bcfd97 | 88 | } |
958d3a7e | 89 | |
00a1c49a | 90 | return istream; |
56d7679d | 91 | } |
56d7679d | 92 | |
2d1df0fc | 93 | void DrawingDocument::DoUpdate() |
457814b5 | 94 | { |
2d1df0fc VZ |
95 | Modify(true); |
96 | UpdateAllViews(); | |
97 | } | |
958d3a7e | 98 | |
2d1df0fc VZ |
99 | void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment) |
100 | { | |
101 | m_doodleSegments.push_back(segment); | |
958d3a7e | 102 | |
2d1df0fc | 103 | DoUpdate(); |
457814b5 | 104 | } |
2d1df0fc VZ |
105 | |
106 | bool DrawingDocument::PopLastSegment(DoodleSegment *segment) | |
56d7679d | 107 | { |
2d1df0fc VZ |
108 | if ( m_doodleSegments.empty() ) |
109 | return false; | |
958d3a7e | 110 | |
2d1df0fc VZ |
111 | if ( segment ) |
112 | *segment = m_doodleSegments.back(); | |
958d3a7e | 113 | |
2d1df0fc | 114 | m_doodleSegments.pop_back(); |
958d3a7e | 115 | |
2d1df0fc | 116 | DoUpdate(); |
958d3a7e | 117 | |
2d1df0fc | 118 | return true; |
56d7679d | 119 | } |
958d3a7e | 120 | |
2d1df0fc VZ |
121 | // ---------------------------------------------------------------------------- |
122 | // DoodleSegment implementation | |
123 | // ---------------------------------------------------------------------------- | |
457814b5 | 124 | |
2d1df0fc | 125 | DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream) |
457814b5 | 126 | { |
56d7679d | 127 | #if wxUSE_STD_IOSTREAM |
2d1df0fc | 128 | DocumentOstream& stream = ostream; |
56d7679d | 129 | #else |
2d1df0fc VZ |
130 | wxTextOutputStream stream(ostream); |
131 | #endif | |
958d3a7e | 132 | |
2d1df0fc VZ |
133 | const wxInt32 count = m_lines.size(); |
134 | stream << count << '\n'; | |
958d3a7e | 135 | |
2d1df0fc | 136 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 137 | { |
2d1df0fc VZ |
138 | const DoodleLine& line = m_lines[n]; |
139 | stream | |
140 | << line.x1 << ' ' | |
141 | << line.y1 << ' ' | |
142 | << line.x2 << ' ' | |
143 | << line.y2 << '\n'; | |
f6bcfd97 | 144 | } |
958d3a7e | 145 | |
00a1c49a | 146 | return ostream; |
56d7679d | 147 | } |
457814b5 | 148 | |
2d1df0fc | 149 | DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream) |
457814b5 | 150 | { |
2d1df0fc VZ |
151 | #if wxUSE_STD_IOSTREAM |
152 | DocumentIstream& stream = istream; | |
56d7679d | 153 | #else |
2d1df0fc VZ |
154 | wxTextInputStream stream(istream); |
155 | #endif | |
958d3a7e | 156 | |
2d1df0fc VZ |
157 | wxInt32 count = 0; |
158 | stream >> count; | |
958d3a7e | 159 | |
2d1df0fc | 160 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 161 | { |
2d1df0fc VZ |
162 | DoodleLine line; |
163 | stream | |
164 | >> line.x1 | |
165 | >> line.y1 | |
166 | >> line.x2 | |
167 | >> line.y2; | |
168 | m_lines.push_back(line); | |
f6bcfd97 | 169 | } |
958d3a7e | 170 | |
00a1c49a | 171 | return istream; |
56d7679d | 172 | } |
457814b5 | 173 | |
2d1df0fc | 174 | // ---------------------------------------------------------------------------- |
828c8f98 | 175 | // wxTextDocument: wxDocument and wxTextCtrl married |
2d1df0fc | 176 | // ---------------------------------------------------------------------------- |
457814b5 | 177 | |
828c8f98 | 178 | IMPLEMENT_CLASS(wxTextDocument, wxDocument) |
457814b5 | 179 | |
112d941f VZ |
180 | bool wxTextDocument::OnCreate(const wxString& path, long flags) |
181 | { | |
182 | if ( !wxDocument::OnCreate(path, flags) ) | |
183 | return false; | |
184 | ||
185 | // subscribe to changes in the text control to update the document state | |
186 | // when it's modified | |
187 | GetTextCtrl()->Connect | |
188 | ( | |
189 | wxEVT_COMMAND_TEXT_UPDATED, | |
190 | wxCommandEventHandler(wxTextDocument::OnTextChange), | |
191 | NULL, | |
192 | this | |
193 | ); | |
194 | ||
195 | return true; | |
196 | } | |
6d3a0824 | 197 | |
457814b5 | 198 | // Since text windows have their own method for saving to/loading from files, |
c9d13e86 | 199 | // we override DoSave/OpenDocument instead of Save/LoadObject |
828c8f98 | 200 | bool wxTextDocument::DoSaveDocument(const wxString& filename) |
457814b5 | 201 | { |
828c8f98 | 202 | return GetTextCtrl()->SaveFile(filename); |
457814b5 JS |
203 | } |
204 | ||
828c8f98 | 205 | bool wxTextDocument::DoOpenDocument(const wxString& filename) |
457814b5 | 206 | { |
6d3a0824 VZ |
207 | if ( !GetTextCtrl()->LoadFile(filename) ) |
208 | return false; | |
209 | ||
210 | // we're not modified by the user yet | |
211 | Modify(false); | |
212 | ||
213 | return true; | |
457814b5 JS |
214 | } |
215 | ||
828c8f98 | 216 | bool wxTextDocument::IsModified() const |
457814b5 | 217 | { |
828c8f98 FM |
218 | wxTextCtrl* wnd = GetTextCtrl(); |
219 | return wxDocument::IsModified() || (wnd && wnd->IsModified()); | |
457814b5 JS |
220 | } |
221 | ||
828c8f98 | 222 | void wxTextDocument::Modify(bool modified) |
457814b5 | 223 | { |
2d1df0fc | 224 | wxDocument::Modify(modified); |
958d3a7e | 225 | |
828c8f98 FM |
226 | wxTextCtrl* wnd = GetTextCtrl(); |
227 | if (wnd && !modified) | |
228 | { | |
229 | wnd->DiscardEdits(); | |
230 | } | |
457814b5 | 231 | } |
6bdf5153 | 232 | |
112d941f VZ |
233 | void wxTextDocument::OnTextChange(wxCommandEvent& event) |
234 | { | |
235 | Modify(true); | |
236 | ||
237 | event.Skip(); | |
238 | } | |
239 | ||
828c8f98 FM |
240 | // ---------------------------------------------------------------------------- |
241 | // TextEditDocument implementation | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument) | |
245 | ||
246 | wxTextCtrl* TextEditDocument::GetTextCtrl() const | |
6bdf5153 | 247 | { |
828c8f98 FM |
248 | wxView* view = GetFirstView(); |
249 | return view ? wxStaticCast(view, TextEditView)->GetText() : NULL; | |
6bdf5153 | 250 | } |
f37f49b6 JS |
251 | |
252 | // ---------------------------------------------------------------------------- | |
253 | // wxImageDocument implementation | |
254 | // ---------------------------------------------------------------------------- | |
255 | ||
256 | ///////////////////////////////////////////////////////////////////////////// | |
257 | // wxImageDocument | |
258 | ||
259 | IMPLEMENT_DYNAMIC_CLASS(wxImageDocument, wxDocument) | |
260 | ||
261 | wxImageDocument::wxImageDocument() : wxDocument() | |
262 | { | |
263 | } | |
264 | ||
265 | wxImageDocument::~wxImageDocument() | |
266 | { | |
267 | } | |
268 | ||
269 | bool wxImageDocument::DeleteContents() | |
270 | { | |
271 | bool ok = wxDocument::DeleteContents(); | |
272 | if (ok && m_image.IsOk()) | |
273 | { | |
274 | m_image.Destroy(); | |
275 | } | |
276 | return ok; | |
277 | } | |
278 | ||
279 | bool wxImageDocument::SaveFile(wxOutputStream* stream, wxBitmapType type) const | |
280 | { | |
281 | return m_image.IsOk() && m_image.SaveFile(*stream, type); | |
282 | } | |
283 | ||
284 | bool wxImageDocument::DoOpenDocument(const wxString& file) | |
285 | { | |
286 | wxFileInputStream stream(file); | |
287 | return stream.IsOk() && DoOpenDocument(&stream); | |
288 | } | |
289 | ||
290 | bool wxImageDocument::DoSaveDocument(const wxString& file) | |
291 | { | |
292 | wxFileOutputStream stream(file); | |
293 | return stream.IsOk() && DoSaveDocument(&stream); | |
294 | } | |
295 | ||
296 | bool wxImageDocument::DoOpenDocument(wxInputStream* stream) | |
297 | { | |
298 | return m_image.LoadFile(*stream); | |
299 | } | |
300 | ||
301 | bool wxImageDocument::DoSaveDocument(wxOutputStream* stream) const | |
302 | { | |
303 | return m_image.IsOk() && SaveFile(stream, m_image.GetType()); | |
304 | } | |
305 |