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