]> git.saurik.com Git - wxWidgets.git/blob - include/wx/docview.h
fixed compiler warning
[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 #ifdef __WXGTK__
161 inline void SetFrame(wxMDIChildFrame *frame) { m_viewFrame = (wxFrame*)frame; }
162 #endif
163
164 virtual void OnActivateView(bool activate, wxView *activeView, wxView *deactiveView);
165 virtual void OnDraw(wxDC *dc) = 0;
166 virtual void OnPrint(wxDC *dc, wxObject *info);
167 virtual void OnUpdate(wxView *sender, wxObject *hint = NULL);
168 virtual void OnChangeFilename(void);
169
170 // Called by framework if created automatically by the
171 // default document manager class: gives view a chance to
172 // initialise
173 virtual bool OnCreate(wxDocument *WXUNUSED(doc), long WXUNUSED(flags)) { return TRUE; };
174
175 // Checks if the view is the last one for the document; if so,
176 // asks user to confirm save data (if modified). If ok,
177 // deletes itself and returns TRUE.
178 virtual bool Close(bool deleteWindow = TRUE);
179
180 // Override to do cleanup/veto close
181 virtual bool OnClose(bool deleteWindow);
182 // Defeat compiler warning
183 inline bool OnClose(void) { return wxEvtHandler::OnClose(); }
184
185 // Extend event processing to search the document's event table
186 virtual bool ProcessEvent(wxEvent& event);
187
188 // A view's window can call this to notify the view it is (in)active.
189 // The function then notifies the document manager.
190 virtual void Activate(bool activate);
191
192 inline wxDocManager *GetDocumentManager(void) const { return m_viewDocument->GetDocumentManager(); }
193
194 #if USE_PRINTING_ARCHITECTURE
195 virtual wxPrintout *OnCreatePrintout(void);
196 #endif
197
198 protected:
199 wxDocument* m_viewDocument;
200 wxString m_viewTypeName;
201 wxFrame* m_viewFrame;
202 };
203
204 // Represents user interface (and other) properties of documents and views
205 class WXDLLEXPORT wxDocTemplate: public wxObject
206 {
207 DECLARE_CLASS(wxDocTemplate)
208
209 friend class WXDLLEXPORT wxDocManager;
210
211 public:
212
213 // Associate document and view types.
214 // They're for identifying what view is associated with what
215 // template/document type
216 wxDocTemplate(wxDocManager *manager, const wxString& descr, const wxString& filter, const wxString& dir,
217 const wxString& ext, const wxString& docTypeName, const wxString& viewTypeName,
218 wxClassInfo *docClassInfo = NULL, wxClassInfo *viewClassInfo = NULL,
219 long flags = wxDEFAULT_TEMPLATE_FLAGS);
220
221 ~wxDocTemplate(void);
222
223 // By default, these two member functions dynamically creates document
224 // and view using dynamic instance construction.
225 // Override these if you need a different method of construction.
226 virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
227 virtual wxView *CreateView(wxDocument *doc, long flags = 0);
228
229 inline wxString GetDefaultExtension(void) const { return m_defaultExt; };
230 inline wxString GetDescription(void) const { return m_description; }
231 inline wxString GetDirectory(void) const { return m_directory; };
232 inline wxDocManager *GetDocumentManager(void) const { return m_documentManager; }
233 inline void SetDocumentManager(wxDocManager *manager) { m_documentManager = manager; }
234 inline wxString GetFileFilter(void) const { return m_fileFilter; };
235 inline long GetFlags(void) const { return m_flags; };
236 virtual wxString GetViewName(void) const { return m_viewTypeName; }
237 virtual wxString GetDocumentName(void) const { return m_docTypeName; }
238
239 inline void SetFileFilter(const wxString& filter) { m_fileFilter = filter; };
240 inline void SetDirectory(const wxString& dir) { m_directory = dir; };
241 inline void SetDescription(const wxString& descr) { m_description = descr; };
242 inline void SetDefaultExtension(const wxString& ext) { m_defaultExt = ext; };
243 inline void SetFlags(long flags) { m_flags = flags; };
244
245 inline bool IsVisible(void) const { return ((m_flags & wxTEMPLATE_VISIBLE) == wxTEMPLATE_VISIBLE); }
246
247 protected:
248 long m_flags;
249 wxString m_fileFilter;
250 wxString m_directory;
251 wxString m_description;
252 wxString m_defaultExt;
253 wxString m_docTypeName;
254 wxString m_viewTypeName;
255 wxDocManager* m_documentManager;
256
257 // For dynamic creation of appropriate instances.
258 wxClassInfo* m_docClassInfo;
259 wxClassInfo* m_viewClassInfo;
260
261 };
262
263 // One object of this class may be created in an application,
264 // to manage all the templates and documents.
265 class WXDLLEXPORT wxDocManager: public wxEvtHandler
266 {
267 DECLARE_DYNAMIC_CLASS(wxDocManager)
268 public:
269 wxDocManager(long flags = wxDEFAULT_DOCMAN_FLAGS, bool initialize = TRUE);
270 ~wxDocManager(void);
271
272 virtual bool Initialize(void);
273
274 // Handlers for common user commands
275 // virtual void OldOnMenuCommand(int command);
276
277 void OnFileClose(wxCommandEvent& event);
278 void OnFileNew(wxCommandEvent& event);
279 void OnFileOpen(wxCommandEvent& event);
280 void OnFileRevert(wxCommandEvent& event);
281 void OnFileSave(wxCommandEvent& event);
282 void OnFileSaveAs(wxCommandEvent& event);
283 void OnPrint(wxCommandEvent& event);
284 void OnPrintSetup(wxCommandEvent& event);
285 void OnPreview(wxCommandEvent& event);
286 void OnUndo(wxCommandEvent& event);
287 void OnRedo(wxCommandEvent& event);
288
289 // Extend event processing to search the view's event table
290 virtual bool ProcessEvent(wxEvent& event);
291
292 virtual wxDocument *CreateDocument(const wxString& path, long flags = 0);
293 virtual wxView *CreateView(wxDocument *doc, long flags = 0);
294 virtual void DeleteTemplate(wxDocTemplate *temp, long flags = 0);
295 virtual bool FlushDoc(wxDocument *doc);
296 virtual wxDocTemplate *MatchTemplate(const wxString& path);
297 virtual wxDocTemplate *SelectDocumentPath(wxDocTemplate **templates,
298 int noTemplates, wxString& path, long flags, bool save = FALSE);
299 virtual wxDocTemplate *SelectDocumentType(wxDocTemplate **templates,
300 int noTemplates);
301 virtual wxDocTemplate *SelectViewType(wxDocTemplate **templates,
302 int noTemplates);
303 virtual wxDocTemplate *FindTemplateForPath(const wxString& path);
304
305 void AssociateTemplate(wxDocTemplate *temp);
306 void DisassociateTemplate(wxDocTemplate *temp);
307
308 wxDocument *GetCurrentDocument(void) const;
309
310 inline void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; }
311 inline int GetMaxDocsOpen(void) const { return m_maxDocsOpen; }
312
313 // Add and remove a document from the manager's list
314 void AddDocument(wxDocument *doc);
315 void RemoveDocument(wxDocument *doc);
316
317 // Clear remaining documents and templates
318 bool Clear(bool force = TRUE);
319
320 // Views or windows should inform the document manager
321 // when a view is going in or out of focus
322 virtual void ActivateView(wxView *view, bool activate = TRUE, bool deleting = FALSE);
323 virtual wxView *GetCurrentView(void) const;
324
325 virtual inline wxList& GetDocuments(void) const { return (wxList&) m_docs; }
326
327 // Make a default document name
328 virtual bool MakeDefaultName(wxString& buf);
329
330 virtual wxFileHistory *OnCreateFileHistory(void);
331 virtual inline wxFileHistory *GetFileHistory(void) const { return m_fileHistory; }
332
333 // File history management
334 virtual void AddFileToHistory(const wxString& file);
335 virtual int GetNoHistoryFiles(void) const;
336 virtual wxString GetHistoryFile(int i) const;
337 virtual void FileHistoryUseMenu(wxMenu *menu);
338 virtual void FileHistoryLoad(const wxString& resourceFile, const wxString& section);
339 virtual void FileHistorySave(const wxString& resourceFile, const wxString& section);
340 protected:
341 long m_flags;
342 int m_defaultDocumentNameCounter;
343 int m_maxDocsOpen;
344 wxList m_docs;
345 wxList m_templates;
346 wxView* m_currentView;
347 wxFileHistory* m_fileHistory;
348
349 DECLARE_EVENT_TABLE()
350 };
351
352 /*
353 * A default child frame
354 */
355
356 class WXDLLEXPORT wxDocChildFrame: public wxFrame
357 {
358 DECLARE_CLASS(wxDocChildFrame)
359
360 public:
361 wxDocChildFrame(wxDocument *doc, wxView *view, wxFrame *frame, wxWindowID id, const wxString& title,
362 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
363 long type = wxDEFAULT_FRAME_STYLE, const wxString& name = "frame");
364 ~wxDocChildFrame(void);
365
366 bool OnClose(void);
367 // Extend event processing to search the view's event table
368 virtual bool ProcessEvent(wxEvent& event);
369
370 // void OldOnMenuCommand(int id);
371 void OnActivate(wxActivateEvent& event);
372
373 inline wxDocument *GetDocument(void) const { return m_childDocument; }
374 inline wxView *GetView(void) const { return m_childView; }
375 inline void SetDocument(wxDocument *doc) { m_childDocument = doc; }
376 inline void SetView(wxView *view) { m_childView = view; }
377 protected:
378 wxDocument* m_childDocument;
379 wxView* m_childView;
380
381 DECLARE_EVENT_TABLE()
382
383 };
384
385 /*
386 * A default parent frame
387 */
388
389 class WXDLLEXPORT wxDocParentFrame: public wxFrame
390 {
391 DECLARE_CLASS(wxDocParentFrame)
392 public:
393 wxDocParentFrame(wxDocManager *manager, wxFrame *frame, wxWindowID id, const wxString& title,
394 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize,
395 long type = wxDEFAULT_FRAME, const wxString& name = "frame");
396
397 bool OnClose(void);
398 // Extend event processing to search the document manager's event table
399 virtual bool ProcessEvent(wxEvent& event);
400
401 // void OldOnMenuCommand(int id);
402 wxDocManager *GetDocumentManager(void) const { return m_docManager; }
403
404 void OnExit(wxCommandEvent& event);
405 void OnMRUFile(wxCommandEvent& event);
406
407 protected:
408 wxDocManager *m_docManager;
409
410 DECLARE_EVENT_TABLE()
411 };
412
413 /*
414 * Provide simple default printing facilities
415 */
416
417 #if USE_PRINTING_ARCHITECTURE
418 class WXDLLEXPORT wxDocPrintout: public wxPrintout
419 {
420 DECLARE_DYNAMIC_CLASS(wxDocPrintout)
421 public:
422 wxDocPrintout(wxView *view = NULL, const wxString& title = "Printout");
423 bool OnPrintPage(int page);
424 bool HasPage(int page);
425 bool OnBeginDocument(int startPage, int endPage);
426 void GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo);
427
428 virtual inline wxView *GetView(void) { return m_printoutView; }
429 protected:
430 wxView* m_printoutView;
431 };
432 #endif
433
434 /*
435 * Command processing framework
436 */
437
438 class WXDLLEXPORT wxCommand: public wxObject
439 {
440 DECLARE_CLASS(wxCommand)
441 public:
442 wxCommand(bool canUndoIt = FALSE, const wxString& name = "");
443 ~wxCommand(void);
444
445 // Override this to perform a command
446 virtual bool Do(void) = 0;
447
448 // Override this to undo a command
449 virtual bool Undo(void) = 0;
450
451 virtual inline bool CanUndo(void) const { return m_canUndo; }
452 virtual inline wxString GetName(void) const { return m_commandName; }
453 protected:
454 bool m_canUndo;
455 wxString m_commandName;
456 };
457
458 class WXDLLEXPORT wxCommandProcessor: public wxObject
459 {
460 DECLARE_DYNAMIC_CLASS(wxCommandProcessor)
461 public:
462 wxCommandProcessor(int maxCommands = 100);
463 ~wxCommandProcessor(void);
464
465 // Pass a command to the processor. The processor calls Do();
466 // if successful, is appended to the command history unless
467 // storeIt is FALSE.
468 virtual bool Submit(wxCommand *command, bool storeIt = TRUE);
469 virtual bool Undo(void);
470 virtual bool Redo(void);
471 virtual bool CanUndo(void);
472
473 // Call this to manage an edit menu.
474 inline void SetEditMenu(wxMenu *menu) { m_commandEditMenu = menu; }
475 inline wxMenu *GetEditMenu(void) const { return m_commandEditMenu; }
476 virtual void SetMenuStrings(void);
477 virtual void Initialize(void);
478
479 inline wxList& GetCommands(void) const { return (wxList&) m_commands; }
480 inline int GetMaxCommands(void) const { return m_maxNoCommands; }
481 virtual void ClearCommands(void);
482
483 protected:
484 int m_maxNoCommands;
485 wxList m_commands;
486 wxNode* m_currentCommand;
487 wxMenu* m_commandEditMenu;
488 };
489
490 class WXDLLEXPORT wxFileHistory: public wxObject
491 {
492 DECLARE_DYNAMIC_CLASS(wxFileHistory)
493 public:
494 wxFileHistory(int maxFiles = 9);
495 ~wxFileHistory(void);
496
497 // File history management
498 virtual void AddFileToHistory(const wxString& file);
499 inline virtual int GetNoHistoryFiles(void) const { return m_fileHistoryN; }
500 virtual wxString GetHistoryFile(int i) const;
501 virtual int GetMaxFiles(void) const { return m_fileMaxFiles; }
502 virtual void FileHistoryUseMenu(wxMenu *menu);
503 virtual void FileHistoryLoad(const wxString& resourceFile, const wxString& section);
504 virtual void FileHistorySave(const wxString& resourceFile, const wxString& section);
505 protected:
506 // Last n files
507 char** m_fileHistory;
508 // Number of files saved
509 int m_fileHistoryN;
510 // Menu to maintain
511 wxMenu* m_fileMenu;
512 // Max files to maintain
513 int m_fileMaxFiles;
514 };
515
516 // For compatibility with existing file formats:
517 // converts from/to a stream to/from a temporary file.
518 bool WXDLLEXPORT wxTransferFileToStream(const wxString& filename, ostream& stream);
519 bool WXDLLEXPORT wxTransferStreamToFile(istream& stream, const wxString& filename);
520
521
522 #endif