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
);
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 // ImageDocument and ImageDetailsDocument implementation
254 // ----------------------------------------------------------------------------
256 IMPLEMENT_DYNAMIC_CLASS(ImageDocument
, wxDocument
)
258 bool ImageDocument::DoOpenDocument(const wxString
& file
)
260 return m_image
.LoadFile(file
);
263 bool ImageDocument::OnOpenDocument(const wxString
& filename
)
265 if ( !wxDocument::OnOpenDocument(filename
) )
268 // we don't have a wxDocTemplate for the image details document as it's
269 // never created by wxWidgets automatically, instead just do it manually
270 ImageDetailsDocument
* const docDetails
= new ImageDetailsDocument(this);
271 docDetails
->SetFilename(filename
);
273 new ImageDetailsView(docDetails
);
278 ImageDetailsDocument::ImageDetailsDocument(ImageDocument
*parent
)
281 const wxImage image
= parent
->GetImage();
283 m_size
.x
= image
.GetWidth();
284 m_size
.y
= image
.GetHeight();
285 m_numColours
= image
.CountColours();
286 m_type
= image
.GetType();
287 m_hasAlpha
= image
.HasAlpha();