In splitter sample, forwarded OnIdle events to wxFrame, else UI updates don't
[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 __DOCH__
13 #define __DOCH__
14
15 #ifdef __GNUG__
16 #pragma interface "docview.h"
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/list.h"
21 #include "wx/cmndata.h"
22 #include "wx/string.h"
23
24 #if USE_PRINTING_ARCHITECTURE
25 #include "wx/print.h"
26 #endif
27
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
38 class WXDLLIMPORT ostream;
39 class WXDLLIMPORT istream;
40
41 // Document manager flags
42 #define wxDOC_SDI 1
43 #define wxDOC_MDI 2
44 #define wxDOC_NEW 4
45 #define wxDOC_SILENT 8
46 #define wxDEFAULT_DOCMAN_FLAGS wxDOC_SDI
47
48 // Document template flags
49 #define wxTEMPLATE_VISIBLE 1
50 #define wxTEMPLATE_INVISIBLE 2
51 #define wxDEFAULT_TEMPLATE_FLAGS wxTEMPLATE_VISIBLE
52
53 #define wxMAX_FILE_HISTORY 9
54
55 class WXDLLEXPORT wxDocument : public wxEvtHandler
56 {
57 DECLARE_ABSTRACT_CLASS(wxDocument)
58 public:
59 wxDocument(wxDocument *parent = NULL);
60 ~wxDocument(void);
61
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; }
71
72 virtual bool Close(void);
73 virtual bool Save(void);
74 virtual bool SaveAs(void);
75 virtual bool Revert(void);
76
77 virtual ostream& SaveObject(ostream& stream);
78 virtual istream& LoadObject(istream& stream);
79
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);
85
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);
90
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);
95
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; }
100
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);
105
106 virtual bool DeleteContents(void);
107
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; }
111
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;
116
117 virtual void UpdateAllViews(wxView *sender = NULL, wxObject *hint = NULL);
118
119 // Remove all views (because we're closing the document)
120 virtual bool DeleteAllViews(void);
121
122 // Other stuff
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; }
126
127 // Get title, or filename if no title, else [unnamed]
128 virtual bool GetPrintableName(wxString& buf) const;
129
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;
133 protected:
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;
142 bool m_savedYet;
143 };
144
145 class WXDLLEXPORT wxView: public wxEvtHandler
146 {
147 DECLARE_ABSTRACT_CLASS(wxView)
148 public:
149 wxView(wxDocument *doc = NULL);
150 ~wxView(void);
151
152 inline wxDocument *GetDocument(void) const { return m_viewDocument; }
153 void SetDocument(wxDocument *doc);
154
155 inline wxString GetViewName(void) const { return m_viewTypeName; }
156 void SetViewName(const wxString& name) { m_viewTypeName = name; };
157
158 inline wxFrame *GetFrame(void) const { return m_viewFrame ; }
159 inline void SetFrame(wxFrame *frame) { m_viewFrame = frame; }
160
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);
166
167 // Called by framework if created automatically by the
168 // default document manager class: gives view a chance to
169 // initialise
170 virtual bool OnCreate(wxDocument *WXUNUSED(doc), long WXUNUSED(flags)) { return TRUE; };
171
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);
176
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(); }
181
182 // Extend event processing to search the document's event table
183 virtual bool ProcessEvent(wxEvent& event);
184
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);
188
189 inline wxDocManager *GetDocumentManager(void) const { return m_viewDocument->GetDocumentManager(); }
190
191 #if USE_PRINTING_ARCHITECTURE
192 virtual wxPrintout *OnCreatePrintout(void);
193 #endif
194
195 protected:
196 wxDocument* m_viewDocument;
197 wxString m_viewTypeName;
198 wxFrame* m_viewFrame;
199 };
200
201 // Represents user interface (and other) properties of documents and views
202 class WXDLLEXPORT wxDocTemplate: public wxObject
203 {
204 DECLARE_CLASS(wxDocTemplate)
205
206 friend class WXDLLEXPORT wxDocManager;
207
208 public:
209
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);
217
218 ~wxDocTemplate(void);
219
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);
225
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; }
235
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; };
241
242 inline bool IsVisible(void) const { return ((m_flags & wxTEMPLATE_VISIBLE) == wxTEMPLATE_VISIBLE); }
243
244 protected:
245 long m_flags;
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;
253
254 // For dynamic creation of appropriate instances.
255 wxClassInfo* m_docClassInfo;
256 wxClassInfo* m_viewClassInfo;
257
258 };
259
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
263 {
264 DECLARE_DYNAMIC_CLASS(wxDocManager)
265 public:
266 wxDocManager(long flags = wxDEFAULT_DOCMAN_FLAGS, bool initialize = TRUE);
267 ~wxDocManager(void);
268
269 virtual bool Initialize(void);
270
271 // Handlers for common user commands
272 // virtual void OldOnMenuCommand(int command);
273
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);
285
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,
294 int noTemplates);
295 virtual wxDocTemplate *SelectViewType(wxDocTemplate **templates,
296 int noTemplates);
297 virtual wxDocTemplate *FindTemplateForPath(const wxString& path);
298
299 void AssociateTemplate(wxDocTemplate *temp);
300 void DisassociateTemplate(wxDocTemplate *temp);
301
302 wxDocument *GetCurrentDocument(void) const;
303
304 inline void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; }
305 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen; }
306
307 // Add and remove a document from the manager's list
308 void AddDocument(wxDocument *doc);
309 void RemoveDocument(wxDocument *doc);
310
311 // Clear remaining documents and templates
312 bool Clear(bool force = TRUE);
313
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; }
318
319 virtual inline wxList& GetDocuments(void) const { return (wxList&) m_docs; }
320
321 // Make a default document name
322 virtual bool MakeDefaultName(wxString& buf);
323
324 virtual wxFileHistory *OnCreateFileHistory(void);
325 virtual inline wxFileHistory *GetFileHistory(void) const { return m_fileHistory; }
326
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);
334 protected:
335 long m_flags;
336 int m_defaultDocumentNameCounter;
337 int m_maxDocsOpen;
338 wxList m_docs;
339 wxList m_templates;
340 wxView* m_currentView;
341 wxFileHistory* m_fileHistory;
342
343 DECLARE_EVENT_TABLE()
344 };
345
346 /*
347 * A default child frame
348 */
349
350 class WXDLLEXPORT wxDocChildFrame: public wxFrame
351 {
352 DECLARE_CLASS(wxDocChildFrame)
353
354 public:
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);
359
360 bool OnClose(void);
361 // Extend event processing to search the view's event table
362 virtual bool ProcessEvent(wxEvent& event);
363
364 // void OldOnMenuCommand(int id);
365 void OnActivate(wxActivateEvent& event);
366
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; }
371 protected:
372 wxDocument* m_childDocument;
373 wxView* m_childView;
374
375 DECLARE_EVENT_TABLE()
376
377 };
378
379 /*
380 * A default parent frame
381 */
382
383 class WXDLLEXPORT wxDocParentFrame: public wxFrame
384 {
385 DECLARE_CLASS(wxDocParentFrame)
386 public:
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");
390
391 bool OnClose(void);
392 // Extend event processing to search the document manager's event table
393 virtual bool ProcessEvent(wxEvent& event);
394
395 // void OldOnMenuCommand(int id);
396 wxDocManager *GetDocumentManager(void) const { return m_docManager; }
397
398 void OnExit(wxCommandEvent& event);
399 void OnMRUFile(wxCommandEvent& event);
400
401 protected:
402 wxDocManager *m_docManager;
403
404 DECLARE_EVENT_TABLE()
405 };
406
407 /*
408 * Provide simple default printing facilities
409 */
410
411 #if USE_PRINTING_ARCHITECTURE
412 class WXDLLEXPORT wxDocPrintout: public wxPrintout
413 {
414 DECLARE_DYNAMIC_CLASS(wxDocPrintout)
415 public:
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);
421
422 virtual inline wxView *GetView(void) { return m_printoutView; }
423 protected:
424 wxView* m_printoutView;
425 };
426 #endif
427
428 /*
429 * Command processing framework
430 */
431
432 class WXDLLEXPORT wxCommand: public wxObject
433 {
434 DECLARE_CLASS(wxCommand)
435 public:
436 wxCommand(bool canUndoIt = FALSE, const wxString& name = "");
437 ~wxCommand(void);
438
439 // Override this to perform a command
440 virtual bool Do(void) = 0;
441
442 // Override this to undo a command
443 virtual bool Undo(void) = 0;
444
445 virtual inline bool CanUndo(void) const { return m_canUndo; }
446 virtual inline wxString GetName(void) const { return m_commandName; }
447 protected:
448 bool m_canUndo;
449 wxString m_commandName;
450 };
451
452 class WXDLLEXPORT wxCommandProcessor: public wxObject
453 {
454 DECLARE_DYNAMIC_CLASS(wxCommandProcessor)
455 public:
456 wxCommandProcessor(int maxCommands = 100);
457 ~wxCommandProcessor(void);
458
459 // Pass a command to the processor. The processor calls Do();
460 // if successful, is appended to the command history unless
461 // storeIt is FALSE.
462 virtual bool Submit(wxCommand *command, bool storeIt = TRUE);
463 virtual bool Undo(void);
464 virtual bool Redo(void);
465 virtual bool CanUndo(void);
466
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);
472
473 inline wxList& GetCommands(void) const { return (wxList&) m_commands; }
474 inline int GetMaxCommands(void) const { return m_maxNoCommands; }
475 virtual void ClearCommands(void);
476
477 protected:
478 int m_maxNoCommands;
479 wxList m_commands;
480 wxNode* m_currentCommand;
481 wxMenu* m_commandEditMenu;
482 };
483
484 class WXDLLEXPORT wxFileHistory: public wxObject
485 {
486 DECLARE_DYNAMIC_CLASS(wxFileHistory)
487 public:
488 wxFileHistory(int maxFiles = 9);
489 ~wxFileHistory(void);
490
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);
499 protected:
500 // Last n files
501 char** m_fileHistory;
502 // Number of files saved
503 int m_fileHistoryN;
504 // Menu to maintain
505 wxMenu* m_fileMenu;
506 // Max files to maintain
507 int m_fileMaxFiles;
508 };
509
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);
514
515
516 #endif