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