]> git.saurik.com Git - wxWidgets.git/blob - src/common/docview.cpp
fixed the menu bug introduced by the previous commit
[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 }
978
979 void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
980 {
981 wxDocument *doc = GetCurrentDocument();
982 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanRedo()) );
983 }
984
985 void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event)
986 {
987 wxDocument *doc = GetCurrentDocument();
988 event.Enable( (doc != (wxDocument*) NULL) );
989 }
990
991 void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent& event)
992 {
993 event.Enable( TRUE );
994 }
995
996 void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event)
997 {
998 wxDocument *doc = GetCurrentDocument();
999 event.Enable( (doc != (wxDocument*) NULL) );
1000 }
1001
1002 wxView *wxDocManager::GetCurrentView() const
1003 {
1004 if (m_currentView)
1005 return m_currentView;
1006 if (m_docs.Number() == 1)
1007 {
1008 wxDocument* doc = (wxDocument*) m_docs.First()->Data();
1009 return doc->GetFirstView();
1010 }
1011 return (wxView *) NULL;
1012 }
1013
1014 // Extend event processing to search the view's event table
1015 bool wxDocManager::ProcessEvent(wxEvent& event)
1016 {
1017 wxView* view = GetCurrentView();
1018 if (view)
1019 {
1020 if (view->ProcessEvent(event))
1021 return TRUE;
1022 }
1023 return wxEvtHandler::ProcessEvent(event);
1024 }
1025
1026 wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
1027 {
1028 wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
1029 int i;
1030 int n = 0;
1031 for (i = 0; i < m_templates.Number(); i++)
1032 {
1033 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
1034 if (temp->IsVisible())
1035 {
1036 templates[n] = temp;
1037 n ++;
1038 }
1039 }
1040 if (n == 0)
1041 {
1042 delete[] templates;
1043 return (wxDocument *) NULL;
1044 }
1045
1046 // If we've reached the max number of docs, close the
1047 // first one.
1048 if (GetDocuments().Number() >= m_maxDocsOpen)
1049 {
1050 wxDocument *doc = (wxDocument *)GetDocuments().First()->Data();
1051 if (doc->Close())
1052 {
1053 // Implicitly deletes the document when
1054 // the last view is deleted
1055 doc->DeleteAllViews();
1056
1057 // Check we're really deleted
1058 if (m_docs.Member(doc))
1059 delete doc;
1060 }
1061 else
1062 {
1063 delete[] templates;
1064 return (wxDocument *) NULL;
1065 }
1066 }
1067
1068 // New document: user chooses a template, unless there's only one.
1069 if (flags & wxDOC_NEW)
1070 {
1071 if (n == 1)
1072 {
1073 wxDocTemplate *temp = templates[0];
1074 delete[] templates;
1075 wxDocument *newDoc = temp->CreateDocument(path, flags);
1076 if (newDoc)
1077 {
1078 newDoc->SetDocumentName(temp->GetDocumentName());
1079 newDoc->SetDocumentTemplate(temp);
1080 newDoc->OnNewDocument();
1081 }
1082 return newDoc;
1083 }
1084
1085 wxDocTemplate *temp = SelectDocumentType(templates, n);
1086 delete[] templates;
1087 if (temp)
1088 {
1089 wxDocument *newDoc = temp->CreateDocument(path, flags);
1090 if (newDoc)
1091 {
1092 newDoc->SetDocumentName(temp->GetDocumentName());
1093 newDoc->SetDocumentTemplate(temp);
1094 newDoc->OnNewDocument();
1095 }
1096 return newDoc;
1097 }
1098 else
1099 return (wxDocument *) NULL;
1100 }
1101
1102 // Existing document
1103 wxDocTemplate *temp = (wxDocTemplate *) NULL;
1104
1105 wxString path2(wxT(""));
1106 if (path != wxT(""))
1107 path2 = path;
1108
1109 if (flags & wxDOC_SILENT)
1110 temp = FindTemplateForPath(path2);
1111 else
1112 temp = SelectDocumentPath(templates, n, path2, flags);
1113
1114 delete[] templates;
1115
1116 if (temp)
1117 {
1118 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1119 if (newDoc)
1120 {
1121 newDoc->SetDocumentName(temp->GetDocumentName());
1122 newDoc->SetDocumentTemplate(temp);
1123 if (!newDoc->OnOpenDocument(path2))
1124 {
1125 newDoc->DeleteAllViews();
1126 // delete newDoc; // Implicitly deleted by DeleteAllViews
1127 return (wxDocument *) NULL;
1128 }
1129 AddFileToHistory(path2);
1130 }
1131 return newDoc;
1132 }
1133
1134 return (wxDocument *) NULL;
1135 }
1136
1137 wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1138 {
1139 wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
1140 int n =0;
1141 int i;
1142 for (i = 0; i < m_templates.Number(); i++)
1143 {
1144 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
1145 if (temp->IsVisible())
1146 {
1147 if (temp->GetDocumentName() == doc->GetDocumentName())
1148 {
1149 templates[n] = temp;
1150 n ++;
1151 }
1152 }
1153 }
1154 if (n == 0)
1155 {
1156 delete[] templates;
1157 return (wxView *) NULL;
1158 }
1159 if (n == 1)
1160 {
1161 wxDocTemplate *temp = templates[0];
1162 delete[] templates;
1163 wxView *view = temp->CreateView(doc, flags);
1164 if (view)
1165 view->SetViewName(temp->GetViewName());
1166 return view;
1167 }
1168
1169 wxDocTemplate *temp = SelectViewType(templates, n);
1170 delete[] templates;
1171 if (temp)
1172 {
1173 wxView *view = temp->CreateView(doc, flags);
1174 if (view)
1175 view->SetViewName(temp->GetViewName());
1176 return view;
1177 }
1178 else
1179 return (wxView *) NULL;
1180 }
1181
1182 // Not yet implemented
1183 void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1184 {
1185 }
1186
1187 // Not yet implemented
1188 bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1189 {
1190 return FALSE;
1191 }
1192
1193 wxDocument *wxDocManager::GetCurrentDocument() const
1194 {
1195 wxView *view = GetCurrentView();
1196 if (view)
1197 return view->GetDocument();
1198 else
1199 return (wxDocument *) NULL;
1200 }
1201
1202 // Make a default document name
1203 bool wxDocManager::MakeDefaultName(wxString& name)
1204 {
1205 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1206 m_defaultDocumentNameCounter++;
1207
1208 return TRUE;
1209 }
1210
1211 // Make a frame title (override this to do something different)
1212 // If docName is empty, a document is not currently active.
1213 wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1214 {
1215 wxString appName = wxTheApp->GetAppName();
1216 wxString title;
1217 if (!doc)
1218 title = appName;
1219 else
1220 {
1221 wxString docName;
1222 doc->GetPrintableName(docName);
1223 title = docName + wxString(_(" - ")) + appName;
1224 }
1225 return title;
1226 }
1227
1228
1229 // Not yet implemented
1230 wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1231 {
1232 return (wxDocTemplate *) NULL;
1233 }
1234
1235 // File history management
1236 void wxDocManager::AddFileToHistory(const wxString& file)
1237 {
1238 if (m_fileHistory)
1239 m_fileHistory->AddFileToHistory(file);
1240 }
1241
1242 void wxDocManager::RemoveFileFromHistory(int i)
1243 {
1244 if (m_fileHistory)
1245 m_fileHistory->RemoveFileFromHistory(i);
1246 }
1247
1248 wxString wxDocManager::GetHistoryFile(int i) const
1249 {
1250 wxString histFile;
1251
1252 if (m_fileHistory)
1253 histFile = m_fileHistory->GetHistoryFile(i);
1254
1255 return histFile;
1256 }
1257
1258 void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1259 {
1260 if (m_fileHistory)
1261 m_fileHistory->UseMenu(menu);
1262 }
1263
1264 void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
1265 {
1266 if (m_fileHistory)
1267 m_fileHistory->RemoveMenu(menu);
1268 }
1269
1270 #if wxUSE_CONFIG
1271 void wxDocManager::FileHistoryLoad(wxConfigBase& config)
1272 {
1273 if (m_fileHistory)
1274 m_fileHistory->Load(config);
1275 }
1276
1277 void wxDocManager::FileHistorySave(wxConfigBase& config)
1278 {
1279 if (m_fileHistory)
1280 m_fileHistory->Save(config);
1281 }
1282 #endif
1283
1284 void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1285 {
1286 if (m_fileHistory)
1287 m_fileHistory->AddFilesToMenu(menu);
1288 }
1289
1290 void wxDocManager::FileHistoryAddFilesToMenu()
1291 {
1292 if (m_fileHistory)
1293 m_fileHistory->AddFilesToMenu();
1294 }
1295
1296 int wxDocManager::GetNoHistoryFiles() const
1297 {
1298 if (m_fileHistory)
1299 return m_fileHistory->GetNoHistoryFiles();
1300 else
1301 return 0;
1302 }
1303
1304
1305 // Find out the document template via matching in the document file format
1306 // against that of the template
1307 wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1308 {
1309 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
1310
1311 // Find the template which this extension corresponds to
1312 int i;
1313 for (i = 0; i < m_templates.Number(); i++)
1314 {
1315 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Nth(i)->Data();
1316 if ( temp->FileMatchesTemplate(path) )
1317 {
1318 theTemplate = temp;
1319 break;
1320 }
1321 }
1322 return theTemplate;
1323 }
1324
1325 // Try to get a more suitable parent frame than the top window,
1326 // for selection dialogs. Otherwise you may get an unexpected
1327 // window being activated when a dialog is shown.
1328 static wxWindow* wxFindSuitableParent()
1329 {
1330 wxWindow* parent = wxTheApp->GetTopWindow();
1331
1332 wxWindow* focusWindow = wxWindow::FindFocus();
1333 if (focusWindow)
1334 {
1335 while (focusWindow &&
1336 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
1337 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
1338
1339 focusWindow = focusWindow->GetParent();
1340
1341 if (focusWindow)
1342 parent = focusWindow;
1343 }
1344 return parent;
1345 }
1346
1347 // Prompts user to open a file, using file specs in templates.
1348 // How to implement in wxWindows? Must extend the file selector
1349 // dialog or implement own; OR match the extension to the
1350 // template extension.
1351
1352 wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
1353 #if defined(__WXMSW__) || defined(__WXGTK__)
1354 int noTemplates,
1355 #else
1356 int WXUNUSED(noTemplates),
1357 #endif
1358 wxString& path,
1359 long WXUNUSED(flags),
1360 bool WXUNUSED(save))
1361 {
1362 // We can only have multiple filters in Windows and GTK
1363 #if defined(__WXMSW__) || defined(__WXGTK__)
1364 wxString descrBuf;
1365
1366 int i;
1367 for (i = 0; i < noTemplates; i++)
1368 {
1369 if (templates[i]->IsVisible())
1370 {
1371 // add a '|' to separate this filter from the previous one
1372 if ( !descrBuf.IsEmpty() )
1373 descrBuf << wxT('|');
1374
1375 descrBuf << templates[i]->GetDescription()
1376 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
1377 << templates[i]->GetFileFilter();
1378 }
1379 }
1380 #else
1381 wxString descrBuf = wxT("*.*");
1382 #endif
1383
1384 int FilterIndex = -1;
1385
1386 wxWindow* parent = wxFindSuitableParent();
1387
1388 wxString pathTmp = wxFileSelectorEx(_("Select a file"),
1389 m_lastDirectory,
1390 wxT(""),
1391 &FilterIndex,
1392 descrBuf,
1393 0,
1394 parent);
1395
1396 wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
1397 if (!pathTmp.IsEmpty())
1398 {
1399 if (!wxFileExists(pathTmp))
1400 {
1401 wxString msgTitle;
1402 if (!wxTheApp->GetAppName().IsEmpty())
1403 msgTitle = wxTheApp->GetAppName();
1404 else
1405 msgTitle = wxString(_("File error"));
1406
1407 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1408 parent);
1409
1410 path = wxT("");
1411 return (wxDocTemplate *) NULL;
1412 }
1413 m_lastDirectory = wxPathOnly(pathTmp);
1414
1415 path = pathTmp;
1416
1417 // first choose the template using the extension, if this fails (i.e.
1418 // wxFileSelectorEx() didn't fill it), then use the path
1419 if ( FilterIndex != -1 )
1420 theTemplate = templates[FilterIndex];
1421 if ( !theTemplate )
1422 theTemplate = FindTemplateForPath(path);
1423 }
1424 else
1425 {
1426 path = wxT("");
1427 }
1428
1429 return theTemplate;
1430
1431 #if 0
1432 // In all other windowing systems, until we have more advanced
1433 // file selectors, we must select the document type (template) first, and
1434 // _then_ pop up the file selector.
1435 wxDocTemplate *temp = SelectDocumentType(templates, noTemplates);
1436 if (!temp)
1437 return (wxDocTemplate *) NULL;
1438
1439 wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""),
1440 temp->GetDefaultExtension(),
1441 temp->GetFileFilter(),
1442 0, wxTheApp->GetTopWindow());
1443
1444 if (pathTmp)
1445 {
1446 path = pathTmp;
1447 return temp;
1448 }
1449 else
1450 return (wxDocTemplate *) NULL;
1451 #endif // 0
1452 }
1453
1454 wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
1455 int noTemplates, bool sort)
1456 {
1457 wxArrayString strings(sort);
1458 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
1459 int i;
1460 int n = 0;
1461 for (i = 0; i < noTemplates; i++)
1462 {
1463 if (templates[i]->IsVisible())
1464 {
1465 strings.Add(templates[i]->m_description);
1466 if (!sort)
1467 {
1468 data[n] = templates[i];
1469 n ++;
1470 }
1471 }
1472 } // for
1473
1474 if (sort)
1475 {
1476 // Yes, this will be slow, but template lists
1477 // are typically short.
1478 int j;
1479 n = strings.Count();
1480 for (i = 0; i < n; i++)
1481 {
1482 for (j = 0; j < noTemplates; j++)
1483 {
1484 if (strings[i] == templates[j]->m_description)
1485 data[i] = templates[j];
1486 }
1487 }
1488 }
1489
1490 wxDocTemplate *theTemplate;
1491
1492 switch ( n )
1493 {
1494 case 0:
1495 // no visible templates, hence nothing to choose from
1496 theTemplate = NULL;
1497 break;
1498
1499 case 1:
1500 // don't propose the user to choose if he heas no choice
1501 theTemplate = data[0];
1502 break;
1503
1504 default:
1505 // propose the user to choose one of several
1506 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1507 (
1508 _("Select a document template"),
1509 _("Templates"),
1510 strings,
1511 (void **)data,
1512 wxFindSuitableParent()
1513 );
1514 }
1515
1516 delete[] data;
1517
1518 return theTemplate;
1519 }
1520
1521 wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
1522 int noTemplates, bool sort)
1523 {
1524 wxArrayString strings(sort);
1525 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
1526 int i;
1527 int n = 0;
1528 for (i = 0; i < noTemplates; i++)
1529 {
1530 wxDocTemplate *templ = templates[i];
1531 if ( templ->IsVisible() && !templ->GetViewName().empty() )
1532 {
1533 strings.Add(templ->m_viewTypeName);
1534 if (!sort)
1535 {
1536 data[n] = templ;
1537 n ++;
1538 }
1539 }
1540 }
1541
1542 if (sort)
1543 {
1544 // Yes, this will be slow, but template lists
1545 // are typically short.
1546 int j;
1547 n = strings.Count();
1548 for (i = 0; i < n; i++)
1549 {
1550 for (j = 0; j < noTemplates; j++)
1551 {
1552 if (strings[i] == templates[j]->m_viewTypeName)
1553 data[i] = templates[j];
1554 }
1555 }
1556 }
1557
1558 wxDocTemplate *theTemplate;
1559
1560 // the same logic as above
1561 switch ( n )
1562 {
1563 case 0:
1564 theTemplate = (wxDocTemplate *)NULL;
1565 break;
1566
1567 case 1:
1568 theTemplate = data[0];
1569 break;
1570
1571 default:
1572 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1573 (
1574 _("Select a document view"),
1575 _("Views"),
1576 strings,
1577 (void **)data,
1578 wxFindSuitableParent()
1579 );
1580
1581 }
1582
1583 delete[] data;
1584 return theTemplate;
1585 }
1586
1587 void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1588 {
1589 if (!m_templates.Member(temp))
1590 m_templates.Append(temp);
1591 }
1592
1593 void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1594 {
1595 m_templates.DeleteObject(temp);
1596 }
1597
1598 // Add and remove a document from the manager's list
1599 void wxDocManager::AddDocument(wxDocument *doc)
1600 {
1601 if (!m_docs.Member(doc))
1602 m_docs.Append(doc);
1603 }
1604
1605 void wxDocManager::RemoveDocument(wxDocument *doc)
1606 {
1607 m_docs.DeleteObject(doc);
1608 }
1609
1610 // Views or windows should inform the document manager
1611 // when a view is going in or out of focus
1612 void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting))
1613 {
1614 // If we're deactiving, and if we're not actually deleting the view, then
1615 // don't reset the current view because we may be going to
1616 // a window without a view.
1617 // WHAT DID I MEAN BY THAT EXACTLY?
1618 /*
1619 if (deleting)
1620 {
1621 if (m_currentView == view)
1622 m_currentView = NULL;
1623 }
1624 else
1625 */
1626 {
1627 if (activate)
1628 m_currentView = view;
1629 else
1630 m_currentView = (wxView *) NULL;
1631 }
1632 }
1633
1634 // ----------------------------------------------------------------------------
1635 // Default document child frame
1636 // ----------------------------------------------------------------------------
1637
1638 BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1639 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
1640 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
1641 END_EVENT_TABLE()
1642
1643 wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1644 wxView *view,
1645 wxFrame *frame,
1646 wxWindowID id,
1647 const wxString& title,
1648 const wxPoint& pos,
1649 const wxSize& size,
1650 long style,
1651 const wxString& name)
1652 : wxFrame(frame, id, title, pos, size, style, name)
1653 {
1654 m_childDocument = doc;
1655 m_childView = view;
1656 if (view)
1657 view->SetFrame(this);
1658 }
1659
1660 wxDocChildFrame::~wxDocChildFrame()
1661 {
1662 }
1663
1664 // Extend event processing to search the view's event table
1665 bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1666 {
1667 if (m_childView)
1668 m_childView->Activate(TRUE);
1669
1670 if ( !m_childView || ! m_childView->ProcessEvent(event) )
1671 {
1672 // Only hand up to the parent if it's a menu command
1673 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
1674 return wxEvtHandler::ProcessEvent(event);
1675 else
1676 return TRUE;
1677 }
1678 else
1679 return TRUE;
1680 }
1681
1682 void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1683 {
1684 wxFrame::OnActivate(event);
1685
1686 if (m_childView)
1687 m_childView->Activate(event.GetActive());
1688 }
1689
1690 void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
1691 {
1692 if (m_childView)
1693 {
1694 bool ans = FALSE;
1695 if (!event.CanVeto())
1696 ans = TRUE; // Must delete.
1697 else
1698 ans = m_childView->Close(FALSE); // FALSE means don't delete associated window
1699
1700 if (ans)
1701 {
1702 m_childView->Activate(FALSE);
1703 delete m_childView;
1704 m_childView = (wxView *) NULL;
1705 m_childDocument = (wxDocument *) NULL;
1706
1707 this->Destroy();
1708 }
1709 else
1710 event.Veto();
1711 }
1712 else
1713 event.Veto();
1714 }
1715
1716 // ----------------------------------------------------------------------------
1717 // Default parent frame
1718 // ----------------------------------------------------------------------------
1719
1720 BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1721 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
1722 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
1723 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
1724 END_EVENT_TABLE()
1725
1726 wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1727 wxFrame *frame,
1728 wxWindowID id,
1729 const wxString& title,
1730 const wxPoint& pos,
1731 const wxSize& size,
1732 long style,
1733 const wxString& name)
1734 : wxFrame(frame, id, title, pos, size, style, name)
1735 {
1736 m_docManager = manager;
1737 }
1738
1739 void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1740 {
1741 Close();
1742 }
1743
1744 void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1745 {
1746 int n = event.GetId() - wxID_FILE1; // the index in MRU list
1747 wxString filename(m_docManager->GetHistoryFile(n));
1748 if ( !filename.IsEmpty() )
1749 {
1750 // verify that the file exists before doing anything else
1751 if ( wxFile::Exists(filename) )
1752 {
1753 // try to open it
1754 (void)m_docManager->CreateDocument(filename, wxDOC_SILENT);
1755 }
1756 else
1757 {
1758 // remove the bogus filename from the MRU list and notify the user
1759 // about it
1760 m_docManager->RemoveFileFromHistory(n);
1761
1762 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been removed from the most recently used files list."),
1763 filename.c_str());
1764 }
1765 }
1766 }
1767
1768 // Extend event processing to search the view's event table
1769 bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1770 {
1771 // Try the document manager, then do default processing
1772 if (!m_docManager || !m_docManager->ProcessEvent(event))
1773 return wxEvtHandler::ProcessEvent(event);
1774 else
1775 return TRUE;
1776 }
1777
1778 // Define the behaviour for the frame closing
1779 // - must delete all frames except for the main one.
1780 void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
1781 {
1782 if (m_docManager->Clear(!event.CanVeto()))
1783 {
1784 this->Destroy();
1785 }
1786 else
1787 event.Veto();
1788 }
1789
1790 #if wxUSE_PRINTING_ARCHITECTURE
1791
1792 wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
1793 : wxPrintout(title)
1794 {
1795 m_printoutView = view;
1796 }
1797
1798 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1799 {
1800 wxDC *dc = GetDC();
1801
1802 // Get the logical pixels per inch of screen and printer
1803 int ppiScreenX, ppiScreenY;
1804 GetPPIScreen(&ppiScreenX, &ppiScreenY);
1805 int ppiPrinterX, ppiPrinterY;
1806 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
1807
1808 // This scales the DC so that the printout roughly represents the
1809 // the screen scaling. The text point size _should_ be the right size
1810 // but in fact is too small for some reason. This is a detail that will
1811 // need to be addressed at some point but can be fudged for the
1812 // moment.
1813 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1814
1815 // Now we have to check in case our real page size is reduced
1816 // (e.g. because we're drawing to a print preview memory DC)
1817 int pageWidth, pageHeight;
1818 int w, h;
1819 dc->GetSize(&w, &h);
1820 GetPageSizePixels(&pageWidth, &pageHeight);
1821
1822 // If printer pageWidth == current DC width, then this doesn't
1823 // change. But w might be the preview bitmap width, so scale down.
1824 float overallScale = scale * (float)(w/(float)pageWidth);
1825 dc->SetUserScale(overallScale, overallScale);
1826
1827 if (m_printoutView)
1828 {
1829 m_printoutView->OnDraw(dc);
1830 }
1831 return TRUE;
1832 }
1833
1834 bool wxDocPrintout::HasPage(int pageNum)
1835 {
1836 return (pageNum == 1);
1837 }
1838
1839 bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1840 {
1841 if (!wxPrintout::OnBeginDocument(startPage, endPage))
1842 return FALSE;
1843
1844 return TRUE;
1845 }
1846
1847 void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1848 {
1849 *minPage = 1;
1850 *maxPage = 1;
1851 *selPageFrom = 1;
1852 *selPageTo = 1;
1853 }
1854
1855 #endif // wxUSE_PRINTING_ARCHITECTURE
1856
1857 // ----------------------------------------------------------------------------
1858 // File history processor
1859 // ----------------------------------------------------------------------------
1860
1861 wxFileHistory::wxFileHistory(int maxFiles)
1862 {
1863 m_fileMaxFiles = maxFiles;
1864 m_fileHistoryN = 0;
1865 m_fileHistory = new wxChar *[m_fileMaxFiles];
1866 }
1867
1868 wxFileHistory::~wxFileHistory()
1869 {
1870 int i;
1871 for (i = 0; i < m_fileHistoryN; i++)
1872 delete[] m_fileHistory[i];
1873 delete[] m_fileHistory;
1874 }
1875
1876 // File history management
1877 void wxFileHistory::AddFileToHistory(const wxString& file)
1878 {
1879 int i;
1880
1881 // Check we don't already have this file
1882 for (i = 0; i < m_fileHistoryN; i++)
1883 {
1884 if ( m_fileHistory[i] && (file == m_fileHistory[i]) )
1885 {
1886 // we do have it, move it to the top of the history
1887 RemoveFileFromHistory (i);
1888 AddFileToHistory (file);
1889 return;
1890 }
1891 }
1892
1893 // if we already have a full history, delete the one at the end
1894 if ( m_fileMaxFiles == m_fileHistoryN )
1895 {
1896 RemoveFileFromHistory (m_fileHistoryN - 1);
1897 AddFileToHistory (file);
1898 return;
1899 }
1900
1901 // Add to the project file history:
1902 // Move existing files (if any) down so we can insert file at beginning.
1903 if (m_fileHistoryN < m_fileMaxFiles)
1904 {
1905 wxNode* node = m_fileMenus.First();
1906 while (node)
1907 {
1908 wxMenu* menu = (wxMenu*) node->Data();
1909 if (m_fileHistoryN == 0)
1910 menu->AppendSeparator();
1911 menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
1912 node = node->Next();
1913 }
1914 m_fileHistoryN ++;
1915 }
1916 // Shuffle filenames down
1917 for (i = (m_fileHistoryN-1); i > 0; i--)
1918 {
1919 m_fileHistory[i] = m_fileHistory[i-1];
1920 }
1921 m_fileHistory[0] = copystring(file);
1922
1923 // this is the directory of the last opened file
1924 wxString pathCurrent;
1925 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
1926 for (i = 0; i < m_fileHistoryN; i++)
1927 {
1928 if ( m_fileHistory[i] )
1929 {
1930 // if in same directory just show the filename; otherwise the full
1931 // path
1932 wxString pathInMenu, path, filename, ext;
1933 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
1934 if ( path == pathCurrent )
1935 {
1936 pathInMenu = filename;
1937 if ( !ext.empty() )
1938 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
1939 }
1940 else
1941 {
1942 // absolute path; could also set relative path
1943 pathInMenu = m_fileHistory[i];
1944 }
1945
1946 wxString buf;
1947 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
1948 wxNode* node = m_fileMenus.First();
1949 while (node)
1950 {
1951 wxMenu* menu = (wxMenu*) node->Data();
1952 menu->SetLabel(wxID_FILE1 + i, buf);
1953 node = node->Next();
1954 }
1955 }
1956 }
1957 }
1958
1959 void wxFileHistory::RemoveFileFromHistory(int i)
1960 {
1961 wxCHECK_RET( i < m_fileHistoryN,
1962 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
1963
1964 // delete the element from the array (could use memmove() too...)
1965 delete [] m_fileHistory[i];
1966
1967 int j;
1968 for ( j = i; j < m_fileHistoryN - 1; j++ )
1969 {
1970 m_fileHistory[j] = m_fileHistory[j + 1];
1971 }
1972
1973 wxNode* node = m_fileMenus.First();
1974 while ( node )
1975 {
1976 wxMenu* menu = (wxMenu*) node->Data();
1977
1978
1979 // shuffle filenames up
1980 wxString buf;
1981 for ( j = i; j < m_fileHistoryN - 1; j++ )
1982 {
1983 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
1984 menu->SetLabel(wxID_FILE1 + j, buf);
1985 }
1986
1987 node = node->Next();
1988
1989 // delete the last menu item which is unused now
1990 if (menu->FindItem(wxID_FILE1 + m_fileHistoryN - 1))
1991 menu->Delete(wxID_FILE1 + m_fileHistoryN - 1);
1992
1993 // delete the last separator too if no more files are left
1994 if ( m_fileHistoryN == 1 )
1995 {
1996 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
1997 if ( node )
1998 {
1999 wxMenuItem *menuItem = node->GetData();
2000 if ( menuItem->IsSeparator() )
2001 {
2002 menu->Delete(menuItem);
2003 }
2004 //else: should we search backwards for the last separator?
2005 }
2006 //else: menu is empty somehow
2007 }
2008 }
2009
2010 m_fileHistoryN--;
2011 }
2012
2013 wxString wxFileHistory::GetHistoryFile(int i) const
2014 {
2015 wxString s;
2016 if ( i < m_fileHistoryN )
2017 {
2018 s = m_fileHistory[i];
2019 }
2020 else
2021 {
2022 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2023 }
2024
2025 return s;
2026 }
2027
2028 void wxFileHistory::UseMenu(wxMenu *menu)
2029 {
2030 if (!m_fileMenus.Member(menu))
2031 m_fileMenus.Append(menu);
2032 }
2033
2034 void wxFileHistory::RemoveMenu(wxMenu *menu)
2035 {
2036 m_fileMenus.DeleteObject(menu);
2037 }
2038
2039 #if wxUSE_CONFIG
2040 void wxFileHistory::Load(wxConfigBase& config)
2041 {
2042 m_fileHistoryN = 0;
2043 wxString buf;
2044 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
2045 wxString historyFile;
2046 while ((m_fileHistoryN <= m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
2047 {
2048 m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
2049 m_fileHistoryN ++;
2050 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
2051 historyFile = wxT("");
2052 }
2053 AddFilesToMenu();
2054 }
2055
2056 void wxFileHistory::Save(wxConfigBase& config)
2057 {
2058 int i;
2059 for (i = 0; i < m_fileHistoryN; i++)
2060 {
2061 wxString buf;
2062 buf.Printf(wxT("file%d"), i+1);
2063 config.Write(buf, wxString(m_fileHistory[i]));
2064 }
2065 }
2066 #endif // wxUSE_CONFIG
2067
2068 void wxFileHistory::AddFilesToMenu()
2069 {
2070 if (m_fileHistoryN > 0)
2071 {
2072 wxNode* node = m_fileMenus.First();
2073 while (node)
2074 {
2075 wxMenu* menu = (wxMenu*) node->Data();
2076 menu->AppendSeparator();
2077 int i;
2078 for (i = 0; i < m_fileHistoryN; i++)
2079 {
2080 if (m_fileHistory[i])
2081 {
2082 wxString buf;
2083 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2084 menu->Append(wxID_FILE1+i, buf);
2085 }
2086 }
2087 node = node->Next();
2088 }
2089 }
2090 }
2091
2092 void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2093 {
2094 if (m_fileHistoryN > 0)
2095 {
2096 menu->AppendSeparator();
2097 int i;
2098 for (i = 0; i < m_fileHistoryN; i++)
2099 {
2100 if (m_fileHistory[i])
2101 {
2102 wxString buf;
2103 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
2104 menu->Append(wxID_FILE1+i, buf);
2105 }
2106 }
2107 }
2108 }
2109
2110 // ----------------------------------------------------------------------------
2111 // Permits compatibility with existing file formats and functions that
2112 // manipulate files directly
2113 // ----------------------------------------------------------------------------
2114
2115 #if wxUSE_STD_IOSTREAM
2116 bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
2117 {
2118 FILE *fd1;
2119 int ch;
2120
2121 if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL)
2122 return FALSE;
2123
2124 while ((ch = getc (fd1)) != EOF)
2125 stream << (unsigned char)ch;
2126
2127 fclose (fd1);
2128 return TRUE;
2129 }
2130
2131 bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
2132 {
2133 FILE *fd1;
2134 int ch;
2135
2136 if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL)
2137 {
2138 return FALSE;
2139 }
2140
2141 while (!stream.eof())
2142 {
2143 ch = stream.get();
2144 if (!stream.eof())
2145 putc (ch, fd1);
2146 }
2147 fclose (fd1);
2148 return TRUE;
2149 }
2150 #else
2151 bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2152 {
2153 FILE *fd1;
2154 int ch;
2155
2156 if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL)
2157 return FALSE;
2158
2159 while ((ch = getc (fd1)) != EOF)
2160 stream.PutC((char) ch);
2161
2162 fclose (fd1);
2163 return TRUE;
2164 }
2165
2166 bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2167 {
2168 FILE *fd1;
2169 char ch;
2170
2171 if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL)
2172 {
2173 return FALSE;
2174 }
2175
2176 int len = stream.StreamSize();
2177 // TODO: is this the correct test for EOF?
2178 while (stream.TellI() < (len - 1))
2179 {
2180 ch = stream.GetC();
2181 putc (ch, fd1);
2182 }
2183 fclose (fd1);
2184 return TRUE;
2185 }
2186 #endif
2187
2188 #endif // wxUSE_DOC_VIEW_ARCHITECTURE
2189