doc.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // Copyright: (c) 1998 Julian Smart
  8. // (c) 2008 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_SAMPLES_DOCVIEW_DOC_H_
  12. #define _WX_SAMPLES_DOCVIEW_DOC_H_
  13. #include "wx/docview.h"
  14. #include "wx/cmdproc.h"
  15. #include "wx/vector.h"
  16. #include "wx/image.h"
  17. // This sample is written to build both with wxUSE_STD_IOSTREAM==0 and 1, which
  18. // somewhat complicates its code but is necessary in order to support building
  19. // it under all platforms and in all build configurations
  20. //
  21. // In your own code you would normally use std::stream classes only and so
  22. // wouldn't need these typedefs
  23. #if wxUSE_STD_IOSTREAM
  24. typedef wxSTD istream DocumentIstream;
  25. typedef wxSTD ostream DocumentOstream;
  26. #else // !wxUSE_STD_IOSTREAM
  27. typedef wxInputStream DocumentIstream;
  28. typedef wxOutputStream DocumentOstream;
  29. #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
  30. // ----------------------------------------------------------------------------
  31. // The document class and its helpers
  32. // ----------------------------------------------------------------------------
  33. // Represents a line from one point to the other
  34. struct DoodleLine
  35. {
  36. DoodleLine() { /* leave fields uninitialized */ }
  37. DoodleLine(const wxPoint& pt1, const wxPoint& pt2)
  38. : x1(pt1.x), y1(pt1.y), x2(pt2.x), y2(pt2.y)
  39. {
  40. }
  41. wxInt32 x1;
  42. wxInt32 y1;
  43. wxInt32 x2;
  44. wxInt32 y2;
  45. };
  46. typedef wxVector<DoodleLine> DoodleLines;
  47. // Contains a list of lines: represents a mouse-down doodle
  48. class DoodleSegment
  49. {
  50. public:
  51. DocumentOstream& SaveObject(DocumentOstream& stream);
  52. DocumentIstream& LoadObject(DocumentIstream& stream);
  53. bool IsEmpty() const { return m_lines.empty(); }
  54. void AddLine(const wxPoint& pt1, const wxPoint& pt2)
  55. {
  56. m_lines.push_back(DoodleLine(pt1, pt2));
  57. }
  58. const DoodleLines& GetLines() const { return m_lines; }
  59. private:
  60. DoodleLines m_lines;
  61. };
  62. typedef wxVector<DoodleSegment> DoodleSegments;
  63. // The drawing document (model) class itself
  64. class DrawingDocument : public wxDocument
  65. {
  66. public:
  67. DrawingDocument() : wxDocument() { }
  68. DocumentOstream& SaveObject(DocumentOstream& stream);
  69. DocumentIstream& LoadObject(DocumentIstream& stream);
  70. // add a new segment to the document
  71. void AddDoodleSegment(const DoodleSegment& segment);
  72. // remove the last segment, if any, and copy it in the provided pointer if
  73. // not NULL and return true or return false and do nothing if there are no
  74. // segments
  75. bool PopLastSegment(DoodleSegment *segment);
  76. // get direct access to our segments (for DrawingView)
  77. const DoodleSegments& GetSegments() const { return m_doodleSegments; }
  78. private:
  79. void DoUpdate();
  80. DoodleSegments m_doodleSegments;
  81. wxDECLARE_DYNAMIC_CLASS(DrawingDocument);
  82. };
  83. // ----------------------------------------------------------------------------
  84. // Some operations (which can be done and undone by the view) on the document:
  85. // ----------------------------------------------------------------------------
  86. // Base class for all operations on DrawingDocument
  87. class DrawingCommand : public wxCommand
  88. {
  89. public:
  90. DrawingCommand(DrawingDocument *doc,
  91. const wxString& name,
  92. const DoodleSegment& segment = DoodleSegment())
  93. : wxCommand(true, name),
  94. m_doc(doc),
  95. m_segment(segment)
  96. {
  97. }
  98. protected:
  99. bool DoAdd() { m_doc->AddDoodleSegment(m_segment); return true; }
  100. bool DoRemove() { return m_doc->PopLastSegment(&m_segment); }
  101. private:
  102. DrawingDocument * const m_doc;
  103. DoodleSegment m_segment;
  104. };
  105. // The command for adding a new segment
  106. class DrawingAddSegmentCommand : public DrawingCommand
  107. {
  108. public:
  109. DrawingAddSegmentCommand(DrawingDocument *doc, const DoodleSegment& segment)
  110. : DrawingCommand(doc, "Add new segment", segment)
  111. {
  112. }
  113. virtual bool Do() { return DoAdd(); }
  114. virtual bool Undo() { return DoRemove(); }
  115. };
  116. // The command for removing the last segment
  117. class DrawingRemoveSegmentCommand : public DrawingCommand
  118. {
  119. public:
  120. DrawingRemoveSegmentCommand(DrawingDocument *doc)
  121. : DrawingCommand(doc, "Remove last segment")
  122. {
  123. }
  124. virtual bool Do() { return DoRemove(); }
  125. virtual bool Undo() { return DoAdd(); }
  126. };
  127. // ----------------------------------------------------------------------------
  128. // wxTextDocument: wxDocument and wxTextCtrl married
  129. // ----------------------------------------------------------------------------
  130. class wxTextDocument : public wxDocument
  131. {
  132. public:
  133. wxTextDocument() : wxDocument() { }
  134. virtual bool OnCreate(const wxString& path, long flags);
  135. virtual wxTextCtrl* GetTextCtrl() const = 0;
  136. virtual bool IsModified() const;
  137. virtual void Modify(bool mod);
  138. protected:
  139. virtual bool DoSaveDocument(const wxString& filename);
  140. virtual bool DoOpenDocument(const wxString& filename);
  141. void OnTextChange(wxCommandEvent& event);
  142. wxDECLARE_NO_COPY_CLASS(wxTextDocument);
  143. wxDECLARE_ABSTRACT_CLASS(wxTextDocument);
  144. };
  145. // ----------------------------------------------------------------------------
  146. // A very simple text document class
  147. // ----------------------------------------------------------------------------
  148. class TextEditDocument : public wxTextDocument
  149. {
  150. public:
  151. TextEditDocument() : wxTextDocument() { }
  152. virtual wxTextCtrl* GetTextCtrl() const;
  153. wxDECLARE_NO_COPY_CLASS(TextEditDocument);
  154. wxDECLARE_DYNAMIC_CLASS(TextEditDocument);
  155. };
  156. // ----------------------------------------------------------------------------
  157. // Image and image details document classes (both are read-only for simplicity)
  158. // ----------------------------------------------------------------------------
  159. // This is a normal document containing an image, just like TextEditDocument
  160. // above contains some text. It can be created from an image file on disk as
  161. // usual.
  162. class ImageDocument : public wxDocument
  163. {
  164. public:
  165. ImageDocument() : wxDocument() { }
  166. virtual bool OnOpenDocument(const wxString& file);
  167. wxImage GetImage() const { return m_image; }
  168. protected:
  169. virtual bool DoOpenDocument(const wxString& file);
  170. private:
  171. wxImage m_image;
  172. wxDECLARE_NO_COPY_CLASS(ImageDocument);
  173. wxDECLARE_DYNAMIC_CLASS(ImageDocument);
  174. };
  175. // This is a child document of ImageDocument: this document doesn't
  176. // correspond to any file on disk, it's part of ImageDocument and can't be
  177. // instantiated independently of it.
  178. class ImageDetailsDocument : public wxDocument
  179. {
  180. public:
  181. ImageDetailsDocument(ImageDocument *parent);
  182. // accessors for ImageDetailsView
  183. wxSize GetSize() const { return m_size; }
  184. unsigned long GetNumColours() const { return m_numColours; }
  185. wxBitmapType GetType() const { return m_type; }
  186. bool HasAlpha() const { return m_hasAlpha; }
  187. private:
  188. // some information about the image we choose to show to the user
  189. wxSize m_size;
  190. unsigned long m_numColours;
  191. wxBitmapType m_type;
  192. bool m_hasAlpha;
  193. wxDECLARE_NO_COPY_CLASS(ImageDetailsDocument);
  194. };
  195. #endif // _WX_SAMPLES_DOCVIEW_DOC_H_