Motif and other mods
[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 // N.B. BC++ doesn't have istream.h, ostream.h
41 # include <iostream.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 // Extend event processing to search the view's event table
376 virtual bool ProcessEvent(wxEvent& event);
377
378 void OnActivate(wxActivateEvent& event);
379 void OnCloseWindow(wxCloseEvent& event);
380
381 inline wxDocument *GetDocument(void) const { return m_childDocument; }
382 inline wxView *GetView(void) const { return m_childView; }
383 inline void SetDocument(wxDocument *doc) { m_childDocument = doc; }
384 inline void SetView(wxView *view) { m_childView = view; }
385 protected:
386 wxDocument* m_childDocument;
387 wxView* m_childView;
388
389 DECLARE_EVENT_TABLE()
390
391 };
392
393 /*
394 * A default parent frame
395 */
396
397 class WXDLLEXPORT wxDocParentFrame: public wxFrame
398 {
399 DECLARE_CLASS(wxDocParentFrame)
400 public:
401 wxDocParentFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
402 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
403 long type = wxDEFAULT_FRAME, const wxString& name = "frame");
404
405 // Extend event processing to search the document manager's event table
406 virtual bool ProcessEvent(wxEvent& event);
407
408 wxDocManager *GetDocumentManager(void) const { return m_docManager; }
409
410 void OnExit(wxCommandEvent& event);
411 void OnMRUFile(wxCommandEvent& event);
412 void OnCloseWindow(wxCloseEvent& event);
413
414 protected:
415 wxDocManager *m_docManager;
416
417 DECLARE_EVENT_TABLE()
418 };
419
420 /*
421 * Provide simple default printing facilities
422 */
423
424 #if wxUSE_PRINTING_ARCHITECTURE
425 class WXDLLEXPORT wxDocPrintout: public wxPrintout
426 {
427 DECLARE_DYNAMIC_CLASS(wxDocPrintout)
428 public:
429 wxDocPrintout(wxView *view = (wxView *) NULL, const wxString& title = "Printout");
430 bool OnPrintPage(int page);
431 bool HasPage(int page);
432 bool OnBeginDocument(int startPage, int endPage);
433 void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
434
435 virtual inline wxView *GetView(void) { return m_printoutView; }
436 protected:
437 wxView* m_printoutView;
438 };
439 #endif
440
441 /*
442 * Command processing framework
443 */
444
445 class WXDLLEXPORT wxCommand: public wxObject
446 {
447 DECLARE_CLASS(wxCommand)
448 public:
449 wxCommand(bool canUndoIt = FALSE, const wxString& name = "");
450 ~wxCommand(void);
451
452 // Override this to perform a command
453 virtual bool Do(void) = 0;
454
455 // Override this to undo a command
456 virtual bool Undo(void) = 0;
457
458 virtual inline bool CanUndo(void) const { return m_canUndo; }
459 virtual inline wxString GetName(void) const { return m_commandName; }
460 protected:
461 bool m_canUndo;
462 wxString m_commandName;
463 };
464
465 class WXDLLEXPORT wxCommandProcessor: public wxObject
466 {
467 DECLARE_DYNAMIC_CLASS(wxCommandProcessor)
468 public:
469 wxCommandProcessor(int maxCommands = 100);
470 ~wxCommandProcessor(void);
471
472 // Pass a command to the processor. The processor calls Do();
473 // if successful, is appended to the command history unless
474 // storeIt is FALSE.
475 virtual bool Submit(wxCommand *command, bool storeIt = TRUE);
476 virtual bool Undo(void);
477 virtual bool Redo(void);
478 virtual bool CanUndo(void) const;
479 virtual bool CanRedo(void) const;
480
481 // Call this to manage an edit menu.
482 inline void SetEditMenu(wxMenu *menu) { m_commandEditMenu = menu; }
483 inline wxMenu *GetEditMenu(void) const { return m_commandEditMenu; }
484 virtual void SetMenuStrings(void);
485 virtual void Initialize(void);
486
487 inline wxList& GetCommands(void) const { return (wxList&) m_commands; }
488 inline int GetMaxCommands(void) const { return m_maxNoCommands; }
489 virtual void ClearCommands(void);
490
491 protected:
492 int m_maxNoCommands;
493 wxList m_commands;
494 wxNode* m_currentCommand;
495 wxMenu* m_commandEditMenu;
496 };
497
498 // File history management
499
500 class WXDLLEXPORT wxFileHistory: public wxObject
501 {
502 DECLARE_DYNAMIC_CLASS(wxFileHistory)
503 public:
504 wxFileHistory(int maxFiles = 9);
505 ~wxFileHistory(void);
506
507 // Operations
508 virtual void AddFileToHistory(const wxString& file);
509 virtual int GetMaxFiles(void) const { return m_fileMaxFiles; }
510 virtual void UseMenu(wxMenu *menu);
511
512 // Remove menu from the list (MDI child may be closing)
513 virtual void RemoveMenu(wxMenu *menu);
514
515 virtual void Load(wxConfigBase& config);
516 virtual void Save(wxConfigBase& config);
517
518 virtual void AddFilesToMenu();
519 virtual void AddFilesToMenu(wxMenu* menu); // Single menu
520
521 // Accessors
522 virtual wxString GetHistoryFile(int i) const;
523
524 // A synonym for GetNoHistoryFiles
525 virtual int GetCount() const { return m_fileHistoryN; }
526 inline int GetNoHistoryFiles(void) const { return m_fileHistoryN; }
527
528 inline wxList& GetMenus() const { return (wxList&) m_fileMenus; }
529
530 protected:
531 // Last n files
532 char** m_fileHistory;
533 // Number of files saved
534 int m_fileHistoryN;
535 // Menus to maintain (may need several for an MDI app)
536 wxList m_fileMenus;
537 // Max files to maintain
538 int m_fileMaxFiles;
539 };
540
541 // For compatibility with existing file formats:
542 // converts from/to a stream to/from a temporary file.
543 bool WXDLLEXPORT wxTransferFileToStream(const wxString& filename, ostream& stream);
544 bool WXDLLEXPORT wxTransferStreamToFile(istream& stream, const wxString& filename);
545
546
547 #endif