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 USE_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 WXDLLIMPORT ostream
;
39 class WXDLLIMPORT istream
;
41 // Document manager flags
45 #define wxDOC_SILENT 8
46 #define wxDEFAULT_DOCMAN_FLAGS wxDOC_SDI
48 // Document template flags
49 #define wxTEMPLATE_VISIBLE 1
50 #define wxTEMPLATE_INVISIBLE 2
51 #define wxDEFAULT_TEMPLATE_FLAGS wxTEMPLATE_VISIBLE
53 #define wxMAX_FILE_HISTORY 9
55 class WXDLLEXPORT wxDocument
: public wxEvtHandler
57 DECLARE_ABSTRACT_CLASS(wxDocument
)
59 wxDocument(wxDocument
*parent
= NULL
);
62 void SetFilename(const wxString
& filename
, bool notifyViews
= FALSE
);
63 inline wxString
GetFilename(void) const { return m_documentFile
; }
64 inline void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
65 inline wxString
GetTitle(void) const { return m_documentTitle
; }
66 inline void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
67 inline wxString
GetDocumentName(void) const { return m_documentTypeName
; }
68 // Has the document been saved yet?
69 inline bool GetDocumentSaved(void) { return m_savedYet
; }
70 inline void SetDocumentSaved(bool saved
= TRUE
) { m_savedYet
= saved
; }
72 virtual bool Close(void);
73 virtual bool Save(void);
74 virtual bool SaveAs(void);
75 virtual bool Revert(void);
77 virtual ostream
& SaveObject(ostream
& stream
);
78 virtual istream
& LoadObject(istream
& stream
);
80 // Called by wxWindows
81 virtual bool OnSaveDocument(const wxString
& filename
);
82 virtual bool OnOpenDocument(const wxString
& filename
);
83 virtual bool OnNewDocument(void);
84 virtual bool OnCloseDocument(void);
86 // Prompts for saving if about to close a modified document.
87 // Returns TRUE if ok to close the document (may have saved in the
88 // meantime, or set modified to FALSE)
89 virtual bool OnSaveModified(void);
91 // Called by framework if created automatically by the
92 // default document manager: gives document a chance to
93 // initialise and (usually) create a view
94 virtual bool OnCreate(const wxString
& path
, long flags
);
96 // By default, creates a base wxCommandProcessor.
97 virtual wxCommandProcessor
*OnCreateCommandProcessor(void);
98 virtual inline wxCommandProcessor
*GetCommandProcessor(void) const { return m_commandProcessor
; }
99 virtual inline void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
101 // Called after a view is added or removed.
102 // The default implementation deletes the document if this
103 // is there are no more views.
104 virtual void OnChangedViewList(void);
106 virtual bool DeleteContents(void);
108 virtual bool Draw(wxDC
&);
109 virtual inline bool IsModified(void) const { return m_documentModified
; }
110 virtual inline void Modify(bool mod
) { m_documentModified
= mod
; }
112 virtual bool AddView(wxView
*view
);
113 virtual bool RemoveView(wxView
*view
);
114 inline wxList
& GetViews(void) const { return (wxList
&) m_documentViews
; }
115 wxView
*GetFirstView(void) const;
117 virtual void UpdateAllViews(wxView
*sender
= NULL
, wxObject
*hint
= NULL
);
119 // Remove all views (because we're closing the document)
120 virtual bool DeleteAllViews(void);
123 virtual wxDocManager
*GetDocumentManager(void) const;
124 virtual inline wxDocTemplate
*GetDocumentTemplate(void) const { return m_documentTemplate
; }
125 virtual inline void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
127 // Get title, or filename if no title, else [unnamed]
128 virtual bool GetPrintableName(wxString
& buf
) const;
130 // Returns a window that can be used as a parent for document-related
131 // dialogs. Override if necessary.
132 virtual wxWindow
*GetDocumentWindow(void) const;
134 wxList m_documentViews
;
135 wxString m_documentFile
;
136 wxString m_documentTitle
;
137 wxString m_documentTypeName
;
138 wxDocTemplate
* m_documentTemplate
;
139 bool m_documentModified
;
140 wxDocument
* m_documentParent
;
141 wxCommandProcessor
* m_commandProcessor
;
145 class WXDLLEXPORT wxView
: public wxEvtHandler
147 DECLARE_ABSTRACT_CLASS(wxView
)
149 wxView(wxDocument
*doc
= NULL
);
152 inline wxDocument
*GetDocument(void) const { return m_viewDocument
; }
153 void SetDocument(wxDocument
*doc
);
155 inline wxString
GetViewName(void) const { return m_viewTypeName
; }
156 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
158 inline wxFrame
*GetFrame(void) const { return m_viewFrame
; }
159 inline void SetFrame(wxFrame
*frame
) { m_viewFrame
= frame
; }
161 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
162 virtual void OnDraw(wxDC
*dc
) = 0;
163 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
164 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= NULL
);
165 virtual void OnChangeFilename(void);
167 // Called by framework if created automatically by the
168 // default document manager class: gives view a chance to
170 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
172 // Checks if the view is the last one for the document; if so,
173 // asks user to confirm save data (if modified). If ok,
174 // deletes itself and returns TRUE.
175 virtual bool Close(bool deleteWindow
= TRUE
);
177 // Override to do cleanup/veto close
178 virtual bool OnClose(bool deleteWindow
);
179 // Defeat compiler warning
180 inline bool OnClose(void) { return wxEvtHandler::OnClose(); }
182 // Extend event processing to search the document's event table
183 virtual bool ProcessEvent(wxEvent
& event
);
185 // A view's window can call this to notify the view it is (in)active.
186 // The function then notifies the document manager.
187 virtual void Activate(bool activate
);
189 inline wxDocManager
*GetDocumentManager(void) const { return m_viewDocument
->GetDocumentManager(); }
191 #if USE_PRINTING_ARCHITECTURE
192 virtual wxPrintout
*OnCreatePrintout(void);
196 wxDocument
* m_viewDocument
;
197 wxString m_viewTypeName
;
198 wxFrame
* m_viewFrame
;
201 // Represents user interface (and other) properties of documents and views
202 class WXDLLEXPORT wxDocTemplate
: public wxObject
204 DECLARE_CLASS(wxDocTemplate
)
206 friend class WXDLLEXPORT wxDocManager
;
210 // Associate document and view types.
211 // They're for identifying what view is associated with what
212 // template/document type
213 wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
, const wxString
& filter
, const wxString
& dir
,
214 const wxString
& ext
, const wxString
& docTypeName
, const wxString
& viewTypeName
,
215 wxClassInfo
*docClassInfo
= NULL
, wxClassInfo
*viewClassInfo
= NULL
,
216 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
218 ~wxDocTemplate(void);
220 // By default, these two member functions dynamically creates document
221 // and view using dynamic instance construction.
222 // Override these if you need a different method of construction.
223 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
224 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
226 inline wxString
GetDefaultExtension(void) const { return m_defaultExt
; };
227 inline wxString
GetDescription(void) const { return m_description
; }
228 inline wxString
GetDirectory(void) const { return m_directory
; };
229 inline wxDocManager
*GetDocumentManager(void) const { return m_documentManager
; }
230 inline void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
231 inline wxString
GetFileFilter(void) const { return m_fileFilter
; };
232 inline long GetFlags(void) const { return m_flags
; };
233 virtual wxString
GetViewName(void) const { return m_viewTypeName
; }
234 virtual wxString
GetDocumentName(void) const { return m_docTypeName
; }
236 inline void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
237 inline void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
238 inline void SetDescription(const wxString
& descr
) { m_description
= descr
; };
239 inline void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
240 inline void SetFlags(long flags
) { m_flags
= flags
; };
242 inline bool IsVisible(void) const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
246 wxString m_fileFilter
;
247 wxString m_directory
;
248 wxString m_description
;
249 wxString m_defaultExt
;
250 wxString m_docTypeName
;
251 wxString m_viewTypeName
;
252 wxDocManager
* m_documentManager
;
254 // For dynamic creation of appropriate instances.
255 wxClassInfo
* m_docClassInfo
;
256 wxClassInfo
* m_viewClassInfo
;
260 // One object of this class may be created in an application,
261 // to manage all the templates and documents.
262 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
264 DECLARE_DYNAMIC_CLASS(wxDocManager
)
266 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
269 virtual bool Initialize(void);
271 // Handlers for common user commands
272 // virtual void OldOnMenuCommand(int command);
274 void OnFileClose(wxCommandEvent
& event
);
275 void OnFileNew(wxCommandEvent
& event
);
276 void OnFileOpen(wxCommandEvent
& event
);
277 void OnFileRevert(wxCommandEvent
& event
);
278 void OnFileSave(wxCommandEvent
& event
);
279 void OnFileSaveAs(wxCommandEvent
& event
);
280 void OnPrint(wxCommandEvent
& event
);
281 void OnPrintSetup(wxCommandEvent
& event
);
282 void OnPreview(wxCommandEvent
& event
);
283 void OnUndo(wxCommandEvent
& event
);
284 void OnRedo(wxCommandEvent
& event
);
286 // Extend event processing to search the view's event table
287 virtual bool ProcessEvent(wxEvent
& event
);
289 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
290 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
291 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
292 virtual bool FlushDoc(wxDocument
*doc
);
293 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
294 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
295 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
296 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
298 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
300 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
302 void AssociateTemplate(wxDocTemplate
*temp
);
303 void DisassociateTemplate(wxDocTemplate
*temp
);
305 wxDocument
*GetCurrentDocument(void) const;
307 inline void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
308 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen
; }
310 // Add and remove a document from the manager's list
311 void AddDocument(wxDocument
*doc
);
312 void RemoveDocument(wxDocument
*doc
);
314 // Clear remaining documents and templates
315 bool Clear(bool force
= TRUE
);
317 // Views or windows should inform the document manager
318 // when a view is going in or out of focus
319 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
320 virtual wxView
*GetCurrentView(void) const;
322 virtual inline wxList
& GetDocuments(void) const { return (wxList
&) m_docs
; }
324 // Make a default document name
325 virtual bool MakeDefaultName(wxString
& buf
);
327 virtual wxFileHistory
*OnCreateFileHistory(void);
328 virtual inline wxFileHistory
*GetFileHistory(void) const { return m_fileHistory
; }
330 // File history management
331 virtual void AddFileToHistory(const wxString
& file
);
332 virtual int GetNoHistoryFiles(void) const;
333 virtual wxString
GetHistoryFile(int i
) const;
334 virtual void FileHistoryUseMenu(wxMenu
*menu
);
335 virtual void FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
);
336 virtual void FileHistorySave(const wxString
& resourceFile
, const wxString
& section
);
339 int m_defaultDocumentNameCounter
;
343 wxView
* m_currentView
;
344 wxFileHistory
* m_fileHistory
;
346 DECLARE_EVENT_TABLE()
350 * A default child frame
353 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
355 DECLARE_CLASS(wxDocChildFrame
)
358 wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
359 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
360 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
361 ~wxDocChildFrame(void);
364 // Extend event processing to search the view's event table
365 virtual bool ProcessEvent(wxEvent
& event
);
367 // void OldOnMenuCommand(int id);
368 void OnActivate(wxActivateEvent
& event
);
370 inline wxDocument
*GetDocument(void) const { return m_childDocument
; }
371 inline wxView
*GetView(void) const { return m_childView
; }
372 inline void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
373 inline void SetView(wxView
*view
) { m_childView
= view
; }
375 wxDocument
* m_childDocument
;
378 DECLARE_EVENT_TABLE()
383 * A default parent frame
386 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
388 DECLARE_CLASS(wxDocParentFrame
)
390 wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
391 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
392 long type
= wxDEFAULT_FRAME
, const wxString
& name
= "frame");
395 // Extend event processing to search the document manager's event table
396 virtual bool ProcessEvent(wxEvent
& event
);
398 // void OldOnMenuCommand(int id);
399 wxDocManager
*GetDocumentManager(void) const { return m_docManager
; }
401 void OnExit(wxCommandEvent
& event
);
402 void OnMRUFile(wxCommandEvent
& event
);
405 wxDocManager
*m_docManager
;
407 DECLARE_EVENT_TABLE()
411 * Provide simple default printing facilities
414 #if USE_PRINTING_ARCHITECTURE
415 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
417 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
419 wxDocPrintout(wxView
*view
= NULL
, const wxString
& title
= "Printout");
420 bool OnPrintPage(int page
);
421 bool HasPage(int page
);
422 bool OnBeginDocument(int startPage
, int endPage
);
423 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
425 virtual inline wxView
*GetView(void) { return m_printoutView
; }
427 wxView
* m_printoutView
;
432 * Command processing framework
435 class WXDLLEXPORT wxCommand
: public wxObject
437 DECLARE_CLASS(wxCommand
)
439 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
442 // Override this to perform a command
443 virtual bool Do(void) = 0;
445 // Override this to undo a command
446 virtual bool Undo(void) = 0;
448 virtual inline bool CanUndo(void) const { return m_canUndo
; }
449 virtual inline wxString
GetName(void) const { return m_commandName
; }
452 wxString m_commandName
;
455 class WXDLLEXPORT wxCommandProcessor
: public wxObject
457 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
459 wxCommandProcessor(int maxCommands
= 100);
460 ~wxCommandProcessor(void);
462 // Pass a command to the processor. The processor calls Do();
463 // if successful, is appended to the command history unless
465 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
466 virtual bool Undo(void);
467 virtual bool Redo(void);
468 virtual bool CanUndo(void);
470 // Call this to manage an edit menu.
471 inline void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
472 inline wxMenu
*GetEditMenu(void) const { return m_commandEditMenu
; }
473 virtual void SetMenuStrings(void);
474 virtual void Initialize(void);
476 inline wxList
& GetCommands(void) const { return (wxList
&) m_commands
; }
477 inline int GetMaxCommands(void) const { return m_maxNoCommands
; }
478 virtual void ClearCommands(void);
483 wxNode
* m_currentCommand
;
484 wxMenu
* m_commandEditMenu
;
487 class WXDLLEXPORT wxFileHistory
: public wxObject
489 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
491 wxFileHistory(int maxFiles
= 9);
492 ~wxFileHistory(void);
494 // File history management
495 virtual void AddFileToHistory(const wxString
& file
);
496 inline virtual int GetNoHistoryFiles(void) const { return m_fileHistoryN
; }
497 virtual wxString
GetHistoryFile(int i
) const;
498 virtual int GetMaxFiles(void) const { return m_fileMaxFiles
; }
499 virtual void FileHistoryUseMenu(wxMenu
*menu
);
500 virtual void FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
);
501 virtual void FileHistorySave(const wxString
& resourceFile
, const wxString
& section
);
504 char** m_fileHistory
;
505 // Number of files saved
509 // Max files to maintain
513 // For compatibility with existing file formats:
514 // converts from/to a stream to/from a temporary file.
515 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
516 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);