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