]> git.saurik.com Git - wxWidgets.git/blob - include/wx/docview.h
Remove RPM .spec files from the repository.
[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 protected:
283 // hook the document into event handlers chain here
284 virtual bool TryBefore(wxEvent& event);
285
286 wxDocument* m_viewDocument;
287 wxString m_viewTypeName;
288 wxWindow* m_viewFrame;
289
290 wxDocChildFrameAnyBase *m_docChildFrame;
291
292 private:
293 DECLARE_ABSTRACT_CLASS(wxView)
294 wxDECLARE_NO_COPY_CLASS(wxView);
295 };
296
297 // Represents user interface (and other) properties of documents and views
298 class WXDLLIMPEXP_CORE wxDocTemplate: public wxObject
299 {
300
301 friend class WXDLLIMPEXP_FWD_CORE wxDocManager;
302
303 public:
304 // Associate document and view types. They're for identifying what view is
305 // associated with what template/document type
306 wxDocTemplate(wxDocManager *manager,
307 const wxString& descr,
308 const wxString& filter,
309 const wxString& dir,
310 const wxString& ext,
311 const wxString& docTypeName,
312 const wxString& viewTypeName,
313 wxClassInfo *docClassInfo = NULL,
314 wxClassInfo *viewClassInfo = NULL,
315 long flags = wxDEFAULT_TEMPLATE_FLAGS);
316
317 virtual ~wxDocTemplate();
318
319 // By default, these two member functions dynamically creates document and
320 // view using dynamic instance construction. Override these if you need a
321 // different method of construction.
322 virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
323 virtual wxView *CreateView(wxDocument *doc, long flags = 0);
324
325 // Helper method for CreateDocument; also allows you to do your own document
326 // creation
327 virtual bool InitDocument(wxDocument* doc,
328 const wxString& path,
329 long flags = 0);
330
331 wxString GetDefaultExtension() const { return m_defaultExt; }
332 wxString GetDescription() const { return m_description; }
333 wxString GetDirectory() const { return m_directory; }
334 wxDocManager *GetDocumentManager() const { return m_documentManager; }
335 void SetDocumentManager(wxDocManager *manager)
336 { m_documentManager = manager; }
337 wxString GetFileFilter() const { return m_fileFilter; }
338 long GetFlags() const { return m_flags; }
339 virtual wxString GetViewName() const { return m_viewTypeName; }
340 virtual wxString GetDocumentName() const { return m_docTypeName; }
341
342 void SetFileFilter(const wxString& filter) { m_fileFilter = filter; }
343 void SetDirectory(const wxString& dir) { m_directory = dir; }
344 void SetDescription(const wxString& descr) { m_description = descr; }
345 void SetDefaultExtension(const wxString& ext) { m_defaultExt = ext; }
346 void SetFlags(long flags) { m_flags = flags; }
347
348 bool IsVisible() const { return (m_flags & wxTEMPLATE_VISIBLE) != 0; }
349
350 wxClassInfo* GetDocClassInfo() const { return m_docClassInfo; }
351 wxClassInfo* GetViewClassInfo() const { return m_viewClassInfo; }
352
353 virtual bool FileMatchesTemplate(const wxString& path);
354
355 protected:
356 long m_flags;
357 wxString m_fileFilter;
358 wxString m_directory;
359 wxString m_description;
360 wxString m_defaultExt;
361 wxString m_docTypeName;
362 wxString m_viewTypeName;
363 wxDocManager* m_documentManager;
364
365 // For dynamic creation of appropriate instances.
366 wxClassInfo* m_docClassInfo;
367 wxClassInfo* m_viewClassInfo;
368
369 // Called by CreateDocument and CreateView to create the actual
370 // document/view object.
371 //
372 // By default uses the ClassInfo provided to the constructor. Override
373 // these functions to provide a different method of creation.
374 virtual wxDocument *DoCreateDocument();
375 virtual wxView *DoCreateView();
376
377 private:
378 DECLARE_CLASS(wxDocTemplate)
379 wxDECLARE_NO_COPY_CLASS(wxDocTemplate);
380 };
381
382 // One object of this class may be created in an application, to manage all
383 // the templates and documents.
384 class WXDLLIMPEXP_CORE wxDocManager: public wxEvtHandler
385 {
386 public:
387 // NB: flags are unused, don't pass wxDOC_XXX to this ctor
388 wxDocManager(long flags = 0, bool initialize = true);
389 virtual ~wxDocManager();
390
391 virtual bool Initialize();
392
393 // Handlers for common user commands
394 void OnFileClose(wxCommandEvent& event);
395 void OnFileCloseAll(wxCommandEvent& event);
396 void OnFileNew(wxCommandEvent& event);
397 void OnFileOpen(wxCommandEvent& event);
398 void OnFileRevert(wxCommandEvent& event);
399 void OnFileSave(wxCommandEvent& event);
400 void OnFileSaveAs(wxCommandEvent& event);
401 void OnMRUFile(wxCommandEvent& event);
402 #if wxUSE_PRINTING_ARCHITECTURE
403 void OnPrint(wxCommandEvent& event);
404 void OnPreview(wxCommandEvent& event);
405 void OnPageSetup(wxCommandEvent& event);
406 #endif // wxUSE_PRINTING_ARCHITECTURE
407 void OnUndo(wxCommandEvent& event);
408 void OnRedo(wxCommandEvent& event);
409
410 // Handlers for UI update commands
411 void OnUpdateFileOpen(wxUpdateUIEvent& event);
412 void OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event);
413 void OnUpdateFileRevert(wxUpdateUIEvent& event);
414 void OnUpdateFileNew(wxUpdateUIEvent& event);
415 void OnUpdateFileSave(wxUpdateUIEvent& event);
416 void OnUpdateFileSaveAs(wxUpdateUIEvent& event);
417 void OnUpdateUndo(wxUpdateUIEvent& event);
418 void OnUpdateRedo(wxUpdateUIEvent& event);
419
420 // called when file format detection didn't work, can be overridden to do
421 // something in this case
422 virtual void OnOpenFileFailure() { }
423
424 virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
425
426 // wrapper around CreateDocument() with a more clear name
427 wxDocument *CreateNewDocument()
428 { return CreateDocument(wxString(), wxDOC_NEW); }
429
430 virtual wxView *CreateView(wxDocument *doc, long flags = 0);
431 virtual void DeleteTemplate(wxDocTemplate *temp, long flags = 0);
432 virtual bool FlushDoc(wxDocument *doc);
433 virtual wxDocTemplate *MatchTemplate(const wxString& path);
434 virtual wxDocTemplate *SelectDocumentPath(wxDocTemplate **templates,
435 int noTemplates, wxString& path, long flags, bool save = false);
436 virtual wxDocTemplate *SelectDocumentType(wxDocTemplate **templates,
437 int noTemplates, bool sort = false);
438 virtual wxDocTemplate *SelectViewType(wxDocTemplate **templates,
439 int noTemplates, bool sort = false);
440 virtual wxDocTemplate *FindTemplateForPath(const wxString& path);
441
442 void AssociateTemplate(wxDocTemplate *temp);
443 void DisassociateTemplate(wxDocTemplate *temp);
444
445 // Find template from document class info, may return NULL.
446 wxDocTemplate* FindTemplate(const wxClassInfo* documentClassInfo);
447
448 // Find document from file name, may return NULL.
449 wxDocument* FindDocumentByPath(const wxString& path) const;
450
451 wxDocument *GetCurrentDocument() const;
452
453 void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; }
454 int GetMaxDocsOpen() const { return m_maxDocsOpen; }
455
456 // Add and remove a document from the manager's list
457 void AddDocument(wxDocument *doc);
458 void RemoveDocument(wxDocument *doc);
459
460 // closes all currently open documents
461 bool CloseDocuments(bool force = true);
462
463 // closes the specified document
464 bool CloseDocument(wxDocument* doc, bool force = false);
465
466 // Clear remaining documents and templates
467 bool Clear(bool force = true);
468
469 // Views or windows should inform the document manager
470 // when a view is going in or out of focus
471 virtual void ActivateView(wxView *view, bool activate = true);
472 virtual wxView *GetCurrentView() const { return m_currentView; }
473
474 // This method tries to find an active view harder than GetCurrentView():
475 // if the latter is NULL, it also checks if we don't have just a single
476 // view and returns it then.
477 wxView *GetAnyUsableView() const;
478
479
480 #ifndef __VISUALC6__
481 wxDocVector GetDocumentsVector() const;
482 wxDocTemplateVector GetTemplatesVector() const;
483 #endif // !__VISUALC6__
484
485 wxList& GetDocuments() { return m_docs; }
486 wxList& GetTemplates() { return m_templates; }
487
488 // Return the default name for a new document (by default returns strings
489 // in the form "unnamed <counter>" but can be overridden)
490 virtual wxString MakeNewDocumentName();
491
492 // Make a frame title (override this to do something different)
493 virtual wxString MakeFrameTitle(wxDocument* doc);
494
495 virtual wxFileHistory *OnCreateFileHistory();
496 virtual wxFileHistory *GetFileHistory() const { return m_fileHistory; }
497
498 // File history management
499 virtual void AddFileToHistory(const wxString& file);
500 virtual void RemoveFileFromHistory(size_t i);
501 virtual size_t GetHistoryFilesCount() const;
502 virtual wxString GetHistoryFile(size_t i) const;
503 virtual void FileHistoryUseMenu(wxMenu *menu);
504 virtual void FileHistoryRemoveMenu(wxMenu *menu);
505 #if wxUSE_CONFIG
506 virtual void FileHistoryLoad(const wxConfigBase& config);
507 virtual void FileHistorySave(wxConfigBase& config);
508 #endif // wxUSE_CONFIG
509
510 virtual void FileHistoryAddFilesToMenu();
511 virtual void FileHistoryAddFilesToMenu(wxMenu* menu);
512
513 wxString GetLastDirectory() const;
514 void SetLastDirectory(const wxString& dir) { m_lastDirectory = dir; }
515
516 // Get the current document manager
517 static wxDocManager* GetDocumentManager() { return sm_docManager; }
518
519 #if wxUSE_PRINTING_ARCHITECTURE
520 wxPageSetupDialogData& GetPageSetupDialogData()
521 { return m_pageSetupDialogData; }
522 const wxPageSetupDialogData& GetPageSetupDialogData() const
523 { return m_pageSetupDialogData; }
524 #endif // wxUSE_PRINTING_ARCHITECTURE
525
526 #if WXWIN_COMPATIBILITY_2_8
527 // deprecated, override GetDefaultName() instead
528 wxDEPRECATED_BUT_USED_INTERNALLY(
529 virtual bool MakeDefaultName(wxString& buf)
530 );
531 #endif
532
533 #if WXWIN_COMPATIBILITY_2_6
534 // deprecated, use GetHistoryFilesCount() instead
535 wxDEPRECATED( size_t GetNoHistoryFiles() const );
536 #endif // WXWIN_COMPATIBILITY_2_6
537
538
539 protected:
540 // Called when a file selected from the MRU list doesn't exist any more.
541 // The default behaviour is to remove the file from the MRU and notify the
542 // user about it but this method can be overridden to customize it.
543 virtual void OnMRUFileNotExist(unsigned n, const wxString& filename);
544
545 // Open the MRU file with the given index in our associated file history.
546 void DoOpenMRUFile(unsigned n);
547 #if wxUSE_PRINTING_ARCHITECTURE
548 virtual wxPreviewFrame* CreatePreviewFrame(wxPrintPreviewBase* preview,
549 wxWindow *parent,
550 const wxString& title);
551 #endif // wxUSE_PRINTING_ARCHITECTURE
552
553 // hook the currently active view into event handlers chain here
554 virtual bool TryBefore(wxEvent& event);
555
556 // return the command processor for the current document, if any
557 wxCommandProcessor *GetCurrentCommandProcessor() const;
558
559 int m_defaultDocumentNameCounter;
560 int m_maxDocsOpen;
561 wxList m_docs;
562 wxList m_templates;
563 wxView* m_currentView;
564 wxFileHistory* m_fileHistory;
565 wxString m_lastDirectory;
566 static wxDocManager* sm_docManager;
567
568 #if wxUSE_PRINTING_ARCHITECTURE
569 wxPageSetupDialogData m_pageSetupDialogData;
570 #endif // wxUSE_PRINTING_ARCHITECTURE
571
572 DECLARE_EVENT_TABLE()
573 DECLARE_DYNAMIC_CLASS(wxDocManager)
574 wxDECLARE_NO_COPY_CLASS(wxDocManager);
575 };
576
577 #if WXWIN_COMPATIBILITY_2_6
578 inline size_t wxDocManager::GetNoHistoryFiles() const
579 {
580 return GetHistoryFilesCount();
581 }
582 #endif // WXWIN_COMPATIBILITY_2_6
583
584 // ----------------------------------------------------------------------------
585 // Base class for child frames -- this is what wxView renders itself into
586 //
587 // Notice that this is a mix-in class so it doesn't derive from wxWindow, only
588 // wxDocChildFrameAny does
589 // ----------------------------------------------------------------------------
590
591 class WXDLLIMPEXP_CORE wxDocChildFrameAnyBase
592 {
593 public:
594 // default ctor, use Create() after it
595 wxDocChildFrameAnyBase()
596 {
597 m_childDocument = NULL;
598 m_childView = NULL;
599 m_win = NULL;
600 }
601
602 // full ctor equivalent to using the default one and Create(0
603 wxDocChildFrameAnyBase(wxDocument *doc, wxView *view, wxWindow *win)
604 {
605 Create(doc, view, win);
606 }
607
608 // method which must be called for an object created using the default ctor
609 //
610 // note that it returns bool just for consistency with Create() methods in
611 // other classes, we never return false from here
612 bool Create(wxDocument *doc, wxView *view, wxWindow *win)
613 {
614 m_childDocument = doc;
615 m_childView = view;
616 m_win = win;
617
618 if ( view )
619 view->SetDocChildFrame(this);
620
621 return true;
622 }
623
624 // dtor doesn't need to be virtual, an object should never be destroyed via
625 // a pointer to this class
626 ~wxDocChildFrameAnyBase()
627 {
628 // prevent the view from deleting us if we're being deleted directly
629 // (and not via Close() + Destroy())
630 if ( m_childView )
631 m_childView->SetDocChildFrame(NULL);
632 }
633
634 wxDocument *GetDocument() const { return m_childDocument; }
635 wxView *GetView() const { return m_childView; }
636 void SetDocument(wxDocument *doc) { m_childDocument = doc; }
637 void SetView(wxView *view) { m_childView = view; }
638
639 wxWindow *GetWindow() const { return m_win; }
640
641 protected:
642 // we're not a wxEvtHandler but we provide this wxEvtHandler-like function
643 // which is called from TryBefore() of the derived classes to give our view
644 // a chance to process the message before the frame event handlers are used
645 bool TryProcessEvent(wxEvent& event);
646
647 // called from EVT_CLOSE handler in the frame: check if we can close and do
648 // cleanup if so; veto the event otherwise
649 bool CloseView(wxCloseEvent& event);
650
651
652 wxDocument* m_childDocument;
653 wxView* m_childView;
654
655 // the associated window: having it here is not terribly elegant but it
656 // allows us to avoid having any virtual functions in this class
657 wxWindow* m_win;
658
659
660 wxDECLARE_NO_COPY_CLASS(wxDocChildFrameAnyBase);
661 };
662
663 // ----------------------------------------------------------------------------
664 // Template implementing child frame concept using the given wxFrame-like class
665 //
666 // This is used to define wxDocChildFrame and wxDocMDIChildFrame: ChildFrame is
667 // a wxFrame or wxMDIChildFrame (although in theory it could be any wxWindow-
668 // derived class as long as it provided a ctor with the same signature as
669 // wxFrame and OnActivate() method) and ParentFrame is either wxFrame or
670 // wxMDIParentFrame.
671 // ----------------------------------------------------------------------------
672
673 template <class ChildFrame, class ParentFrame>
674 class WXDLLIMPEXP_CORE wxDocChildFrameAny : public ChildFrame,
675 public wxDocChildFrameAnyBase
676 {
677 public:
678 typedef ChildFrame BaseClass;
679
680 // default ctor, use Create after it
681 wxDocChildFrameAny() { }
682
683 // ctor for a frame showing the given view of the specified document
684 wxDocChildFrameAny(wxDocument *doc,
685 wxView *view,
686 ParentFrame *parent,
687 wxWindowID id,
688 const wxString& title,
689 const wxPoint& pos = wxDefaultPosition,
690 const wxSize& size = wxDefaultSize,
691 long style = wxDEFAULT_FRAME_STYLE,
692 const wxString& name = wxFrameNameStr)
693 {
694 Create(doc, view, parent, id, title, pos, size, style, name);
695 }
696
697 bool Create(wxDocument *doc,
698 wxView *view,
699 ParentFrame *parent,
700 wxWindowID id,
701 const wxString& title,
702 const wxPoint& pos = wxDefaultPosition,
703 const wxSize& size = wxDefaultSize,
704 long style = wxDEFAULT_FRAME_STYLE,
705 const wxString& name = wxFrameNameStr)
706 {
707 if ( !wxDocChildFrameAnyBase::Create(doc, view, this) )
708 return false;
709
710 if ( !BaseClass::Create(parent, id, title, pos, size, style, name) )
711 return false;
712
713 this->Connect(wxEVT_ACTIVATE,
714 wxActivateEventHandler(wxDocChildFrameAny::OnActivate));
715 this->Connect(wxEVT_CLOSE_WINDOW,
716 wxCloseEventHandler(wxDocChildFrameAny::OnCloseWindow));
717
718 return true;
719 }
720
721 virtual bool Destroy()
722 {
723 // FIXME: why exactly do we do this? to avoid activation events during
724 // destructions maybe?
725 m_childView = NULL;
726 return BaseClass::Destroy();
727 }
728
729 protected:
730 // hook the child view into event handlers chain here
731 virtual bool TryBefore(wxEvent& event)
732 {
733 return TryProcessEvent(event) || BaseClass::TryBefore(event);
734 }
735
736 private:
737 void OnActivate(wxActivateEvent& event)
738 {
739 BaseClass::OnActivate(event);
740
741 if ( m_childView )
742 m_childView->Activate(event.GetActive());
743 }
744
745 void OnCloseWindow(wxCloseEvent& event)
746 {
747 if ( CloseView(event) )
748 Destroy();
749 //else: vetoed
750 }
751
752 wxDECLARE_NO_COPY_TEMPLATE_CLASS_2(wxDocChildFrameAny,
753 ChildFrame, ParentFrame);
754 };
755
756 // ----------------------------------------------------------------------------
757 // A default child frame: we need to define it as a class just for wxRTTI,
758 // otherwise we could simply typedef it
759 // ----------------------------------------------------------------------------
760
761 #ifdef __VISUALC6__
762 // "non dll-interface class 'wxDocChildFrameAny<>' used as base interface
763 // for dll-interface class 'wxDocChildFrame'" -- this is bogus as the
764 // template will be DLL-exported but only once it is used as base class
765 // here!
766 #pragma warning (push)
767 #pragma warning (disable:4275)
768 #endif
769
770 typedef wxDocChildFrameAny<wxFrame, wxFrame> wxDocChildFrameBase;
771
772 class WXDLLIMPEXP_CORE wxDocChildFrame : public wxDocChildFrameBase
773 {
774 public:
775 wxDocChildFrame()
776 {
777 }
778
779 wxDocChildFrame(wxDocument *doc,
780 wxView *view,
781 wxFrame *parent,
782 wxWindowID id,
783 const wxString& title,
784 const wxPoint& pos = wxDefaultPosition,
785 const wxSize& size = wxDefaultSize,
786 long style = wxDEFAULT_FRAME_STYLE,
787 const wxString& name = wxFrameNameStr)
788 : wxDocChildFrameBase(doc, view,
789 parent, id, title, pos, size, style, name)
790 {
791 }
792
793 bool Create(wxDocument *doc,
794 wxView *view,
795 wxFrame *parent,
796 wxWindowID id,
797 const wxString& title,
798 const wxPoint& pos = wxDefaultPosition,
799 const wxSize& size = wxDefaultSize,
800 long style = wxDEFAULT_FRAME_STYLE,
801 const wxString& name = wxFrameNameStr)
802 {
803 return wxDocChildFrameBase::Create
804 (
805 doc, view,
806 parent, id, title, pos, size, style, name
807 );
808 }
809
810 private:
811 DECLARE_CLASS(wxDocChildFrame)
812 wxDECLARE_NO_COPY_CLASS(wxDocChildFrame);
813 };
814
815 // ----------------------------------------------------------------------------
816 // wxDocParentFrame and related classes.
817 //
818 // As with wxDocChildFrame we define a template base class used by both normal
819 // and MDI versions
820 // ----------------------------------------------------------------------------
821
822 // Base class containing type-independent code of wxDocParentFrameAny
823 //
824 // Similarly to wxDocChildFrameAnyBase, this class is a mix-in and doesn't
825 // derive from wxWindow.
826 class WXDLLIMPEXP_CORE wxDocParentFrameAnyBase
827 {
828 public:
829 wxDocParentFrameAnyBase(wxWindow* frame)
830 : m_frame(frame)
831 {
832 m_docManager = NULL;
833 }
834
835 wxDocManager *GetDocumentManager() const { return m_docManager; }
836
837 protected:
838 // This is similar to wxDocChildFrameAnyBase method with the same name:
839 // while we're not an event handler ourselves and so can't override
840 // TryBefore(), we provide a helper that the derived template class can use
841 // from its TryBefore() implementation.
842 bool TryProcessEvent(wxEvent& event);
843
844 wxWindow* const m_frame;
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() : wxDocParentFrameAnyBase(this) { }
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 : wxDocParentFrameAnyBase(this)
867 {
868 Create(manager, frame, id, title, pos, size, style, name);
869 }
870
871 bool Create(wxDocManager *manager,
872 wxFrame *frame,
873 wxWindowID id,
874 const wxString& title,
875 const wxPoint& pos = wxDefaultPosition,
876 const wxSize& size = wxDefaultSize,
877 long style = wxDEFAULT_FRAME_STYLE,
878 const wxString& name = wxFrameNameStr)
879 {
880 m_docManager = manager;
881
882 if ( !BaseFrame::Create(frame, id, title, pos, size, style, name) )
883 return false;
884
885 this->Connect(wxID_EXIT, wxEVT_MENU,
886 wxCommandEventHandler(wxDocParentFrameAny::OnExit));
887 this->Connect(wxEVT_CLOSE_WINDOW,
888 wxCloseEventHandler(wxDocParentFrameAny::OnCloseWindow));
889
890 return true;
891 }
892
893 protected:
894 // hook the document manager into event handling chain here
895 virtual bool TryBefore(wxEvent& event)
896 {
897 return TryProcessEvent(event) || BaseFrame::TryBefore(event);
898 }
899
900 private:
901 void OnExit(wxCommandEvent& WXUNUSED(event))
902 {
903 this->Close();
904 }
905
906 void OnCloseWindow(wxCloseEvent& event)
907 {
908 if ( m_docManager && !m_docManager->Clear(!event.CanVeto()) )
909 {
910 // The user decided not to close finally, abort.
911 event.Veto();
912 }
913 else
914 {
915 // Just skip the event, base class handler will destroy the window.
916 event.Skip();
917 }
918 }
919
920
921 wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAny);
922 };
923
924 typedef wxDocParentFrameAny<wxFrame> wxDocParentFrameBase;
925
926 class WXDLLIMPEXP_CORE wxDocParentFrame : public wxDocParentFrameBase
927 {
928 public:
929 wxDocParentFrame() : wxDocParentFrameBase() { }
930
931 wxDocParentFrame(wxDocManager *manager,
932 wxFrame *parent,
933 wxWindowID id,
934 const wxString& title,
935 const wxPoint& pos = wxDefaultPosition,
936 const wxSize& size = wxDefaultSize,
937 long style = wxDEFAULT_FRAME_STYLE,
938 const wxString& name = wxFrameNameStr)
939 : wxDocParentFrameBase(manager,
940 parent, id, title, pos, size, style, name)
941 {
942 }
943
944 bool Create(wxDocManager *manager,
945 wxFrame *parent,
946 wxWindowID id,
947 const wxString& title,
948 const wxPoint& pos = wxDefaultPosition,
949 const wxSize& size = wxDefaultSize,
950 long style = wxDEFAULT_FRAME_STYLE,
951 const wxString& name = wxFrameNameStr)
952 {
953 return wxDocParentFrameBase::Create(manager,
954 parent, id, title,
955 pos, size, style, name);
956 }
957
958 private:
959 DECLARE_CLASS(wxDocParentFrame)
960 wxDECLARE_NO_COPY_CLASS(wxDocParentFrame);
961 };
962
963 #ifdef __VISUALC6__
964 // reenable warning 4275
965 #pragma warning (pop)
966 #endif
967
968 // ----------------------------------------------------------------------------
969 // Provide simple default printing facilities
970 // ----------------------------------------------------------------------------
971
972 #if wxUSE_PRINTING_ARCHITECTURE
973 class WXDLLIMPEXP_CORE wxDocPrintout : public wxPrintout
974 {
975 public:
976 wxDocPrintout(wxView *view = NULL, const wxString& title = wxString());
977
978 // implement wxPrintout methods
979 virtual bool OnPrintPage(int page);
980 virtual bool HasPage(int page);
981 virtual bool OnBeginDocument(int startPage, int endPage);
982 virtual void GetPageInfo(int *minPage, int *maxPage,
983 int *selPageFrom, int *selPageTo);
984
985 virtual wxView *GetView() { return m_printoutView; }
986
987 protected:
988 wxView* m_printoutView;
989
990 private:
991 DECLARE_DYNAMIC_CLASS(wxDocPrintout)
992 wxDECLARE_NO_COPY_CLASS(wxDocPrintout);
993 };
994 #endif // wxUSE_PRINTING_ARCHITECTURE
995
996 // For compatibility with existing file formats:
997 // converts from/to a stream to/from a temporary file.
998 #if wxUSE_STD_IOSTREAM
999 bool WXDLLIMPEXP_CORE
1000 wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream);
1001 bool WXDLLIMPEXP_CORE
1002 wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename);
1003 #else
1004 bool WXDLLIMPEXP_CORE
1005 wxTransferFileToStream(const wxString& filename, wxOutputStream& stream);
1006 bool WXDLLIMPEXP_CORE
1007 wxTransferStreamToFile(wxInputStream& stream, const wxString& filename);
1008 #endif // wxUSE_STD_IOSTREAM
1009
1010
1011 // these flags are not used anywhere by wxWidgets and kept only for an unlikely
1012 // case of existing user code using them for its own purposes
1013 #if WXWIN_COMPATIBILITY_2_8
1014 enum
1015 {
1016 wxDOC_SDI = 1,
1017 wxDOC_MDI,
1018 wxDEFAULT_DOCMAN_FLAGS = wxDOC_SDI
1019 };
1020 #endif // WXWIN_COMPATIBILITY_2_8
1021
1022 #ifndef __VISUALC6__
1023 inline wxViewVector wxDocument::GetViewsVector() const
1024 {
1025 return m_documentViews.AsVector<wxView*>();
1026 }
1027
1028 inline wxDocVector wxDocManager::GetDocumentsVector() const
1029 {
1030 return m_docs.AsVector<wxDocument*>();
1031 }
1032
1033 inline wxDocTemplateVector wxDocManager::GetTemplatesVector() const
1034 {
1035 return m_templates.AsVector<wxDocTemplate*>();
1036 }
1037 #endif // !__VISUALC6__
1038
1039 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
1040
1041 #endif // _WX_DOCH__