]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/docview.cpp | |
3 | // Purpose: Document/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 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "docview.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #if wxUSE_DOC_VIEW_ARCHITECTURE | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/string.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/app.h" | |
37 | #include "wx/dc.h" | |
38 | #include "wx/dialog.h" | |
39 | #include "wx/menu.h" | |
40 | #include "wx/list.h" | |
41 | #include "wx/filedlg.h" | |
42 | #include "wx/intl.h" | |
43 | #include "wx/log.h" | |
44 | #endif | |
45 | ||
46 | #include "wx/ffile.h" | |
47 | ||
48 | #ifdef __WXMAC__ | |
49 | #include "wx/filename.h" | |
50 | #endif | |
51 | ||
52 | #ifdef __WXGTK__ | |
53 | #include "wx/mdi.h" | |
54 | #endif | |
55 | ||
56 | #if wxUSE_PRINTING_ARCHITECTURE | |
57 | #include "wx/prntbase.h" | |
58 | #include "wx/printdlg.h" | |
59 | #endif | |
60 | ||
61 | #include "wx/msgdlg.h" | |
62 | #include "wx/choicdlg.h" | |
63 | #include "wx/docview.h" | |
64 | #include "wx/confbase.h" | |
65 | #include "wx/file.h" | |
66 | #include "wx/cmdproc.h" | |
67 | ||
68 | #include <stdio.h> | |
69 | #include <string.h> | |
70 | ||
71 | #if wxUSE_STD_IOSTREAM | |
72 | #include "wx/ioswrap.h" | |
73 | #if wxUSE_IOSTREAMH | |
74 | #include <fstream.h> | |
75 | #else | |
76 | #include <fstream> | |
77 | #endif | |
78 | #else | |
79 | #include "wx/wfstream.h" | |
80 | #endif | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxWindows macros | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler) | |
87 | IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler) | |
88 | IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject) | |
89 | IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler) | |
90 | IMPLEMENT_CLASS(wxDocChildFrame, wxFrame) | |
91 | IMPLEMENT_CLASS(wxDocParentFrame, wxFrame) | |
92 | ||
93 | #if wxUSE_PRINTING_ARCHITECTURE | |
94 | IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout) | |
95 | #endif | |
96 | ||
97 | IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject) | |
98 | ||
99 | // ---------------------------------------------------------------------------- | |
100 | // function prototypes | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | static inline wxString FindExtension(const wxChar *path); | |
104 | static wxWindow* wxFindSuitableParent(void); | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // local constants | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | static const wxChar *s_MRUEntryFormat = wxT("&%d %s"); | |
111 | ||
112 | // ============================================================================ | |
113 | // implementation | |
114 | // ============================================================================ | |
115 | ||
116 | // ---------------------------------------------------------------------------- | |
117 | // local functions | |
118 | // ---------------------------------------------------------------------------- | |
119 | ||
120 | static wxString FindExtension(const wxChar *path) | |
121 | { | |
122 | wxString ext; | |
123 | wxSplitPath(path, NULL, NULL, &ext); | |
124 | ||
125 | // VZ: extensions are considered not case sensitive - is this really a good | |
126 | // idea? | |
127 | return ext.MakeLower(); | |
128 | } | |
129 | ||
130 | // ---------------------------------------------------------------------------- | |
131 | // Definition of wxDocument | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
134 | wxDocument::wxDocument(wxDocument *parent) | |
135 | { | |
136 | m_documentModified = FALSE; | |
137 | m_documentParent = parent; | |
138 | m_documentTemplate = (wxDocTemplate *) NULL; | |
139 | m_commandProcessor = (wxCommandProcessor*) NULL; | |
140 | m_savedYet = FALSE; | |
141 | } | |
142 | ||
143 | bool wxDocument::DeleteContents() | |
144 | { | |
145 | return TRUE; | |
146 | } | |
147 | ||
148 | wxDocument::~wxDocument() | |
149 | { | |
150 | DeleteContents(); | |
151 | ||
152 | if (m_commandProcessor) | |
153 | delete m_commandProcessor; | |
154 | ||
155 | if (GetDocumentManager()) | |
156 | GetDocumentManager()->RemoveDocument(this); | |
157 | ||
158 | // Not safe to do here, since it'll invoke virtual view functions | |
159 | // expecting to see valid derived objects: and by the time we get here, | |
160 | // we've called destructors higher up. | |
161 | //DeleteAllViews(); | |
162 | } | |
163 | ||
164 | bool wxDocument::Close() | |
165 | { | |
166 | if (OnSaveModified()) | |
167 | return OnCloseDocument(); | |
168 | else | |
169 | return FALSE; | |
170 | } | |
171 | ||
172 | bool wxDocument::OnCloseDocument() | |
173 | { | |
174 | // Tell all views that we're about to close | |
175 | NotifyClosing(); | |
176 | DeleteContents(); | |
177 | Modify(FALSE); | |
178 | return TRUE; | |
179 | } | |
180 | ||
181 | // Note that this implicitly deletes the document when the last view is | |
182 | // deleted. | |
183 | bool wxDocument::DeleteAllViews() | |
184 | { | |
185 | wxDocManager* manager = GetDocumentManager(); | |
186 | wxList::iterator it, en; | |
187 | ||
188 | for ( it = m_documentViews.begin(), en = m_documentViews.end(); | |
189 | it != en; | |
190 | ) | |
191 | { | |
192 | wxView *view = (wxView *)*it; | |
193 | if (!view->Close()) | |
194 | return FALSE; | |
195 | ||
196 | wxList::iterator next = it; ++next; | |
197 | ||
198 | delete view; // Deletes node implicitly | |
199 | it = next; | |
200 | } | |
201 | // If we haven't yet deleted the document (for example | |
202 | // if there were no views) then delete it. | |
203 | if (manager && manager->GetDocuments().Member(this)) | |
204 | delete this; | |
205 | ||
206 | return TRUE; | |
207 | } | |
208 | ||
209 | wxView *wxDocument::GetFirstView() const | |
210 | { | |
211 | if (m_documentViews.GetCount() == 0) | |
212 | return (wxView *) NULL; | |
213 | return (wxView *)m_documentViews.GetFirst()->GetData(); | |
214 | } | |
215 | ||
216 | wxDocManager *wxDocument::GetDocumentManager() const | |
217 | { | |
218 | return (m_documentTemplate ? m_documentTemplate->GetDocumentManager() : (wxDocManager*) NULL); | |
219 | } | |
220 | ||
221 | bool wxDocument::OnNewDocument() | |
222 | { | |
223 | if (!OnSaveModified()) | |
224 | return FALSE; | |
225 | ||
226 | if (OnCloseDocument()==FALSE) return FALSE; | |
227 | DeleteContents(); | |
228 | Modify(FALSE); | |
229 | SetDocumentSaved(FALSE); | |
230 | ||
231 | wxString name; | |
232 | GetDocumentManager()->MakeDefaultName(name); | |
233 | SetTitle(name); | |
234 | SetFilename(name, TRUE); | |
235 | ||
236 | return TRUE; | |
237 | } | |
238 | ||
239 | bool wxDocument::Save() | |
240 | { | |
241 | if (!IsModified() && m_savedYet) | |
242 | return TRUE; | |
243 | ||
244 | if ( m_documentFile.empty() || !m_savedYet ) | |
245 | return SaveAs(); | |
246 | ||
247 | return OnSaveDocument(m_documentFile); | |
248 | } | |
249 | ||
250 | bool wxDocument::SaveAs() | |
251 | { | |
252 | wxDocTemplate *docTemplate = GetDocumentTemplate(); | |
253 | if (!docTemplate) | |
254 | return FALSE; | |
255 | ||
256 | wxString tmp = wxFileSelector(_("Save as"), | |
257 | docTemplate->GetDirectory(), | |
258 | wxFileNameFromPath(GetFilename()), | |
259 | docTemplate->GetDefaultExtension(), | |
260 | docTemplate->GetFileFilter(), | |
261 | wxSAVE | wxOVERWRITE_PROMPT, | |
262 | GetDocumentWindow()); | |
263 | ||
264 | if (tmp.IsEmpty()) | |
265 | return FALSE; | |
266 | ||
267 | wxString fileName(tmp); | |
268 | wxString path, name, ext; | |
269 | wxSplitPath(fileName, & path, & name, & ext); | |
270 | ||
271 | if (ext.IsEmpty() || ext == wxT("")) | |
272 | { | |
273 | fileName += wxT("."); | |
274 | fileName += docTemplate->GetDefaultExtension(); | |
275 | } | |
276 | ||
277 | SetFilename(fileName); | |
278 | SetTitle(wxFileNameFromPath(fileName)); | |
279 | ||
280 | // Notify the views that the filename has changed | |
281 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); | |
282 | while (node) | |
283 | { | |
284 | wxView *view = (wxView *)node->GetData(); | |
285 | view->OnChangeFilename(); | |
286 | node = node->GetNext(); | |
287 | } | |
288 | ||
289 | // Files that were not saved correctly are not added to the FileHistory. | |
290 | if (!OnSaveDocument(m_documentFile)) | |
291 | return FALSE; | |
292 | ||
293 | // A file that doesn't use the default extension of its document template cannot be opened | |
294 | // via the FileHistory, so we do not add it. | |
295 | if (docTemplate->FileMatchesTemplate(fileName)) | |
296 | { | |
297 | GetDocumentManager()->AddFileToHistory(fileName); | |
298 | } | |
299 | else | |
300 | { | |
301 | // The user will probably not be able to open the file again, so | |
302 | // we could warn about the wrong file-extension here. | |
303 | } | |
304 | return TRUE; | |
305 | } | |
306 | ||
307 | bool wxDocument::OnSaveDocument(const wxString& file) | |
308 | { | |
309 | if ( !file ) | |
310 | return FALSE; | |
311 | ||
312 | wxString msgTitle; | |
313 | if (wxTheApp->GetAppName() != wxT("")) | |
314 | msgTitle = wxTheApp->GetAppName(); | |
315 | else | |
316 | msgTitle = wxString(_("File error")); | |
317 | ||
318 | #if wxUSE_STD_IOSTREAM | |
319 | wxSTD ofstream store(file.mb_str()); | |
320 | if (store.fail() || store.bad()) | |
321 | #else | |
322 | wxFileOutputStream store(file); | |
323 | if (store.GetLastError() != wxSTREAM_NO_ERROR) | |
324 | #endif | |
325 | { | |
326 | (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION, | |
327 | GetDocumentWindow()); | |
328 | // Saving error | |
329 | return FALSE; | |
330 | } | |
331 | if (!SaveObject(store)) | |
332 | { | |
333 | (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION, | |
334 | GetDocumentWindow()); | |
335 | // Saving error | |
336 | return FALSE; | |
337 | } | |
338 | Modify(FALSE); | |
339 | SetFilename(file); | |
340 | SetDocumentSaved(TRUE); | |
341 | #ifdef __WXMAC__ | |
342 | wxFileName fn(file) ; | |
343 | fn.MacSetDefaultTypeAndCreator() ; | |
344 | #endif | |
345 | return TRUE; | |
346 | } | |
347 | ||
348 | bool wxDocument::OnOpenDocument(const wxString& file) | |
349 | { | |
350 | if (!OnSaveModified()) | |
351 | return FALSE; | |
352 | ||
353 | wxString msgTitle; | |
354 | if (wxTheApp->GetAppName() != wxT("")) | |
355 | msgTitle = wxTheApp->GetAppName(); | |
356 | else | |
357 | msgTitle = wxString(_("File error")); | |
358 | ||
359 | #if wxUSE_STD_IOSTREAM | |
360 | wxSTD ifstream store(file.mb_str()); | |
361 | if (store.fail() || store.bad()) | |
362 | #else | |
363 | wxFileInputStream store(file); | |
364 | if (store.GetLastError() != wxSTREAM_NO_ERROR) | |
365 | #endif | |
366 | { | |
367 | (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION, | |
368 | GetDocumentWindow()); | |
369 | return FALSE; | |
370 | } | |
371 | #if wxUSE_STD_IOSTREAM | |
372 | LoadObject(store); | |
373 | if ( !store && !store.eof() ) | |
374 | #else | |
375 | int res = LoadObject(store).GetLastError(); | |
376 | if ((res != wxSTREAM_NO_ERROR) && | |
377 | (res != wxSTREAM_EOF)) | |
378 | #endif | |
379 | { | |
380 | (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK|wxICON_EXCLAMATION, | |
381 | GetDocumentWindow()); | |
382 | return FALSE; | |
383 | } | |
384 | SetFilename(file, TRUE); | |
385 | Modify(FALSE); | |
386 | m_savedYet = TRUE; | |
387 | ||
388 | UpdateAllViews(); | |
389 | ||
390 | return TRUE; | |
391 | } | |
392 | ||
393 | #if wxUSE_STD_IOSTREAM | |
394 | wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream) | |
395 | #else | |
396 | wxInputStream& wxDocument::LoadObject(wxInputStream& stream) | |
397 | #endif | |
398 | { | |
399 | return stream; | |
400 | } | |
401 | ||
402 | #if wxUSE_STD_IOSTREAM | |
403 | wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream) | |
404 | #else | |
405 | wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream) | |
406 | #endif | |
407 | { | |
408 | return stream; | |
409 | } | |
410 | ||
411 | bool wxDocument::Revert() | |
412 | { | |
413 | return FALSE; | |
414 | } | |
415 | ||
416 | ||
417 | // Get title, or filename if no title, else unnamed | |
418 | bool wxDocument::GetPrintableName(wxString& buf) const | |
419 | { | |
420 | if (m_documentTitle != wxT("")) | |
421 | { | |
422 | buf = m_documentTitle; | |
423 | return TRUE; | |
424 | } | |
425 | else if (m_documentFile != wxT("")) | |
426 | { | |
427 | buf = wxFileNameFromPath(m_documentFile); | |
428 | return TRUE; | |
429 | } | |
430 | else | |
431 | { | |
432 | buf = _("unnamed"); | |
433 | return TRUE; | |
434 | } | |
435 | } | |
436 | ||
437 | wxWindow *wxDocument::GetDocumentWindow() const | |
438 | { | |
439 | wxView *view = GetFirstView(); | |
440 | if (view) | |
441 | return view->GetFrame(); | |
442 | else | |
443 | return wxTheApp->GetTopWindow(); | |
444 | } | |
445 | ||
446 | wxCommandProcessor *wxDocument::OnCreateCommandProcessor() | |
447 | { | |
448 | return new wxCommandProcessor; | |
449 | } | |
450 | ||
451 | // TRUE if safe to close | |
452 | bool wxDocument::OnSaveModified() | |
453 | { | |
454 | if (IsModified()) | |
455 | { | |
456 | wxString title; | |
457 | GetPrintableName(title); | |
458 | ||
459 | wxString msgTitle; | |
460 | if (wxTheApp->GetAppName() != wxT("")) | |
461 | msgTitle = wxTheApp->GetAppName(); | |
462 | else | |
463 | msgTitle = wxString(_("Warning")); | |
464 | ||
465 | wxString prompt; | |
466 | prompt.Printf(_("Do you want to save changes to document %s?"), | |
467 | (const wxChar *)title); | |
468 | int res = wxMessageBox(prompt, msgTitle, | |
469 | wxYES_NO|wxCANCEL|wxICON_QUESTION, | |
470 | GetDocumentWindow()); | |
471 | if (res == wxNO) | |
472 | { | |
473 | Modify(FALSE); | |
474 | return TRUE; | |
475 | } | |
476 | else if (res == wxYES) | |
477 | return Save(); | |
478 | else if (res == wxCANCEL) | |
479 | return FALSE; | |
480 | } | |
481 | return TRUE; | |
482 | } | |
483 | ||
484 | bool wxDocument::Draw(wxDC& WXUNUSED(context)) | |
485 | { | |
486 | return TRUE; | |
487 | } | |
488 | ||
489 | bool wxDocument::AddView(wxView *view) | |
490 | { | |
491 | if (!m_documentViews.Member(view)) | |
492 | { | |
493 | m_documentViews.Append(view); | |
494 | OnChangedViewList(); | |
495 | } | |
496 | return TRUE; | |
497 | } | |
498 | ||
499 | bool wxDocument::RemoveView(wxView *view) | |
500 | { | |
501 | (void)m_documentViews.DeleteObject(view); | |
502 | OnChangedViewList(); | |
503 | return TRUE; | |
504 | } | |
505 | ||
506 | bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags) | |
507 | { | |
508 | if (GetDocumentTemplate()->CreateView(this, flags)) | |
509 | return TRUE; | |
510 | else | |
511 | return FALSE; | |
512 | } | |
513 | ||
514 | // Called after a view is added or removed. | |
515 | // The default implementation deletes the document if | |
516 | // there are no more views. | |
517 | void wxDocument::OnChangedViewList() | |
518 | { | |
519 | if (m_documentViews.GetCount() == 0) | |
520 | { | |
521 | if (OnSaveModified()) | |
522 | { | |
523 | delete this; | |
524 | } | |
525 | } | |
526 | } | |
527 | ||
528 | void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint) | |
529 | { | |
530 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); | |
531 | while (node) | |
532 | { | |
533 | wxView *view = (wxView *)node->GetData(); | |
534 | if (view != sender) | |
535 | view->OnUpdate(sender, hint); | |
536 | node = node->GetNext(); | |
537 | } | |
538 | } | |
539 | ||
540 | void wxDocument::NotifyClosing() | |
541 | { | |
542 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); | |
543 | while (node) | |
544 | { | |
545 | wxView *view = (wxView *)node->GetData(); | |
546 | view->OnClosingDocument(); | |
547 | node = node->GetNext(); | |
548 | } | |
549 | } | |
550 | ||
551 | void wxDocument::SetFilename(const wxString& filename, bool notifyViews) | |
552 | { | |
553 | m_documentFile = filename; | |
554 | if ( notifyViews ) | |
555 | { | |
556 | // Notify the views that the filename has changed | |
557 | wxList::compatibility_iterator node = m_documentViews.GetFirst(); | |
558 | while (node) | |
559 | { | |
560 | wxView *view = (wxView *)node->GetData(); | |
561 | view->OnChangeFilename(); | |
562 | node = node->GetNext(); | |
563 | } | |
564 | } | |
565 | } | |
566 | ||
567 | // ---------------------------------------------------------------------------- | |
568 | // Document view | |
569 | // ---------------------------------------------------------------------------- | |
570 | ||
571 | wxView::wxView() | |
572 | { | |
573 | m_viewDocument = (wxDocument*) NULL; | |
574 | ||
575 | m_viewFrame = (wxFrame *) NULL; | |
576 | } | |
577 | ||
578 | wxView::~wxView() | |
579 | { | |
580 | GetDocumentManager()->ActivateView(this, FALSE); | |
581 | m_viewDocument->RemoveView(this); | |
582 | } | |
583 | ||
584 | // Extend event processing to search the document's event table | |
585 | bool wxView::ProcessEvent(wxEvent& event) | |
586 | { | |
587 | if ( !GetDocument() || !GetDocument()->ProcessEvent(event) ) | |
588 | return wxEvtHandler::ProcessEvent(event); | |
589 | ||
590 | return TRUE; | |
591 | } | |
592 | ||
593 | void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView)) | |
594 | { | |
595 | } | |
596 | ||
597 | void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info)) | |
598 | { | |
599 | OnDraw(dc); | |
600 | } | |
601 | ||
602 | void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint)) | |
603 | { | |
604 | } | |
605 | ||
606 | void wxView::OnChangeFilename() | |
607 | { | |
608 | if (GetFrame() && GetDocument()) | |
609 | { | |
610 | wxString title; | |
611 | ||
612 | GetDocument()->GetPrintableName(title); | |
613 | ||
614 | GetFrame()->SetTitle(title); | |
615 | } | |
616 | } | |
617 | ||
618 | void wxView::SetDocument(wxDocument *doc) | |
619 | { | |
620 | m_viewDocument = doc; | |
621 | if (doc) | |
622 | doc->AddView(this); | |
623 | } | |
624 | ||
625 | bool wxView::Close(bool deleteWindow) | |
626 | { | |
627 | if (OnClose(deleteWindow)) | |
628 | return TRUE; | |
629 | else | |
630 | return FALSE; | |
631 | } | |
632 | ||
633 | void wxView::Activate(bool activate) | |
634 | { | |
635 | if (GetDocument() && GetDocumentManager()) | |
636 | { | |
637 | OnActivateView(activate, this, GetDocumentManager()->GetCurrentView()); | |
638 | GetDocumentManager()->ActivateView(this, activate); | |
639 | } | |
640 | } | |
641 | ||
642 | bool wxView::OnClose(bool WXUNUSED(deleteWindow)) | |
643 | { | |
644 | return GetDocument() ? GetDocument()->Close() : TRUE; | |
645 | } | |
646 | ||
647 | #if wxUSE_PRINTING_ARCHITECTURE | |
648 | wxPrintout *wxView::OnCreatePrintout() | |
649 | { | |
650 | return new wxDocPrintout(this); | |
651 | } | |
652 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
653 | ||
654 | // ---------------------------------------------------------------------------- | |
655 | // wxDocTemplate | |
656 | // ---------------------------------------------------------------------------- | |
657 | ||
658 | wxDocTemplate::wxDocTemplate(wxDocManager *manager, | |
659 | const wxString& descr, | |
660 | const wxString& filter, | |
661 | const wxString& dir, | |
662 | const wxString& ext, | |
663 | const wxString& docTypeName, | |
664 | const wxString& viewTypeName, | |
665 | wxClassInfo *docClassInfo, | |
666 | wxClassInfo *viewClassInfo, | |
667 | long flags) | |
668 | { | |
669 | m_documentManager = manager; | |
670 | m_description = descr; | |
671 | m_directory = dir; | |
672 | m_defaultExt = ext; | |
673 | m_fileFilter = filter; | |
674 | m_flags = flags; | |
675 | m_docTypeName = docTypeName; | |
676 | m_viewTypeName = viewTypeName; | |
677 | m_documentManager->AssociateTemplate(this); | |
678 | ||
679 | m_docClassInfo = docClassInfo; | |
680 | m_viewClassInfo = viewClassInfo; | |
681 | } | |
682 | ||
683 | wxDocTemplate::~wxDocTemplate() | |
684 | { | |
685 | m_documentManager->DisassociateTemplate(this); | |
686 | } | |
687 | ||
688 | // Tries to dynamically construct an object of the right class. | |
689 | wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags) | |
690 | { | |
691 | if (!m_docClassInfo) | |
692 | return (wxDocument *) NULL; | |
693 | wxDocument *doc = (wxDocument *)m_docClassInfo->CreateObject(); | |
694 | doc->SetFilename(path); | |
695 | doc->SetDocumentTemplate(this); | |
696 | GetDocumentManager()->AddDocument(doc); | |
697 | doc->SetCommandProcessor(doc->OnCreateCommandProcessor()); | |
698 | ||
699 | if (doc->OnCreate(path, flags)) | |
700 | return doc; | |
701 | else | |
702 | { | |
703 | if (GetDocumentManager()->GetDocuments().Member(doc)) | |
704 | doc->DeleteAllViews(); | |
705 | return (wxDocument *) NULL; | |
706 | } | |
707 | } | |
708 | ||
709 | wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags) | |
710 | { | |
711 | if (!m_viewClassInfo) | |
712 | return (wxView *) NULL; | |
713 | wxView *view = (wxView *)m_viewClassInfo->CreateObject(); | |
714 | view->SetDocument(doc); | |
715 | if (view->OnCreate(doc, flags)) | |
716 | { | |
717 | return view; | |
718 | } | |
719 | else | |
720 | { | |
721 | delete view; | |
722 | return (wxView *) NULL; | |
723 | } | |
724 | } | |
725 | ||
726 | // The default (very primitive) format detection: check is the extension is | |
727 | // that of the template | |
728 | bool wxDocTemplate::FileMatchesTemplate(const wxString& path) | |
729 | { | |
730 | return GetDefaultExtension().IsSameAs(FindExtension(path)); | |
731 | } | |
732 | ||
733 | // ---------------------------------------------------------------------------- | |
734 | // wxDocManager | |
735 | // ---------------------------------------------------------------------------- | |
736 | ||
737 | BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler) | |
738 | EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen) | |
739 | EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose) | |
740 | EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll) | |
741 | EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert) | |
742 | EVT_MENU(wxID_NEW, wxDocManager::OnFileNew) | |
743 | EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave) | |
744 | EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs) | |
745 | EVT_MENU(wxID_UNDO, wxDocManager::OnUndo) | |
746 | EVT_MENU(wxID_REDO, wxDocManager::OnRedo) | |
747 | ||
748 | EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen) | |
749 | EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateFileClose) | |
750 | EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateFileClose) | |
751 | EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateFileRevert) | |
752 | EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew) | |
753 | EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave) | |
754 | EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateFileSaveAs) | |
755 | EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo) | |
756 | EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo) | |
757 | ||
758 | #if wxUSE_PRINTING_ARCHITECTURE | |
759 | EVT_MENU(wxID_PRINT, wxDocManager::OnPrint) | |
760 | EVT_MENU(wxID_PRINT_SETUP, wxDocManager::OnPrintSetup) | |
761 | EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview) | |
762 | ||
763 | EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdatePrint) | |
764 | EVT_UPDATE_UI(wxID_PRINT_SETUP, wxDocManager::OnUpdatePrintSetup) | |
765 | EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdatePreview) | |
766 | #endif | |
767 | END_EVENT_TABLE() | |
768 | ||
769 | wxDocManager* wxDocManager::sm_docManager = (wxDocManager*) NULL; | |
770 | ||
771 | wxDocManager::wxDocManager(long flags, bool initialize) | |
772 | { | |
773 | m_defaultDocumentNameCounter = 1; | |
774 | m_flags = flags; | |
775 | m_currentView = (wxView *) NULL; | |
776 | m_maxDocsOpen = 10000; | |
777 | m_fileHistory = (wxFileHistory *) NULL; | |
778 | if (initialize) | |
779 | Initialize(); | |
780 | sm_docManager = this; | |
781 | } | |
782 | ||
783 | wxDocManager::~wxDocManager() | |
784 | { | |
785 | Clear(); | |
786 | if (m_fileHistory) | |
787 | delete m_fileHistory; | |
788 | sm_docManager = (wxDocManager*) NULL; | |
789 | } | |
790 | ||
791 | // closes the specified document | |
792 | bool wxDocManager::CloseDocument(wxDocument* doc, bool force) | |
793 | { | |
794 | if (doc->Close() || force) | |
795 | { | |
796 | // Implicitly deletes the document when | |
797 | // the last view is deleted | |
798 | doc->DeleteAllViews(); | |
799 | ||
800 | // Check we're really deleted | |
801 | if (m_docs.Member(doc)) | |
802 | delete doc; | |
803 | ||
804 | return TRUE; | |
805 | } | |
806 | return FALSE; | |
807 | } | |
808 | ||
809 | bool wxDocManager::CloseDocuments(bool force) | |
810 | { | |
811 | wxList::compatibility_iterator node = m_docs.GetFirst(); | |
812 | while (node) | |
813 | { | |
814 | wxDocument *doc = (wxDocument *)node->GetData(); | |
815 | wxList::compatibility_iterator next = node->GetNext(); | |
816 | ||
817 | if (!CloseDocument(doc, force)) | |
818 | return FALSE; | |
819 | ||
820 | // This assumes that documents are not connected in | |
821 | // any way, i.e. deleting one document does NOT | |
822 | // delete another. | |
823 | node = next; | |
824 | } | |
825 | return TRUE; | |
826 | } | |
827 | ||
828 | bool wxDocManager::Clear(bool force) | |
829 | { | |
830 | if (!CloseDocuments(force)) | |
831 | return FALSE; | |
832 | ||
833 | wxList::compatibility_iterator node = m_templates.GetFirst(); | |
834 | while (node) | |
835 | { | |
836 | wxDocTemplate *templ = (wxDocTemplate*) node->GetData(); | |
837 | wxList::compatibility_iterator next = node->GetNext(); | |
838 | delete templ; | |
839 | node = next; | |
840 | } | |
841 | return TRUE; | |
842 | } | |
843 | ||
844 | bool wxDocManager::Initialize() | |
845 | { | |
846 | m_fileHistory = OnCreateFileHistory(); | |
847 | return TRUE; | |
848 | } | |
849 | ||
850 | wxFileHistory *wxDocManager::OnCreateFileHistory() | |
851 | { | |
852 | return new wxFileHistory; | |
853 | } | |
854 | ||
855 | void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event)) | |
856 | { | |
857 | wxDocument *doc = GetCurrentDocument(); | |
858 | if (!doc) | |
859 | return; | |
860 | if (doc->Close()) | |
861 | { | |
862 | doc->DeleteAllViews(); | |
863 | if (m_docs.Member(doc)) | |
864 | delete doc; | |
865 | } | |
866 | } | |
867 | ||
868 | void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event)) | |
869 | { | |
870 | CloseDocuments(FALSE); | |
871 | } | |
872 | ||
873 | void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event)) | |
874 | { | |
875 | CreateDocument( wxT(""), wxDOC_NEW ); | |
876 | } | |
877 | ||
878 | void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event)) | |
879 | { | |
880 | if ( !CreateDocument( wxT(""), 0) ) | |
881 | { | |
882 | OnOpenFileFailure(); | |
883 | } | |
884 | } | |
885 | ||
886 | void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event)) | |
887 | { | |
888 | wxDocument *doc = GetCurrentDocument(); | |
889 | if (!doc) | |
890 | return; | |
891 | doc->Revert(); | |
892 | } | |
893 | ||
894 | void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event)) | |
895 | { | |
896 | wxDocument *doc = GetCurrentDocument(); | |
897 | if (!doc) | |
898 | return; | |
899 | doc->Save(); | |
900 | } | |
901 | ||
902 | void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event)) | |
903 | { | |
904 | wxDocument *doc = GetCurrentDocument(); | |
905 | if (!doc) | |
906 | return; | |
907 | doc->SaveAs(); | |
908 | } | |
909 | ||
910 | void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event)) | |
911 | { | |
912 | #if wxUSE_PRINTING_ARCHITECTURE | |
913 | wxView *view = GetCurrentView(); | |
914 | if (!view) | |
915 | return; | |
916 | ||
917 | wxPrintout *printout = view->OnCreatePrintout(); | |
918 | if (printout) | |
919 | { | |
920 | wxPrinter printer; | |
921 | printer.Print(view->GetFrame(), printout, TRUE); | |
922 | ||
923 | delete printout; | |
924 | } | |
925 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
926 | } | |
927 | ||
928 | void wxDocManager::OnPrintSetup(wxCommandEvent& WXUNUSED(event)) | |
929 | { | |
930 | #if wxUSE_PRINTING_ARCHITECTURE | |
931 | wxWindow *parentWin = wxTheApp->GetTopWindow(); | |
932 | wxView *view = GetCurrentView(); | |
933 | if (view) | |
934 | parentWin = view->GetFrame(); | |
935 | ||
936 | wxPrintDialogData data; | |
937 | ||
938 | wxPrintDialog printerDialog(parentWin, &data); | |
939 | printerDialog.GetPrintDialogData().SetSetupDialog(TRUE); | |
940 | printerDialog.ShowModal(); | |
941 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
942 | } | |
943 | ||
944 | void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event)) | |
945 | { | |
946 | #if wxUSE_PRINTING_ARCHITECTURE | |
947 | wxView *view = GetCurrentView(); | |
948 | if (!view) | |
949 | return; | |
950 | ||
951 | wxPrintout *printout = view->OnCreatePrintout(); | |
952 | if (printout) | |
953 | { | |
954 | // Pass two printout objects: for preview, and possible printing. | |
955 | wxPrintPreviewBase *preview = new wxPrintPreview(printout, view->OnCreatePrintout()); | |
956 | if ( !preview->Ok() ) | |
957 | { | |
958 | delete preview; | |
959 | wxMessageBox( _("Sorry, print preview needs a printer to be installed.") ); | |
960 | return; | |
961 | } | |
962 | ||
963 | wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"), | |
964 | wxPoint(100, 100), wxSize(600, 650)); | |
965 | frame->Centre(wxBOTH); | |
966 | frame->Initialize(); | |
967 | frame->Show(TRUE); | |
968 | } | |
969 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
970 | } | |
971 | ||
972 | void wxDocManager::OnUndo(wxCommandEvent& event) | |
973 | { | |
974 | wxDocument *doc = GetCurrentDocument(); | |
975 | if (!doc) | |
976 | return; | |
977 | if (doc->GetCommandProcessor()) | |
978 | doc->GetCommandProcessor()->Undo(); | |
979 | else | |
980 | event.Skip(); | |
981 | } | |
982 | ||
983 | void wxDocManager::OnRedo(wxCommandEvent& event) | |
984 | { | |
985 | wxDocument *doc = GetCurrentDocument(); | |
986 | if (!doc) | |
987 | return; | |
988 | if (doc->GetCommandProcessor()) | |
989 | doc->GetCommandProcessor()->Redo(); | |
990 | else | |
991 | event.Skip(); | |
992 | } | |
993 | ||
994 | // Handlers for UI update commands | |
995 | ||
996 | void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event) | |
997 | { | |
998 | event.Enable( TRUE ); | |
999 | } | |
1000 | ||
1001 | void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent& event) | |
1002 | { | |
1003 | wxDocument *doc = GetCurrentDocument(); | |
1004 | event.Enable( (doc != (wxDocument*) NULL) ); | |
1005 | } | |
1006 | ||
1007 | void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event) | |
1008 | { | |
1009 | wxDocument *doc = GetCurrentDocument(); | |
1010 | event.Enable( (doc != (wxDocument*) NULL) ); | |
1011 | } | |
1012 | ||
1013 | void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event) | |
1014 | { | |
1015 | event.Enable( TRUE ); | |
1016 | } | |
1017 | ||
1018 | void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event) | |
1019 | { | |
1020 | wxDocument *doc = GetCurrentDocument(); | |
1021 | event.Enable( doc && doc->IsModified() ); | |
1022 | } | |
1023 | ||
1024 | void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event) | |
1025 | { | |
1026 | wxDocument *doc = GetCurrentDocument(); | |
1027 | event.Enable( (doc != (wxDocument*) NULL) ); | |
1028 | } | |
1029 | ||
1030 | void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event) | |
1031 | { | |
1032 | wxDocument *doc = GetCurrentDocument(); | |
1033 | if (!doc) | |
1034 | event.Enable(FALSE); | |
1035 | else if (!doc->GetCommandProcessor()) | |
1036 | event.Skip(); | |
1037 | else | |
1038 | { | |
1039 | event.Enable( doc->GetCommandProcessor()->CanUndo() ); | |
1040 | doc->GetCommandProcessor()->SetMenuStrings(); | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event) | |
1045 | { | |
1046 | wxDocument *doc = GetCurrentDocument(); | |
1047 | if (!doc) | |
1048 | event.Enable(FALSE); | |
1049 | else if (!doc->GetCommandProcessor()) | |
1050 | event.Skip(); | |
1051 | else | |
1052 | { | |
1053 | event.Enable( doc->GetCommandProcessor()->CanRedo() ); | |
1054 | doc->GetCommandProcessor()->SetMenuStrings(); | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event) | |
1059 | { | |
1060 | wxDocument *doc = GetCurrentDocument(); | |
1061 | event.Enable( (doc != (wxDocument*) NULL) ); | |
1062 | } | |
1063 | ||
1064 | void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent& event) | |
1065 | { | |
1066 | event.Enable( TRUE ); | |
1067 | } | |
1068 | ||
1069 | void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event) | |
1070 | { | |
1071 | wxDocument *doc = GetCurrentDocument(); | |
1072 | event.Enable( (doc != (wxDocument*) NULL) ); | |
1073 | } | |
1074 | ||
1075 | wxView *wxDocManager::GetCurrentView() const | |
1076 | { | |
1077 | if (m_currentView) | |
1078 | return m_currentView; | |
1079 | if (m_docs.GetCount() == 1) | |
1080 | { | |
1081 | wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData(); | |
1082 | return doc->GetFirstView(); | |
1083 | } | |
1084 | return (wxView *) NULL; | |
1085 | } | |
1086 | ||
1087 | // Extend event processing to search the view's event table | |
1088 | bool wxDocManager::ProcessEvent(wxEvent& event) | |
1089 | { | |
1090 | wxView* view = GetCurrentView(); | |
1091 | if (view) | |
1092 | { | |
1093 | if (view->ProcessEvent(event)) | |
1094 | return TRUE; | |
1095 | } | |
1096 | return wxEvtHandler::ProcessEvent(event); | |
1097 | } | |
1098 | ||
1099 | wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags) | |
1100 | { | |
1101 | wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()]; | |
1102 | int n = 0; | |
1103 | ||
1104 | for (size_t i = 0; i < m_templates.GetCount(); i++) | |
1105 | { | |
1106 | wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData()); | |
1107 | if (temp->IsVisible()) | |
1108 | { | |
1109 | templates[n] = temp; | |
1110 | n ++; | |
1111 | } | |
1112 | } | |
1113 | if (n == 0) | |
1114 | { | |
1115 | delete[] templates; | |
1116 | return (wxDocument *) NULL; | |
1117 | } | |
1118 | ||
1119 | wxDocument* docToClose = NULL; | |
1120 | ||
1121 | // If we've reached the max number of docs, close the | |
1122 | // first one. | |
1123 | if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen ) | |
1124 | { | |
1125 | wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData(); | |
1126 | docToClose = doc; | |
1127 | } | |
1128 | ||
1129 | // New document: user chooses a template, unless there's only one. | |
1130 | if (flags & wxDOC_NEW) | |
1131 | { | |
1132 | if (n == 1) | |
1133 | { | |
1134 | if (docToClose) | |
1135 | { | |
1136 | if (!CloseDocument(docToClose, FALSE)) | |
1137 | { | |
1138 | delete[] templates; | |
1139 | return NULL; | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | wxDocTemplate *temp = templates[0]; | |
1144 | delete[] templates; | |
1145 | wxDocument *newDoc = temp->CreateDocument(path, flags); | |
1146 | ||
1147 | if (newDoc) | |
1148 | { | |
1149 | newDoc->SetDocumentName(temp->GetDocumentName()); | |
1150 | newDoc->SetDocumentTemplate(temp); | |
1151 | newDoc->OnNewDocument(); | |
1152 | } | |
1153 | return newDoc; | |
1154 | } | |
1155 | ||
1156 | wxDocTemplate *temp = SelectDocumentType(templates, n); | |
1157 | delete[] templates; | |
1158 | if (temp) | |
1159 | { | |
1160 | if (docToClose) | |
1161 | { | |
1162 | if (!CloseDocument(docToClose, FALSE)) | |
1163 | { | |
1164 | return NULL; | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | wxDocument *newDoc = temp->CreateDocument(path, flags); | |
1169 | ||
1170 | if (newDoc) | |
1171 | { | |
1172 | newDoc->SetDocumentName(temp->GetDocumentName()); | |
1173 | newDoc->SetDocumentTemplate(temp); | |
1174 | newDoc->OnNewDocument(); | |
1175 | } | |
1176 | return newDoc; | |
1177 | } | |
1178 | else | |
1179 | return (wxDocument *) NULL; | |
1180 | } | |
1181 | ||
1182 | // Existing document | |
1183 | wxDocTemplate *temp; | |
1184 | ||
1185 | wxString path2(wxT("")); | |
1186 | if (path != wxT("")) | |
1187 | path2 = path; | |
1188 | ||
1189 | if (flags & wxDOC_SILENT) | |
1190 | { | |
1191 | temp = FindTemplateForPath(path2); | |
1192 | if (!temp) | |
1193 | { | |
1194 | // Since we do not add files with non-default extensions to the FileHistory this | |
1195 | // can only happen if the application changes the allowed templates in runtime. | |
1196 | (void)wxMessageBox(_("Sorry, the format for this file is unknown."), | |
1197 | _("Open File"), | |
1198 | wxOK | wxICON_EXCLAMATION, wxFindSuitableParent()); | |
1199 | } | |
1200 | } | |
1201 | else | |
1202 | temp = SelectDocumentPath(templates, n, path2, flags); | |
1203 | ||
1204 | delete[] templates; | |
1205 | ||
1206 | if (temp) | |
1207 | { | |
1208 | if (docToClose) | |
1209 | { | |
1210 | if (!CloseDocument(docToClose, FALSE)) | |
1211 | { | |
1212 | return NULL; | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | wxDocument *newDoc = temp->CreateDocument(path2, flags); | |
1217 | if (newDoc) | |
1218 | { | |
1219 | newDoc->SetDocumentName(temp->GetDocumentName()); | |
1220 | newDoc->SetDocumentTemplate(temp); | |
1221 | if (!newDoc->OnOpenDocument(path2)) | |
1222 | { | |
1223 | newDoc->DeleteAllViews(); | |
1224 | // delete newDoc; // Implicitly deleted by DeleteAllViews | |
1225 | return (wxDocument *) NULL; | |
1226 | } | |
1227 | // A file that doesn't use the default extension of its document | |
1228 | // template cannot be opened via the FileHistory, so we do not | |
1229 | // add it. | |
1230 | if (temp->FileMatchesTemplate(path2)) | |
1231 | AddFileToHistory(path2); | |
1232 | } | |
1233 | return newDoc; | |
1234 | } | |
1235 | ||
1236 | return (wxDocument *) NULL; | |
1237 | } | |
1238 | ||
1239 | wxView *wxDocManager::CreateView(wxDocument *doc, long flags) | |
1240 | { | |
1241 | wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()]; | |
1242 | int n =0; | |
1243 | ||
1244 | for (size_t i = 0; i < m_templates.GetCount(); i++) | |
1245 | { | |
1246 | wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData()); | |
1247 | if (temp->IsVisible()) | |
1248 | { | |
1249 | if (temp->GetDocumentName() == doc->GetDocumentName()) | |
1250 | { | |
1251 | templates[n] = temp; | |
1252 | n ++; | |
1253 | } | |
1254 | } | |
1255 | } | |
1256 | if (n == 0) | |
1257 | { | |
1258 | delete[] templates; | |
1259 | return (wxView *) NULL; | |
1260 | } | |
1261 | if (n == 1) | |
1262 | { | |
1263 | wxDocTemplate *temp = templates[0]; | |
1264 | delete[] templates; | |
1265 | wxView *view = temp->CreateView(doc, flags); | |
1266 | if (view) | |
1267 | view->SetViewName(temp->GetViewName()); | |
1268 | return view; | |
1269 | } | |
1270 | ||
1271 | wxDocTemplate *temp = SelectViewType(templates, n); | |
1272 | delete[] templates; | |
1273 | if (temp) | |
1274 | { | |
1275 | wxView *view = temp->CreateView(doc, flags); | |
1276 | if (view) | |
1277 | view->SetViewName(temp->GetViewName()); | |
1278 | return view; | |
1279 | } | |
1280 | else | |
1281 | return (wxView *) NULL; | |
1282 | } | |
1283 | ||
1284 | // Not yet implemented | |
1285 | void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags)) | |
1286 | { | |
1287 | } | |
1288 | ||
1289 | // Not yet implemented | |
1290 | bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc)) | |
1291 | { | |
1292 | return FALSE; | |
1293 | } | |
1294 | ||
1295 | wxDocument *wxDocManager::GetCurrentDocument() const | |
1296 | { | |
1297 | wxView *view = GetCurrentView(); | |
1298 | if (view) | |
1299 | return view->GetDocument(); | |
1300 | else | |
1301 | return (wxDocument *) NULL; | |
1302 | } | |
1303 | ||
1304 | // Make a default document name | |
1305 | bool wxDocManager::MakeDefaultName(wxString& name) | |
1306 | { | |
1307 | name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter); | |
1308 | m_defaultDocumentNameCounter++; | |
1309 | ||
1310 | return TRUE; | |
1311 | } | |
1312 | ||
1313 | // Make a frame title (override this to do something different) | |
1314 | // If docName is empty, a document is not currently active. | |
1315 | wxString wxDocManager::MakeFrameTitle(wxDocument* doc) | |
1316 | { | |
1317 | wxString appName = wxTheApp->GetAppName(); | |
1318 | wxString title; | |
1319 | if (!doc) | |
1320 | title = appName; | |
1321 | else | |
1322 | { | |
1323 | wxString docName; | |
1324 | doc->GetPrintableName(docName); | |
1325 | title = docName + wxString(_(" - ")) + appName; | |
1326 | } | |
1327 | return title; | |
1328 | } | |
1329 | ||
1330 | ||
1331 | // Not yet implemented | |
1332 | wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path)) | |
1333 | { | |
1334 | return (wxDocTemplate *) NULL; | |
1335 | } | |
1336 | ||
1337 | // File history management | |
1338 | void wxDocManager::AddFileToHistory(const wxString& file) | |
1339 | { | |
1340 | if (m_fileHistory) | |
1341 | m_fileHistory->AddFileToHistory(file); | |
1342 | } | |
1343 | ||
1344 | void wxDocManager::RemoveFileFromHistory(size_t i) | |
1345 | { | |
1346 | if (m_fileHistory) | |
1347 | m_fileHistory->RemoveFileFromHistory(i); | |
1348 | } | |
1349 | ||
1350 | wxString wxDocManager::GetHistoryFile(size_t i) const | |
1351 | { | |
1352 | wxString histFile; | |
1353 | ||
1354 | if (m_fileHistory) | |
1355 | histFile = m_fileHistory->GetHistoryFile(i); | |
1356 | ||
1357 | return histFile; | |
1358 | } | |
1359 | ||
1360 | void wxDocManager::FileHistoryUseMenu(wxMenu *menu) | |
1361 | { | |
1362 | if (m_fileHistory) | |
1363 | m_fileHistory->UseMenu(menu); | |
1364 | } | |
1365 | ||
1366 | void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu) | |
1367 | { | |
1368 | if (m_fileHistory) | |
1369 | m_fileHistory->RemoveMenu(menu); | |
1370 | } | |
1371 | ||
1372 | #if wxUSE_CONFIG | |
1373 | void wxDocManager::FileHistoryLoad(wxConfigBase& config) | |
1374 | { | |
1375 | if (m_fileHistory) | |
1376 | m_fileHistory->Load(config); | |
1377 | } | |
1378 | ||
1379 | void wxDocManager::FileHistorySave(wxConfigBase& config) | |
1380 | { | |
1381 | if (m_fileHistory) | |
1382 | m_fileHistory->Save(config); | |
1383 | } | |
1384 | #endif | |
1385 | ||
1386 | void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu) | |
1387 | { | |
1388 | if (m_fileHistory) | |
1389 | m_fileHistory->AddFilesToMenu(menu); | |
1390 | } | |
1391 | ||
1392 | void wxDocManager::FileHistoryAddFilesToMenu() | |
1393 | { | |
1394 | if (m_fileHistory) | |
1395 | m_fileHistory->AddFilesToMenu(); | |
1396 | } | |
1397 | ||
1398 | size_t wxDocManager::GetHistoryFilesCount() const | |
1399 | { | |
1400 | return m_fileHistory ? m_fileHistory->GetCount() : 0; | |
1401 | } | |
1402 | ||
1403 | ||
1404 | // Find out the document template via matching in the document file format | |
1405 | // against that of the template | |
1406 | wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path) | |
1407 | { | |
1408 | wxDocTemplate *theTemplate = (wxDocTemplate *) NULL; | |
1409 | ||
1410 | // Find the template which this extension corresponds to | |
1411 | for (size_t i = 0; i < m_templates.GetCount(); i++) | |
1412 | { | |
1413 | wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData(); | |
1414 | if ( temp->FileMatchesTemplate(path) ) | |
1415 | { | |
1416 | theTemplate = temp; | |
1417 | break; | |
1418 | } | |
1419 | } | |
1420 | return theTemplate; | |
1421 | } | |
1422 | ||
1423 | // Try to get a more suitable parent frame than the top window, | |
1424 | // for selection dialogs. Otherwise you may get an unexpected | |
1425 | // window being activated when a dialog is shown. | |
1426 | static wxWindow* wxFindSuitableParent() | |
1427 | { | |
1428 | wxWindow* parent = wxTheApp->GetTopWindow(); | |
1429 | ||
1430 | wxWindow* focusWindow = wxWindow::FindFocus(); | |
1431 | if (focusWindow) | |
1432 | { | |
1433 | while (focusWindow && | |
1434 | !focusWindow->IsKindOf(CLASSINFO(wxDialog)) && | |
1435 | !focusWindow->IsKindOf(CLASSINFO(wxFrame))) | |
1436 | ||
1437 | focusWindow = focusWindow->GetParent(); | |
1438 | ||
1439 | if (focusWindow) | |
1440 | parent = focusWindow; | |
1441 | } | |
1442 | return parent; | |
1443 | } | |
1444 | ||
1445 | // Prompts user to open a file, using file specs in templates. | |
1446 | // Must extend the file selector dialog or implement own; OR | |
1447 | // match the extension to the template extension. | |
1448 | ||
1449 | wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates, | |
1450 | #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__) | |
1451 | int noTemplates, | |
1452 | #else | |
1453 | int WXUNUSED(noTemplates), | |
1454 | #endif | |
1455 | wxString& path, | |
1456 | long WXUNUSED(flags), | |
1457 | bool WXUNUSED(save)) | |
1458 | { | |
1459 | // We can only have multiple filters in Windows and GTK | |
1460 | #if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__) | |
1461 | wxString descrBuf; | |
1462 | ||
1463 | int i; | |
1464 | for (i = 0; i < noTemplates; i++) | |
1465 | { | |
1466 | if (templates[i]->IsVisible()) | |
1467 | { | |
1468 | // add a '|' to separate this filter from the previous one | |
1469 | if ( !descrBuf.IsEmpty() ) | |
1470 | descrBuf << wxT('|'); | |
1471 | ||
1472 | descrBuf << templates[i]->GetDescription() | |
1473 | << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |") | |
1474 | << templates[i]->GetFileFilter(); | |
1475 | } | |
1476 | } | |
1477 | #else | |
1478 | wxString descrBuf = wxT("*.*"); | |
1479 | #endif | |
1480 | ||
1481 | int FilterIndex = -1; | |
1482 | ||
1483 | wxWindow* parent = wxFindSuitableParent(); | |
1484 | ||
1485 | wxString pathTmp = wxFileSelectorEx(_("Select a file"), | |
1486 | m_lastDirectory, | |
1487 | wxT(""), | |
1488 | &FilterIndex, | |
1489 | descrBuf, | |
1490 | 0, | |
1491 | parent); | |
1492 | ||
1493 | wxDocTemplate *theTemplate = (wxDocTemplate *)NULL; | |
1494 | if (!pathTmp.IsEmpty()) | |
1495 | { | |
1496 | if (!wxFileExists(pathTmp)) | |
1497 | { | |
1498 | wxString msgTitle; | |
1499 | if (!wxTheApp->GetAppName().IsEmpty()) | |
1500 | msgTitle = wxTheApp->GetAppName(); | |
1501 | else | |
1502 | msgTitle = wxString(_("File error")); | |
1503 | ||
1504 | (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION, | |
1505 | parent); | |
1506 | ||
1507 | path = wxT(""); | |
1508 | return (wxDocTemplate *) NULL; | |
1509 | } | |
1510 | m_lastDirectory = wxPathOnly(pathTmp); | |
1511 | ||
1512 | path = pathTmp; | |
1513 | ||
1514 | // first choose the template using the extension, if this fails (i.e. | |
1515 | // wxFileSelectorEx() didn't fill it), then use the path | |
1516 | if ( FilterIndex != -1 ) | |
1517 | theTemplate = templates[FilterIndex]; | |
1518 | if ( !theTemplate ) | |
1519 | theTemplate = FindTemplateForPath(path); | |
1520 | if ( !theTemplate ) | |
1521 | { | |
1522 | // Since we do not add files with non-default extensions to the FileHistory this | |
1523 | // can only happen if the application changes the allowed templates in runtime. | |
1524 | (void)wxMessageBox(_("Sorry, the format for this file is unknown."), | |
1525 | _("Open File"), | |
1526 | wxOK | wxICON_EXCLAMATION, wxFindSuitableParent()); | |
1527 | } | |
1528 | } | |
1529 | else | |
1530 | { | |
1531 | path = wxT(""); | |
1532 | } | |
1533 | ||
1534 | return theTemplate; | |
1535 | } | |
1536 | ||
1537 | wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates, | |
1538 | int noTemplates, bool sort) | |
1539 | { | |
1540 | wxArrayString strings; | |
1541 | wxDocTemplate **data = new wxDocTemplate *[noTemplates]; | |
1542 | int i; | |
1543 | int n = 0; | |
1544 | ||
1545 | for (i = 0; i < noTemplates; i++) | |
1546 | { | |
1547 | if (templates[i]->IsVisible()) | |
1548 | { | |
1549 | int j; | |
1550 | bool want = TRUE; | |
1551 | for (j = 0; j < n; j++) | |
1552 | { | |
1553 | //filter out NOT unique documents + view combinations | |
1554 | if ( templates[i]->m_docTypeName == data[j]->m_docTypeName && | |
1555 | templates[i]->m_viewTypeName == data[j]->m_viewTypeName | |
1556 | ) | |
1557 | want = FALSE; | |
1558 | } | |
1559 | ||
1560 | if ( want ) | |
1561 | { | |
1562 | strings.Add(templates[i]->m_description); | |
1563 | ||
1564 | data[n] = templates[i]; | |
1565 | n ++; | |
1566 | } | |
1567 | } | |
1568 | } // for | |
1569 | ||
1570 | if (sort) | |
1571 | { | |
1572 | strings.Sort(wxStringSortAscending); | |
1573 | // Yes, this will be slow, but template lists | |
1574 | // are typically short. | |
1575 | int j; | |
1576 | n = strings.Count(); | |
1577 | for (i = 0; i < n; i++) | |
1578 | { | |
1579 | for (j = 0; j < noTemplates; j++) | |
1580 | { | |
1581 | if (strings[i] == templates[j]->m_description) | |
1582 | data[i] = templates[j]; | |
1583 | } | |
1584 | } | |
1585 | } | |
1586 | ||
1587 | wxDocTemplate *theTemplate; | |
1588 | ||
1589 | switch ( n ) | |
1590 | { | |
1591 | case 0: | |
1592 | // no visible templates, hence nothing to choose from | |
1593 | theTemplate = NULL; | |
1594 | break; | |
1595 | ||
1596 | case 1: | |
1597 | // don't propose the user to choose if he heas no choice | |
1598 | theTemplate = data[0]; | |
1599 | break; | |
1600 | ||
1601 | default: | |
1602 | // propose the user to choose one of several | |
1603 | theTemplate = (wxDocTemplate *)wxGetSingleChoiceData | |
1604 | ( | |
1605 | _("Select a document template"), | |
1606 | _("Templates"), | |
1607 | strings, | |
1608 | (void **)data, | |
1609 | wxFindSuitableParent() | |
1610 | ); | |
1611 | } | |
1612 | ||
1613 | delete[] data; | |
1614 | ||
1615 | return theTemplate; | |
1616 | } | |
1617 | ||
1618 | wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates, | |
1619 | int noTemplates, bool sort) | |
1620 | { | |
1621 | wxArrayString strings; | |
1622 | wxDocTemplate **data = new wxDocTemplate *[noTemplates]; | |
1623 | int i; | |
1624 | int n = 0; | |
1625 | ||
1626 | for (i = 0; i < noTemplates; i++) | |
1627 | { | |
1628 | wxDocTemplate *templ = templates[i]; | |
1629 | if ( templ->IsVisible() && !templ->GetViewName().empty() ) | |
1630 | { | |
1631 | int j; | |
1632 | bool want = TRUE; | |
1633 | for (j = 0; j < n; j++) | |
1634 | { | |
1635 | //filter out NOT unique views | |
1636 | if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName ) | |
1637 | want = FALSE; | |
1638 | } | |
1639 | ||
1640 | if ( want ) | |
1641 | { | |
1642 | strings.Add(templ->m_viewTypeName); | |
1643 | data[n] = templ; | |
1644 | n ++; | |
1645 | } | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | if (sort) | |
1650 | { | |
1651 | strings.Sort(wxStringSortAscending); | |
1652 | // Yes, this will be slow, but template lists | |
1653 | // are typically short. | |
1654 | int j; | |
1655 | n = strings.Count(); | |
1656 | for (i = 0; i < n; i++) | |
1657 | { | |
1658 | for (j = 0; j < noTemplates; j++) | |
1659 | { | |
1660 | if (strings[i] == templates[j]->m_viewTypeName) | |
1661 | data[i] = templates[j]; | |
1662 | } | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | wxDocTemplate *theTemplate; | |
1667 | ||
1668 | // the same logic as above | |
1669 | switch ( n ) | |
1670 | { | |
1671 | case 0: | |
1672 | theTemplate = (wxDocTemplate *)NULL; | |
1673 | break; | |
1674 | ||
1675 | case 1: | |
1676 | theTemplate = data[0]; | |
1677 | break; | |
1678 | ||
1679 | default: | |
1680 | theTemplate = (wxDocTemplate *)wxGetSingleChoiceData | |
1681 | ( | |
1682 | _("Select a document view"), | |
1683 | _("Views"), | |
1684 | strings, | |
1685 | (void **)data, | |
1686 | wxFindSuitableParent() | |
1687 | ); | |
1688 | ||
1689 | } | |
1690 | ||
1691 | delete[] data; | |
1692 | return theTemplate; | |
1693 | } | |
1694 | ||
1695 | void wxDocManager::AssociateTemplate(wxDocTemplate *temp) | |
1696 | { | |
1697 | if (!m_templates.Member(temp)) | |
1698 | m_templates.Append(temp); | |
1699 | } | |
1700 | ||
1701 | void wxDocManager::DisassociateTemplate(wxDocTemplate *temp) | |
1702 | { | |
1703 | m_templates.DeleteObject(temp); | |
1704 | } | |
1705 | ||
1706 | // Add and remove a document from the manager's list | |
1707 | void wxDocManager::AddDocument(wxDocument *doc) | |
1708 | { | |
1709 | if (!m_docs.Member(doc)) | |
1710 | m_docs.Append(doc); | |
1711 | } | |
1712 | ||
1713 | void wxDocManager::RemoveDocument(wxDocument *doc) | |
1714 | { | |
1715 | m_docs.DeleteObject(doc); | |
1716 | } | |
1717 | ||
1718 | // Views or windows should inform the document manager | |
1719 | // when a view is going in or out of focus | |
1720 | void wxDocManager::ActivateView(wxView *view, bool activate) | |
1721 | { | |
1722 | if ( activate ) | |
1723 | { | |
1724 | m_currentView = view; | |
1725 | } | |
1726 | else // deactivate | |
1727 | { | |
1728 | if ( m_currentView == view ) | |
1729 | { | |
1730 | // don't keep stale pointer | |
1731 | m_currentView = (wxView *) NULL; | |
1732 | } | |
1733 | } | |
1734 | } | |
1735 | ||
1736 | // ---------------------------------------------------------------------------- | |
1737 | // Default document child frame | |
1738 | // ---------------------------------------------------------------------------- | |
1739 | ||
1740 | BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame) | |
1741 | EVT_ACTIVATE(wxDocChildFrame::OnActivate) | |
1742 | EVT_CLOSE(wxDocChildFrame::OnCloseWindow) | |
1743 | END_EVENT_TABLE() | |
1744 | ||
1745 | wxDocChildFrame::wxDocChildFrame(wxDocument *doc, | |
1746 | wxView *view, | |
1747 | wxFrame *frame, | |
1748 | wxWindowID id, | |
1749 | const wxString& title, | |
1750 | const wxPoint& pos, | |
1751 | const wxSize& size, | |
1752 | long style, | |
1753 | const wxString& name) | |
1754 | : wxFrame(frame, id, title, pos, size, style, name) | |
1755 | { | |
1756 | m_childDocument = doc; | |
1757 | m_childView = view; | |
1758 | if (view) | |
1759 | view->SetFrame(this); | |
1760 | } | |
1761 | ||
1762 | wxDocChildFrame::~wxDocChildFrame() | |
1763 | { | |
1764 | } | |
1765 | ||
1766 | // Extend event processing to search the view's event table | |
1767 | bool wxDocChildFrame::ProcessEvent(wxEvent& event) | |
1768 | { | |
1769 | if (m_childView) | |
1770 | m_childView->Activate(TRUE); | |
1771 | ||
1772 | if ( !m_childView || ! m_childView->ProcessEvent(event) ) | |
1773 | { | |
1774 | // Only hand up to the parent if it's a menu command | |
1775 | if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event)) | |
1776 | return wxEvtHandler::ProcessEvent(event); | |
1777 | else | |
1778 | return TRUE; | |
1779 | } | |
1780 | else | |
1781 | return TRUE; | |
1782 | } | |
1783 | ||
1784 | void wxDocChildFrame::OnActivate(wxActivateEvent& event) | |
1785 | { | |
1786 | wxFrame::OnActivate(event); | |
1787 | ||
1788 | if (m_childView) | |
1789 | m_childView->Activate(event.GetActive()); | |
1790 | } | |
1791 | ||
1792 | void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event) | |
1793 | { | |
1794 | if (m_childView) | |
1795 | { | |
1796 | bool ans = event.CanVeto() | |
1797 | ? m_childView->Close(FALSE) // FALSE means don't delete associated window | |
1798 | : TRUE; // Must delete. | |
1799 | ||
1800 | if (ans) | |
1801 | { | |
1802 | m_childView->Activate(FALSE); | |
1803 | delete m_childView; | |
1804 | m_childView = (wxView *) NULL; | |
1805 | m_childDocument = (wxDocument *) NULL; | |
1806 | ||
1807 | this->Destroy(); | |
1808 | } | |
1809 | else | |
1810 | event.Veto(); | |
1811 | } | |
1812 | else | |
1813 | event.Veto(); | |
1814 | } | |
1815 | ||
1816 | // ---------------------------------------------------------------------------- | |
1817 | // Default parent frame | |
1818 | // ---------------------------------------------------------------------------- | |
1819 | ||
1820 | BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame) | |
1821 | EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit) | |
1822 | EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile) | |
1823 | EVT_CLOSE(wxDocParentFrame::OnCloseWindow) | |
1824 | END_EVENT_TABLE() | |
1825 | ||
1826 | wxDocParentFrame::wxDocParentFrame(wxDocManager *manager, | |
1827 | wxFrame *frame, | |
1828 | wxWindowID id, | |
1829 | const wxString& title, | |
1830 | const wxPoint& pos, | |
1831 | const wxSize& size, | |
1832 | long style, | |
1833 | const wxString& name) | |
1834 | : wxFrame(frame, id, title, pos, size, style, name) | |
1835 | { | |
1836 | m_docManager = manager; | |
1837 | } | |
1838 | ||
1839 | void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event)) | |
1840 | { | |
1841 | Close(); | |
1842 | } | |
1843 | ||
1844 | void wxDocParentFrame::OnMRUFile(wxCommandEvent& event) | |
1845 | { | |
1846 | int n = event.GetId() - wxID_FILE1; // the index in MRU list | |
1847 | wxString filename(m_docManager->GetHistoryFile(n)); | |
1848 | if ( !filename.IsEmpty() ) | |
1849 | { | |
1850 | // verify that the file exists before doing anything else | |
1851 | if ( wxFile::Exists(filename) ) | |
1852 | { | |
1853 | // try to open it | |
1854 | if (!m_docManager->CreateDocument(filename, wxDOC_SILENT)) | |
1855 | { | |
1856 | // remove the file from the MRU list. The user should already be notified. | |
1857 | m_docManager->RemoveFileFromHistory(n); | |
1858 | ||
1859 | wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."), | |
1860 | filename.c_str()); | |
1861 | } | |
1862 | } | |
1863 | else | |
1864 | { | |
1865 | // remove the bogus filename from the MRU list and notify the user | |
1866 | // about it | |
1867 | m_docManager->RemoveFileFromHistory(n); | |
1868 | ||
1869 | wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."), | |
1870 | filename.c_str()); | |
1871 | } | |
1872 | } | |
1873 | } | |
1874 | ||
1875 | // Extend event processing to search the view's event table | |
1876 | bool wxDocParentFrame::ProcessEvent(wxEvent& event) | |
1877 | { | |
1878 | // Try the document manager, then do default processing | |
1879 | if (!m_docManager || !m_docManager->ProcessEvent(event)) | |
1880 | return wxEvtHandler::ProcessEvent(event); | |
1881 | else | |
1882 | return TRUE; | |
1883 | } | |
1884 | ||
1885 | // Define the behaviour for the frame closing | |
1886 | // - must delete all frames except for the main one. | |
1887 | void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event) | |
1888 | { | |
1889 | if (m_docManager->Clear(!event.CanVeto())) | |
1890 | { | |
1891 | this->Destroy(); | |
1892 | } | |
1893 | else | |
1894 | event.Veto(); | |
1895 | } | |
1896 | ||
1897 | #if wxUSE_PRINTING_ARCHITECTURE | |
1898 | ||
1899 | wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title) | |
1900 | : wxPrintout(title) | |
1901 | { | |
1902 | m_printoutView = view; | |
1903 | } | |
1904 | ||
1905 | bool wxDocPrintout::OnPrintPage(int WXUNUSED(page)) | |
1906 | { | |
1907 | wxDC *dc = GetDC(); | |
1908 | ||
1909 | // Get the logical pixels per inch of screen and printer | |
1910 | int ppiScreenX, ppiScreenY; | |
1911 | GetPPIScreen(&ppiScreenX, &ppiScreenY); | |
1912 | wxUnusedVar(ppiScreenY); | |
1913 | int ppiPrinterX, ppiPrinterY; | |
1914 | GetPPIPrinter(&ppiPrinterX, &ppiPrinterY); | |
1915 | wxUnusedVar(ppiPrinterY); | |
1916 | ||
1917 | // This scales the DC so that the printout roughly represents the | |
1918 | // the screen scaling. The text point size _should_ be the right size | |
1919 | // but in fact is too small for some reason. This is a detail that will | |
1920 | // need to be addressed at some point but can be fudged for the | |
1921 | // moment. | |
1922 | float scale = (float)((float)ppiPrinterX/(float)ppiScreenX); | |
1923 | ||
1924 | // Now we have to check in case our real page size is reduced | |
1925 | // (e.g. because we're drawing to a print preview memory DC) | |
1926 | int pageWidth, pageHeight; | |
1927 | int w, h; | |
1928 | dc->GetSize(&w, &h); | |
1929 | GetPageSizePixels(&pageWidth, &pageHeight); | |
1930 | wxUnusedVar(pageHeight); | |
1931 | ||
1932 | // If printer pageWidth == current DC width, then this doesn't | |
1933 | // change. But w might be the preview bitmap width, so scale down. | |
1934 | float overallScale = scale * (float)(w/(float)pageWidth); | |
1935 | dc->SetUserScale(overallScale, overallScale); | |
1936 | ||
1937 | if (m_printoutView) | |
1938 | { | |
1939 | m_printoutView->OnDraw(dc); | |
1940 | } | |
1941 | return TRUE; | |
1942 | } | |
1943 | ||
1944 | bool wxDocPrintout::HasPage(int pageNum) | |
1945 | { | |
1946 | return (pageNum == 1); | |
1947 | } | |
1948 | ||
1949 | bool wxDocPrintout::OnBeginDocument(int startPage, int endPage) | |
1950 | { | |
1951 | if (!wxPrintout::OnBeginDocument(startPage, endPage)) | |
1952 | return FALSE; | |
1953 | ||
1954 | return TRUE; | |
1955 | } | |
1956 | ||
1957 | void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) | |
1958 | { | |
1959 | *minPage = 1; | |
1960 | *maxPage = 1; | |
1961 | *selPageFrom = 1; | |
1962 | *selPageTo = 1; | |
1963 | } | |
1964 | ||
1965 | #endif // wxUSE_PRINTING_ARCHITECTURE | |
1966 | ||
1967 | // ---------------------------------------------------------------------------- | |
1968 | // File history processor | |
1969 | // ---------------------------------------------------------------------------- | |
1970 | ||
1971 | static inline wxChar* MYcopystring(const wxString& s) | |
1972 | { | |
1973 | wxChar* copy = new wxChar[s.length() + 1]; | |
1974 | return wxStrcpy(copy, s.c_str()); | |
1975 | } | |
1976 | ||
1977 | static inline wxChar* MYcopystring(const wxChar* s) | |
1978 | { | |
1979 | wxChar* copy = new wxChar[wxStrlen(s) + 1]; | |
1980 | return wxStrcpy(copy, s); | |
1981 | } | |
1982 | ||
1983 | wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase) | |
1984 | { | |
1985 | m_fileMaxFiles = maxFiles; | |
1986 | m_idBase = idBase; | |
1987 | m_fileHistoryN = 0; | |
1988 | m_fileHistory = new wxChar *[m_fileMaxFiles]; | |
1989 | } | |
1990 | ||
1991 | wxFileHistory::~wxFileHistory() | |
1992 | { | |
1993 | size_t i; | |
1994 | for (i = 0; i < m_fileHistoryN; i++) | |
1995 | delete[] m_fileHistory[i]; | |
1996 | delete[] m_fileHistory; | |
1997 | } | |
1998 | ||
1999 | // File history management | |
2000 | void wxFileHistory::AddFileToHistory(const wxString& file) | |
2001 | { | |
2002 | size_t i; | |
2003 | ||
2004 | // Check we don't already have this file | |
2005 | for (i = 0; i < m_fileHistoryN; i++) | |
2006 | { | |
2007 | #if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names | |
2008 | wxString testString; | |
2009 | if ( m_fileHistory[i] ) | |
2010 | testString = m_fileHistory[i]; | |
2011 | if ( m_fileHistory[i] && ( file.Lower() == testString.Lower() ) ) | |
2012 | #else | |
2013 | if ( m_fileHistory[i] && ( file == m_fileHistory[i] ) ) | |
2014 | #endif | |
2015 | { | |
2016 | // we do have it, move it to the top of the history | |
2017 | RemoveFileFromHistory (i); | |
2018 | AddFileToHistory (file); | |
2019 | return; | |
2020 | } | |
2021 | } | |
2022 | ||
2023 | // if we already have a full history, delete the one at the end | |
2024 | if ( m_fileMaxFiles == m_fileHistoryN ) | |
2025 | { | |
2026 | RemoveFileFromHistory (m_fileHistoryN - 1); | |
2027 | AddFileToHistory (file); | |
2028 | return; | |
2029 | } | |
2030 | ||
2031 | // Add to the project file history: | |
2032 | // Move existing files (if any) down so we can insert file at beginning. | |
2033 | if (m_fileHistoryN < m_fileMaxFiles) | |
2034 | { | |
2035 | wxList::compatibility_iterator node = m_fileMenus.GetFirst(); | |
2036 | while (node) | |
2037 | { | |
2038 | wxMenu* menu = (wxMenu*) node->GetData(); | |
2039 | if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() ) | |
2040 | { | |
2041 | menu->AppendSeparator(); | |
2042 | } | |
2043 | menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]")); | |
2044 | node = node->GetNext(); | |
2045 | } | |
2046 | m_fileHistoryN ++; | |
2047 | } | |
2048 | // Shuffle filenames down | |
2049 | for (i = (m_fileHistoryN-1); i > 0; i--) | |
2050 | { | |
2051 | m_fileHistory[i] = m_fileHistory[i-1]; | |
2052 | } | |
2053 | m_fileHistory[0] = MYcopystring(file); | |
2054 | ||
2055 | // this is the directory of the last opened file | |
2056 | wxString pathCurrent; | |
2057 | wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL ); | |
2058 | for (i = 0; i < m_fileHistoryN; i++) | |
2059 | { | |
2060 | if ( m_fileHistory[i] ) | |
2061 | { | |
2062 | // if in same directory just show the filename; otherwise the full | |
2063 | // path | |
2064 | wxString pathInMenu, path, filename, ext; | |
2065 | wxSplitPath( m_fileHistory[i], &path, &filename, &ext ); | |
2066 | if ( path == pathCurrent ) | |
2067 | { | |
2068 | pathInMenu = filename; | |
2069 | if ( !ext.empty() ) | |
2070 | pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext; | |
2071 | } | |
2072 | else | |
2073 | { | |
2074 | // absolute path; could also set relative path | |
2075 | pathInMenu = m_fileHistory[i]; | |
2076 | } | |
2077 | ||
2078 | wxString buf; | |
2079 | buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str()); | |
2080 | wxList::compatibility_iterator node = m_fileMenus.GetFirst(); | |
2081 | while (node) | |
2082 | { | |
2083 | wxMenu* menu = (wxMenu*) node->GetData(); | |
2084 | menu->SetLabel(m_idBase + i, buf); | |
2085 | node = node->GetNext(); | |
2086 | } | |
2087 | } | |
2088 | } | |
2089 | } | |
2090 | ||
2091 | void wxFileHistory::RemoveFileFromHistory(size_t i) | |
2092 | { | |
2093 | wxCHECK_RET( i < m_fileHistoryN, | |
2094 | wxT("invalid index in wxFileHistory::RemoveFileFromHistory") ); | |
2095 | ||
2096 | // delete the element from the array (could use memmove() too...) | |
2097 | delete [] m_fileHistory[i]; | |
2098 | ||
2099 | size_t j; | |
2100 | for ( j = i; j < m_fileHistoryN - 1; j++ ) | |
2101 | { | |
2102 | m_fileHistory[j] = m_fileHistory[j + 1]; | |
2103 | } | |
2104 | ||
2105 | wxList::compatibility_iterator node = m_fileMenus.GetFirst(); | |
2106 | while ( node ) | |
2107 | { | |
2108 | wxMenu* menu = (wxMenu*) node->GetData(); | |
2109 | ||
2110 | // shuffle filenames up | |
2111 | wxString buf; | |
2112 | for ( j = i; j < m_fileHistoryN - 1; j++ ) | |
2113 | { | |
2114 | buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]); | |
2115 | menu->SetLabel(m_idBase + j, buf); | |
2116 | } | |
2117 | ||
2118 | node = node->GetNext(); | |
2119 | ||
2120 | // delete the last menu item which is unused now | |
2121 | wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1; | |
2122 | if (menu->FindItem(lastItemId)) | |
2123 | { | |
2124 | menu->Delete(lastItemId); | |
2125 | } | |
2126 | ||
2127 | // delete the last separator too if no more files are left | |
2128 | if ( m_fileHistoryN == 1 ) | |
2129 | { | |
2130 | wxMenuItemList::compatibility_iterator node = menu->GetMenuItems().GetLast(); | |
2131 | if ( node ) | |
2132 | { | |
2133 | wxMenuItem *menuItem = node->GetData(); | |
2134 | if ( menuItem->IsSeparator() ) | |
2135 | { | |
2136 | menu->Delete(menuItem); | |
2137 | } | |
2138 | //else: should we search backwards for the last separator? | |
2139 | } | |
2140 | //else: menu is empty somehow | |
2141 | } | |
2142 | } | |
2143 | ||
2144 | m_fileHistoryN--; | |
2145 | } | |
2146 | ||
2147 | wxString wxFileHistory::GetHistoryFile(size_t i) const | |
2148 | { | |
2149 | wxString s; | |
2150 | if ( i < m_fileHistoryN ) | |
2151 | { | |
2152 | s = m_fileHistory[i]; | |
2153 | } | |
2154 | else | |
2155 | { | |
2156 | wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") ); | |
2157 | } | |
2158 | ||
2159 | return s; | |
2160 | } | |
2161 | ||
2162 | void wxFileHistory::UseMenu(wxMenu *menu) | |
2163 | { | |
2164 | if (!m_fileMenus.Member(menu)) | |
2165 | m_fileMenus.Append(menu); | |
2166 | } | |
2167 | ||
2168 | void wxFileHistory::RemoveMenu(wxMenu *menu) | |
2169 | { | |
2170 | m_fileMenus.DeleteObject(menu); | |
2171 | } | |
2172 | ||
2173 | #if wxUSE_CONFIG | |
2174 | void wxFileHistory::Load(wxConfigBase& config) | |
2175 | { | |
2176 | m_fileHistoryN = 0; | |
2177 | wxString buf; | |
2178 | buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1); | |
2179 | wxString historyFile; | |
2180 | while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT(""))) | |
2181 | { | |
2182 | m_fileHistory[m_fileHistoryN] = MYcopystring((const wxChar*) historyFile); | |
2183 | m_fileHistoryN ++; | |
2184 | buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1); | |
2185 | historyFile = wxT(""); | |
2186 | } | |
2187 | AddFilesToMenu(); | |
2188 | } | |
2189 | ||
2190 | void wxFileHistory::Save(wxConfigBase& config) | |
2191 | { | |
2192 | size_t i; | |
2193 | for (i = 0; i < m_fileMaxFiles; i++) | |
2194 | { | |
2195 | wxString buf; | |
2196 | buf.Printf(wxT("file%d"), (int)i+1); | |
2197 | if (i < m_fileHistoryN) | |
2198 | config.Write(buf, wxString(m_fileHistory[i])); | |
2199 | else | |
2200 | config.Write(buf, wxEmptyString); | |
2201 | } | |
2202 | } | |
2203 | #endif // wxUSE_CONFIG | |
2204 | ||
2205 | void wxFileHistory::AddFilesToMenu() | |
2206 | { | |
2207 | if (m_fileHistoryN > 0) | |
2208 | { | |
2209 | wxList::compatibility_iterator node = m_fileMenus.GetFirst(); | |
2210 | while (node) | |
2211 | { | |
2212 | wxMenu* menu = (wxMenu*) node->GetData(); | |
2213 | if (menu->GetMenuItemCount()) | |
2214 | { | |
2215 | menu->AppendSeparator(); | |
2216 | } | |
2217 | ||
2218 | size_t i; | |
2219 | for (i = 0; i < m_fileHistoryN; i++) | |
2220 | { | |
2221 | if (m_fileHistory[i]) | |
2222 | { | |
2223 | wxString buf; | |
2224 | buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]); | |
2225 | menu->Append(m_idBase+i, buf); | |
2226 | } | |
2227 | } | |
2228 | node = node->GetNext(); | |
2229 | } | |
2230 | } | |
2231 | } | |
2232 | ||
2233 | void wxFileHistory::AddFilesToMenu(wxMenu* menu) | |
2234 | { | |
2235 | if (m_fileHistoryN > 0) | |
2236 | { | |
2237 | if (menu->GetMenuItemCount()) | |
2238 | { | |
2239 | menu->AppendSeparator(); | |
2240 | } | |
2241 | ||
2242 | size_t i; | |
2243 | for (i = 0; i < m_fileHistoryN; i++) | |
2244 | { | |
2245 | if (m_fileHistory[i]) | |
2246 | { | |
2247 | wxString buf; | |
2248 | buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]); | |
2249 | menu->Append(m_idBase+i, buf); | |
2250 | } | |
2251 | } | |
2252 | } | |
2253 | } | |
2254 | ||
2255 | // ---------------------------------------------------------------------------- | |
2256 | // Permits compatibility with existing file formats and functions that | |
2257 | // manipulate files directly | |
2258 | // ---------------------------------------------------------------------------- | |
2259 | ||
2260 | #if wxUSE_STD_IOSTREAM | |
2261 | ||
2262 | bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream) | |
2263 | { | |
2264 | wxFFile file(filename, _T("rb")); | |
2265 | if ( !file.IsOpened() ) | |
2266 | return FALSE; | |
2267 | ||
2268 | char buf[4096]; | |
2269 | ||
2270 | size_t nRead; | |
2271 | do | |
2272 | { | |
2273 | nRead = file.Read(buf, WXSIZEOF(buf)); | |
2274 | if ( file.Error() ) | |
2275 | return FALSE; | |
2276 | ||
2277 | stream.write(buf, nRead); | |
2278 | if ( !stream ) | |
2279 | return FALSE; | |
2280 | } | |
2281 | while ( !file.Eof() ); | |
2282 | ||
2283 | return TRUE; | |
2284 | } | |
2285 | ||
2286 | bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename) | |
2287 | { | |
2288 | wxFFile file(filename, _T("wb")); | |
2289 | if ( !file.IsOpened() ) | |
2290 | return FALSE; | |
2291 | ||
2292 | char buf[4096]; | |
2293 | do | |
2294 | { | |
2295 | stream.read(buf, WXSIZEOF(buf)); | |
2296 | if ( !stream.bad() ) // fail may be set on EOF, don't use operator!() | |
2297 | { | |
2298 | if ( !file.Write(buf, stream.gcount()) ) | |
2299 | return FALSE; | |
2300 | } | |
2301 | } | |
2302 | while ( !stream.eof() ); | |
2303 | ||
2304 | return TRUE; | |
2305 | } | |
2306 | ||
2307 | #else // !wxUSE_STD_IOSTREAM | |
2308 | ||
2309 | bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream) | |
2310 | { | |
2311 | wxFFile file(filename, _T("rb")); | |
2312 | if ( !file.IsOpened() ) | |
2313 | return FALSE; | |
2314 | ||
2315 | char buf[4096]; | |
2316 | ||
2317 | size_t nRead; | |
2318 | do | |
2319 | { | |
2320 | nRead = file.Read(buf, WXSIZEOF(buf)); | |
2321 | if ( file.Error() ) | |
2322 | return FALSE; | |
2323 | ||
2324 | stream.Write(buf, nRead); | |
2325 | if ( !stream ) | |
2326 | return FALSE; | |
2327 | } | |
2328 | while ( !file.Eof() ); | |
2329 | ||
2330 | return TRUE; | |
2331 | } | |
2332 | ||
2333 | bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename) | |
2334 | { | |
2335 | wxFFile file(filename, _T("wb")); | |
2336 | if ( !file.IsOpened() ) | |
2337 | return FALSE; | |
2338 | ||
2339 | char buf[4096]; | |
2340 | do | |
2341 | { | |
2342 | stream.Read(buf, WXSIZEOF(buf)); | |
2343 | ||
2344 | const size_t nRead = stream.LastRead(); | |
2345 | if ( !nRead || !file.Write(buf, nRead) ) | |
2346 | return FALSE; | |
2347 | } | |
2348 | while ( !stream.Eof() ); | |
2349 | ||
2350 | return TRUE; | |
2351 | } | |
2352 | ||
2353 | #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM | |
2354 | ||
2355 | #endif // wxUSE_DOC_VIEW_ARCHITECTURE | |
2356 |