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"
24 #if wxUSE_PRINTING_ARCHITECTURE
28 class WXDLLEXPORT wxWindow
;
29 class WXDLLEXPORT wxDocument
;
30 class WXDLLEXPORT wxView
;
31 class WXDLLEXPORT wxDocTemplate
;
32 class WXDLLEXPORT wxDocManager
;
33 class WXDLLEXPORT wxPrintInfo
;
34 class WXDLLEXPORT wxCommand
;
35 class WXDLLEXPORT wxCommandProcessor
;
36 class WXDLLEXPORT wxFileHistory
;
37 class WXDLLEXPORT wxConfigBase
;
39 #include "wx/ioswrap.h"
41 // Document manager flags
48 wxDEFAULT_DOCMAN_FLAGS
= wxDOC_SDI
51 // Document template flags
54 wxTEMPLATE_VISIBLE
= 1,
56 wxDEFAULT_TEMPLATE_FLAGS
= wxTEMPLATE_VISIBLE
59 #define wxMAX_FILE_HISTORY 9
61 class WXDLLEXPORT wxDocument
: public wxEvtHandler
63 DECLARE_ABSTRACT_CLASS(wxDocument
)
66 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
70 void SetFilename(const wxString
& filename
, bool notifyViews
= FALSE
);
71 wxString
GetFilename() const { return m_documentFile
; }
73 void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
74 wxString
GetTitle() const { return m_documentTitle
; }
76 void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
77 wxString
GetDocumentName() const { return m_documentTypeName
; }
79 bool GetDocumentSaved() const { return m_savedYet
; }
80 void SetDocumentSaved(bool saved
= TRUE
) { m_savedYet
= saved
; }
84 virtual bool SaveAs();
85 virtual bool Revert();
87 virtual ostream
& SaveObject(ostream
& stream
);
88 virtual istream
& LoadObject(istream
& stream
);
90 // Called by wxWindows
91 virtual bool OnSaveDocument(const wxString
& filename
);
92 virtual bool OnOpenDocument(const wxString
& filename
);
93 virtual bool OnNewDocument();
94 virtual bool OnCloseDocument();
96 // Prompts for saving if about to close a modified document. Returns TRUE
97 // if ok to close the document (may have saved in the meantime, or set
99 virtual bool OnSaveModified();
101 // Called by framework if created automatically by the default document
102 // manager: gives document a chance to initialise and (usually) create a
104 virtual bool OnCreate(const wxString
& path
, long flags
);
106 // By default, creates a base wxCommandProcessor.
107 virtual wxCommandProcessor
*OnCreateCommandProcessor();
108 virtual wxCommandProcessor
*GetCommandProcessor() const { return m_commandProcessor
; }
109 virtual void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
111 // Called after a view is added or removed. The default implementation
112 // deletes the document if this is there are no more views.
113 virtual void OnChangedViewList();
115 virtual bool DeleteContents();
117 virtual bool Draw(wxDC
&);
118 virtual bool IsModified() const { return m_documentModified
; }
119 virtual void Modify(bool mod
) { m_documentModified
= mod
; }
121 virtual bool AddView(wxView
*view
);
122 virtual bool RemoveView(wxView
*view
);
123 wxList
& GetViews() const { return (wxList
&) m_documentViews
; }
124 wxView
*GetFirstView() const;
126 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
128 // Remove all views (because we're closing the document)
129 virtual bool DeleteAllViews();
132 virtual wxDocManager
*GetDocumentManager() const;
133 virtual wxDocTemplate
*GetDocumentTemplate() const { return m_documentTemplate
; }
134 virtual void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
136 // Get title, or filename if no title, else [unnamed]
137 virtual bool GetPrintableName(wxString
& buf
) const;
139 // Returns a window that can be used as a parent for document-related
140 // dialogs. Override if necessary.
141 virtual wxWindow
*GetDocumentWindow() const;
144 wxList m_documentViews
;
145 wxString m_documentFile
;
146 wxString m_documentTitle
;
147 wxString m_documentTypeName
;
148 wxDocTemplate
* m_documentTemplate
;
149 bool m_documentModified
;
150 wxDocument
* m_documentParent
;
151 wxCommandProcessor
* m_commandProcessor
;
155 class WXDLLEXPORT wxView
: public wxEvtHandler
157 DECLARE_ABSTRACT_CLASS(wxView
)
160 // wxView(wxDocument *doc = (wxDocument *) NULL);
164 wxDocument
*GetDocument() const { return m_viewDocument
; }
165 void SetDocument(wxDocument
*doc
);
167 wxString
GetViewName() const { return m_viewTypeName
; }
168 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
170 wxFrame
*GetFrame() const { return m_viewFrame
; }
171 void SetFrame(wxFrame
*frame
) { m_viewFrame
= frame
; }
173 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
174 virtual void OnDraw(wxDC
*dc
) = 0;
175 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
176 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
177 virtual void OnChangeFilename();
179 // Called by framework if created automatically by the default document
180 // manager class: gives view a chance to initialise
181 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
183 // Checks if the view is the last one for the document; if so, asks user
184 // to confirm save data (if modified). If ok, deletes itself and returns
186 virtual bool Close(bool deleteWindow
= TRUE
);
188 // Override to do cleanup/veto close
189 virtual bool OnClose(bool deleteWindow
);
191 #if WXWIN_COMPATIBILITY
192 // Defeat compiler warning
193 bool OnClose() { return wxEvtHandler::OnClose(); }
196 // Extend event processing to search the document's event table
197 virtual bool ProcessEvent(wxEvent
& event
);
199 // A view's window can call this to notify the view it is (in)active.
200 // The function then notifies the document manager.
201 virtual void Activate(bool activate
);
203 wxDocManager
*GetDocumentManager() const
204 { return m_viewDocument
->GetDocumentManager(); }
206 #if wxUSE_PRINTING_ARCHITECTURE
207 virtual wxPrintout
*OnCreatePrintout();
211 wxDocument
* m_viewDocument
;
212 wxString m_viewTypeName
;
213 wxFrame
* m_viewFrame
;
216 // Represents user interface (and other) properties of documents and views
217 class WXDLLEXPORT wxDocTemplate
: public wxObject
219 DECLARE_CLASS(wxDocTemplate
)
221 friend class WXDLLEXPORT wxDocManager
;
224 // Associate document and view types. They're for identifying what view is
225 // associated with what template/document type
226 wxDocTemplate(wxDocManager
*manager
,
227 const wxString
& descr
,
228 const wxString
& filter
,
231 const wxString
& docTypeName
,
232 const wxString
& viewTypeName
,
233 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
,
234 wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
235 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
239 // By default, these two member functions dynamically creates document and
240 // view using dynamic instance construction. Override these if you need a
241 // different method of construction.
242 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
243 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
245 wxString
GetDefaultExtension() const { return m_defaultExt
; };
246 wxString
GetDescription() const { return m_description
; }
247 wxString
GetDirectory() const { return m_directory
; };
248 wxDocManager
*GetDocumentManager() const { return m_documentManager
; }
249 void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
250 wxString
GetFileFilter() const { return m_fileFilter
; };
251 long GetFlags() const { return m_flags
; };
252 virtual wxString
GetViewName() const { return m_viewTypeName
; }
253 virtual wxString
GetDocumentName() const { return m_docTypeName
; }
255 void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
256 void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
257 void SetDescription(const wxString
& descr
) { m_description
= descr
; };
258 void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
259 void SetFlags(long flags
) { m_flags
= flags
; };
261 bool IsVisible() const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
263 virtual bool FileMatchesTemplate(const wxString
& path
);
267 wxString m_fileFilter
;
268 wxString m_directory
;
269 wxString m_description
;
270 wxString m_defaultExt
;
271 wxString m_docTypeName
;
272 wxString m_viewTypeName
;
273 wxDocManager
* m_documentManager
;
275 // For dynamic creation of appropriate instances.
276 wxClassInfo
* m_docClassInfo
;
277 wxClassInfo
* m_viewClassInfo
;
280 // One object of this class may be created in an application, to manage all
281 // the templates and documents.
282 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
284 DECLARE_DYNAMIC_CLASS(wxDocManager
)
287 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
290 virtual bool Initialize();
292 // Handlers for common user commands
293 void OnFileClose(wxCommandEvent
& event
);
294 void OnFileNew(wxCommandEvent
& event
);
295 void OnFileOpen(wxCommandEvent
& event
);
296 void OnFileRevert(wxCommandEvent
& event
);
297 void OnFileSave(wxCommandEvent
& event
);
298 void OnFileSaveAs(wxCommandEvent
& event
);
299 void OnPrint(wxCommandEvent
& event
);
300 void OnPrintSetup(wxCommandEvent
& event
);
301 void OnPreview(wxCommandEvent
& event
);
302 void OnUndo(wxCommandEvent
& event
);
303 void OnRedo(wxCommandEvent
& event
);
305 // Extend event processing to search the view's event table
306 virtual bool ProcessEvent(wxEvent
& event
);
308 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
309 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
310 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
311 virtual bool FlushDoc(wxDocument
*doc
);
312 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
313 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
314 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
315 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
317 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
319 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
321 void AssociateTemplate(wxDocTemplate
*temp
);
322 void DisassociateTemplate(wxDocTemplate
*temp
);
324 wxDocument
*GetCurrentDocument() const;
326 void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
327 int GetMaxDocsOpen() const { return m_maxDocsOpen
; }
329 // Add and remove a document from the manager's list
330 void AddDocument(wxDocument
*doc
);
331 void RemoveDocument(wxDocument
*doc
);
333 // Clear remaining documents and templates
334 bool Clear(bool force
= TRUE
);
336 // Views or windows should inform the document manager
337 // when a view is going in or out of focus
338 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
339 virtual wxView
*GetCurrentView() const;
341 virtual wxList
& GetDocuments() const { return (wxList
&) m_docs
; }
343 // Make a default document name
344 virtual bool MakeDefaultName(wxString
& buf
);
346 virtual wxFileHistory
*OnCreateFileHistory();
347 virtual wxFileHistory
*GetFileHistory() const { return m_fileHistory
; }
349 // File history management
350 virtual void AddFileToHistory(const wxString
& file
);
351 virtual int GetNoHistoryFiles() const;
352 virtual wxString
GetHistoryFile(int i
) const;
353 virtual void FileHistoryUseMenu(wxMenu
*menu
);
354 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
356 virtual void FileHistoryLoad(wxConfigBase
& config
);
357 virtual void FileHistorySave(wxConfigBase
& config
);
358 #endif // wxUSE_CONFIG
360 virtual void FileHistoryAddFilesToMenu();
361 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
365 int m_defaultDocumentNameCounter
;
369 wxView
* m_currentView
;
370 wxFileHistory
* m_fileHistory
;
372 DECLARE_EVENT_TABLE()
375 // ----------------------------------------------------------------------------
376 // A default child frame
377 // ----------------------------------------------------------------------------
379 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
381 DECLARE_CLASS(wxDocChildFrame
)
384 wxDocChildFrame(wxDocument
*doc
,
388 const wxString
& title
,
389 const wxPoint
& pos
= wxDefaultPosition
,
390 const wxSize
& size
= wxDefaultSize
,
391 long type
= wxDEFAULT_FRAME_STYLE
,
392 const wxString
& name
= "frame");
395 // Extend event processing to search the view's event table
396 virtual bool ProcessEvent(wxEvent
& event
);
398 void OnActivate(wxActivateEvent
& event
);
399 void OnCloseWindow(wxCloseEvent
& event
);
401 wxDocument
*GetDocument() const { return m_childDocument
; }
402 wxView
*GetView() const { return m_childView
; }
403 void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
404 void SetView(wxView
*view
) { m_childView
= view
; }
407 wxDocument
* m_childDocument
;
410 DECLARE_EVENT_TABLE()
413 // ----------------------------------------------------------------------------
414 // A default parent frame
415 // ----------------------------------------------------------------------------
417 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
419 DECLARE_CLASS(wxDocParentFrame
)
422 wxDocParentFrame(wxDocManager
*manager
,
425 const wxString
& title
,
426 const wxPoint
& pos
= wxDefaultPosition
,
427 const wxSize
& size
= wxDefaultSize
,
428 long type
= wxDEFAULT_FRAME_STYLE
,
429 const wxString
& name
= "frame");
431 // Extend event processing to search the document manager's event table
432 virtual bool ProcessEvent(wxEvent
& event
);
434 wxDocManager
*GetDocumentManager() const { return m_docManager
; }
436 void OnExit(wxCommandEvent
& event
);
437 void OnMRUFile(wxCommandEvent
& event
);
438 void OnCloseWindow(wxCloseEvent
& event
);
441 wxDocManager
*m_docManager
;
443 DECLARE_EVENT_TABLE()
446 // ----------------------------------------------------------------------------
447 // Provide simple default printing facilities
448 // ----------------------------------------------------------------------------
450 #if wxUSE_PRINTING_ARCHITECTURE
451 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
453 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
456 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= "Printout");
457 bool OnPrintPage(int page
);
458 bool HasPage(int page
);
459 bool OnBeginDocument(int startPage
, int endPage
);
460 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
462 virtual wxView
*GetView() { return m_printoutView
; }
465 wxView
* m_printoutView
;
467 #endif // wxUSE_PRINTING_ARCHITECTURE
469 // ----------------------------------------------------------------------------
470 // Command processing framework
471 // ----------------------------------------------------------------------------
473 class WXDLLEXPORT wxCommand
: public wxObject
475 DECLARE_CLASS(wxCommand
)
478 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
481 // Override this to perform a command
482 virtual bool Do() = 0;
484 // Override this to undo a command
485 virtual bool Undo() = 0;
487 virtual bool CanUndo() const { return m_canUndo
; }
488 virtual wxString
GetName() const { return m_commandName
; }
492 wxString m_commandName
;
495 class WXDLLEXPORT wxCommandProcessor
: public wxObject
497 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
500 wxCommandProcessor(int maxCommands
= 100);
501 ~wxCommandProcessor();
503 // Pass a command to the processor. The processor calls Do(); if
504 // successful, is appended to the command history unless storeIt is FALSE.
505 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
508 virtual bool CanUndo() const;
509 virtual bool CanRedo() const;
511 // Call this to manage an edit menu.
512 void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
513 wxMenu
*GetEditMenu() const { return m_commandEditMenu
; }
514 virtual void SetMenuStrings();
515 virtual void Initialize();
517 wxList
& GetCommands() const { return (wxList
&) m_commands
; }
518 int GetMaxCommands() const { return m_maxNoCommands
; }
519 virtual void ClearCommands();
524 wxNode
* m_currentCommand
;
525 wxMenu
* m_commandEditMenu
;
528 // ----------------------------------------------------------------------------
529 // File history management
530 // ----------------------------------------------------------------------------
532 class WXDLLEXPORT wxFileHistory
: public wxObject
534 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
537 wxFileHistory(int maxFiles
= 9);
541 virtual void AddFileToHistory(const wxString
& file
);
542 virtual int GetMaxFiles() const { return m_fileMaxFiles
; }
543 virtual void UseMenu(wxMenu
*menu
);
545 // Remove menu from the list (MDI child may be closing)
546 virtual void RemoveMenu(wxMenu
*menu
);
549 virtual void Load(wxConfigBase
& config
);
550 virtual void Save(wxConfigBase
& config
);
551 #endif // wxUSE_CONFIG
553 virtual void AddFilesToMenu();
554 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
557 virtual wxString
GetHistoryFile(int i
) const;
559 // A synonym for GetNoHistoryFiles
560 virtual int GetCount() const { return m_fileHistoryN
; }
561 int GetNoHistoryFiles() const { return m_fileHistoryN
; }
563 wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
567 wxChar
** m_fileHistory
;
568 // Number of files saved
570 // Menus to maintain (may need several for an MDI app)
572 // Max files to maintain
576 // For compatibility with existing file formats:
577 // converts from/to a stream to/from a temporary file.
578 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
579 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);