]>
git.saurik.com Git - wxWidgets.git/blob - 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
7 // Copyright: (c) 1998 Julian Smart
8 // (c) 2008 Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ----------------------------------------------------------------------------
14 // ----------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 #if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
31 #if wxUSE_STD_IOSTREAM
32 #include "wx/ioswrap.h"
34 #include "wx/txtstrm.h"
36 #include "wx/wfstream.h"
41 // ----------------------------------------------------------------------------
42 // DrawingDocument implementation
43 // ----------------------------------------------------------------------------
45 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
47 DocumentOstream
& DrawingDocument::SaveObject(DocumentOstream
& ostream
)
49 #if wxUSE_STD_IOSTREAM
50 DocumentOstream
& stream
= ostream
;
52 wxTextOutputStream
stream(ostream
);
55 wxDocument::SaveObject(ostream
);
57 const wxInt32 count
= m_doodleSegments
.size();
58 stream
<< count
<< '\n';
60 for ( int n
= 0; n
< count
; n
++ )
62 m_doodleSegments
[n
].SaveObject(ostream
);
69 DocumentIstream
& DrawingDocument::LoadObject(DocumentIstream
& istream
)
71 #if wxUSE_STD_IOSTREAM
72 DocumentIstream
& stream
= istream
;
74 wxTextInputStream
stream(istream
);
77 wxDocument::LoadObject(istream
);
83 wxLogWarning("Drawing document corrupted: invalid segments count.");
84 #if wxUSE_STD_IOSTREAM
85 istream
.clear(std::ios::badbit
);
87 istream
.Reset(wxSTREAM_READ_ERROR
);
92 for ( int n
= 0; n
< count
; n
++ )
94 DoodleSegment segment
;
95 segment
.LoadObject(istream
);
96 m_doodleSegments
.push_back(segment
);
102 void DrawingDocument::DoUpdate()
108 void DrawingDocument::AddDoodleSegment(const DoodleSegment
& segment
)
110 m_doodleSegments
.push_back(segment
);
115 bool DrawingDocument::PopLastSegment(DoodleSegment
*segment
)
117 if ( m_doodleSegments
.empty() )
121 *segment
= m_doodleSegments
.back();
123 m_doodleSegments
.pop_back();
130 // ----------------------------------------------------------------------------
131 // DoodleSegment implementation
132 // ----------------------------------------------------------------------------
134 DocumentOstream
& DoodleSegment::SaveObject(DocumentOstream
& ostream
)
136 #if wxUSE_STD_IOSTREAM
137 DocumentOstream
& stream
= ostream
;
139 wxTextOutputStream
stream(ostream
);
142 const wxInt32 count
= m_lines
.size();
143 stream
<< count
<< '\n';
145 for ( int n
= 0; n
< count
; n
++ )
147 const DoodleLine
& line
= m_lines
[n
];
158 DocumentIstream
& DoodleSegment::LoadObject(DocumentIstream
& istream
)
160 #if wxUSE_STD_IOSTREAM
161 DocumentIstream
& stream
= istream
;
163 wxTextInputStream
stream(istream
);
169 for ( int n
= 0; n
< count
; n
++ )
177 m_lines
.push_back(line
);
183 // ----------------------------------------------------------------------------
184 // wxTextDocument: wxDocument and wxTextCtrl married
185 // ----------------------------------------------------------------------------
187 IMPLEMENT_CLASS(wxTextDocument
, wxDocument
)
189 bool wxTextDocument::OnCreate(const wxString
& path
, long flags
)
191 if ( !wxDocument::OnCreate(path
, flags
) )
194 // subscribe to changes in the text control to update the document state
195 // when it's modified
196 GetTextCtrl()->Connect
199 wxCommandEventHandler(wxTextDocument::OnTextChange
),
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
)
211 return GetTextCtrl()->SaveFile(filename
);
214 bool wxTextDocument::DoOpenDocument(const wxString
& filename
)
216 if ( !GetTextCtrl()->LoadFile(filename
) )
219 // we're not modified by the user yet
225 bool wxTextDocument::IsModified() const
227 wxTextCtrl
* wnd
= GetTextCtrl();
228 return wxDocument::IsModified() || (wnd
&& wnd
->IsModified());
231 void wxTextDocument::Modify(bool modified
)
233 wxDocument::Modify(modified
);
235 wxTextCtrl
* wnd
= GetTextCtrl();
236 if (wnd
&& !modified
)
242 void wxTextDocument::OnTextChange(wxCommandEvent
& event
)
249 // ----------------------------------------------------------------------------
250 // TextEditDocument implementation
251 // ----------------------------------------------------------------------------
253 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
255 wxTextCtrl
* TextEditDocument::GetTextCtrl() const
257 wxView
* view
= GetFirstView();
258 return view
? wxStaticCast(view
, TextEditView
)->GetText() : NULL
;
261 // ----------------------------------------------------------------------------
262 // ImageDocument and ImageDetailsDocument implementation
263 // ----------------------------------------------------------------------------
265 IMPLEMENT_DYNAMIC_CLASS(ImageDocument
, wxDocument
)
267 bool ImageDocument::DoOpenDocument(const wxString
& file
)
269 return m_image
.LoadFile(file
);
272 bool ImageDocument::OnOpenDocument(const wxString
& filename
)
274 if ( !wxDocument::OnOpenDocument(filename
) )
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
);
282 new ImageDetailsView(docDetails
);
287 ImageDetailsDocument::ImageDetailsDocument(ImageDocument
*parent
)
290 const wxImage image
= parent
->GetImage();
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();