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
);
100 // need this to keep from hiding the virtual from wxObject
101 virtual void LoadObject(wxObjectInputStream
& stream
) { wxObject::LoadObject(stream
); };
104 // Called by wxWindows
105 virtual bool OnSaveDocument(const wxString
& filename
);
106 virtual bool OnOpenDocument(const wxString
& filename
);
107 virtual bool OnNewDocument();
108 virtual bool OnCloseDocument();
110 // Prompts for saving if about to close a modified document. Returns TRUE
111 // if ok to close the document (may have saved in the meantime, or set
112 // modified to FALSE)
113 virtual bool OnSaveModified();
115 // Called by framework if created automatically by the default document
116 // manager: gives document a chance to initialise and (usually) create a
118 virtual bool OnCreate(const wxString
& path
, long flags
);
120 // By default, creates a base wxCommandProcessor.
121 virtual wxCommandProcessor
*OnCreateCommandProcessor();
122 virtual wxCommandProcessor
*GetCommandProcessor() const { return m_commandProcessor
; }
123 virtual void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
125 // Called after a view is added or removed. The default implementation
126 // deletes the document if this is there are no more views.
127 virtual void OnChangedViewList();
129 virtual bool DeleteContents();
131 virtual bool Draw(wxDC
&);
132 virtual bool IsModified() const { return m_documentModified
; }
133 virtual void Modify(bool mod
) { m_documentModified
= mod
; }
135 virtual bool AddView(wxView
*view
);
136 virtual bool RemoveView(wxView
*view
);
137 wxList
& GetViews() const { return (wxList
&) m_documentViews
; }
138 wxView
*GetFirstView() const;
140 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
142 // Remove all views (because we're closing the document)
143 virtual bool DeleteAllViews();
146 virtual wxDocManager
*GetDocumentManager() const;
147 virtual wxDocTemplate
*GetDocumentTemplate() const { return m_documentTemplate
; }
148 virtual void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
150 // Get title, or filename if no title, else [unnamed]
151 virtual bool GetPrintableName(wxString
& buf
) const;
153 // Returns a window that can be used as a parent for document-related
154 // dialogs. Override if necessary.
155 virtual wxWindow
*GetDocumentWindow() const;
158 wxList m_documentViews
;
159 wxString m_documentFile
;
160 wxString m_documentTitle
;
161 wxString m_documentTypeName
;
162 wxDocTemplate
* m_documentTemplate
;
163 bool m_documentModified
;
164 wxDocument
* m_documentParent
;
165 wxCommandProcessor
* m_commandProcessor
;
169 class WXDLLEXPORT wxView
: public wxEvtHandler
171 DECLARE_ABSTRACT_CLASS(wxView
)
174 // wxView(wxDocument *doc = (wxDocument *) NULL);
178 wxDocument
*GetDocument() const { return m_viewDocument
; }
179 void SetDocument(wxDocument
*doc
);
181 wxString
GetViewName() const { return m_viewTypeName
; }
182 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
184 wxFrame
*GetFrame() const { return m_viewFrame
; }
185 void SetFrame(wxFrame
*frame
) { m_viewFrame
= frame
; }
187 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
188 virtual void OnDraw(wxDC
*dc
) = 0;
189 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
190 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
191 virtual void OnChangeFilename();
193 // Called by framework if created automatically by the default document
194 // manager class: gives view a chance to initialise
195 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
197 // Checks if the view is the last one for the document; if so, asks user
198 // to confirm save data (if modified). If ok, deletes itself and returns
200 virtual bool Close(bool deleteWindow
= TRUE
);
202 // Override to do cleanup/veto close
203 virtual bool OnClose(bool deleteWindow
);
205 #if WXWIN_COMPATIBILITY
206 // Defeat compiler warning
207 bool OnClose() { return wxEvtHandler::OnClose(); }
210 // Extend event processing to search the document's event table
211 virtual bool ProcessEvent(wxEvent
& event
);
213 // A view's window can call this to notify the view it is (in)active.
214 // The function then notifies the document manager.
215 virtual void Activate(bool activate
);
217 wxDocManager
*GetDocumentManager() const
218 { return m_viewDocument
->GetDocumentManager(); }
220 #if wxUSE_PRINTING_ARCHITECTURE
221 virtual wxPrintout
*OnCreatePrintout();
225 wxDocument
* m_viewDocument
;
226 wxString m_viewTypeName
;
227 wxFrame
* m_viewFrame
;
230 // Represents user interface (and other) properties of documents and views
231 class WXDLLEXPORT wxDocTemplate
: public wxObject
233 DECLARE_CLASS(wxDocTemplate
)
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 wxString
GetDefaultExtension() const { return m_defaultExt
; };
260 wxString
GetDescription() const { return m_description
; }
261 wxString
GetDirectory() const { return m_directory
; };
262 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
263 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
264 wxString
GetFileFilter() const { return m_fileFilter
; };
265 long GetFlags() const { return m_flags
; };
266 virtual wxString
GetViewName() const { return m_viewTypeName
; }
267 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
269 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
270 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
271 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
272 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
273 void SetFlags(long flags
) { m_flags
= flags
; };
275 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
277 virtual bool FileMatchesTemplate(const wxString
& path
);
281 wxString m_fileFilter
;
282 wxString m_directory
;
283 wxString m_description
;
284 wxString m_defaultExt
;
285 wxString m_docTypeName
;
286 wxString m_viewTypeName
;
287 wxDocManager
* m_documentManager
;
289 // For dynamic creation of appropriate instances.
290 wxClassInfo
* m_docClassInfo
;
291 wxClassInfo
* m_viewClassInfo
;
294 // One object of this class may be created in an application, to manage all
295 // the templates and documents.
296 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
298 DECLARE_DYNAMIC_CLASS(wxDocManager
)
301 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
304 virtual bool Initialize();
306 // Handlers for common user commands
307 void OnFileClose(wxCommandEvent
& event
);
308 void OnFileNew(wxCommandEvent
& event
);
309 void OnFileOpen(wxCommandEvent
& event
);
310 void OnFileRevert(wxCommandEvent
& event
);
311 void OnFileSave(wxCommandEvent
& event
);
312 void OnFileSaveAs(wxCommandEvent
& event
);
313 void OnPrint(wxCommandEvent
& event
);
314 void OnPrintSetup(wxCommandEvent
& event
);
315 void OnPreview(wxCommandEvent
& event
);
316 void OnUndo(wxCommandEvent
& event
);
317 void OnRedo(wxCommandEvent
& event
);
319 // Handlers for UI update commands
320 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
321 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
322 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
323 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
324 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
325 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
326 void OnUpdateUndo(wxUpdateUIEvent
& event
);
327 void OnUpdateRedo(wxUpdateUIEvent
& event
);
329 void OnUpdatePrint(wxUpdateUIEvent
& event
);
330 void OnUpdatePrintSetup(wxUpdateUIEvent
& event
);
331 void OnUpdatePreview(wxUpdateUIEvent
& event
);
333 // Extend event processing to search the view's event table
334 virtual bool ProcessEvent(wxEvent
& event
);
336 // called when file format detection didn't work, can be overridden to do
337 // something in this case
338 // This is of course completely stupid, because if the file dialog is
339 // cancelled you get an assert. Brilliant. -- JACS
340 // virtual void OnOpenFileFailure() { wxFAIL_MSG(_T("file format mismatch")); }
341 virtual void OnOpenFileFailure() { }
343 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
344 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
345 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
346 virtual bool FlushDoc(wxDocument
*doc
);
347 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
348 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
349 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
350 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
351 int noTemplates
, bool sort
= FALSE
);
352 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
353 int noTemplates
, bool sort
= FALSE
);
354 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
356 void AssociateTemplate(wxDocTemplate
*temp
);
357 void DisassociateTemplate(wxDocTemplate
*temp
);
359 wxDocument
*GetCurrentDocument() const;
361 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
362 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
364 // Add and remove a document from the manager's list
365 void AddDocument(wxDocument
*doc
);
366 void RemoveDocument(wxDocument
*doc
);
368 // Clear remaining documents and templates
369 bool Clear(bool force
= TRUE
);
371 // Views or windows should inform the document manager
372 // when a view is going in or out of focus
373 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
374 virtual wxView
*GetCurrentView() const;
376 wxList
& GetDocuments() { return m_docs
; }
377 wxList
& GetTemplates() { return m_templates
; }
379 // Make a default document name
380 virtual bool MakeDefaultName(wxString
& buf
);
382 // Make a frame title (override this to do something different)
383 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
385 virtual wxFileHistory
*OnCreateFileHistory();
386 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
388 // File history management
389 virtual void AddFileToHistory(const wxString
& file
);
390 virtual void RemoveFileFromHistory(int i
);
391 virtual int GetNoHistoryFiles() const;
392 virtual wxString
GetHistoryFile(int i
) const;
393 virtual void FileHistoryUseMenu(wxMenu
*menu
);
394 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
396 virtual void FileHistoryLoad(wxConfigBase
& config
);
397 virtual void FileHistorySave(wxConfigBase
& config
);
398 #endif // wxUSE_CONFIG
400 virtual void FileHistoryAddFilesToMenu();
401 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
403 inline wxString
GetLastDirectory() const { return m_lastDirectory
; }
404 inline void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
406 // Get the current document manager
407 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
411 int m_defaultDocumentNameCounter
;
415 wxView
* m_currentView
;
416 wxFileHistory
* m_fileHistory
;
417 wxString m_lastDirectory
;
418 static wxDocManager
* sm_docManager
;
420 DECLARE_EVENT_TABLE()
423 // ----------------------------------------------------------------------------
424 // A default child frame
425 // ----------------------------------------------------------------------------
427 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
429 DECLARE_CLASS(wxDocChildFrame
)
432 wxDocChildFrame(wxDocument
*doc
,
436 const wxString
& title
,
437 const wxPoint
& pos
= wxDefaultPosition
,
438 const wxSize
& size
= wxDefaultSize
,
439 long type
= wxDEFAULT_FRAME_STYLE
,
440 const wxString
& name
= "frame");
443 // Extend event processing to search the view's event table
444 virtual bool ProcessEvent(wxEvent
& event
);
446 void OnActivate(wxActivateEvent
& event
);
447 void OnCloseWindow(wxCloseEvent
& event
);
449 wxDocument
*GetDocument() const { return m_childDocument
; }
450 wxView
*GetView() const { return m_childView
; }
451 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
452 void SetView(wxView
*view
) { m_childView
= view
; }
453 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
456 wxDocument
* m_childDocument
;
459 DECLARE_EVENT_TABLE()
462 // ----------------------------------------------------------------------------
463 // A default parent frame
464 // ----------------------------------------------------------------------------
466 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
468 DECLARE_CLASS(wxDocParentFrame
)
471 wxDocParentFrame(wxDocManager
*manager
,
474 const wxString
& title
,
475 const wxPoint
& pos
= wxDefaultPosition
,
476 const wxSize
& size
= wxDefaultSize
,
477 long type
= wxDEFAULT_FRAME_STYLE
,
478 const wxString
& name
= "frame");
480 // Extend event processing to search the document manager's event table
481 virtual bool ProcessEvent(wxEvent
& event
);
483 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
485 void OnExit(wxCommandEvent
& event
);
486 void OnMRUFile(wxCommandEvent
& event
);
487 void OnCloseWindow(wxCloseEvent
& event
);
490 wxDocManager
*m_docManager
;
492 DECLARE_EVENT_TABLE()
495 // ----------------------------------------------------------------------------
496 // Provide simple default printing facilities
497 // ----------------------------------------------------------------------------
499 #if wxUSE_PRINTING_ARCHITECTURE
500 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
502 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
505 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= "Printout");
506 bool OnPrintPage(int page
);
507 bool HasPage(int page
);
508 bool OnBeginDocument(int startPage
, int endPage
);
509 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
511 virtual wxView
*GetView() { return m_printoutView
; }
514 wxView
* m_printoutView
;
516 #endif // wxUSE_PRINTING_ARCHITECTURE
518 // ----------------------------------------------------------------------------
519 // File history management
520 // ----------------------------------------------------------------------------
522 class WXDLLEXPORT wxFileHistory
: public wxObject
524 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
527 wxFileHistory(int maxFiles
= 9);
531 virtual void AddFileToHistory(const wxString
& file
);
532 virtual void RemoveFileFromHistory(int i
);
533 virtual int GetMaxFiles() const { return m_fileMaxFiles
; }
534 virtual void UseMenu(wxMenu
*menu
);
536 // Remove menu from the list (MDI child may be closing)
537 virtual void RemoveMenu(wxMenu
*menu
);
540 virtual void Load(wxConfigBase
& config
);
541 virtual void Save(wxConfigBase
& config
);
542 #endif // wxUSE_CONFIG
544 virtual void AddFilesToMenu();
545 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
548 virtual wxString
GetHistoryFile(int i
) const;
550 // A synonym for GetNoHistoryFiles
551 virtual int GetCount() const { return m_fileHistoryN
; }
552 int GetNoHistoryFiles() const { return m_fileHistoryN
; }
554 wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
558 wxChar
** m_fileHistory
;
559 // Number of files saved
561 // Menus to maintain (may need several for an MDI app)
563 // Max files to maintain
567 #if wxUSE_STD_IOSTREAM
568 // For compatibility with existing file formats:
569 // converts from/to a stream to/from a temporary file.
570 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
571 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
573 // For compatibility with existing file formats:
574 // converts from/to a stream to/from a temporary file.
575 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
576 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);