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 inline void SetFrame(wxMDIChildFrame
*frame
) { m_viewFrame
= (wxFrame
*)frame
; }
164 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
165 virtual void OnDraw(wxDC
*dc
) = 0;
166 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
167 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= NULL
);
168 virtual void OnChangeFilename(void);
170 // Called by framework if created automatically by the
171 // default document manager class: gives view a chance to
173 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
175 // Checks if the view is the last one for the document; if so,
176 // asks user to confirm save data (if modified). If ok,
177 // deletes itself and returns TRUE.
178 virtual bool Close(bool deleteWindow
= TRUE
);
180 // Override to do cleanup/veto close
181 virtual bool OnClose(bool deleteWindow
);
182 // Defeat compiler warning
183 inline bool OnClose(void) { return wxEvtHandler::OnClose(); }
185 // Extend event processing to search the document's event table
186 virtual bool ProcessEvent(wxEvent
& event
);
188 // A view's window can call this to notify the view it is (in)active.
189 // The function then notifies the document manager.
190 virtual void Activate(bool activate
);
192 inline wxDocManager
*GetDocumentManager(void) const { return m_viewDocument
->GetDocumentManager(); }
194 #if USE_PRINTING_ARCHITECTURE
195 virtual wxPrintout
*OnCreatePrintout(void);
199 wxDocument
* m_viewDocument
;
200 wxString m_viewTypeName
;
201 wxFrame
* m_viewFrame
;
204 // Represents user interface (and other) properties of documents and views
205 class WXDLLEXPORT wxDocTemplate
: public wxObject
207 DECLARE_CLASS(wxDocTemplate
)
209 friend class WXDLLEXPORT wxDocManager
;
213 // Associate document and view types.
214 // They're for identifying what view is associated with what
215 // template/document type
216 wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
, const wxString
& filter
, const wxString
& dir
,
217 const wxString
& ext
, const wxString
& docTypeName
, const wxString
& viewTypeName
,
218 wxClassInfo
*docClassInfo
= NULL
, wxClassInfo
*viewClassInfo
= NULL
,
219 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
221 ~wxDocTemplate(void);
223 // By default, these two member functions dynamically creates document
224 // and view using dynamic instance construction.
225 // Override these if you need a different method of construction.
226 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
227 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
229 inline wxString
GetDefaultExtension(void) const { return m_defaultExt
; };
230 inline wxString
GetDescription(void) const { return m_description
; }
231 inline wxString
GetDirectory(void) const { return m_directory
; };
232 inline wxDocManager
*GetDocumentManager(void) const { return m_documentManager
; }
233 inline void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
234 inline wxString
GetFileFilter(void) const { return m_fileFilter
; };
235 inline long GetFlags(void) const { return m_flags
; };
236 virtual wxString
GetViewName(void) const { return m_viewTypeName
; }
237 virtual wxString
GetDocumentName(void) const { return m_docTypeName
; }
239 inline void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
240 inline void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
241 inline void SetDescription(const wxString
& descr
) { m_description
= descr
; };
242 inline void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
243 inline void SetFlags(long flags
) { m_flags
= flags
; };
245 inline bool IsVisible(void) const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
249 wxString m_fileFilter
;
250 wxString m_directory
;
251 wxString m_description
;
252 wxString m_defaultExt
;
253 wxString m_docTypeName
;
254 wxString m_viewTypeName
;
255 wxDocManager
* m_documentManager
;
257 // For dynamic creation of appropriate instances.
258 wxClassInfo
* m_docClassInfo
;
259 wxClassInfo
* m_viewClassInfo
;
263 // One object of this class may be created in an application,
264 // to manage all the templates and documents.
265 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
267 DECLARE_DYNAMIC_CLASS(wxDocManager
)
269 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
272 virtual bool Initialize(void);
274 // Handlers for common user commands
275 // virtual void OldOnMenuCommand(int command);
277 void OnFileClose(wxCommandEvent
& event
);
278 void OnFileNew(wxCommandEvent
& event
);
279 void OnFileOpen(wxCommandEvent
& event
);
280 void OnFileRevert(wxCommandEvent
& event
);
281 void OnFileSave(wxCommandEvent
& event
);
282 void OnFileSaveAs(wxCommandEvent
& event
);
283 void OnPrint(wxCommandEvent
& event
);
284 void OnPrintSetup(wxCommandEvent
& event
);
285 void OnPreview(wxCommandEvent
& event
);
286 void OnUndo(wxCommandEvent
& event
);
287 void OnRedo(wxCommandEvent
& event
);
289 // Extend event processing to search the view's event table
290 virtual bool ProcessEvent(wxEvent
& event
);
292 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
293 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
294 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
295 virtual bool FlushDoc(wxDocument
*doc
);
296 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
297 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
298 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
299 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
301 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
303 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
305 void AssociateTemplate(wxDocTemplate
*temp
);
306 void DisassociateTemplate(wxDocTemplate
*temp
);
308 wxDocument
*GetCurrentDocument(void) const;
310 inline void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
311 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen
; }
313 // Add and remove a document from the manager's list
314 void AddDocument(wxDocument
*doc
);
315 void RemoveDocument(wxDocument
*doc
);
317 // Clear remaining documents and templates
318 bool Clear(bool force
= TRUE
);
320 // Views or windows should inform the document manager
321 // when a view is going in or out of focus
322 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
323 virtual wxView
*GetCurrentView(void) const;
325 virtual inline wxList
& GetDocuments(void) const { return (wxList
&) m_docs
; }
327 // Make a default document name
328 virtual bool MakeDefaultName(wxString
& buf
);
330 virtual wxFileHistory
*OnCreateFileHistory(void);
331 virtual inline wxFileHistory
*GetFileHistory(void) const { return m_fileHistory
; }
333 // File history management
334 virtual void AddFileToHistory(const wxString
& file
);
335 virtual int GetNoHistoryFiles(void) const;
336 virtual wxString
GetHistoryFile(int i
) const;
337 virtual void FileHistoryUseMenu(wxMenu
*menu
);
338 virtual void FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
);
339 virtual void FileHistorySave(const wxString
& resourceFile
, const wxString
& section
);
342 int m_defaultDocumentNameCounter
;
346 wxView
* m_currentView
;
347 wxFileHistory
* m_fileHistory
;
349 DECLARE_EVENT_TABLE()
353 * A default child frame
356 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
358 DECLARE_CLASS(wxDocChildFrame
)
361 wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
362 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
363 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
364 ~wxDocChildFrame(void);
367 // Extend event processing to search the view's event table
368 virtual bool ProcessEvent(wxEvent
& event
);
370 // void OldOnMenuCommand(int id);
371 void OnActivate(wxActivateEvent
& event
);
373 inline wxDocument
*GetDocument(void) const { return m_childDocument
; }
374 inline wxView
*GetView(void) const { return m_childView
; }
375 inline void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
376 inline void SetView(wxView
*view
) { m_childView
= view
; }
378 wxDocument
* m_childDocument
;
381 DECLARE_EVENT_TABLE()
386 * A default parent frame
389 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
391 DECLARE_CLASS(wxDocParentFrame
)
393 wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
394 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
395 long type
= wxDEFAULT_FRAME
, const wxString
& name
= "frame");
398 // Extend event processing to search the document manager's event table
399 virtual bool ProcessEvent(wxEvent
& event
);
401 // void OldOnMenuCommand(int id);
402 wxDocManager
*GetDocumentManager(void) const { return m_docManager
; }
404 void OnExit(wxCommandEvent
& event
);
405 void OnMRUFile(wxCommandEvent
& event
);
408 wxDocManager
*m_docManager
;
410 DECLARE_EVENT_TABLE()
414 * Provide simple default printing facilities
417 #if USE_PRINTING_ARCHITECTURE
418 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
420 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
422 wxDocPrintout(wxView
*view
= NULL
, const wxString
& title
= "Printout");
423 bool OnPrintPage(int page
);
424 bool HasPage(int page
);
425 bool OnBeginDocument(int startPage
, int endPage
);
426 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
428 virtual inline wxView
*GetView(void) { return m_printoutView
; }
430 wxView
* m_printoutView
;
435 * Command processing framework
438 class WXDLLEXPORT wxCommand
: public wxObject
440 DECLARE_CLASS(wxCommand
)
442 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
445 // Override this to perform a command
446 virtual bool Do(void) = 0;
448 // Override this to undo a command
449 virtual bool Undo(void) = 0;
451 virtual inline bool CanUndo(void) const { return m_canUndo
; }
452 virtual inline wxString
GetName(void) const { return m_commandName
; }
455 wxString m_commandName
;
458 class WXDLLEXPORT wxCommandProcessor
: public wxObject
460 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
462 wxCommandProcessor(int maxCommands
= 100);
463 ~wxCommandProcessor(void);
465 // Pass a command to the processor. The processor calls Do();
466 // if successful, is appended to the command history unless
468 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
469 virtual bool Undo(void);
470 virtual bool Redo(void);
471 virtual bool CanUndo(void);
473 // Call this to manage an edit menu.
474 inline void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
475 inline wxMenu
*GetEditMenu(void) const { return m_commandEditMenu
; }
476 virtual void SetMenuStrings(void);
477 virtual void Initialize(void);
479 inline wxList
& GetCommands(void) const { return (wxList
&) m_commands
; }
480 inline int GetMaxCommands(void) const { return m_maxNoCommands
; }
481 virtual void ClearCommands(void);
486 wxNode
* m_currentCommand
;
487 wxMenu
* m_commandEditMenu
;
490 class WXDLLEXPORT wxFileHistory
: public wxObject
492 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
494 wxFileHistory(int maxFiles
= 9);
495 ~wxFileHistory(void);
497 // File history management
498 virtual void AddFileToHistory(const wxString
& file
);
499 inline virtual int GetNoHistoryFiles(void) const { return m_fileHistoryN
; }
500 virtual wxString
GetHistoryFile(int i
) const;
501 virtual int GetMaxFiles(void) const { return m_fileMaxFiles
; }
502 virtual void FileHistoryUseMenu(wxMenu
*menu
);
503 virtual void FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
);
504 virtual void FileHistorySave(const wxString
& resourceFile
, const wxString
& section
);
507 char** m_fileHistory
;
508 // Number of files saved
512 // Max files to maintain
516 // For compatibility with existing file formats:
517 // converts from/to a stream to/from a temporary file.
518 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
519 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);