Separated creation and initialisation of a document via its doc template.
[wxWidgets.git] / include / wx / docview.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: docview.h
3 // Purpose: Doc/View classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DOCH__
13 #define _WX_DOCH__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "docview.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_DOC_VIEW_ARCHITECTURE
22
23 #include "wx/list.h"
24 #include "wx/cmndata.h"
25 #include "wx/string.h"
26 #include "wx/frame.h"
27
28 #if wxUSE_PRINTING_ARCHITECTURE
29 #include "wx/print.h"
30 #endif
31
32 class WXDLLEXPORT wxWindow;
33 class WXDLLEXPORT wxDocument;
34 class WXDLLEXPORT wxView;
35 class WXDLLEXPORT wxDocTemplate;
36 class WXDLLEXPORT wxDocManager;
37 class WXDLLEXPORT wxPrintInfo;
38 class WXDLLEXPORT wxCommandProcessor;
39 class WXDLLEXPORT wxFileHistory;
40 class WXDLLEXPORT wxConfigBase;
41
42 #if wxUSE_STD_IOSTREAM
43 #include "wx/iosfwrap.h"
44 #else
45 #include "wx/stream.h"
46 #endif
47
48 // Document manager flags
49 enum
50 {
51 wxDOC_SDI = 1,
52 wxDOC_MDI,
53 wxDOC_NEW,
54 wxDOC_SILENT,
55 wxDEFAULT_DOCMAN_FLAGS = wxDOC_SDI
56 };
57
58 // Document template flags
59 enum
60 {
61 wxTEMPLATE_VISIBLE = 1,
62 wxTEMPLATE_INVISIBLE,
63 wxDEFAULT_TEMPLATE_FLAGS = wxTEMPLATE_VISIBLE
64 };
65
66 #define wxMAX_FILE_HISTORY 9
67
68 class WXDLLEXPORT wxDocument : public wxEvtHandler
69 {
70 public:
71 wxDocument(wxDocument *parent = (wxDocument *) NULL);
72 ~wxDocument();
73
74 // accessors
75 void SetFilename(const wxString& filename, bool notifyViews = FALSE);
76 wxString GetFilename() const { return m_documentFile; }
77
78 void SetTitle(const wxString& title) { m_documentTitle = title; };
79 wxString GetTitle() const { return m_documentTitle; }
80
81 void SetDocumentName(const wxString& name) { m_documentTypeName = name; };
82 wxString GetDocumentName() const { return m_documentTypeName; }
83
84 bool GetDocumentSaved() const { return m_savedYet; }
85 void SetDocumentSaved(bool saved = TRUE) { m_savedYet = saved; }
86
87 virtual bool Close();
88 virtual bool Save();
89 virtual bool SaveAs();
90 virtual bool Revert();
91
92 #if wxUSE_STD_IOSTREAM
93 virtual wxSTD ostream& SaveObject(wxSTD ostream& stream);
94 virtual wxSTD istream& LoadObject(wxSTD istream& stream);
95 #else
96 virtual wxOutputStream& SaveObject(wxOutputStream& stream);
97 virtual wxInputStream& LoadObject(wxInputStream& stream);
98 #endif
99
100 // Called by wxWidgets
101 virtual bool OnSaveDocument(const wxString& filename);
102 virtual bool OnOpenDocument(const wxString& filename);
103 virtual bool OnNewDocument();
104 virtual bool OnCloseDocument();
105
106 // Prompts for saving if about to close a modified document. Returns TRUE
107 // if ok to close the document (may have saved in the meantime, or set
108 // modified to FALSE)
109 virtual bool OnSaveModified();
110
111 // Called by framework if created automatically by the default document
112 // manager: gives document a chance to initialise and (usually) create a
113 // view
114 virtual bool OnCreate(const wxString& path, long flags);
115
116 // By default, creates a base wxCommandProcessor.
117 virtual wxCommandProcessor *OnCreateCommandProcessor();
118 virtual wxCommandProcessor *GetCommandProcessor() const { return m_commandProcessor; }
119 virtual void SetCommandProcessor(wxCommandProcessor *proc) { m_commandProcessor = proc; }
120
121 // Called after a view is added or removed. The default implementation
122 // deletes the document if this is there are no more views.
123 virtual void OnChangedViewList();
124
125 virtual bool DeleteContents();
126
127 virtual bool Draw(wxDC&);
128 virtual bool IsModified() const { return m_documentModified; }
129 virtual void Modify(bool mod) { m_documentModified = mod; }
130
131 virtual bool AddView(wxView *view);
132 virtual bool RemoveView(wxView *view);
133 wxList& GetViews() const { return (wxList&) m_documentViews; }
134 wxView *GetFirstView() const;
135
136 virtual void UpdateAllViews(wxView *sender = (wxView *) NULL, wxObject *hint = (wxObject *) NULL);
137 virtual void NotifyClosing();
138
139 // Remove all views (because we're closing the document)
140 virtual bool DeleteAllViews();
141
142 // Other stuff
143 virtual wxDocManager *GetDocumentManager() const;
144 virtual wxDocTemplate *GetDocumentTemplate() const { return m_documentTemplate; }
145 virtual void SetDocumentTemplate(wxDocTemplate *temp) { m_documentTemplate = temp; }
146
147 // Get title, or filename if no title, else [unnamed]
148 virtual bool GetPrintableName(wxString& buf) const;
149
150 // Returns a window that can be used as a parent for document-related
151 // dialogs. Override if necessary.
152 virtual wxWindow *GetDocumentWindow() const;
153
154 protected:
155 wxList m_documentViews;
156 wxString m_documentFile;
157 wxString m_documentTitle;
158 wxString m_documentTypeName;
159 wxDocTemplate* m_documentTemplate;
160 bool m_documentModified;
161 wxDocument* m_documentParent;
162 wxCommandProcessor* m_commandProcessor;
163 bool m_savedYet;
164
165 private:
166 DECLARE_ABSTRACT_CLASS(wxDocument)
167 DECLARE_NO_COPY_CLASS(wxDocument)
168 };
169
170 class WXDLLEXPORT wxView: public wxEvtHandler
171 {
172 public:
173 // wxView(wxDocument *doc = (wxDocument *) NULL);
174 wxView();
175 ~wxView();
176
177 wxDocument *GetDocument() const { return m_viewDocument; }
178 virtual void SetDocument(wxDocument *doc);
179
180 wxString GetViewName() const { return m_viewTypeName; }
181 void SetViewName(const wxString& name) { m_viewTypeName = name; };
182
183 wxWindow *GetFrame() const { return m_viewFrame ; }
184 void SetFrame(wxWindow *frame) { m_viewFrame = frame; }
185
186 virtual void OnActivateView(bool activate, wxView *activeView, wxView *deactiveView);
187 virtual void OnDraw(wxDC *dc) = 0;
188 virtual void OnPrint(wxDC *dc, wxObject *info);
189 virtual void OnUpdate(wxView *sender, wxObject *hint = (wxObject *) NULL);
190 virtual void OnClosingDocument() {};
191 virtual void OnChangeFilename();
192
193 // Called by framework if created automatically by the default document
194 // manager class: gives view a chance to initialise
195 virtual bool OnCreate(wxDocument *WXUNUSED(doc), long WXUNUSED(flags)) { return TRUE; };
196
197 // Checks if the view is the last one for the document; if so, asks user
198 // to confirm save data (if modified). If ok, deletes itself and returns
199 // TRUE.
200 virtual bool Close(bool deleteWindow = TRUE);
201
202 // Override to do cleanup/veto close
203 virtual bool OnClose(bool deleteWindow);
204
205 // Extend event processing to search the document's event table
206 virtual bool ProcessEvent(wxEvent& event);
207
208 // A view's window can call this to notify the view it is (in)active.
209 // The function then notifies the document manager.
210 virtual void Activate(bool activate);
211
212 wxDocManager *GetDocumentManager() const
213 { return m_viewDocument->GetDocumentManager(); }
214
215 #if wxUSE_PRINTING_ARCHITECTURE
216 virtual wxPrintout *OnCreatePrintout();
217 #endif
218
219 protected:
220 wxDocument* m_viewDocument;
221 wxString m_viewTypeName;
222 wxWindow* m_viewFrame;
223
224 private:
225 DECLARE_ABSTRACT_CLASS(wxView)
226 DECLARE_NO_COPY_CLASS(wxView)
227 };
228
229 // Represents user interface (and other) properties of documents and views
230 class WXDLLEXPORT wxDocTemplate: public wxObject
231 {
232
233 friend class WXDLLEXPORT wxDocManager;
234
235 public:
236 // Associate document and view types. They're for identifying what view is
237 // associated with what template/document type
238 wxDocTemplate(wxDocManager *manager,
239 const wxString& descr,
240 const wxString& filter,
241 const wxString& dir,
242 const wxString& ext,
243 const wxString& docTypeName,
244 const wxString& viewTypeName,
245 wxClassInfo *docClassInfo = (wxClassInfo *) NULL,
246 wxClassInfo *viewClassInfo = (wxClassInfo *)NULL,
247 long flags = wxDEFAULT_TEMPLATE_FLAGS);
248
249 ~wxDocTemplate();
250
251 // By default, these two member functions dynamically creates document and
252 // view using dynamic instance construction. Override these if you need a
253 // different method of construction.
254 virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
255 virtual wxView *CreateView(wxDocument *doc, long flags = 0);
256
257 // Helper method for CreateDocument; also allows you to do your own document
258 // creation
259 virtual bool InitDocument(wxDocument* doc, const wxString& path, long flags = 0);
260
261 wxString GetDefaultExtension() const { return m_defaultExt; };
262 wxString GetDescription() const { return m_description; }
263 wxString GetDirectory() const { return m_directory; };
264 wxDocManager *GetDocumentManager() const { return m_documentManager; }
265 void SetDocumentManager(wxDocManager *manager) { m_documentManager = manager; }
266 wxString GetFileFilter() const { return m_fileFilter; };
267 long GetFlags() const { return m_flags; };
268 virtual wxString GetViewName() const { return m_viewTypeName; }
269 virtual wxString GetDocumentName() const { return m_docTypeName; }
270
271 void SetFileFilter(const wxString& filter) { m_fileFilter = filter; };
272 void SetDirectory(const wxString& dir) { m_directory = dir; };
273 void SetDescription(const wxString& descr) { m_description = descr; };
274 void SetDefaultExtension(const wxString& ext) { m_defaultExt = ext; };
275 void SetFlags(long flags) { m_flags = flags; };
276
277 bool IsVisible() const { return ((m_flags & wxTEMPLATE_VISIBLE) == wxTEMPLATE_VISIBLE); }
278
279 virtual bool FileMatchesTemplate(const wxString& path);
280
281 protected:
282 long m_flags;
283 wxString m_fileFilter;
284 wxString m_directory;
285 wxString m_description;
286 wxString m_defaultExt;
287 wxString m_docTypeName;
288 wxString m_viewTypeName;
289 wxDocManager* m_documentManager;
290
291 // For dynamic creation of appropriate instances.
292 wxClassInfo* m_docClassInfo;
293 wxClassInfo* m_viewClassInfo;
294
295 private:
296 DECLARE_CLASS(wxDocTemplate)
297 DECLARE_NO_COPY_CLASS(wxDocTemplate)
298 };
299
300 // One object of this class may be created in an application, to manage all
301 // the templates and documents.
302 class WXDLLEXPORT wxDocManager: public wxEvtHandler
303 {
304 public:
305 wxDocManager(long flags = wxDEFAULT_DOCMAN_FLAGS, bool initialize = TRUE);
306 ~wxDocManager();
307
308 virtual bool Initialize();
309
310 // Handlers for common user commands
311 void OnFileClose(wxCommandEvent& event);
312 void OnFileCloseAll(wxCommandEvent& event);
313 void OnFileNew(wxCommandEvent& event);
314 void OnFileOpen(wxCommandEvent& event);
315 void OnFileRevert(wxCommandEvent& event);
316 void OnFileSave(wxCommandEvent& event);
317 void OnFileSaveAs(wxCommandEvent& event);
318 void OnPrint(wxCommandEvent& event);
319 void OnPrintSetup(wxCommandEvent& event);
320 void OnPreview(wxCommandEvent& event);
321 void OnUndo(wxCommandEvent& event);
322 void OnRedo(wxCommandEvent& event);
323
324 // Handlers for UI update commands
325 void OnUpdateFileOpen(wxUpdateUIEvent& event);
326 void OnUpdateFileClose(wxUpdateUIEvent& event);
327 void OnUpdateFileRevert(wxUpdateUIEvent& event);
328 void OnUpdateFileNew(wxUpdateUIEvent& event);
329 void OnUpdateFileSave(wxUpdateUIEvent& event);
330 void OnUpdateFileSaveAs(wxUpdateUIEvent& event);
331 void OnUpdateUndo(wxUpdateUIEvent& event);
332 void OnUpdateRedo(wxUpdateUIEvent& event);
333
334 void OnUpdatePrint(wxUpdateUIEvent& event);
335 void OnUpdatePrintSetup(wxUpdateUIEvent& event);
336 void OnUpdatePreview(wxUpdateUIEvent& event);
337
338 // Extend event processing to search the view's event table
339 virtual bool ProcessEvent(wxEvent& event);
340
341 // called when file format detection didn't work, can be overridden to do
342 // something in this case
343 virtual void OnOpenFileFailure() { }
344
345 virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
346 virtual wxView *CreateView(wxDocument *doc, long flags = 0);
347 virtual void DeleteTemplate(wxDocTemplate *temp, long flags = 0);
348 virtual bool FlushDoc(wxDocument *doc);
349 virtual wxDocTemplate *MatchTemplate(const wxString& path);
350 virtual wxDocTemplate *SelectDocumentPath(wxDocTemplate **templates,
351 int noTemplates, wxString& path, long flags, bool save = FALSE);
352 virtual wxDocTemplate *SelectDocumentType(wxDocTemplate **templates,
353 int noTemplates, bool sort = FALSE);
354 virtual wxDocTemplate *SelectViewType(wxDocTemplate **templates,
355 int noTemplates, bool sort = FALSE);
356 virtual wxDocTemplate *FindTemplateForPath(const wxString& path);
357
358 void AssociateTemplate(wxDocTemplate *temp);
359 void DisassociateTemplate(wxDocTemplate *temp);
360
361 wxDocument *GetCurrentDocument() const;
362
363 void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; }
364 int GetMaxDocsOpen() const { return m_maxDocsOpen; }
365
366 // Add and remove a document from the manager's list
367 void AddDocument(wxDocument *doc);
368 void RemoveDocument(wxDocument *doc);
369
370 // closes all currently open documents
371 bool CloseDocuments(bool force = TRUE);
372
373 // closes the specified document
374 bool CloseDocument(wxDocument* doc, bool force = FALSE);
375
376 // Clear remaining documents and templates
377 bool Clear(bool force = TRUE);
378
379 // Views or windows should inform the document manager
380 // when a view is going in or out of focus
381 virtual void ActivateView(wxView *view, bool activate = TRUE);
382 virtual wxView *GetCurrentView() const;
383
384 wxList& GetDocuments() { return m_docs; }
385 wxList& GetTemplates() { return m_templates; }
386
387 // Make a default document name
388 virtual bool MakeDefaultName(wxString& buf);
389
390 // Make a frame title (override this to do something different)
391 virtual wxString MakeFrameTitle(wxDocument* doc);
392
393 virtual wxFileHistory *OnCreateFileHistory();
394 virtual wxFileHistory *GetFileHistory() const { return m_fileHistory; }
395
396 // File history management
397 virtual void AddFileToHistory(const wxString& file);
398 virtual void RemoveFileFromHistory(size_t i);
399 virtual size_t GetHistoryFilesCount() const;
400 virtual wxString GetHistoryFile(size_t i) const;
401 virtual void FileHistoryUseMenu(wxMenu *menu);
402 virtual void FileHistoryRemoveMenu(wxMenu *menu);
403 #if wxUSE_CONFIG
404 virtual void FileHistoryLoad(wxConfigBase& config);
405 virtual void FileHistorySave(wxConfigBase& config);
406 #endif // wxUSE_CONFIG
407
408 virtual void FileHistoryAddFilesToMenu();
409 virtual void FileHistoryAddFilesToMenu(wxMenu* menu);
410
411 wxString GetLastDirectory() const { return m_lastDirectory; }
412 void SetLastDirectory(const wxString& dir) { m_lastDirectory = dir; }
413
414 // Get the current document manager
415 static wxDocManager* GetDocumentManager() { return sm_docManager; }
416
417 // deprecated, use GetHistoryFilesCount() instead
418 wxDEPRECATED( size_t GetNoHistoryFiles() const );
419
420 protected:
421 long m_flags;
422 int m_defaultDocumentNameCounter;
423 int m_maxDocsOpen;
424 wxList m_docs;
425 wxList m_templates;
426 wxView* m_currentView;
427 wxFileHistory* m_fileHistory;
428 wxString m_lastDirectory;
429 static wxDocManager* sm_docManager;
430
431 DECLARE_EVENT_TABLE()
432 DECLARE_DYNAMIC_CLASS(wxDocManager)
433 DECLARE_NO_COPY_CLASS(wxDocManager)
434 };
435
436 inline size_t wxDocManager::GetNoHistoryFiles() const
437 {
438 return GetHistoryFilesCount();
439 }
440
441 // ----------------------------------------------------------------------------
442 // A default child frame
443 // ----------------------------------------------------------------------------
444
445 class WXDLLEXPORT wxDocChildFrame : public wxFrame
446 {
447 public:
448 wxDocChildFrame(wxDocument *doc,
449 wxView *view,
450 wxFrame *frame,
451 wxWindowID id,
452 const wxString& title,
453 const wxPoint& pos = wxDefaultPosition,
454 const wxSize& size = wxDefaultSize,
455 long type = wxDEFAULT_FRAME_STYLE,
456 const wxString& name = wxT("frame"));
457 ~wxDocChildFrame();
458
459 // Extend event processing to search the view's event table
460 virtual bool ProcessEvent(wxEvent& event);
461
462 void OnActivate(wxActivateEvent& event);
463 void OnCloseWindow(wxCloseEvent& event);
464
465 wxDocument *GetDocument() const { return m_childDocument; }
466 wxView *GetView() const { return m_childView; }
467 void SetDocument(wxDocument *doc) { m_childDocument = doc; }
468 void SetView(wxView *view) { m_childView = view; }
469 bool Destroy() { m_childView = (wxView *)NULL; return wxFrame::Destroy(); }
470
471 protected:
472 wxDocument* m_childDocument;
473 wxView* m_childView;
474
475 private:
476 DECLARE_CLASS(wxDocChildFrame)
477 DECLARE_EVENT_TABLE()
478 DECLARE_NO_COPY_CLASS(wxDocChildFrame)
479 };
480
481 // ----------------------------------------------------------------------------
482 // A default parent frame
483 // ----------------------------------------------------------------------------
484
485 class WXDLLEXPORT wxDocParentFrame : public wxFrame
486 {
487 public:
488 wxDocParentFrame(wxDocManager *manager,
489 wxFrame *frame,
490 wxWindowID id,
491 const wxString& title,
492 const wxPoint& pos = wxDefaultPosition,
493 const wxSize& size = wxDefaultSize,
494 long type = wxDEFAULT_FRAME_STYLE,
495 const wxString& name = wxT("frame"));
496
497 // Extend event processing to search the document manager's event table
498 virtual bool ProcessEvent(wxEvent& event);
499
500 wxDocManager *GetDocumentManager() const { return m_docManager; }
501
502 void OnExit(wxCommandEvent& event);
503 void OnMRUFile(wxCommandEvent& event);
504 void OnCloseWindow(wxCloseEvent& event);
505
506 protected:
507 wxDocManager *m_docManager;
508
509 private:
510 DECLARE_CLASS(wxDocParentFrame)
511 DECLARE_EVENT_TABLE()
512 DECLARE_NO_COPY_CLASS(wxDocParentFrame)
513 };
514
515 // ----------------------------------------------------------------------------
516 // Provide simple default printing facilities
517 // ----------------------------------------------------------------------------
518
519 #if wxUSE_PRINTING_ARCHITECTURE
520 class WXDLLEXPORT wxDocPrintout : public wxPrintout
521 {
522 public:
523 wxDocPrintout(wxView *view = (wxView *) NULL, const wxString& title = wxT("Printout"));
524 bool OnPrintPage(int page);
525 bool HasPage(int page);
526 bool OnBeginDocument(int startPage, int endPage);
527 void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
528
529 virtual wxView *GetView() { return m_printoutView; }
530
531 protected:
532 wxView* m_printoutView;
533
534 private:
535 DECLARE_DYNAMIC_CLASS(wxDocPrintout)
536 DECLARE_NO_COPY_CLASS(wxDocPrintout)
537 };
538 #endif // wxUSE_PRINTING_ARCHITECTURE
539
540 // ----------------------------------------------------------------------------
541 // File history management
542 // ----------------------------------------------------------------------------
543
544 class WXDLLEXPORT wxFileHistory : public wxObject
545 {
546 public:
547 wxFileHistory(size_t maxFiles = 9, wxWindowID idBase = wxID_FILE1);
548 ~wxFileHistory();
549
550 // Operations
551 virtual void AddFileToHistory(const wxString& file);
552 virtual void RemoveFileFromHistory(size_t i);
553 virtual int GetMaxFiles() const { return m_fileMaxFiles; }
554 virtual void UseMenu(wxMenu *menu);
555
556 // Remove menu from the list (MDI child may be closing)
557 virtual void RemoveMenu(wxMenu *menu);
558
559 #if wxUSE_CONFIG
560 virtual void Load(wxConfigBase& config);
561 virtual void Save(wxConfigBase& config);
562 #endif // wxUSE_CONFIG
563
564 virtual void AddFilesToMenu();
565 virtual void AddFilesToMenu(wxMenu* menu); // Single menu
566
567 // Accessors
568 virtual wxString GetHistoryFile(size_t i) const;
569 virtual size_t GetCount() const { return m_fileHistoryN; }
570
571 const wxList& GetMenus() const { return m_fileMenus; }
572
573 // deprecated, use GetCount() instead
574 wxDEPRECATED( size_t GetNoHistoryFiles() const );
575
576 protected:
577 // Last n files
578 wxChar** m_fileHistory;
579 // Number of files saved
580 size_t m_fileHistoryN;
581 // Menus to maintain (may need several for an MDI app)
582 wxList m_fileMenus;
583 // Max files to maintain
584 size_t m_fileMaxFiles;
585
586 private:
587 // The ID of the first history menu item (Doesn't have to be wxID_FILE1)
588 wxWindowID m_idBase;
589
590 DECLARE_DYNAMIC_CLASS(wxFileHistory)
591 DECLARE_NO_COPY_CLASS(wxFileHistory)
592 };
593
594 inline size_t wxFileHistory::GetNoHistoryFiles() const
595 {
596 return m_fileHistoryN;
597 }
598
599 #if wxUSE_STD_IOSTREAM
600 // For compatibility with existing file formats:
601 // converts from/to a stream to/from a temporary file.
602 bool WXDLLEXPORT wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream);
603 bool WXDLLEXPORT wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename);
604 #else
605 // For compatibility with existing file formats:
606 // converts from/to a stream to/from a temporary file.
607 bool WXDLLEXPORT wxTransferFileToStream(const wxString& filename, wxOutputStream& stream);
608 bool WXDLLEXPORT wxTransferStreamToFile(wxInputStream& stream, const wxString& filename);
609 #endif // wxUSE_STD_IOSTREAM
610
611 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
612
613 #endif // _WX_DOCH__