]>
Commit | Line | Data |
---|---|---|
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 |
45 | IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument) |
46 | ||
00a1c49a | 47 | DocumentOstream& 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 | 69 | DocumentIstream& 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 | 92 | void DrawingDocument::DoUpdate() |
457814b5 | 93 | { |
2d1df0fc VZ |
94 | Modify(true); |
95 | UpdateAllViews(); | |
96 | } | |
958d3a7e | 97 | |
2d1df0fc VZ |
98 | void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment) |
99 | { | |
100 | m_doodleSegments.push_back(segment); | |
958d3a7e | 101 | |
2d1df0fc | 102 | DoUpdate(); |
457814b5 | 103 | } |
2d1df0fc VZ |
104 | |
105 | bool 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 | 124 | DocumentOstream& 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 | 148 | DocumentIstream& 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 VZ |
173 | // ---------------------------------------------------------------------------- |
174 | // TextEditDocument implementation | |
175 | // ---------------------------------------------------------------------------- | |
457814b5 JS |
176 | |
177 | IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument) | |
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 |
1c6dca77 | 181 | bool TextEditDocument::DoSaveDocument(const wxString& filename) |
457814b5 | 182 | { |
2d1df0fc | 183 | return GetFirstView()->GetText()->SaveFile(filename); |
457814b5 JS |
184 | } |
185 | ||
1c6dca77 | 186 | bool TextEditDocument::DoOpenDocument(const wxString& filename) |
457814b5 | 187 | { |
2d1df0fc | 188 | return GetFirstView()->GetText()->LoadFile(filename); |
457814b5 JS |
189 | } |
190 | ||
2d1df0fc | 191 | bool TextEditDocument::IsModified() const |
457814b5 | 192 | { |
6bdf5153 | 193 | TextEditView* view = GetFirstView(); |
2d1df0fc | 194 | return wxDocument::IsModified() || (view && view->GetText()->IsModified()); |
457814b5 JS |
195 | } |
196 | ||
2d1df0fc | 197 | void TextEditDocument::Modify(bool modified) |
457814b5 | 198 | { |
6bdf5153 | 199 | TextEditView* view = GetFirstView(); |
958d3a7e | 200 | |
2d1df0fc | 201 | wxDocument::Modify(modified); |
958d3a7e | 202 | |
2d1df0fc VZ |
203 | if ( !modified && view && view->GetText() ) |
204 | view->GetText()->DiscardEdits(); | |
457814b5 | 205 | } |
6bdf5153 VZ |
206 | |
207 | TextEditView* TextEditDocument::GetFirstView() const | |
208 | { | |
2d1df0fc VZ |
209 | wxView* view = wxDocument::GetFirstView(); |
210 | return view ? wxStaticCast(view, TextEditView) : NULL; | |
6bdf5153 VZ |
211 | } |
212 |