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