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