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
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
24 #if !wxUSE_DOC_VIEW_ARCHITECTURE
25 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
32 #if wxUSE_STD_IOSTREAM
33 #include "wx/ioswrap.h"
35 #include "wx/txtstrm.h"
37 #include "wx/wfstream.h"
42 // ----------------------------------------------------------------------------
43 // DrawingDocument implementation
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(DrawingDocument
, wxDocument
)
48 DocumentOstream
& DrawingDocument::SaveObject(DocumentOstream
& ostream
)
50 #if wxUSE_STD_IOSTREAM
51 DocumentOstream
& stream
= ostream
;
53 wxTextOutputStream
stream(ostream
);
56 wxDocument::SaveObject(ostream
);
58 const wxInt32 count
= m_doodleSegments
.size();
59 stream
<< count
<< '\n';
61 for ( int n
= 0; n
< count
; n
++ )
63 m_doodleSegments
[n
].SaveObject(ostream
);
70 DocumentIstream
& DrawingDocument::LoadObject(DocumentIstream
& istream
)
72 #if wxUSE_STD_IOSTREAM
73 DocumentIstream
& stream
= istream
;
75 wxTextInputStream
stream(istream
);
78 wxDocument::LoadObject(istream
);
84 wxLogWarning("Drawing document corrupted: invalid segments count.");
85 #if wxUSE_STD_IOSTREAM
86 istream
.clear(std::ios::badbit
);
88 istream
.Reset(wxSTREAM_READ_ERROR
);
93 for ( int n
= 0; n
< count
; n
++ )
95 DoodleSegment segment
;
96 segment
.LoadObject(istream
);
97 m_doodleSegments
.push_back(segment
);
103 void DrawingDocument::DoUpdate()
109 void DrawingDocument::AddDoodleSegment(const DoodleSegment
& segment
)
111 m_doodleSegments
.push_back(segment
);
116 bool DrawingDocument::PopLastSegment(DoodleSegment
*segment
)
118 if ( m_doodleSegments
.empty() )
122 *segment
= m_doodleSegments
.back();
124 m_doodleSegments
.pop_back();
131 // ----------------------------------------------------------------------------
132 // DoodleSegment implementation
133 // ----------------------------------------------------------------------------
135 DocumentOstream
& DoodleSegment::SaveObject(DocumentOstream
& ostream
)
137 #if wxUSE_STD_IOSTREAM
138 DocumentOstream
& stream
= ostream
;
140 wxTextOutputStream
stream(ostream
);
143 const wxInt32 count
= m_lines
.size();
144 stream
<< count
<< '\n';
146 for ( int n
= 0; n
< count
; n
++ )
148 const DoodleLine
& line
= m_lines
[n
];
159 DocumentIstream
& DoodleSegment::LoadObject(DocumentIstream
& istream
)
161 #if wxUSE_STD_IOSTREAM
162 DocumentIstream
& stream
= istream
;
164 wxTextInputStream
stream(istream
);
170 for ( int n
= 0; n
< count
; n
++ )
178 m_lines
.push_back(line
);
184 // ----------------------------------------------------------------------------
185 // wxTextDocument: wxDocument and wxTextCtrl married
186 // ----------------------------------------------------------------------------
188 IMPLEMENT_CLASS(wxTextDocument
, wxDocument
)
190 bool wxTextDocument::OnCreate(const wxString
& path
, long flags
)
192 if ( !wxDocument::OnCreate(path
, flags
) )
195 // subscribe to changes in the text control to update the document state
196 // when it's modified
197 GetTextCtrl()->Connect
199 wxEVT_COMMAND_TEXT_UPDATED
,
200 wxCommandEventHandler(wxTextDocument::OnTextChange
),
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
)
212 return GetTextCtrl()->SaveFile(filename
);
215 bool wxTextDocument::DoOpenDocument(const wxString
& filename
)
217 if ( !GetTextCtrl()->LoadFile(filename
) )
220 // we're not modified by the user yet
226 bool wxTextDocument::IsModified() const
228 wxTextCtrl
* wnd
= GetTextCtrl();
229 return wxDocument::IsModified() || (wnd
&& wnd
->IsModified());
232 void wxTextDocument::Modify(bool modified
)
234 wxDocument::Modify(modified
);
236 wxTextCtrl
* wnd
= GetTextCtrl();
237 if (wnd
&& !modified
)
243 void wxTextDocument::OnTextChange(wxCommandEvent
& event
)
250 // ----------------------------------------------------------------------------
251 // TextEditDocument implementation
252 // ----------------------------------------------------------------------------
254 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
256 wxTextCtrl
* TextEditDocument::GetTextCtrl() const
258 wxView
* view
= GetFirstView();
259 return view
? wxStaticCast(view
, TextEditView
)->GetText() : NULL
;
262 // ----------------------------------------------------------------------------
263 // ImageDocument and ImageDetailsDocument implementation
264 // ----------------------------------------------------------------------------
266 IMPLEMENT_DYNAMIC_CLASS(ImageDocument
, wxDocument
)
268 bool ImageDocument::DoOpenDocument(const wxString
& file
)
270 return m_image
.LoadFile(file
);
273 bool ImageDocument::OnOpenDocument(const wxString
& filename
)
275 if ( !wxDocument::OnOpenDocument(filename
) )
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
);
283 new ImageDetailsView(docDetails
);
288 ImageDetailsDocument::ImageDetailsDocument(ImageDocument
*parent
)
291 const wxImage image
= parent
->GetImage();
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();