]> git.saurik.com Git - wxWidgets.git/blob - samples/docview/doc.h
don't always disable new events code; leave it enabled for MSVC8+
[wxWidgets.git] / samples / docview / doc.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/docview/doc.h
3 // Purpose: Document classes
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_SAMPLES_DOCVIEW_DOC_H_
14 #define _WX_SAMPLES_DOCVIEW_DOC_H_
15
16 #include "wx/docview.h"
17 #include "wx/cmdproc.h"
18 #include "wx/vector.h"
19
20 // This sample is written to build both with wxUSE_STD_IOSTREAM==0 and 1, which
21 // somewhat complicates its code but is necessary in order to support building
22 // it under all platforms and in all build configurations
23 //
24 // In your own code you would normally use std::stream classes only and so
25 // wouldn't need these typedefs
26 #if wxUSE_STD_IOSTREAM
27 typedef wxSTD istream DocumentIstream;
28 typedef wxSTD ostream DocumentOstream;
29 #else // !wxUSE_STD_IOSTREAM
30 typedef wxInputStream DocumentIstream;
31 typedef wxOutputStream DocumentOstream;
32 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
33
34 // ----------------------------------------------------------------------------
35 // The document class and its helpers
36 // ----------------------------------------------------------------------------
37
38 // Represents a line from one point to the other
39 struct DoodleLine
40 {
41 DoodleLine() { /* leave fields uninitialized */ }
42
43 DoodleLine(const wxPoint& pt1, const wxPoint& pt2)
44 : x1(pt1.x), y1(pt1.y), x2(pt2.x), y2(pt2.y)
45 {
46 }
47
48 wxInt32 x1;
49 wxInt32 y1;
50 wxInt32 x2;
51 wxInt32 y2;
52 };
53
54 typedef wxVector<DoodleLine> DoodleLines;
55
56 // Contains a list of lines: represents a mouse-down doodle
57 class DoodleSegment
58 {
59 public:
60 DocumentOstream& SaveObject(DocumentOstream& stream);
61 DocumentIstream& LoadObject(DocumentIstream& stream);
62
63 bool IsEmpty() const { return m_lines.empty(); }
64 void AddLine(const wxPoint& pt1, const wxPoint& pt2)
65 {
66 m_lines.push_back(DoodleLine(pt1, pt2));
67 }
68 const DoodleLines& GetLines() const { return m_lines; }
69
70 private:
71 DoodleLines m_lines;
72 };
73
74 typedef wxVector<DoodleSegment> DoodleSegments;
75
76
77 // The drawing document (model) class itself
78 class DrawingDocument : public wxDocument
79 {
80 public:
81 DrawingDocument() : wxDocument() { }
82
83 DocumentOstream& SaveObject(DocumentOstream& stream);
84 DocumentIstream& LoadObject(DocumentIstream& stream);
85
86 // add a new segment to the document
87 void AddDoodleSegment(const DoodleSegment& segment);
88
89 // remove the last segment, if any, and copy it in the provided pointer if
90 // not NULL and return true or return false and do nothing if there are no
91 // segments
92 bool PopLastSegment(DoodleSegment *segment);
93
94 // get direct access to our segments (for DrawingView)
95 const DoodleSegments& GetSegments() const { return m_doodleSegments; }
96
97 private:
98 void DoUpdate();
99
100 DoodleSegments m_doodleSegments;
101
102 DECLARE_DYNAMIC_CLASS(DrawingDocument)
103 };
104
105
106 // ----------------------------------------------------------------------------
107 // Some operations (which can be done and undone by the view) on the document:
108 // ----------------------------------------------------------------------------
109
110 // Base class for all operations on DrawingDocument
111 class DrawingCommand : public wxCommand
112 {
113 public:
114 DrawingCommand(DrawingDocument *doc,
115 const wxString& name,
116 const DoodleSegment& segment = DoodleSegment())
117 : wxCommand(true, name),
118 m_doc(doc),
119 m_segment(segment)
120 {
121 }
122
123 protected:
124 bool DoAdd() { m_doc->AddDoodleSegment(m_segment); return true; }
125 bool DoRemove() { return m_doc->PopLastSegment(&m_segment); }
126
127 private:
128 DrawingDocument * const m_doc;
129 DoodleSegment m_segment;
130 };
131
132 // The command for adding a new segment
133 class DrawingAddSegmentCommand : public DrawingCommand
134 {
135 public:
136 DrawingAddSegmentCommand(DrawingDocument *doc, const DoodleSegment& segment)
137 : DrawingCommand(doc, "Add new segment", segment)
138 {
139 }
140
141 virtual bool Do() { return DoAdd(); }
142 virtual bool Undo() { return DoRemove(); }
143 };
144
145 // The command for removing the last segment
146 class DrawingRemoveSegmentCommand : public DrawingCommand
147 {
148 public:
149 DrawingRemoveSegmentCommand(DrawingDocument *doc)
150 : DrawingCommand(doc, "Remove last segment")
151 {
152 }
153
154 virtual bool Do() { return DoRemove(); }
155 virtual bool Undo() { return DoAdd(); }
156 };
157
158
159 // ----------------------------------------------------------------------------
160 // wxTextDocument: wxDocument and wxTextCtrl married
161 // ----------------------------------------------------------------------------
162
163 class wxTextDocument : public wxDocument
164 {
165 public:
166 wxTextDocument() : wxDocument() { }
167 virtual wxTextCtrl* GetTextCtrl() const = 0;
168
169 virtual bool IsModified() const;
170 virtual void Modify(bool mod);
171
172 protected:
173 virtual bool DoSaveDocument(const wxString& filename);
174 virtual bool DoOpenDocument(const wxString& filename);
175
176 DECLARE_NO_COPY_CLASS(wxTextDocument)
177 DECLARE_CLASS(wxTextDocument)
178 };
179
180 // ----------------------------------------------------------------------------
181 // A very simple text document class
182 // ----------------------------------------------------------------------------
183
184 class TextEditDocument : public wxTextDocument
185 {
186 public:
187 TextEditDocument() : wxTextDocument() { }
188 virtual wxTextCtrl* GetTextCtrl() const;
189
190 DECLARE_NO_COPY_CLASS(TextEditDocument)
191 DECLARE_DYNAMIC_CLASS(TextEditDocument)
192 };
193
194 #endif // _WX_SAMPLES_DOCVIEW_DOC_H_