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