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