]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.cpp
initialize wxDocChildFrameAnyBase before creating the real window to ensure that...
[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
JS
6// Created: 04/01/98
7// RCS-ID: $Id$
2d1df0fc
VZ
8// Copyright: (c) 1998 Julian Smart
9// (c) 2008 Vadim Zeitlin
2f6c54eb 10// Licence: wxWindows license
457814b5
JS
11/////////////////////////////////////////////////////////////////////////////
12
2d1df0fc
VZ
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
457814b5
JS
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
2d1df0fc 21 #pragma hdrstop
457814b5
JS
22#endif
23
2d1df0fc
VZ
24#if !wxUSE_DOC_VIEW_ARCHITECTURE
25 #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
457814b5 26#endif
2d1df0fc
VZ
27
28#ifndef WX_PRECOMP
29 #include "wx/wx.h"
450a5bdd 30#endif
457814b5 31
76cf603e
VZ
32#if wxUSE_STD_IOSTREAM
33 #include "wx/ioswrap.h"
34#else
35 #include "wx/txtstrm.h"
36#endif
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;
958d3a7e 81
2d1df0fc 82 for ( int n = 0; n < count; n++ )
f6bcfd97 83 {
2d1df0fc 84 DoodleSegment segment;
00a1c49a 85 segment.LoadObject(istream);
2d1df0fc 86 m_doodleSegments.push_back(segment);
f6bcfd97 87 }
958d3a7e 88
00a1c49a 89 return istream;
56d7679d 90}
56d7679d 91
2d1df0fc 92void DrawingDocument::DoUpdate()
457814b5 93{
2d1df0fc
VZ
94 Modify(true);
95 UpdateAllViews();
96}
958d3a7e 97
2d1df0fc
VZ
98void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
99{
100 m_doodleSegments.push_back(segment);
958d3a7e 101
2d1df0fc 102 DoUpdate();
457814b5 103}
2d1df0fc
VZ
104
105bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
56d7679d 106{
2d1df0fc
VZ
107 if ( m_doodleSegments.empty() )
108 return false;
958d3a7e 109
2d1df0fc
VZ
110 if ( segment )
111 *segment = m_doodleSegments.back();
958d3a7e 112
2d1df0fc 113 m_doodleSegments.pop_back();
958d3a7e 114
2d1df0fc 115 DoUpdate();
958d3a7e 116
2d1df0fc 117 return true;
56d7679d 118}
958d3a7e 119
2d1df0fc
VZ
120// ----------------------------------------------------------------------------
121// DoodleSegment implementation
122// ----------------------------------------------------------------------------
457814b5 123
2d1df0fc 124DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
457814b5 125{
56d7679d 126#if wxUSE_STD_IOSTREAM
2d1df0fc 127 DocumentOstream& stream = ostream;
56d7679d 128#else
2d1df0fc
VZ
129 wxTextOutputStream stream(ostream);
130#endif
958d3a7e 131
2d1df0fc
VZ
132 const wxInt32 count = m_lines.size();
133 stream << count << '\n';
958d3a7e 134
2d1df0fc 135 for ( int n = 0; n < count; n++ )
f6bcfd97 136 {
2d1df0fc
VZ
137 const DoodleLine& line = m_lines[n];
138 stream
139 << line.x1 << ' '
140 << line.y1 << ' '
141 << line.x2 << ' '
142 << line.y2 << '\n';
f6bcfd97 143 }
958d3a7e 144
00a1c49a 145 return ostream;
56d7679d 146}
457814b5 147
2d1df0fc 148DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
457814b5 149{
2d1df0fc
VZ
150#if wxUSE_STD_IOSTREAM
151 DocumentIstream& stream = istream;
56d7679d 152#else
2d1df0fc
VZ
153 wxTextInputStream stream(istream);
154#endif
958d3a7e 155
2d1df0fc
VZ
156 wxInt32 count = 0;
157 stream >> count;
958d3a7e 158
2d1df0fc 159 for ( int n = 0; n < count; n++ )
f6bcfd97 160 {
2d1df0fc
VZ
161 DoodleLine line;
162 stream
163 >> line.x1
164 >> line.y1
165 >> line.x2
166 >> line.y2;
167 m_lines.push_back(line);
f6bcfd97 168 }
958d3a7e 169
00a1c49a 170 return istream;
56d7679d 171}
457814b5 172
2d1df0fc 173// ----------------------------------------------------------------------------
828c8f98 174// wxTextDocument: wxDocument and wxTextCtrl married
2d1df0fc 175// ----------------------------------------------------------------------------
457814b5 176
828c8f98 177IMPLEMENT_CLASS(wxTextDocument, wxDocument)
457814b5
JS
178
179// Since text windows have their own method for saving to/loading from files,
c9d13e86 180// we override DoSave/OpenDocument instead of Save/LoadObject
828c8f98 181bool wxTextDocument::DoSaveDocument(const wxString& filename)
457814b5 182{
828c8f98 183 return GetTextCtrl()->SaveFile(filename);
457814b5
JS
184}
185
828c8f98 186bool wxTextDocument::DoOpenDocument(const wxString& filename)
457814b5 187{
828c8f98 188 return GetTextCtrl()->LoadFile(filename);
457814b5
JS
189}
190
828c8f98 191bool wxTextDocument::IsModified() const
457814b5 192{
828c8f98
FM
193 wxTextCtrl* wnd = GetTextCtrl();
194 return wxDocument::IsModified() || (wnd && wnd->IsModified());
457814b5
JS
195}
196
828c8f98 197void wxTextDocument::Modify(bool modified)
457814b5 198{
2d1df0fc 199 wxDocument::Modify(modified);
958d3a7e 200
828c8f98
FM
201 wxTextCtrl* wnd = GetTextCtrl();
202 if (wnd && !modified)
203 {
204 wnd->DiscardEdits();
205 }
457814b5 206}
6bdf5153 207
828c8f98
FM
208// ----------------------------------------------------------------------------
209// TextEditDocument implementation
210// ----------------------------------------------------------------------------
211
212IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
213
214wxTextCtrl* TextEditDocument::GetTextCtrl() const
6bdf5153 215{
828c8f98
FM
216 wxView* view = GetFirstView();
217 return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
6bdf5153 218}