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