1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Doc/View classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
17 #if wxUSE_DOC_VIEW_ARCHITECTURE
20 #include "wx/string.h"
23 #if wxUSE_PRINTING_ARCHITECTURE
27 class WXDLLEXPORT wxWindow
;
28 class WXDLLEXPORT wxDocument
;
29 class WXDLLEXPORT wxView
;
30 class WXDLLEXPORT wxDocTemplate
;
31 class WXDLLEXPORT wxDocManager
;
32 class WXDLLEXPORT wxPrintInfo
;
33 class WXDLLEXPORT wxCommandProcessor
;
34 class WXDLLEXPORT wxFileHistory
;
35 class WXDLLEXPORT wxConfigBase
;
37 #if wxUSE_STD_IOSTREAM
38 #include "wx/iosfwrap.h"
40 #include "wx/stream.h"
43 // Document manager flags
50 wxDEFAULT_DOCMAN_FLAGS
= wxDOC_SDI
53 // Document template flags
56 wxTEMPLATE_VISIBLE
= 1,
58 wxDEFAULT_TEMPLATE_FLAGS
= wxTEMPLATE_VISIBLE
61 #define wxMAX_FILE_HISTORY 9
63 class WXDLLEXPORT wxDocument
: public wxEvtHandler
66 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
67 virtual ~wxDocument();
70 void SetFilename(const wxString
& filename
, bool notifyViews
= false);
71 wxString
GetFilename() const { return m_documentFile
; }
73 void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
74 wxString
GetTitle() const { return m_documentTitle
; }
76 void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
77 wxString
GetDocumentName() const { return m_documentTypeName
; }
79 bool GetDocumentSaved() const { return m_savedYet
; }
80 void SetDocumentSaved(bool saved
= true) { m_savedYet
= saved
; }
84 virtual bool SaveAs();
85 virtual bool Revert();
87 #if wxUSE_STD_IOSTREAM
88 virtual wxSTD ostream
& SaveObject(wxSTD ostream
& stream
);
89 virtual wxSTD istream
& LoadObject(wxSTD istream
& stream
);
91 virtual wxOutputStream
& SaveObject(wxOutputStream
& stream
);
92 virtual wxInputStream
& LoadObject(wxInputStream
& stream
);
95 // Called by wxWidgets
96 virtual bool OnSaveDocument(const wxString
& filename
);
97 virtual bool OnOpenDocument(const wxString
& filename
);
98 virtual bool OnNewDocument();
99 virtual bool OnCloseDocument();
101 // Prompts for saving if about to close a modified document. Returns true
102 // if ok to close the document (may have saved in the meantime, or set
103 // modified to false)
104 virtual bool OnSaveModified();
106 // Called by framework if created automatically by the default document
107 // manager: gives document a chance to initialise and (usually) create a
109 virtual bool OnCreate(const wxString
& path
, long flags
);
111 // By default, creates a base wxCommandProcessor.
112 virtual wxCommandProcessor
*OnCreateCommandProcessor();
113 virtual wxCommandProcessor
*GetCommandProcessor() const { return m_commandProcessor
; }
114 virtual void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
116 // Called after a view is added or removed. The default implementation
117 // deletes the document if this is there are no more views.
118 virtual void OnChangedViewList();
120 virtual bool DeleteContents();
122 virtual bool Draw(wxDC
&);
123 virtual bool IsModified() const { return m_documentModified
; }
124 virtual void Modify(bool mod
) { m_documentModified
= mod
; }
126 virtual bool AddView(wxView
*view
);
127 virtual bool RemoveView(wxView
*view
);
128 wxList
& GetViews() { return m_documentViews
; }
129 const wxList
& GetViews() const { return m_documentViews
; }
130 wxView
*GetFirstView() const;
132 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
133 virtual void NotifyClosing();
135 // Remove all views (because we're closing the document)
136 virtual bool DeleteAllViews();
139 virtual wxDocManager
*GetDocumentManager() const;
140 virtual wxDocTemplate
*GetDocumentTemplate() const { return m_documentTemplate
; }
141 virtual void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
143 // Get title, or filename if no title, else [unnamed]
144 virtual bool GetPrintableName(wxString
& buf
) const;
146 // Returns a window that can be used as a parent for document-related
147 // dialogs. Override if necessary.
148 virtual wxWindow
*GetDocumentWindow() const;
151 wxList m_documentViews
;
152 wxString m_documentFile
;
153 wxString m_documentTitle
;
154 wxString m_documentTypeName
;
155 wxDocTemplate
* m_documentTemplate
;
156 bool m_documentModified
;
157 wxDocument
* m_documentParent
;
158 wxCommandProcessor
* m_commandProcessor
;
161 // Called by OnSaveDocument and OnOpenDocument to implement standard
162 // Save/Load behavior. Re-implement in derived class for custom
164 virtual bool DoSaveDocument(const wxString
& file
);
165 virtual bool DoOpenDocument(const wxString
& file
);
168 DECLARE_ABSTRACT_CLASS(wxDocument
)
169 DECLARE_NO_COPY_CLASS(wxDocument
)
172 class WXDLLEXPORT wxView
: public wxEvtHandler
175 // wxView(wxDocument *doc = (wxDocument *) NULL);
179 wxDocument
*GetDocument() const { return m_viewDocument
; }
180 virtual void SetDocument(wxDocument
*doc
);
182 wxString
GetViewName() const { return m_viewTypeName
; }
183 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
185 wxWindow
*GetFrame() const { return m_viewFrame
; }
186 void SetFrame(wxWindow
*frame
) { m_viewFrame
= frame
; }
188 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
189 virtual void OnDraw(wxDC
*dc
) = 0;
190 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
191 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
192 virtual void OnClosingDocument() {}
193 virtual void OnChangeFilename();
195 // Called by framework if created automatically by the default document
196 // manager class: gives view a chance to initialise
197 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return true; };
199 // Checks if the view is the last one for the document; if so, asks user
200 // to confirm save data (if modified). If ok, deletes itself and returns
202 virtual bool Close(bool deleteWindow
= true);
204 // Override to do cleanup/veto close
205 virtual bool OnClose(bool deleteWindow
);
207 // Extend event processing to search the document's event table
208 virtual bool ProcessEvent(wxEvent
& event
);
210 // A view's window can call this to notify the view it is (in)active.
211 // The function then notifies the document manager.
212 virtual void Activate(bool activate
);
214 wxDocManager
*GetDocumentManager() const
215 { return m_viewDocument
->GetDocumentManager(); }
217 #if wxUSE_PRINTING_ARCHITECTURE
218 virtual wxPrintout
*OnCreatePrintout();
222 wxDocument
* m_viewDocument
;
223 wxString m_viewTypeName
;
224 wxWindow
* m_viewFrame
;
227 DECLARE_ABSTRACT_CLASS(wxView
)
228 DECLARE_NO_COPY_CLASS(wxView
)
231 // Represents user interface (and other) properties of documents and views
232 class WXDLLEXPORT wxDocTemplate
: public wxObject
235 friend class WXDLLEXPORT wxDocManager
;
238 // Associate document and view types. They're for identifying what view is
239 // associated with what template/document type
240 wxDocTemplate(wxDocManager
*manager
,
241 const wxString
& descr
,
242 const wxString
& filter
,
245 const wxString
& docTypeName
,
246 const wxString
& viewTypeName
,
247 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
248 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
249 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
251 virtual ~wxDocTemplate();
253 // By default, these two member functions dynamically creates document and
254 // view using dynamic instance construction. Override these if you need a
255 // different method of construction.
256 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
257 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
259 // Helper method for CreateDocument; also allows you to do your own document
261 virtual bool InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
= 0);
263 wxString
GetDefaultExtension() const { return m_defaultExt
; };
264 wxString
GetDescription() const { return m_description
; }
265 wxString
GetDirectory() const { return m_directory
; };
266 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
267 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
268 wxString
GetFileFilter() const { return m_fileFilter
; };
269 long GetFlags() const { return m_flags
; };
270 virtual wxString
GetViewName() const { return m_viewTypeName
; }
271 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
273 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
274 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
275 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
276 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
277 void SetFlags(long flags
) { m_flags
= flags
; };
279 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
281 wxClassInfo
* GetDocClassInfo() const { return m_docClassInfo
; }
282 wxClassInfo
* GetViewClassInfo() const { return m_viewClassInfo
; }
284 virtual bool FileMatchesTemplate(const wxString
& path
);
288 wxString m_fileFilter
;
289 wxString m_directory
;
290 wxString m_description
;
291 wxString m_defaultExt
;
292 wxString m_docTypeName
;
293 wxString m_viewTypeName
;
294 wxDocManager
* m_documentManager
;
296 // For dynamic creation of appropriate instances.
297 wxClassInfo
* m_docClassInfo
;
298 wxClassInfo
* m_viewClassInfo
;
300 // Called by CreateDocument and CreateView to create the actual document/view object.
301 // By default uses the ClassInfo provided to the constructor. Override these functions
302 // to provide a different method of creation.
303 virtual wxDocument
*DoCreateDocument();
304 virtual wxView
*DoCreateView();
307 DECLARE_CLASS(wxDocTemplate
)
308 DECLARE_NO_COPY_CLASS(wxDocTemplate
)
311 // One object of this class may be created in an application, to manage all
312 // the templates and documents.
313 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
316 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= true);
317 virtual ~wxDocManager();
319 virtual bool Initialize();
321 // Handlers for common user commands
322 void OnFileClose(wxCommandEvent
& event
);
323 void OnFileCloseAll(wxCommandEvent
& event
);
324 void OnFileNew(wxCommandEvent
& event
);
325 void OnFileOpen(wxCommandEvent
& event
);
326 void OnFileRevert(wxCommandEvent
& event
);
327 void OnFileSave(wxCommandEvent
& event
);
328 void OnFileSaveAs(wxCommandEvent
& event
);
329 void OnPrint(wxCommandEvent
& event
);
330 void OnPreview(wxCommandEvent
& event
);
331 void OnUndo(wxCommandEvent
& event
);
332 void OnRedo(wxCommandEvent
& event
);
334 // Handlers for UI update commands
335 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
336 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
337 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
338 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
339 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
340 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
341 void OnUpdateUndo(wxUpdateUIEvent
& event
);
342 void OnUpdateRedo(wxUpdateUIEvent
& event
);
344 void OnUpdatePrint(wxUpdateUIEvent
& event
);
345 void OnUpdatePreview(wxUpdateUIEvent
& event
);
347 // Extend event processing to search the view's event table
348 virtual bool ProcessEvent(wxEvent
& event
);
350 // called when file format detection didn't work, can be overridden to do
351 // something in this case
352 virtual void OnOpenFileFailure() { }
354 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
355 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
356 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
357 virtual bool FlushDoc(wxDocument
*doc
);
358 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
359 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
360 int noTemplates
, wxString
& path
, long flags
, bool save
= false);
361 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
362 int noTemplates
, bool sort
= false);
363 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
364 int noTemplates
, bool sort
= false);
365 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
367 void AssociateTemplate(wxDocTemplate
*temp
);
368 void DisassociateTemplate(wxDocTemplate
*temp
);
370 wxDocument
*GetCurrentDocument() const;
372 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
373 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
375 // Add and remove a document from the manager's list
376 void AddDocument(wxDocument
*doc
);
377 void RemoveDocument(wxDocument
*doc
);
379 // closes all currently open documents
380 bool CloseDocuments(bool force
= true);
382 // closes the specified document
383 bool CloseDocument(wxDocument
* doc
, bool force
= false);
385 // Clear remaining documents and templates
386 bool Clear(bool force
= true);
388 // Views or windows should inform the document manager
389 // when a view is going in or out of focus
390 virtual void ActivateView(wxView
*view
, bool activate
= true);
391 virtual wxView
*GetCurrentView() const;
393 wxList
& GetDocuments() { return m_docs
; }
394 wxList
& GetTemplates() { return m_templates
; }
396 // Make a default document name
397 virtual bool MakeDefaultName(wxString
& buf
);
399 // Make a frame title (override this to do something different)
400 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
402 virtual wxFileHistory
*OnCreateFileHistory();
403 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
405 // File history management
406 virtual void AddFileToHistory(const wxString
& file
);
407 virtual void RemoveFileFromHistory(size_t i
);
408 virtual size_t GetHistoryFilesCount() const;
409 virtual wxString
GetHistoryFile(size_t i
) const;
410 virtual void FileHistoryUseMenu(wxMenu
*menu
);
411 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
413 virtual void FileHistoryLoad(wxConfigBase
& config
);
414 virtual void FileHistorySave(wxConfigBase
& config
);
415 #endif // wxUSE_CONFIG
417 virtual void FileHistoryAddFilesToMenu();
418 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
420 wxString
GetLastDirectory() const { return m_lastDirectory
; }
421 void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
423 // Get the current document manager
424 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
426 #if WXWIN_COMPATIBILITY_2_6
427 // deprecated, use GetHistoryFilesCount() instead
428 wxDEPRECATED( size_t GetNoHistoryFiles() const );
429 #endif // WXWIN_COMPATIBILITY_2_6
433 int m_defaultDocumentNameCounter
;
437 wxView
* m_currentView
;
438 wxFileHistory
* m_fileHistory
;
439 wxString m_lastDirectory
;
440 static wxDocManager
* sm_docManager
;
442 DECLARE_EVENT_TABLE()
443 DECLARE_DYNAMIC_CLASS(wxDocManager
)
444 DECLARE_NO_COPY_CLASS(wxDocManager
)
447 #if WXWIN_COMPATIBILITY_2_6
448 inline size_t wxDocManager::GetNoHistoryFiles() const
450 return GetHistoryFilesCount();
452 #endif // WXWIN_COMPATIBILITY_2_6
454 // ----------------------------------------------------------------------------
455 // A default child frame
456 // ----------------------------------------------------------------------------
458 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
461 wxDocChildFrame(wxDocument
*doc
,
465 const wxString
& title
,
466 const wxPoint
& pos
= wxDefaultPosition
,
467 const wxSize
& size
= wxDefaultSize
,
468 long type
= wxDEFAULT_FRAME_STYLE
,
469 const wxString
& name
= wxT("frame"));
470 virtual ~wxDocChildFrame(){}
472 // Extend event processing to search the view's event table
473 virtual bool ProcessEvent(wxEvent
& event
);
475 void OnActivate(wxActivateEvent
& event
);
476 void OnCloseWindow(wxCloseEvent
& event
);
478 wxDocument
*GetDocument() const { return m_childDocument
; }
479 wxView
*GetView() const { return m_childView
; }
480 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
481 void SetView(wxView
*view
) { m_childView
= view
; }
482 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
485 wxDocument
* m_childDocument
;
489 DECLARE_CLASS(wxDocChildFrame
)
490 DECLARE_EVENT_TABLE()
491 DECLARE_NO_COPY_CLASS(wxDocChildFrame
)
494 // ----------------------------------------------------------------------------
495 // A default parent frame
496 // ----------------------------------------------------------------------------
498 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
502 wxDocParentFrame(wxDocManager
*manager
,
505 const wxString
& title
,
506 const wxPoint
& pos
= wxDefaultPosition
,
507 const wxSize
& size
= wxDefaultSize
,
508 long style
= wxDEFAULT_FRAME_STYLE
,
509 const wxString
& name
= wxFrameNameStr
);
511 bool Create(wxDocManager
*manager
,
514 const wxString
& title
,
515 const wxPoint
& pos
= wxDefaultPosition
,
516 const wxSize
& size
= wxDefaultSize
,
517 long style
= wxDEFAULT_FRAME_STYLE
,
518 const wxString
& name
= wxFrameNameStr
);
520 // Extend event processing to search the document manager's event table
521 virtual bool ProcessEvent(wxEvent
& event
);
523 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
525 void OnExit(wxCommandEvent
& event
);
526 void OnMRUFile(wxCommandEvent
& event
);
527 void OnCloseWindow(wxCloseEvent
& event
);
530 wxDocManager
*m_docManager
;
533 typedef wxFrame base_type
;
534 DECLARE_CLASS(wxDocParentFrame
)
535 DECLARE_EVENT_TABLE()
536 DECLARE_NO_COPY_CLASS(wxDocParentFrame
)
539 // ----------------------------------------------------------------------------
540 // Provide simple default printing facilities
541 // ----------------------------------------------------------------------------
543 #if wxUSE_PRINTING_ARCHITECTURE
544 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
547 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= wxT("Printout"));
548 bool OnPrintPage(int page
);
549 bool HasPage(int page
);
550 bool OnBeginDocument(int startPage
, int endPage
);
551 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
553 virtual wxView
*GetView() { return m_printoutView
; }
556 wxView
* m_printoutView
;
559 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
560 DECLARE_NO_COPY_CLASS(wxDocPrintout
)
562 #endif // wxUSE_PRINTING_ARCHITECTURE
564 // ----------------------------------------------------------------------------
565 // File history management
566 // ----------------------------------------------------------------------------
568 class WXDLLEXPORT wxFileHistory
: public wxObject
571 wxFileHistory(size_t maxFiles
= 9, wxWindowID idBase
= wxID_FILE1
);
572 virtual ~wxFileHistory();
575 virtual void AddFileToHistory(const wxString
& file
);
576 virtual void RemoveFileFromHistory(size_t i
);
577 virtual int GetMaxFiles() const { return (int)m_fileMaxFiles
; }
578 virtual void UseMenu(wxMenu
*menu
);
580 // Remove menu from the list (MDI child may be closing)
581 virtual void RemoveMenu(wxMenu
*menu
);
584 virtual void Load(wxConfigBase
& config
);
585 virtual void Save(wxConfigBase
& config
);
586 #endif // wxUSE_CONFIG
588 virtual void AddFilesToMenu();
589 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
592 virtual wxString
GetHistoryFile(size_t i
) const;
593 virtual size_t GetCount() const { return m_fileHistoryN
; }
595 const wxList
& GetMenus() const { return m_fileMenus
; }
597 #if wxABI_VERSION >= 20802
599 void SetBaseId(wxWindowID baseId
) { m_idBase
= baseId
; }
600 wxWindowID
GetBaseId() const { return m_idBase
; }
601 #endif // wxABI 2.8.2+
603 #if WXWIN_COMPATIBILITY_2_6
604 // deprecated, use GetCount() instead
605 wxDEPRECATED( size_t GetNoHistoryFiles() const );
606 #endif // WXWIN_COMPATIBILITY_2_6
610 wxChar
** m_fileHistory
;
611 // Number of files saved
612 size_t m_fileHistoryN
;
613 // Menus to maintain (may need several for an MDI app)
615 // Max files to maintain
616 size_t m_fileMaxFiles
;
619 // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
622 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
623 DECLARE_NO_COPY_CLASS(wxFileHistory
)
626 #if WXWIN_COMPATIBILITY_2_6
627 inline size_t wxFileHistory::GetNoHistoryFiles() const
629 return m_fileHistoryN
;
631 #endif // WXWIN_COMPATIBILITY_2_6
633 #if wxUSE_STD_IOSTREAM
634 // For compatibility with existing file formats:
635 // converts from/to a stream to/from a temporary file.
636 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
637 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
639 // For compatibility with existing file formats:
640 // converts from/to a stream to/from a temporary file.
641 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
642 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);
643 #endif // wxUSE_STD_IOSTREAM
645 #endif // wxUSE_DOC_VIEW_ARCHITECTURE