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