]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/docview/doc.h
Fix, or at least make less common, deadlock in the thread sample.
[wxWidgets.git] / samples / docview / doc.h
... / ...
CommitLineData
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 licence
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#include "wx/image.h"
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
41{
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
49 wxInt32 x1;
50 wxInt32 y1;
51 wxInt32 x2;
52 wxInt32 y2;
53};
54
55typedef wxVector<DoodleLine> DoodleLines;
56
57// Contains a list of lines: represents a mouse-down doodle
58class DoodleSegment
59{
60public:
61 DocumentOstream& SaveObject(DocumentOstream& stream);
62 DocumentIstream& LoadObject(DocumentIstream& stream);
63
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; }
70
71private:
72 DoodleLines m_lines;
73};
74
75typedef wxVector<DoodleSegment> DoodleSegments;
76
77
78// The drawing document (model) class itself
79class DrawingDocument : public wxDocument
80{
81public:
82 DrawingDocument() : wxDocument() { }
83
84 DocumentOstream& SaveObject(DocumentOstream& stream);
85 DocumentIstream& LoadObject(DocumentIstream& stream);
86
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)
104};
105
106
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
112class DrawingCommand : public wxCommand
113{
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
124protected:
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{
149public:
150 DrawingRemoveSegmentCommand(DrawingDocument *doc)
151 : DrawingCommand(doc, "Remove last segment")
152 {
153 }
154
155 virtual bool Do() { return DoRemove(); }
156 virtual bool Undo() { return DoAdd(); }
157};
158
159
160// ----------------------------------------------------------------------------
161// wxTextDocument: wxDocument and wxTextCtrl married
162// ----------------------------------------------------------------------------
163
164class wxTextDocument : public wxDocument
165{
166public:
167 wxTextDocument() : wxDocument() { }
168
169 virtual bool OnCreate(const wxString& path, long flags);
170
171 virtual wxTextCtrl* GetTextCtrl() const = 0;
172
173 virtual bool IsModified() const;
174 virtual void Modify(bool mod);
175
176protected:
177 virtual bool DoSaveDocument(const wxString& filename);
178 virtual bool DoOpenDocument(const wxString& filename);
179
180 void OnTextChange(wxCommandEvent& event);
181
182 wxDECLARE_NO_COPY_CLASS(wxTextDocument);
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
196 wxDECLARE_NO_COPY_CLASS(TextEditDocument);
197 DECLARE_DYNAMIC_CLASS(TextEditDocument)
198};
199
200// ----------------------------------------------------------------------------
201// Image and image details document classes (both are read-only for simplicity)
202// ----------------------------------------------------------------------------
203
204// This is a normal document containing an image, just like TextEditDocument
205// above contains some text. It can be created from an image file on disk as
206// usual.
207class ImageDocument : public wxDocument
208{
209public:
210 ImageDocument() : wxDocument() { }
211
212 virtual bool OnOpenDocument(const wxString& file);
213
214 wxImage GetImage() const { return m_image; }
215
216protected:
217 virtual bool DoOpenDocument(const wxString& file);
218
219private:
220 wxImage m_image;
221
222 wxDECLARE_NO_COPY_CLASS(ImageDocument);
223 DECLARE_DYNAMIC_CLASS(ImageDocument)
224};
225
226// This is a child document of ImageDocument: this document doesn't
227// correspond to any file on disk, it's part of ImageDocument and can't be
228// instantiated independently of it.
229class ImageDetailsDocument : public wxDocument
230{
231public:
232 ImageDetailsDocument(ImageDocument *parent);
233
234 // accessors for ImageDetailsView
235 wxSize GetSize() const { return m_size; }
236 unsigned long GetNumColours() const { return m_numColours; }
237 wxBitmapType GetType() const { return m_type; }
238 bool HasAlpha() const { return m_hasAlpha; }
239
240private:
241 // some information about the image we choose to show to the user
242 wxSize m_size;
243 unsigned long m_numColours;
244 wxBitmapType m_type;
245 bool m_hasAlpha;
246
247 wxDECLARE_NO_COPY_CLASS(ImageDetailsDocument);
248};
249
250#endif // _WX_SAMPLES_DOCVIEW_DOC_H_