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