doc.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/docview/doc.cpp
  3. // Purpose: Implements document functionality
  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. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. // For compilers that support precompilation, includes "wx/wx.h".
  15. #include "wx/wxprec.h"
  16. #ifdef __BORLANDC__
  17. #pragma hdrstop
  18. #endif
  19. #if !wxUSE_DOC_VIEW_ARCHITECTURE
  20. #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
  21. #endif
  22. #ifndef WX_PRECOMP
  23. #include "wx/wx.h"
  24. #endif
  25. #if wxUSE_STD_IOSTREAM
  26. #include "wx/ioswrap.h"
  27. #else
  28. #include "wx/txtstrm.h"
  29. #endif
  30. #include "wx/wfstream.h"
  31. #include "doc.h"
  32. #include "view.h"
  33. // ----------------------------------------------------------------------------
  34. // DrawingDocument implementation
  35. // ----------------------------------------------------------------------------
  36. IMPLEMENT_DYNAMIC_CLASS(DrawingDocument, wxDocument)
  37. DocumentOstream& DrawingDocument::SaveObject(DocumentOstream& ostream)
  38. {
  39. #if wxUSE_STD_IOSTREAM
  40. DocumentOstream& stream = ostream;
  41. #else
  42. wxTextOutputStream stream(ostream);
  43. #endif
  44. wxDocument::SaveObject(ostream);
  45. const wxInt32 count = m_doodleSegments.size();
  46. stream << count << '\n';
  47. for ( int n = 0; n < count; n++ )
  48. {
  49. m_doodleSegments[n].SaveObject(ostream);
  50. stream << '\n';
  51. }
  52. return ostream;
  53. }
  54. DocumentIstream& DrawingDocument::LoadObject(DocumentIstream& istream)
  55. {
  56. #if wxUSE_STD_IOSTREAM
  57. DocumentIstream& stream = istream;
  58. #else
  59. wxTextInputStream stream(istream);
  60. #endif
  61. wxDocument::LoadObject(istream);
  62. wxInt32 count = 0;
  63. stream >> count;
  64. if ( count < 0 )
  65. {
  66. wxLogWarning("Drawing document corrupted: invalid segments count.");
  67. #if wxUSE_STD_IOSTREAM
  68. istream.clear(std::ios::badbit);
  69. #else
  70. istream.Reset(wxSTREAM_READ_ERROR);
  71. #endif
  72. return istream;
  73. }
  74. for ( int n = 0; n < count; n++ )
  75. {
  76. DoodleSegment segment;
  77. segment.LoadObject(istream);
  78. m_doodleSegments.push_back(segment);
  79. }
  80. return istream;
  81. }
  82. void DrawingDocument::DoUpdate()
  83. {
  84. Modify(true);
  85. UpdateAllViews();
  86. }
  87. void DrawingDocument::AddDoodleSegment(const DoodleSegment& segment)
  88. {
  89. m_doodleSegments.push_back(segment);
  90. DoUpdate();
  91. }
  92. bool DrawingDocument::PopLastSegment(DoodleSegment *segment)
  93. {
  94. if ( m_doodleSegments.empty() )
  95. return false;
  96. if ( segment )
  97. *segment = m_doodleSegments.back();
  98. m_doodleSegments.pop_back();
  99. DoUpdate();
  100. return true;
  101. }
  102. // ----------------------------------------------------------------------------
  103. // DoodleSegment implementation
  104. // ----------------------------------------------------------------------------
  105. DocumentOstream& DoodleSegment::SaveObject(DocumentOstream& ostream)
  106. {
  107. #if wxUSE_STD_IOSTREAM
  108. DocumentOstream& stream = ostream;
  109. #else
  110. wxTextOutputStream stream(ostream);
  111. #endif
  112. const wxInt32 count = m_lines.size();
  113. stream << count << '\n';
  114. for ( int n = 0; n < count; n++ )
  115. {
  116. const DoodleLine& line = m_lines[n];
  117. stream
  118. << line.x1 << ' '
  119. << line.y1 << ' '
  120. << line.x2 << ' '
  121. << line.y2 << '\n';
  122. }
  123. return ostream;
  124. }
  125. DocumentIstream& DoodleSegment::LoadObject(DocumentIstream& istream)
  126. {
  127. #if wxUSE_STD_IOSTREAM
  128. DocumentIstream& stream = istream;
  129. #else
  130. wxTextInputStream stream(istream);
  131. #endif
  132. wxInt32 count = 0;
  133. stream >> count;
  134. for ( int n = 0; n < count; n++ )
  135. {
  136. DoodleLine line;
  137. stream
  138. >> line.x1
  139. >> line.y1
  140. >> line.x2
  141. >> line.y2;
  142. m_lines.push_back(line);
  143. }
  144. return istream;
  145. }
  146. // ----------------------------------------------------------------------------
  147. // wxTextDocument: wxDocument and wxTextCtrl married
  148. // ----------------------------------------------------------------------------
  149. IMPLEMENT_CLASS(wxTextDocument, wxDocument)
  150. bool wxTextDocument::OnCreate(const wxString& path, long flags)
  151. {
  152. if ( !wxDocument::OnCreate(path, flags) )
  153. return false;
  154. // subscribe to changes in the text control to update the document state
  155. // when it's modified
  156. GetTextCtrl()->Connect
  157. (
  158. wxEVT_TEXT,
  159. wxCommandEventHandler(wxTextDocument::OnTextChange),
  160. NULL,
  161. this
  162. );
  163. return true;
  164. }
  165. // Since text windows have their own method for saving to/loading from files,
  166. // we override DoSave/OpenDocument instead of Save/LoadObject
  167. bool wxTextDocument::DoSaveDocument(const wxString& filename)
  168. {
  169. return GetTextCtrl()->SaveFile(filename);
  170. }
  171. bool wxTextDocument::DoOpenDocument(const wxString& filename)
  172. {
  173. if ( !GetTextCtrl()->LoadFile(filename) )
  174. return false;
  175. // we're not modified by the user yet
  176. Modify(false);
  177. return true;
  178. }
  179. bool wxTextDocument::IsModified() const
  180. {
  181. wxTextCtrl* wnd = GetTextCtrl();
  182. return wxDocument::IsModified() || (wnd && wnd->IsModified());
  183. }
  184. void wxTextDocument::Modify(bool modified)
  185. {
  186. wxDocument::Modify(modified);
  187. wxTextCtrl* wnd = GetTextCtrl();
  188. if (wnd && !modified)
  189. {
  190. wnd->DiscardEdits();
  191. }
  192. }
  193. void wxTextDocument::OnTextChange(wxCommandEvent& event)
  194. {
  195. Modify(true);
  196. event.Skip();
  197. }
  198. // ----------------------------------------------------------------------------
  199. // TextEditDocument implementation
  200. // ----------------------------------------------------------------------------
  201. IMPLEMENT_DYNAMIC_CLASS(TextEditDocument, wxDocument)
  202. wxTextCtrl* TextEditDocument::GetTextCtrl() const
  203. {
  204. wxView* view = GetFirstView();
  205. return view ? wxStaticCast(view, TextEditView)->GetText() : NULL;
  206. }
  207. // ----------------------------------------------------------------------------
  208. // ImageDocument and ImageDetailsDocument implementation
  209. // ----------------------------------------------------------------------------
  210. IMPLEMENT_DYNAMIC_CLASS(ImageDocument, wxDocument)
  211. bool ImageDocument::DoOpenDocument(const wxString& file)
  212. {
  213. return m_image.LoadFile(file);
  214. }
  215. bool ImageDocument::OnOpenDocument(const wxString& filename)
  216. {
  217. if ( !wxDocument::OnOpenDocument(filename) )
  218. return false;
  219. // we don't have a wxDocTemplate for the image details document as it's
  220. // never created by wxWidgets automatically, instead just do it manually
  221. ImageDetailsDocument * const docDetails = new ImageDetailsDocument(this);
  222. docDetails->SetFilename(filename);
  223. new ImageDetailsView(docDetails);
  224. return true;
  225. }
  226. ImageDetailsDocument::ImageDetailsDocument(ImageDocument *parent)
  227. : wxDocument(parent)
  228. {
  229. const wxImage image = parent->GetImage();
  230. m_size.x = image.GetWidth();
  231. m_size.y = image.GetHeight();
  232. m_numColours = image.CountColours();
  233. m_type = image.GetType();
  234. m_hasAlpha = image.HasAlpha();
  235. }