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"
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
);
82 for ( int n
= 0; n
< count
; n
++ )
84 DoodleSegment segment
;
85 segment
.LoadObject(istream
);
86 m_doodleSegments
.push_back(segment
);
92 void DrawingDocument::DoUpdate()
98 void DrawingDocument::AddDoodleSegment(const DoodleSegment
& segment
)
100 m_doodleSegments
.push_back(segment
);
105 bool DrawingDocument::PopLastSegment(DoodleSegment
*segment
)
107 if ( m_doodleSegments
.empty() )
111 *segment
= m_doodleSegments
.back();
113 m_doodleSegments
.pop_back();
120 // ----------------------------------------------------------------------------
121 // DoodleSegment implementation
122 // ----------------------------------------------------------------------------
124 DocumentOstream
& DoodleSegment::SaveObject(DocumentOstream
& ostream
)
126 #if wxUSE_STD_IOSTREAM
127 DocumentOstream
& stream
= ostream
;
129 wxTextOutputStream
stream(ostream
);
132 const wxInt32 count
= m_lines
.size();
133 stream
<< count
<< '\n';
135 for ( int n
= 0; n
< count
; n
++ )
137 const DoodleLine
& line
= m_lines
[n
];
148 DocumentIstream
& DoodleSegment::LoadObject(DocumentIstream
& istream
)
150 #if wxUSE_STD_IOSTREAM
151 DocumentIstream
& stream
= istream
;
153 wxTextInputStream
stream(istream
);
159 for ( int n
= 0; n
< count
; n
++ )
167 m_lines
.push_back(line
);
173 // ----------------------------------------------------------------------------
174 // wxTextDocument: wxDocument and wxTextCtrl married
175 // ----------------------------------------------------------------------------
177 IMPLEMENT_CLASS(wxTextDocument
, wxDocument
)
179 bool wxTextDocument::OnCreate(const wxString
& path
, long flags
)
181 if ( !wxDocument::OnCreate(path
, flags
) )
184 // subscribe to changes in the text control to update the document state
185 // when it's modified
186 GetTextCtrl()->Connect
188 wxEVT_COMMAND_TEXT_UPDATED
,
189 wxCommandEventHandler(wxTextDocument::OnTextChange
),
197 // Since text windows have their own method for saving to/loading from files,
198 // we override DoSave/OpenDocument instead of Save/LoadObject
199 bool wxTextDocument::DoSaveDocument(const wxString
& filename
)
201 return GetTextCtrl()->SaveFile(filename
);
204 bool wxTextDocument::DoOpenDocument(const wxString
& filename
)
206 return GetTextCtrl()->LoadFile(filename
);
209 bool wxTextDocument::IsModified() const
211 wxTextCtrl
* wnd
= GetTextCtrl();
212 return wxDocument::IsModified() || (wnd
&& wnd
->IsModified());
215 void wxTextDocument::Modify(bool modified
)
217 wxDocument::Modify(modified
);
219 wxTextCtrl
* wnd
= GetTextCtrl();
220 if (wnd
&& !modified
)
226 void wxTextDocument::OnTextChange(wxCommandEvent
& event
)
233 // ----------------------------------------------------------------------------
234 // TextEditDocument implementation
235 // ----------------------------------------------------------------------------
237 IMPLEMENT_DYNAMIC_CLASS(TextEditDocument
, wxDocument
)
239 wxTextCtrl
* TextEditDocument::GetTextCtrl() const
241 wxView
* view
= GetFirstView();
242 return view
? wxStaticCast(view
, TextEditView
)->GetText() : NULL
;