]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/docview/doc.cpp
bringing target naming in line with other projects
[wxWidgets.git] / samples / docview / doc.cpp
... / ...
CommitLineData
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
46IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
47
48DocumentOstream& 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
70DocumentIstream& 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
83 for ( int n = 0; n < count; n++ )
84 {
85 DoodleSegment segment;
86 segment.LoadObject(istream);
87 m_doodleSegments.push_back(segment);
88 }
89
90 return istream;
91}
92
93void DrawingDocument::DoUpdate()
94{
95 Modify(true);
96 UpdateAllViews();
97}
98
99void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
100{
101 m_doodleSegments.push_back(segment);
102
103 DoUpdate();
104}
105
106bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
107{
108 if ( m_doodleSegments.empty() )
109 return false;
110
111 if ( segment )
112 *segment = m_doodleSegments.back();
113
114 m_doodleSegments.pop_back();
115
116 DoUpdate();
117
118 return true;
119}
120
121// ----------------------------------------------------------------------------
122// DoodleSegment implementation
123// ----------------------------------------------------------------------------
124
125DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
126{
127#if wxUSE_STD_IOSTREAM
128 DocumentOstream& stream = ostream;
129#else
130 wxTextOutputStream stream(ostream);
131#endif
132
133 const wxInt32 count = m_lines.size();
134 stream << count << '\n';
135
136 for ( int n = 0; n < count; n++ )
137 {
138 const DoodleLine& line = m_lines[n];
139 stream
140 << line.x1 << ' '
141 << line.y1 << ' '
142 << line.x2 << ' '
143 << line.y2 << '\n';
144 }
145
146 return ostream;
147}
148
149DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
150{
151#if wxUSE_STD_IOSTREAM
152 DocumentIstream& stream = istream;
153#else
154 wxTextInputStream stream(istream);
155#endif
156
157 wxInt32 count = 0;
158 stream >> count;
159
160 for ( int n = 0; n < count; n++ )
161 {
162 DoodleLine line;
163 stream
164 >> line.x1
165 >> line.y1
166 >> line.x2
167 >> line.y2;
168 m_lines.push_back(line);
169 }
170
171 return istream;
172}
173
174// ----------------------------------------------------------------------------
175// wxTextDocument: wxDocument and wxTextCtrl married
176// ----------------------------------------------------------------------------
177
178IMPLEMENT_CLASS(wxTextDocument, wxDocument)
179
180bool 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}
197
198// Since text windows have their own method for saving to/loading from files,
199// we override DoSave/OpenDocument instead of Save/LoadObject
200bool wxTextDocument::DoSaveDocument(const wxString& filename)
201{
202 return GetTextCtrl()->SaveFile(filename);
203}
204
205bool wxTextDocument::DoOpenDocument(const wxString& filename)
206{
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;
214}
215
216bool wxTextDocument::IsModified() const
217{
218 wxTextCtrl* wnd = GetTextCtrl();
219 return wxDocument::IsModified() || (wnd && wnd->IsModified());
220}
221
222void wxTextDocument::Modify(bool modified)
223{
224 wxDocument::Modify(modified);
225
226 wxTextCtrl* wnd = GetTextCtrl();
227 if (wnd && !modified)
228 {
229 wnd->DiscardEdits();
230 }
231}
232
233void wxTextDocument::OnTextChange(wxCommandEvent& event)
234{
235 Modify(true);
236
237 event.Skip();
238}
239
240// ----------------------------------------------------------------------------
241// TextEditDocument implementation
242// ----------------------------------------------------------------------------
243
244IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
245
246wxTextCtrl* TextEditDocument::GetTextCtrl() const
247{
248 wxView* view = GetFirstView();
249 return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
250}
251
252// ----------------------------------------------------------------------------
253// ImageDocument and wxImageDetailsDocument implementation
254// ----------------------------------------------------------------------------
255
256IMPLEMENT_DYNAMIC_CLASS(ImageDocument, wxDocument)
257
258bool ImageDocument::DoOpenDocument(const wxString& file)
259{
260 return m_image.LoadFile(file);
261}
262