1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Doc/View classes
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "docview.h"
21 #include "wx/cmndata.h"
22 #include "wx/string.h"
25 #if wxUSE_PRINTING_ARCHITECTURE
29 class WXDLLEXPORT wxWindow
;
30 class WXDLLEXPORT wxDocument
;
31 class WXDLLEXPORT wxView
;
32 class WXDLLEXPORT wxDocTemplate
;
33 class WXDLLEXPORT wxDocManager
;
34 class WXDLLEXPORT wxPrintInfo
;
35 class WXDLLEXPORT wxCommandProcessor
;
36 class WXDLLEXPORT wxFileHistory
;
37 class WXDLLEXPORT wxConfigBase
;
39 #if wxUSE_STD_IOSTREAM
40 #include "wx/ioswrap.h"
42 #include "wx/stream.h"
45 // Document manager flags
52 wxDEFAULT_DOCMAN_FLAGS
= wxDOC_SDI
55 // Document template flags
58 wxTEMPLATE_VISIBLE
= 1,
60 wxDEFAULT_TEMPLATE_FLAGS
= wxTEMPLATE_VISIBLE
63 #define wxMAX_FILE_HISTORY 9
65 class WXDLLEXPORT wxDocument
: public wxEvtHandler
67 DECLARE_ABSTRACT_CLASS(wxDocument
)
70 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
74 void SetFilename(const wxString
& filename
, bool notifyViews
= FALSE
);
75 wxString
GetFilename() const { return m_documentFile
; }
77 void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
78 wxString
GetTitle() const { return m_documentTitle
; }
80 void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
81 wxString
GetDocumentName() const { return m_documentTypeName
; }
83 bool GetDocumentSaved() const { return m_savedYet
; }
84 void SetDocumentSaved(bool saved
= TRUE
) { m_savedYet
= saved
; }
88 virtual bool SaveAs();
89 virtual bool Revert();
91 #if wxUSE_STD_IOSTREAM
92 virtual wxSTD ostream
& SaveObject(wxSTD ostream
& stream
);
93 virtual wxSTD istream
& LoadObject(wxSTD istream
& stream
);
95 virtual wxOutputStream
& SaveObject(wxOutputStream
& stream
);
96 virtual wxInputStream
& LoadObject(wxInputStream
& stream
);
99 // Called by wxWindows
100 virtual bool OnSaveDocument(const wxString
& filename
);
101 virtual bool OnOpenDocument(const wxString
& filename
);
102 virtual bool OnNewDocument();
103 virtual bool OnCloseDocument();
105 // Prompts for saving if about to close a modified document. Returns TRUE
106 // if ok to close the document (may have saved in the meantime, or set
107 // modified to FALSE)
108 virtual bool OnSaveModified();
110 // Called by framework if created automatically by the default document
111 // manager: gives document a chance to initialise and (usually) create a
113 virtual bool OnCreate(const wxString
& path
, long flags
);
115 // By default, creates a base wxCommandProcessor.
116 virtual wxCommandProcessor
*OnCreateCommandProcessor();
117 virtual wxCommandProcessor
*GetCommandProcessor() const { return m_commandProcessor
; }
118 virtual void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
120 // Called after a view is added or removed. The default implementation
121 // deletes the document if this is there are no more views.
122 virtual void OnChangedViewList();
124 virtual bool DeleteContents();
126 virtual bool Draw(wxDC
&);
127 virtual bool IsModified() const { return m_documentModified
; }
128 virtual void Modify(bool mod
) { m_documentModified
= mod
; }
130 virtual bool AddView(wxView
*view
);
131 virtual bool RemoveView(wxView
*view
);
132 wxList
& GetViews() const { return (wxList
&) m_documentViews
; }
133 wxView
*GetFirstView() const;
135 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
137 // Remove all views (because we're closing the document)
138 virtual bool DeleteAllViews();
141 virtual wxDocManager
*GetDocumentManager() const;
142 virtual wxDocTemplate
*GetDocumentTemplate() const { return m_documentTemplate
; }
143 virtual void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
145 // Get title, or filename if no title, else [unnamed]
146 virtual bool GetPrintableName(wxString
& buf
) const;
148 // Returns a window that can be used as a parent for document-related
149 // dialogs. Override if necessary.
150 virtual wxWindow
*GetDocumentWindow() const;
153 wxList m_documentViews
;
154 wxString m_documentFile
;
155 wxString m_documentTitle
;
156 wxString m_documentTypeName
;
157 wxDocTemplate
* m_documentTemplate
;
158 bool m_documentModified
;
159 wxDocument
* m_documentParent
;
160 wxCommandProcessor
* m_commandProcessor
;
164 class WXDLLEXPORT wxView
: public wxEvtHandler
166 DECLARE_ABSTRACT_CLASS(wxView
)
169 // wxView(wxDocument *doc = (wxDocument *) NULL);
173 wxDocument
*GetDocument() const { return m_viewDocument
; }
174 virtual void SetDocument(wxDocument
*doc
);
176 wxString
GetViewName() const { return m_viewTypeName
; }
177 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
179 wxFrame
*GetFrame() const { return m_viewFrame
; }
180 void SetFrame(wxFrame
*frame
) { m_viewFrame
= frame
; }
182 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
183 virtual void OnDraw(wxDC
*dc
) = 0;
184 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
185 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
186 virtual void OnChangeFilename();
188 // Called by framework if created automatically by the default document
189 // manager class: gives view a chance to initialise
190 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
192 // Checks if the view is the last one for the document; if so, asks user
193 // to confirm save data (if modified). If ok, deletes itself and returns
195 virtual bool Close(bool deleteWindow
= TRUE
);
197 // Override to do cleanup/veto close
198 virtual bool OnClose(bool deleteWindow
);
200 #if WXWIN_COMPATIBILITY
201 // Defeat compiler warning
202 bool OnClose() { return wxEvtHandler::OnClose(); }
205 // Extend event processing to search the document's event table
206 virtual bool ProcessEvent(wxEvent
& event
);
208 // A view's window can call this to notify the view it is (in)active.
209 // The function then notifies the document manager.
210 virtual void Activate(bool activate
);
212 wxDocManager
*GetDocumentManager() const
213 { return m_viewDocument
->GetDocumentManager(); }
215 #if wxUSE_PRINTING_ARCHITECTURE
216 virtual wxPrintout
*OnCreatePrintout();
220 wxDocument
* m_viewDocument
;
221 wxString m_viewTypeName
;
222 wxFrame
* m_viewFrame
;
225 // Represents user interface (and other) properties of documents and views
226 class WXDLLEXPORT wxDocTemplate
: public wxObject
228 DECLARE_CLASS(wxDocTemplate
)
230 friend class WXDLLEXPORT wxDocManager
;
233 // Associate document and view types. They're for identifying what view is
234 // associated with what template/document type
235 wxDocTemplate(wxDocManager
*manager
,
236 const wxString
& descr
,
237 const wxString
& filter
,
240 const wxString
& docTypeName
,
241 const wxString
& viewTypeName
,
242 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
243 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
244 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
248 // By default, these two member functions dynamically creates document and
249 // view using dynamic instance construction. Override these if you need a
250 // different method of construction.
251 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
252 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
254 wxString
GetDefaultExtension() const { return m_defaultExt
; };
255 wxString
GetDescription() const { return m_description
; }
256 wxString
GetDirectory() const { return m_directory
; };
257 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
258 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
259 wxString
GetFileFilter() const { return m_fileFilter
; };
260 long GetFlags() const { return m_flags
; };
261 virtual wxString
GetViewName() const { return m_viewTypeName
; }
262 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
264 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
265 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
266 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
267 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
268 void SetFlags(long flags
) { m_flags
= flags
; };
270 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
272 virtual bool FileMatchesTemplate(const wxString
& path
);
276 wxString m_fileFilter
;
277 wxString m_directory
;
278 wxString m_description
;
279 wxString m_defaultExt
;
280 wxString m_docTypeName
;
281 wxString m_viewTypeName
;
282 wxDocManager
* m_documentManager
;
284 // For dynamic creation of appropriate instances.
285 wxClassInfo
* m_docClassInfo
;
286 wxClassInfo
* m_viewClassInfo
;
289 // One object of this class may be created in an application, to manage all
290 // the templates and documents.
291 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
293 DECLARE_DYNAMIC_CLASS(wxDocManager
)
296 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
299 virtual bool Initialize();
301 // Handlers for common user commands
302 void OnFileClose(wxCommandEvent
& event
);
303 void OnFileCloseAll(wxCommandEvent
& event
);
304 void OnFileNew(wxCommandEvent
& event
);
305 void OnFileOpen(wxCommandEvent
& event
);
306 void OnFileRevert(wxCommandEvent
& event
);
307 void OnFileSave(wxCommandEvent
& event
);
308 void OnFileSaveAs(wxCommandEvent
& event
);
309 void OnPrint(wxCommandEvent
& event
);
310 void OnPrintSetup(wxCommandEvent
& event
);
311 void OnPreview(wxCommandEvent
& event
);
312 void OnUndo(wxCommandEvent
& event
);
313 void OnRedo(wxCommandEvent
& event
);
315 // Handlers for UI update commands
316 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
317 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
318 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
319 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
320 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
321 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
322 void OnUpdateUndo(wxUpdateUIEvent
& event
);
323 void OnUpdateRedo(wxUpdateUIEvent
& event
);
325 void OnUpdatePrint(wxUpdateUIEvent
& event
);
326 void OnUpdatePrintSetup(wxUpdateUIEvent
& event
);
327 void OnUpdatePreview(wxUpdateUIEvent
& event
);
329 // Extend event processing to search the view's event table
330 virtual bool ProcessEvent(wxEvent
& event
);
332 // called when file format detection didn't work, can be overridden to do
333 // something in this case
334 // This is of course completely stupid, because if the file dialog is
335 // cancelled you get an assert. Brilliant. -- JACS
336 // virtual void OnOpenFileFailure() { wxFAIL_MSG(_T("file format mismatch")); }
337 virtual void OnOpenFileFailure() { }
339 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
340 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
341 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
342 virtual bool FlushDoc(wxDocument
*doc
);
343 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
344 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
345 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
346 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
347 int noTemplates
, bool sort
= FALSE
);
348 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
349 int noTemplates
, bool sort
= FALSE
);
350 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
352 void AssociateTemplate(wxDocTemplate
*temp
);
353 void DisassociateTemplate(wxDocTemplate
*temp
);
355 wxDocument
*GetCurrentDocument() const;
357 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
358 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
360 // Add and remove a document from the manager's list
361 void AddDocument(wxDocument
*doc
);
362 void RemoveDocument(wxDocument
*doc
);
364 // closes all currently open documents
365 bool CloseDocuments(bool force
= TRUE
);
367 // Clear remaining documents and templates
368 bool Clear(bool force
= TRUE
);
370 // Views or windows should inform the document manager
371 // when a view is going in or out of focus
372 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
373 virtual wxView
*GetCurrentView() const;
375 wxList
& GetDocuments() { return m_docs
; }
376 wxList
& GetTemplates() { return m_templates
; }
378 // Make a default document name
379 virtual bool MakeDefaultName(wxString
& buf
);
381 // Make a frame title (override this to do something different)
382 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
384 virtual wxFileHistory
*OnCreateFileHistory();
385 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
387 // File history management
388 virtual void AddFileToHistory(const wxString
& file
);
389 virtual void RemoveFileFromHistory(int i
);
390 virtual int GetNoHistoryFiles() const;
391 virtual wxString
GetHistoryFile(int i
) const;
392 virtual void FileHistoryUseMenu(wxMenu
*menu
);
393 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
395 virtual void FileHistoryLoad(wxConfigBase
& config
);
396 virtual void FileHistorySave(wxConfigBase
& config
);
397 #endif // wxUSE_CONFIG
399 virtual void FileHistoryAddFilesToMenu();
400 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
402 inline wxString
GetLastDirectory() const { return m_lastDirectory
; }
403 inline void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
405 // Get the current document manager
406 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
410 int m_defaultDocumentNameCounter
;
414 wxView
* m_currentView
;
415 wxFileHistory
* m_fileHistory
;
416 wxString m_lastDirectory
;
417 static wxDocManager
* sm_docManager
;
419 DECLARE_EVENT_TABLE()
422 // ----------------------------------------------------------------------------
423 // A default child frame
424 // ----------------------------------------------------------------------------
426 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
428 DECLARE_CLASS(wxDocChildFrame
)
431 wxDocChildFrame(wxDocument
*doc
,
435 const wxString
& title
,
436 const wxPoint
& pos
= wxDefaultPosition
,
437 const wxSize
& size
= wxDefaultSize
,
438 long type
= wxDEFAULT_FRAME_STYLE
,
439 const wxString
& name
= "frame");
442 // Extend event processing to search the view's event table
443 virtual bool ProcessEvent(wxEvent
& event
);
445 void OnActivate(wxActivateEvent
& event
);
446 void OnCloseWindow(wxCloseEvent
& event
);
448 wxDocument
*GetDocument() const { return m_childDocument
; }
449 wxView
*GetView() const { return m_childView
; }
450 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
451 void SetView(wxView
*view
) { m_childView
= view
; }
452 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
455 wxDocument
* m_childDocument
;
458 DECLARE_EVENT_TABLE()
461 // ----------------------------------------------------------------------------
462 // A default parent frame
463 // ----------------------------------------------------------------------------
465 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
467 DECLARE_CLASS(wxDocParentFrame
)
470 wxDocParentFrame(wxDocManager
*manager
,
473 const wxString
& title
,
474 const wxPoint
& pos
= wxDefaultPosition
,
475 const wxSize
& size
= wxDefaultSize
,
476 long type
= wxDEFAULT_FRAME_STYLE
,
477 const wxString
& name
= "frame");
479 // Extend event processing to search the document manager's event table
480 virtual bool ProcessEvent(wxEvent
& event
);
482 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
484 void OnExit(wxCommandEvent
& event
);
485 void OnMRUFile(wxCommandEvent
& event
);
486 void OnCloseWindow(wxCloseEvent
& event
);
489 wxDocManager
*m_docManager
;
491 DECLARE_EVENT_TABLE()
494 // ----------------------------------------------------------------------------
495 // Provide simple default printing facilities
496 // ----------------------------------------------------------------------------
498 #if wxUSE_PRINTING_ARCHITECTURE
499 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
501 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
504 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= "Printout");
505 bool OnPrintPage(int page
);
506 bool HasPage(int page
);
507 bool OnBeginDocument(int startPage
, int endPage
);
508 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
510 virtual wxView
*GetView() { return m_printoutView
; }
513 wxView
* m_printoutView
;
515 #endif // wxUSE_PRINTING_ARCHITECTURE
517 // ----------------------------------------------------------------------------
518 // File history management
519 // ----------------------------------------------------------------------------
521 class WXDLLEXPORT wxFileHistory
: public wxObject
523 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
526 wxFileHistory(int maxFiles
= 9);
530 virtual void AddFileToHistory(const wxString
& file
);
531 virtual void RemoveFileFromHistory(int i
);
532 virtual int GetMaxFiles() const { return m_fileMaxFiles
; }
533 virtual void UseMenu(wxMenu
*menu
);
535 // Remove menu from the list (MDI child may be closing)
536 virtual void RemoveMenu(wxMenu
*menu
);
539 virtual void Load(wxConfigBase
& config
);
540 virtual void Save(wxConfigBase
& config
);
541 #endif // wxUSE_CONFIG
543 virtual void AddFilesToMenu();
544 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
547 virtual wxString
GetHistoryFile(int i
) const;
549 // A synonym for GetNoHistoryFiles
550 virtual int GetCount() const { return m_fileHistoryN
; }
551 int GetNoHistoryFiles() const { return m_fileHistoryN
; }
553 wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
557 wxChar
** m_fileHistory
;
558 // Number of files saved
560 // Menus to maintain (may need several for an MDI app)
562 // Max files to maintain
566 #if wxUSE_STD_IOSTREAM
567 // For compatibility with existing file formats:
568 // converts from/to a stream to/from a temporary file.
569 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
570 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
572 // For compatibility with existing file formats:
573 // converts from/to a stream to/from a temporary file.
574 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
575 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);