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
;
166 DECLARE_ABSTRACT_CLASS(wxDocument
)
167 DECLARE_NO_COPY_CLASS(wxDocument
)
170 class WXDLLEXPORT wxView
: public wxEvtHandler
173 // wxView(wxDocument *doc = (wxDocument *) NULL);
177 wxDocument
*GetDocument() const { return m_viewDocument
; }
178 virtual void SetDocument(wxDocument
*doc
);
180 wxString
GetViewName() const { return m_viewTypeName
; }
181 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
183 wxWindow
*GetFrame() const { return m_viewFrame
; }
184 void SetFrame(wxWindow
*frame
) { m_viewFrame
= frame
; }
186 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
187 virtual void OnDraw(wxDC
*dc
) = 0;
188 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
189 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
190 virtual void OnClosingDocument() {};
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 // 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 wxWindow
* m_viewFrame
;
225 DECLARE_ABSTRACT_CLASS(wxView
)
226 DECLARE_NO_COPY_CLASS(wxView
)
229 // Represents user interface (and other) properties of documents and views
230 class WXDLLEXPORT wxDocTemplate
: public wxObject
233 friend class WXDLLEXPORT wxDocManager
;
236 // Associate document and view types. They're for identifying what view is
237 // associated with what template/document type
238 wxDocTemplate(wxDocManager
*manager
,
239 const wxString
& descr
,
240 const wxString
& filter
,
243 const wxString
& docTypeName
,
244 const wxString
& viewTypeName
,
245 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
246 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
247 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
251 // By default, these two member functions dynamically creates document and
252 // view using dynamic instance construction. Override these if you need a
253 // different method of construction.
254 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
255 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
257 wxString
GetDefaultExtension() const { return m_defaultExt
; };
258 wxString
GetDescription() const { return m_description
; }
259 wxString
GetDirectory() const { return m_directory
; };
260 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
261 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
262 wxString
GetFileFilter() const { return m_fileFilter
; };
263 long GetFlags() const { return m_flags
; };
264 virtual wxString
GetViewName() const { return m_viewTypeName
; }
265 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
267 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
268 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
269 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
270 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
271 void SetFlags(long flags
) { m_flags
= flags
; };
273 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
275 virtual bool FileMatchesTemplate(const wxString
& path
);
279 wxString m_fileFilter
;
280 wxString m_directory
;
281 wxString m_description
;
282 wxString m_defaultExt
;
283 wxString m_docTypeName
;
284 wxString m_viewTypeName
;
285 wxDocManager
* m_documentManager
;
287 // For dynamic creation of appropriate instances.
288 wxClassInfo
* m_docClassInfo
;
289 wxClassInfo
* m_viewClassInfo
;
292 DECLARE_CLASS(wxDocTemplate
)
293 DECLARE_NO_COPY_CLASS(wxDocTemplate
)
296 // One object of this class may be created in an application, to manage all
297 // the templates and documents.
298 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
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 OnFileCloseAll(wxCommandEvent
& event
);
309 void OnFileNew(wxCommandEvent
& event
);
310 void OnFileOpen(wxCommandEvent
& event
);
311 void OnFileRevert(wxCommandEvent
& event
);
312 void OnFileSave(wxCommandEvent
& event
);
313 void OnFileSaveAs(wxCommandEvent
& event
);
314 void OnPrint(wxCommandEvent
& event
);
315 void OnPrintSetup(wxCommandEvent
& event
);
316 void OnPreview(wxCommandEvent
& event
);
317 void OnUndo(wxCommandEvent
& event
);
318 void OnRedo(wxCommandEvent
& event
);
320 // Handlers for UI update commands
321 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
322 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
323 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
324 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
325 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
326 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
327 void OnUpdateUndo(wxUpdateUIEvent
& event
);
328 void OnUpdateRedo(wxUpdateUIEvent
& event
);
330 void OnUpdatePrint(wxUpdateUIEvent
& event
);
331 void OnUpdatePrintSetup(wxUpdateUIEvent
& event
);
332 void OnUpdatePreview(wxUpdateUIEvent
& event
);
334 // Extend event processing to search the view's event table
335 virtual bool ProcessEvent(wxEvent
& event
);
337 // called when file format detection didn't work, can be overridden to do
338 // something in this case
339 virtual void OnOpenFileFailure() { }
341 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
342 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
343 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
344 virtual bool FlushDoc(wxDocument
*doc
);
345 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
346 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
347 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
348 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
349 int noTemplates
, bool sort
= FALSE
);
350 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
351 int noTemplates
, bool sort
= FALSE
);
352 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
354 void AssociateTemplate(wxDocTemplate
*temp
);
355 void DisassociateTemplate(wxDocTemplate
*temp
);
357 wxDocument
*GetCurrentDocument() const;
359 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
360 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
362 // Add and remove a document from the manager's list
363 void AddDocument(wxDocument
*doc
);
364 void RemoveDocument(wxDocument
*doc
);
366 // closes all currently open documents
367 bool CloseDocuments(bool force
= TRUE
);
369 // closes the specified document
370 bool CloseDocument(wxDocument
* doc
, bool force
= FALSE
);
372 // Clear remaining documents and templates
373 bool Clear(bool force
= TRUE
);
375 // Views or windows should inform the document manager
376 // when a view is going in or out of focus
377 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
);
378 virtual wxView
*GetCurrentView() const;
380 wxList
& GetDocuments() { return m_docs
; }
381 wxList
& GetTemplates() { return m_templates
; }
383 // Make a default document name
384 virtual bool MakeDefaultName(wxString
& buf
);
386 // Make a frame title (override this to do something different)
387 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
389 virtual wxFileHistory
*OnCreateFileHistory();
390 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
392 // File history management
393 virtual void AddFileToHistory(const wxString
& file
);
394 virtual void RemoveFileFromHistory(size_t i
);
395 virtual size_t GetHistoryFilesCount() const;
396 virtual wxString
GetHistoryFile(size_t i
) const;
397 virtual void FileHistoryUseMenu(wxMenu
*menu
);
398 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
400 virtual void FileHistoryLoad(wxConfigBase
& config
);
401 virtual void FileHistorySave(wxConfigBase
& config
);
402 #endif // wxUSE_CONFIG
404 virtual void FileHistoryAddFilesToMenu();
405 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
407 wxString
GetLastDirectory() const { return m_lastDirectory
; }
408 void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
410 // Get the current document manager
411 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
413 // deprecated, use GetHistoryFilesCount() instead
414 wxDEPRECATED( size_t GetNoHistoryFiles() const );
418 int m_defaultDocumentNameCounter
;
422 wxView
* m_currentView
;
423 wxFileHistory
* m_fileHistory
;
424 wxString m_lastDirectory
;
425 static wxDocManager
* sm_docManager
;
427 DECLARE_EVENT_TABLE()
428 DECLARE_DYNAMIC_CLASS(wxDocManager
)
429 DECLARE_NO_COPY_CLASS(wxDocManager
)
432 inline size_t wxDocManager::GetNoHistoryFiles() const
434 return GetHistoryFilesCount();
437 // ----------------------------------------------------------------------------
438 // A default child frame
439 // ----------------------------------------------------------------------------
441 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
444 wxDocChildFrame(wxDocument
*doc
,
448 const wxString
& title
,
449 const wxPoint
& pos
= wxDefaultPosition
,
450 const wxSize
& size
= wxDefaultSize
,
451 long type
= wxDEFAULT_FRAME_STYLE
,
452 const wxString
& name
= wxT("frame"));
455 // Extend event processing to search the view's event table
456 virtual bool ProcessEvent(wxEvent
& event
);
458 void OnActivate(wxActivateEvent
& event
);
459 void OnCloseWindow(wxCloseEvent
& event
);
461 wxDocument
*GetDocument() const { return m_childDocument
; }
462 wxView
*GetView() const { return m_childView
; }
463 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
464 void SetView(wxView
*view
) { m_childView
= view
; }
465 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
468 wxDocument
* m_childDocument
;
472 DECLARE_CLASS(wxDocChildFrame
)
473 DECLARE_EVENT_TABLE()
474 DECLARE_NO_COPY_CLASS(wxDocChildFrame
)
477 // ----------------------------------------------------------------------------
478 // A default parent frame
479 // ----------------------------------------------------------------------------
481 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
484 wxDocParentFrame(wxDocManager
*manager
,
487 const wxString
& title
,
488 const wxPoint
& pos
= wxDefaultPosition
,
489 const wxSize
& size
= wxDefaultSize
,
490 long type
= wxDEFAULT_FRAME_STYLE
,
491 const wxString
& name
= wxT("frame"));
493 // Extend event processing to search the document manager's event table
494 virtual bool ProcessEvent(wxEvent
& event
);
496 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
498 void OnExit(wxCommandEvent
& event
);
499 void OnMRUFile(wxCommandEvent
& event
);
500 void OnCloseWindow(wxCloseEvent
& event
);
503 wxDocManager
*m_docManager
;
506 DECLARE_CLASS(wxDocParentFrame
)
507 DECLARE_EVENT_TABLE()
508 DECLARE_NO_COPY_CLASS(wxDocParentFrame
)
511 // ----------------------------------------------------------------------------
512 // Provide simple default printing facilities
513 // ----------------------------------------------------------------------------
515 #if wxUSE_PRINTING_ARCHITECTURE
516 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
519 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= wxT("Printout"));
520 bool OnPrintPage(int page
);
521 bool HasPage(int page
);
522 bool OnBeginDocument(int startPage
, int endPage
);
523 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
525 virtual wxView
*GetView() { return m_printoutView
; }
528 wxView
* m_printoutView
;
531 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
532 DECLARE_NO_COPY_CLASS(wxDocPrintout
)
534 #endif // wxUSE_PRINTING_ARCHITECTURE
536 // ----------------------------------------------------------------------------
537 // File history management
538 // ----------------------------------------------------------------------------
540 class WXDLLEXPORT wxFileHistory
: public wxObject
543 wxFileHistory(size_t maxFiles
= 9, wxWindowID idBase
= wxID_FILE1
);
547 virtual void AddFileToHistory(const wxString
& file
);
548 virtual void RemoveFileFromHistory(size_t i
);
549 virtual int GetMaxFiles() const { return m_fileMaxFiles
; }
550 virtual void UseMenu(wxMenu
*menu
);
552 // Remove menu from the list (MDI child may be closing)
553 virtual void RemoveMenu(wxMenu
*menu
);
556 virtual void Load(wxConfigBase
& config
);
557 virtual void Save(wxConfigBase
& config
);
558 #endif // wxUSE_CONFIG
560 virtual void AddFilesToMenu();
561 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
564 virtual wxString
GetHistoryFile(size_t i
) const;
565 virtual size_t GetCount() const { return m_fileHistoryN
; }
567 const wxList
& GetMenus() const { return m_fileMenus
; }
569 // deprecated, use GetCount() instead
570 wxDEPRECATED( size_t GetNoHistoryFiles() const );
574 wxChar
** m_fileHistory
;
575 // Number of files saved
576 size_t m_fileHistoryN
;
577 // Menus to maintain (may need several for an MDI app)
579 // Max files to maintain
580 size_t m_fileMaxFiles
;
583 // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
586 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
587 DECLARE_NO_COPY_CLASS(wxFileHistory
)
590 inline size_t wxFileHistory::GetNoHistoryFiles() const
592 return m_fileHistoryN
;
595 #if wxUSE_STD_IOSTREAM
596 // For compatibility with existing file formats:
597 // converts from/to a stream to/from a temporary file.
598 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
599 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
601 // For compatibility with existing file formats:
602 // converts from/to a stream to/from a temporary file.
603 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
604 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);
605 #endif // wxUSE_STD_IOSTREAM
607 #endif // wxUSE_DOC_VIEW_ARCHITECTURE