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