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() const { return (wxList
&) 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
);
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);
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 // deprecated, use GetHistoryFilesCount() instead
427 wxDEPRECATED( size_t GetNoHistoryFiles() const );
431 int m_defaultDocumentNameCounter
;
435 wxView
* m_currentView
;
436 wxFileHistory
* m_fileHistory
;
437 wxString m_lastDirectory
;
438 static wxDocManager
* sm_docManager
;
440 DECLARE_EVENT_TABLE()
441 DECLARE_DYNAMIC_CLASS(wxDocManager
)
442 DECLARE_NO_COPY_CLASS(wxDocManager
)
445 inline size_t wxDocManager::GetNoHistoryFiles() const
447 return GetHistoryFilesCount();
450 // ----------------------------------------------------------------------------
451 // A default child frame
452 // ----------------------------------------------------------------------------
454 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
457 wxDocChildFrame(wxDocument
*doc
,
461 const wxString
& title
,
462 const wxPoint
& pos
= wxDefaultPosition
,
463 const wxSize
& size
= wxDefaultSize
,
464 long type
= wxDEFAULT_FRAME_STYLE
,
465 const wxString
& name
= wxT("frame"));
468 // Extend event processing to search the view's event table
469 virtual bool ProcessEvent(wxEvent
& event
);
471 void OnActivate(wxActivateEvent
& event
);
472 void OnCloseWindow(wxCloseEvent
& event
);
474 wxDocument
*GetDocument() const { return m_childDocument
; }
475 wxView
*GetView() const { return m_childView
; }
476 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
477 void SetView(wxView
*view
) { m_childView
= view
; }
478 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
481 wxDocument
* m_childDocument
;
485 DECLARE_CLASS(wxDocChildFrame
)
486 DECLARE_EVENT_TABLE()
487 DECLARE_NO_COPY_CLASS(wxDocChildFrame
)
490 // ----------------------------------------------------------------------------
491 // A default parent frame
492 // ----------------------------------------------------------------------------
494 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
497 wxDocParentFrame(wxDocManager
*manager
,
500 const wxString
& title
,
501 const wxPoint
& pos
= wxDefaultPosition
,
502 const wxSize
& size
= wxDefaultSize
,
503 long type
= wxDEFAULT_FRAME_STYLE
,
504 const wxString
& name
= wxT("frame"));
506 // Extend event processing to search the document manager's event table
507 virtual bool ProcessEvent(wxEvent
& event
);
509 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
511 void OnExit(wxCommandEvent
& event
);
512 void OnMRUFile(wxCommandEvent
& event
);
513 void OnCloseWindow(wxCloseEvent
& event
);
516 wxDocManager
*m_docManager
;
519 DECLARE_CLASS(wxDocParentFrame
)
520 DECLARE_EVENT_TABLE()
521 DECLARE_NO_COPY_CLASS(wxDocParentFrame
)
524 // ----------------------------------------------------------------------------
525 // Provide simple default printing facilities
526 // ----------------------------------------------------------------------------
528 #if wxUSE_PRINTING_ARCHITECTURE
529 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
532 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= wxT("Printout"));
533 bool OnPrintPage(int page
);
534 bool HasPage(int page
);
535 bool OnBeginDocument(int startPage
, int endPage
);
536 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
538 virtual wxView
*GetView() { return m_printoutView
; }
541 wxView
* m_printoutView
;
544 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
545 DECLARE_NO_COPY_CLASS(wxDocPrintout
)
547 #endif // wxUSE_PRINTING_ARCHITECTURE
549 // ----------------------------------------------------------------------------
550 // File history management
551 // ----------------------------------------------------------------------------
553 class WXDLLEXPORT wxFileHistory
: public wxObject
556 wxFileHistory(size_t maxFiles
= 9, wxWindowID idBase
= wxID_FILE1
);
560 virtual void AddFileToHistory(const wxString
& file
);
561 virtual void RemoveFileFromHistory(size_t i
);
562 virtual int GetMaxFiles() const { return (int)m_fileMaxFiles
; }
563 virtual void UseMenu(wxMenu
*menu
);
565 // Remove menu from the list (MDI child may be closing)
566 virtual void RemoveMenu(wxMenu
*menu
);
569 virtual void Load(wxConfigBase
& config
);
570 virtual void Save(wxConfigBase
& config
);
571 #endif // wxUSE_CONFIG
573 virtual void AddFilesToMenu();
574 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
577 virtual wxString
GetHistoryFile(size_t i
) const;
578 virtual size_t GetCount() const { return m_fileHistoryN
; }
580 const wxList
& GetMenus() const { return m_fileMenus
; }
582 // deprecated, use GetCount() instead
583 wxDEPRECATED( size_t GetNoHistoryFiles() const );
587 wxChar
** m_fileHistory
;
588 // Number of files saved
589 size_t m_fileHistoryN
;
590 // Menus to maintain (may need several for an MDI app)
592 // Max files to maintain
593 size_t m_fileMaxFiles
;
596 // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
599 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
600 DECLARE_NO_COPY_CLASS(wxFileHistory
)
603 inline size_t wxFileHistory::GetNoHistoryFiles() const
605 return m_fileHistoryN
;
608 #if wxUSE_STD_IOSTREAM
609 // For compatibility with existing file formats:
610 // converts from/to a stream to/from a temporary file.
611 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
612 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
614 // For compatibility with existing file formats:
615 // converts from/to a stream to/from a temporary file.
616 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
617 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);
618 #endif // wxUSE_STD_IOSTREAM
620 #endif // wxUSE_DOC_VIEW_ARCHITECTURE