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