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