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