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