docview.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: docview.h
  3. // Purpose: interface of various doc/view framework classes
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. A vector of wxDocument pointers.
  9. @since 2.9.5
  10. */
  11. typedef wxVector<wxDocument*> wxDocVector;
  12. /**
  13. A vector of wxView pointers.
  14. @since 2.9.5
  15. */
  16. typedef wxVector<wxView*> wxViewVector;
  17. /**
  18. A vector of wxDocTemplate pointers.
  19. @since 2.9.5
  20. */
  21. typedef wxVector<wxDocTemplate*> wxDocTemplateVector;
  22. /**
  23. @class wxDocTemplate
  24. The wxDocTemplate class is used to model the relationship between a
  25. document class and a view class.
  26. @library{wxcore}
  27. @category{docview}
  28. @see @ref overview_docview_wxdoctemplate, wxDocument, wxView
  29. */
  30. class wxDocTemplate : public wxObject
  31. {
  32. public:
  33. /**
  34. Constructor. Create instances dynamically near the start of your
  35. application after creating a wxDocManager instance, and before doing
  36. any document or view operations.
  37. @param manager
  38. The document manager object which manages this template.
  39. @param descr
  40. A short description of what the template is for. This string will
  41. be displayed in the file filter list of Windows file selectors.
  42. @param filter
  43. An appropriate file filter such as "*.txt".
  44. @param dir
  45. The default directory to use for file selectors.
  46. @param ext
  47. The default file extension (such as "txt").
  48. @param docTypeName
  49. A name that should be unique for a given type of document, used for
  50. gathering a list of views relevant to a particular document.
  51. @param viewTypeName
  52. A name that should be unique for a given view.
  53. @param docClassInfo
  54. A pointer to the run-time document class information as returned by
  55. the wxCLASSINFO() macro, e.g. wxCLASSINFO(MyDocumentClass). If this is
  56. not supplied, you will need to derive a new wxDocTemplate class and
  57. override the CreateDocument() member to return a new document
  58. instance on demand.
  59. @param viewClassInfo
  60. A pointer to the run-time view class information as returned by the
  61. wxCLASSINFO() macro, e.g. wxCLASSINFO(MyViewClass). If this is not
  62. supplied, you will need to derive a new wxDocTemplate class and
  63. override the CreateView() member to return a new view instance on
  64. demand.
  65. @param flags
  66. A bit list of the following:
  67. - wxTEMPLATE_VISIBLE - The template may be displayed to the
  68. user in dialogs.
  69. - wxTEMPLATE_INVISIBLE - The template may not be displayed to
  70. the user in dialogs.
  71. - wxDEFAULT_TEMPLATE_FLAGS - Defined as wxTEMPLATE_VISIBLE.
  72. @beginWxPerlOnly
  73. In wxPerl @a docClassInfo and @a viewClassInfo can be either
  74. @c Wx::ClassInfo objects or strings containing the name of the
  75. perl packages which are to be used as @c Wx::Document and
  76. @c Wx::View classes (they must have a constructor named new);
  77. as an example:
  78. - Wx::DocTemplate->new(docmgr, descr, filter, dir, ext,
  79. docTypeName, viewTypeName, docClassInfo, viewClassInfo,
  80. flags): will construct document and view objects from the
  81. class information.
  82. - Wx::DocTemplate->new(docmgr, descr, filter, dir, ext,
  83. docTypeName, viewTypeName, docClassName, viewClassName,
  84. flags): will construct document and view objects from perl
  85. packages.
  86. - Wx::DocTemplate->new(docmgr, descr, filter, dir, ext,
  87. docTypeName, viewTypeName):
  88. in this case @c Wx::DocTemplate::CreateDocument() and
  89. @c Wx::DocTemplate::CreateView() must be overridden
  90. @endWxPerlOnly
  91. */
  92. wxDocTemplate(wxDocManager* manager, const wxString& descr,
  93. const wxString& filter, const wxString& dir,
  94. const wxString& ext, const wxString& docTypeName,
  95. const wxString& viewTypeName, wxClassInfo* docClassInfo = 0,
  96. wxClassInfo* viewClassInfo = 0,
  97. long flags = wxTEMPLATE_VISIBLE);
  98. /**
  99. Destructor.
  100. */
  101. virtual ~wxDocTemplate();
  102. /**
  103. Creates a new instance of the associated document class. If you have
  104. not supplied a wxClassInfo parameter to the template constructor, you
  105. will need to override this function to return an appropriate document
  106. instance.
  107. This function calls InitDocument() which in turns calls
  108. wxDocument::OnCreate().
  109. */
  110. virtual wxDocument* CreateDocument(const wxString& path, long flags = 0);
  111. /**
  112. Creates a new instance of the associated view class.
  113. If you have not supplied a wxClassInfo parameter to the template
  114. constructor, you will need to override this function to return an
  115. appropriate view instance.
  116. If the new view initialization fails, it must call
  117. wxDocument::RemoveView() for consistency with the default behaviour of
  118. this function.
  119. */
  120. virtual wxView* CreateView(wxDocument* doc, long flags = 0);
  121. /**
  122. This function implements the default (very primitive) format detection
  123. which checks if the extension is that of the template.
  124. @param path
  125. The path to be checked against the template.
  126. */
  127. virtual bool FileMatchesTemplate(const wxString& path);
  128. /**
  129. Returns the default file extension for the document data, as passed to
  130. the document template constructor.
  131. */
  132. wxString GetDefaultExtension() const;
  133. /**
  134. Returns the text description of this template, as passed to the
  135. document template constructor.
  136. */
  137. wxString GetDescription() const;
  138. /**
  139. Returns the default directory, as passed to the document template
  140. constructor.
  141. */
  142. wxString GetDirectory() const;
  143. /**
  144. Returns the run-time class information that allows document
  145. instances to be constructed dynamically, as passed to the document
  146. template constructor.
  147. */
  148. wxClassInfo* GetDocClassInfo() const;
  149. /**
  150. Returns a pointer to the document manager instance for which this
  151. template was created.
  152. */
  153. wxDocManager* GetDocumentManager() const;
  154. /**
  155. Returns the document type name, as passed to the document template
  156. constructor.
  157. */
  158. virtual wxString GetDocumentName() const;
  159. /**
  160. Returns the file filter, as passed to the document template
  161. constructor.
  162. */
  163. wxString GetFileFilter() const;
  164. /**
  165. Returns the flags, as passed to the document template constructor.
  166. */
  167. long GetFlags() const;
  168. /**
  169. Returns a reference to the wxPageSetupDialogData associated with the
  170. printing operations of this document manager.
  171. */
  172. //@{
  173. wxPageSetupDialogData& GetPageSetupDialogData();
  174. const wxPageSetupDialogData& GetPageSetupDialogData() const;
  175. //@}
  176. /**
  177. Returns the run-time class information that allows view instances
  178. to be constructed dynamically, as passed to the document template
  179. constructor.
  180. */
  181. wxClassInfo* GetViewClassInfo() const;
  182. /**
  183. Returns the view type name, as passed to the document template
  184. constructor.
  185. */
  186. virtual wxString GetViewName() const;
  187. /**
  188. Initialises the document, calling wxDocument::OnCreate().
  189. This is called from CreateDocument().
  190. If you override this method, notice that you must @em delete the @a doc
  191. if its initialization fails for consistency with the default behaviour.
  192. @param doc
  193. The document to initialize.
  194. @param path
  195. The associated file path.
  196. @param flags
  197. Flags passed to CreateDocument().
  198. @return
  199. @true if the initialization was successful or @false if it failed
  200. in which case @a doc should be deleted by this function.
  201. */
  202. virtual bool InitDocument(wxDocument* doc,
  203. const wxString& path,
  204. long flags = 0);
  205. /**
  206. Returns @true if the document template can be shown in user dialogs,
  207. @false otherwise.
  208. */
  209. bool IsVisible() const;
  210. /**
  211. Sets the default file extension.
  212. */
  213. void SetDefaultExtension(const wxString& ext);
  214. /**
  215. Sets the template description.
  216. */
  217. void SetDescription(const wxString& descr);
  218. /**
  219. Sets the default directory.
  220. */
  221. void SetDirectory(const wxString& dir);
  222. /**
  223. Sets the pointer to the document manager instance for which this
  224. template was created. Should not be called by the application.
  225. */
  226. void SetDocumentManager(wxDocManager* manager);
  227. /**
  228. Sets the file filter.
  229. */
  230. void SetFileFilter(const wxString& filter);
  231. /**
  232. Sets the internal document template flags (see the constructor
  233. description for more details).
  234. */
  235. void SetFlags(long flags);
  236. /**
  237. The default extension for files of this type.
  238. */
  239. wxString m_defaultExt;
  240. /**
  241. A short description of this template.
  242. */
  243. wxString m_description;
  244. /**
  245. The default directory for files of this type.
  246. */
  247. wxString m_directory;
  248. /**
  249. Run-time class information that allows document instances to be
  250. constructed dynamically.
  251. */
  252. wxClassInfo* m_docClassInfo;
  253. /**
  254. The named type of the document associated with this template.
  255. */
  256. wxString m_docTypeName;
  257. /**
  258. A pointer to the document manager for which this template was created.
  259. */
  260. wxDocTemplate* m_documentManager;
  261. /**
  262. The file filter (such as "*.txt") to be used in file selector dialogs.
  263. */
  264. wxString m_fileFilter;
  265. /**
  266. The flags passed to the constructor.
  267. */
  268. long m_flags;
  269. /**
  270. Run-time class information that allows view instances to be constructed
  271. dynamically.
  272. */
  273. wxClassInfo* m_viewClassInfo;
  274. /**
  275. The named type of the view associated with this template.
  276. */
  277. wxString m_viewTypeName;
  278. };
  279. /**
  280. @class wxDocManager
  281. The wxDocManager class is part of the document/view framework supported by
  282. wxWidgets, and cooperates with the wxView, wxDocument and wxDocTemplate
  283. classes.
  284. @library{wxcore}
  285. @category{docview}
  286. @see @ref overview_docview_wxdocmanager, wxDocument, wxView, wxDocTemplate,
  287. wxFileHistory
  288. */
  289. class wxDocManager : public wxEvtHandler
  290. {
  291. public:
  292. /**
  293. Constructor. Create a document manager instance dynamically near the
  294. start of your application before doing any document or view operations.
  295. If @a initialize is @true, the Initialize() function will be called to
  296. create a default history list object. If you derive from wxDocManager,
  297. you may wish to call the base constructor with @false, and then call
  298. Initialize() in your own constructor, to allow your own Initialize() or
  299. OnCreateFileHistory functions to be called.
  300. @param flags
  301. Currently unused.
  302. @param initialize
  303. Indicates whether Initialize() should be called by this ctor.
  304. */
  305. wxDocManager(long flags = 0, bool initialize = true);
  306. /**
  307. Destructor.
  308. */
  309. virtual ~wxDocManager();
  310. /**
  311. Sets the current view.
  312. */
  313. virtual void ActivateView(wxView* doc, bool activate = true);
  314. /**
  315. Adds the document to the list of documents.
  316. */
  317. void AddDocument(wxDocument* doc);
  318. /**
  319. Adds a file to the file history list, if we have a pointer to an
  320. appropriate file menu.
  321. */
  322. virtual void AddFileToHistory(const wxString& filename);
  323. /**
  324. Adds the template to the document manager's template list.
  325. */
  326. void AssociateTemplate(wxDocTemplate* temp);
  327. /**
  328. Search for a particular document template.
  329. Example:
  330. @code
  331. // creating a document instance of the specified document type:
  332. m_doc = (MyDoc*)docManager->FindTemplate(CLASSINFO(MyDoc))->
  333. CreateDocument(wxEmptyString, wxDOC_SILENT);
  334. @endcode
  335. @param classinfo
  336. Class info of a document class for which a wxDocTemplate had been
  337. previously created.
  338. @return
  339. Pointer to a wxDocTemplate, or @NULL if none found.
  340. @since 2.9.2
  341. */
  342. wxDocTemplate* FindTemplate(const wxClassInfo* classinfo);
  343. /**
  344. Search for the document corresponding to the given file.
  345. @param path
  346. Document file path.
  347. @return
  348. Pointer to a wxDocument, or @NULL if none found.
  349. @since 2.9.5
  350. */
  351. wxDocument* FindDocumentByPath(const wxString& path) const;
  352. /**
  353. Closes the specified document.
  354. If @a force is @true, the document is closed even if it has unsaved
  355. changes.
  356. @param doc
  357. The document to close, must be non-@NULL.
  358. @param force
  359. If @true, close the document even if wxDocument::Close() returns
  360. @false.
  361. @return
  362. @true if the document was closed or @false if closing it was
  363. cancelled by user (only in @a force = @false case).
  364. */
  365. bool CloseDocument(wxDocument *doc, bool force = false);
  366. /**
  367. Closes all currently opened documents.
  368. @see CloseDocument()
  369. */
  370. bool CloseDocuments(bool force = true);
  371. /**
  372. Creates a new document.
  373. This function can either create a document corresponding to a new
  374. file or to an already existing one depending on whether @c wxDOC_NEW is
  375. specified in the @a flags.
  376. By default, this function asks the user for the type of document to
  377. open and the path to its file if it's not specified, i.e. if @a path is
  378. empty. Specifying @c wxDOC_SILENT flag suppresses any prompts and means
  379. that the @a path must be non-empty and there must be a registered
  380. document template handling the extension of this file, otherwise a
  381. warning message is logged and the function returns @NULL. Notice that
  382. @c wxDOC_SILENT can be combined with @c wxDOC_NEW, however in this case
  383. the @a path must still be specified, even if the file with this path
  384. typically won't exist.
  385. Finally notice that if this document manager was configured to allow
  386. only a limited number of simultaneously opened documents using
  387. SetMaxDocsOpen(), this function will try to close the oldest existing
  388. document if this number was reached before creating a new document.
  389. And if closing the old document fails (e.g. because it was vetoed by
  390. user), this function fails as well.
  391. @param path
  392. Path to a file or an empty string. If the path is empty, the user
  393. will be asked to select it (thus, this is incompatible with the use
  394. of @c wxDOC_SILENT). The file should exist unless @a flags includes
  395. @c wxDOC_NEW.
  396. @param flags
  397. By default, none. May include @c wxDOC_NEW to indicate that the new
  398. document corresponds to a new file and not an existing one and
  399. @c wxDOC_SILENT to suppress any dialogs asking the user about the
  400. file path and type.
  401. @return a new document object or @NULL on failure.
  402. */
  403. virtual wxDocument* CreateDocument(const wxString& path, long flags = 0);
  404. /**
  405. Creates an empty new document.
  406. This is equivalent to calling CreateDocument() with @c wxDOC_NEW flags
  407. and without the file name.
  408. */
  409. wxDocument *CreateNewDocument();
  410. /**
  411. Creates a new view for the given document. If more than one view is
  412. allowed for the document (by virtue of multiple templates mentioning
  413. the same document type), a choice of view is presented to the user.
  414. */
  415. virtual wxView* CreateView(wxDocument* doc, long flags = 0);
  416. /**
  417. Removes the template from the list of templates.
  418. */
  419. void DisassociateTemplate(wxDocTemplate* temp);
  420. /**
  421. Appends the files in the history list to all menus managed by the file
  422. history object.
  423. */
  424. virtual void FileHistoryAddFilesToMenu();
  425. /**
  426. Appends the files in the history list to the given @a menu only.
  427. */
  428. virtual void FileHistoryAddFilesToMenu(wxMenu* menu);
  429. /**
  430. Loads the file history from a config object.
  431. @see wxConfigBase
  432. */
  433. virtual void FileHistoryLoad(const wxConfigBase& config);
  434. /**
  435. Removes the given menu from the list of menus managed by the file
  436. history object.
  437. */
  438. virtual void FileHistoryRemoveMenu(wxMenu* menu);
  439. /**
  440. Saves the file history into a config object. This must be called
  441. explicitly by the application.
  442. @see wxConfigBase
  443. */
  444. virtual void FileHistorySave(wxConfigBase& resourceFile);
  445. /**
  446. Use this menu for appending recently-visited document filenames, for
  447. convenient access. Calling this function with a valid menu pointer
  448. enables the history list functionality.
  449. @note You can add multiple menus using this function, to be managed by
  450. the file history object.
  451. */
  452. virtual void FileHistoryUseMenu(wxMenu* menu);
  453. /**
  454. Given a path, try to find template that matches the extension. This is
  455. only an approximate method of finding a template for creating a
  456. document.
  457. */
  458. virtual wxDocTemplate* FindTemplateForPath(const wxString& path);
  459. /**
  460. Returns the view to apply a user command to.
  461. This method tries to find the view that the user wants to interact
  462. with. It returns the same view as GetCurrentDocument() if there is any
  463. currently active view but falls back to the first view of the first
  464. document if there is no active view.
  465. @since 2.9.5
  466. */
  467. wxView* GetAnyUsableView() const;
  468. /**
  469. Returns the document associated with the currently active view (if
  470. any).
  471. */
  472. wxDocument* GetCurrentDocument() const;
  473. /**
  474. Returns the currently active view.
  475. This method can return @NULL if no view is currently active.
  476. @see GetAnyUsableView()
  477. */
  478. virtual wxView* GetCurrentView() const;
  479. /**
  480. Returns a vector of wxDocument pointers.
  481. @since 2.9.5
  482. */
  483. wxDocVector GetDocumentsVector() const;
  484. /**
  485. Returns a vector of wxDocTemplate pointers.
  486. @since 2.9.5
  487. */
  488. wxDocTemplateVector GetTemplatesVector() const;
  489. /**
  490. Returns a reference to the list of documents.
  491. */
  492. wxList& GetDocuments();
  493. /**
  494. Returns a pointer to file history.
  495. */
  496. virtual wxFileHistory* GetFileHistory() const;
  497. /**
  498. Returns the number of files currently stored in the file history.
  499. */
  500. virtual size_t GetHistoryFilesCount() const;
  501. /**
  502. Returns the directory last selected by the user when opening a file.
  503. Initially empty.
  504. */
  505. wxString GetLastDirectory() const;
  506. /**
  507. Returns the number of documents that can be open simultaneously.
  508. */
  509. int GetMaxDocsOpen() const;
  510. /**
  511. Returns a reference to the list of associated templates.
  512. */
  513. wxList& GetTemplates();
  514. /**
  515. Initializes data; currently just calls OnCreateFileHistory().
  516. Some data cannot always be initialized in the constructor because the
  517. programmer must be given the opportunity to override functionality. If
  518. OnCreateFileHistory() was called from the constructor, an overridden
  519. virtual OnCreateFileHistory() would not be called due to C++'s
  520. 'interesting' constructor semantics. In fact Initialize() @e is called
  521. from the wxDocManager constructor, but this can be vetoed by passing
  522. @false to the second argument, allowing the derived class's constructor
  523. to call Initialize(), possibly calling a different
  524. OnCreateFileHistory() from the default.
  525. The bottom line: if you're not deriving from Initialize(), forget it
  526. and construct wxDocManager with no arguments.
  527. */
  528. virtual bool Initialize();
  529. /**
  530. Return a string containing a suitable default name for a new document.
  531. By default this is implemented by appending an integer counter to the
  532. string @b unnamed but can be overridden in the derived classes to do
  533. something more appropriate.
  534. */
  535. virtual wxString MakeNewDocumentName();
  536. /**
  537. A hook to allow a derived class to create a different type of file
  538. history. Called from Initialize().
  539. */
  540. virtual wxFileHistory* OnCreateFileHistory();
  541. /**
  542. Closes and deletes the currently active document.
  543. */
  544. void OnFileClose(wxCommandEvent& event);
  545. /**
  546. Closes and deletes all the currently opened documents.
  547. */
  548. void OnFileCloseAll(wxCommandEvent& event);
  549. /**
  550. Creates a document from a list of templates (if more than one
  551. template).
  552. */
  553. void OnFileNew(wxCommandEvent& event);
  554. /**
  555. Creates a new document and reads in the selected file.
  556. */
  557. void OnFileOpen(wxCommandEvent& event);
  558. /**
  559. Reverts the current document by calling wxDocument::Revert() for the
  560. current document.
  561. */
  562. void OnFileRevert(wxCommandEvent& event);
  563. /**
  564. Saves the current document by calling wxDocument::Save() for the
  565. current document.
  566. */
  567. void OnFileSave(wxCommandEvent& event);
  568. /**
  569. Calls wxDocument::SaveAs() for the current document.
  570. */
  571. void OnFileSaveAs(wxCommandEvent& event);
  572. /**
  573. Removes the document from the list of documents.
  574. */
  575. void RemoveDocument(wxDocument* doc);
  576. /**
  577. Under Windows, pops up a file selector with a list of filters
  578. corresponding to document templates. The wxDocTemplate corresponding to
  579. the selected file's extension is returned.
  580. On other platforms, if there is more than one document template a
  581. choice list is popped up, followed by a file selector.
  582. This function is used in CreateDocument().
  583. @beginWxPerlOnly
  584. In wxPerl @a templates is a reference to a list of templates.
  585. If you override this method in your document manager it must
  586. return two values, eg:
  587. @code
  588. (doctemplate, path) = My::DocManager->SelectDocumentPath(...);
  589. @endcode
  590. @endWxPerlOnly
  591. */
  592. virtual wxDocTemplate* SelectDocumentPath(wxDocTemplate** templates,
  593. int noTemplates, wxString& path,
  594. long flags, bool save = false);
  595. /**
  596. Returns a document template by asking the user (if there is more than
  597. one template). This function is used in CreateDocument().
  598. @param templates
  599. Pointer to an array of templates from which to choose a desired
  600. template.
  601. @param noTemplates
  602. Number of templates being pointed to by the templates pointer.
  603. @param sort
  604. If more than one template is passed into templates, then this
  605. parameter indicates whether the list of templates that the user
  606. will have to choose from is sorted or not when shown the choice box
  607. dialog. Default is @false.
  608. @beginWxPerlOnly
  609. In wxPerl @a templates is a reference to a list of templates.
  610. @endWxPerlOnly
  611. */
  612. virtual wxDocTemplate* SelectDocumentType(wxDocTemplate** templates,
  613. int noTemplates,
  614. bool sort = false);
  615. /**
  616. Returns a document template by asking the user (if there is more than
  617. one template), displaying a list of valid views. This function is used
  618. in CreateView(). The dialog normally will not appear because the array
  619. of templates only contains those relevant to the document in question,
  620. and often there will only be one such.
  621. @param templates
  622. Pointer to an array of templates from which to choose a desired
  623. template.
  624. @param noTemplates
  625. Number of templates being pointed to by the templates pointer.
  626. @param sort
  627. If more than one template is passed into templates, then this
  628. parameter indicates whether the list of templates that the user
  629. will have to choose from is sorted or not when shown the choice box
  630. dialog. Default is @false.
  631. @beginWxPerlOnly
  632. In wxPerl @a templates is a reference to a list of templates.
  633. @endWxPerlOnly
  634. */
  635. virtual wxDocTemplate* SelectViewType(wxDocTemplate** templates,
  636. int noTemplates, bool sort = false);
  637. /**
  638. Sets the directory to be displayed to the user when opening a file.
  639. Initially this is empty.
  640. */
  641. void SetLastDirectory(const wxString& dir);
  642. /**
  643. Sets the maximum number of documents that can be open at a time. By
  644. default, this is @c INT_MAX, i.e. the number of documents is unlimited.
  645. If you set it to 1, existing documents will be saved and deleted when
  646. the user tries to open or create a new one (similar to the behaviour of
  647. Windows Write, for example). Allowing multiple documents gives
  648. behaviour more akin to MS Word and other Multiple Document Interface
  649. applications.
  650. */
  651. void SetMaxDocsOpen(int n);
  652. protected:
  653. /**
  654. Called when a file selected from the MRU list doesn't exist any more.
  655. The default behaviour is to remove the file from the MRU (most recently
  656. used) files list and the corresponding menu and notify the user about
  657. it but this method can be overridden to customize it.
  658. For example, an application may want to just give an error about the
  659. missing file @a filename but not remove it from the file history. Or it
  660. could ask the user whether the file should be kept or removed.
  661. Notice that this method is called only if the file selected by user
  662. from the MRU files in the menu doesn't exist, but not if opening it
  663. failed for any other reason because in the latter case the default
  664. behaviour of removing the file from the MRU list is inappropriate.
  665. If you still want to do it, you would need to do it by calling
  666. RemoveFileFromHistory() explicitly in the part of the file opening code
  667. that may fail.
  668. @since 2.9.3
  669. @param n
  670. The index of the file in the MRU list, it can be passed to
  671. RemoveFileFromHistory() to remove this file from the list.
  672. @param filename
  673. The full name of the file.
  674. */
  675. virtual void OnMRUFileNotExist(unsigned n, const wxString& filename);
  676. /**
  677. Create the frame used for print preview.
  678. This method can be overridden if you need to change the behaviour or
  679. appearance of the preview window. By default, a standard wxPreviewFrame
  680. is created.
  681. @since 2.9.1
  682. @param preview The associated preview object.
  683. @param parent The parent window for the frame.
  684. @param title The suggested title for the print preview frame.
  685. @return A new print preview frame, must not return @NULL.
  686. */
  687. virtual wxPreviewFrame* CreatePreviewFrame(wxPrintPreviewBase* preview,
  688. wxWindow* parent,
  689. const wxString& title);
  690. /**
  691. The currently active view.
  692. */
  693. wxView* m_currentView;
  694. /**
  695. Stores the integer to be used for the next default document name.
  696. */
  697. int m_defaultDocumentNameCounter;
  698. /**
  699. A list of all documents.
  700. */
  701. wxList m_docs;
  702. /**
  703. A pointer to an instance of wxFileHistory, which manages the history of
  704. recently-visited files on the File menu.
  705. */
  706. wxFileHistory* m_fileHistory;
  707. /**
  708. The directory last selected by the user when opening a file.
  709. */
  710. wxString m_lastDirectory;
  711. /**
  712. Stores the maximum number of documents that can be opened before
  713. existing documents are closed.
  714. By default, this is @c INT_MAX i.e. practically unlimited.
  715. */
  716. int m_maxDocsOpen;
  717. };
  718. /**
  719. @class wxView
  720. The view class can be used to model the viewing and editing component of
  721. an application's file-based data. It is part of the document/view framework
  722. supported by wxWidgets, and cooperates with the wxDocument, wxDocTemplate
  723. and wxDocManager classes.
  724. @library{wxcore}
  725. @category{docview}
  726. @see @ref overview_docview_wxview, wxDocument, wxDocTemplate, wxDocManager
  727. */
  728. class wxView : public wxEvtHandler
  729. {
  730. public:
  731. /**
  732. Constructor. Define your own default constructor to initialize
  733. application-specific data.
  734. */
  735. wxView();
  736. /**
  737. Destructor. Removes itself from the document's list of views.
  738. */
  739. virtual ~wxView();
  740. /**
  741. Call this from your view frame's wxDocChildFrame::OnActivate() member
  742. to tell the framework which view is currently active. If your windowing
  743. system doesn't call wxDocChildFrame::OnActivate(), you may need to call
  744. this function from any place where you know the view must be active,
  745. and the framework will need to get the current view.
  746. The prepackaged view frame wxDocChildFrame calls Activate() from its
  747. wxDocChildFrame::OnActivate() member.
  748. This function calls OnActivateView().
  749. */
  750. virtual void Activate(bool activate);
  751. /**
  752. Closes the view by calling OnClose(). If @a deleteWindow is @true, this
  753. function should delete the window associated with the view.
  754. */
  755. virtual bool Close(bool deleteWindow = true);
  756. /**
  757. Gets a pointer to the document associated with the view.
  758. */
  759. wxDocument* GetDocument() const;
  760. /**
  761. Returns a pointer to the document manager instance associated with this
  762. view.
  763. */
  764. wxDocManager* GetDocumentManager() const;
  765. /**
  766. Gets the frame associated with the view (if any). Note that this
  767. "frame" is not a wxFrame at all in the generic MDI implementation which
  768. uses notebook pages instead of frames and this is why this method
  769. returns a wxWindow and not a wxFrame.
  770. */
  771. wxWindow* GetFrame() const;
  772. /**
  773. Gets the name associated with the view (passed to the wxDocTemplate
  774. constructor). Not currently used by the framework.
  775. */
  776. wxString GetViewName() const;
  777. /**
  778. Called when a view is activated by means of Activate(). The default
  779. implementation does nothing.
  780. */
  781. virtual void OnActivateView(bool activate, wxView* activeView,
  782. wxView* deactiveView);
  783. /**
  784. Called when the filename has changed. The default implementation
  785. constructs a suitable title and sets the title of the view frame (if any).
  786. */
  787. virtual void OnChangeFilename();
  788. /**
  789. Implements closing behaviour. The default implementation calls
  790. wxDocument::Close() to close the associated document. Does not delete
  791. the view. The application may wish to do some cleaning up operations in
  792. this function, @e if a call to wxDocument::Close() succeeded. For
  793. example, if your views all share the same window, you need to
  794. disassociate the window from the view and perhaps clear the window. If
  795. @a deleteWindow is @true, delete the frame associated with the view.
  796. */
  797. virtual bool OnClose(bool deleteWindow);
  798. /**
  799. Override this to clean up the view when the document is being closed.
  800. */
  801. virtual void OnClosingDocument();
  802. /**
  803. wxDocManager or wxDocument creates a wxView via a wxDocTemplate. Just
  804. after the wxDocTemplate creates the wxView, it calls OnCreate(). The
  805. wxView can create a wxDocChildFrame (or derived class) in its
  806. wxView::OnCreate() member function. This wxDocChildFrame provides user
  807. interface elements to view and/or edit the contents of the wxDocument.
  808. By default, simply returns @true. If the function returns @false, the
  809. view will be deleted.
  810. */
  811. virtual bool OnCreate(wxDocument* doc, long flags);
  812. /**
  813. If the printing framework is enabled in the library, this function
  814. returns a wxPrintout object for the purposes of printing. It should
  815. create a new object every time it is called; the framework will delete
  816. objects it creates.
  817. By default, this function returns an instance of wxDocPrintout, which
  818. prints and previews one page by calling OnDraw().
  819. Override to return an instance of a class other than wxDocPrintout.
  820. */
  821. virtual wxPrintout* OnCreatePrintout();
  822. /**
  823. Override this function to render the view on the given device context.
  824. */
  825. virtual void OnDraw(wxDC* dc) = 0;
  826. /**
  827. Called when the view should be updated.
  828. @param sender
  829. A pointer to the wxView that sent the update request, or @NULL if
  830. no single view requested the update (for instance, when the
  831. document is opened).
  832. @param hint
  833. This is unused currently, but may in future contain
  834. application-specific information for making updating more
  835. efficient.
  836. */
  837. virtual void OnUpdate(wxView* sender, wxObject* hint = 0);
  838. /**
  839. Associates the given document with the view. Normally called by the
  840. framework.
  841. */
  842. virtual void SetDocument(wxDocument* doc);
  843. /**
  844. Sets the frame associated with this view. The application should call
  845. this if possible, to tell the view about the frame.
  846. See GetFrame() for the explanation about the mismatch between the
  847. "Frame" in the method name and the type of its parameter.
  848. */
  849. void SetFrame(wxWindow* frame);
  850. /**
  851. Sets the view type name. Should only be called by the framework.
  852. */
  853. void SetViewName(const wxString& name);
  854. /**
  855. The document associated with this view. There may be more than one view
  856. per document, but there can never be more than one document for one
  857. view.
  858. */
  859. wxDocument* m_viewDocument;
  860. /**
  861. Frame associated with the view, if any.
  862. */
  863. wxFrame* m_viewFrame;
  864. /**
  865. The view type name given to the wxDocTemplate constructor, copied to
  866. this variable when the view is created. Not currently used by the
  867. framework.
  868. */
  869. wxString m_viewTypeName;
  870. };
  871. /**
  872. @class wxDocChildFrame
  873. The wxDocChildFrame class provides a default frame for displaying documents
  874. on separate windows. This class can only be used for SDI (not MDI) child
  875. frames.
  876. The class is part of the document/view framework supported by wxWidgets,
  877. and cooperates with the wxView, wxDocument, wxDocManager and wxDocTemplate
  878. classes.
  879. Notice that this class handles ::wxEVT_ACTIVATE event and activates the
  880. child view on receiving it. Don't intercept this event unless you want to
  881. prevent from this happening.
  882. The same remark applies to ::wxEVT_CLOSE_WINDOW, as wxDocParentFrame the
  883. frame handles this event by trying to close the associated view.
  884. @library{wxcore}
  885. @category{docview}
  886. @see @ref overview_docview, @ref page_samples_docview, wxFrame
  887. */
  888. class wxDocChildFrame : public wxFrame
  889. {
  890. public:
  891. /**
  892. Constructor.
  893. */
  894. wxDocChildFrame(wxDocument* doc, wxView* view, wxFrame* parent,
  895. wxWindowID id, const wxString& title,
  896. const wxPoint& pos = wxDefaultPosition,
  897. const wxSize& size = wxDefaultSize,
  898. long style = wxDEFAULT_FRAME_STYLE,
  899. const wxString& name = wxFrameNameStr);
  900. /**
  901. Destructor.
  902. */
  903. virtual ~wxDocChildFrame();
  904. /**
  905. Returns the document associated with this frame.
  906. */
  907. wxDocument* GetDocument() const;
  908. /**
  909. Returns the view associated with this frame.
  910. */
  911. wxView* GetView() const;
  912. /**
  913. Sets the document for this frame.
  914. */
  915. void SetDocument(wxDocument* doc);
  916. /**
  917. Sets the view for this frame.
  918. */
  919. void SetView(wxView* view);
  920. /**
  921. The document associated with the frame.
  922. */
  923. wxDocument* m_childDocument;
  924. /**
  925. The view associated with the frame.
  926. */
  927. wxView* m_childView;
  928. };
  929. /**
  930. @class wxDocParentFrame
  931. The wxDocParentFrame class provides a default top-level frame for
  932. applications using the document/view framework. This class can only be used
  933. for SDI (not MDI) parent frames.
  934. It cooperates with the wxView, wxDocument, wxDocManager and wxDocTemplate
  935. classes.
  936. Notice that this class processes ::wxEVT_CLOSE_WINDOW event and tries to
  937. close all open views from its handler. If all the views can be closed, i.e.
  938. if none of them contains unsaved changes or the user decides to not save
  939. them, the window is destroyed. Don't intercept this event in your code
  940. unless you want to replace this logic.
  941. @library{wxcore}
  942. @category{docview}
  943. @see @ref overview_docview, @ref page_samples_docview, wxFrame
  944. */
  945. class wxDocParentFrame : public wxFrame
  946. {
  947. public:
  948. /**
  949. Default constructor.
  950. */
  951. wxDocParentFrame();
  952. /**
  953. Constructor.
  954. */
  955. wxDocParentFrame(wxDocManager* manager, wxFrame* parent,
  956. wxWindowID id, const wxString& title,
  957. const wxPoint& pos = wxDefaultPosition,
  958. const wxSize& size = wxDefaultSize,
  959. long style = wxDEFAULT_FRAME_STYLE,
  960. const wxString& name = wxFrameNameStr);
  961. /**
  962. Destructor.
  963. */
  964. virtual ~wxDocParentFrame();
  965. /**
  966. Used in two-step construction.
  967. */
  968. bool Create(wxDocManager* manager, wxFrame* parent, wxWindowID id,
  969. const wxString& title, const wxPoint& pos = wxDefaultPosition,
  970. const wxSize& size = wxDefaultSize, long style = 541072960,
  971. const wxString& name = wxFrameNameStr);
  972. /**
  973. Returns the associated document manager object.
  974. */
  975. wxDocManager* GetDocumentManager() const;
  976. };
  977. /**
  978. @class wxDocument
  979. The document class can be used to model an application's file-based data.
  980. It is part of the document/view framework supported by wxWidgets, and
  981. cooperates with the wxView, wxDocTemplate and wxDocManager classes.
  982. A normal document is the one created without parent document and is
  983. associated with a disk file. Since version 2.9.2 wxWidgets also supports a
  984. special kind of documents called <em>child documents</em> which are virtual
  985. in the sense that they do not correspond to a file but rather to a part of
  986. their parent document. Because of this, the child documents can't be
  987. created directly by user but can only be created by the parent document
  988. (usually when it's being created itself). They also can't be independently
  989. saved. A child document has its own view with the corresponding window.
  990. This view can be closed by user but, importantly, is also automatically
  991. closed when its parent document is closed. Thus, child documents may be
  992. convenient for creating additional windows which need to be closed when the
  993. main document is. The docview sample demonstrates this use of child
  994. documents by creating a child document containing the information about the
  995. parameters of the image opened in the main document.
  996. @library{wxcore}
  997. @category{docview}
  998. @see @ref overview_docview, wxView, wxDocTemplate, wxDocManager
  999. */
  1000. class wxDocument : public wxEvtHandler
  1001. {
  1002. public:
  1003. /**
  1004. Constructor. Define your own default constructor to initialize
  1005. application-specific data.
  1006. @param parent
  1007. Specifying a non-@c NULL parent document here makes this document a
  1008. special <em>child document</em>, see their description in the class
  1009. documentation. Notice that this parameter exists but is ignored in
  1010. wxWidgets versions prior to 2.9.1.
  1011. */
  1012. wxDocument(wxDocument* parent = NULL);
  1013. /**
  1014. Destructor. Removes itself from the document manager.
  1015. */
  1016. virtual ~wxDocument();
  1017. /**
  1018. If the view is not already in the list of views, adds the view and
  1019. calls OnChangedViewList().
  1020. */
  1021. virtual bool AddView(wxView* view);
  1022. /**
  1023. Returns true if the document hasn't been modified since the last time
  1024. it had been saved.
  1025. Notice that this function returns @false if the document had been never
  1026. saved at all, so it may be also used to test whether it makes sense to
  1027. save the document: if it returns @true, there is nothing to save but if
  1028. @false is returned, it can be saved, even if it might be not modified
  1029. (this can be used to create an empty document file by the user).
  1030. @see IsModified(), GetDocumentSaved()
  1031. @since 2.9.0
  1032. */
  1033. bool AlreadySaved() const;
  1034. /**
  1035. Activate the first view of the document if any.
  1036. This function simply calls the Raise() method of the frame of the first
  1037. view. You may need to override the Raise() method to get the desired
  1038. effect if you are not using a standard wxFrame for your view. For
  1039. instance, if your document is inside its own notebook tab you could
  1040. implement Raise() like this:
  1041. @code
  1042. void MyNotebookPage::Raise()
  1043. {
  1044. wxNotebook* notebook = wxStaticCast(GetParent(), wxNotebook);
  1045. notebook->SetSelection(notebook->FindPage(this));
  1046. }
  1047. @endcode
  1048. @see GetFirstView()
  1049. @since 2.9.5
  1050. */
  1051. void Activate() const;
  1052. /**
  1053. Closes the document, by calling OnSaveModified() and then (if this
  1054. returned @true) OnCloseDocument(). This does not normally delete the
  1055. document object, use DeleteAllViews() to do this implicitly.
  1056. */
  1057. virtual bool Close();
  1058. /**
  1059. Calls wxView::Close() and deletes each view. Deleting the final view
  1060. will implicitly delete the document itself, because the wxView
  1061. destructor calls RemoveView(). This in turns calls OnChangedViewList(),
  1062. whose default implemention is to save and delete the document if no
  1063. views exist.
  1064. */
  1065. virtual bool DeleteAllViews();
  1066. /**
  1067. Virtual method called from OnCloseDocument().
  1068. This method may be overridden to perform any additional cleanup which
  1069. might be needed when the document is closed.
  1070. The return value of this method is currently ignored.
  1071. The default version does nothing and simply returns @true.
  1072. */
  1073. virtual bool DeleteContents();
  1074. /**
  1075. Returns a pointer to the command processor associated with this
  1076. document.
  1077. @see wxCommandProcessor
  1078. */
  1079. virtual wxCommandProcessor* GetCommandProcessor() const;
  1080. /**
  1081. Gets a pointer to the associated document manager.
  1082. */
  1083. virtual wxDocManager* GetDocumentManager() const;
  1084. /**
  1085. Gets the document type name for this document. See the comment for
  1086. @ref m_documentTypeName.
  1087. */
  1088. wxString GetDocumentName() const;
  1089. /**
  1090. Return true if this document had been already saved.
  1091. @see IsModified()
  1092. */
  1093. bool GetDocumentSaved() const;
  1094. /**
  1095. Gets a pointer to the template that created the document.
  1096. */
  1097. virtual wxDocTemplate* GetDocumentTemplate() const;
  1098. /**
  1099. Intended to return a suitable window for using as a parent for
  1100. document-related dialog boxes. By default, uses the frame associated
  1101. with the first view.
  1102. */
  1103. virtual wxWindow* GetDocumentWindow() const;
  1104. /**
  1105. Gets the filename associated with this document, or "" if none is
  1106. associated.
  1107. */
  1108. wxString GetFilename() const;
  1109. /**
  1110. A convenience function to get the first view for a document, because in
  1111. many cases a document will only have a single view.
  1112. @see GetViews()
  1113. */
  1114. wxView* GetFirstView() const;
  1115. /**
  1116. Gets the title for this document. The document title is used for an
  1117. associated frame (if any), and is usually constructed by the framework
  1118. from the filename.
  1119. */
  1120. wxString GetTitle() const;
  1121. /**
  1122. Return the document name suitable to be shown to the user. The default
  1123. implementation uses the document title, if any, of the name part of the
  1124. document filename if it was set or, otherwise, the string @b unnamed.
  1125. */
  1126. virtual wxString GetUserReadableName() const;
  1127. /**
  1128. Returns a vector of wxView pointers.
  1129. @since 2.9.5
  1130. */
  1131. wxViewVector GetViewsVector() const;
  1132. //@{
  1133. /**
  1134. Returns the list whose elements are the views on the document.
  1135. @see GetFirstView()
  1136. */
  1137. wxList& GetViews();
  1138. const wxList& GetViews() const;
  1139. //@}
  1140. /**
  1141. Returns true if this document is a child document corresponding to a
  1142. part of the parent document and not a disk file as usual.
  1143. This method can be used to check whether file-related operations make
  1144. sense for this document as they only apply to top-level documents and
  1145. not child ones.
  1146. @since 2.9.2
  1147. */
  1148. bool IsChildDocument() const;
  1149. /**
  1150. Returns @true if the document has been modified since the last save,
  1151. @false otherwise. You may need to override this if your document view
  1152. maintains its own record of being modified.
  1153. @see Modify()
  1154. */
  1155. virtual bool IsModified() const;
  1156. //@{
  1157. /**
  1158. Override this function and call it from your own LoadObject() before
  1159. streaming your own data. LoadObject() is called by the framework
  1160. automatically when the document contents need to be loaded.
  1161. @note This version of LoadObject() may not exist depending on how
  1162. wxWidgets was configured.
  1163. */
  1164. virtual istream& LoadObject(istream& stream);
  1165. virtual wxInputStream& LoadObject(wxInputStream& stream);
  1166. //@}
  1167. /**
  1168. Call with @true to mark the document as modified since the last save,
  1169. @false otherwise. You may need to override this if your document view
  1170. maintains its own record of being modified.
  1171. @see IsModified()
  1172. */
  1173. virtual void Modify(bool modify);
  1174. /**
  1175. Called when a view is added to or deleted from this document. The
  1176. default implementation saves and deletes the document if no views exist
  1177. (the last one has just been removed).
  1178. */
  1179. virtual void OnChangedViewList();
  1180. /**
  1181. This virtual function is called when the document is being closed.
  1182. The default implementation calls DeleteContents() (which may be
  1183. overridden to perform additional cleanup) and sets the modified flag to
  1184. @false. You can override it to supply additional behaviour when the
  1185. document is closed with Close().
  1186. Notice that previous wxWidgets versions used to call this function also
  1187. from OnNewDocument(), rather counter-intuitively. This is no longer the
  1188. case since wxWidgets 2.9.0.
  1189. */
  1190. virtual bool OnCloseDocument();
  1191. /**
  1192. Called just after the document object is created to give it a chance to
  1193. initialize itself.
  1194. The default implementation uses the template associated with the
  1195. document to create an initial view.
  1196. For compatibility reasons, this method may either delete the document
  1197. itself if its initialization fails or not do it in which case it is
  1198. deleted by caller. It is recommended to delete the document explicitly
  1199. in this function if it can't be initialized.
  1200. @param path
  1201. The associated file path.
  1202. @param flags
  1203. Flags passed to CreateDocument().
  1204. @return
  1205. @true if the initialization was successful or @false if it failed.
  1206. */
  1207. virtual bool OnCreate(const wxString& path, long flags);
  1208. /**
  1209. Override this function if you want a different (or no) command
  1210. processor to be created when the document is created. By default, it
  1211. returns an instance of wxCommandProcessor.
  1212. @see wxCommandProcessor
  1213. */
  1214. virtual wxCommandProcessor* OnCreateCommandProcessor();
  1215. /**
  1216. The default implementation calls OnSaveModified() and DeleteContents(),
  1217. makes a default title for the document, and notifies the views that the
  1218. filename (in fact, the title) has changed.
  1219. */
  1220. virtual bool OnNewDocument();
  1221. /**
  1222. Constructs an input file stream for the given filename (which must not
  1223. be empty), and calls LoadObject(). If LoadObject() returns @true, the
  1224. document is set to unmodified; otherwise, an error message box is
  1225. displayed. The document's views are notified that the filename has
  1226. changed, to give windows an opportunity to update their titles. All of
  1227. the document's views are then updated.
  1228. */
  1229. virtual bool OnOpenDocument(const wxString& filename);
  1230. /**
  1231. Constructs an output file stream for the given filename (which must not
  1232. be empty), and calls SaveObject(). If SaveObject() returns @true, the
  1233. document is set to unmodified; otherwise, an error message box is
  1234. displayed.
  1235. */
  1236. virtual bool OnSaveDocument(const wxString& filename);
  1237. /**
  1238. If the document has been modified, prompts the user to ask if the
  1239. changes should be saved. If the user replies Yes, the Save() function
  1240. is called. If No, the document is marked as unmodified and the function
  1241. succeeds. If Cancel, the function fails.
  1242. */
  1243. virtual bool OnSaveModified();
  1244. /**
  1245. Removes the view from the document's list of views, and calls
  1246. OnChangedViewList().
  1247. */
  1248. virtual bool RemoveView(wxView* view);
  1249. /**
  1250. Saves the document by calling OnSaveDocument() if there is an
  1251. associated filename, or SaveAs() if there is no filename.
  1252. */
  1253. virtual bool Save();
  1254. /**
  1255. Prompts the user for a file to save to, and then calls
  1256. OnSaveDocument().
  1257. */
  1258. virtual bool SaveAs();
  1259. /**
  1260. Discard changes and load last saved version.
  1261. Prompts the user first, and then calls DoOpenDocument() to reload the
  1262. current file.
  1263. */
  1264. virtual bool Revert();
  1265. //@{
  1266. /**
  1267. Override this function and call it from your own SaveObject() before
  1268. streaming your own data. SaveObject() is called by the framework
  1269. automatically when the document contents need to be saved.
  1270. @note This version of SaveObject() may not exist depending on how
  1271. wxWidgets was configured.
  1272. */
  1273. virtual ostream& SaveObject(ostream& stream);
  1274. virtual wxOutputStream& SaveObject(wxOutputStream& stream);
  1275. //@}
  1276. /**
  1277. Sets the command processor to be used for this document. The document
  1278. will then be responsible for its deletion. Normally you should not call
  1279. this; override OnCreateCommandProcessor() instead.
  1280. @see wxCommandProcessor
  1281. */
  1282. virtual void SetCommandProcessor(wxCommandProcessor* processor);
  1283. /**
  1284. Sets the document type name for this document. See the comment for
  1285. @ref m_documentTypeName.
  1286. */
  1287. void SetDocumentName(const wxString& name);
  1288. /**
  1289. Sets the pointer to the template that created the document. Should only
  1290. be called by the framework.
  1291. */
  1292. virtual void SetDocumentTemplate(wxDocTemplate* templ);
  1293. /**
  1294. Sets if this document has been already saved or not.
  1295. Normally there is no need to call this function as the document-view
  1296. framework does it itself as the documents are loaded from and saved to
  1297. the files. However it may be useful in some particular cases, for
  1298. example it may be called with @false argument to prevent the user
  1299. from saving the just opened document into the same file if this
  1300. shouldn't be done for some reason (e.g. file format version changes and
  1301. a new extension should be used for saving).
  1302. @see GetDocumentSaved(), AlreadySaved()
  1303. */
  1304. void SetDocumentSaved(bool saved = true);
  1305. /**
  1306. Sets the filename for this document. Usually called by the framework.
  1307. Calls OnChangeFilename() which in turn calls wxView::OnChangeFilename() for
  1308. all views if @a notifyViews is @true.
  1309. */
  1310. void SetFilename(const wxString& filename, bool notifyViews = false);
  1311. /**
  1312. If @a notifyViews is @true, wxView::OnChangeFilename() is called for
  1313. all views.
  1314. @since 2.9.0
  1315. */
  1316. virtual void OnChangeFilename(bool notifyViews);
  1317. /**
  1318. Sets the title for this document. The document title is used for an
  1319. associated frame (if any), and is usually constructed by the framework
  1320. from the filename.
  1321. */
  1322. void SetTitle(const wxString& title);
  1323. /**
  1324. Updates all views. If @a sender is non-@NULL, does not update this
  1325. view. @a hint represents optional information to allow a view to
  1326. optimize its update.
  1327. */
  1328. virtual void UpdateAllViews(wxView* sender = NULL, wxObject* hint = NULL);
  1329. protected:
  1330. /**
  1331. This method is called by OnSaveDocument() to really save the document
  1332. contents to the specified file.
  1333. Base class version creates a file-based stream and calls SaveObject().
  1334. Override this if you need to do something else or prefer not to use
  1335. SaveObject() at all.
  1336. */
  1337. virtual bool DoSaveDocument(const wxString& file);
  1338. /**
  1339. This method is called by OnOpenDocument() to really load the document
  1340. contents from the specified file.
  1341. Base class version creates a file-based stream and calls LoadObject().
  1342. Override this if you need to do something else or prefer not to use
  1343. LoadObject() at all.
  1344. */
  1345. virtual bool DoOpenDocument(const wxString& file);
  1346. /**
  1347. A pointer to the command processor associated with this document.
  1348. */
  1349. wxCommandProcessor* m_commandProcessor;
  1350. /**
  1351. Filename associated with this document ("" if none).
  1352. */
  1353. wxString m_documentFile;
  1354. /**
  1355. @true if the document has been modified, @false otherwise.
  1356. */
  1357. bool m_documentModified;
  1358. /**
  1359. A pointer to the template from which this document was created.
  1360. */
  1361. wxDocTemplate* m_documentTemplate;
  1362. /**
  1363. Document title. The document title is used for an associated frame (if
  1364. any), and is usually constructed by the framework from the filename.
  1365. */
  1366. wxString m_documentTitle;
  1367. /**
  1368. The document type name given to the wxDocTemplate constructor, copied
  1369. to this variable when the document is created. If several document
  1370. templates are created that use the same document type, this variable is
  1371. used in wxDocManager::CreateView() to collate a list of alternative
  1372. view types that can be used on this kind of document. Do not change the
  1373. value of this variable.
  1374. */
  1375. wxString m_documentTypeName;
  1376. /**
  1377. List of wxView instances associated with this document.
  1378. */
  1379. wxList m_documentViews;
  1380. };
  1381. // ============================================================================
  1382. // Global functions/macros
  1383. // ============================================================================
  1384. /** @addtogroup group_funcmacro_file */
  1385. //@{
  1386. /**
  1387. Copies the given file to @a stream. Useful when converting an old
  1388. application to use streams (within the document/view framework, for
  1389. example).
  1390. @header{wx/docview.h}
  1391. */
  1392. bool wxTransferFileToStream(const wxString& filename,
  1393. ostream& stream);
  1394. /**
  1395. Copies the given stream to the file @a filename. Useful when converting an
  1396. old application to use streams (within the document/view framework, for
  1397. example).
  1398. @header{wx/docview.h}
  1399. */
  1400. bool wxTransferStreamToFile(istream& stream,
  1401. const wxString& filename);
  1402. //@}