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
;
40 // N.B. BC++ doesn't have istream.h, ostream.h
41 # include <iostream.h>
50 // Document manager flags
54 #define wxDOC_SILENT 8
55 #define wxDEFAULT_DOCMAN_FLAGS wxDOC_SDI
57 // Document template flags
58 #define wxTEMPLATE_VISIBLE 1
59 #define wxTEMPLATE_INVISIBLE 2
60 #define wxDEFAULT_TEMPLATE_FLAGS wxTEMPLATE_VISIBLE
62 #define wxMAX_FILE_HISTORY 9
64 class WXDLLEXPORT wxDocument
: public wxEvtHandler
66 DECLARE_ABSTRACT_CLASS(wxDocument
)
68 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
71 void SetFilename(const wxString
& filename
, bool notifyViews
= FALSE
);
72 inline wxString
GetFilename(void) const { return m_documentFile
; }
73 inline void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
74 inline wxString
GetTitle(void) const { return m_documentTitle
; }
75 inline void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
76 inline wxString
GetDocumentName(void) const { return m_documentTypeName
; }
77 // Has the document been saved yet?
78 inline bool GetDocumentSaved(void) { return m_savedYet
; }
79 inline void SetDocumentSaved(bool saved
= TRUE
) { m_savedYet
= saved
; }
81 virtual bool Close(void);
82 virtual bool Save(void);
83 virtual bool SaveAs(void);
84 virtual bool Revert(void);
86 virtual ostream
& SaveObject(ostream
& stream
);
87 virtual istream
& LoadObject(istream
& stream
);
89 // Called by wxWindows
90 virtual bool OnSaveDocument(const wxString
& filename
);
91 virtual bool OnOpenDocument(const wxString
& filename
);
92 virtual bool OnNewDocument(void);
93 virtual bool OnCloseDocument(void);
95 // Prompts for saving if about to close a modified document.
96 // Returns TRUE if ok to close the document (may have saved in the
97 // meantime, or set modified to FALSE)
98 virtual bool OnSaveModified(void);
100 // Called by framework if created automatically by the
101 // default document manager: gives document a chance to
102 // initialise and (usually) create a view
103 virtual bool OnCreate(const wxString
& path
, long flags
);
105 // By default, creates a base wxCommandProcessor.
106 virtual wxCommandProcessor
*OnCreateCommandProcessor(void);
107 virtual inline wxCommandProcessor
*GetCommandProcessor(void) const { return m_commandProcessor
; }
108 virtual inline void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
110 // Called after a view is added or removed.
111 // The default implementation deletes the document if this
112 // is there are no more views.
113 virtual void OnChangedViewList(void);
115 virtual bool DeleteContents(void);
117 virtual bool Draw(wxDC
&);
118 virtual inline bool IsModified(void) const { return m_documentModified
; }
119 virtual inline void Modify(bool mod
) { m_documentModified
= mod
; }
121 virtual bool AddView(wxView
*view
);
122 virtual bool RemoveView(wxView
*view
);
123 inline wxList
& GetViews(void) const { return (wxList
&) m_documentViews
; }
124 wxView
*GetFirstView(void) 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(void);
132 virtual wxDocManager
*GetDocumentManager(void) const;
133 virtual inline wxDocTemplate
*GetDocumentTemplate(void) const { return m_documentTemplate
; }
134 virtual inline 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(void) const;
143 wxList m_documentViews
;
144 wxString m_documentFile
;
145 wxString m_documentTitle
;
146 wxString m_documentTypeName
;
147 wxDocTemplate
* m_documentTemplate
;
148 bool m_documentModified
;
149 wxDocument
* m_documentParent
;
150 wxCommandProcessor
* m_commandProcessor
;
154 class WXDLLEXPORT wxView
: public wxEvtHandler
156 DECLARE_ABSTRACT_CLASS(wxView
)
158 wxView(wxDocument
*doc
= (wxDocument
*) NULL
);
161 inline wxDocument
*GetDocument(void) const { return m_viewDocument
; }
162 void SetDocument(wxDocument
*doc
);
164 inline wxString
GetViewName(void) const { return m_viewTypeName
; }
165 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
167 inline wxFrame
*GetFrame(void) const { return m_viewFrame
; }
168 inline void SetFrame(wxFrame
*frame
) { m_viewFrame
= frame
; }
170 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
171 virtual void OnDraw(wxDC
*dc
) = 0;
172 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
173 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
174 virtual void OnChangeFilename(void);
176 // Called by framework if created automatically by the
177 // default document manager class: gives view a chance to
179 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
181 // Checks if the view is the last one for the document; if so,
182 // asks user to confirm save data (if modified). If ok,
183 // deletes itself and returns TRUE.
184 virtual bool Close(bool deleteWindow
= TRUE
);
186 // Override to do cleanup/veto close
187 virtual bool OnClose(bool deleteWindow
);
188 // Defeat compiler warning
189 inline bool OnClose(void) { return wxEvtHandler::OnClose(); }
191 // Extend event processing to search the document's event table
192 virtual bool ProcessEvent(wxEvent
& event
);
194 // A view's window can call this to notify the view it is (in)active.
195 // The function then notifies the document manager.
196 virtual void Activate(bool activate
);
198 inline wxDocManager
*GetDocumentManager(void) const { return m_viewDocument
->GetDocumentManager(); }
200 #if wxUSE_PRINTING_ARCHITECTURE
201 virtual wxPrintout
*OnCreatePrintout(void);
205 wxDocument
* m_viewDocument
;
206 wxString m_viewTypeName
;
207 wxFrame
* m_viewFrame
;
210 // Represents user interface (and other) properties of documents and views
211 class WXDLLEXPORT wxDocTemplate
: public wxObject
213 DECLARE_CLASS(wxDocTemplate
)
215 friend class WXDLLEXPORT wxDocManager
;
219 // Associate document and view types.
220 // They're for identifying what view is associated with what
221 // template/document type
222 wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
, const wxString
& filter
, const wxString
& dir
,
223 const wxString
& ext
, const wxString
& docTypeName
, const wxString
& viewTypeName
,
224 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
, wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
225 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
227 ~wxDocTemplate(void);
229 // By default, these two member functions dynamically creates document
230 // and view using dynamic instance construction.
231 // Override these if you need a different method of construction.
232 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
233 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
235 inline wxString
GetDefaultExtension(void) const { return m_defaultExt
; };
236 inline wxString
GetDescription(void) const { return m_description
; }
237 inline wxString
GetDirectory(void) const { return m_directory
; };
238 inline wxDocManager
*GetDocumentManager(void) const { return m_documentManager
; }
239 inline void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
240 inline wxString
GetFileFilter(void) const { return m_fileFilter
; };
241 inline long GetFlags(void) const { return m_flags
; };
242 virtual wxString
GetViewName(void) const { return m_viewTypeName
; }
243 virtual wxString
GetDocumentName(void) const { return m_docTypeName
; }
245 inline void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
246 inline void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
247 inline void SetDescription(const wxString
& descr
) { m_description
= descr
; };
248 inline void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
249 inline void SetFlags(long flags
) { m_flags
= flags
; };
251 inline bool IsVisible(void) const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
255 wxString m_fileFilter
;
256 wxString m_directory
;
257 wxString m_description
;
258 wxString m_defaultExt
;
259 wxString m_docTypeName
;
260 wxString m_viewTypeName
;
261 wxDocManager
* m_documentManager
;
263 // For dynamic creation of appropriate instances.
264 wxClassInfo
* m_docClassInfo
;
265 wxClassInfo
* m_viewClassInfo
;
269 // One object of this class may be created in an application,
270 // to manage all the templates and documents.
271 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
273 DECLARE_DYNAMIC_CLASS(wxDocManager
)
275 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
278 virtual bool Initialize(void);
280 // Handlers for common user commands
281 // virtual void OldOnMenuCommand(int command);
283 void OnFileClose(wxCommandEvent
& event
);
284 void OnFileNew(wxCommandEvent
& event
);
285 void OnFileOpen(wxCommandEvent
& event
);
286 void OnFileRevert(wxCommandEvent
& event
);
287 void OnFileSave(wxCommandEvent
& event
);
288 void OnFileSaveAs(wxCommandEvent
& event
);
289 void OnPrint(wxCommandEvent
& event
);
290 void OnPrintSetup(wxCommandEvent
& event
);
291 void OnPreview(wxCommandEvent
& event
);
292 void OnUndo(wxCommandEvent
& event
);
293 void OnRedo(wxCommandEvent
& event
);
295 // Extend event processing to search the view's event table
296 virtual bool ProcessEvent(wxEvent
& event
);
298 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
299 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
300 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
301 virtual bool FlushDoc(wxDocument
*doc
);
302 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
303 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
304 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
305 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
307 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
309 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
311 void AssociateTemplate(wxDocTemplate
*temp
);
312 void DisassociateTemplate(wxDocTemplate
*temp
);
314 wxDocument
*GetCurrentDocument(void) const;
316 inline void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
317 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen
; }
319 // Add and remove a document from the manager's list
320 void AddDocument(wxDocument
*doc
);
321 void RemoveDocument(wxDocument
*doc
);
323 // Clear remaining documents and templates
324 bool Clear(bool force
= TRUE
);
326 // Views or windows should inform the document manager
327 // when a view is going in or out of focus
328 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
329 virtual wxView
*GetCurrentView(void) const;
331 virtual inline wxList
& GetDocuments(void) const { return (wxList
&) m_docs
; }
333 // Make a default document name
334 virtual bool MakeDefaultName(wxString
& buf
);
336 virtual wxFileHistory
*OnCreateFileHistory(void);
337 virtual inline wxFileHistory
*GetFileHistory(void) const { return m_fileHistory
; }
339 // File history management
340 virtual void AddFileToHistory(const wxString
& file
);
341 virtual int GetNoHistoryFiles(void) const;
342 virtual wxString
GetHistoryFile(int i
) const;
343 virtual void FileHistoryUseMenu(wxMenu
*menu
);
344 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
345 virtual void FileHistoryLoad(wxConfigBase
& config
);
346 virtual void FileHistorySave(wxConfigBase
& config
);
347 virtual void FileHistoryAddFilesToMenu();
348 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
351 int m_defaultDocumentNameCounter
;
355 wxView
* m_currentView
;
356 wxFileHistory
* m_fileHistory
;
358 DECLARE_EVENT_TABLE()
362 * A default child frame
365 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
367 DECLARE_CLASS(wxDocChildFrame
)
370 wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
371 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
372 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
373 ~wxDocChildFrame(void);
375 // Extend event processing to search the view's event table
376 virtual bool ProcessEvent(wxEvent
& event
);
378 void OnActivate(wxActivateEvent
& event
);
379 void OnCloseWindow(wxCloseEvent
& event
);
381 inline wxDocument
*GetDocument(void) const { return m_childDocument
; }
382 inline wxView
*GetView(void) const { return m_childView
; }
383 inline void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
384 inline void SetView(wxView
*view
) { m_childView
= view
; }
386 wxDocument
* m_childDocument
;
389 DECLARE_EVENT_TABLE()
394 * A default parent frame
397 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
399 DECLARE_CLASS(wxDocParentFrame
)
401 wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
402 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
403 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
405 // Extend event processing to search the document manager's event table
406 virtual bool ProcessEvent(wxEvent
& event
);
408 wxDocManager
*GetDocumentManager(void) const { return m_docManager
; }
410 void OnExit(wxCommandEvent
& event
);
411 void OnMRUFile(wxCommandEvent
& event
);
412 void OnCloseWindow(wxCloseEvent
& event
);
415 wxDocManager
*m_docManager
;
417 DECLARE_EVENT_TABLE()
421 * Provide simple default printing facilities
424 #if wxUSE_PRINTING_ARCHITECTURE
425 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
427 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
429 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= "Printout");
430 bool OnPrintPage(int page
);
431 bool HasPage(int page
);
432 bool OnBeginDocument(int startPage
, int endPage
);
433 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
435 virtual inline wxView
*GetView(void) { return m_printoutView
; }
437 wxView
* m_printoutView
;
442 * Command processing framework
445 class WXDLLEXPORT wxCommand
: public wxObject
447 DECLARE_CLASS(wxCommand
)
449 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
452 // Override this to perform a command
453 virtual bool Do(void) = 0;
455 // Override this to undo a command
456 virtual bool Undo(void) = 0;
458 virtual inline bool CanUndo(void) const { return m_canUndo
; }
459 virtual inline wxString
GetName(void) const { return m_commandName
; }
462 wxString m_commandName
;
465 class WXDLLEXPORT wxCommandProcessor
: public wxObject
467 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
469 wxCommandProcessor(int maxCommands
= 100);
470 ~wxCommandProcessor(void);
472 // Pass a command to the processor. The processor calls Do();
473 // if successful, is appended to the command history unless
475 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
476 virtual bool Undo(void);
477 virtual bool Redo(void);
478 virtual bool CanUndo(void) const;
479 virtual bool CanRedo(void) const;
481 // Call this to manage an edit menu.
482 inline void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
483 inline wxMenu
*GetEditMenu(void) const { return m_commandEditMenu
; }
484 virtual void SetMenuStrings(void);
485 virtual void Initialize(void);
487 inline wxList
& GetCommands(void) const { return (wxList
&) m_commands
; }
488 inline int GetMaxCommands(void) const { return m_maxNoCommands
; }
489 virtual void ClearCommands(void);
494 wxNode
* m_currentCommand
;
495 wxMenu
* m_commandEditMenu
;
498 // File history management
500 class WXDLLEXPORT wxFileHistory
: public wxObject
502 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
504 wxFileHistory(int maxFiles
= 9);
505 ~wxFileHistory(void);
508 virtual void AddFileToHistory(const wxString
& file
);
509 virtual int GetMaxFiles(void) const { return m_fileMaxFiles
; }
510 virtual void UseMenu(wxMenu
*menu
);
512 // Remove menu from the list (MDI child may be closing)
513 virtual void RemoveMenu(wxMenu
*menu
);
515 virtual void Load(wxConfigBase
& config
);
516 virtual void Save(wxConfigBase
& config
);
518 virtual void AddFilesToMenu();
519 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
522 virtual wxString
GetHistoryFile(int i
) const;
524 // A synonym for GetNoHistoryFiles
525 virtual int GetCount() const { return m_fileHistoryN
; }
526 inline int GetNoHistoryFiles(void) const { return m_fileHistoryN
; }
528 inline wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
532 char** m_fileHistory
;
533 // Number of files saved
535 // Menus to maintain (may need several for an MDI app)
537 // Max files to maintain
541 // For compatibility with existing file formats:
542 // converts from/to a stream to/from a temporary file.
543 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
544 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);