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 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
287 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
288 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
289 virtual bool FlushDoc(wxDocument
*doc
);
290 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
291 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
292 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
293 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
295 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
297 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
299 void AssociateTemplate(wxDocTemplate
*temp
);
300 void DisassociateTemplate(wxDocTemplate
*temp
);
302 wxDocument
*GetCurrentDocument(void) const;
304 inline void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
305 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen
; }
307 // Add and remove a document from the manager's list
308 void AddDocument(wxDocument
*doc
);
309 void RemoveDocument(wxDocument
*doc
);
311 // Clear remaining documents and templates
312 bool Clear(bool force
= TRUE
);
314 // Views or windows should inform the document manager
315 // when a view is going in or out of focus
316 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
317 virtual inline wxView
*GetCurrentView(void) const { return m_currentView
; }
319 virtual inline wxList
& GetDocuments(void) const { return (wxList
&) m_docs
; }
321 // Make a default document name
322 virtual bool MakeDefaultName(wxString
& buf
);
324 virtual wxFileHistory
*OnCreateFileHistory(void);
325 virtual inline wxFileHistory
*GetFileHistory(void) const { return m_fileHistory
; }
327 // File history management
328 virtual void AddFileToHistory(const wxString
& file
);
329 virtual int GetNoHistoryFiles(void) const;
330 virtual wxString
GetHistoryFile(int i
) const;
331 virtual void FileHistoryUseMenu(wxMenu
*menu
);
332 virtual void FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
);
333 virtual void FileHistorySave(const wxString
& resourceFile
, const wxString
& section
);
336 int m_defaultDocumentNameCounter
;
340 wxView
* m_currentView
;
341 wxFileHistory
* m_fileHistory
;
343 DECLARE_EVENT_TABLE()
347 * A default child frame
350 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
352 DECLARE_CLASS(wxDocChildFrame
)
355 wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, const wxString
& title
,
356 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
357 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
358 ~wxDocChildFrame(void);
361 // Extend event processing to search the view's event table
362 virtual bool ProcessEvent(wxEvent
& event
);
364 // void OldOnMenuCommand(int id);
365 void OnActivate(wxActivateEvent
& event
);
367 inline wxDocument
*GetDocument(void) const { return m_childDocument
; }
368 inline wxView
*GetView(void) const { return m_childView
; }
369 inline void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
370 inline void SetView(wxView
*view
) { m_childView
= view
; }
372 wxDocument
* m_childDocument
;
375 DECLARE_EVENT_TABLE()
380 * A default parent frame
383 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
385 DECLARE_CLASS(wxDocParentFrame
)
387 wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, const wxString
& title
,
388 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
389 long type
= wxDEFAULT_FRAME
, const wxString
& name
= "frame");
392 // Extend event processing to search the document manager's event table
393 virtual bool ProcessEvent(wxEvent
& event
);
395 // void OldOnMenuCommand(int id);
396 wxDocManager
*GetDocumentManager(void) const { return m_docManager
; }
398 void OnExit(wxCommandEvent
& event
);
399 void OnMRUFile(wxCommandEvent
& event
);
402 wxDocManager
*m_docManager
;
404 DECLARE_EVENT_TABLE()
408 * Provide simple default printing facilities
411 #if USE_PRINTING_ARCHITECTURE
412 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
414 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
416 wxDocPrintout(wxView
*view
= NULL
, const wxString
& title
= "Printout");
417 bool OnPrintPage(int page
);
418 bool HasPage(int page
);
419 bool OnBeginDocument(int startPage
, int endPage
);
420 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
422 virtual inline wxView
*GetView(void) { return m_printoutView
; }
424 wxView
* m_printoutView
;
429 * Command processing framework
432 class WXDLLEXPORT wxCommand
: public wxObject
434 DECLARE_CLASS(wxCommand
)
436 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
439 // Override this to perform a command
440 virtual bool Do(void) = 0;
442 // Override this to undo a command
443 virtual bool Undo(void) = 0;
445 virtual inline bool CanUndo(void) const { return m_canUndo
; }
446 virtual inline wxString
GetName(void) const { return m_commandName
; }
449 wxString m_commandName
;
452 class WXDLLEXPORT wxCommandProcessor
: public wxObject
454 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
456 wxCommandProcessor(int maxCommands
= 100);
457 ~wxCommandProcessor(void);
459 // Pass a command to the processor. The processor calls Do();
460 // if successful, is appended to the command history unless
462 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
463 virtual bool Undo(void);
464 virtual bool Redo(void);
465 virtual bool CanUndo(void);
467 // Call this to manage an edit menu.
468 inline void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
469 inline wxMenu
*GetEditMenu(void) const { return m_commandEditMenu
; }
470 virtual void SetMenuStrings(void);
471 virtual void Initialize(void);
473 inline wxList
& GetCommands(void) const { return (wxList
&) m_commands
; }
474 inline int GetMaxCommands(void) const { return m_maxNoCommands
; }
475 virtual void ClearCommands(void);
480 wxNode
* m_currentCommand
;
481 wxMenu
* m_commandEditMenu
;
484 class WXDLLEXPORT wxFileHistory
: public wxObject
486 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
488 wxFileHistory(int maxFiles
= 9);
489 ~wxFileHistory(void);
491 // File history management
492 virtual void AddFileToHistory(const wxString
& file
);
493 inline virtual int GetNoHistoryFiles(void) const { return m_fileHistoryN
; }
494 virtual wxString
GetHistoryFile(int i
) const;
495 virtual int GetMaxFiles(void) const { return m_fileMaxFiles
; }
496 virtual void FileHistoryUseMenu(wxMenu
*menu
);
497 virtual void FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
);
498 virtual void FileHistorySave(const wxString
& resourceFile
, const wxString
& section
);
501 char** m_fileHistory
;
502 // Number of files saved
506 // Max files to maintain
510 // For compatibility with existing file formats:
511 // converts from/to a stream to/from a temporary file.
512 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
513 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);