]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/doc.cpp
fix vertical mouse wheel event rotation value, sign was reversed in r74805
[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 // Copyright: (c) 1998 Julian Smart
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ----------------------------------------------------------------------------
13 // headers
14 // ----------------------------------------------------------------------------
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #endif
30
31 #if wxUSE_STD_IOSTREAM
32 #include "wx/ioswrap.h"
33 #else
34 #include "wx/txtstrm.h"
35 #endif
36 #include "wx/wfstream.h"
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 if ( count < 0 )
82 {
83 wxLogWarning("Drawing document corrupted: invalid segments count.");
84 #if wxUSE_STD_IOSTREAM
85 istream.clear(std::ios::badbit);
86 #else
87 istream.Reset(wxSTREAM_READ_ERROR);
88 #endif
89 return istream;
90 }
91
92 for ( int n = 0; n < count; n++ )
93 {
94 DoodleSegment segment;
95 segment.LoadObject(istream);
96 m_doodleSegments.push_back(segment);
97 }
98
99 return istream;
100 }
101
102 void DrawingDocument::DoUpdate()
103 {
104 Modify(true);
105 UpdateAllViews();
106 }
107
108 void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
109 {
110 m_doodleSegments.push_back(segment);
111
112 DoUpdate();
113 }
114
115 bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
116 {
117 if ( m_doodleSegments.empty() )
118 return false;
119
120 if ( segment )
121 *segment = m_doodleSegments.back();
122
123 m_doodleSegments.pop_back();
124
125 DoUpdate();
126
127 return true;
128 }
129
130 // ----------------------------------------------------------------------------
131 // DoodleSegment implementation
132 // ----------------------------------------------------------------------------
133
134 DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
135 {
136 #if wxUSE_STD_IOSTREAM
137 DocumentOstream& stream = ostream;
138 #else
139 wxTextOutputStream stream(ostream);
140 #endif
141
142 const wxInt32 count = m_lines.size();
143 stream << count << '\n';
144
145 for ( int n = 0; n < count; n++ )
146 {
147 const DoodleLine& line = m_lines[n];
148 stream
149 << line.x1 << ' '
150 << line.y1 << ' '
151 << line.x2 << ' '
152 << line.y2 << '\n';
153 }
154
155 return ostream;
156 }
157
158 DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
159 {
160 #if wxUSE_STD_IOSTREAM
161 DocumentIstream& stream = istream;
162 #else
163 wxTextInputStream stream(istream);
164 #endif
165
166 wxInt32 count = 0;
167 stream >> count;
168
169 for ( int n = 0; n < count; n++ )
170 {
171 DoodleLine line;
172 stream
173 >> line.x1
174 >> line.y1
175 >> line.x2
176 >> line.y2;
177 m_lines.push_back(line);
178 }
179
180 return istream;
181 }
182
183 // ----------------------------------------------------------------------------
184 // wxTextDocument: wxDocument and wxTextCtrl married
185 // ----------------------------------------------------------------------------
186
187 IMPLEMENT_CLASS(wxTextDocument, wxDocument)
188
189 bool wxTextDocument::OnCreate(const wxString& path, long flags)
190 {
191 if ( !wxDocument::OnCreate(path, flags) )
192 return false;
193
194 // subscribe to changes in the text control to update the document state
195 // when it's modified
196 GetTextCtrl()->Connect
197 (
198 wxEVT_TEXT,
199 wxCommandEventHandler(wxTextDocument::OnTextChange),
200 NULL,
201 this
202 );
203
204 return true;
205 }
206
207 // Since text windows have their own method for saving to/loading from files,
208 // we override DoSave/OpenDocument instead of Save/LoadObject
209 bool wxTextDocument::DoSaveDocument(const wxString& filename)
210 {
211 return GetTextCtrl()->SaveFile(filename);
212 }
213
214 bool wxTextDocument::DoOpenDocument(const wxString& filename)
215 {
216 if ( !GetTextCtrl()->LoadFile(filename) )
217 return false;
218
219 // we're not modified by the user yet
220 Modify(false);
221
222 return true;
223 }
224
225 bool wxTextDocument::IsModified() const
226 {
227 wxTextCtrl* wnd = GetTextCtrl();
228 return wxDocument::IsModified() || (wnd && wnd->IsModified());
229 }
230
231 void wxTextDocument::Modify(bool modified)
232 {
233 wxDocument::Modify(modified);
234
235 wxTextCtrl* wnd = GetTextCtrl();
236 if (wnd && !modified)
237 {
238 wnd->DiscardEdits();
239 }
240 }
241
242 void wxTextDocument::OnTextChange(wxCommandEvent& event)
243 {
244 Modify(true);
245
246 event.Skip();
247 }
248
249 // ----------------------------------------------------------------------------
250 // TextEditDocument implementation
251 // ----------------------------------------------------------------------------
252
253 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
254
255 wxTextCtrl* TextEditDocument::GetTextCtrl() const
256 {
257 wxView* view = GetFirstView();
258 return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
259 }
260
261 // ----------------------------------------------------------------------------
262 // ImageDocument and ImageDetailsDocument implementation
263 // ----------------------------------------------------------------------------
264
265 IMPLEMENT_DYNAMIC_CLASS(ImageDocument, wxDocument)
266
267 bool ImageDocument::DoOpenDocument(const wxString& file)
268 {
269 return m_image.LoadFile(file);
270 }
271
272 bool ImageDocument::OnOpenDocument(const wxString& filename)
273 {
274 if ( !wxDocument::OnOpenDocument(filename) )
275 return false;
276
277 // we don't have a wxDocTemplate for the image details document as it's
278 // never created by wxWidgets automatically, instead just do it manually
279 ImageDetailsDocument * const docDetails = new ImageDetailsDocument(this);
280 docDetails->SetFilename(filename);
281
282 new ImageDetailsView(docDetails);
283
284 return true;
285 }
286
287 ImageDetailsDocument::ImageDetailsDocument(ImageDocument *parent)
288 : wxDocument(parent)
289 {
290 const wxImage image = parent->GetImage();
291
292 m_size.x = image.GetWidth();
293 m_size.y = image.GetHeight();
294 m_numColours = image.CountColours();
295 m_type = image.GetType();
296 m_hasAlpha = image.HasAlpha();
297 }