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
;
37 class WXDLLEXPORT wxConfigBase
;
39 class WXDLLIMPORT ostream
;
40 class WXDLLIMPORT istream
;
42 // Document manager flags
46 #define wxDOC_SILENT 8
47 #define wxDEFAULT_DOCMAN_FLAGS wxDOC_SDI
49 // Document template flags
50 #define wxTEMPLATE_VISIBLE 1
51 #define wxTEMPLATE_INVISIBLE 2
52 #define wxDEFAULT_TEMPLATE_FLAGS wxTEMPLATE_VISIBLE
54 #define wxMAX_FILE_HISTORY 9
56 class WXDLLEXPORT wxDocument
: public wxEvtHandler
58 DECLARE_ABSTRACT_CLASS(wxDocument
)
60 wxDocument(wxDocument
*parent
= (wxDocument
*) NULL
);
63 void SetFilename(const wxString
& filename
, bool notifyViews
= FALSE
);
64 inline wxString
GetFilename(void) const { return m_documentFile
; }
65 inline void SetTitle(const wxString
& title
) { m_documentTitle
= title
; };
66 inline wxString
GetTitle(void) const { return m_documentTitle
; }
67 inline void SetDocumentName(const wxString
& name
) { m_documentTypeName
= name
; };
68 inline wxString
GetDocumentName(void) const { return m_documentTypeName
; }
69 // Has the document been saved yet?
70 inline bool GetDocumentSaved(void) { return m_savedYet
; }
71 inline void SetDocumentSaved(bool saved
= TRUE
) { m_savedYet
= saved
; }
73 virtual bool Close(void);
74 virtual bool Save(void);
75 virtual bool SaveAs(void);
76 virtual bool Revert(void);
78 virtual ostream
& SaveObject(ostream
& stream
);
79 virtual istream
& LoadObject(istream
& stream
);
81 // Called by wxWindows
82 virtual bool OnSaveDocument(const wxString
& filename
);
83 virtual bool OnOpenDocument(const wxString
& filename
);
84 virtual bool OnNewDocument(void);
85 virtual bool OnCloseDocument(void);
87 // Prompts for saving if about to close a modified document.
88 // Returns TRUE if ok to close the document (may have saved in the
89 // meantime, or set modified to FALSE)
90 virtual bool OnSaveModified(void);
92 // Called by framework if created automatically by the
93 // default document manager: gives document a chance to
94 // initialise and (usually) create a view
95 virtual bool OnCreate(const wxString
& path
, long flags
);
97 // By default, creates a base wxCommandProcessor.
98 virtual wxCommandProcessor
*OnCreateCommandProcessor(void);
99 virtual inline wxCommandProcessor
*GetCommandProcessor(void) const { return m_commandProcessor
; }
100 virtual inline void SetCommandProcessor(wxCommandProcessor
*proc
) { m_commandProcessor
= proc
; }
102 // Called after a view is added or removed.
103 // The default implementation deletes the document if this
104 // is there are no more views.
105 virtual void OnChangedViewList(void);
107 virtual bool DeleteContents(void);
109 virtual bool Draw(wxDC
&);
110 virtual inline bool IsModified(void) const { return m_documentModified
; }
111 virtual inline void Modify(bool mod
) { m_documentModified
= mod
; }
113 virtual bool AddView(wxView
*view
);
114 virtual bool RemoveView(wxView
*view
);
115 inline wxList
& GetViews(void) const { return (wxList
&) m_documentViews
; }
116 wxView
*GetFirstView(void) const;
118 virtual void UpdateAllViews(wxView
*sender
= (wxView
*) NULL
, wxObject
*hint
= (wxObject
*) NULL
);
120 // Remove all views (because we're closing the document)
121 virtual bool DeleteAllViews(void);
124 virtual wxDocManager
*GetDocumentManager(void) const;
125 virtual inline wxDocTemplate
*GetDocumentTemplate(void) const { return m_documentTemplate
; }
126 virtual inline void SetDocumentTemplate(wxDocTemplate
*temp
) { m_documentTemplate
= temp
; }
128 // Get title, or filename if no title, else [unnamed]
129 virtual bool GetPrintableName(wxString
& buf
) const;
131 // Returns a window that can be used as a parent for document-related
132 // dialogs. Override if necessary.
133 virtual wxWindow
*GetDocumentWindow(void) const;
135 wxList m_documentViews
;
136 wxString m_documentFile
;
137 wxString m_documentTitle
;
138 wxString m_documentTypeName
;
139 wxDocTemplate
* m_documentTemplate
;
140 bool m_documentModified
;
141 wxDocument
* m_documentParent
;
142 wxCommandProcessor
* m_commandProcessor
;
146 class WXDLLEXPORT wxView
: public wxEvtHandler
148 DECLARE_ABSTRACT_CLASS(wxView
)
150 wxView(wxDocument
*doc
= (wxDocument
*) NULL
);
153 inline wxDocument
*GetDocument(void) const { return m_viewDocument
; }
154 void SetDocument(wxDocument
*doc
);
156 inline wxString
GetViewName(void) const { return m_viewTypeName
; }
157 void SetViewName(const wxString
& name
) { m_viewTypeName
= name
; };
159 inline wxFrame
*GetFrame(void) const { return m_viewFrame
; }
160 inline void SetFrame(wxFrame
*frame
) { m_viewFrame
= frame
; }
162 virtual void OnActivateView(bool activate
, wxView
*activeView
, wxView
*deactiveView
);
163 virtual void OnDraw(wxDC
*dc
) = 0;
164 virtual void OnPrint(wxDC
*dc
, wxObject
*info
);
165 virtual void OnUpdate(wxView
*sender
, wxObject
*hint
= (wxObject
*) NULL
);
166 virtual void OnChangeFilename(void);
168 // Called by framework if created automatically by the
169 // default document manager class: gives view a chance to
171 virtual bool OnCreate(wxDocument
*WXUNUSED(doc
), long WXUNUSED(flags
)) { return TRUE
; };
173 // Checks if the view is the last one for the document; if so,
174 // asks user to confirm save data (if modified). If ok,
175 // deletes itself and returns TRUE.
176 virtual bool Close(bool deleteWindow
= TRUE
);
178 // Override to do cleanup/veto close
179 virtual bool OnClose(bool deleteWindow
);
180 // Defeat compiler warning
181 inline bool OnClose(void) { return wxEvtHandler::OnClose(); }
183 // Extend event processing to search the document's event table
184 virtual bool ProcessEvent(wxEvent
& event
);
186 // A view's window can call this to notify the view it is (in)active.
187 // The function then notifies the document manager.
188 virtual void Activate(bool activate
);
190 inline wxDocManager
*GetDocumentManager(void) const { return m_viewDocument
->GetDocumentManager(); }
192 #if USE_PRINTING_ARCHITECTURE
193 virtual wxPrintout
*OnCreatePrintout(void);
197 wxDocument
* m_viewDocument
;
198 wxString m_viewTypeName
;
199 wxFrame
* m_viewFrame
;
202 // Represents user interface (and other) properties of documents and views
203 class WXDLLEXPORT wxDocTemplate
: public wxObject
205 DECLARE_CLASS(wxDocTemplate
)
207 friend class WXDLLEXPORT wxDocManager
;
211 // Associate document and view types.
212 // They're for identifying what view is associated with what
213 // template/document type
214 wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
, const wxString
& filter
, const wxString
& dir
,
215 const wxString
& ext
, const wxString
& docTypeName
, const wxString
& viewTypeName
,
216 wxClassInfo
*docClassInfo
= (wxClassInfo
*) NULL
, wxClassInfo
*viewClassInfo
= (wxClassInfo
*)NULL
,
217 long flags
= wxDEFAULT_TEMPLATE_FLAGS
);
219 ~wxDocTemplate(void);
221 // By default, these two member functions dynamically creates document
222 // and view using dynamic instance construction.
223 // Override these if you need a different method of construction.
224 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
225 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
227 inline wxString
GetDefaultExtension(void) const { return m_defaultExt
; };
228 inline wxString
GetDescription(void) const { return m_description
; }
229 inline wxString
GetDirectory(void) const { return m_directory
; };
230 inline wxDocManager
*GetDocumentManager(void) const { return m_documentManager
; }
231 inline void SetDocumentManager(wxDocManager
*manager
) { m_documentManager
= manager
; }
232 inline wxString
GetFileFilter(void) const { return m_fileFilter
; };
233 inline long GetFlags(void) const { return m_flags
; };
234 virtual wxString
GetViewName(void) const { return m_viewTypeName
; }
235 virtual wxString
GetDocumentName(void) const { return m_docTypeName
; }
237 inline void SetFileFilter(const wxString
& filter
) { m_fileFilter
= filter
; };
238 inline void SetDirectory(const wxString
& dir
) { m_directory
= dir
; };
239 inline void SetDescription(const wxString
& descr
) { m_description
= descr
; };
240 inline void SetDefaultExtension(const wxString
& ext
) { m_defaultExt
= ext
; };
241 inline void SetFlags(long flags
) { m_flags
= flags
; };
243 inline bool IsVisible(void) const { return ((m_flags
& wxTEMPLATE_VISIBLE
) == wxTEMPLATE_VISIBLE
); }
247 wxString m_fileFilter
;
248 wxString m_directory
;
249 wxString m_description
;
250 wxString m_defaultExt
;
251 wxString m_docTypeName
;
252 wxString m_viewTypeName
;
253 wxDocManager
* m_documentManager
;
255 // For dynamic creation of appropriate instances.
256 wxClassInfo
* m_docClassInfo
;
257 wxClassInfo
* m_viewClassInfo
;
261 // One object of this class may be created in an application,
262 // to manage all the templates and documents.
263 class WXDLLEXPORT wxDocManager
: public wxEvtHandler
265 DECLARE_DYNAMIC_CLASS(wxDocManager
)
267 wxDocManager(long flags
= wxDEFAULT_DOCMAN_FLAGS
, bool initialize
= TRUE
);
270 virtual bool Initialize(void);
272 // Handlers for common user commands
273 // virtual void OldOnMenuCommand(int command);
275 void OnFileClose(wxCommandEvent
& event
);
276 void OnFileNew(wxCommandEvent
& event
);
277 void OnFileOpen(wxCommandEvent
& event
);
278 void OnFileRevert(wxCommandEvent
& event
);
279 void OnFileSave(wxCommandEvent
& event
);
280 void OnFileSaveAs(wxCommandEvent
& event
);
281 void OnPrint(wxCommandEvent
& event
);
282 void OnPrintSetup(wxCommandEvent
& event
);
283 void OnPreview(wxCommandEvent
& event
);
284 void OnUndo(wxCommandEvent
& event
);
285 void OnRedo(wxCommandEvent
& event
);
287 // Extend event processing to search the view's event table
288 virtual bool ProcessEvent(wxEvent
& event
);
290 virtual wxDocument
*CreateDocument(const wxString
& path
, long flags
= 0);
291 virtual wxView
*CreateView(wxDocument
*doc
, long flags
= 0);
292 virtual void DeleteTemplate(wxDocTemplate
*temp
, long flags
= 0);
293 virtual bool FlushDoc(wxDocument
*doc
);
294 virtual wxDocTemplate
*MatchTemplate(const wxString
& path
);
295 virtual wxDocTemplate
*SelectDocumentPath(wxDocTemplate
**templates
,
296 int noTemplates
, wxString
& path
, long flags
, bool save
= FALSE
);
297 virtual wxDocTemplate
*SelectDocumentType(wxDocTemplate
**templates
,
299 virtual wxDocTemplate
*SelectViewType(wxDocTemplate
**templates
,
301 virtual wxDocTemplate
*FindTemplateForPath(const wxString
& path
);
303 void AssociateTemplate(wxDocTemplate
*temp
);
304 void DisassociateTemplate(wxDocTemplate
*temp
);
306 wxDocument
*GetCurrentDocument(void) const;
308 inline void SetMaxDocsOpen(int n
) { m_maxDocsOpen
= n
; }
309 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen
; }
311 // Add and remove a document from the manager's list
312 void AddDocument(wxDocument
*doc
);
313 void RemoveDocument(wxDocument
*doc
);
315 // Clear remaining documents and templates
316 bool Clear(bool force
= TRUE
);
318 // Views or windows should inform the document manager
319 // when a view is going in or out of focus
320 virtual void ActivateView(wxView
*view
, bool activate
= TRUE
, bool deleting
= FALSE
);
321 virtual wxView
*GetCurrentView(void) const;
323 virtual inline wxList
& GetDocuments(void) const { return (wxList
&) m_docs
; }
325 // Make a default document name
326 virtual bool MakeDefaultName(wxString
& buf
);
328 virtual wxFileHistory
*OnCreateFileHistory(void);
329 virtual inline wxFileHistory
*GetFileHistory(void) const { return m_fileHistory
; }
331 // File history management
332 virtual void AddFileToHistory(const wxString
& file
);
333 virtual int GetNoHistoryFiles(void) const;
334 virtual wxString
GetHistoryFile(int i
) const;
335 virtual void FileHistoryUseMenu(wxMenu
*menu
);
336 virtual void FileHistoryRemoveMenu(wxMenu
*menu
);
337 virtual void FileHistoryLoad(wxConfigBase
& config
);
338 virtual void FileHistorySave(wxConfigBase
& config
);
339 virtual void FileHistoryAddFilesToMenu();
340 virtual void FileHistoryAddFilesToMenu(wxMenu
* menu
);
343 int m_defaultDocumentNameCounter
;
347 wxView
* m_currentView
;
348 wxFileHistory
* m_fileHistory
;
350 DECLARE_EVENT_TABLE()
354 * A default child frame
357 class WXDLLEXPORT wxDocChildFrame
: public wxFrame
359 DECLARE_CLASS(wxDocChildFrame
)
362 wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
363 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
364 long type
= wxDEFAULT_FRAME_STYLE
, const wxString
& name
= "frame");
365 ~wxDocChildFrame(void);
368 // Extend event processing to search the view's event table
369 virtual bool ProcessEvent(wxEvent
& event
);
371 // void OldOnMenuCommand(int id);
372 void OnActivate(wxActivateEvent
& event
);
374 inline wxDocument
*GetDocument(void) const { return m_childDocument
; }
375 inline wxView
*GetView(void) const { return m_childView
; }
376 inline void SetDocument(wxDocument
*doc
) { m_childDocument
= doc
; }
377 inline void SetView(wxView
*view
) { m_childView
= view
; }
379 wxDocument
* m_childDocument
;
382 DECLARE_EVENT_TABLE()
387 * A default parent frame
390 class WXDLLEXPORT wxDocParentFrame
: public wxFrame
392 DECLARE_CLASS(wxDocParentFrame
)
394 wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
395 const wxPoint
& pos
= wxDefaultPosition
, const wxSize
& size
= wxDefaultSize
,
396 long type
= wxDEFAULT_FRAME
, const wxString
& name
= "frame");
399 // Extend event processing to search the document manager's event table
400 virtual bool ProcessEvent(wxEvent
& event
);
402 // void OldOnMenuCommand(int id);
403 wxDocManager
*GetDocumentManager(void) const { return m_docManager
; }
405 void OnExit(wxCommandEvent
& event
);
406 void OnMRUFile(wxCommandEvent
& event
);
409 wxDocManager
*m_docManager
;
411 DECLARE_EVENT_TABLE()
415 * Provide simple default printing facilities
418 #if USE_PRINTING_ARCHITECTURE
419 class WXDLLEXPORT wxDocPrintout
: public wxPrintout
421 DECLARE_DYNAMIC_CLASS(wxDocPrintout
)
423 wxDocPrintout(wxView
*view
= (wxView
*) NULL
, const wxString
& title
= "Printout");
424 bool OnPrintPage(int page
);
425 bool HasPage(int page
);
426 bool OnBeginDocument(int startPage
, int endPage
);
427 void GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
);
429 virtual inline wxView
*GetView(void) { return m_printoutView
; }
431 wxView
* m_printoutView
;
436 * Command processing framework
439 class WXDLLEXPORT wxCommand
: public wxObject
441 DECLARE_CLASS(wxCommand
)
443 wxCommand(bool canUndoIt
= FALSE
, const wxString
& name
= "");
446 // Override this to perform a command
447 virtual bool Do(void) = 0;
449 // Override this to undo a command
450 virtual bool Undo(void) = 0;
452 virtual inline bool CanUndo(void) const { return m_canUndo
; }
453 virtual inline wxString
GetName(void) const { return m_commandName
; }
456 wxString m_commandName
;
459 class WXDLLEXPORT wxCommandProcessor
: public wxObject
461 DECLARE_DYNAMIC_CLASS(wxCommandProcessor
)
463 wxCommandProcessor(int maxCommands
= 100);
464 ~wxCommandProcessor(void);
466 // Pass a command to the processor. The processor calls Do();
467 // if successful, is appended to the command history unless
469 virtual bool Submit(wxCommand
*command
, bool storeIt
= TRUE
);
470 virtual bool Undo(void);
471 virtual bool Redo(void);
472 virtual bool CanUndo(void) const;
473 virtual bool CanRedo(void) const;
475 // Call this to manage an edit menu.
476 inline void SetEditMenu(wxMenu
*menu
) { m_commandEditMenu
= menu
; }
477 inline wxMenu
*GetEditMenu(void) const { return m_commandEditMenu
; }
478 virtual void SetMenuStrings(void);
479 virtual void Initialize(void);
481 inline wxList
& GetCommands(void) const { return (wxList
&) m_commands
; }
482 inline int GetMaxCommands(void) const { return m_maxNoCommands
; }
483 virtual void ClearCommands(void);
488 wxNode
* m_currentCommand
;
489 wxMenu
* m_commandEditMenu
;
492 // File history management
494 class WXDLLEXPORT wxFileHistory
: public wxObject
496 DECLARE_DYNAMIC_CLASS(wxFileHistory
)
498 wxFileHistory(int maxFiles
= 9);
499 ~wxFileHistory(void);
502 virtual void AddFileToHistory(const wxString
& file
);
503 virtual int GetMaxFiles(void) const { return m_fileMaxFiles
; }
504 virtual void UseMenu(wxMenu
*menu
);
506 // Remove menu from the list (MDI child may be closing)
507 virtual void RemoveMenu(wxMenu
*menu
);
509 virtual void Load(wxConfigBase
& config
);
510 virtual void Save(wxConfigBase
& config
);
512 virtual void AddFilesToMenu();
513 virtual void AddFilesToMenu(wxMenu
* menu
); // Single menu
516 virtual wxString
GetHistoryFile(int i
) const;
518 // A synonym for GetNoHistoryFiles
519 virtual int GetCount() const { return m_fileHistoryN
; }
520 inline int GetNoHistoryFiles(void) const { return m_fileHistoryN
; }
522 inline wxList
& GetMenus() const { return (wxList
&) m_fileMenus
; }
526 char** m_fileHistory
;
527 // Number of files saved
529 // Menus to maintain (may need several for an MDI app)
531 // Max files to maintain
535 // For compatibility with existing file formats:
536 // converts from/to a stream to/from a temporary file.
537 bool WXDLLEXPORT
wxTransferFileToStream(const wxString
& filename
, ostream
& stream
);
538 bool WXDLLEXPORT
wxTransferStreamToFile(istream
& stream
, const wxString
& filename
);