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