]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.cpp
Dispatch pending events without waiting for idle time (closes #10994).
[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 178
112d941f
VZ
179bool wxTextDocument::OnCreate(const wxString& path, long flags)
180{
181 if ( !wxDocument::OnCreate(path, flags) )
182 return false;
183
184 // subscribe to changes in the text control to update the document state
185 // when it's modified
186 GetTextCtrl()->Connect
187 (
188 wxEVT_COMMAND_TEXT_UPDATED,
189 wxCommandEventHandler(wxTextDocument::OnTextChange),
190 NULL,
191 this
192 );
193
194 return true;
195}
6d3a0824 196
457814b5 197// Since text windows have their own method for saving to/loading from files,
c9d13e86 198// we override DoSave/OpenDocument instead of Save/LoadObject
828c8f98 199bool wxTextDocument::DoSaveDocument(const wxString& filename)
457814b5 200{
828c8f98 201 return GetTextCtrl()->SaveFile(filename);
457814b5
JS
202}
203
828c8f98 204bool wxTextDocument::DoOpenDocument(const wxString& filename)
457814b5 205{
6d3a0824
VZ
206 if ( !GetTextCtrl()->LoadFile(filename) )
207 return false;
208
209 // we're not modified by the user yet
210 Modify(false);
211
212 return true;
457814b5
JS
213}
214
828c8f98 215bool wxTextDocument::IsModified() const
457814b5 216{
828c8f98
FM
217 wxTextCtrl* wnd = GetTextCtrl();
218 return wxDocument::IsModified() || (wnd && wnd->IsModified());
457814b5
JS
219}
220
828c8f98 221void wxTextDocument::Modify(bool modified)
457814b5 222{
2d1df0fc 223 wxDocument::Modify(modified);
958d3a7e 224
828c8f98
FM
225 wxTextCtrl* wnd = GetTextCtrl();
226 if (wnd && !modified)
227 {
228 wnd->DiscardEdits();
229 }
457814b5 230}
6bdf5153 231
112d941f
VZ
232void wxTextDocument::OnTextChange(wxCommandEvent& event)
233{
234 Modify(true);
235
236 event.Skip();
237}
238
828c8f98
FM
239// ----------------------------------------------------------------------------
240// TextEditDocument implementation
241// ----------------------------------------------------------------------------
242
243IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
244
245wxTextCtrl* TextEditDocument::GetTextCtrl() const
6bdf5153 246{
828c8f98
FM
247 wxView* view = GetFirstView();
248 return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
6bdf5153 249}