]> git.saurik.com Git - wxWidgets.git/blame - samples/docview/doc.h
Use Unix EOL format for the new files.
[wxWidgets.git] / samples / docview / doc.h
CommitLineData
457814b5 1/////////////////////////////////////////////////////////////////////////////
2d1df0fc 2// Name: samples/docview/doc.h
457814b5
JS
3// Purpose: Document classes
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
526954c5 10// Licence: wxWindows licence
457814b5
JS
11/////////////////////////////////////////////////////////////////////////////
12
2d1df0fc
VZ
13#ifndef _WX_SAMPLES_DOCVIEW_DOC_H_
14#define _WX_SAMPLES_DOCVIEW_DOC_H_
457814b5
JS
15
16#include "wx/docview.h"
606b005f 17#include "wx/cmdproc.h"
2d1df0fc 18#include "wx/vector.h"
f37f49b6 19#include "wx/image.h"
2d1df0fc
VZ
20
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
24//
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
34
35// ----------------------------------------------------------------------------
36// The document class and its helpers
37// ----------------------------------------------------------------------------
38
39// Represents a line from one point to the other
40struct DoodleLine
457814b5 41{
2d1df0fc
VZ
42 DoodleLine() { /* leave fields uninitialized */ }
43
44 DoodleLine(const wxPoint& pt1, const wxPoint& pt2)
45 : x1(pt1.x), y1(pt1.y), x2(pt2.x), y2(pt2.y)
46 {
47 }
48
f6bcfd97
BP
49 wxInt32 x1;
50 wxInt32 y1;
51 wxInt32 x2;
52 wxInt32 y2;
457814b5
JS
53};
54
2d1df0fc
VZ
55typedef wxVector<DoodleLine> DoodleLines;
56
457814b5 57// Contains a list of lines: represents a mouse-down doodle
2d1df0fc 58class DoodleSegment
457814b5 59{
f6bcfd97 60public:
2d1df0fc
VZ
61 DocumentOstream& SaveObject(DocumentOstream& stream);
62 DocumentIstream& LoadObject(DocumentIstream& stream);
958d3a7e 63
2d1df0fc
VZ
64 bool IsEmpty() const { return m_lines.empty(); }
65 void AddLine(const wxPoint& pt1, const wxPoint& pt2)
66 {
67 m_lines.push_back(DoodleLine(pt1, pt2));
68 }
69 const DoodleLines& GetLines() const { return m_lines; }
958d3a7e 70
2d1df0fc
VZ
71private:
72 DoodleLines m_lines;
457814b5
JS
73};
74
2d1df0fc
VZ
75typedef wxVector<DoodleSegment> DoodleSegments;
76
77
78// The drawing document (model) class itself
6bdf5153 79class DrawingDocument : public wxDocument
457814b5 80{
f6bcfd97 81public:
2d1df0fc 82 DrawingDocument() : wxDocument() { }
958d3a7e 83
2d1df0fc
VZ
84 DocumentOstream& SaveObject(DocumentOstream& stream);
85 DocumentIstream& LoadObject(DocumentIstream& stream);
958d3a7e 86
2d1df0fc
VZ
87 // add a new segment to the document
88 void AddDoodleSegment(const DoodleSegment& segment);
89
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
92 // segments
93 bool PopLastSegment(DoodleSegment *segment);
94
95 // get direct access to our segments (for DrawingView)
96 const DoodleSegments& GetSegments() const { return m_doodleSegments; }
97
98private:
99 void DoUpdate();
100
101 DoodleSegments m_doodleSegments;
102
103 DECLARE_DYNAMIC_CLASS(DrawingDocument)
457814b5
JS
104};
105
457814b5 106
2d1df0fc
VZ
107// ----------------------------------------------------------------------------
108// Some operations (which can be done and undone by the view) on the document:
109// ----------------------------------------------------------------------------
110
111// Base class for all operations on DrawingDocument
6bdf5153 112class DrawingCommand : public wxCommand
457814b5 113{
2d1df0fc
VZ
114public:
115 DrawingCommand(DrawingDocument *doc,
116 const wxString& name,
117 const DoodleSegment& segment = DoodleSegment())
118 : wxCommand(true, name),
119 m_doc(doc),
120 m_segment(segment)
121 {
122 }
123
f6bcfd97 124protected:
2d1df0fc
VZ
125 bool DoAdd() { m_doc->AddDoodleSegment(m_segment); return true; }
126 bool DoRemove() { return m_doc->PopLastSegment(&m_segment); }
127
128private:
129 DrawingDocument * const m_doc;
130 DoodleSegment m_segment;
131};
132
133// The command for adding a new segment
134class DrawingAddSegmentCommand : public DrawingCommand
135{
136public:
137 DrawingAddSegmentCommand(DrawingDocument *doc, const DoodleSegment& segment)
138 : DrawingCommand(doc, "Add new segment", segment)
139 {
140 }
141
142 virtual bool Do() { return DoAdd(); }
143 virtual bool Undo() { return DoRemove(); }
144};
145
146// The command for removing the last segment
147class DrawingRemoveSegmentCommand : public DrawingCommand
148{
f6bcfd97 149public:
2d1df0fc
VZ
150 DrawingRemoveSegmentCommand(DrawingDocument *doc)
151 : DrawingCommand(doc, "Remove last segment")
152 {
153 }
958d3a7e 154
2d1df0fc
VZ
155 virtual bool Do() { return DoRemove(); }
156 virtual bool Undo() { return DoAdd(); }
457814b5
JS
157};
158
2d1df0fc
VZ
159
160// ----------------------------------------------------------------------------
828c8f98 161// wxTextDocument: wxDocument and wxTextCtrl married
2d1df0fc
VZ
162// ----------------------------------------------------------------------------
163
828c8f98 164class wxTextDocument : public wxDocument
457814b5 165{
f6bcfd97 166public:
828c8f98 167 wxTextDocument() : wxDocument() { }
112d941f
VZ
168
169 virtual bool OnCreate(const wxString& path, long flags);
170
828c8f98 171 virtual wxTextCtrl* GetTextCtrl() const = 0;
6bdf5153 172
2d1df0fc 173 virtual bool IsModified() const;
f6bcfd97 174 virtual void Modify(bool mod);
457814b5 175
828c8f98
FM
176protected:
177 virtual bool DoSaveDocument(const wxString& filename);
178 virtual bool DoOpenDocument(const wxString& filename);
179
112d941f
VZ
180 void OnTextChange(wxCommandEvent& event);
181
c0c133e1 182 wxDECLARE_NO_COPY_CLASS(wxTextDocument);
828c8f98
FM
183 DECLARE_CLASS(wxTextDocument)
184};
185
186// ----------------------------------------------------------------------------
187// A very simple text document class
188// ----------------------------------------------------------------------------
189
190class TextEditDocument : public wxTextDocument
191{
192public:
193 TextEditDocument() : wxTextDocument() { }
194 virtual wxTextCtrl* GetTextCtrl() const;
195
c0c133e1 196 wxDECLARE_NO_COPY_CLASS(TextEditDocument);
2d1df0fc
VZ
197 DECLARE_DYNAMIC_CLASS(TextEditDocument)
198};
457814b5 199
f37f49b6 200// ----------------------------------------------------------------------------
2d4a03f8 201// A document class representing an image
f37f49b6
JS
202// ----------------------------------------------------------------------------
203
2d4a03f8 204class ImageDocument : public wxDocument
f37f49b6 205{
f37f49b6 206public:
2d4a03f8 207 ImageDocument() : wxDocument() { }
f37f49b6 208
2d4a03f8 209 wxImage GetImage() const { return m_image; }
f37f49b6 210
2d4a03f8 211protected:
f37f49b6 212 virtual bool DoOpenDocument(const wxString& file);
f37f49b6 213
2d4a03f8
VZ
214private:
215 wxImage m_image;
f37f49b6 216
2d4a03f8
VZ
217 wxDECLARE_NO_COPY_CLASS(ImageDocument);
218 DECLARE_DYNAMIC_CLASS(ImageDocument)
f37f49b6
JS
219};
220
2d1df0fc 221#endif // _WX_SAMPLES_DOCVIEW_DOC_H_