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