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
8 // Copyright: (c) 1998 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_SAMPLES_DOCVIEW_DOC_H_
14 #define _WX_SAMPLES_DOCVIEW_DOC_H_
16 #include "wx/docview.h"
17 #include "wx/cmdproc.h"
18 #include "wx/vector.h"
21 // This sample is written to build both with wxUSE_STD_IOSTREAM==0 and 1, which
22 // somewhat complicates its code but is necessary in order to support building
23 // it under all platforms and in all build configurations
25 // In your own code you would normally use std::stream classes only and so
26 // wouldn't need these typedefs
27 #if wxUSE_STD_IOSTREAM
28 typedef wxSTD istream DocumentIstream
;
29 typedef wxSTD ostream DocumentOstream
;
30 #else // !wxUSE_STD_IOSTREAM
31 typedef wxInputStream DocumentIstream
;
32 typedef wxOutputStream DocumentOstream
;
33 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
35 // ----------------------------------------------------------------------------
36 // The document class and its helpers
37 // ----------------------------------------------------------------------------
39 // Represents a line from one point to the other
42 DoodleLine() { /* leave fields uninitialized */ }
44 DoodleLine(const wxPoint
& pt1
, const wxPoint
& pt2
)
45 : x1(pt1
.x
), y1(pt1
.y
), x2(pt2
.x
), y2(pt2
.y
)
55 typedef wxVector
<DoodleLine
> DoodleLines
;
57 // Contains a list of lines: represents a mouse-down doodle
61 DocumentOstream
& SaveObject(DocumentOstream
& stream
);
62 DocumentIstream
& LoadObject(DocumentIstream
& stream
);
64 bool IsEmpty() const { return m_lines
.empty(); }
65 void AddLine(const wxPoint
& pt1
, const wxPoint
& pt2
)
67 m_lines
.push_back(DoodleLine(pt1
, pt2
));
69 const DoodleLines
& GetLines() const { return m_lines
; }
75 typedef wxVector
<DoodleSegment
> DoodleSegments
;
78 // The drawing document (model) class itself
79 class DrawingDocument
: public wxDocument
82 DrawingDocument() : wxDocument() { }
84 DocumentOstream
& SaveObject(DocumentOstream
& stream
);
85 DocumentIstream
& LoadObject(DocumentIstream
& stream
);
87 // add a new segment to the document
88 void AddDoodleSegment(const DoodleSegment
& segment
);
90 // remove the last segment, if any, and copy it in the provided pointer if
91 // not NULL and return true or return false and do nothing if there are no
93 bool PopLastSegment(DoodleSegment
*segment
);
95 // get direct access to our segments (for DrawingView)
96 const DoodleSegments
& GetSegments() const { return m_doodleSegments
; }
101 DoodleSegments m_doodleSegments
;
103 DECLARE_DYNAMIC_CLASS(DrawingDocument
)
107 // ----------------------------------------------------------------------------
108 // Some operations (which can be done and undone by the view) on the document:
109 // ----------------------------------------------------------------------------
111 // Base class for all operations on DrawingDocument
112 class DrawingCommand
: public wxCommand
115 DrawingCommand(DrawingDocument
*doc
,
116 const wxString
& name
,
117 const DoodleSegment
& segment
= DoodleSegment())
118 : wxCommand(true, name
),
125 bool DoAdd() { m_doc
->AddDoodleSegment(m_segment
); return true; }
126 bool DoRemove() { return m_doc
->PopLastSegment(&m_segment
); }
129 DrawingDocument
* const m_doc
;
130 DoodleSegment m_segment
;
133 // The command for adding a new segment
134 class DrawingAddSegmentCommand
: public DrawingCommand
137 DrawingAddSegmentCommand(DrawingDocument
*doc
, const DoodleSegment
& segment
)
138 : DrawingCommand(doc
, "Add new segment", segment
)
142 virtual bool Do() { return DoAdd(); }
143 virtual bool Undo() { return DoRemove(); }
146 // The command for removing the last segment
147 class DrawingRemoveSegmentCommand
: public DrawingCommand
150 DrawingRemoveSegmentCommand(DrawingDocument
*doc
)
151 : DrawingCommand(doc
, "Remove last segment")
155 virtual bool Do() { return DoRemove(); }
156 virtual bool Undo() { return DoAdd(); }
160 // ----------------------------------------------------------------------------
161 // wxTextDocument: wxDocument and wxTextCtrl married
162 // ----------------------------------------------------------------------------
164 class wxTextDocument
: public wxDocument
167 wxTextDocument() : wxDocument() { }
169 virtual bool OnCreate(const wxString
& path
, long flags
);
171 virtual wxTextCtrl
* GetTextCtrl() const = 0;
173 virtual bool IsModified() const;
174 virtual void Modify(bool mod
);
177 virtual bool DoSaveDocument(const wxString
& filename
);
178 virtual bool DoOpenDocument(const wxString
& filename
);
180 void OnTextChange(wxCommandEvent
& event
);
182 wxDECLARE_NO_COPY_CLASS(wxTextDocument
);
183 DECLARE_CLASS(wxTextDocument
)
186 // ----------------------------------------------------------------------------
187 // A very simple text document class
188 // ----------------------------------------------------------------------------
190 class TextEditDocument
: public wxTextDocument
193 TextEditDocument() : wxTextDocument() { }
194 virtual wxTextCtrl
* GetTextCtrl() const;
196 wxDECLARE_NO_COPY_CLASS(TextEditDocument
);
197 DECLARE_DYNAMIC_CLASS(TextEditDocument
)
200 // ----------------------------------------------------------------------------
201 // A document class representing an image
202 // ----------------------------------------------------------------------------
204 class ImageDocument
: public wxDocument
207 ImageDocument() : wxDocument() { }
209 wxImage
GetImage() const { return m_image
; }
212 virtual bool DoOpenDocument(const wxString
& file
);
217 wxDECLARE_NO_COPY_CLASS(ImageDocument
);
218 DECLARE_DYNAMIC_CLASS(ImageDocument
)
221 #endif // _WX_SAMPLES_DOCVIEW_DOC_H_