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/cmndata.h"
21 #include "wx/string.h"
24 #if wxUSE_PRINTING_ARCHITECTURE
28 class WXDLLEXPORT wxWindow
;
29 class WXDLLEXPORT wxDocument
;
30 class WXDLLEXPORT wxView
;
31 class WXDLLEXPORT wxDocTemplate
;
32 class WXDLLEXPORT wxDocManager
;
33 class WXDLLEXPORT wxPrintInfo
;
34 class WXDLLEXPORT wxCommandProcessor
;
35 class WXDLLEXPORT wxFileHistory
;
36 class WXDLLEXPORT wxConfigBase
;
38 #if wxUSE_STD_IOSTREAM
39 #include "wx/iosfwrap.h"
41 #include "wx/stream.h"
44 // Document manager flags
51 wxDEFAULT_DOCMAN_FLAGS
= wxDOC_SDI
54 // Document template flags
57 wxTEMPLATE_VISIBLE
= 1,
59 wxDEFAULT_TEMPLATE_FLAGS
= wxTEMPLATE_VISIBLE
62 #define wxMAX_FILE_HISTORY 9
64 class WXDLLEXPORT wxDocument
: public wxEvtHandler
67 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
71 void SetFilename(const wxString
& filename
, bool notifyViews
= false);
72 wxString
GetFilename() const { return m_documentFile
; }
74 void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
75 wxString
GetTitle() const { return m_documentTitle
; }
77 void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
78 wxString
GetDocumentName() const { return m_documentTypeName
; }
80 bool GetDocumentSaved() const { return m_savedYet
; }
81 void SetDocumentSaved(bool saved
= true) { m_savedYet
= saved
; }
85 virtual bool SaveAs();
86 virtual bool Revert();
88 #if wxUSE_STD_IOSTREAM
89 virtual wxSTD ostream
& SaveObject(wxSTD ostream
& stream
);
90 virtual wxSTD istream
& LoadObject(wxSTD istream
& stream
);
92 virtual wxOutputStream
& SaveObject(wxOutputStream
& stream
);
93 virtual wxInputStream
& LoadObject(wxInputStream
& stream
);
96 // Called by wxWidgets
97 virtual bool OnSaveDocument(const wxString
& filename
);
98 virtual bool OnOpenDocument(const wxString
& filename
);
99 virtual bool OnNewDocument();
100 virtual bool OnCloseDocument();
102 // Prompts for saving if about to close a modified document. Returns true
103 // if ok to close the document (may have saved in the meantime, or set
104 // modified to false)
105 virtual bool OnSaveModified();
107 // Called by framework if created automatically by the default document
108 // manager: gives document a chance to initialise and (usually) create a
110 virtual bool OnCreate(const wxString
& path
, long flags
);
112 // By default, creates a base wxCommandProcessor.
113 virtual wxCommandProcessor
*OnCreateCommandProcessor();
114 virtual wxCommandProcessor
*GetCommandProcessor() const { return m_commandProcessor
; }
115 virtual void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
117 // Called after a view is added or removed. The default implementation
118 // deletes the document if this is there are no more views.
119 virtual void OnChangedViewList();
121 virtual bool DeleteContents();
123 virtual bool Draw(wxDC
&);
124 virtual bool IsModified() const { return m_documentModified
; }
125 virtual void Modify(bool mod
) { m_documentModified
= mod
; }
127 virtual bool AddView(wxView
*view
);
128 virtual bool RemoveView(wxView
*view
);
129 wxList
& GetViews() { return m_documentViews
; }
130 const wxList
& GetViews() const { return m_documentViews
; }
131 wxView
*GetFirstView() const;
133 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
134 virtual void NotifyClosing();
136 // Remove all views (because we're closing the document)
137 virtual bool DeleteAllViews();
140 virtual wxDocManager
*GetDocumentManager() const;
141 virtual wxDocTemplate
*GetDocumentTemplate() const { return m_documentTemplate
; }
142 virtual void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
144 // Get title, or filename if no title, else [unnamed]
145 virtual bool GetPrintableName(wxString
& buf
) const;
147 // Returns a window that can be used as a parent for document-related
148 // dialogs. Override if necessary.
149 virtual wxWindow
*GetDocumentWindow() const;
152 wxList m_documentViews
;
153 wxString m_documentFile
;
154 wxString m_documentTitle
;
155 wxString m_documentTypeName
;
156 wxDocTemplate
* m_documentTemplate
;
157 bool m_documentModified
;
158 wxDocument
* m_documentParent
;
159 wxCommandProcessor
* m_commandProcessor
;
162 // Called by OnSaveDocument and OnOpenDocument to implement standard
163 // Save/Load behavior. Re-implement in derived class for custom
165 virtual bool DoSaveDocument(const wxString
& file
);
166 virtual bool DoOpenDocument(const wxString
& file
);
169 DECLARE_ABSTRACT_CLASS(wxDocument
)
170 DECLARE_NO_COPY_CLASS(wxDocument
)
173 class WXDLLEXPORT wxView
: public wxEvtHandler
176 // wxView(wxDocument *doc = (wxDocument *) NULL);
180 wxDocument
*GetDocument() const { return m_viewDocument
; }
181 virtual void SetDocument(wxDocument
*doc
);
183 wxString
GetViewName() const { return m_viewTypeName
; }
184 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
186 wxWindow
*GetFrame() const { return m_viewFrame
; }
187 void SetFrame(wxWindow
*frame
) { m_viewFrame
= frame
; }
189 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
190 virtual void OnDraw(wxDC
*dc
) = 0;
191 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
192 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
193 virtual void OnClosingDocument() {}
194 virtual void OnChangeFilename();
196 // Called by framework if created automatically by the default document
197 // manager class: gives view a chance to initialise
198 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return true; };
200 // Checks if the view is the last one for the document; if so, asks user
201 // to confirm save data (if modified). If ok, deletes itself and returns
203 virtual bool Close(bool deleteWindow
= true);
205 // Override to do cleanup/veto close
206 virtual bool OnClose(bool deleteWindow
);
208 // Extend event processing to search the document's event table
209 virtual bool ProcessEvent(wxEvent
& event
);
211 // A view's window can call this to notify the view it is (in)active.
212 // The function then notifies the document manager.
213 virtual void Activate(bool activate
);
215 wxDocManager
*GetDocumentManager() const
216 { return m_viewDocument
->GetDocumentManager(); }
218 #if wxUSE_PRINTING_ARCHITECTURE
219 virtual wxPrintout
*OnCreatePrintout();
223 wxDocument
* m_viewDocument
;
224 wxString m_viewTypeName
;
225 wxWindow
* m_viewFrame
;
228 DECLARE_ABSTRACT_CLASS(wxView
)
229 DECLARE_NO_COPY_CLASS(wxView
)
232 // Represents user interface (and other) properties of documents and views
233 class WXDLLEXPORT wxDocTemplate
: public wxObject
236 friend class WXDLLEXPORT wxDocManager
;
239 // Associate document and view types. They're for identifying what view is
240 // associated with what template/document type
241 wxDocTemplate(wxDocManager
*manager
,
242 const wxString
& descr
,
243 const wxString
& filter
,
246 const wxString
& docTypeName
,
247 const wxString
& viewTypeName
,
248 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
249 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
250 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
254 // By default, these two member functions dynamically creates document and
255 // view using dynamic instance construction. Override these if you need a
256 // different method of construction.
257 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
258 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
260 // Helper method for CreateDocument; also allows you to do your own document
262 virtual bool InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
= 0);
264 wxString
GetDefaultExtension() const { return m_defaultExt
; };
265 wxString
GetDescription() const { return m_description
; }
266 wxString
GetDirectory() const { return m_directory
; };
267 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
268 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
269 wxString
GetFileFilter() const { return m_fileFilter
; };
270 long GetFlags() const { return m_flags
; };
271 virtual wxString
GetViewName() const { return m_viewTypeName
; }
272 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
274 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
275 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
276 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
277 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
278 void SetFlags(long flags
) { m_flags
= flags
; };
280 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
282 wxClassInfo
* GetDocClassInfo() const { return m_docClassInfo
; }
283 wxClassInfo
* GetViewClassInfo() const { return m_viewClassInfo
; }
285 virtual bool FileMatchesTemplate(const wxString
& path
);
289 wxString m_fileFilter
;
290 wxString m_directory
;
291 wxString m_description
;
292 wxString m_defaultExt
;
293 wxString m_docTypeName
;
294 wxString m_viewTypeName
;
295 wxDocManager
* m_documentManager
;
297 // For dynamic creation of appropriate instances.
298 wxClassInfo
* m_docClassInfo
;
299 wxClassInfo
* m_viewClassInfo
;
301 // Called by CreateDocument and CreateView to create the actual document/view object.
302 // By default uses the ClassInfo provided to the constructor. Override these functions
303 // to provide a different method of creation.
304 virtual wxDocument
*DoCreateDocument();
305 virtual wxView
*DoCreateView();
308 DECLARE_CLASS(wxDocTemplate
)
309 DECLARE_NO_COPY_CLASS(wxDocTemplate
)
312 // One object of this class may be created in an application, to manage all
313 // the templates and documents.
314 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
317 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= true);
320 virtual bool Initialize();
322 // Handlers for common user commands
323 void OnFileClose(wxCommandEvent
& event
);
324 void OnFileCloseAll(wxCommandEvent
& event
);
325 void OnFileNew(wxCommandEvent
& event
);
326 void OnFileOpen(wxCommandEvent
& event
);
327 void OnFileRevert(wxCommandEvent
& event
);
328 void OnFileSave(wxCommandEvent
& event
);
329 void OnFileSaveAs(wxCommandEvent
& event
);
330 void OnPrint(wxCommandEvent
& event
);
331 void OnPreview(wxCommandEvent
& event
);
332 void OnUndo(wxCommandEvent
& event
);
333 void OnRedo(wxCommandEvent
& event
);
335 // Handlers for UI update commands
336 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
337 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
338 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
339 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
340 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
341 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
342 void OnUpdateUndo(wxUpdateUIEvent
& event
);
343 void OnUpdateRedo(wxUpdateUIEvent
& event
);
345 void OnUpdatePrint(wxUpdateUIEvent
& event
);
346 void OnUpdatePreview(wxUpdateUIEvent
& event
);
348 // Extend event processing to search the view's event table
349 virtual bool ProcessEvent(wxEvent
& event
);
351 // called when file format detection didn't work, can be overridden to do
352 // something in this case
353 virtual void OnOpenFileFailure() { }
355 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
356 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
357 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
358 virtual bool FlushDoc(wxDocument
*doc
);
359 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
360 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
361 int noTemplates
, wxString
& path
, long flags
, bool save
= false);
362 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
363 int noTemplates
, bool sort
= false);
364 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
365 int noTemplates
, bool sort
= false);
366 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
368 void AssociateTemplate(wxDocTemplate
*temp
);
369 void DisassociateTemplate(wxDocTemplate
*temp
);
371 wxDocument
*GetCurrentDocument() const;
373 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
374 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
376 // Add and remove a document from the manager's list
377 void AddDocument(wxDocument
*doc
);
378 void RemoveDocument(wxDocument
*doc
);
380 // closes all currently open documents
381 bool CloseDocuments(bool force
= true);
383 // closes the specified document
384 bool CloseDocument(wxDocument
* doc
, bool force
= false);
386 // Clear remaining documents and templates
387 bool Clear(bool force
= true);
389 // Views or windows should inform the document manager
390 // when a view is going in or out of focus
391 virtual void ActivateView(wxView
*view
, bool activate
= true);
392 virtual wxView
*GetCurrentView() const;
394 wxList
& GetDocuments() { return m_docs
; }
395 wxList
& GetTemplates() { return m_templates
; }
397 // Make a default document name
398 virtual bool MakeDefaultName(wxString
& buf
);
400 // Make a frame title (override this to do something different)
401 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
403 virtual wxFileHistory
*OnCreateFileHistory();
404 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
406 // File history management
407 virtual void AddFileToHistory(const wxString
& file
);
408 virtual void RemoveFileFromHistory(size_t i
);
409 virtual size_t GetHistoryFilesCount() const;
410 virtual wxString
GetHistoryFile(size_t i
) const;
411 virtual void FileHistoryUseMenu(wxMenu
*menu
);
412 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
414 virtual void FileHistoryLoad(wxConfigBase
& config
);
415 virtual void FileHistorySave(wxConfigBase
& config
);
416 #endif // wxUSE_CONFIG
418 virtual void FileHistoryAddFilesToMenu();
419 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
421 wxString
GetLastDirectory() const { return m_lastDirectory
; }
422 void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
424 // Get the current document manager
425 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
427 #if WXWIN_COMPATIBILITY_2_6
428 // deprecated, use GetHistoryFilesCount() instead
429 wxDEPRECATED( size_t GetNoHistoryFiles() const );
430 #endif // WXWIN_COMPATIBILITY_2_6
434 int m_defaultDocumentNameCounter
;
438 wxView
* m_currentView
;
439 wxFileHistory
* m_fileHistory
;
440 wxString m_lastDirectory
;
441 static wxDocManager
* sm_docManager
;
443 DECLARE_EVENT_TABLE()
444 DECLARE_DYNAMIC_CLASS(wxDocManager
)
445 DECLARE_NO_COPY_CLASS(wxDocManager
)
448 #if WXWIN_COMPATIBILITY_2_6
449 inline size_t wxDocManager::GetNoHistoryFiles() const
451 return GetHistoryFilesCount();
453 #endif // WXWIN_COMPATIBILITY_2_6
455 // ----------------------------------------------------------------------------
456 // A default child frame
457 // ----------------------------------------------------------------------------
459 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
462 wxDocChildFrame(wxDocument
*doc
,
466 const wxString
& title
,
467 const wxPoint
& pos
= wxDefaultPosition
,
468 const wxSize
& size
= wxDefaultSize
,
469 long type
= wxDEFAULT_FRAME_STYLE
,
470 const wxString
& name
= wxT("frame"));
473 // Extend event processing to search the view's event table
474 virtual bool ProcessEvent(wxEvent
& event
);
476 void OnActivate(wxActivateEvent
& event
);
477 void OnCloseWindow(wxCloseEvent
& event
);
479 wxDocument
*GetDocument() const { return m_childDocument
; }
480 wxView
*GetView() const { return m_childView
; }
481 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
482 void SetView(wxView
*view
) { m_childView
= view
; }
483 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
486 wxDocument
* m_childDocument
;
490 DECLARE_CLASS(wxDocChildFrame
)
491 DECLARE_EVENT_TABLE()
492 DECLARE_NO_COPY_CLASS(wxDocChildFrame
)
495 // ----------------------------------------------------------------------------
496 // A default parent frame
497 // ----------------------------------------------------------------------------
499 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 type
= wxDEFAULT_FRAME_STYLE
,
509 const wxString
& name
= wxT("frame"));
511 // Extend event processing to search the document manager's event table
512 virtual bool ProcessEvent(wxEvent
& event
);
514 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
516 void OnExit(wxCommandEvent
& event
);
517 void OnMRUFile(wxCommandEvent
& event
);
518 void OnCloseWindow(wxCloseEvent
& event
);
521 wxDocManager
*m_docManager
;
524 DECLARE_CLASS(wxDocParentFrame
)
525 DECLARE_EVENT_TABLE()
526 DECLARE_NO_COPY_CLASS(wxDocParentFrame
)
529 // ----------------------------------------------------------------------------
530 // Provide simple default printing facilities
531 // ----------------------------------------------------------------------------
533 #if wxUSE_PRINTING_ARCHITECTURE
534 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
537 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= wxT("Printout"));
538 bool OnPrintPage(int page
);
539 bool HasPage(int page
);
540 bool OnBeginDocument(int startPage
, int endPage
);
541 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
543 virtual wxView
*GetView() { return m_printoutView
; }
546 wxView
* m_printoutView
;
549 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
550 DECLARE_NO_COPY_CLASS(wxDocPrintout
)
552 #endif // wxUSE_PRINTING_ARCHITECTURE
554 // ----------------------------------------------------------------------------
555 // File history management
556 // ----------------------------------------------------------------------------
558 class WXDLLEXPORT wxFileHistory
: public wxObject
561 wxFileHistory(size_t maxFiles
= 9, wxWindowID idBase
= wxID_FILE1
);
565 virtual void AddFileToHistory(const wxString
& file
);
566 virtual void RemoveFileFromHistory(size_t i
);
567 virtual int GetMaxFiles() const { return (int)m_fileMaxFiles
; }
568 virtual void UseMenu(wxMenu
*menu
);
570 // Remove menu from the list (MDI child may be closing)
571 virtual void RemoveMenu(wxMenu
*menu
);
574 virtual void Load(wxConfigBase
& config
);
575 virtual void Save(wxConfigBase
& config
);
576 #endif // wxUSE_CONFIG
578 virtual void AddFilesToMenu();
579 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
582 virtual wxString
GetHistoryFile(size_t i
) const;
583 virtual size_t GetCount() const { return m_fileHistoryN
; }
585 const wxList
& GetMenus() const { return m_fileMenus
; }
587 #if WXWIN_COMPATIBILITY_2_6
588 // deprecated, use GetCount() instead
589 wxDEPRECATED( size_t GetNoHistoryFiles() const );
590 #endif // WXWIN_COMPATIBILITY_2_6
594 wxChar
** m_fileHistory
;
595 // Number of files saved
596 size_t m_fileHistoryN
;
597 // Menus to maintain (may need several for an MDI app)
599 // Max files to maintain
600 size_t m_fileMaxFiles
;
603 // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
606 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
607 DECLARE_NO_COPY_CLASS(wxFileHistory
)
610 #if WXWIN_COMPATIBILITY_2_6
611 inline size_t wxFileHistory::GetNoHistoryFiles() const
613 return m_fileHistoryN
;
615 #endif // WXWIN_COMPATIBILITY_2_6
617 #if wxUSE_STD_IOSTREAM
618 // For compatibility with existing file formats:
619 // converts from/to a stream to/from a temporary file.
620 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
621 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
623 // For compatibility with existing file formats:
624 // converts from/to a stream to/from a temporary file.
625 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
626 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);
627 #endif // wxUSE_STD_IOSTREAM
629 #endif // wxUSE_DOC_VIEW_ARCHITECTURE