]>
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 | ||
47 | DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& stream) | |
457814b5 | 48 | { |
f6bcfd97 | 49 | wxDocument::SaveObject(stream); |
958d3a7e | 50 | |
2d1df0fc VZ |
51 | const wxInt32 count = m_doodleSegments.size(); |
52 | stream << count << '\n'; | |
958d3a7e | 53 | |
2d1df0fc | 54 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 55 | { |
2d1df0fc | 56 | m_doodleSegments[n].SaveObject(stream); |
f6bcfd97 | 57 | stream << '\n'; |
f6bcfd97 | 58 | } |
958d3a7e | 59 | |
f6bcfd97 | 60 | return stream; |
457814b5 | 61 | } |
958d3a7e | 62 | |
2d1df0fc VZ |
63 | DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& stream) |
64 | { | |
65 | wxDocument::LoadObject(stream); | |
958d3a7e | 66 | |
2d1df0fc VZ |
67 | wxInt32 count = 0; |
68 | stream >> count; | |
958d3a7e | 69 | |
2d1df0fc | 70 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 71 | { |
2d1df0fc VZ |
72 | DoodleSegment segment; |
73 | segment.LoadObject(stream); | |
74 | m_doodleSegments.push_back(segment); | |
f6bcfd97 | 75 | } |
958d3a7e | 76 | |
f6bcfd97 | 77 | return stream; |
56d7679d | 78 | } |
56d7679d | 79 | |
2d1df0fc | 80 | void DrawingDocument::DoUpdate() |
457814b5 | 81 | { |
2d1df0fc VZ |
82 | Modify(true); |
83 | UpdateAllViews(); | |
84 | } | |
958d3a7e | 85 | |
2d1df0fc VZ |
86 | void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment) |
87 | { | |
88 | m_doodleSegments.push_back(segment); | |
958d3a7e | 89 | |
2d1df0fc | 90 | DoUpdate(); |
457814b5 | 91 | } |
2d1df0fc VZ |
92 | |
93 | bool DrawingDocument::PopLastSegment(DoodleSegment *segment) | |
56d7679d | 94 | { |
2d1df0fc VZ |
95 | if ( m_doodleSegments.empty() ) |
96 | return false; | |
958d3a7e | 97 | |
2d1df0fc VZ |
98 | if ( segment ) |
99 | *segment = m_doodleSegments.back(); | |
958d3a7e | 100 | |
2d1df0fc | 101 | m_doodleSegments.pop_back(); |
958d3a7e | 102 | |
2d1df0fc | 103 | DoUpdate(); |
958d3a7e | 104 | |
2d1df0fc | 105 | return true; |
56d7679d | 106 | } |
958d3a7e | 107 | |
2d1df0fc VZ |
108 | // ---------------------------------------------------------------------------- |
109 | // DoodleSegment implementation | |
110 | // ---------------------------------------------------------------------------- | |
457814b5 | 111 | |
2d1df0fc | 112 | DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream) |
457814b5 | 113 | { |
56d7679d | 114 | #if wxUSE_STD_IOSTREAM |
2d1df0fc | 115 | DocumentOstream& stream = ostream; |
56d7679d | 116 | #else |
2d1df0fc VZ |
117 | wxTextOutputStream stream(ostream); |
118 | #endif | |
958d3a7e | 119 | |
2d1df0fc VZ |
120 | const wxInt32 count = m_lines.size(); |
121 | stream << count << '\n'; | |
958d3a7e | 122 | |
2d1df0fc | 123 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 124 | { |
2d1df0fc VZ |
125 | const DoodleLine& line = m_lines[n]; |
126 | stream | |
127 | << line.x1 << ' ' | |
128 | << line.y1 << ' ' | |
129 | << line.x2 << ' ' | |
130 | << line.y2 << '\n'; | |
f6bcfd97 | 131 | } |
958d3a7e | 132 | |
f6bcfd97 | 133 | return stream; |
56d7679d | 134 | } |
457814b5 | 135 | |
2d1df0fc | 136 | DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream) |
457814b5 | 137 | { |
2d1df0fc VZ |
138 | #if wxUSE_STD_IOSTREAM |
139 | DocumentIstream& stream = istream; | |
56d7679d | 140 | #else |
2d1df0fc VZ |
141 | wxTextInputStream stream(istream); |
142 | #endif | |
958d3a7e | 143 | |
2d1df0fc VZ |
144 | wxInt32 count = 0; |
145 | stream >> count; | |
958d3a7e | 146 | |
2d1df0fc | 147 | for ( int n = 0; n < count; n++ ) |
f6bcfd97 | 148 | { |
2d1df0fc VZ |
149 | DoodleLine line; |
150 | stream | |
151 | >> line.x1 | |
152 | >> line.y1 | |
153 | >> line.x2 | |
154 | >> line.y2; | |
155 | m_lines.push_back(line); | |
f6bcfd97 | 156 | } |
958d3a7e | 157 | |
f6bcfd97 | 158 | return stream; |
56d7679d | 159 | } |
457814b5 | 160 | |
2d1df0fc VZ |
161 | // ---------------------------------------------------------------------------- |
162 | // TextEditDocument implementation | |
163 | // ---------------------------------------------------------------------------- | |
457814b5 JS |
164 | |
165 | IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument) | |
166 | ||
167 | // Since text windows have their own method for saving to/loading from files, | |
c9d13e86 | 168 | // we override DoSave/OpenDocument instead of Save/LoadObject |
1c6dca77 | 169 | bool TextEditDocument::DoSaveDocument(const wxString& filename) |
457814b5 | 170 | { |
2d1df0fc | 171 | return GetFirstView()->GetText()->SaveFile(filename); |
457814b5 JS |
172 | } |
173 | ||
1c6dca77 | 174 | bool TextEditDocument::DoOpenDocument(const wxString& filename) |
457814b5 | 175 | { |
2d1df0fc | 176 | return GetFirstView()->GetText()->LoadFile(filename); |
457814b5 JS |
177 | } |
178 | ||
2d1df0fc | 179 | bool TextEditDocument::IsModified() const |
457814b5 | 180 | { |
6bdf5153 | 181 | TextEditView* view = GetFirstView(); |
2d1df0fc | 182 | return wxDocument::IsModified() || (view && view->GetText()->IsModified()); |
457814b5 JS |
183 | } |
184 | ||
2d1df0fc | 185 | void TextEditDocument::Modify(bool modified) |
457814b5 | 186 | { |
6bdf5153 | 187 | TextEditView* view = GetFirstView(); |
958d3a7e | 188 | |
2d1df0fc | 189 | wxDocument::Modify(modified); |
958d3a7e | 190 | |
2d1df0fc VZ |
191 | if ( !modified && view && view->GetText() ) |
192 | view->GetText()->DiscardEdits(); | |
457814b5 | 193 | } |
6bdf5153 VZ |
194 | |
195 | TextEditView* TextEditDocument::GetFirstView() const | |
196 | { | |
2d1df0fc VZ |
197 | wxView* view = wxDocument::GetFirstView(); |
198 | return view ? wxStaticCast(view, TextEditView) : NULL; | |
6bdf5153 VZ |
199 | } |
200 |