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