]> git.saurik.com Git - wxWidgets.git/blob - include/wx/docview.h
Blind compilation fix for VC6 after r73004.
[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 {
152 return m_documentViews.AsVector<wxView*>();
153 }
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 wxDocument *GetCurrentDocument() const;
450
451 void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; }
452 int GetMaxDocsOpen() const { return m_maxDocsOpen; }
453
454 // Add and remove a document from the manager's list
455 void AddDocument(wxDocument *doc);
456 void RemoveDocument(wxDocument *doc);
457
458 // closes all currently open documents
459 bool CloseDocuments(bool force = true);
460
461 // closes the specified document
462 bool CloseDocument(wxDocument* doc, bool force = false);
463
464 // Clear remaining documents and templates
465 bool Clear(bool force = true);
466
467 // Views or windows should inform the document manager
468 // when a view is going in or out of focus
469 virtual void ActivateView(wxView *view, bool activate = true);
470 virtual wxView *GetCurrentView() const { return m_currentView; }
471
472 #ifndef __VISUALC6__
473 wxDocVector GetDocumentsVector() const
474 {
475 return m_docs.AsVector<wxDocument*>();
476 }
477
478 wxDocTemplateVector GetTemplatesVector() const
479 {
480 return m_templates.AsVector<wxDocTemplate*>();
481 }
482 #endif // !__VISUALC6__
483
484 wxList& GetDocuments() { return m_docs; }
485 wxList& GetTemplates() { return m_templates; }
486
487 // Return the default name for a new document (by default returns strings
488 // in the form "unnamed <counter>" but can be overridden)
489 virtual wxString MakeNewDocumentName();
490
491 // Make a frame title (override this to do something different)
492 virtual wxString MakeFrameTitle(wxDocument* doc);
493
494 virtual wxFileHistory *OnCreateFileHistory();
495 virtual wxFileHistory *GetFileHistory() const { return m_fileHistory; }
496
497 // File history management
498 virtual void AddFileToHistory(const wxString& file);
499 virtual void RemoveFileFromHistory(size_t i);
500 virtual size_t GetHistoryFilesCount() const;
501 virtual wxString GetHistoryFile(size_t i) const;
502 virtual void FileHistoryUseMenu(wxMenu *menu);
503 virtual void FileHistoryRemoveMenu(wxMenu *menu);
504 #if wxUSE_CONFIG
505 virtual void FileHistoryLoad(const wxConfigBase& config);
506 virtual void FileHistorySave(wxConfigBase& config);
507 #endif // wxUSE_CONFIG
508
509 virtual void FileHistoryAddFilesToMenu();
510 virtual void FileHistoryAddFilesToMenu(wxMenu* menu);
511
512 wxString GetLastDirectory() const;
513 void SetLastDirectory(const wxString& dir) { m_lastDirectory = dir; }
514
515 // Get the current document manager
516 static wxDocManager* GetDocumentManager() { return sm_docManager; }
517
518 #if wxUSE_PRINTING_ARCHITECTURE
519 wxPageSetupDialogData& GetPageSetupDialogData()
520 { return m_pageSetupDialogData; }
521 const wxPageSetupDialogData& GetPageSetupDialogData() const
522 { return m_pageSetupDialogData; }
523 #endif // wxUSE_PRINTING_ARCHITECTURE
524
525 #if WXWIN_COMPATIBILITY_2_8
526 // deprecated, override GetDefaultName() instead
527 wxDEPRECATED_BUT_USED_INTERNALLY(
528 virtual bool MakeDefaultName(wxString& buf)
529 );
530 #endif
531
532 #if WXWIN_COMPATIBILITY_2_6
533 // deprecated, use GetHistoryFilesCount() instead
534 wxDEPRECATED( size_t GetNoHistoryFiles() const );
535 #endif // WXWIN_COMPATIBILITY_2_6
536
537
538 protected:
539 // Called when a file selected from the MRU list doesn't exist any more.
540 // The default behaviour is to remove the file from the MRU and notify the
541 // user about it but this method can be overridden to customize it.
542 virtual void OnMRUFileNotExist(unsigned n, const wxString& filename);
543
544 // Open the MRU file with the given index in our associated file history.
545 void DoOpenMRUFile(unsigned n);
546 #if wxUSE_PRINTING_ARCHITECTURE
547 virtual wxPreviewFrame* CreatePreviewFrame(wxPrintPreviewBase* preview,
548 wxWindow *parent,
549 const wxString& title);
550 #endif // wxUSE_PRINTING_ARCHITECTURE
551
552 // hook the currently active view into event handlers chain here
553 virtual bool TryBefore(wxEvent& event);
554
555 // return the command processor for the current document, if any
556 wxCommandProcessor *GetCurrentCommandProcessor() const;
557
558 // this method tries to find an active view harder than GetCurrentView():
559 // if the latter is NULL, it also checks if we don't have just a single
560 // view and returns it then
561 wxView *GetActiveView() const;
562
563 // activate the first view of the given document if any
564 void ActivateDocument(wxDocument *doc);
565
566
567 int m_defaultDocumentNameCounter;
568 int m_maxDocsOpen;
569 wxList m_docs;
570 wxList m_templates;
571 wxView* m_currentView;
572 wxFileHistory* m_fileHistory;
573 wxString m_lastDirectory;
574 static wxDocManager* sm_docManager;
575
576 #if wxUSE_PRINTING_ARCHITECTURE
577 wxPageSetupDialogData m_pageSetupDialogData;
578 #endif // wxUSE_PRINTING_ARCHITECTURE
579
580 DECLARE_EVENT_TABLE()
581 DECLARE_DYNAMIC_CLASS(wxDocManager)
582 wxDECLARE_NO_COPY_CLASS(wxDocManager);
583 };
584
585 #if WXWIN_COMPATIBILITY_2_6
586 inline size_t wxDocManager::GetNoHistoryFiles() const
587 {
588 return GetHistoryFilesCount();
589 }
590 #endif // WXWIN_COMPATIBILITY_2_6
591
592 // ----------------------------------------------------------------------------
593 // Base class for child frames -- this is what wxView renders itself into
594 //
595 // Notice that this is a mix-in class so it doesn't derive from wxWindow, only
596 // wxDocChildFrameAny does
597 // ----------------------------------------------------------------------------
598
599 class WXDLLIMPEXP_CORE wxDocChildFrameAnyBase
600 {
601 public:
602 // default ctor, use Create() after it
603 wxDocChildFrameAnyBase()
604 {
605 m_childDocument = NULL;
606 m_childView = NULL;
607 m_win = NULL;
608 }
609
610 // full ctor equivalent to using the default one and Create(0
611 wxDocChildFrameAnyBase(wxDocument *doc, wxView *view, wxWindow *win)
612 {
613 Create(doc, view, win);
614 }
615
616 // method which must be called for an object created using the default ctor
617 //
618 // note that it returns bool just for consistency with Create() methods in
619 // other classes, we never return false from here
620 bool Create(wxDocument *doc, wxView *view, wxWindow *win)
621 {
622 m_childDocument = doc;
623 m_childView = view;
624 m_win = win;
625
626 if ( view )
627 view->SetDocChildFrame(this);
628
629 return true;
630 }
631
632 // dtor doesn't need to be virtual, an object should never be destroyed via
633 // a pointer to this class
634 ~wxDocChildFrameAnyBase()
635 {
636 // prevent the view from deleting us if we're being deleted directly
637 // (and not via Close() + Destroy())
638 if ( m_childView )
639 m_childView->SetDocChildFrame(NULL);
640 }
641
642 wxDocument *GetDocument() const { return m_childDocument; }
643 wxView *GetView() const { return m_childView; }
644 void SetDocument(wxDocument *doc) { m_childDocument = doc; }
645 void SetView(wxView *view) { m_childView = view; }
646
647 wxWindow *GetWindow() const { return m_win; }
648
649 protected:
650 // we're not a wxEvtHandler but we provide this wxEvtHandler-like function
651 // which is called from TryBefore() of the derived classes to give our view
652 // a chance to process the message before the frame event handlers are used
653 bool TryProcessEvent(wxEvent& event)
654 {
655 return m_childView && m_childView->ProcessEventLocally(event);
656 }
657
658 // called from EVT_CLOSE handler in the frame: check if we can close and do
659 // cleanup if so; veto the event otherwise
660 bool CloseView(wxCloseEvent& event);
661
662
663 wxDocument* m_childDocument;
664 wxView* m_childView;
665
666 // the associated window: having it here is not terribly elegant but it
667 // allows us to avoid having any virtual functions in this class
668 wxWindow* m_win;
669
670
671 wxDECLARE_NO_COPY_CLASS(wxDocChildFrameAnyBase);
672 };
673
674 // ----------------------------------------------------------------------------
675 // Template implementing child frame concept using the given wxFrame-like class
676 //
677 // This is used to define wxDocChildFrame and wxDocMDIChildFrame: ChildFrame is
678 // a wxFrame or wxMDIChildFrame (although in theory it could be any wxWindow-
679 // derived class as long as it provided a ctor with the same signature as
680 // wxFrame and OnActivate() method) and ParentFrame is either wxFrame or
681 // wxMDIParentFrame.
682 // ----------------------------------------------------------------------------
683
684 template <class ChildFrame, class ParentFrame>
685 class WXDLLIMPEXP_CORE wxDocChildFrameAny : public ChildFrame,
686 public wxDocChildFrameAnyBase
687 {
688 public:
689 typedef ChildFrame BaseClass;
690
691 // default ctor, use Create after it
692 wxDocChildFrameAny() { }
693
694 // ctor for a frame showing the given view of the specified document
695 wxDocChildFrameAny(wxDocument *doc,
696 wxView *view,
697 ParentFrame *parent,
698 wxWindowID id,
699 const wxString& title,
700 const wxPoint& pos = wxDefaultPosition,
701 const wxSize& size = wxDefaultSize,
702 long style = wxDEFAULT_FRAME_STYLE,
703 const wxString& name = wxFrameNameStr)
704 {
705 Create(doc, view, parent, id, title, pos, size, style, name);
706 }
707
708 bool Create(wxDocument *doc,
709 wxView *view,
710 ParentFrame *parent,
711 wxWindowID id,
712 const wxString& title,
713 const wxPoint& pos = wxDefaultPosition,
714 const wxSize& size = wxDefaultSize,
715 long style = wxDEFAULT_FRAME_STYLE,
716 const wxString& name = wxFrameNameStr)
717 {
718 if ( !wxDocChildFrameAnyBase::Create(doc, view, this) )
719 return false;
720
721 if ( !BaseClass::Create(parent, id, title, pos, size, style, name) )
722 return false;
723
724 this->Connect(wxEVT_ACTIVATE,
725 wxActivateEventHandler(wxDocChildFrameAny::OnActivate));
726 this->Connect(wxEVT_CLOSE_WINDOW,
727 wxCloseEventHandler(wxDocChildFrameAny::OnCloseWindow));
728
729 return true;
730 }
731
732 virtual bool Destroy()
733 {
734 // FIXME: why exactly do we do this? to avoid activation events during
735 // destructions maybe?
736 m_childView = NULL;
737 return BaseClass::Destroy();
738 }
739
740 protected:
741 // hook the child view into event handlers chain here
742 virtual bool TryBefore(wxEvent& event)
743 {
744 return TryProcessEvent(event) || BaseClass::TryBefore(event);
745 }
746
747 private:
748 void OnActivate(wxActivateEvent& event)
749 {
750 BaseClass::OnActivate(event);
751
752 if ( m_childView )
753 m_childView->Activate(event.GetActive());
754 }
755
756 void OnCloseWindow(wxCloseEvent& event)
757 {
758 if ( CloseView(event) )
759 Destroy();
760 //else: vetoed
761 }
762
763 wxDECLARE_NO_COPY_TEMPLATE_CLASS_2(wxDocChildFrameAny,
764 ChildFrame, ParentFrame);
765 };
766
767 // ----------------------------------------------------------------------------
768 // A default child frame: we need to define it as a class just for wxRTTI,
769 // otherwise we could simply typedef it
770 // ----------------------------------------------------------------------------
771
772 #ifdef __VISUALC6__
773 // "non dll-interface class 'wxDocChildFrameAny<>' used as base interface
774 // for dll-interface class 'wxDocChildFrame'" -- this is bogus as the
775 // template will be DLL-exported but only once it is used as base class
776 // here!
777 #pragma warning (push)
778 #pragma warning (disable:4275)
779 #endif
780
781 typedef wxDocChildFrameAny<wxFrame, wxFrame> wxDocChildFrameBase;
782
783 class WXDLLIMPEXP_CORE wxDocChildFrame : public wxDocChildFrameBase
784 {
785 public:
786 wxDocChildFrame()
787 {
788 }
789
790 wxDocChildFrame(wxDocument *doc,
791 wxView *view,
792 wxFrame *parent,
793 wxWindowID id,
794 const wxString& title,
795 const wxPoint& pos = wxDefaultPosition,
796 const wxSize& size = wxDefaultSize,
797 long style = wxDEFAULT_FRAME_STYLE,
798 const wxString& name = wxFrameNameStr)
799 : wxDocChildFrameBase(doc, view,
800 parent, id, title, pos, size, style, name)
801 {
802 }
803
804 bool Create(wxDocument *doc,
805 wxView *view,
806 wxFrame *parent,
807 wxWindowID id,
808 const wxString& title,
809 const wxPoint& pos = wxDefaultPosition,
810 const wxSize& size = wxDefaultSize,
811 long style = wxDEFAULT_FRAME_STYLE,
812 const wxString& name = wxFrameNameStr)
813 {
814 return wxDocChildFrameBase::Create
815 (
816 doc, view,
817 parent, id, title, pos, size, style, name
818 );
819 }
820
821 private:
822 DECLARE_CLASS(wxDocChildFrame)
823 wxDECLARE_NO_COPY_CLASS(wxDocChildFrame);
824 };
825
826 // ----------------------------------------------------------------------------
827 // wxDocParentFrame and related classes.
828 //
829 // As with wxDocChildFrame we define a template base class used by both normal
830 // and MDI versions
831 // ----------------------------------------------------------------------------
832
833 // Base class containing type-independent code of wxDocParentFrameAny
834 //
835 // Similarly to wxDocChildFrameAnyBase, this class is a mix-in and doesn't
836 // derive from wxWindow.
837 class WXDLLIMPEXP_CORE wxDocParentFrameAnyBase
838 {
839 public:
840 wxDocParentFrameAnyBase() { m_docManager = NULL; }
841
842 wxDocManager *GetDocumentManager() const { return m_docManager; }
843
844 protected:
845 wxDocManager *m_docManager;
846
847 wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAnyBase);
848 };
849
850 // This is similar to wxDocChildFrameAny and is used to provide common
851 // implementation for both wxDocParentFrame and wxDocMDIParentFrame
852 template <class BaseFrame>
853 class WXDLLIMPEXP_CORE wxDocParentFrameAny : public BaseFrame,
854 public wxDocParentFrameAnyBase
855 {
856 public:
857 wxDocParentFrameAny() { }
858 wxDocParentFrameAny(wxDocManager *manager,
859 wxFrame *frame,
860 wxWindowID id,
861 const wxString& title,
862 const wxPoint& pos = wxDefaultPosition,
863 const wxSize& size = wxDefaultSize,
864 long style = wxDEFAULT_FRAME_STYLE,
865 const wxString& name = wxFrameNameStr)
866 {
867 Create(manager, frame, id, title, pos, size, style, name);
868 }
869
870 bool Create(wxDocManager *manager,
871 wxFrame *frame,
872 wxWindowID id,
873 const wxString& title,
874 const wxPoint& pos = wxDefaultPosition,
875 const wxSize& size = wxDefaultSize,
876 long style = wxDEFAULT_FRAME_STYLE,
877 const wxString& name = wxFrameNameStr)
878 {
879 m_docManager = manager;
880
881 if ( !BaseFrame::Create(frame, id, title, pos, size, style, name) )
882 return false;
883
884 this->Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED,
885 wxCommandEventHandler(wxDocParentFrameAny::OnExit));
886 this->Connect(wxEVT_CLOSE_WINDOW,
887 wxCloseEventHandler(wxDocParentFrameAny::OnCloseWindow));
888
889 return true;
890 }
891
892 protected:
893 // hook the document manager into event handling chain here
894 virtual bool TryBefore(wxEvent& event)
895 {
896 if ( m_docManager && m_docManager->ProcessEventLocally(event) )
897 return true;
898
899 return BaseFrame::TryBefore(event);
900 }
901
902 private:
903 void OnExit(wxCommandEvent& WXUNUSED(event))
904 {
905 this->Close();
906 }
907
908 void OnCloseWindow(wxCloseEvent& event)
909 {
910 if ( m_docManager && !m_docManager->Clear(!event.CanVeto()) )
911 {
912 // The user decided not to close finally, abort.
913 event.Veto();
914 }
915 else
916 {
917 // Just skip the event, base class handler will destroy the window.
918 event.Skip();
919 }
920 }
921
922
923 wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAny);
924 };
925
926 typedef wxDocParentFrameAny<wxFrame> wxDocParentFrameBase;
927
928 class WXDLLIMPEXP_CORE wxDocParentFrame : public wxDocParentFrameBase
929 {
930 public:
931 wxDocParentFrame() : wxDocParentFrameBase() { }
932
933 wxDocParentFrame(wxDocManager *manager,
934 wxFrame *parent,
935 wxWindowID id,
936 const wxString& title,
937 const wxPoint& pos = wxDefaultPosition,
938 const wxSize& size = wxDefaultSize,
939 long style = wxDEFAULT_FRAME_STYLE,
940 const wxString& name = wxFrameNameStr)
941 : wxDocParentFrameBase(manager,
942 parent, id, title, pos, size, style, name)
943 {
944 }
945
946 bool Create(wxDocManager *manager,
947 wxFrame *parent,
948 wxWindowID id,
949 const wxString& title,
950 const wxPoint& pos = wxDefaultPosition,
951 const wxSize& size = wxDefaultSize,
952 long style = wxDEFAULT_FRAME_STYLE,
953 const wxString& name = wxFrameNameStr)
954 {
955 return wxDocParentFrameBase::Create(manager,
956 parent, id, title,
957 pos, size, style, name);
958 }
959
960 private:
961 DECLARE_CLASS(wxDocParentFrame)
962 wxDECLARE_NO_COPY_CLASS(wxDocParentFrame);
963 };
964
965 #ifdef __VISUALC6__
966 // reenable warning 4275
967 #pragma warning (pop)
968 #endif
969
970 // ----------------------------------------------------------------------------
971 // Provide simple default printing facilities
972 // ----------------------------------------------------------------------------
973
974 #if wxUSE_PRINTING_ARCHITECTURE
975 class WXDLLIMPEXP_CORE wxDocPrintout : public wxPrintout
976 {
977 public:
978 wxDocPrintout(wxView *view = NULL, const wxString& title = wxString());
979
980 // implement wxPrintout methods
981 virtual bool OnPrintPage(int page);
982 virtual bool HasPage(int page);
983 virtual bool OnBeginDocument(int startPage, int endPage);
984 virtual void GetPageInfo(int *minPage, int *maxPage,
985 int *selPageFrom, int *selPageTo);
986
987 virtual wxView *GetView() { return m_printoutView; }
988
989 protected:
990 wxView* m_printoutView;
991
992 private:
993 DECLARE_DYNAMIC_CLASS(wxDocPrintout)
994 wxDECLARE_NO_COPY_CLASS(wxDocPrintout);
995 };
996 #endif // wxUSE_PRINTING_ARCHITECTURE
997
998 // For compatibility with existing file formats:
999 // converts from/to a stream to/from a temporary file.
1000 #if wxUSE_STD_IOSTREAM
1001 bool WXDLLIMPEXP_CORE
1002 wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream);
1003 bool WXDLLIMPEXP_CORE
1004 wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename);
1005 #else
1006 bool WXDLLIMPEXP_CORE
1007 wxTransferFileToStream(const wxString& filename, wxOutputStream& stream);
1008 bool WXDLLIMPEXP_CORE
1009 wxTransferStreamToFile(wxInputStream& stream, const wxString& filename);
1010 #endif // wxUSE_STD_IOSTREAM
1011
1012
1013 // these flags are not used anywhere by wxWidgets and kept only for an unlikely
1014 // case of existing user code using them for its own purposes
1015 #if WXWIN_COMPATIBILITY_2_8
1016 enum
1017 {
1018 wxDOC_SDI = 1,
1019 wxDOC_MDI,
1020 wxDEFAULT_DOCMAN_FLAGS = wxDOC_SDI
1021 };
1022 #endif // WXWIN_COMPATIBILITY_2_8
1023
1024 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
1025
1026 #endif // _WX_DOCH__