1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Document/view classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "docview.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if USE_DOC_VIEW_ARCHITECTURE
33 #include "wx/dialog.h"
36 #include "wx/filedlg.h"
43 #include "wx/msgdlg.h"
44 #include "wx/choicdlg.h"
45 #include "wx/docview.h"
46 #include "wx/printdlg.h"
47 #include "wx/generic/prntdlgg.h"
48 #include "wx/generic/printps.h"
61 #if !USE_SHARED_LIBRARY
62 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
63 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
64 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
66 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
67 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
68 #if USE_PRINTING_ARCHITECTURE
69 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
71 IMPLEMENT_CLASS(wxCommand
, wxObject
)
72 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
73 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
74 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
78 * Definition of wxDocument
81 wxDocument::wxDocument(wxDocument
*parent
)
83 m_documentModified
=FALSE
;
86 m_documentParent
=parent
;
87 m_documentTemplate
= NULL
;
88 m_documentTypeName
= "";
92 bool wxDocument::DeleteContents(void)
97 wxDocument::~wxDocument(void)
101 if (m_commandProcessor
)
102 delete m_commandProcessor
;
104 GetDocumentManager()->RemoveDocument(this);
106 // Not safe to do here, since it'll
107 // invoke virtual view functions expecting to see
108 // valid derived objects: and by the time we get
109 // here, we've called destructors higher up.
113 bool wxDocument::Close(void)
115 if (OnSaveModified())
116 return OnCloseDocument();
121 bool wxDocument::OnCloseDocument(void)
128 // Note that this implicitly deletes the document when
129 // the last view is deleted.
130 bool wxDocument::DeleteAllViews(void)
132 wxNode
*node
= m_documentViews
.First();
135 wxView
*view
= (wxView
*)node
->Data();
139 wxNode
*next
= node
->Next();
141 delete view
; // Deletes node implicitly
147 wxView
*wxDocument::GetFirstView(void) const
149 if (m_documentViews
.Number() == 0)
151 return (wxView
*)m_documentViews
.First()->Data();
154 wxDocManager
*wxDocument::GetDocumentManager(void) const
156 return m_documentTemplate
->GetDocumentManager();
159 bool wxDocument::OnNewDocument(void)
161 if (!OnSaveModified())
164 if (OnCloseDocument()==FALSE
) return FALSE
;
167 SetDocumentSaved(FALSE
);
170 GetDocumentManager()->MakeDefaultName(name
);
172 SetFilename(name
, TRUE
);
177 bool wxDocument::Save(void)
181 if (!IsModified()) return TRUE
;
182 if (m_documentFile
== "" || !m_savedYet
)
185 ret
= OnSaveDocument(m_documentFile
);
187 SetDocumentSaved(TRUE
);
191 bool wxDocument::SaveAs(void)
193 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
197 char *tmp
= wxFileSelector("Save as", docTemplate
->GetDirectory(), GetFilename(),
198 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
199 0, GetDocumentWindow());
206 SetTitle(wxFileNameFromPath(tmp
));
208 GetDocumentManager()->AddFileToHistory(tmp
);
210 // Notify the views that the filename has changed
211 wxNode
*node
= m_documentViews
.First();
214 wxView
*view
= (wxView
*)node
->Data();
215 view
->OnChangeFilename();
219 return OnSaveDocument(m_documentFile
);
222 bool wxDocument::OnSaveDocument(const wxString
& file
)
228 if (wxTheApp
->GetAppName() != "")
229 msgTitle
= wxTheApp
->GetAppName();
231 msgTitle
= wxString("File error");
233 ofstream
store(file
);
234 if (store
.fail() || store
.bad())
236 (void)wxMessageBox("Sorry, could not open this file for saving.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
237 GetDocumentWindow());
241 if (SaveObject(store
)==FALSE
)
243 (void)wxMessageBox("Sorry, could not save this file.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
244 GetDocumentWindow());
253 bool wxDocument::OnOpenDocument(const wxString
& file
)
255 if (!OnSaveModified())
259 if (wxTheApp
->GetAppName() != "")
260 msgTitle
= wxTheApp
->GetAppName();
262 msgTitle
= wxString("File error");
264 ifstream
store(file
);
265 if (store
.fail() || store
.bad())
267 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
268 GetDocumentWindow());
271 if (LoadObject(store
)==FALSE
)
273 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
274 GetDocumentWindow());
277 SetFilename(file
, TRUE
);
285 istream
& wxDocument::LoadObject(istream
& stream
)
287 // wxObject::LoadObject(stream);
292 ostream
& wxDocument::SaveObject(ostream
& stream
)
294 // wxObject::SaveObject(stream);
299 bool wxDocument::Revert(void)
305 // Get title, or filename if no title, else unnamed
306 bool wxDocument::GetPrintableName(wxString
& buf
) const
308 if (m_documentTitle
!= "")
310 buf
= m_documentTitle
;
313 else if (m_documentFile
!= "")
315 buf
= wxFileNameFromPath(m_documentFile
);
325 wxWindow
*wxDocument::GetDocumentWindow(void) const
327 wxView
*view
= GetFirstView();
329 return view
->GetFrame();
331 return wxTheApp
->GetTopWindow();
334 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
336 return new wxCommandProcessor
;
339 // TRUE if safe to close
340 bool wxDocument::OnSaveModified(void)
346 GetPrintableName(title
);
349 if (wxTheApp
->GetAppName() != "")
350 msgTitle
= wxTheApp
->GetAppName();
352 msgTitle
= wxString("Warning");
354 sprintf(buf
, "Do you want to save changes to document %s?", (const char *)title
);
355 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
356 GetDocumentWindow());
362 else if (res
== wxYES
)
364 else if (res
== wxCANCEL
)
370 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
375 bool wxDocument::AddView(wxView
*view
)
377 if (!m_documentViews
.Member(view
))
379 m_documentViews
.Append(view
);
385 bool wxDocument::RemoveView(wxView
*view
)
387 (void)m_documentViews
.DeleteObject(view
);
392 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
394 if (GetDocumentTemplate()->CreateView(this, flags
))
400 // Called after a view is added or removed.
401 // The default implementation deletes the document if
402 // there are no more views.
403 void wxDocument::OnChangedViewList(void)
405 if (m_documentViews
.Number() == 0)
407 if (OnSaveModified())
414 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
416 wxNode
*node
= m_documentViews
.First();
419 wxView
*view
= (wxView
*)node
->Data();
420 view
->OnUpdate(sender
, hint
);
425 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
427 m_documentFile
= filename
;
430 // Notify the views that the filename has changed
431 wxNode
*node
= m_documentViews
.First();
434 wxView
*view
= (wxView
*)node
->Data();
435 view
->OnChangeFilename();
446 wxView::wxView(wxDocument
*doc
)
454 wxView::~wxView(void)
456 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
457 m_viewDocument
->RemoveView(this);
460 // Extend event processing to search the document's event table
461 bool wxView::ProcessEvent(wxEvent
& event
)
463 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
464 return wxEvtHandler::ProcessEvent(event
);
469 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
473 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
478 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
482 void wxView::OnChangeFilename(void)
484 if (GetFrame() && GetDocument())
487 GetDocument()->GetPrintableName(name
);
489 GetFrame()->SetTitle(name
);
493 void wxView::SetDocument(wxDocument
*doc
)
495 m_viewDocument
= doc
;
500 bool wxView::Close(bool deleteWindow
)
502 if (OnClose(deleteWindow
))
508 void wxView::Activate(bool activate
)
510 if (GetDocumentManager())
512 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
513 GetDocumentManager()->ActivateView(this, activate
);
517 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
519 return GetDocument() ? GetDocument()->Close() : TRUE
;
522 #if USE_PRINTING_ARCHITECTURE
523 wxPrintout
*wxView::OnCreatePrintout(void)
525 return new wxDocPrintout(this);
534 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
535 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
536 const wxString
& docTypeName
, const wxString
& viewTypeName
,
537 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
539 m_documentManager
= manager
;
541 m_description
= descr
;
544 m_fileFilter
= filter
;
546 m_docTypeName
= docTypeName
;
547 m_viewTypeName
= viewTypeName
;
548 m_documentManager
->AssociateTemplate(this);
550 m_docClassInfo
= docClassInfo
;
551 m_viewClassInfo
= viewClassInfo
;
554 wxDocTemplate::~wxDocTemplate(void)
556 m_documentManager
->DisassociateTemplate(this);
559 // Tries to dynamically construct an object of the right
561 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
565 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
566 doc
->SetFilename(path
);
567 doc
->SetDocumentTemplate(this);
568 GetDocumentManager()->AddDocument(doc
);
569 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
571 if (doc
->OnCreate(path
, flags
))
580 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
582 if (!m_viewClassInfo
)
584 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
585 view
->SetDocument(doc
);
586 if (view
->OnCreate(doc
, flags
))
597 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
598 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
599 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
600 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
601 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
602 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
603 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
604 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
605 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
606 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
607 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
608 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
611 wxDocManager::wxDocManager(long flags
, bool initialize
)
613 m_defaultDocumentNameCounter
= 1;
615 m_currentView
= NULL
;
616 m_maxDocsOpen
= 10000;
617 m_fileHistory
= NULL
;
622 wxDocManager::~wxDocManager(void)
626 delete m_fileHistory
;
629 bool wxDocManager::Clear(bool force
)
631 wxNode
*node
= m_docs
.First();
634 wxDocument
*doc
= (wxDocument
*)node
->Data();
635 wxNode
*next
= node
->Next();
637 if (!doc
->Close() && !force
)
640 // Implicitly deletes the document when the last
641 // view is removed (deleted)
642 doc
->DeleteAllViews();
644 // Check document is deleted
645 if (m_docs
.Member(doc
))
648 // This assumes that documents are not connected in
649 // any way, i.e. deleting one document does NOT
653 node
= m_templates
.First();
656 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
657 wxNode
* next
= node
->Next();
664 bool wxDocManager::Initialize(void)
666 m_fileHistory
= OnCreateFileHistory();
670 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
672 return new wxFileHistory
;
675 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
677 wxDocument
*doc
= GetCurrentDocument();
682 doc
->DeleteAllViews();
683 if (m_docs
.Member(doc
))
688 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
690 CreateDocument(wxString(""), wxDOC_NEW
);
693 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
695 CreateDocument(wxString(""), 0);
698 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
700 wxDocument
*doc
= GetCurrentDocument();
706 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
708 wxDocument
*doc
= GetCurrentDocument();
714 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
716 wxDocument
*doc
= GetCurrentDocument();
722 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
724 wxView
*view
= GetCurrentView();
728 wxPrintout
*printout
= view
->OnCreatePrintout();
731 // TODO: trouble about this is that it pulls in the postscript
734 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
736 wxWindowsPrinter printer
;
737 printer
.Print(view
->GetFrame(), printout
, TRUE
);
742 wxPostScriptPrinter printer
;
743 printer
.Print(view
->GetFrame(), printout
, TRUE
);
750 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
752 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
753 wxView
*view
= GetCurrentView();
755 parentWin
= view
->GetFrame();
760 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
762 wxPrintDialog
printerDialog(parentWin
, & data
);
763 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
764 printerDialog
.ShowModal();
769 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
770 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
771 printerDialog
.ShowModal();
775 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
777 wxView
*view
= GetCurrentView();
781 wxPrintout
*printout
= view
->OnCreatePrintout();
784 // Pass two printout objects: for preview, and possible printing.
785 wxPrintPreviewBase
*preview
= NULL
;
787 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
788 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
791 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
793 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), "Print Preview",
794 wxPoint(100, 100), wxSize(600, 650));
795 frame
->Centre(wxBOTH
);
801 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
803 wxDocument
*doc
= GetCurrentDocument();
806 if (doc
->GetCommandProcessor())
807 doc
->GetCommandProcessor()->Undo();
810 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
812 wxDocument
*doc
= GetCurrentDocument();
815 if (doc
->GetCommandProcessor())
816 doc
->GetCommandProcessor()->Redo();
819 wxView
*wxDocManager::GetCurrentView(void) const
822 return m_currentView
;
823 if (m_docs
.Number() == 1)
825 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
826 return doc
->GetFirstView();
831 // Extend event processing to search the view's event table
832 bool wxDocManager::ProcessEvent(wxEvent
& event
)
834 wxView
* view
= GetCurrentView();
837 if (view
->ProcessEvent(event
))
840 return wxEvtHandler::ProcessEvent(event
);
843 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
845 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
848 for (i
= 0; i
< m_templates
.Number(); i
++)
850 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
851 if (temp
->IsVisible())
863 // If we've reached the max number of docs, close the
865 if (GetDocuments().Number() >= m_maxDocsOpen
)
867 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
870 // Implicitly deletes the document when
871 // the last view is deleted
872 doc
->DeleteAllViews();
874 // Check we're really deleted
875 if (m_docs
.Member(doc
))
882 // New document: user chooses a template, unless there's only one.
883 if (flags
& wxDOC_NEW
)
887 wxDocTemplate
*temp
= templates
[0];
889 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
892 newDoc
->SetDocumentName(temp
->GetDocumentName());
893 newDoc
->SetDocumentTemplate(temp
);
894 newDoc
->OnNewDocument();
899 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
903 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
906 newDoc
->SetDocumentName(temp
->GetDocumentName());
907 newDoc
->SetDocumentTemplate(temp
);
908 newDoc
->OnNewDocument();
917 wxDocTemplate
*temp
= NULL
;
923 if (flags
& wxDOC_SILENT
)
924 temp
= FindTemplateForPath(path2
);
926 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
932 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
935 newDoc
->SetDocumentName(temp
->GetDocumentName());
936 newDoc
->SetDocumentTemplate(temp
);
937 if (!newDoc
->OnOpenDocument(path2
))
942 AddFileToHistory(path2
);
950 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
952 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
955 for (i
= 0; i
< m_templates
.Number(); i
++)
957 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
958 if (temp
->IsVisible())
960 if (temp
->GetDocumentName() == doc
->GetDocumentName())
974 wxDocTemplate
*temp
= templates
[0];
976 wxView
*view
= temp
->CreateView(doc
, flags
);
978 view
->SetViewName(temp
->GetViewName());
982 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
986 wxView
*view
= temp
->CreateView(doc
, flags
);
988 view
->SetViewName(temp
->GetViewName());
995 // Not yet implemented
996 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1000 // Not yet implemented
1001 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1006 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1009 return m_currentView
->GetDocument();
1014 // Make a default document name
1015 bool wxDocManager::MakeDefaultName(wxString
& name
)
1018 sprintf(buf
, "unnamed%d", m_defaultDocumentNameCounter
);
1019 m_defaultDocumentNameCounter
++;
1024 // Not yet implemented
1025 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1030 // File history management
1031 void wxDocManager::AddFileToHistory(const wxString
& file
)
1034 m_fileHistory
->AddFileToHistory(file
);
1037 wxString
wxDocManager::GetHistoryFile(int i
) const
1040 return wxString(m_fileHistory
->GetHistoryFile(i
));
1042 return wxString("");
1045 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1048 m_fileHistory
->FileHistoryUseMenu(menu
);
1051 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1054 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1057 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1060 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1063 int wxDocManager::GetNoHistoryFiles(void) const
1066 return m_fileHistory
->GetNoHistoryFiles();
1071 static char *FindExtension(char *path
)
1073 static char ext
[10];
1074 int len
= strlen(path
);
1078 for (i
= (len
-1); i
> 0; i
--)
1084 for (j
= i
+1; j
< len
; j
++)
1085 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1096 // Given a path, try to find a matching template. Won't
1097 // always work, of course.
1098 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1100 char *theExt
= FindExtension((char *)(const char *)path
);
1103 wxDocTemplate
*theTemplate
= NULL
;
1105 if (m_templates
.Number() == 1)
1106 return (wxDocTemplate
*)m_templates
.First()->Data();
1108 // Find the template which this extension corresponds to
1110 for (i
= 0; i
< m_templates
.Number(); i
++)
1112 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1113 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1122 // Prompts user to open a file, using file specs in templates.
1123 // How to implement in wxWindows? Must extend the file selector
1124 // dialog or implement own; OR match the extension to the
1125 // template extension.
1126 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1127 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1129 // We can only have multiple filters in Windows
1131 char *descrBuf
= new char[1000];
1134 for (i
= 0; i
< noTemplates
; i
++)
1136 if (templates
[i
]->IsVisible())
1138 strcat(descrBuf
, templates
[i
]->GetDescription());
1139 strcat(descrBuf
, " (");
1140 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1141 strcat(descrBuf
, ") ");
1142 strcat(descrBuf
, "|");
1143 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1144 strcat(descrBuf
, "|");
1147 int len
= strlen(descrBuf
);
1150 descrBuf
[len
-1] = 0;
1152 char *pathTmp
= wxFileSelector("Select a file", "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1157 char *theExt
= FindExtension((char *)(const char *)path
);
1161 // This is dodgy in that we're selecting the template on the
1162 // basis of the file extension, which may not be a standard
1163 // one. We really want to know exactly which template was
1164 // chosen by using a more advanced file selector.
1165 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1174 // In all other windowing systems, until we have more advanced
1175 // file selectors, we must select the document type (template) first, and
1176 // _then_ pop up the file selector.
1177 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1181 char *pathTmp
= wxFileSelector("Select a file", "", "",
1182 temp
->GetDefaultExtension(),
1183 temp
->GetFileFilter(),
1184 0, wxTheApp
->GetTopWindow());
1196 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1199 char **strings
= new char *[noTemplates
];
1200 char **data
= new char *[noTemplates
];
1203 for (i
= 0; i
< noTemplates
; i
++)
1205 if (templates
[i
]->IsVisible())
1207 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1208 data
[n
] = (char *)templates
[i
];
1220 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1226 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document template", "Templates", n
,
1233 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1236 char **strings
= new char *[noTemplates
];
1237 char **data
= new char *[noTemplates
];
1240 for (i
= 0; i
< noTemplates
; i
++)
1242 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1244 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1245 data
[n
] = (char *)templates
[i
];
1249 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document view", "Views", n
,
1256 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1258 if (!m_templates
.Member(temp
))
1259 m_templates
.Append(temp
);
1262 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1264 m_templates
.DeleteObject(temp
);
1267 // Add and remove a document from the manager's list
1268 void wxDocManager::AddDocument(wxDocument
*doc
)
1270 if (!m_docs
.Member(doc
))
1274 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1276 m_docs
.DeleteObject(doc
);
1279 // Views or windows should inform the document manager
1280 // when a view is going in or out of focus
1281 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1283 // If we're deactiving, and if we're not actually deleting the view, then
1284 // don't reset the current view because we may be going to
1285 // a window without a view.
1286 // WHAT DID I MEAN BY THAT EXACTLY?
1290 if (m_currentView == view)
1291 m_currentView = NULL;
1297 m_currentView
= view
;
1299 m_currentView
= NULL
;
1304 * Default document child frame
1307 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1308 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1311 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1312 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1313 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1315 m_childDocument
= doc
;
1318 view
->SetFrame(this);
1321 wxDocChildFrame::~wxDocChildFrame(void)
1325 // Extend event processing to search the view's event table
1326 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1329 m_childView
->Activate(TRUE
);
1331 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1333 // Only hand up to the parent if it's a menu command
1334 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1335 return wxEvtHandler::ProcessEvent(event
);
1343 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1345 wxFrame::OnActivate(event
);
1348 m_childView
->Activate(event
.GetActive());
1351 bool wxDocChildFrame::OnClose(void)
1353 // Close view but don't delete the frame while doing so!
1354 // ...since it will be deleted by wxWindows if we return TRUE.
1357 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1360 m_childView
->Activate(FALSE
);
1363 m_childDocument
= NULL
;
1372 * Default parent frame
1375 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1376 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1377 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE9
, wxDocParentFrame::OnMRUFile
)
1380 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1381 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1382 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1384 m_docManager
= manager
;
1387 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1392 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1394 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1396 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1399 // Extend event processing to search the view's event table
1400 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1402 // Try the document manager, then do default processing
1403 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1404 return wxEvtHandler::ProcessEvent(event
);
1409 // Define the behaviour for the frame closing
1410 // - must delete all frames except for the main one.
1411 bool wxDocParentFrame::OnClose(void)
1413 return m_docManager
->Clear(FALSE
);
1416 #if USE_PRINTING_ARCHITECTURE
1418 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1419 wxPrintout(WXSTRINGCAST title
)
1421 m_printoutView
= view
;
1424 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1428 // Get the logical pixels per inch of screen and printer
1429 int ppiScreenX
, ppiScreenY
;
1430 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1431 int ppiPrinterX
, ppiPrinterY
;
1432 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1434 // This scales the DC so that the printout roughly represents the
1435 // the screen scaling. The text point size _should_ be the right size
1436 // but in fact is too small for some reason. This is a detail that will
1437 // need to be addressed at some point but can be fudged for the
1439 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1441 // Now we have to check in case our real page size is reduced
1442 // (e.g. because we're drawing to a print preview memory DC)
1443 int pageWidth
, pageHeight
;
1445 dc
->GetSize(&w
, &h
);
1446 GetPageSizePixels(&pageWidth
, &pageHeight
);
1448 // If printer pageWidth == current DC width, then this doesn't
1449 // change. But w might be the preview bitmap width, so scale down.
1450 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1451 dc
->SetUserScale(overallScale
, overallScale
);
1455 m_printoutView
->OnDraw(dc
);
1460 bool wxDocPrintout::HasPage(int pageNum
)
1462 return (pageNum
== 1);
1465 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1467 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1473 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1484 * Command processing framework
1487 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1489 m_canUndo
= canUndoIt
;
1490 m_commandName
= name
;
1493 wxCommand::~wxCommand(void)
1497 // Command processor
1498 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1500 m_maxNoCommands
= maxCommands
;
1501 m_currentCommand
= NULL
;
1502 m_commandEditMenu
= NULL
;
1505 wxCommandProcessor::~wxCommandProcessor(void)
1510 // Pass a command to the processor. The processor calls Do();
1511 // if successful, is appended to the command history unless
1512 // storeIt is FALSE.
1513 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1515 bool success
= command
->Do();
1516 if (success
&& storeIt
)
1518 if (m_commands
.Number() == m_maxNoCommands
)
1520 wxNode
*firstNode
= m_commands
.First();
1521 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1522 delete firstCommand
;
1526 // Correct a bug: we must chop off the current 'branch'
1527 // so that we're at the end of the command list.
1528 if (!m_currentCommand
)
1532 wxNode
*node
= m_currentCommand
->Next();
1535 wxNode
*next
= node
->Next();
1536 delete (wxCommand
*)node
->Data();
1542 m_commands
.Append(command
);
1543 m_currentCommand
= m_commands
.Last();
1549 bool wxCommandProcessor::Undo(void)
1551 if (m_currentCommand
)
1553 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1554 if (command
->CanUndo())
1556 bool success
= command
->Undo();
1559 m_currentCommand
= m_currentCommand
->Previous();
1568 bool wxCommandProcessor::Redo(void)
1570 wxCommand
*redoCommand
= NULL
;
1571 wxNode
*redoNode
= NULL
;
1572 if (m_currentCommand
&& m_currentCommand
->Next())
1574 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1575 redoNode
= m_currentCommand
->Next();
1579 if (m_commands
.Number() > 0)
1581 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1582 redoNode
= m_commands
.First();
1588 bool success
= redoCommand
->Do();
1591 m_currentCommand
= redoNode
;
1599 bool wxCommandProcessor::CanUndo(void)
1601 if (m_currentCommand
)
1602 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1606 void wxCommandProcessor::Initialize(void)
1608 m_currentCommand
= m_commands
.Last();
1612 void wxCommandProcessor::SetMenuStrings(void)
1614 if (m_commandEditMenu
)
1617 if (m_currentCommand
)
1619 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1620 wxString
commandName(command
->GetName());
1621 if (commandName
== "") commandName
= "Unnamed command";
1622 bool canUndo
= command
->CanUndo();
1624 buf
= wxString("&Undo ") + commandName
;
1626 buf
= wxString("Can't &Undo ") + commandName
;
1628 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1629 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1631 // We can redo, if we're not at the end of the history.
1632 if (m_currentCommand
->Next())
1634 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1635 wxString
redoCommandName(redoCommand
->GetName());
1636 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1637 buf
= wxString("&Redo ") + redoCommandName
;
1638 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1639 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1643 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1644 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1649 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1650 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1652 if (m_commands
.Number() == 0)
1654 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1655 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1659 // currentCommand is NULL but there are commands: this means that
1660 // we've undone to the start of the list, but can redo the first.
1661 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1662 wxString
redoCommandName(redoCommand
->GetName());
1663 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1664 buf
= wxString("&Redo ") + redoCommandName
;
1665 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1666 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1672 void wxCommandProcessor::ClearCommands(void)
1674 wxNode
*node
= m_commands
.First();
1677 wxCommand
*command
= (wxCommand
*)node
->Data();
1680 node
= m_commands
.First();
1682 m_currentCommand
= NULL
;
1687 * File history processor
1690 wxFileHistory::wxFileHistory(int maxFiles
)
1692 m_fileMaxFiles
= maxFiles
;
1695 m_fileHistory
= new char *[m_fileMaxFiles
];
1698 wxFileHistory::~wxFileHistory(void)
1701 for (i
= 0; i
< m_fileHistoryN
; i
++)
1702 delete[] m_fileHistory
[i
];
1703 delete[] m_fileHistory
;
1706 // File history management
1707 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1714 // Check we don't already have this file
1715 for (i
= 0; i
< m_fileHistoryN
; i
++)
1717 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1721 // Add to the project file history:
1722 // Move existing files (if any) down so we can insert file at beginning.
1724 // First delete filename that has popped off the end of the array (if any)
1725 if (m_fileHistoryN
== m_fileMaxFiles
)
1727 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1728 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1730 if (m_fileHistoryN
< m_fileMaxFiles
)
1732 if (m_fileHistoryN
== 0)
1733 m_fileMenu
->AppendSeparator();
1734 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1737 // Shuffle filenames down
1738 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1740 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1742 m_fileHistory
[0] = copystring(file
);
1744 for (i
= 0; i
< m_fileHistoryN
; i
++)
1745 if (m_fileHistory
[i
])
1748 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1749 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1753 wxString
wxFileHistory::GetHistoryFile(int i
) const
1755 if (i
< m_fileHistoryN
)
1756 return wxString(m_fileHistory
[i
]);
1758 return wxString("");
1761 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1766 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1771 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1772 char *historyFile
= NULL
;
1773 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1775 // wxGetResource allocates memory so this is o.k.
1776 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1778 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1784 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1789 for (i
= 0; i
< m_fileHistoryN
; i
++)
1791 sprintf(buf
, "file%d", i
+1);
1792 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1802 wxPrintInfo::wxPrintInfo(void)
1807 wxPrintInfo::~wxPrintInfo(void)
1813 * Permits compatibility with existing file formats and functions
1814 * that manipulate files directly
1817 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1822 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1825 while ((ch
= getc (fd1
)) != EOF
)
1826 stream
<< (unsigned char)ch
;
1832 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1837 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1842 while (!stream
.eof())
1853 // End USE_DOC_VIEW_ARCHITECTURE