]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/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) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_DOCH__ | |
13 | #define _WX_DOCH__ | |
14 | ||
15 | #include "wx/defs.h" | |
16 | ||
17 | #if wxUSE_DOC_VIEW_ARCHITECTURE | |
18 | ||
19 | #include "wx/list.h" | |
20 | #include "wx/string.h" | |
21 | #include "wx/frame.h" | |
22 | #include "wx/filehistory.h" | |
23 | ||
24 | #if wxUSE_PRINTING_ARCHITECTURE | |
25 | #include "wx/print.h" | |
26 | #endif | |
27 | ||
28 | class WXDLLIMPEXP_FWD_CORE wxWindow; | |
29 | class WXDLLIMPEXP_FWD_CORE wxDocument; | |
30 | class WXDLLIMPEXP_FWD_CORE wxView; | |
31 | class WXDLLIMPEXP_FWD_CORE wxDocTemplate; | |
32 | class WXDLLIMPEXP_FWD_CORE wxDocManager; | |
33 | class WXDLLIMPEXP_FWD_CORE wxPrintInfo; | |
34 | class WXDLLIMPEXP_FWD_CORE wxCommandProcessor; | |
35 | class WXDLLIMPEXP_FWD_BASE wxConfigBase; | |
36 | ||
37 | class wxDocChildFrameAnyBase; | |
38 | ||
39 | #if wxUSE_STD_IOSTREAM | |
40 | #include "wx/iosfwrap.h" | |
41 | #else | |
42 | #include "wx/stream.h" | |
43 | #endif | |
44 | ||
45 | // Flags for wxDocManager (can be combined). | |
46 | enum | |
47 | { | |
48 | wxDOC_NEW = 1, | |
49 | wxDOC_SILENT = 2 | |
50 | }; | |
51 | ||
52 | // Document template flags | |
53 | enum | |
54 | { | |
55 | wxTEMPLATE_VISIBLE = 1, | |
56 | wxTEMPLATE_INVISIBLE = 2, | |
57 | wxDEFAULT_TEMPLATE_FLAGS = wxTEMPLATE_VISIBLE | |
58 | }; | |
59 | ||
60 | #define wxMAX_FILE_HISTORY 9 | |
61 | ||
62 | class WXDLLIMPEXP_CORE wxDocument : public wxEvtHandler | |
63 | { | |
64 | public: | |
65 | wxDocument(wxDocument *parent = NULL); | |
66 | virtual ~wxDocument(); | |
67 | ||
68 | // accessors | |
69 | void SetFilename(const wxString& filename, bool notifyViews = false); | |
70 | wxString GetFilename() const { return m_documentFile; } | |
71 | ||
72 | void SetTitle(const wxString& title) { m_documentTitle = title; } | |
73 | wxString GetTitle() const { return m_documentTitle; } | |
74 | ||
75 | void SetDocumentName(const wxString& name) { m_documentTypeName = name; } | |
76 | wxString GetDocumentName() const { return m_documentTypeName; } | |
77 | ||
78 | // access the flag indicating whether this document had been already saved, | |
79 | // SetDocumentSaved() is only used internally, don't call it | |
80 | bool GetDocumentSaved() const { return m_savedYet; } | |
81 | void SetDocumentSaved(bool saved = true) { m_savedYet = saved; } | |
82 | ||
83 | // return true if the document hasn't been modified since the last time it | |
84 | // was saved (implying that it returns false if it was never saved, even if | |
85 | // the document is not modified) | |
86 | bool AlreadySaved() const { return !IsModified() && GetDocumentSaved(); } | |
87 | ||
88 | virtual bool Close(); | |
89 | virtual bool Save(); | |
90 | virtual bool SaveAs(); | |
91 | virtual bool Revert(); | |
92 | ||
93 | #if wxUSE_STD_IOSTREAM | |
94 | virtual wxSTD ostream& SaveObject(wxSTD ostream& stream); | |
95 | virtual wxSTD istream& LoadObject(wxSTD istream& stream); | |
96 | #else | |
97 | virtual wxOutputStream& SaveObject(wxOutputStream& stream); | |
98 | virtual wxInputStream& LoadObject(wxInputStream& stream); | |
99 | #endif | |
100 | ||
101 | // Called by wxWidgets | |
102 | virtual bool OnSaveDocument(const wxString& filename); | |
103 | virtual bool OnOpenDocument(const wxString& filename); | |
104 | virtual bool OnNewDocument(); | |
105 | virtual bool OnCloseDocument(); | |
106 | ||
107 | // Prompts for saving if about to close a modified document. Returns true | |
108 | // if ok to close the document (may have saved in the meantime, or set | |
109 | // modified to false) | |
110 | virtual bool OnSaveModified(); | |
111 | ||
112 | // if you override, remember to call the default | |
113 | // implementation (wxDocument::OnChangeFilename) | |
114 | virtual void OnChangeFilename(bool notifyViews); | |
115 | ||
116 | // Called by framework if created automatically by the default document | |
117 | // manager: gives document a chance to initialise and (usually) create a | |
118 | // view | |
119 | virtual bool OnCreate(const wxString& path, long flags); | |
120 | ||
121 | // By default, creates a base wxCommandProcessor. | |
122 | virtual wxCommandProcessor *OnCreateCommandProcessor(); | |
123 | virtual wxCommandProcessor *GetCommandProcessor() const | |
124 | { return m_commandProcessor; } | |
125 | virtual void SetCommandProcessor(wxCommandProcessor *proc) | |
126 | { m_commandProcessor = proc; } | |
127 | ||
128 | // Called after a view is added or removed. The default implementation | |
129 | // deletes the document if this is there are no more views. | |
130 | virtual void OnChangedViewList(); | |
131 | ||
132 | // Called from OnCloseDocument(), does nothing by default but may be | |
133 | // overridden. Return value is ignored. | |
134 | virtual bool DeleteContents(); | |
135 | ||
136 | virtual bool Draw(wxDC&); | |
137 | virtual bool IsModified() const { return m_documentModified; } | |
138 | virtual void Modify(bool mod); | |
139 | ||
140 | virtual bool AddView(wxView *view); | |
141 | virtual bool RemoveView(wxView *view); | |
142 | wxList& GetViews() { return m_documentViews; } | |
143 | const wxList& GetViews() const { return m_documentViews; } | |
144 | wxView *GetFirstView() const; | |
145 | ||
146 | virtual void UpdateAllViews(wxView *sender = NULL, wxObject *hint = NULL); | |
147 | virtual void NotifyClosing(); | |
148 | ||
149 | // Remove all views (because we're closing the document) | |
150 | virtual bool DeleteAllViews(); | |
151 | ||
152 | // Other stuff | |
153 | virtual wxDocManager *GetDocumentManager() const; | |
154 | virtual wxDocTemplate *GetDocumentTemplate() const | |
155 | { return m_documentTemplate; } | |
156 | virtual void SetDocumentTemplate(wxDocTemplate *temp) | |
157 | { m_documentTemplate = temp; } | |
158 | ||
159 | // Get the document name to be shown to the user: the title if there is | |
160 | // any, otherwise the filename if the document was saved and, finally, | |
161 | // "unnamed" otherwise | |
162 | virtual wxString GetUserReadableName() const; | |
163 | ||
164 | #if WXWIN_COMPATIBILITY_2_8 | |
165 | // use GetUserReadableName() instead | |
166 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
167 | virtual bool GetPrintableName(wxString& buf) const | |
168 | ); | |
169 | #endif // WXWIN_COMPATIBILITY_2_8 | |
170 | ||
171 | // Returns a window that can be used as a parent for document-related | |
172 | // dialogs. Override if necessary. | |
173 | virtual wxWindow *GetDocumentWindow() const; | |
174 | ||
175 | protected: | |
176 | wxList m_documentViews; | |
177 | wxString m_documentFile; | |
178 | wxString m_documentTitle; | |
179 | wxString m_documentTypeName; | |
180 | wxDocTemplate* m_documentTemplate; | |
181 | bool m_documentModified; | |
182 | wxDocument* m_documentParent; | |
183 | wxCommandProcessor* m_commandProcessor; | |
184 | bool m_savedYet; | |
185 | ||
186 | // Called by OnSaveDocument and OnOpenDocument to implement standard | |
187 | // Save/Load behavior. Re-implement in derived class for custom | |
188 | // behavior. | |
189 | virtual bool DoSaveDocument(const wxString& file); | |
190 | virtual bool DoOpenDocument(const wxString& file); | |
191 | ||
192 | // the default implementation of GetUserReadableName() | |
193 | wxString DoGetUserReadableName() const; | |
194 | ||
195 | private: | |
196 | DECLARE_ABSTRACT_CLASS(wxDocument) | |
197 | wxDECLARE_NO_COPY_CLASS(wxDocument); | |
198 | }; | |
199 | ||
200 | class WXDLLIMPEXP_CORE wxView: public wxEvtHandler | |
201 | { | |
202 | public: | |
203 | wxView(); | |
204 | virtual ~wxView(); | |
205 | ||
206 | wxDocument *GetDocument() const { return m_viewDocument; } | |
207 | virtual void SetDocument(wxDocument *doc); | |
208 | ||
209 | wxString GetViewName() const { return m_viewTypeName; } | |
210 | void SetViewName(const wxString& name) { m_viewTypeName = name; } | |
211 | ||
212 | wxWindow *GetFrame() const { return m_viewFrame ; } | |
213 | void SetFrame(wxWindow *frame) { m_viewFrame = frame; } | |
214 | ||
215 | virtual void OnActivateView(bool activate, | |
216 | wxView *activeView, | |
217 | wxView *deactiveView); | |
218 | virtual void OnDraw(wxDC *dc) = 0; | |
219 | virtual void OnPrint(wxDC *dc, wxObject *info); | |
220 | virtual void OnUpdate(wxView *sender, wxObject *hint = NULL); | |
221 | virtual void OnClosingDocument() {} | |
222 | virtual void OnChangeFilename(); | |
223 | ||
224 | // Called by framework if created automatically by the default document | |
225 | // manager class: gives view a chance to initialise | |
226 | virtual bool OnCreate(wxDocument *WXUNUSED(doc), long WXUNUSED(flags)) | |
227 | { return true; } | |
228 | ||
229 | // Checks if the view is the last one for the document; if so, asks user | |
230 | // to confirm save data (if modified). If ok, deletes itself and returns | |
231 | // true. | |
232 | virtual bool Close(bool deleteWindow = true); | |
233 | ||
234 | // Override to do cleanup/veto close | |
235 | virtual bool OnClose(bool deleteWindow); | |
236 | ||
237 | // A view's window can call this to notify the view it is (in)active. | |
238 | // The function then notifies the document manager. | |
239 | virtual void Activate(bool activate); | |
240 | ||
241 | wxDocManager *GetDocumentManager() const | |
242 | { return m_viewDocument->GetDocumentManager(); } | |
243 | ||
244 | #if wxUSE_PRINTING_ARCHITECTURE | |
245 | virtual wxPrintout *OnCreatePrintout(); | |
246 | #endif | |
247 | ||
248 | // implementation only | |
249 | // ------------------- | |
250 | ||
251 | // set the associated frame, it is used to reset its view when we're | |
252 | // destroyed | |
253 | void SetDocChildFrame(wxDocChildFrameAnyBase *docChildFrame); | |
254 | ||
255 | protected: | |
256 | // hook the document into event handlers chain here | |
257 | virtual bool TryBefore(wxEvent& event); | |
258 | ||
259 | wxDocument* m_viewDocument; | |
260 | wxString m_viewTypeName; | |
261 | wxWindow* m_viewFrame; | |
262 | ||
263 | wxDocChildFrameAnyBase *m_docChildFrame; | |
264 | ||
265 | private: | |
266 | DECLARE_ABSTRACT_CLASS(wxView) | |
267 | wxDECLARE_NO_COPY_CLASS(wxView); | |
268 | }; | |
269 | ||
270 | // Represents user interface (and other) properties of documents and views | |
271 | class WXDLLIMPEXP_CORE wxDocTemplate: public wxObject | |
272 | { | |
273 | ||
274 | friend class WXDLLIMPEXP_FWD_CORE wxDocManager; | |
275 | ||
276 | public: | |
277 | // Associate document and view types. They're for identifying what view is | |
278 | // associated with what template/document type | |
279 | wxDocTemplate(wxDocManager *manager, | |
280 | const wxString& descr, | |
281 | const wxString& filter, | |
282 | const wxString& dir, | |
283 | const wxString& ext, | |
284 | const wxString& docTypeName, | |
285 | const wxString& viewTypeName, | |
286 | wxClassInfo *docClassInfo = NULL, | |
287 | wxClassInfo *viewClassInfo = NULL, | |
288 | long flags = wxDEFAULT_TEMPLATE_FLAGS); | |
289 | ||
290 | virtual ~wxDocTemplate(); | |
291 | ||
292 | // By default, these two member functions dynamically creates document and | |
293 | // view using dynamic instance construction. Override these if you need a | |
294 | // different method of construction. | |
295 | virtual wxDocument *CreateDocument(const wxString& path, long flags = 0); | |
296 | virtual wxView *CreateView(wxDocument *doc, long flags = 0); | |
297 | ||
298 | // Helper method for CreateDocument; also allows you to do your own document | |
299 | // creation | |
300 | virtual bool InitDocument(wxDocument* doc, | |
301 | const wxString& path, | |
302 | long flags = 0); | |
303 | ||
304 | wxString GetDefaultExtension() const { return m_defaultExt; } | |
305 | wxString GetDescription() const { return m_description; } | |
306 | wxString GetDirectory() const { return m_directory; } | |
307 | wxDocManager *GetDocumentManager() const { return m_documentManager; } | |
308 | void SetDocumentManager(wxDocManager *manager) | |
309 | { m_documentManager = manager; } | |
310 | wxString GetFileFilter() const { return m_fileFilter; } | |
311 | long GetFlags() const { return m_flags; } | |
312 | virtual wxString GetViewName() const { return m_viewTypeName; } | |
313 | virtual wxString GetDocumentName() const { return m_docTypeName; } | |
314 | ||
315 | void SetFileFilter(const wxString& filter) { m_fileFilter = filter; } | |
316 | void SetDirectory(const wxString& dir) { m_directory = dir; } | |
317 | void SetDescription(const wxString& descr) { m_description = descr; } | |
318 | void SetDefaultExtension(const wxString& ext) { m_defaultExt = ext; } | |
319 | void SetFlags(long flags) { m_flags = flags; } | |
320 | ||
321 | bool IsVisible() const { return (m_flags & wxTEMPLATE_VISIBLE) != 0; } | |
322 | ||
323 | wxClassInfo* GetDocClassInfo() const { return m_docClassInfo; } | |
324 | wxClassInfo* GetViewClassInfo() const { return m_viewClassInfo; } | |
325 | ||
326 | virtual bool FileMatchesTemplate(const wxString& path); | |
327 | ||
328 | protected: | |
329 | long m_flags; | |
330 | wxString m_fileFilter; | |
331 | wxString m_directory; | |
332 | wxString m_description; | |
333 | wxString m_defaultExt; | |
334 | wxString m_docTypeName; | |
335 | wxString m_viewTypeName; | |
336 | wxDocManager* m_documentManager; | |
337 | ||
338 | // For dynamic creation of appropriate instances. | |
339 | wxClassInfo* m_docClassInfo; | |
340 | wxClassInfo* m_viewClassInfo; | |
341 | ||
342 | // Called by CreateDocument and CreateView to create the actual | |
343 | // document/view object. | |
344 | // | |
345 | // By default uses the ClassInfo provided to the constructor. Override | |
346 | // these functions to provide a different method of creation. | |
347 | virtual wxDocument *DoCreateDocument(); | |
348 | virtual wxView *DoCreateView(); | |
349 | ||
350 | private: | |
351 | DECLARE_CLASS(wxDocTemplate) | |
352 | wxDECLARE_NO_COPY_CLASS(wxDocTemplate); | |
353 | }; | |
354 | ||
355 | // One object of this class may be created in an application, to manage all | |
356 | // the templates and documents. | |
357 | class WXDLLIMPEXP_CORE wxDocManager: public wxEvtHandler | |
358 | { | |
359 | public: | |
360 | // NB: flags are unused, don't pass wxDOC_XXX to this ctor | |
361 | wxDocManager(long flags = 0, bool initialize = true); | |
362 | virtual ~wxDocManager(); | |
363 | ||
364 | virtual bool Initialize(); | |
365 | ||
366 | // Handlers for common user commands | |
367 | void OnFileClose(wxCommandEvent& event); | |
368 | void OnFileCloseAll(wxCommandEvent& event); | |
369 | void OnFileNew(wxCommandEvent& event); | |
370 | void OnFileOpen(wxCommandEvent& event); | |
371 | void OnFileRevert(wxCommandEvent& event); | |
372 | void OnFileSave(wxCommandEvent& event); | |
373 | void OnFileSaveAs(wxCommandEvent& event); | |
374 | void OnPrint(wxCommandEvent& event); | |
375 | void OnPreview(wxCommandEvent& event); | |
376 | void OnUndo(wxCommandEvent& event); | |
377 | void OnRedo(wxCommandEvent& event); | |
378 | ||
379 | // Handlers for UI update commands | |
380 | void OnUpdateFileOpen(wxUpdateUIEvent& event); | |
381 | void OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event); | |
382 | void OnUpdateFileRevert(wxUpdateUIEvent& event); | |
383 | void OnUpdateFileNew(wxUpdateUIEvent& event); | |
384 | void OnUpdateFileSave(wxUpdateUIEvent& event); | |
385 | void OnUpdateUndo(wxUpdateUIEvent& event); | |
386 | void OnUpdateRedo(wxUpdateUIEvent& event); | |
387 | ||
388 | // called when file format detection didn't work, can be overridden to do | |
389 | // something in this case | |
390 | virtual void OnOpenFileFailure() { } | |
391 | ||
392 | virtual wxDocument *CreateDocument(const wxString& path, long flags = 0); | |
393 | ||
394 | // wrapper around CreateDocument() with a more clear name | |
395 | wxDocument *CreateNewDocument() | |
396 | { return CreateDocument(wxString(), wxDOC_NEW); } | |
397 | ||
398 | virtual wxView *CreateView(wxDocument *doc, long flags = 0); | |
399 | virtual void DeleteTemplate(wxDocTemplate *temp, long flags = 0); | |
400 | virtual bool FlushDoc(wxDocument *doc); | |
401 | virtual wxDocTemplate *MatchTemplate(const wxString& path); | |
402 | virtual wxDocTemplate *SelectDocumentPath(wxDocTemplate **templates, | |
403 | int noTemplates, wxString& path, long flags, bool save = false); | |
404 | virtual wxDocTemplate *SelectDocumentType(wxDocTemplate **templates, | |
405 | int noTemplates, bool sort = false); | |
406 | virtual wxDocTemplate *SelectViewType(wxDocTemplate **templates, | |
407 | int noTemplates, bool sort = false); | |
408 | virtual wxDocTemplate *FindTemplateForPath(const wxString& path); | |
409 | ||
410 | void AssociateTemplate(wxDocTemplate *temp); | |
411 | void DisassociateTemplate(wxDocTemplate *temp); | |
412 | ||
413 | wxDocument *GetCurrentDocument() const; | |
414 | ||
415 | void SetMaxDocsOpen(int n) { m_maxDocsOpen = n; } | |
416 | int GetMaxDocsOpen() const { return m_maxDocsOpen; } | |
417 | ||
418 | // Add and remove a document from the manager's list | |
419 | void AddDocument(wxDocument *doc); | |
420 | void RemoveDocument(wxDocument *doc); | |
421 | ||
422 | // closes all currently open documents | |
423 | bool CloseDocuments(bool force = true); | |
424 | ||
425 | // closes the specified document | |
426 | bool CloseDocument(wxDocument* doc, bool force = false); | |
427 | ||
428 | // Clear remaining documents and templates | |
429 | bool Clear(bool force = true); | |
430 | ||
431 | // Views or windows should inform the document manager | |
432 | // when a view is going in or out of focus | |
433 | virtual void ActivateView(wxView *view, bool activate = true); | |
434 | virtual wxView *GetCurrentView() const { return m_currentView; } | |
435 | ||
436 | wxList& GetDocuments() { return m_docs; } | |
437 | wxList& GetTemplates() { return m_templates; } | |
438 | ||
439 | // Return the default name for a new document (by default returns strings | |
440 | // in the form "unnamed <counter>" but can be overridden) | |
441 | virtual wxString MakeNewDocumentName(); | |
442 | ||
443 | // Make a frame title (override this to do something different) | |
444 | virtual wxString MakeFrameTitle(wxDocument* doc); | |
445 | ||
446 | virtual wxFileHistory *OnCreateFileHistory(); | |
447 | virtual wxFileHistory *GetFileHistory() const { return m_fileHistory; } | |
448 | ||
449 | // File history management | |
450 | virtual void AddFileToHistory(const wxString& file); | |
451 | virtual void RemoveFileFromHistory(size_t i); | |
452 | virtual size_t GetHistoryFilesCount() const; | |
453 | virtual wxString GetHistoryFile(size_t i) const; | |
454 | virtual void FileHistoryUseMenu(wxMenu *menu); | |
455 | virtual void FileHistoryRemoveMenu(wxMenu *menu); | |
456 | #if wxUSE_CONFIG | |
457 | virtual void FileHistoryLoad(const wxConfigBase& config); | |
458 | virtual void FileHistorySave(wxConfigBase& config); | |
459 | #endif // wxUSE_CONFIG | |
460 | ||
461 | virtual void FileHistoryAddFilesToMenu(); | |
462 | virtual void FileHistoryAddFilesToMenu(wxMenu* menu); | |
463 | ||
464 | wxString GetLastDirectory() const; | |
465 | void SetLastDirectory(const wxString& dir) { m_lastDirectory = dir; } | |
466 | ||
467 | // Get the current document manager | |
468 | static wxDocManager* GetDocumentManager() { return sm_docManager; } | |
469 | ||
470 | #if WXWIN_COMPATIBILITY_2_8 | |
471 | // deprecated, override GetDefaultName() instead | |
472 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
473 | virtual bool MakeDefaultName(wxString& buf) | |
474 | ); | |
475 | #endif | |
476 | ||
477 | #if WXWIN_COMPATIBILITY_2_6 | |
478 | // deprecated, use GetHistoryFilesCount() instead | |
479 | wxDEPRECATED( size_t GetNoHistoryFiles() const ); | |
480 | #endif // WXWIN_COMPATIBILITY_2_6 | |
481 | ||
482 | protected: | |
483 | #if wxUSE_PRINTING_ARCHITECTURE | |
484 | virtual wxPreviewFrame* CreatePreviewFrame(wxPrintPreviewBase* preview, | |
485 | wxWindow *parent, | |
486 | const wxString& title); | |
487 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
488 | ||
489 | // hook the currently active view into event handlers chain here | |
490 | virtual bool TryBefore(wxEvent& event); | |
491 | ||
492 | // return the command processor for the current document, if any | |
493 | wxCommandProcessor *GetCurrentCommandProcessor() const; | |
494 | ||
495 | // this method tries to find an active view harder than GetCurrentView(): | |
496 | // if the latter is NULL, it also checks if we don't have just a single | |
497 | // view and returns it then | |
498 | wxView *GetActiveView() const; | |
499 | ||
500 | ||
501 | int m_defaultDocumentNameCounter; | |
502 | int m_maxDocsOpen; | |
503 | wxList m_docs; | |
504 | wxList m_templates; | |
505 | wxView* m_currentView; | |
506 | wxFileHistory* m_fileHistory; | |
507 | wxString m_lastDirectory; | |
508 | static wxDocManager* sm_docManager; | |
509 | ||
510 | DECLARE_EVENT_TABLE() | |
511 | DECLARE_DYNAMIC_CLASS(wxDocManager) | |
512 | wxDECLARE_NO_COPY_CLASS(wxDocManager); | |
513 | }; | |
514 | ||
515 | #if WXWIN_COMPATIBILITY_2_6 | |
516 | inline size_t wxDocManager::GetNoHistoryFiles() const | |
517 | { | |
518 | return GetHistoryFilesCount(); | |
519 | } | |
520 | #endif // WXWIN_COMPATIBILITY_2_6 | |
521 | ||
522 | // ---------------------------------------------------------------------------- | |
523 | // Base class for child frames -- this is what wxView renders itself into | |
524 | // | |
525 | // Notice that this is a mix-in class so it doesn't derive from wxWindow, only | |
526 | // wxDocChildFrameAny does | |
527 | // ---------------------------------------------------------------------------- | |
528 | ||
529 | class WXDLLIMPEXP_CORE wxDocChildFrameAnyBase | |
530 | { | |
531 | public: | |
532 | // default ctor, use Create() after it | |
533 | wxDocChildFrameAnyBase() | |
534 | { | |
535 | m_childDocument = NULL; | |
536 | m_childView = NULL; | |
537 | m_win = NULL; | |
538 | } | |
539 | ||
540 | // full ctor equivalent to using the default one and Create(0 | |
541 | wxDocChildFrameAnyBase(wxDocument *doc, wxView *view, wxWindow *win) | |
542 | { | |
543 | Create(doc, view, win); | |
544 | } | |
545 | ||
546 | // method which must be called for an object created using the default ctor | |
547 | // | |
548 | // note that it returns bool just for consistency with Create() methods in | |
549 | // other classes, we never return false from here | |
550 | bool Create(wxDocument *doc, wxView *view, wxWindow *win) | |
551 | { | |
552 | m_childDocument = doc; | |
553 | m_childView = view; | |
554 | m_win = win; | |
555 | ||
556 | if ( view ) | |
557 | view->SetDocChildFrame(this); | |
558 | ||
559 | return true; | |
560 | } | |
561 | ||
562 | // dtor doesn't need to be virtual, an object should never be destroyed via | |
563 | // a pointer to this class | |
564 | ~wxDocChildFrameAnyBase() | |
565 | { | |
566 | // prevent the view from deleting us if we're being deleted directly | |
567 | // (and not via Close() + Destroy()) | |
568 | if ( m_childView ) | |
569 | m_childView->SetDocChildFrame(NULL); | |
570 | } | |
571 | ||
572 | wxDocument *GetDocument() const { return m_childDocument; } | |
573 | wxView *GetView() const { return m_childView; } | |
574 | void SetDocument(wxDocument *doc) { m_childDocument = doc; } | |
575 | void SetView(wxView *view) { m_childView = view; } | |
576 | ||
577 | wxWindow *GetWindow() const { return m_win; } | |
578 | ||
579 | protected: | |
580 | // we're not a wxEvtHandler but we provide this wxEvtHandler-like function | |
581 | // which is called from TryBefore() of the derived classes to give our view | |
582 | // a chance to process the message before the frame event handlers are used | |
583 | bool TryProcessEvent(wxEvent& event) | |
584 | { | |
585 | return m_childView && m_childView->ProcessEventLocally(event); | |
586 | } | |
587 | ||
588 | // called from EVT_CLOSE handler in the frame: check if we can close and do | |
589 | // cleanup if so; veto the event otherwise | |
590 | bool CloseView(wxCloseEvent& event); | |
591 | ||
592 | ||
593 | wxDocument* m_childDocument; | |
594 | wxView* m_childView; | |
595 | ||
596 | // the associated window: having it here is not terribly elegant but it | |
597 | // allows us to avoid having any virtual functions in this class | |
598 | wxWindow* m_win; | |
599 | ||
600 | ||
601 | wxDECLARE_NO_COPY_CLASS(wxDocChildFrameAnyBase); | |
602 | }; | |
603 | ||
604 | // ---------------------------------------------------------------------------- | |
605 | // Template implementing child frame concept using the given wxFrame-like class | |
606 | // | |
607 | // This is used to define wxDocChildFrame and wxDocMDIChildFrame: ChildFrame is | |
608 | // a wxFrame or wxMDIChildFrame (although in theory it could be any wxWindow- | |
609 | // derived class as long as it provided a ctor with the same signature as | |
610 | // wxFrame and OnActivate() method) and ParentFrame is either wxFrame or | |
611 | // wxMDIParentFrame. | |
612 | // ---------------------------------------------------------------------------- | |
613 | ||
614 | template <class ChildFrame, class ParentFrame> | |
615 | class WXDLLIMPEXP_CORE wxDocChildFrameAny : public ChildFrame, | |
616 | public wxDocChildFrameAnyBase | |
617 | { | |
618 | public: | |
619 | typedef ChildFrame BaseClass; | |
620 | ||
621 | // default ctor, use Create after it | |
622 | wxDocChildFrameAny() { } | |
623 | ||
624 | // ctor for a frame showing the given view of the specified document | |
625 | wxDocChildFrameAny(wxDocument *doc, | |
626 | wxView *view, | |
627 | ParentFrame *parent, | |
628 | wxWindowID id, | |
629 | const wxString& title, | |
630 | const wxPoint& pos = wxDefaultPosition, | |
631 | const wxSize& size = wxDefaultSize, | |
632 | long style = wxDEFAULT_FRAME_STYLE, | |
633 | const wxString& name = wxFrameNameStr) | |
634 | { | |
635 | Create(doc, view, parent, id, title, pos, size, style, name); | |
636 | } | |
637 | ||
638 | bool Create(wxDocument *doc, | |
639 | wxView *view, | |
640 | ParentFrame *parent, | |
641 | wxWindowID id, | |
642 | const wxString& title, | |
643 | const wxPoint& pos = wxDefaultPosition, | |
644 | const wxSize& size = wxDefaultSize, | |
645 | long style = wxDEFAULT_FRAME_STYLE, | |
646 | const wxString& name = wxFrameNameStr) | |
647 | { | |
648 | if ( !wxDocChildFrameAnyBase::Create(doc, view, this) ) | |
649 | return false; | |
650 | ||
651 | if ( !BaseClass::Create(parent, id, title, pos, size, style, name) ) | |
652 | return false; | |
653 | ||
654 | this->Connect(wxEVT_ACTIVATE, | |
655 | wxActivateEventHandler(wxDocChildFrameAny::OnActivate)); | |
656 | this->Connect(wxEVT_CLOSE_WINDOW, | |
657 | wxCloseEventHandler(wxDocChildFrameAny::OnCloseWindow)); | |
658 | ||
659 | return true; | |
660 | } | |
661 | ||
662 | virtual bool Destroy() | |
663 | { | |
664 | // FIXME: why exactly do we do this? to avoid activation events during | |
665 | // destructions maybe? | |
666 | m_childView = NULL; | |
667 | return BaseClass::Destroy(); | |
668 | } | |
669 | ||
670 | protected: | |
671 | // hook the child view into event handlers chain here | |
672 | virtual bool TryBefore(wxEvent& event) | |
673 | { | |
674 | return TryProcessEvent(event) || BaseClass::TryBefore(event); | |
675 | } | |
676 | ||
677 | private: | |
678 | void OnActivate(wxActivateEvent& event) | |
679 | { | |
680 | BaseClass::OnActivate(event); | |
681 | ||
682 | if ( m_childView ) | |
683 | m_childView->Activate(event.GetActive()); | |
684 | } | |
685 | ||
686 | void OnCloseWindow(wxCloseEvent& event) | |
687 | { | |
688 | if ( CloseView(event) ) | |
689 | Destroy(); | |
690 | //else: vetoed | |
691 | } | |
692 | ||
693 | wxDECLARE_NO_COPY_TEMPLATE_CLASS_2(wxDocChildFrameAny, | |
694 | ChildFrame, ParentFrame); | |
695 | }; | |
696 | ||
697 | // ---------------------------------------------------------------------------- | |
698 | // A default child frame: we need to define it as a class just for wxRTTI, | |
699 | // otherwise we could simply typedef it | |
700 | // ---------------------------------------------------------------------------- | |
701 | ||
702 | #ifdef __VISUALC6__ | |
703 | // "non dll-interface class 'wxDocChildFrameAny<>' used as base interface | |
704 | // for dll-interface class 'wxDocChildFrame'" -- this is bogus as the | |
705 | // template will be DLL-exported but only once it is used as base class | |
706 | // here! | |
707 | #pragma warning (push) | |
708 | #pragma warning (disable:4275) | |
709 | #endif | |
710 | ||
711 | typedef wxDocChildFrameAny<wxFrame, wxFrame> wxDocChildFrameBase; | |
712 | ||
713 | class WXDLLIMPEXP_CORE wxDocChildFrame : public wxDocChildFrameBase | |
714 | { | |
715 | public: | |
716 | wxDocChildFrame() | |
717 | { | |
718 | } | |
719 | ||
720 | wxDocChildFrame(wxDocument *doc, | |
721 | wxView *view, | |
722 | wxFrame *parent, | |
723 | wxWindowID id, | |
724 | const wxString& title, | |
725 | const wxPoint& pos = wxDefaultPosition, | |
726 | const wxSize& size = wxDefaultSize, | |
727 | long style = wxDEFAULT_FRAME_STYLE, | |
728 | const wxString& name = wxFrameNameStr) | |
729 | : wxDocChildFrameBase(doc, view, | |
730 | parent, id, title, pos, size, style, name) | |
731 | { | |
732 | } | |
733 | ||
734 | bool Create(wxDocument *doc, | |
735 | wxView *view, | |
736 | wxFrame *parent, | |
737 | wxWindowID id, | |
738 | const wxString& title, | |
739 | const wxPoint& pos = wxDefaultPosition, | |
740 | const wxSize& size = wxDefaultSize, | |
741 | long style = wxDEFAULT_FRAME_STYLE, | |
742 | const wxString& name = wxFrameNameStr) | |
743 | { | |
744 | return wxDocChildFrameBase::Create | |
745 | ( | |
746 | doc, view, | |
747 | parent, id, title, pos, size, style, name | |
748 | ); | |
749 | } | |
750 | ||
751 | private: | |
752 | DECLARE_CLASS(wxDocChildFrame) | |
753 | wxDECLARE_NO_COPY_CLASS(wxDocChildFrame); | |
754 | }; | |
755 | ||
756 | // ---------------------------------------------------------------------------- | |
757 | // wxDocParentFrame and related classes. | |
758 | // | |
759 | // As with wxDocChildFrame we define a template base class used by both normal | |
760 | // and MDI versions | |
761 | // ---------------------------------------------------------------------------- | |
762 | ||
763 | // Base class containing type-independent code of wxDocParentFrameAny | |
764 | // | |
765 | // Similarly to wxDocChildFrameAnyBase, this class is a mix-in and doesn't | |
766 | // derive from wxWindow. | |
767 | class WXDLLIMPEXP_CORE wxDocParentFrameAnyBase | |
768 | { | |
769 | public: | |
770 | wxDocParentFrameAnyBase() { m_docManager = NULL; } | |
771 | ||
772 | wxDocManager *GetDocumentManager() const { return m_docManager; } | |
773 | ||
774 | protected: | |
775 | // Open the MRU file with the given index in our associated file history. | |
776 | // | |
777 | // This is called from the derived class event handler for the MRU menu | |
778 | // items. | |
779 | void DoOpenMRUFile(unsigned n); | |
780 | ||
781 | wxDocManager *m_docManager; | |
782 | ||
783 | wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAnyBase); | |
784 | }; | |
785 | ||
786 | // This is similar to wxDocChildFrameAny and is used to provide common | |
787 | // implementation for both wxDocParentFrame and wxDocMDIParentFrame | |
788 | template <class BaseFrame> | |
789 | class WXDLLIMPEXP_CORE wxDocParentFrameAny : public BaseFrame, | |
790 | public wxDocParentFrameAnyBase | |
791 | { | |
792 | public: | |
793 | wxDocParentFrameAny() { } | |
794 | wxDocParentFrameAny(wxDocManager *manager, | |
795 | wxFrame *frame, | |
796 | wxWindowID id, | |
797 | const wxString& title, | |
798 | const wxPoint& pos = wxDefaultPosition, | |
799 | const wxSize& size = wxDefaultSize, | |
800 | long style = wxDEFAULT_FRAME_STYLE, | |
801 | const wxString& name = wxFrameNameStr) | |
802 | { | |
803 | Create(manager, frame, id, title, pos, size, style, name); | |
804 | } | |
805 | ||
806 | bool Create(wxDocManager *manager, | |
807 | wxFrame *frame, | |
808 | wxWindowID id, | |
809 | const wxString& title, | |
810 | const wxPoint& pos = wxDefaultPosition, | |
811 | const wxSize& size = wxDefaultSize, | |
812 | long style = wxDEFAULT_FRAME_STYLE, | |
813 | const wxString& name = wxFrameNameStr) | |
814 | { | |
815 | m_docManager = manager; | |
816 | ||
817 | if ( !BaseFrame::Create(frame, id, title, pos, size, style, name) ) | |
818 | return false; | |
819 | ||
820 | this->Connect(wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, | |
821 | wxCommandEventHandler(wxDocParentFrameAny::OnExit)); | |
822 | this->Connect(wxID_FILE1, wxID_FILE9, wxEVT_COMMAND_MENU_SELECTED, | |
823 | wxCommandEventHandler(wxDocParentFrameAny::OnMRUFile)); | |
824 | this->Connect(wxEVT_CLOSE_WINDOW, | |
825 | wxCloseEventHandler(wxDocParentFrameAny::OnCloseWindow)); | |
826 | ||
827 | return true; | |
828 | } | |
829 | ||
830 | protected: | |
831 | // hook the document manager into event handling chain here | |
832 | virtual bool TryBefore(wxEvent& event) | |
833 | { | |
834 | if ( m_docManager && m_docManager->ProcessEventLocally(event) ) | |
835 | return true; | |
836 | ||
837 | return BaseFrame::TryBefore(event); | |
838 | } | |
839 | ||
840 | private: | |
841 | void OnExit(wxCommandEvent& WXUNUSED(event)) | |
842 | { | |
843 | this->Close(); | |
844 | } | |
845 | ||
846 | void OnMRUFile(wxCommandEvent& event) | |
847 | { | |
848 | DoOpenMRUFile(event.GetId() - wxID_FILE1); | |
849 | } | |
850 | ||
851 | void OnCloseWindow(wxCloseEvent& event) | |
852 | { | |
853 | if ( m_docManager && !m_docManager->Clear(!event.CanVeto()) ) | |
854 | { | |
855 | // The user decided not to close finally, abort. | |
856 | event.Veto(); | |
857 | } | |
858 | else | |
859 | { | |
860 | // Just skip the event, base class handler will destroy the window. | |
861 | event.Skip(); | |
862 | } | |
863 | } | |
864 | ||
865 | ||
866 | wxDECLARE_NO_COPY_CLASS(wxDocParentFrameAny); | |
867 | }; | |
868 | ||
869 | typedef wxDocParentFrameAny<wxFrame> wxDocParentFrameBase; | |
870 | ||
871 | class WXDLLIMPEXP_CORE wxDocParentFrame : public wxDocParentFrameBase | |
872 | { | |
873 | public: | |
874 | wxDocParentFrame() : wxDocParentFrameBase() { } | |
875 | ||
876 | wxDocParentFrame(wxDocManager *manager, | |
877 | wxFrame *parent, | |
878 | wxWindowID id, | |
879 | const wxString& title, | |
880 | const wxPoint& pos = wxDefaultPosition, | |
881 | const wxSize& size = wxDefaultSize, | |
882 | long style = wxDEFAULT_FRAME_STYLE, | |
883 | const wxString& name = wxFrameNameStr) | |
884 | : wxDocParentFrameBase(manager, | |
885 | parent, id, title, pos, size, style, name) | |
886 | { | |
887 | } | |
888 | ||
889 | bool Create(wxDocManager *manager, | |
890 | wxFrame *parent, | |
891 | wxWindowID id, | |
892 | const wxString& title, | |
893 | const wxPoint& pos = wxDefaultPosition, | |
894 | const wxSize& size = wxDefaultSize, | |
895 | long style = wxDEFAULT_FRAME_STYLE, | |
896 | const wxString& name = wxFrameNameStr) | |
897 | { | |
898 | return wxDocParentFrameBase::Create(manager, | |
899 | parent, id, title, | |
900 | pos, size, style, name); | |
901 | } | |
902 | ||
903 | private: | |
904 | DECLARE_CLASS(wxDocParentFrame) | |
905 | wxDECLARE_NO_COPY_CLASS(wxDocParentFrame); | |
906 | }; | |
907 | ||
908 | #ifdef __VISUALC6__ | |
909 | // reenable warning 4275 | |
910 | #pragma warning (pop) | |
911 | #endif | |
912 | ||
913 | // ---------------------------------------------------------------------------- | |
914 | // Provide simple default printing facilities | |
915 | // ---------------------------------------------------------------------------- | |
916 | ||
917 | #if wxUSE_PRINTING_ARCHITECTURE | |
918 | class WXDLLIMPEXP_CORE wxDocPrintout : public wxPrintout | |
919 | { | |
920 | public: | |
921 | wxDocPrintout(wxView *view = NULL, const wxString& title = wxT("Printout")); | |
922 | ||
923 | // implement wxPrintout methods | |
924 | virtual bool OnPrintPage(int page); | |
925 | virtual bool HasPage(int page); | |
926 | virtual bool OnBeginDocument(int startPage, int endPage); | |
927 | virtual void GetPageInfo(int *minPage, int *maxPage, | |
928 | int *selPageFrom, int *selPageTo); | |
929 | ||
930 | virtual wxView *GetView() { return m_printoutView; } | |
931 | ||
932 | protected: | |
933 | wxView* m_printoutView; | |
934 | ||
935 | private: | |
936 | DECLARE_DYNAMIC_CLASS(wxDocPrintout) | |
937 | wxDECLARE_NO_COPY_CLASS(wxDocPrintout); | |
938 | }; | |
939 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
940 | ||
941 | // For compatibility with existing file formats: | |
942 | // converts from/to a stream to/from a temporary file. | |
943 | #if wxUSE_STD_IOSTREAM | |
944 | bool WXDLLIMPEXP_CORE | |
945 | wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream); | |
946 | bool WXDLLIMPEXP_CORE | |
947 | wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename); | |
948 | #else | |
949 | bool WXDLLIMPEXP_CORE | |
950 | wxTransferFileToStream(const wxString& filename, wxOutputStream& stream); | |
951 | bool WXDLLIMPEXP_CORE | |
952 | wxTransferStreamToFile(wxInputStream& stream, const wxString& filename); | |
953 | #endif // wxUSE_STD_IOSTREAM | |
954 | ||
955 | ||
956 | // these flags are not used anywhere by wxWidgets and kept only for an unlikely | |
957 | // case of existing user code using them for its own purposes | |
958 | #ifdef WXWIN_COMPATIBILITY_2_8 | |
959 | enum | |
960 | { | |
961 | wxDOC_SDI = 1, | |
962 | wxDOC_MDI, | |
963 | wxDEFAULT_DOCMAN_FLAGS = wxDOC_SDI | |
964 | }; | |
965 | #endif // WXWIN_COMPATIBILITY_2_8 | |
966 | ||
967 | #endif // wxUSE_DOC_VIEW_ARCHITECTURE | |
968 | ||
969 | #endif // _WX_DOCH__ |