]>
Commit | Line | Data |
---|---|---|
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 licence | |
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 | #include "wx/wfstream.h" | |
38 | ||
39 | #include "doc.h" | |
40 | #include "view.h" | |
41 | ||
42 | // ---------------------------------------------------------------------------- | |
43 | // DrawingDocument implementation | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument) | |
47 | ||
48 | DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& ostream) | |
49 | { | |
50 | #if wxUSE_STD_IOSTREAM | |
51 | DocumentOstream& stream = ostream; | |
52 | #else | |
53 | wxTextOutputStream stream(ostream); | |
54 | #endif | |
55 | ||
56 | wxDocument::SaveObject(ostream); | |
57 | ||
58 | const wxInt32 count = m_doodleSegments.size(); | |
59 | stream << count << '\n'; | |
60 | ||
61 | for ( int n = 0; n < count; n++ ) | |
62 | { | |
63 | m_doodleSegments[n].SaveObject(ostream); | |
64 | stream << '\n'; | |
65 | } | |
66 | ||
67 | return ostream; | |
68 | } | |
69 | ||
70 | DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream) | |
71 | { | |
72 | #if wxUSE_STD_IOSTREAM | |
73 | DocumentIstream& stream = istream; | |
74 | #else | |
75 | wxTextInputStream stream(istream); | |
76 | #endif | |
77 | ||
78 | wxDocument::LoadObject(istream); | |
79 | ||
80 | wxInt32 count = 0; | |
81 | stream >> count; | |
82 | if ( count < 0 ) | |
83 | { | |
84 | wxLogWarning("Drawing document corrupted: invalid segments count."); | |
85 | #if wxUSE_STD_IOSTREAM | |
86 | istream.clear(std::ios::badbit); | |
87 | #else | |
88 | istream.Reset(wxSTREAM_READ_ERROR); | |
89 | #endif | |
90 | return istream; | |
91 | } | |
92 | ||
93 | for ( int n = 0; n < count; n++ ) | |
94 | { | |
95 | DoodleSegment segment; | |
96 | segment.LoadObject(istream); | |
97 | m_doodleSegments.push_back(segment); | |
98 | } | |
99 | ||
100 | return istream; | |
101 | } | |
102 | ||
103 | void DrawingDocument::DoUpdate() | |
104 | { | |
105 | Modify(true); | |
106 | UpdateAllViews(); | |
107 | } | |
108 | ||
109 | void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment) | |
110 | { | |
111 | m_doodleSegments.push_back(segment); | |
112 | ||
113 | DoUpdate(); | |
114 | } | |
115 | ||
116 | bool DrawingDocument::PopLastSegment(DoodleSegment *segment) | |
117 | { | |
118 | if ( m_doodleSegments.empty() ) | |
119 | return false; | |
120 | ||
121 | if ( segment ) | |
122 | *segment = m_doodleSegments.back(); | |
123 | ||
124 | m_doodleSegments.pop_back(); | |
125 | ||
126 | DoUpdate(); | |
127 | ||
128 | return true; | |
129 | } | |
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // DoodleSegment implementation | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
135 | DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream) | |
136 | { | |
137 | #if wxUSE_STD_IOSTREAM | |
138 | DocumentOstream& stream = ostream; | |
139 | #else | |
140 | wxTextOutputStream stream(ostream); | |
141 | #endif | |
142 | ||
143 | const wxInt32 count = m_lines.size(); | |
144 | stream << count << '\n'; | |
145 | ||
146 | for ( int n = 0; n < count; n++ ) | |
147 | { | |
148 | const DoodleLine& line = m_lines[n]; | |
149 | stream | |
150 | << line.x1 << ' ' | |
151 | << line.y1 << ' ' | |
152 | << line.x2 << ' ' | |
153 | << line.y2 << '\n'; | |
154 | } | |
155 | ||
156 | return ostream; | |
157 | } | |
158 | ||
159 | DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream) | |
160 | { | |
161 | #if wxUSE_STD_IOSTREAM | |
162 | DocumentIstream& stream = istream; | |
163 | #else | |
164 | wxTextInputStream stream(istream); | |
165 | #endif | |
166 | ||
167 | wxInt32 count = 0; | |
168 | stream >> count; | |
169 | ||
170 | for ( int n = 0; n < count; n++ ) | |
171 | { | |
172 | DoodleLine line; | |
173 | stream | |
174 | >> line.x1 | |
175 | >> line.y1 | |
176 | >> line.x2 | |
177 | >> line.y2; | |
178 | m_lines.push_back(line); | |
179 | } | |
180 | ||
181 | return istream; | |
182 | } | |
183 | ||
184 | // ---------------------------------------------------------------------------- | |
185 | // wxTextDocument: wxDocument and wxTextCtrl married | |
186 | // ---------------------------------------------------------------------------- | |
187 | ||
188 | IMPLEMENT_CLASS(wxTextDocument, wxDocument) | |
189 | ||
190 | bool wxTextDocument::OnCreate(const wxString& path, long flags) | |
191 | { | |
192 | if ( !wxDocument::OnCreate(path, flags) ) | |
193 | return false; | |
194 | ||
195 | // subscribe to changes in the text control to update the document state | |
196 | // when it's modified | |
197 | GetTextCtrl()->Connect | |
198 | ( | |
199 | wxEVT_COMMAND_TEXT_UPDATED, | |
200 | wxCommandEventHandler(wxTextDocument::OnTextChange), | |
201 | NULL, | |
202 | this | |
203 | ); | |
204 | ||
205 | return true; | |
206 | } | |
207 | ||
208 | // Since text windows have their own method for saving to/loading from files, | |
209 | // we override DoSave/OpenDocument instead of Save/LoadObject | |
210 | bool wxTextDocument::DoSaveDocument(const wxString& filename) | |
211 | { | |
212 | return GetTextCtrl()->SaveFile(filename); | |
213 | } | |
214 | ||
215 | bool wxTextDocument::DoOpenDocument(const wxString& filename) | |
216 | { | |
217 | if ( !GetTextCtrl()->LoadFile(filename) ) | |
218 | return false; | |
219 | ||
220 | // we're not modified by the user yet | |
221 | Modify(false); | |
222 | ||
223 | return true; | |
224 | } | |
225 | ||
226 | bool wxTextDocument::IsModified() const | |
227 | { | |
228 | wxTextCtrl* wnd = GetTextCtrl(); | |
229 | return wxDocument::IsModified() || (wnd && wnd->IsModified()); | |
230 | } | |
231 | ||
232 | void wxTextDocument::Modify(bool modified) | |
233 | { | |
234 | wxDocument::Modify(modified); | |
235 | ||
236 | wxTextCtrl* wnd = GetTextCtrl(); | |
237 | if (wnd && !modified) | |
238 | { | |
239 | wnd->DiscardEdits(); | |
240 | } | |
241 | } | |
242 | ||
243 | void wxTextDocument::OnTextChange(wxCommandEvent& event) | |
244 | { | |
245 | Modify(true); | |
246 | ||
247 | event.Skip(); | |
248 | } | |
249 | ||
250 | // ---------------------------------------------------------------------------- | |
251 | // TextEditDocument implementation | |
252 | // ---------------------------------------------------------------------------- | |
253 | ||
254 | IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument) | |
255 | ||
256 | wxTextCtrl* TextEditDocument::GetTextCtrl() const | |
257 | { | |
258 | wxView* view = GetFirstView(); | |
259 | return view ? wxStaticCast(view, TextEditView)->GetText() : NULL; | |
260 | } | |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // ImageDocument and ImageDetailsDocument implementation | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | IMPLEMENT_DYNAMIC_CLASS(ImageDocument, wxDocument) | |
267 | ||
268 | bool ImageDocument::DoOpenDocument(const wxString& file) | |
269 | { | |
270 | return m_image.LoadFile(file); | |
271 | } | |
272 | ||
273 | bool ImageDocument::OnOpenDocument(const wxString& filename) | |
274 | { | |
275 | if ( !wxDocument::OnOpenDocument(filename) ) | |
276 | return false; | |
277 | ||
278 | // we don't have a wxDocTemplate for the image details document as it's | |
279 | // never created by wxWidgets automatically, instead just do it manually | |
280 | ImageDetailsDocument * const docDetails = new ImageDetailsDocument(this); | |
281 | docDetails->SetFilename(filename); | |
282 | ||
283 | new ImageDetailsView(docDetails); | |
284 | ||
285 | return true; | |
286 | } | |
287 | ||
288 | ImageDetailsDocument::ImageDetailsDocument(ImageDocument *parent) | |
289 | : wxDocument(parent) | |
290 | { | |
291 | const wxImage image = parent->GetImage(); | |
292 | ||
293 | m_size.x = image.GetWidth(); | |
294 | m_size.y = image.GetHeight(); | |
295 | m_numColours = image.CountColours(); | |
296 | m_type = image.GetType(); | |
297 | m_hasAlpha = image.HasAlpha(); | |
298 | } |