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