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
;
38 class WXDLLEXPORT wxConfigBase
;
42 // N.B. BC++ doesn't have istream.h, ostream.h
43 # include <iostream.h>
52 // Document manager flags
56 #define wxDOC_SILENT 8
57 #define wxDEFAULT_DOCMAN_FLAGS wxDOC_SDI
59 // Document template flags
60 #define wxTEMPLATE_VISIBLE 1
61 #define wxTEMPLATE_INVISIBLE 2
62 #define wxDEFAULT_TEMPLATE_FLAGS wxTEMPLATE_VISIBLE
64 #define wxMAX_FILE_HISTORY 9
66 class WXDLLEXPORT wxDocument
: public wxEvtHandler
68 DECLARE_ABSTRACT_CLASS(wxDocument
)
70 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
73 void SetFilename(const wxString
& filename
, bool notifyViews
= FALSE
);
74 inline wxString
GetFilename(void) const { return m_documentFile
; }
75 inline void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
76 inline wxString
GetTitle(void) const { return m_documentTitle
; }
77 inline void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
78 inline wxString
GetDocumentName(void) const { return m_documentTypeName
; }
79 // Has the document been saved yet?
80 inline bool GetDocumentSaved(void) { return m_savedYet
; }
81 inline void SetDocumentSaved(bool saved
= TRUE
) { m_savedYet
= saved
; }
83 virtual bool Close(void);
84 virtual bool Save(void);
85 virtual bool SaveAs(void);
86 virtual bool Revert(void);
88 virtual ostream
& SaveObject(ostream
& stream
);
89 virtual istream
& LoadObject(istream
& stream
);
91 // Called by wxWindows
92 virtual bool OnSaveDocument(const wxString
& filename
);
93 virtual bool OnOpenDocument(const wxString
& filename
);
94 virtual bool OnNewDocument(void);
95 virtual bool OnCloseDocument(void);
97 // Prompts for saving if about to close a modified document.
98 // Returns TRUE if ok to close the document (may have saved in the
99 // meantime, or set modified to FALSE)
100 virtual bool OnSaveModified(void);
102 // Called by framework if created automatically by the
103 // default document manager: gives document a chance to
104 // initialise and (usually) create a view
105 virtual bool OnCreate(const wxString
& path
, long flags
);
107 // By default, creates a base wxCommandProcessor.
108 virtual wxCommandProcessor
*OnCreateCommandProcessor(void);
109 virtual inline wxCommandProcessor
*GetCommandProcessor(void) const { return m_commandProcessor
; }
110 virtual inline void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
112 // Called after a view is added or removed.
113 // The default implementation deletes the document if this
114 // is there are no more views.
115 virtual void OnChangedViewList(void);
117 virtual bool DeleteContents(void);
119 virtual bool Draw(wxDC
&);
120 virtual inline bool IsModified(void) const { return m_documentModified
; }
121 virtual inline void Modify(bool mod
) { m_documentModified
= mod
; }
123 virtual bool AddView(wxView
*view
);
124 virtual bool RemoveView(wxView
*view
);
125 inline wxList
& GetViews(void) const { return (wxList
&) m_documentViews
; }
126 wxView
*GetFirstView(void) const;
128 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
130 // Remove all views (because we're closing the document)
131 virtual bool DeleteAllViews(void);
134 virtual wxDocManager
*GetDocumentManager(void) const;
135 virtual inline wxDocTemplate
*GetDocumentTemplate(void) const { return m_documentTemplate
; }
136 virtual inline void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
138 // Get title, or filename if no title, else [unnamed]
139 virtual bool GetPrintableName(wxString
& buf
) const;
141 // Returns a window that can be used as a parent for document-related
142 // dialogs. Override if necessary.
143 virtual wxWindow
*GetDocumentWindow(void) const;
145 wxList m_documentViews
;
146 wxString m_documentFile
;
147 wxString m_documentTitle
;
148 wxString m_documentTypeName
;
149 wxDocTemplate
* m_documentTemplate
;
150 bool m_documentModified
;
151 wxDocument
* m_documentParent
;
152 wxCommandProcessor
* m_commandProcessor
;
156 class WXDLLEXPORT wxView
: public wxEvtHandler
158 DECLARE_ABSTRACT_CLASS(wxView
)
160 // wxView(wxDocument *doc = (wxDocument *) NULL);
164 inline wxDocument
*GetDocument(void) const { return m_viewDocument
; }
165 void SetDocument(wxDocument
*doc
);
167 inline wxString
GetViewName(void) const { return m_viewTypeName
; }
168 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
170 inline wxFrame
*GetFrame(void) const { return m_viewFrame
; }
171 inline 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(void);
179 // Called by framework if created automatically by the
180 // default document manager class: gives view a chance to
182 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
184 // Checks if the view is the last one for the document; if so,
185 // asks user to confirm save data (if modified). If ok,
186 // deletes itself and returns TRUE.
187 virtual bool Close(bool deleteWindow
= TRUE
);
189 // Override to do cleanup/veto close
190 virtual bool OnClose(bool deleteWindow
);
192 #if WXWIN_COMPATIBILITY
193 // Defeat compiler warning
194 bool OnClose(void) { return wxEvtHandler::OnClose(); }
197 // Extend event processing to search the document's event table
198 virtual bool ProcessEvent(wxEvent
& event
);
200 // A view's window can call this to notify the view it is (in)active.
201 // The function then notifies the document manager.
202 virtual void Activate(bool activate
);
204 wxDocManager
*GetDocumentManager(void) const
205 { return m_viewDocument
->GetDocumentManager(); }
207 #if wxUSE_PRINTING_ARCHITECTURE
208 virtual wxPrintout
*OnCreatePrintout(void);
212 wxDocument
* m_viewDocument
;
213 wxString m_viewTypeName
;
214 wxFrame
* m_viewFrame
;
217 // Represents user interface (and other) properties of documents and views
218 class WXDLLEXPORT wxDocTemplate
: public wxObject
220 DECLARE_CLASS(wxDocTemplate
)
222 friend class WXDLLEXPORT wxDocManager
;
226 // Associate document and view types.
227 // They're for identifying what view is associated with what
228 // template/document type
229 wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
, const wxString
& filter
, const wxString
& dir
,
230 const wxString
& ext
, const wxString
& docTypeName
, const wxString
& viewTypeName
,
231 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
, wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
232 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
234 ~wxDocTemplate(void);
236 // By default, these two member functions dynamically creates document
237 // and view using dynamic instance construction.
238 // Override these if you need a different method of construction.
239 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
240 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
242 inline wxString
GetDefaultExtension(void) const { return m_defaultExt
; };
243 inline wxString
GetDescription(void) const { return m_description
; }
244 inline wxString
GetDirectory(void) const { return m_directory
; };
245 inline wxDocManager
*GetDocumentManager(void) const { return m_documentManager
; }
246 inline void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
247 inline wxString
GetFileFilter(void) const { return m_fileFilter
; };
248 inline long GetFlags(void) const { return m_flags
; };
249 virtual wxString
GetViewName(void) const { return m_viewTypeName
; }
250 virtual wxString
GetDocumentName(void) const { return m_docTypeName
; }
252 inline void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
253 inline void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
254 inline void SetDescription(const wxString
& descr
) { m_description
= descr
; };
255 inline void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
256 inline void SetFlags(long flags
) { m_flags
= flags
; };
258 inline bool IsVisible(void) const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
262 wxString m_fileFilter
;
263 wxString m_directory
;
264 wxString m_description
;
265 wxString m_defaultExt
;
266 wxString m_docTypeName
;
267 wxString m_viewTypeName
;
268 wxDocManager
* m_documentManager
;
270 // For dynamic creation of appropriate instances.
271 wxClassInfo
* m_docClassInfo
;
272 wxClassInfo
* m_viewClassInfo
;
276 // One object of this class may be created in an application,
277 // to manage all the templates and documents.
278 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
280 DECLARE_DYNAMIC_CLASS(wxDocManager
)
282 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
285 virtual bool Initialize(void);
287 // Handlers for common user commands
288 // virtual void OldOnMenuCommand(int command);
290 void OnFileClose(wxCommandEvent
& event
);
291 void OnFileNew(wxCommandEvent
& event
);
292 void OnFileOpen(wxCommandEvent
& event
);
293 void OnFileRevert(wxCommandEvent
& event
);
294 void OnFileSave(wxCommandEvent
& event
);
295 void OnFileSaveAs(wxCommandEvent
& event
);
296 void OnPrint(wxCommandEvent
& event
);
297 void OnPrintSetup(wxCommandEvent
& event
);
298 void OnPreview(wxCommandEvent
& event
);
299 void OnUndo(wxCommandEvent
& event
);
300 void OnRedo(wxCommandEvent
& event
);
302 // Extend event processing to search the view's event table
303 virtual bool ProcessEvent(wxEvent
& event
);
305 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
306 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
307 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
308 virtual bool FlushDoc(wxDocument
*doc
);
309 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
310 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
311 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
312 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
314 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
316 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
318 void AssociateTemplate(wxDocTemplate
*temp
);
319 void DisassociateTemplate(wxDocTemplate
*temp
);
321 wxDocument
*GetCurrentDocument(void) const;
323 inline void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
324 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen
; }
326 // Add and remove a document from the manager's list
327 void AddDocument(wxDocument
*doc
);
328 void RemoveDocument(wxDocument
*doc
);
330 // Clear remaining documents and templates
331 bool Clear(bool force
= TRUE
);
333 // Views or windows should inform the document manager
334 // when a view is going in or out of focus
335 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
336 virtual wxView
*GetCurrentView(void) const;
338 virtual inline wxList
& GetDocuments(void) const { return (wxList
&) m_docs
; }
340 // Make a default document name
341 virtual bool MakeDefaultName(wxString
& buf
);
343 virtual wxFileHistory
*OnCreateFileHistory(void);
344 virtual inline wxFileHistory
*GetFileHistory(void) const { return m_fileHistory
; }
346 // File history management
347 virtual void AddFileToHistory(const wxString
& file
);
348 virtual int GetNoHistoryFiles(void) const;
349 virtual wxString
GetHistoryFile(int i
) const;
350 virtual void FileHistoryUseMenu(wxMenu
*menu
);
351 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
353 virtual void FileHistoryLoad(wxConfigBase
& config
);
354 virtual void FileHistorySave(wxConfigBase
& config
);
356 virtual void FileHistoryAddFilesToMenu();
357 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
360 int m_defaultDocumentNameCounter
;
364 wxView
* m_currentView
;
365 wxFileHistory
* m_fileHistory
;
367 DECLARE_EVENT_TABLE()
371 * A default child frame
374 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
376 DECLARE_CLASS(wxDocChildFrame
)
379 wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
380 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
381 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
382 ~wxDocChildFrame(void);
384 // Extend event processing to search the view's event table
385 virtual bool ProcessEvent(wxEvent
& event
);
387 void OnActivate(wxActivateEvent
& event
);
388 void OnCloseWindow(wxCloseEvent
& event
);
390 inline wxDocument
*GetDocument(void) const { return m_childDocument
; }
391 inline wxView
*GetView(void) const { return m_childView
; }
392 inline void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
393 inline void SetView(wxView
*view
) { m_childView
= view
; }
395 wxDocument
* m_childDocument
;
398 DECLARE_EVENT_TABLE()
403 * A default parent frame
406 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
408 DECLARE_CLASS(wxDocParentFrame
)
410 wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
411 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
412 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
414 // Extend event processing to search the document manager's event table
415 virtual bool ProcessEvent(wxEvent
& event
);
417 wxDocManager
*GetDocumentManager(void) const { return m_docManager
; }
419 void OnExit(wxCommandEvent
& event
);
420 void OnMRUFile(wxCommandEvent
& event
);
421 void OnCloseWindow(wxCloseEvent
& event
);
424 wxDocManager
*m_docManager
;
426 DECLARE_EVENT_TABLE()
430 * Provide simple default printing facilities
433 #if wxUSE_PRINTING_ARCHITECTURE
434 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
436 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
438 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= "Printout");
439 bool OnPrintPage(int page
);
440 bool HasPage(int page
);
441 bool OnBeginDocument(int startPage
, int endPage
);
442 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
444 virtual inline wxView
*GetView(void) { return m_printoutView
; }
446 wxView
* m_printoutView
;
451 * Command processing framework
454 class WXDLLEXPORT wxCommand
: public wxObject
456 DECLARE_CLASS(wxCommand
)
458 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
461 // Override this to perform a command
462 virtual bool Do(void) = 0;
464 // Override this to undo a command
465 virtual bool Undo(void) = 0;
467 virtual inline bool CanUndo(void) const { return m_canUndo
; }
468 virtual inline wxString
GetName(void) const { return m_commandName
; }
471 wxString m_commandName
;
474 class WXDLLEXPORT wxCommandProcessor
: public wxObject
476 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
478 wxCommandProcessor(int maxCommands
= 100);
479 ~wxCommandProcessor(void);
481 // Pass a command to the processor. The processor calls Do();
482 // if successful, is appended to the command history unless
484 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
485 virtual bool Undo(void);
486 virtual bool Redo(void);
487 virtual bool CanUndo(void) const;
488 virtual bool CanRedo(void) const;
490 // Call this to manage an edit menu.
491 inline void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
492 inline wxMenu
*GetEditMenu(void) const { return m_commandEditMenu
; }
493 virtual void SetMenuStrings(void);
494 virtual void Initialize(void);
496 inline wxList
& GetCommands(void) const { return (wxList
&) m_commands
; }
497 inline int GetMaxCommands(void) const { return m_maxNoCommands
; }
498 virtual void ClearCommands(void);
503 wxNode
* m_currentCommand
;
504 wxMenu
* m_commandEditMenu
;
507 // File history management
509 class WXDLLEXPORT wxFileHistory
: public wxObject
511 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
513 wxFileHistory(int maxFiles
= 9);
514 ~wxFileHistory(void);
517 virtual void AddFileToHistory(const wxString
& file
);
518 virtual int GetMaxFiles(void) const { return m_fileMaxFiles
; }
519 virtual void UseMenu(wxMenu
*menu
);
521 // Remove menu from the list (MDI child may be closing)
522 virtual void RemoveMenu(wxMenu
*menu
);
525 virtual void Load(wxConfigBase
& config
);
526 virtual void Save(wxConfigBase
& config
);
529 virtual void AddFilesToMenu();
530 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
533 virtual wxString
GetHistoryFile(int i
) const;
535 // A synonym for GetNoHistoryFiles
536 virtual int GetCount() const { return m_fileHistoryN
; }
537 inline int GetNoHistoryFiles(void) const { return m_fileHistoryN
; }
539 inline wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
543 char** m_fileHistory
;
544 // Number of files saved
546 // Menus to maintain (may need several for an MDI app)
548 // Max files to maintain
552 // For compatibility with existing file formats:
553 // converts from/to a stream to/from a temporary file.
554 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
555 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);