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 license
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
);
83 for ( int n
= 0; n
< count
; n
++ )
85 DoodleSegment segment
;
86 segment
.LoadObject(istream
);
87 m_doodleSegments
.push_back(segment
);
93 void DrawingDocument::DoUpdate()
99 void DrawingDocument::AddDoodleSegment(const DoodleSegment
& segment
)
101 m_doodleSegments
.push_back(segment
);
106 bool DrawingDocument::PopLastSegment(DoodleSegment
*segment
)
108 if ( m_doodleSegments
.empty() )
112 *segment
= m_doodleSegments
.back();
114 m_doodleSegments
.pop_back();
121 // ----------------------------------------------------------------------------
122 // DoodleSegment implementation
123 // ----------------------------------------------------------------------------
125 DocumentOstream
& DoodleSegment::SaveObject(DocumentOstream
& ostream
)
127 #if wxUSE_STD_IOSTREAM
128 DocumentOstream
& stream
= ostream
;
130 wxTextOutputStream
stream(ostream
);
133 const wxInt32 count
= m_lines
.size();
134 stream
<< count
<< '\n';
136 for ( int n
= 0; n
< count
; n
++ )
138 const DoodleLine
& line
= m_lines
[n
];
149 DocumentIstream
& DoodleSegment::LoadObject(DocumentIstream
& istream
)
151 #if wxUSE_STD_IOSTREAM
152 DocumentIstream
& stream
= istream
;
154 wxTextInputStream
stream(istream
);
160 for ( int n
= 0; n
< count
; n
++ )
168 m_lines
.push_back(line
);
174 // ----------------------------------------------------------------------------
175 // wxTextDocument: wxDocument and wxTextCtrl married
176 // ----------------------------------------------------------------------------
178 IMPLEMENT_CLASS(wxTextDocument
, wxDocument
)
180 bool wxTextDocument::OnCreate(const wxString
& path
, long flags
)
182 if ( !wxDocument::OnCreate(path
, flags
) )
185 // subscribe to changes in the text control to update the document state
186 // when it's modified
187 GetTextCtrl()->Connect
189 wxEVT_COMMAND_TEXT_UPDATED
,
190 wxCommandEventHandler(wxTextDocument::OnTextChange
),
198 // Since text windows have their own method for saving to/loading from files,
199 // we override DoSave/OpenDocument instead of Save/LoadObject
200 bool wxTextDocument::DoSaveDocument(const wxString
& filename
)
202 return GetTextCtrl()->SaveFile(filename
);
205 bool wxTextDocument::DoOpenDocument(const wxString
& filename
)
207 if ( !GetTextCtrl()->LoadFile(filename
) )
210 // we're not modified by the user yet
216 bool wxTextDocument::IsModified() const
218 wxTextCtrl
* wnd
= GetTextCtrl();
219 return wxDocument::IsModified() || (wnd
&& wnd
->IsModified());
222 void wxTextDocument::Modify(bool modified
)
224 wxDocument::Modify(modified
);
226 wxTextCtrl
* wnd
= GetTextCtrl();
227 if (wnd
&& !modified
)
233 void wxTextDocument::OnTextChange(wxCommandEvent
& event
)
240 // ----------------------------------------------------------------------------
241 // TextEditDocument implementation
242 // ----------------------------------------------------------------------------
244 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
246 wxTextCtrl
* TextEditDocument::GetTextCtrl() const
248 wxView
* view
= GetFirstView();
249 return view
? wxStaticCast(view
, TextEditView
)->GetText() : NULL
;
252 // ----------------------------------------------------------------------------
253 // wxImageDocument implementation
254 // ----------------------------------------------------------------------------
256 /////////////////////////////////////////////////////////////////////////////
259 IMPLEMENT_DYNAMIC_CLASS(wxImageDocument
, wxDocument
)
261 wxImageDocument::wxImageDocument() : wxDocument()
265 wxImageDocument::~wxImageDocument()
269 bool wxImageDocument::DeleteContents()
271 bool ok
= wxDocument::DeleteContents();
272 if (ok
&& m_image
.IsOk())
279 bool wxImageDocument::SaveFile(wxOutputStream
* stream
, wxBitmapType type
) const
281 return m_image
.IsOk() && m_image
.SaveFile(*stream
, type
);
284 bool wxImageDocument::DoOpenDocument(const wxString
& file
)
286 wxFileInputStream
stream(file
);
287 return stream
.IsOk() && DoOpenDocument(&stream
);
290 bool wxImageDocument::DoSaveDocument(const wxString
& file
)
292 wxFileOutputStream
stream(file
);
293 return stream
.IsOk() && DoSaveDocument(&stream
);
296 bool wxImageDocument::DoOpenDocument(wxInputStream
* stream
)
298 return m_image
.LoadFile(*stream
);
301 bool wxImageDocument::DoSaveDocument(wxOutputStream
* stream
) const
303 return m_image
.IsOk() && SaveFile(stream
, m_image
.GetType());