1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Doc/View classes
4 // Author: Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(__APPLE__)
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 wxWindows
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 #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 wxWindow
* m_viewFrame
;
230 DECLARE_ABSTRACT_CLASS(wxView
)
231 DECLARE_NO_COPY_CLASS(wxView
)
234 // Represents user interface (and other) properties of documents and views
235 class WXDLLEXPORT wxDocTemplate
: public wxObject
238 friend class WXDLLEXPORT wxDocManager
;
241 // Associate document and view types. They're for identifying what view is
242 // associated with what template/document type
243 wxDocTemplate(wxDocManager
*manager
,
244 const wxString
& descr
,
245 const wxString
& filter
,
248 const wxString
& docTypeName
,
249 const wxString
& viewTypeName
,
250 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
251 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
252 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
256 // By default, these two member functions dynamically creates document and
257 // view using dynamic instance construction. Override these if you need a
258 // different method of construction.
259 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
260 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
262 wxString
GetDefaultExtension() const { return m_defaultExt
; };
263 wxString
GetDescription() const { return m_description
; }
264 wxString
GetDirectory() const { return m_directory
; };
265 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
266 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
267 wxString
GetFileFilter() const { return m_fileFilter
; };
268 long GetFlags() const { return m_flags
; };
269 virtual wxString
GetViewName() const { return m_viewTypeName
; }
270 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
272 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
273 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
274 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
275 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
276 void SetFlags(long flags
) { m_flags
= flags
; };
278 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
280 virtual bool FileMatchesTemplate(const wxString
& path
);
284 wxString m_fileFilter
;
285 wxString m_directory
;
286 wxString m_description
;
287 wxString m_defaultExt
;
288 wxString m_docTypeName
;
289 wxString m_viewTypeName
;
290 wxDocManager
* m_documentManager
;
292 // For dynamic creation of appropriate instances.
293 wxClassInfo
* m_docClassInfo
;
294 wxClassInfo
* m_viewClassInfo
;
297 DECLARE_CLASS(wxDocTemplate
)
298 DECLARE_NO_COPY_CLASS(wxDocTemplate
)
301 // One object of this class may be created in an application, to manage all
302 // the templates and documents.
303 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
305 DECLARE_DYNAMIC_CLASS(wxDocManager
)
308 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
311 virtual bool Initialize();
313 // Handlers for common user commands
314 void OnFileClose(wxCommandEvent
& event
);
315 void OnFileCloseAll(wxCommandEvent
& event
);
316 void OnFileNew(wxCommandEvent
& event
);
317 void OnFileOpen(wxCommandEvent
& event
);
318 void OnFileRevert(wxCommandEvent
& event
);
319 void OnFileSave(wxCommandEvent
& event
);
320 void OnFileSaveAs(wxCommandEvent
& event
);
321 void OnPrint(wxCommandEvent
& event
);
322 void OnPrintSetup(wxCommandEvent
& event
);
323 void OnPreview(wxCommandEvent
& event
);
324 void OnUndo(wxCommandEvent
& event
);
325 void OnRedo(wxCommandEvent
& event
);
327 // Handlers for UI update commands
328 void OnUpdateFileOpen(wxUpdateUIEvent
& event
);
329 void OnUpdateFileClose(wxUpdateUIEvent
& event
);
330 void OnUpdateFileRevert(wxUpdateUIEvent
& event
);
331 void OnUpdateFileNew(wxUpdateUIEvent
& event
);
332 void OnUpdateFileSave(wxUpdateUIEvent
& event
);
333 void OnUpdateFileSaveAs(wxUpdateUIEvent
& event
);
334 void OnUpdateUndo(wxUpdateUIEvent
& event
);
335 void OnUpdateRedo(wxUpdateUIEvent
& event
);
337 void OnUpdatePrint(wxUpdateUIEvent
& event
);
338 void OnUpdatePrintSetup(wxUpdateUIEvent
& event
);
339 void OnUpdatePreview(wxUpdateUIEvent
& event
);
341 // Extend event processing to search the view's event table
342 virtual bool ProcessEvent(wxEvent
& event
);
344 // called when file format detection didn't work, can be overridden to do
345 // something in this case
346 virtual void OnOpenFileFailure() { }
348 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
349 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
350 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
351 virtual bool FlushDoc(wxDocument
*doc
);
352 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
353 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
354 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
355 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
356 int noTemplates
, bool sort
= FALSE
);
357 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
358 int noTemplates
, bool sort
= FALSE
);
359 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
361 void AssociateTemplate(wxDocTemplate
*temp
);
362 void DisassociateTemplate(wxDocTemplate
*temp
);
364 wxDocument
*GetCurrentDocument() const;
366 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
367 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
369 // Add and remove a document from the manager's list
370 void AddDocument(wxDocument
*doc
);
371 void RemoveDocument(wxDocument
*doc
);
373 // closes all currently open documents
374 bool CloseDocuments(bool force
= TRUE
);
376 // Clear remaining documents and templates
377 bool Clear(bool force
= TRUE
);
379 // Views or windows should inform the document manager
380 // when a view is going in or out of focus
381 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
382 virtual wxView
*GetCurrentView() const;
384 wxList
& GetDocuments() { return m_docs
; }
385 wxList
& GetTemplates() { return m_templates
; }
387 // Make a default document name
388 virtual bool MakeDefaultName(wxString
& buf
);
390 // Make a frame title (override this to do something different)
391 virtual wxString
MakeFrameTitle(wxDocument
* doc
);
393 virtual wxFileHistory
*OnCreateFileHistory();
394 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
396 // File history management
397 virtual void AddFileToHistory(const wxString
& file
);
398 virtual void RemoveFileFromHistory(int i
);
399 virtual int GetNoHistoryFiles() const;
400 virtual wxString
GetHistoryFile(int i
) const;
401 virtual void FileHistoryUseMenu(wxMenu
*menu
);
402 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
404 virtual void FileHistoryLoad(wxConfigBase
& config
);
405 virtual void FileHistorySave(wxConfigBase
& config
);
406 #endif // wxUSE_CONFIG
408 virtual void FileHistoryAddFilesToMenu();
409 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
411 inline wxString
GetLastDirectory() const { return m_lastDirectory
; }
412 inline void SetLastDirectory(const wxString
& dir
) { m_lastDirectory
= dir
; }
414 // Get the current document manager
415 static wxDocManager
* GetDocumentManager() { return sm_docManager
; }
419 int m_defaultDocumentNameCounter
;
423 wxView
* m_currentView
;
424 wxFileHistory
* m_fileHistory
;
425 wxString m_lastDirectory
;
426 static wxDocManager
* sm_docManager
;
428 DECLARE_EVENT_TABLE()
429 DECLARE_NO_COPY_CLASS(wxDocManager
)
432 // ----------------------------------------------------------------------------
433 // A default child frame
434 // ----------------------------------------------------------------------------
436 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
439 wxDocChildFrame(wxDocument
*doc
,
443 const wxString
& title
,
444 const wxPoint
& pos
= wxDefaultPosition
,
445 const wxSize
& size
= wxDefaultSize
,
446 long type
= wxDEFAULT_FRAME_STYLE
,
447 const wxString
& name
= wxT("frame"));
450 // Extend event processing to search the view's event table
451 virtual bool ProcessEvent(wxEvent
& event
);
453 void OnActivate(wxActivateEvent
& event
);
454 void OnCloseWindow(wxCloseEvent
& event
);
456 wxDocument
*GetDocument() const { return m_childDocument
; }
457 wxView
*GetView() const { return m_childView
; }
458 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
459 void SetView(wxView
*view
) { m_childView
= view
; }
460 bool Destroy() { m_childView
= (wxView
*)NULL
; return wxFrame::Destroy(); }
463 wxDocument
* m_childDocument
;
467 DECLARE_CLASS(wxDocChildFrame
)
468 DECLARE_EVENT_TABLE()
469 DECLARE_NO_COPY_CLASS(wxDocChildFrame
)
472 // ----------------------------------------------------------------------------
473 // A default parent frame
474 // ----------------------------------------------------------------------------
476 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
479 wxDocParentFrame(wxDocManager
*manager
,
482 const wxString
& title
,
483 const wxPoint
& pos
= wxDefaultPosition
,
484 const wxSize
& size
= wxDefaultSize
,
485 long type
= wxDEFAULT_FRAME_STYLE
,
486 const wxString
& name
= wxT("frame"));
488 // Extend event processing to search the document manager's event table
489 virtual bool ProcessEvent(wxEvent
& event
);
491 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
493 void OnExit(wxCommandEvent
& event
);
494 void OnMRUFile(wxCommandEvent
& event
);
495 void OnCloseWindow(wxCloseEvent
& event
);
498 wxDocManager
*m_docManager
;
501 DECLARE_CLASS(wxDocParentFrame
)
502 DECLARE_EVENT_TABLE()
503 DECLARE_NO_COPY_CLASS(wxDocParentFrame
)
506 // ----------------------------------------------------------------------------
507 // Provide simple default printing facilities
508 // ----------------------------------------------------------------------------
510 #if wxUSE_PRINTING_ARCHITECTURE
511 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
514 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= wxT("Printout"));
515 bool OnPrintPage(int page
);
516 bool HasPage(int page
);
517 bool OnBeginDocument(int startPage
, int endPage
);
518 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
520 virtual wxView
*GetView() { return m_printoutView
; }
523 wxView
* m_printoutView
;
526 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
527 DECLARE_NO_COPY_CLASS(wxDocPrintout
)
529 #endif // wxUSE_PRINTING_ARCHITECTURE
531 // ----------------------------------------------------------------------------
532 // File history management
533 // ----------------------------------------------------------------------------
535 class WXDLLEXPORT wxFileHistory
: public wxObject
538 wxFileHistory(int maxFiles
= 9);
542 virtual void AddFileToHistory(const wxString
& file
);
543 virtual void RemoveFileFromHistory(int i
);
544 virtual int GetMaxFiles() const { return m_fileMaxFiles
; }
545 virtual void UseMenu(wxMenu
*menu
);
547 // Remove menu from the list (MDI child may be closing)
548 virtual void RemoveMenu(wxMenu
*menu
);
551 virtual void Load(wxConfigBase
& config
);
552 virtual void Save(wxConfigBase
& config
);
553 #endif // wxUSE_CONFIG
555 virtual void AddFilesToMenu();
556 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
559 virtual wxString
GetHistoryFile(int i
) const;
561 // A synonym for GetNoHistoryFiles
562 virtual int GetCount() const { return m_fileHistoryN
; }
563 int GetNoHistoryFiles() const { return m_fileHistoryN
; }
565 wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
569 wxChar
** m_fileHistory
;
570 // Number of files saved
572 // Menus to maintain (may need several for an MDI app)
574 // Max files to maintain
578 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
579 DECLARE_NO_COPY_CLASS(wxFileHistory
)
582 #if wxUSE_STD_IOSTREAM
583 // For compatibility with existing file formats:
584 // converts from/to a stream to/from a temporary file.
585 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxSTD ostream
& stream
);
586 bool WXDLLEXPORT
wxTransferStreamToFile(wxSTD istream
& stream
, const wxString
& filename
);
588 // For compatibility with existing file formats:
589 // converts from/to a stream to/from a temporary file.
590 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, wxOutputStream
& stream
);
591 bool WXDLLEXPORT
wxTransferStreamToFile(wxInputStream
& stream
, const wxString
& filename
);
592 #endif // wxUSE_STD_IOSTREAM
594 #endif // wxUSE_DOC_VIEW_ARCHITECTURE