docview.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/docview.h
  3. // Purpose: Doc/View classes
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DOCH__
  11. #define _WX_DOCH__
  12. #include "wx/defs.h"
  13. #if wxUSE_DOC_VIEW_ARCHITECTURE
  14. #include "wx/list.h"
  15. #include "wx/dlist.h"
  16. #include "wx/string.h"
  17. #include "wx/frame.h"
  18. #include "wx/filehistory.h"
  19. #include "wx/vector.h"
  20. #if wxUSE_PRINTING_ARCHITECTURE
  21. #include "wx/print.h"
  22. #endif
  23. class WXDLLIMPEXP_FWD_CORE wxWindow;
  24. class WXDLLIMPEXP_FWD_CORE wxDocument;
  25. class WXDLLIMPEXP_FWD_CORE wxView;
  26. class WXDLLIMPEXP_FWD_CORE wxDocTemplate;
  27. class WXDLLIMPEXP_FWD_CORE wxDocManager;
  28. class WXDLLIMPEXP_FWD_CORE wxPrintInfo;
  29. class WXDLLIMPEXP_FWD_CORE wxCommandProcessor;
  30. class WXDLLIMPEXP_FWD_BASE wxConfigBase;
  31. class wxDocChildFrameAnyBase;
  32. #if wxUSE_STD_IOSTREAM
  33. #include "wx/iosfwrap.h"
  34. #else
  35. #include "wx/stream.h"
  36. #endif
  37. // Flags for wxDocManager (can be combined).
  38. enum
  39. {
  40. wxDOC_NEW = 1,
  41. wxDOC_SILENT = 2
  42. };
  43. // Document template flags
  44. enum
  45. {
  46. wxTEMPLATE_VISIBLE = 1,
  47. wxTEMPLATE_INVISIBLE = 2,
  48. wxDEFAULT_TEMPLATE_FLAGS = wxTEMPLATE_VISIBLE
  49. };
  50. #define wxMAX_FILE_HISTORY 9
  51. typedef wxVector<wxDocument*> wxDocVector;
  52. typedef wxVector<wxView*> wxViewVector;
  53. typedef wxVector<wxDocTemplate*> wxDocTemplateVector;
  54. class WXDLLIMPEXP_CORE wxDocument : public wxEvtHandler
  55. {
  56. public:
  57. wxDocument(wxDocument *parent = NULL);
  58. virtual ~wxDocument();
  59. // accessors
  60. void SetFilename(const wxString& filename, bool notifyViews = false);
  61. wxString GetFilename() const { return m_documentFile; }
  62. void SetTitle(const wxString& title) { m_documentTitle = title; }
  63. wxString GetTitle() const { return m_documentTitle; }
  64. void SetDocumentName(const wxString& name) { m_documentTypeName = name; }
  65. wxString GetDocumentName() const { return m_documentTypeName; }
  66. // access the flag indicating whether this document had been already saved,
  67. // SetDocumentSaved() is only used internally, don't call it
  68. bool GetDocumentSaved() const { return m_savedYet; }
  69. void SetDocumentSaved(bool saved = true) { m_savedYet = saved; }
  70. // activate the first view of the document if any
  71. void Activate();
  72. // return true if the document hasn't been modified since the last time it
  73. // was saved (implying that it returns false if it was never saved, even if
  74. // the document is not modified)
  75. bool AlreadySaved() const { return !IsModified() && GetDocumentSaved(); }
  76. virtual bool Close();
  77. virtual bool Save();
  78. virtual bool SaveAs();
  79. virtual bool Revert();
  80. #if wxUSE_STD_IOSTREAM
  81. virtual wxSTD ostream& SaveObject(wxSTD ostream& stream);
  82. virtual wxSTD istream& LoadObject(wxSTD istream& stream);
  83. #else
  84. virtual wxOutputStream& SaveObject(wxOutputStream& stream);
  85. virtual wxInputStream& LoadObject(wxInputStream& stream);
  86. #endif
  87. // Called by wxWidgets
  88. virtual bool OnSaveDocument(const wxString& filename);
  89. virtual bool OnOpenDocument(const wxString& filename);
  90. virtual bool OnNewDocument();
  91. virtual bool OnCloseDocument();
  92. // Prompts for saving if about to close a modified document. Returns true
  93. // if ok to close the document (may have saved in the meantime, or set
  94. // modified to false)
  95. virtual bool OnSaveModified();
  96. // if you override, remember to call the default
  97. // implementation (wxDocument::OnChangeFilename)
  98. virtual void OnChangeFilename(bool notifyViews);
  99. // Called by framework if created automatically by the default document
  100. // manager: gives document a chance to initialise and (usually) create a
  101. // view
  102. virtual bool OnCreate(const wxString& path, long flags);
  103. // By default, creates a base wxCommandProcessor.
  104. virtual wxCommandProcessor *OnCreateCommandProcessor();
  105. virtual wxCommandProcessor *GetCommandProcessor() const
  106. { return m_commandProcessor; }
  107. virtual void SetCommandProcessor(wxCommandProcessor *proc)
  108. { m_commandProcessor = proc; }
  109. // Called after a view is added or removed. The default implementation
  110. // deletes the document if this is there are no more views.
  111. virtual void OnChangedViewList();
  112. // Called from OnCloseDocument(), does nothing by default but may be
  113. // overridden. Return value is ignored.
  114. virtual bool DeleteContents();
  115. virtual bool Draw(wxDC&);
  116. virtual bool IsModified() const { return m_documentModified; }
  117. virtual void Modify(bool mod);
  118. virtual bool AddView(wxView *view);
  119. virtual bool RemoveView(wxView *view);
  120. #ifndef __VISUALC6__
  121. wxViewVector GetViewsVector() const;
  122. #endif // !__VISUALC6__
  123. wxList& GetViews() { return m_documentViews; }
  124. const wxList& GetViews() const { return m_documentViews; }
  125. wxView *GetFirstView() const;
  126. virtual void UpdateAllViews(wxView *sender = NULL, wxObject *hint = NULL);
  127. virtual void NotifyClosing();
  128. // Remove all views (because we're closing the document)
  129. virtual bool DeleteAllViews();
  130. // Other stuff
  131. virtual wxDocManager *GetDocumentManager() const;
  132. virtual wxDocTemplate *GetDocumentTemplate() const
  133. { return m_documentTemplate; }
  134. virtual void SetDocumentTemplate(wxDocTemplate *temp)
  135. { m_documentTemplate = temp; }
  136. // Get the document name to be shown to the user: the title if there is
  137. // any, otherwise the filename if the document was saved and, finally,
  138. // "unnamed" otherwise
  139. virtual wxString GetUserReadableName() const;
  140. #if WXWIN_COMPATIBILITY_2_8
  141. // use GetUserReadableName() instead
  142. wxDEPRECATED_BUT_USED_INTERNALLY(
  143. virtual bool GetPrintableName(wxString& buf) const
  144. );
  145. #endif // WXWIN_COMPATIBILITY_2_8
  146. // Returns a window that can be used as a parent for document-related
  147. // dialogs. Override if necessary.
  148. virtual wxWindow *GetDocumentWindow() const;
  149. // Returns true if this document is a child document corresponding to a
  150. // part of the parent document and not a disk file as usual.
  151. bool IsChildDocument() const { return m_documentParent != NULL; }
  152. protected:
  153. wxList m_documentViews;
  154. wxString m_documentFile;
  155. wxString m_documentTitle;
  156. wxString m_documentTypeName;
  157. wxDocTemplate* m_documentTemplate;
  158. bool m_documentModified;
  159. // if the document parent is non-NULL, it's a pseudo-document corresponding
  160. // to a part of the parent document which can't be saved or loaded
  161. // independently of its parent and is always closed when its parent is
  162. wxDocument* m_documentParent;
  163. wxCommandProcessor* m_commandProcessor;
  164. bool m_savedYet;
  165. // Called by OnSaveDocument and OnOpenDocument to implement standard
  166. // Save/Load behaviour. Re-implement in derived class for custom
  167. // behaviour.
  168. virtual bool DoSaveDocument(const wxString& file);
  169. virtual bool DoOpenDocument(const wxString& file);
  170. // the default implementation of GetUserReadableName()
  171. wxString DoGetUserReadableName() const;
  172. private:
  173. // list of all documents whose m_documentParent is this one
  174. typedef wxDList<wxDocument> DocsList;
  175. DocsList m_childDocuments;
  176. DECLARE_ABSTRACT_CLASS(wxDocument)
  177. wxDECLARE_NO_COPY_CLASS(wxDocument);
  178. };
  179. class WXDLLIMPEXP_CORE wxView: public wxEvtHandler
  180. {
  181. public:
  182. wxView();
  183. virtual ~wxView();
  184. wxDocument *GetDocument() const { return m_viewDocument; }
  185. virtual void SetDocument(wxDocument *doc);
  186. wxString GetViewName() const { return m_viewTypeName; }
  187. void SetViewName(const wxString& name) { m_viewTypeName = name; }
  188. wxWindow *GetFrame() const { return m_viewFrame ; }
  189. void SetFrame(wxWindow *frame) { m_viewFrame = frame; }
  190. virtual void OnActivateView(bool activate,
  191. wxView *activeView,
  192. wxView *deactiveView);
  193. virtual void OnDraw(wxDC *dc) = 0;
  194. virtual void OnPrint(wxDC *dc, wxObject *info);
  195. virtual void OnUpdate(wxView *sender, wxObject *hint = NULL);
  196. virtual void OnClosingDocument() {}
  197. virtual void OnChangeFilename();
  198. // Called by framework if created automatically by the default document
  199. // manager class: gives view a chance to initialise
  200. virtual bool OnCreate(wxDocument *WXUNUSED(doc), long WXUNUSED(flags))
  201. { return true; }
  202. // Checks if the view is the last one for the document; if so, asks user
  203. // to confirm save data (if modified). If ok, deletes itself and returns
  204. // true.
  205. virtual bool Close(bool deleteWindow = true);
  206. // Override to do cleanup/veto close
  207. virtual bool OnClose(bool deleteWindow);
  208. // A view's window can call this to notify the view it is (in)active.
  209. // The function then notifies the document manager.
  210. virtual void Activate(bool activate);
  211. wxDocManager *GetDocumentManager() const
  212. { return m_viewDocument->GetDocumentManager(); }
  213. #if wxUSE_PRINTING_ARCHITECTURE
  214. virtual wxPrintout *OnCreatePrintout();
  215. #endif
  216. // implementation only
  217. // -------------------
  218. // set the associated frame, it is used to reset its view when we're
  219. // destroyed
  220. void SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame);
  221. // get the associated frame, may be NULL during destruction
  222. wxDocChildFrameAnyBase* GetDocChildFrame() const { return m_docChildFrame; }
  223. protected:
  224. // hook the document into event handlers chain here
  225. virtual bool TryBefore(wxEvent& event);
  226. wxDocument* m_viewDocument;
  227. wxString m_viewTypeName;
  228. wxWindow* m_viewFrame;
  229. wxDocChildFrameAnyBase *m_docChildFrame;
  230. private:
  231. DECLARE_ABSTRACT_CLASS(wxView)
  232. wxDECLARE_NO_COPY_CLASS(wxView);
  233. };
  234. // Represents user interface (and other) properties of documents and views
  235. class WXDLLIMPEXP_CORE wxDocTemplate: public wxObject
  236. {
  237. friend class WXDLLIMPEXP_FWD_CORE wxDocManager;
  238. public:
  239. // Associate document and view types. They're for identifying what view is
  240. // associated with what template/document type
  241. wxDocTemplate(wxDocManager *manager,
  242. const wxString& descr,
  243. const wxString& filter,
  244. const wxString& dir,
  245. const wxString& ext,
  246. const wxString& docTypeName,
  247. const wxString& viewTypeName,
  248. wxClassInfo *docClassInfo = NULL,
  249. wxClassInfo *viewClassInfo = NULL,
  250. long flags = wxDEFAULT_TEMPLATE_FLAGS);
  251. virtual ~wxDocTemplate();
  252. // By default, these two member functions dynamically creates document and
  253. // view using dynamic instance construction. Override these if you need a
  254. // different method of construction.
  255. virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
  256. virtual wxView *CreateView(wxDocument *doc, long flags = 0);
  257. // Helper method for CreateDocument; also allows you to do your own document
  258. // creation
  259. virtual bool InitDocument(wxDocument* doc,
  260. const wxString& path,
  261. long flags = 0);
  262. wxString GetDefaultExtension() const { return m_defaultExt; }
  263. wxString GetDescription() const { return m_description; }
  264. wxString GetDirectory() const { return m_directory; }
  265. wxDocManager *GetDocumentManager() const { return m_documentManager; }
  266. void SetDocumentManager(wxDocManager *manager)
  267. { m_documentManager = manager; }
  268. wxString GetFileFilter() const { return m_fileFilter; }
  269. long GetFlags() const { return m_flags; }
  270. virtual wxString GetViewName() const { return m_viewTypeName; }
  271. virtual wxString GetDocumentName() const { return m_docTypeName; }
  272. void SetFileFilter(const wxString& filter) { m_fileFilter = filter; }
  273. void SetDirectory(const wxString& dir) { m_directory = dir; }
  274. void SetDescription(const wxString& descr) { m_description = descr; }
  275. void SetDefaultExtension(const wxString& ext) { m_defaultExt = ext; }
  276. void SetFlags(long flags) { m_flags = flags; }
  277. bool IsVisible() const { return (m_flags & wxTEMPLATE_VISIBLE) != 0; }
  278. wxClassInfo* GetDocClassInfo() const { return m_docClassInfo; }
  279. wxClassInfo* GetViewClassInfo() const { return m_viewClassInfo; }
  280. virtual bool FileMatchesTemplate(const wxString& path);
  281. protected:
  282. long m_flags;
  283. wxString m_fileFilter;
  284. wxString m_directory;
  285. wxString m_description;
  286. wxString m_defaultExt;
  287. wxString m_docTypeName;
  288. wxString m_viewTypeName;
  289. wxDocManager* m_documentManager;
  290. // For dynamic creation of appropriate instances.
  291. wxClassInfo* m_docClassInfo;
  292. wxClassInfo* m_viewClassInfo;
  293. // Called by CreateDocument and CreateView to create the actual
  294. // document/view object.
  295. //
  296. // By default uses the ClassInfo provided to the constructor. Override
  297. // these functions to provide a different method of creation.
  298. virtual wxDocument *DoCreateDocument();
  299. virtual wxView *DoCreateView();
  300. private:
  301. DECLARE_CLASS(wxDocTemplate)
  302. wxDECLARE_NO_COPY_CLASS(wxDocTemplate);
  303. };
  304. // One object of this class may be created in an application, to manage all
  305. // the templates and documents.
  306. class WXDLLIMPEXP_CORE wxDocManager: public wxEvtHandler
  307. {
  308. public:
  309. // NB: flags are unused, don't pass wxDOC_XXX to this ctor
  310. wxDocManager(long flags = 0, bool initialize = true);
  311. virtual ~wxDocManager();
  312. virtual bool Initialize();
  313. // Handlers for common user commands
  314. void OnFileClose(wxCommandEvent& event);
  315. void OnFileCloseAll(wxCommandEvent& event);
  316. void OnFileNew(wxCommandEvent& event);
  317. void OnFileOpen(wxCommandEvent& event);
  318. void OnFileRevert(wxCommandEvent& event);
  319. void OnFileSave(wxCommandEvent& event);
  320. void OnFileSaveAs(wxCommandEvent& event);
  321. void OnMRUFile(wxCommandEvent& event);
  322. #if wxUSE_PRINTING_ARCHITECTURE
  323. void OnPrint(wxCommandEvent& event);
  324. void OnPreview(wxCommandEvent& event);
  325. void OnPageSetup(wxCommandEvent& event);
  326. #endif // wxUSE_PRINTING_ARCHITECTURE
  327. void OnUndo(wxCommandEvent& event);
  328. void OnRedo(wxCommandEvent& event);
  329. // Handlers for UI update commands
  330. void OnUpdateFileOpen(wxUpdateUIEvent& event);
  331. void OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event);
  332. void OnUpdateFileRevert(wxUpdateUIEvent& event);
  333. void OnUpdateFileNew(wxUpdateUIEvent& event);
  334. void OnUpdateFileSave(wxUpdateUIEvent& event);
  335. void OnUpdateFileSaveAs(wxUpdateUIEvent& event);
  336. void OnUpdateUndo(wxUpdateUIEvent& event);
  337. void OnUpdateRedo(wxUpdateUIEvent& event);
  338. // called when file format detection didn't work, can be overridden to do
  339. // something in this case
  340. virtual void OnOpenFileFailure() { }
  341. virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
  342. // wrapper around CreateDocument() with a more clear name
  343. wxDocument *CreateNewDocument()
  344. { return CreateDocument(wxString(), wxDOC_NEW); }
  345. virtual wxView *CreateView(wxDocument *doc, long flags = 0);
  346. virtual void DeleteTemplate(wxDocTemplate *temp, long flags = 0);
  347. virtual bool FlushDoc(wxDocument *doc);
  348. virtual wxDocTemplate *MatchTemplate(const wxString& path);
  349. virtual wxDocTemplate *SelectDocumentPath(wxDocTemplate **templates,
  350. int noTemplates, wxString& path, long flags, bool save = false);
  351. virtual wxDocTemplate *SelectDocumentType(wxDocTemplate **templates,
  352. int noTemplates, bool sort = false);
  353. virtual wxDocTemplate *SelectViewType(wxDocTemplate **templates,
  354. int noTemplates, bool sort = false);
  355. virtual wxDocTemplate *FindTemplateForPath(const wxString& path);
  356. void AssociateTemplate(wxDocTemplate *temp);
  357. void DisassociateTemplate(wxDocTemplate *temp);
  358. // Find template from document class info, may return NULL.
  359. wxDocTemplate* FindTemplate(const wxClassInfo* documentClassInfo);
  360. // Find document from file name, may return NULL.
  361. wxDocument* FindDocumentByPath(const wxString& path) const;
  362. wxDocument *GetCurrentDocument() const;
  363. void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; }
  364. int GetMaxDocsOpen() const { return m_maxDocsOpen; }
  365. // Add and remove a document from the manager's list
  366. void AddDocument(wxDocument *doc);
  367. void RemoveDocument(wxDocument *doc);
  368. // closes all currently open documents
  369. bool CloseDocuments(bool force = true);
  370. // closes the specified document
  371. bool CloseDocument(wxDocument* doc, bool force = false);
  372. // Clear remaining documents and templates
  373. bool Clear(bool force = true);
  374. // Views or windows should inform the document manager
  375. // when a view is going in or out of focus
  376. virtual void ActivateView(wxView *view, bool activate = true);
  377. virtual wxView *GetCurrentView() const { return m_currentView; }
  378. // This method tries to find an active view harder than GetCurrentView():
  379. // if the latter is NULL, it also checks if we don't have just a single
  380. // view and returns it then.
  381. wxView *GetAnyUsableView() const;
  382. #ifndef __VISUALC6__
  383. wxDocVector GetDocumentsVector() const;
  384. wxDocTemplateVector GetTemplatesVector() const;
  385. #endif // !__VISUALC6__
  386. wxList& GetDocuments() { return m_docs; }
  387. wxList& GetTemplates() { return m_templates; }
  388. // Return the default name for a new document (by default returns strings
  389. // in the form "unnamed <counter>" but can be overridden)
  390. virtual wxString MakeNewDocumentName();
  391. // Make a frame title (override this to do something different)
  392. virtual wxString MakeFrameTitle(wxDocument* doc);
  393. virtual wxFileHistory *OnCreateFileHistory();
  394. virtual wxFileHistory *GetFileHistory() const { return m_fileHistory; }
  395. // File history management
  396. virtual void AddFileToHistory(const wxString& file);
  397. virtual void RemoveFileFromHistory(size_t i);
  398. virtual size_t GetHistoryFilesCount() const;
  399. virtual wxString GetHistoryFile(size_t i) const;
  400. virtual void FileHistoryUseMenu(wxMenu *menu);
  401. virtual void FileHistoryRemoveMenu(wxMenu *menu);
  402. #if wxUSE_CONFIG
  403. virtual void FileHistoryLoad(const wxConfigBase& config);
  404. virtual void FileHistorySave(wxConfigBase& config);
  405. #endif // wxUSE_CONFIG
  406. virtual void FileHistoryAddFilesToMenu();
  407. virtual void FileHistoryAddFilesToMenu(wxMenu* menu);
  408. wxString GetLastDirectory() const;
  409. void SetLastDirectory(const wxString& dir) { m_lastDirectory = dir; }
  410. // Get the current document manager
  411. static wxDocManager* GetDocumentManager() { return sm_docManager; }
  412. #if wxUSE_PRINTING_ARCHITECTURE
  413. wxPageSetupDialogData& GetPageSetupDialogData()
  414. { return m_pageSetupDialogData; }
  415. const wxPageSetupDialogData& GetPageSetupDialogData() const
  416. { return m_pageSetupDialogData; }
  417. #endif // wxUSE_PRINTING_ARCHITECTURE
  418. #if WXWIN_COMPATIBILITY_2_8
  419. // deprecated, override GetDefaultName() instead
  420. wxDEPRECATED_BUT_USED_INTERNALLY(
  421. virtual bool MakeDefaultName(wxString& buf)
  422. );
  423. #endif
  424. #if WXWIN_COMPATIBILITY_2_6
  425. // deprecated, use GetHistoryFilesCount() instead
  426. wxDEPRECATED( size_t GetNoHistoryFiles() const );
  427. #endif // WXWIN_COMPATIBILITY_2_6
  428. protected:
  429. // Called when a file selected from the MRU list doesn't exist any more.
  430. // The default behaviour is to remove the file from the MRU and notify the
  431. // user about it but this method can be overridden to customize it.
  432. virtual void OnMRUFileNotExist(unsigned n, const wxString& filename);
  433. // Open the MRU file with the given index in our associated file history.
  434. void DoOpenMRUFile(unsigned n);
  435. #if wxUSE_PRINTING_ARCHITECTURE
  436. virtual wxPreviewFrame* CreatePreviewFrame(wxPrintPreviewBase* preview,
  437. wxWindow *parent,
  438. const wxString& title);
  439. #endif // wxUSE_PRINTING_ARCHITECTURE
  440. // hook the currently active view into event handlers chain here
  441. virtual bool TryBefore(wxEvent& event);
  442. // return the command processor for the current document, if any
  443. wxCommandProcessor *GetCurrentCommandProcessor() const;
  444. int m_defaultDocumentNameCounter;
  445. int m_maxDocsOpen;
  446. wxList m_docs;
  447. wxList m_templates;
  448. wxView* m_currentView;
  449. wxFileHistory* m_fileHistory;
  450. wxString m_lastDirectory;
  451. static wxDocManager* sm_docManager;
  452. #if wxUSE_PRINTING_ARCHITECTURE
  453. wxPageSetupDialogData m_pageSetupDialogData;
  454. #endif // wxUSE_PRINTING_ARCHITECTURE
  455. DECLARE_EVENT_TABLE()
  456. DECLARE_DYNAMIC_CLASS(wxDocManager)
  457. wxDECLARE_NO_COPY_CLASS(wxDocManager);
  458. };
  459. #if WXWIN_COMPATIBILITY_2_6
  460. inline size_t wxDocManager::GetNoHistoryFiles() const
  461. {
  462. return GetHistoryFilesCount();
  463. }
  464. #endif // WXWIN_COMPATIBILITY_2_6
  465. // ----------------------------------------------------------------------------
  466. // Base class for child frames -- this is what wxView renders itself into
  467. //
  468. // Notice that this is a mix-in class so it doesn't derive from wxWindow, only
  469. // wxDocChildFrameAny does
  470. // ----------------------------------------------------------------------------
  471. class WXDLLIMPEXP_CORE wxDocChildFrameAnyBase
  472. {
  473. public:
  474. // default ctor, use Create() after it
  475. wxDocChildFrameAnyBase()
  476. {
  477. m_childDocument = NULL;
  478. m_childView = NULL;
  479. m_win = NULL;
  480. m_lastEvent = NULL;
  481. }
  482. // full ctor equivalent to using the default one and Create()
  483. wxDocChildFrameAnyBase(wxDocument *doc, wxView *view, wxWindow *win)
  484. {
  485. Create(doc, view, win);
  486. }
  487. // method which must be called for an object created using the default ctor
  488. //
  489. // note that it returns bool just for consistency with Create() methods in
  490. // other classes, we never return false from here
  491. bool Create(wxDocument *doc, wxView *view, wxWindow *win)
  492. {
  493. m_childDocument = doc;
  494. m_childView = view;
  495. m_win = win;
  496. if ( view )
  497. view->SetDocChildFrame(this);
  498. return true;
  499. }
  500. // dtor doesn't need to be virtual, an object should never be destroyed via
  501. // a pointer to this class
  502. ~wxDocChildFrameAnyBase()
  503. {
  504. // prevent the view from deleting us if we're being deleted directly
  505. // (and not via Close() + Destroy())
  506. if ( m_childView )
  507. m_childView->SetDocChildFrame(NULL);
  508. }
  509. wxDocument *GetDocument() const { return m_childDocument; }
  510. wxView *GetView() const { return m_childView; }
  511. void SetDocument(wxDocument *doc) { m_childDocument = doc; }
  512. void SetView(wxView *view) { m_childView = view; }
  513. wxWindow *GetWindow() const { return m_win; }
  514. // implementation only
  515. // Check if this event had been just processed in this frame.
  516. bool HasAlreadyProcessed(wxEvent& event) const
  517. {
  518. return m_lastEvent == &event;
  519. }
  520. protected:
  521. // we're not a wxEvtHandler but we provide this wxEvtHandler-like function
  522. // which is called from TryBefore() of the derived classes to give our view
  523. // a chance to process the message before the frame event handlers are used
  524. bool TryProcessEvent(wxEvent& event);
  525. // called from EVT_CLOSE handler in the frame: check if we can close and do
  526. // cleanup if so; veto the event otherwise
  527. bool CloseView(wxCloseEvent& event);
  528. wxDocument* m_childDocument;
  529. wxView* m_childView;
  530. // the associated window: having it here is not terribly elegant but it
  531. // allows us to avoid having any virtual functions in this class
  532. wxWindow* m_win;
  533. private:
  534. // Pointer to the last processed event used to avoid sending the same event
  535. // twice to wxDocManager, from here and from wxDocParentFrameAnyBase.
  536. wxEvent* m_lastEvent;
  537. wxDECLARE_NO_COPY_CLASS(wxDocChildFrameAnyBase);
  538. };
  539. // ----------------------------------------------------------------------------
  540. // Template implementing child frame concept using the given wxFrame-like class
  541. //
  542. // This is used to define wxDocChildFrame and wxDocMDIChildFrame: ChildFrame is
  543. // a wxFrame or wxMDIChildFrame (although in theory it could be any wxWindow-
  544. // derived class as long as it provided a ctor with the same signature as
  545. // wxFrame and OnActivate() method) and ParentFrame is either wxFrame or
  546. // wxMDIParentFrame.
  547. // ----------------------------------------------------------------------------
  548. template <class ChildFrame, class ParentFrame>
  549. class WXDLLIMPEXP_CORE wxDocChildFrameAny : public ChildFrame,
  550. public wxDocChildFrameAnyBase
  551. {
  552. public:
  553. typedef ChildFrame BaseClass;
  554. // default ctor, use Create after it
  555. wxDocChildFrameAny() { }
  556. // ctor for a frame showing the given view of the specified document
  557. wxDocChildFrameAny(wxDocument *doc,
  558. wxView *view,
  559. ParentFrame *parent,
  560. wxWindowID id,
  561. const wxString& title,
  562. const wxPoint& pos = wxDefaultPosition,
  563. const wxSize& size = wxDefaultSize,
  564. long style = wxDEFAULT_FRAME_STYLE,
  565. const wxString& name = wxFrameNameStr)
  566. {
  567. Create(doc, view, parent, id, title, pos, size, style, name);
  568. }
  569. bool Create(wxDocument *doc,
  570. wxView *view,
  571. ParentFrame *parent,
  572. wxWindowID id,
  573. const wxString& title,
  574. const wxPoint& pos = wxDefaultPosition,
  575. const wxSize& size = wxDefaultSize,
  576. long style = wxDEFAULT_FRAME_STYLE,
  577. const wxString& name = wxFrameNameStr)
  578. {
  579. if ( !wxDocChildFrameAnyBase::Create(doc, view, this) )
  580. return false;
  581. if ( !BaseClass::Create(parent, id, title, pos, size, style, name) )
  582. return false;
  583. this->Connect(wxEVT_ACTIVATE,
  584. wxActivateEventHandler(wxDocChildFrameAny::OnActivate));
  585. this->Connect(wxEVT_CLOSE_WINDOW,
  586. wxCloseEventHandler(wxDocChildFrameAny::OnCloseWindow));
  587. return true;
  588. }
  589. virtual bool Destroy()
  590. {
  591. // FIXME: why exactly do we do this? to avoid activation events during
  592. // destructions maybe?
  593. m_childView = NULL;
  594. return BaseClass::Destroy();
  595. }
  596. protected:
  597. // hook the child view into event handlers chain here
  598. virtual bool TryBefore(wxEvent& event)
  599. {
  600. return TryProcessEvent(event) || BaseClass::TryBefore(event);
  601. }
  602. private:
  603. void OnActivate(wxActivateEvent& event)
  604. {
  605. BaseClass::OnActivate(event);
  606. if ( m_childView )
  607. m_childView->Activate(event.GetActive());
  608. }
  609. void OnCloseWindow(wxCloseEvent& event)
  610. {
  611. if ( CloseView(event) )
  612. Destroy();
  613. //else: vetoed
  614. }
  615. wxDECLARE_NO_COPY_TEMPLATE_CLASS_2(wxDocChildFrameAny,
  616. ChildFrame, ParentFrame);
  617. };
  618. // ----------------------------------------------------------------------------
  619. // A default child frame: we need to define it as a class just for wxRTTI,
  620. // otherwise we could simply typedef it
  621. // ----------------------------------------------------------------------------
  622. #ifdef __VISUALC6__
  623. // "non dll-interface class 'wxDocChildFrameAny<>' used as base interface
  624. // for dll-interface class 'wxDocChildFrame'" -- this is bogus as the
  625. // template will be DLL-exported but only once it is used as base class
  626. // here!
  627. #pragma warning (push)
  628. #pragma warning (disable:4275)
  629. #endif
  630. typedef wxDocChildFrameAny<wxFrame, wxFrame> wxDocChildFrameBase;
  631. class WXDLLIMPEXP_CORE wxDocChildFrame : public wxDocChildFrameBase
  632. {
  633. public:
  634. wxDocChildFrame()
  635. {
  636. }
  637. wxDocChildFrame(wxDocument *doc,
  638. wxView *view,
  639. wxFrame *parent,
  640. wxWindowID id,
  641. const wxString& title,
  642. const wxPoint& pos = wxDefaultPosition,
  643. const wxSize& size = wxDefaultSize,
  644. long style = wxDEFAULT_FRAME_STYLE,
  645. const wxString& name = wxFrameNameStr)
  646. : wxDocChildFrameBase(doc, view,
  647. parent, id, title, pos, size, style, name)
  648. {
  649. }
  650. bool Create(wxDocument *doc,
  651. wxView *view,
  652. wxFrame *parent,
  653. wxWindowID id,
  654. const wxString& title,
  655. const wxPoint& pos = wxDefaultPosition,
  656. const wxSize& size = wxDefaultSize,
  657. long style = wxDEFAULT_FRAME_STYLE,
  658. const wxString& name = wxFrameNameStr)
  659. {
  660. return wxDocChildFrameBase::Create
  661. (
  662. doc, view,
  663. parent, id, title, pos, size, style, name
  664. );
  665. }
  666. private:
  667. DECLARE_CLASS(wxDocChildFrame)
  668. wxDECLARE_NO_COPY_CLASS(wxDocChildFrame);
  669. };
  670. // ----------------------------------------------------------------------------
  671. // wxDocParentFrame and related classes.
  672. //
  673. // As with wxDocChildFrame we define a template base class used by both normal
  674. // and MDI versions
  675. // ----------------------------------------------------------------------------
  676. // Base class containing type-independent code of wxDocParentFrameAny
  677. //
  678. // Similarly to wxDocChildFrameAnyBase, this class is a mix-in and doesn't
  679. // derive from wxWindow.
  680. class WXDLLIMPEXP_CORE wxDocParentFrameAnyBase
  681. {
  682. public:
  683. wxDocParentFrameAnyBase(wxWindow* frame)
  684. : m_frame(frame)
  685. {
  686. m_docManager = NULL;
  687. }
  688. wxDocManager *GetDocumentManager() const { return m_docManager; }
  689. protected:
  690. // This is similar to wxDocChildFrameAnyBase method with the same name:
  691. // while we're not an event handler ourselves and so can't override
  692. // TryBefore(), we provide a helper that the derived template class can use
  693. // from its TryBefore() implementation.
  694. bool TryProcessEvent(wxEvent& event);
  695. wxWindow* const m_frame;
  696. wxDocManager *m_docManager;
  697. wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAnyBase);
  698. };
  699. // This is similar to wxDocChildFrameAny and is used to provide common
  700. // implementation for both wxDocParentFrame and wxDocMDIParentFrame
  701. template <class BaseFrame>
  702. class WXDLLIMPEXP_CORE wxDocParentFrameAny : public BaseFrame,
  703. public wxDocParentFrameAnyBase
  704. {
  705. public:
  706. wxDocParentFrameAny() : wxDocParentFrameAnyBase(this) { }
  707. wxDocParentFrameAny(wxDocManager *manager,
  708. wxFrame *frame,
  709. wxWindowID id,
  710. const wxString& title,
  711. const wxPoint& pos = wxDefaultPosition,
  712. const wxSize& size = wxDefaultSize,
  713. long style = wxDEFAULT_FRAME_STYLE,
  714. const wxString& name = wxFrameNameStr)
  715. : wxDocParentFrameAnyBase(this)
  716. {
  717. Create(manager, frame, id, title, pos, size, style, name);
  718. }
  719. bool Create(wxDocManager *manager,
  720. wxFrame *frame,
  721. wxWindowID id,
  722. const wxString& title,
  723. const wxPoint& pos = wxDefaultPosition,
  724. const wxSize& size = wxDefaultSize,
  725. long style = wxDEFAULT_FRAME_STYLE,
  726. const wxString& name = wxFrameNameStr)
  727. {
  728. m_docManager = manager;
  729. if ( !BaseFrame::Create(frame, id, title, pos, size, style, name) )
  730. return false;
  731. this->Connect(wxID_EXIT, wxEVT_MENU,
  732. wxCommandEventHandler(wxDocParentFrameAny::OnExit));
  733. this->Connect(wxEVT_CLOSE_WINDOW,
  734. wxCloseEventHandler(wxDocParentFrameAny::OnCloseWindow));
  735. return true;
  736. }
  737. protected:
  738. // hook the document manager into event handling chain here
  739. virtual bool TryBefore(wxEvent& event)
  740. {
  741. // It is important to send the event to the base class first as
  742. // wxMDIParentFrame overrides its TryBefore() to send the menu events
  743. // to the currently active child frame and the child must get them
  744. // before our own TryProcessEvent() is executed, not afterwards.
  745. return BaseFrame::TryBefore(event) || TryProcessEvent(event);
  746. }
  747. private:
  748. void OnExit(wxCommandEvent& WXUNUSED(event))
  749. {
  750. this->Close();
  751. }
  752. void OnCloseWindow(wxCloseEvent& event)
  753. {
  754. if ( m_docManager && !m_docManager->Clear(!event.CanVeto()) )
  755. {
  756. // The user decided not to close finally, abort.
  757. event.Veto();
  758. }
  759. else
  760. {
  761. // Just skip the event, base class handler will destroy the window.
  762. event.Skip();
  763. }
  764. }
  765. wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAny);
  766. };
  767. typedef wxDocParentFrameAny<wxFrame> wxDocParentFrameBase;
  768. class WXDLLIMPEXP_CORE wxDocParentFrame : public wxDocParentFrameBase
  769. {
  770. public:
  771. wxDocParentFrame() : wxDocParentFrameBase() { }
  772. wxDocParentFrame(wxDocManager *manager,
  773. wxFrame *parent,
  774. wxWindowID id,
  775. const wxString& title,
  776. const wxPoint& pos = wxDefaultPosition,
  777. const wxSize& size = wxDefaultSize,
  778. long style = wxDEFAULT_FRAME_STYLE,
  779. const wxString& name = wxFrameNameStr)
  780. : wxDocParentFrameBase(manager,
  781. parent, id, title, pos, size, style, name)
  782. {
  783. }
  784. bool Create(wxDocManager *manager,
  785. wxFrame *parent,
  786. wxWindowID id,
  787. const wxString& title,
  788. const wxPoint& pos = wxDefaultPosition,
  789. const wxSize& size = wxDefaultSize,
  790. long style = wxDEFAULT_FRAME_STYLE,
  791. const wxString& name = wxFrameNameStr)
  792. {
  793. return wxDocParentFrameBase::Create(manager,
  794. parent, id, title,
  795. pos, size, style, name);
  796. }
  797. private:
  798. DECLARE_CLASS(wxDocParentFrame)
  799. wxDECLARE_NO_COPY_CLASS(wxDocParentFrame);
  800. };
  801. #ifdef __VISUALC6__
  802. // reenable warning 4275
  803. #pragma warning (pop)
  804. #endif
  805. // ----------------------------------------------------------------------------
  806. // Provide simple default printing facilities
  807. // ----------------------------------------------------------------------------
  808. #if wxUSE_PRINTING_ARCHITECTURE
  809. class WXDLLIMPEXP_CORE wxDocPrintout : public wxPrintout
  810. {
  811. public:
  812. wxDocPrintout(wxView *view = NULL, const wxString& title = wxString());
  813. // implement wxPrintout methods
  814. virtual bool OnPrintPage(int page);
  815. virtual bool HasPage(int page);
  816. virtual bool OnBeginDocument(int startPage, int endPage);
  817. virtual void GetPageInfo(int *minPage, int *maxPage,
  818. int *selPageFrom, int *selPageTo);
  819. virtual wxView *GetView() { return m_printoutView; }
  820. protected:
  821. wxView* m_printoutView;
  822. private:
  823. DECLARE_DYNAMIC_CLASS(wxDocPrintout)
  824. wxDECLARE_NO_COPY_CLASS(wxDocPrintout);
  825. };
  826. #endif // wxUSE_PRINTING_ARCHITECTURE
  827. // For compatibility with existing file formats:
  828. // converts from/to a stream to/from a temporary file.
  829. #if wxUSE_STD_IOSTREAM
  830. bool WXDLLIMPEXP_CORE
  831. wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream);
  832. bool WXDLLIMPEXP_CORE
  833. wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename);
  834. #else
  835. bool WXDLLIMPEXP_CORE
  836. wxTransferFileToStream(const wxString& filename, wxOutputStream& stream);
  837. bool WXDLLIMPEXP_CORE
  838. wxTransferStreamToFile(wxInputStream& stream, const wxString& filename);
  839. #endif // wxUSE_STD_IOSTREAM
  840. // these flags are not used anywhere by wxWidgets and kept only for an unlikely
  841. // case of existing user code using them for its own purposes
  842. #if WXWIN_COMPATIBILITY_2_8
  843. enum
  844. {
  845. wxDOC_SDI = 1,
  846. wxDOC_MDI,
  847. wxDEFAULT_DOCMAN_FLAGS = wxDOC_SDI
  848. };
  849. #endif // WXWIN_COMPATIBILITY_2_8
  850. #ifndef __VISUALC6__
  851. inline wxViewVector wxDocument::GetViewsVector() const
  852. {
  853. return m_documentViews.AsVector<wxView*>();
  854. }
  855. inline wxDocVector wxDocManager::GetDocumentsVector() const
  856. {
  857. return m_docs.AsVector<wxDocument*>();
  858. }
  859. inline wxDocTemplateVector wxDocManager::GetTemplatesVector() const
  860. {
  861. return m_templates.AsVector<wxDocTemplate*>();
  862. }
  863. #endif // !__VISUALC6__
  864. #endif // wxUSE_DOC_VIEW_ARCHITECTURE
  865. #endif // _WX_DOCH__