1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Doc/View classes
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "docview.h"
21 #if wxUSE_DOC_VIEW_ARCHITECTURE
24 #include "wx/cmndata.h"
25 #include "wx/string.h"
28 #if wxUSE_PRINTING_ARCHITECTURE
32 class WXDLLEXPORT wxWindow
;
33 class WXDLLEXPORT wxDocument
;
34 class WXDLLEXPORT wxView
;
35 class WXDLLEXPORT wxDocTemplate
;
36 class WXDLLEXPORT wxDocManager
;
37 class WXDLLEXPORT wxPrintInfo
;
38 class WXDLLEXPORT wxCommandProcessor
;
39 class WXDLLEXPORT wxFileHistory
;
40 class WXDLLEXPORT wxConfigBase
;
42 #if wxUSE_STD_IOSTREAM
43 #include "wx/iosfwrap.h"
45 #include "wx/stream.h"
48 // Document manager flags
55 wxDEFAULT_DOCMAN_FLAGS
= wxDOC_SDI
58 // Document template flags
61 wxTEMPLATE_VISIBLE
= 1,
63 wxDEFAULT_TEMPLATE_FLAGS
= wxTEMPLATE_VISIBLE
66 #define wxMAX_FILE_HISTORY 9
68 class WXDLLEXPORT wxDocument
: public wxEvtHandler
71 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
75 void SetFilename(const wxString
& filename
, bool notifyViews
= false);
76 wxString
GetFilename() const { return m_documentFile
; }
78 void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
79 wxString
GetTitle() const { return m_documentTitle
; }
81 void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
82 wxString
GetDocumentName() const { return m_documentTypeName
; }
84 bool GetDocumentSaved() const { return m_savedYet
; }
85 void SetDocumentSaved(bool saved
= true) { m_savedYet
= saved
; }
89 virtual bool SaveAs();
90 virtual bool Revert();
92 #if wxUSE_STD_IOSTREAM
93 virtual wxSTD ostream
& SaveObject(wxSTD ostream
& stream
);
94 virtual wxSTD istream
& LoadObject(wxSTD istream
& stream
);
96 virtual wxOutputStream
& SaveObject(wxOutputStream
& stream
);
97 virtual wxInputStream
& LoadObject(wxInputStream
& stream
);
100 // Called by wxWidgets
101 virtual bool OnSaveDocument(const wxString
& filename
);
102 virtual bool OnOpenDocument(const wxString
& filename
);
103 virtual bool OnNewDocument();
104 virtual bool OnCloseDocument();
106 // Prompts for saving if about to close a modified document. Returns true
107 // if ok to close the document (may have saved in the meantime, or set
108 // modified to false)
109 virtual bool OnSaveModified();
111 // Called by framework if created automatically by the default document
112 // manager: gives document a chance to initialise and (usually) create a
114 virtual bool OnCreate(const wxString
& path
, long flags
);
116 // By default, creates a base wxCommandProcessor.
117 virtual wxCommandProcessor
*OnCreateCommandProcessor();
118 virtual wxCommandProcessor
*GetCommandProcessor() const { return m_commandProcessor
; }
119 virtual void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
121 // Called after a view is added or removed. The default implementation
122 // deletes the document if this is there are no more views.
123 virtual void OnChangedViewList();
125 virtual bool DeleteContents();
127 virtual bool Draw(wxDC
&);
128 virtual bool IsModified() const { return m_documentModified
; }
129 virtual void Modify(bool mod
) { m_documentModified
= mod
; }
131 virtual bool AddView(wxView
*view
);
132 virtual bool RemoveView(wxView
*view
);
133 wxList
& GetViews() const { return (wxList
&) m_documentViews
; }
134 wxView
*GetFirstView() const;
136 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
137 virtual void NotifyClosing();
139 // Remove all views (because we're closing the document)
140 virtual bool DeleteAllViews();
143 virtual wxDocManager
*GetDocumentManager() const;
144 virtual wxDocTemplate
*GetDocumentTemplate() const { return m_documentTemplate
; }
145 virtual void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
147 // Get title, or filename if no title, else [unnamed]
148 virtual bool GetPrintableName(wxString
& buf
) const;
150 // Returns a window that can be used as a parent for document-related
151 // dialogs. Override if necessary.
152 virtual wxWindow
*GetDocumentWindow() const;
155 wxList m_documentViews
;
156 wxString m_documentFile
;
157 wxString m_documentTitle
;
158 wxString m_documentTypeName
;
159 wxDocTemplate
* m_documentTemplate
;
160 bool m_documentModified
;
161 wxDocument
* m_documentParent
;
162 wxCommandProcessor
* m_commandProcessor
;
165 // Called by OnSaveDocument and OnOpenDocument to implement standard
166 // Save/Load behavior. Re-implement in derived class for custom
168 virtual bool DoSaveDocument(const wxString
& file
);
169 virtual bool DoOpenDocument(const wxString
& file
);
172 DECLARE_ABSTRACT_CLASS(wxDocument
)
173 DECLARE_NO_COPY_CLASS(wxDocument
)
176 class WXDLLEXPORT wxView
: public wxEvtHandler
179 // wxView(wxDocument *doc = (wxDocument *) NULL);
183 wxDocument
*GetDocument() const { return m_viewDocument
; }
184 virtual void SetDocument(wxDocument
*doc
);
186 wxString
GetViewName() const { return m_viewTypeName
; }
187 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
189 wxWindow
*GetFrame() const { return m_viewFrame
; }
190 void SetFrame(wxWindow
*frame
) { m_viewFrame
= frame
; }
192 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
193 virtual void OnDraw(wxDC
*dc
) = 0;
194 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
195 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
196 virtual void OnClosingDocument() {}
197 virtual void OnChangeFilename();
199 // Called by framework if created automatically by the default document
200 // manager class: gives view a chance to initialise
201 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return true; };
203 // Checks if the view is the last one for the document; if so, asks user
204 // to confirm save data (if modified). If ok, deletes itself and returns
206 virtual bool Close(bool deleteWindow
= true);
208 // Override to do cleanup/veto close
209 virtual bool OnClose(bool deleteWindow
);
211 // Extend event processing to search the document's event table
212 virtual bool ProcessEvent(wxEvent
& event
);
214 // A view's window can call this to notify the view it is (in)active.
215 // The function then notifies the document manager.
216 virtual void Activate(bool activate
);
218 wxDocManager
*GetDocumentManager() const
219 { return m_viewDocument
->GetDocumentManager(); }
221 #if wxUSE_PRINTING_ARCHITECTURE
222 virtual wxPrintout
*OnCreatePrintout();
226 wxDocument
* m_viewDocument
;
227 wxString m_viewTypeName
;
228 wxWindow
* m_viewFrame
;
231 DECLARE_ABSTRACT_CLASS(wxView
)
232 DECLARE_NO_COPY_CLASS(wxView
)
235 // Represents user interface (and other) properties of documents and views
236 class WXDLLEXPORT wxDocTemplate
: public wxObject
239 friend class WXDLLEXPORT wxDocManager
;
242 // Associate document and view types. They're for identifying what view is
243 // associated with what template/document type
244 wxDocTemplate(wxDocManager
*manager
,
245 const wxString
& descr
,
246 const wxString
& filter
,
249 const wxString
& docTypeName
,
250 const wxString
& viewTypeName
,
251 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
252 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
253 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
257 // By default, these two member functions dynamically creates document and
258 // view using dynamic instance construction. Override these if you need a
259 // different method of construction.
260 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
261 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
263 // Helper method for CreateDocument; also allows you to do your own document
265 virtual bool InitDocument(wxDocument
* doc
, const wxString
& path
, long flags
= 0);
267 wxString
GetDefaultExtension() const { return m_defaultExt
; };
268 wxString
GetDescription() const { return m_description
; }
269 wxString
GetDirectory() const { return m_directory
; };
270 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
271 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
272 wxString
GetFileFilter() const { return m_fileFilter
; };
273 long GetFlags() const { return m_flags
; };
274 virtual wxString
GetViewName() const { return m_viewTypeName
; }
275 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
277 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
278 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
279 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
280 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
281 void SetFlags(long flags
) { m_flags
= flags
; };
283 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
285 wxClassInfo
* GetDocClassInfo() const { return m_docClassInfo
; }
286 wxClassInfo
* GetViewClassInfo() const { return m_viewClassInfo
; }
288 virtual bool FileMatchesTemplate(const wxString
& path
);
292 wxString m_fileFilter
;
293 wxString m_directory
;
294 wxString m_description
;
295 wxString m_defaultExt
;
296 wxString m_docTypeName
;
297 wxString m_viewTypeName
;
298 wxDocManager
* m_documentManager
;
300 // For dynamic creation of appropriate instances.
301 wxClassInfo
* m_docClassInfo
;
302 wxClassInfo
* m_viewClassInfo
;
304 // Called by CreateDocument and CreateView to create the actual document/view object.
305 // By default uses the ClassInfo provided to the constructor. Override these functions
306 // to provide a different method of creation.
307 virtual wxDocument
*DoCreateDocument();
308 virtual wxView
*DoCreateView();
311 DECLARE_CLASS(wxDocTemplate
)
312 DECLARE_NO_COPY_CLASS(wxDocTemplate
)
315 // One object of this class may be created in an application, to manage all
316 // the templates and documents.
317 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
320 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= true);
323 virtual bool Initialize();
325 // Handlers for common user commands
326 void OnFileClose(wxCommandEvent
& event
);
327 void OnFileCloseAll(wxCommandEvent
& event
);
328 void OnFileNew(wxCommandEvent
& event
);
329 void OnFileOpen(wxCommandEvent
& event
);
330 void OnFileRevert(wxCommandEvent
& event
);
331 void OnFileSave(wxCommandEvent
& event
);
332 void OnFileSaveAs(wxCommandEvent
& event
);
333 void OnPrint(wxCommandEvent
& event
);
334 void OnPrintSetup(wxCommandEvent
& event
);
335 void OnPreview(wxCommandEvent
& event
);
336 void OnUndo(wxCommandEvent
& event
);
337 void OnRedo(wxCommandEvent
& event
);
339 // Handlers for UI update commands
340 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
341 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
342 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
343 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
344 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
345 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
346 void OnUpdateUndo(wxUpdateUIEvent
& event
);
347 void OnUpdateRedo(wxUpdateUIEvent
& event
);
349 void OnUpdatePrint(wxUpdateUIEvent
& event
);
350 void OnUpdatePrintSetup(wxUpdateUIEvent
& event
);
351 void OnUpdatePreview(wxUpdateUIEvent
& event
);
353 // Extend event processing to search the view's event table
354 virtual bool ProcessEvent(wxEvent
& event
);
356 // called when file format detection didn't work, can be overridden to do
357 // something in this case
358 virtual void OnOpenFileFailure() { }
360 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
361 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
362 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
363 virtual bool FlushDoc(wxDocument
*doc
);
364 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
365 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
366 int noTemplates
, wxString
& path
, long flags
, bool save
= false);
367 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
368 int noTemplates
, bool sort
= false);
369 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
370 int noTemplates
, bool sort
= false);
371 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
373 void AssociateTemplate(wxDocTemplate
*temp
);
374 void DisassociateTemplate(wxDocTemplate
*temp
);
376 wxDocument
*GetCurrentDocument() const;
378 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
379 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
381 // Add and remove a document from the manager's list
382 void AddDocument(wxDocument
*doc
);
383 void RemoveDocument(wxDocument
*doc
);
385 // closes all currently open documents
386 bool CloseDocuments(bool force
= true);
388 // closes the specified document
389 bool CloseDocument(wxDocument
* doc
, bool force
= false);
391 // Clear remaining documents and templates
392 bool Clear(bool force
= true);
394 // Views or windows should inform the document manager
395 // when a view is going in or out of focus
396 virtual void ActivateView(wxView
*view
, bool activate
= true);
397 virtual wxView
*GetCurrentView() const;
399 wxList
& GetDocuments() { return m_docs
; }
400 wxList
& GetTemplates() { return m_templates
; }
402 // Make a default document name
403 virtual bool MakeDefaultName(wxString
& buf
);
405 // Make a frame title (override this to do something different)
406 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
408 virtual wxFileHistory
*OnCreateFileHistory();
409 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
411 // File history management
412 virtual void AddFileToHistory(const wxString
& file
);
413 virtual void RemoveFileFromHistory(size_t i
);
414 virtual size_t GetHistoryFilesCount() const;
415 virtual wxString
GetHistoryFile(size_t i
) const;
416 virtual void FileHistoryUseMenu(wxMenu
*menu
);
417 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
419 virtual void FileHistoryLoad(wxConfigBase
& config
);
420 virtual void FileHistorySave(wxConfigBase
& config
);
421 #endif // wxUSE_CONFIG
423 virtual void FileHistoryAddFilesToMenu();
424 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
426 wxString
GetLastDirectory() const { return m_lastDirectory
; }
427 void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
429 // Get the current document manager
430 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
432 // deprecated, use GetHistoryFilesCount() instead
433 wxDEPRECATED( size_t GetNoHistoryFiles() const );
437 int m_defaultDocumentNameCounter
;
441 wxView
* m_currentView
;
442 wxFileHistory
* m_fileHistory
;
443 wxString m_lastDirectory
;
444 static wxDocManager
* sm_docManager
;
446 DECLARE_EVENT_TABLE()
447 DECLARE_DYNAMIC_CLASS(wxDocManager
)
448 DECLARE_NO_COPY_CLASS(wxDocManager
)
451 inline size_t wxDocManager::GetNoHistoryFiles() const
453 return GetHistoryFilesCount();
456 // ----------------------------------------------------------------------------
457 // A default child frame
458 // ----------------------------------------------------------------------------
460 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
463 wxDocChildFrame(wxDocument
*doc
,
467 const wxString
& title
,
468 const wxPoint
& pos
= wxDefaultPosition
,
469 const wxSize
& size
= wxDefaultSize
,
470 long type
= wxDEFAULT_FRAME_STYLE
,
471 const wxString
& name
= wxT("frame"));
474 // Extend event processing to search the view's event table
475 virtual bool ProcessEvent(wxEvent
& event
);
477 void OnActivate(wxActivateEvent
& event
);
478 void OnCloseWindow(wxCloseEvent
& event
);
480 wxDocument
*GetDocument() const { return m_childDocument
; }
481 wxView
*GetView() const { return m_childView
; }
482 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
483 void SetView(wxView
*view
) { m_childView
= view
; }
484 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
487 wxDocument
* m_childDocument
;
491 DECLARE_CLASS(wxDocChildFrame
)
492 DECLARE_EVENT_TABLE()
493 DECLARE_NO_COPY_CLASS(wxDocChildFrame
)
496 // ----------------------------------------------------------------------------
497 // A default parent frame
498 // ----------------------------------------------------------------------------
500 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
503 wxDocParentFrame(wxDocManager
*manager
,
506 const wxString
& title
,
507 const wxPoint
& pos
= wxDefaultPosition
,
508 const wxSize
& size
= wxDefaultSize
,
509 long type
= wxDEFAULT_FRAME_STYLE
,
510 const wxString
& name
= wxT("frame"));
512 // Extend event processing to search the document manager's event table
513 virtual bool ProcessEvent(wxEvent
& event
);
515 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
517 void OnExit(wxCommandEvent
& event
);
518 void OnMRUFile(wxCommandEvent
& event
);
519 void OnCloseWindow(wxCloseEvent
& event
);
522 wxDocManager
*m_docManager
;
525 DECLARE_CLASS(wxDocParentFrame
)
526 DECLARE_EVENT_TABLE()
527 DECLARE_NO_COPY_CLASS(wxDocParentFrame
)
530 // ----------------------------------------------------------------------------
531 // Provide simple default printing facilities
532 // ----------------------------------------------------------------------------
534 #if wxUSE_PRINTING_ARCHITECTURE
535 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
538 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= wxT("Printout"));
539 bool OnPrintPage(int page
);
540 bool HasPage(int page
);
541 bool OnBeginDocument(int startPage
, int endPage
);
542 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
544 virtual wxView
*GetView() { return m_printoutView
; }
547 wxView
* m_printoutView
;
550 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
551 DECLARE_NO_COPY_CLASS(wxDocPrintout
)
553 #endif // wxUSE_PRINTING_ARCHITECTURE
555 // ----------------------------------------------------------------------------
556 // File history management
557 // ----------------------------------------------------------------------------
559 class WXDLLEXPORT wxFileHistory
: public wxObject
562 wxFileHistory(size_t maxFiles
= 9, wxWindowID idBase
= wxID_FILE1
);
566 virtual void AddFileToHistory(const wxString
& file
);
567 virtual void RemoveFileFromHistory(size_t i
);
568 virtual int GetMaxFiles() const { return m_fileMaxFiles
; }
569 virtual void UseMenu(wxMenu
*menu
);
571 // Remove menu from the list (MDI child may be closing)
572 virtual void RemoveMenu(wxMenu
*menu
);
575 virtual void Load(wxConfigBase
& config
);
576 virtual void Save(wxConfigBase
& config
);
577 #endif // wxUSE_CONFIG
579 virtual void AddFilesToMenu();
580 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
583 virtual wxString
GetHistoryFile(size_t i
) const;
584 virtual size_t GetCount() const { return m_fileHistoryN
; }
586 const wxList
& GetMenus() const { return m_fileMenus
; }
588 // deprecated, use GetCount() instead
589 wxDEPRECATED( size_t GetNoHistoryFiles() const );
593 wxChar
** m_fileHistory
;
594 // Number of files saved
595 size_t m_fileHistoryN
;
596 // Menus to maintain (may need several for an MDI app)
598 // Max files to maintain
599 size_t m_fileMaxFiles
;
602 // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
605 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
606 DECLARE_NO_COPY_CLASS(wxFileHistory
)
609 inline size_t wxFileHistory::GetNoHistoryFiles() const
611 return m_fileHistoryN
;
614 #if wxUSE_STD_IOSTREAM
615 // For compatibility with existing file formats:
616 // converts from/to a stream to/from a temporary file.
617 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
618 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
620 // For compatibility with existing file formats:
621 // converts from/to a stream to/from a temporary file.
622 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
623 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);
624 #endif // wxUSE_STD_IOSTREAM
626 #endif // wxUSE_DOC_VIEW_ARCHITECTURE