]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.cpp
Fix recreating of wxBitmapComboBox using untyped client data.
[wxWidgets.git] / samples / docview / doc.cpp
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
2d1df0fc 2// Name: samples/docview/doc.cpp
457814b5
JS
3// Purpose: Implements document functionality
4// Author: Julian Smart
2d1df0fc 5// Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
457814b5 6// Created: 04/01/98
2d1df0fc
VZ
7// Copyright: (c) 1998 Julian Smart
8// (c) 2008 Vadim Zeitlin
526954c5 9// Licence: wxWindows licence
457814b5
JS
10/////////////////////////////////////////////////////////////////////////////
11
2d1df0fc
VZ
12// ----------------------------------------------------------------------------
13// headers
14// ----------------------------------------------------------------------------
15
457814b5
JS
16// For compilers that support precompilation, includes "wx/wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
2d1df0fc 20 #pragma hdrstop
457814b5
JS
21#endif
22
2d1df0fc
VZ
23#if !wxUSE_DOC_VIEW_ARCHITECTURE
24 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5 25#endif
2d1df0fc
VZ
26
27#ifndef WX_PRECOMP
28 #include "wx/wx.h"
450a5bdd 29#endif
457814b5 30
76cf603e
VZ
31#if wxUSE_STD_IOSTREAM
32 #include "wx/ioswrap.h"
33#else
34 #include "wx/txtstrm.h"
35#endif
f37f49b6 36#include "wx/wfstream.h"
76cf603e 37
457814b5
JS
38#include "doc.h"
39#include "view.h"
457814b5 40
2d1df0fc
VZ
41// ----------------------------------------------------------------------------
42// DrawingDocument implementation
43// ----------------------------------------------------------------------------
457814b5 44
2d1df0fc
VZ
45IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
46
00a1c49a 47DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& ostream)
457814b5 48{
00a1c49a
VZ
49#if wxUSE_STD_IOSTREAM
50 DocumentOstream& stream = ostream;
51#else
52 wxTextOutputStream stream(ostream);
53#endif
54
55 wxDocument::SaveObject(ostream);
958d3a7e 56
2d1df0fc
VZ
57 const wxInt32 count = m_doodleSegments.size();
58 stream << count << '\n';
958d3a7e 59
2d1df0fc 60 for ( int n = 0; n < count; n++ )
f6bcfd97 61 {
00a1c49a 62 m_doodleSegments[n].SaveObject(ostream);
f6bcfd97 63 stream << '\n';
f6bcfd97 64 }
958d3a7e 65
00a1c49a 66 return ostream;
457814b5 67}
958d3a7e 68
00a1c49a 69DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream)
2d1df0fc 70{
00a1c49a
VZ
71#if wxUSE_STD_IOSTREAM
72 DocumentIstream& stream = istream;
73#else
74 wxTextInputStream stream(istream);
75#endif
76
77 wxDocument::LoadObject(istream);
958d3a7e 78
2d1df0fc
VZ
79 wxInt32 count = 0;
80 stream >> count;
90693f47
VZ
81 if ( count < 0 )
82 {
83 wxLogWarning("Drawing document corrupted: invalid segments count.");
84#if wxUSE_STD_IOSTREAM
85 istream.clear(std::ios::badbit);
86#else
87 istream.Reset(wxSTREAM_READ_ERROR);
88#endif
89 return istream;
90 }
958d3a7e 91
2d1df0fc 92 for ( int n = 0; n < count; n++ )
f6bcfd97 93 {
2d1df0fc 94 DoodleSegment segment;
00a1c49a 95 segment.LoadObject(istream);
2d1df0fc 96 m_doodleSegments.push_back(segment);
f6bcfd97 97 }
958d3a7e 98
00a1c49a 99 return istream;
56d7679d 100}
56d7679d 101
2d1df0fc 102void DrawingDocument::DoUpdate()
457814b5 103{
2d1df0fc
VZ
104 Modify(true);
105 UpdateAllViews();
106}
958d3a7e 107
2d1df0fc
VZ
108void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
109{
110 m_doodleSegments.push_back(segment);
958d3a7e 111
2d1df0fc 112 DoUpdate();
457814b5 113}
2d1df0fc
VZ
114
115bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
56d7679d 116{
2d1df0fc
VZ
117 if ( m_doodleSegments.empty() )
118 return false;
958d3a7e 119
2d1df0fc
VZ
120 if ( segment )
121 *segment = m_doodleSegments.back();
958d3a7e 122
2d1df0fc 123 m_doodleSegments.pop_back();
958d3a7e 124
2d1df0fc 125 DoUpdate();
958d3a7e 126
2d1df0fc 127 return true;
56d7679d 128}
958d3a7e 129
2d1df0fc
VZ
130// ----------------------------------------------------------------------------
131// DoodleSegment implementation
132// ----------------------------------------------------------------------------
457814b5 133
2d1df0fc 134DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
457814b5 135{
56d7679d 136#if wxUSE_STD_IOSTREAM
2d1df0fc 137 DocumentOstream& stream = ostream;
56d7679d 138#else
2d1df0fc
VZ
139 wxTextOutputStream stream(ostream);
140#endif
958d3a7e 141
2d1df0fc
VZ
142 const wxInt32 count = m_lines.size();
143 stream << count << '\n';
958d3a7e 144
2d1df0fc 145 for ( int n = 0; n < count; n++ )
f6bcfd97 146 {
2d1df0fc
VZ
147 const DoodleLine& line = m_lines[n];
148 stream
149 << line.x1 << ' '
150 << line.y1 << ' '
151 << line.x2 << ' '
152 << line.y2 << '\n';
f6bcfd97 153 }
958d3a7e 154
00a1c49a 155 return ostream;
56d7679d 156}
457814b5 157
2d1df0fc 158DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
457814b5 159{
2d1df0fc
VZ
160#if wxUSE_STD_IOSTREAM
161 DocumentIstream& stream = istream;
56d7679d 162#else
2d1df0fc
VZ
163 wxTextInputStream stream(istream);
164#endif
958d3a7e 165
2d1df0fc
VZ
166 wxInt32 count = 0;
167 stream >> count;
958d3a7e 168
2d1df0fc 169 for ( int n = 0; n < count; n++ )
f6bcfd97 170 {
2d1df0fc
VZ
171 DoodleLine line;
172 stream
173 >> line.x1
174 >> line.y1
175 >> line.x2
176 >> line.y2;
177 m_lines.push_back(line);
f6bcfd97 178 }
958d3a7e 179
00a1c49a 180 return istream;
56d7679d 181}
457814b5 182
2d1df0fc 183// ----------------------------------------------------------------------------
828c8f98 184// wxTextDocument: wxDocument and wxTextCtrl married
2d1df0fc 185// ----------------------------------------------------------------------------
457814b5 186
828c8f98 187IMPLEMENT_CLASS(wxTextDocument, wxDocument)
457814b5 188
112d941f
VZ
189bool wxTextDocument::OnCreate(const wxString& path, long flags)
190{
191 if ( !wxDocument::OnCreate(path, flags) )
192 return false;
193
194 // subscribe to changes in the text control to update the document state
195 // when it's modified
196 GetTextCtrl()->Connect
197 (
ce7fe42e 198 wxEVT_TEXT,
112d941f
VZ
199 wxCommandEventHandler(wxTextDocument::OnTextChange),
200 NULL,
201 this
202 );
203
204 return true;
205}
6d3a0824 206
457814b5 207// Since text windows have their own method for saving to/loading from files,
c9d13e86 208// we override DoSave/OpenDocument instead of Save/LoadObject
828c8f98 209bool wxTextDocument::DoSaveDocument(const wxString& filename)
457814b5 210{
828c8f98 211 return GetTextCtrl()->SaveFile(filename);
457814b5
JS
212}
213
828c8f98 214bool wxTextDocument::DoOpenDocument(const wxString& filename)
457814b5 215{
6d3a0824
VZ
216 if ( !GetTextCtrl()->LoadFile(filename) )
217 return false;
218
219 // we're not modified by the user yet
220 Modify(false);
221
222 return true;
457814b5
JS
223}
224
828c8f98 225bool wxTextDocument::IsModified() const
457814b5 226{
828c8f98
FM
227 wxTextCtrl* wnd = GetTextCtrl();
228 return wxDocument::IsModified() || (wnd && wnd->IsModified());
457814b5
JS
229}
230
828c8f98 231void wxTextDocument::Modify(bool modified)
457814b5 232{
2d1df0fc 233 wxDocument::Modify(modified);
958d3a7e 234
828c8f98
FM
235 wxTextCtrl* wnd = GetTextCtrl();
236 if (wnd && !modified)
237 {
238 wnd->DiscardEdits();
239 }
457814b5 240}
6bdf5153 241
112d941f
VZ
242void wxTextDocument::OnTextChange(wxCommandEvent& event)
243{
244 Modify(true);
245
246 event.Skip();
247}
248
828c8f98
FM
249// ----------------------------------------------------------------------------
250// TextEditDocument implementation
251// ----------------------------------------------------------------------------
252
253IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
254
255wxTextCtrl* TextEditDocument::GetTextCtrl() const
6bdf5153 256{
828c8f98
FM
257 wxView* view = GetFirstView();
258 return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
6bdf5153 259}
f37f49b6
JS
260
261// ----------------------------------------------------------------------------
4db97e24 262// ImageDocument and ImageDetailsDocument implementation
f37f49b6
JS
263// ----------------------------------------------------------------------------
264
2d4a03f8 265IMPLEMENT_DYNAMIC_CLASS(ImageDocument, wxDocument)
f37f49b6 266
2d4a03f8 267bool ImageDocument::DoOpenDocument(const wxString& file)
f37f49b6 268{
2d4a03f8 269 return m_image.LoadFile(file);
f37f49b6
JS
270}
271
4db97e24
VZ
272bool ImageDocument::OnOpenDocument(const wxString& filename)
273{
274 if ( !wxDocument::OnOpenDocument(filename) )
275 return false;
276
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);
281
282 new ImageDetailsView(docDetails);
283
284 return true;
285}
286
287ImageDetailsDocument::ImageDetailsDocument(ImageDocument *parent)
288 : wxDocument(parent)
289{
290 const wxImage image = parent->GetImage();
291
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();
297}