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