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"
39 #include "wx/msgdlg.h"
40 #include "wx/choicdlg.h"
41 #include "wx/docview.h"
42 #include "wx/printdlg.h"
43 #include "wx/generic/prntdlgg.h"
44 #include "wx/generic/printps.h"
57 #if !USE_SHARED_LIBRARY
58 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
59 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
60 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
61 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
62 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
63 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
64 #if USE_PRINTING_ARCHITECTURE
65 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
67 IMPLEMENT_CLASS(wxCommand
, wxObject
)
68 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
69 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
70 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
74 * Definition of wxDocument
77 wxDocument::wxDocument(wxDocument
*parent
)
79 m_documentModified
=FALSE
;
82 m_documentParent
=parent
;
83 m_documentTemplate
= NULL
;
84 m_documentTypeName
= "";
88 bool wxDocument::DeleteContents(void)
93 wxDocument::~wxDocument(void)
97 if (m_commandProcessor
)
98 delete m_commandProcessor
;
100 GetDocumentManager()->RemoveDocument(this);
102 // Not safe to do here, since it'll
103 // invoke virtual view functions expecting to see
104 // valid derived objects: and by the time we get
105 // here, we've called destructors higher up.
109 bool wxDocument::Close(void)
111 if (OnSaveModified())
112 return OnCloseDocument();
117 bool wxDocument::OnCloseDocument(void)
124 // Note that this implicitly deletes the document when
125 // the last view is deleted.
126 bool wxDocument::DeleteAllViews(void)
128 wxNode
*node
= m_documentViews
.First();
131 wxView
*view
= (wxView
*)node
->Data();
135 wxNode
*next
= node
->Next();
137 delete view
; // Deletes node implicitly
143 wxView
*wxDocument::GetFirstView(void) const
145 if (m_documentViews
.Number() == 0)
147 return (wxView
*)m_documentViews
.First()->Data();
150 wxDocManager
*wxDocument::GetDocumentManager(void) const
152 return m_documentTemplate
->GetDocumentManager();
155 bool wxDocument::OnNewDocument(void)
157 if (!OnSaveModified())
160 if (OnCloseDocument()==FALSE
) return FALSE
;
163 SetDocumentSaved(FALSE
);
166 GetDocumentManager()->MakeDefaultName(name
);
168 SetFilename(name
, TRUE
);
173 bool wxDocument::Save(void)
177 if (!IsModified()) return TRUE
;
178 if (m_documentFile
== "" || !m_savedYet
)
181 ret
= OnSaveDocument(m_documentFile
);
183 SetDocumentSaved(TRUE
);
187 bool wxDocument::SaveAs(void)
189 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
193 char *tmp
= wxFileSelector("Save as", docTemplate
->GetDirectory(), GetFilename(),
194 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
195 0, GetDocumentWindow());
202 SetTitle(wxFileNameFromPath(tmp
));
204 GetDocumentManager()->AddFileToHistory(tmp
);
206 // Notify the views that the filename has changed
207 wxNode
*node
= m_documentViews
.First();
210 wxView
*view
= (wxView
*)node
->Data();
211 view
->OnChangeFilename();
215 return OnSaveDocument(m_documentFile
);
218 bool wxDocument::OnSaveDocument(const wxString
& file
)
224 if (wxTheApp
->GetAppName() != "")
225 msgTitle
= wxTheApp
->GetAppName();
227 msgTitle
= wxString("File error");
229 ofstream
store(file
);
230 if (store
.fail() || store
.bad())
232 (void)wxMessageBox("Sorry, could not open this file for saving.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
233 GetDocumentWindow());
237 if (SaveObject(store
)==FALSE
)
239 (void)wxMessageBox("Sorry, could not save this file.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
240 GetDocumentWindow());
249 bool wxDocument::OnOpenDocument(const wxString
& file
)
251 if (!OnSaveModified())
255 if (wxTheApp
->GetAppName() != "")
256 msgTitle
= wxTheApp
->GetAppName();
258 msgTitle
= wxString("File error");
260 ifstream
store(file
);
261 if (store
.fail() || store
.bad())
263 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
264 GetDocumentWindow());
267 if (LoadObject(store
)==FALSE
)
269 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
270 GetDocumentWindow());
273 SetFilename(file
, TRUE
);
281 istream
& wxDocument::LoadObject(istream
& stream
)
283 // wxObject::LoadObject(stream);
288 ostream
& wxDocument::SaveObject(ostream
& stream
)
290 // wxObject::SaveObject(stream);
295 bool wxDocument::Revert(void)
301 // Get title, or filename if no title, else unnamed
302 bool wxDocument::GetPrintableName(wxString
& buf
) const
304 if (m_documentTitle
!= "")
306 buf
= m_documentTitle
;
309 else if (m_documentFile
!= "")
311 buf
= wxFileNameFromPath(m_documentFile
);
321 wxWindow
*wxDocument::GetDocumentWindow(void) const
323 wxView
*view
= GetFirstView();
325 return view
->GetFrame();
327 return wxTheApp
->GetTopWindow();
330 wxCommandProcessor
*wxDocument::OnCreateCommandProcessor(void)
332 return new wxCommandProcessor
;
335 // TRUE if safe to close
336 bool wxDocument::OnSaveModified(void)
342 GetPrintableName(title
);
345 if (wxTheApp
->GetAppName() != "")
346 msgTitle
= wxTheApp
->GetAppName();
348 msgTitle
= wxString("Warning");
350 sprintf(buf
, "Do you want to save changes to document %s?", (const char *)title
);
351 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
352 GetDocumentWindow());
358 else if (res
== wxYES
)
360 else if (res
== wxCANCEL
)
366 bool wxDocument::Draw(wxDC
& WXUNUSED(context
))
371 bool wxDocument::AddView(wxView
*view
)
373 if (!m_documentViews
.Member(view
))
375 m_documentViews
.Append(view
);
381 bool wxDocument::RemoveView(wxView
*view
)
383 (void)m_documentViews
.DeleteObject(view
);
388 bool wxDocument::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
390 if (GetDocumentTemplate()->CreateView(this, flags
))
396 // Called after a view is added or removed.
397 // The default implementation deletes the document if
398 // there are no more views.
399 void wxDocument::OnChangedViewList(void)
401 if (m_documentViews
.Number() == 0)
403 if (OnSaveModified())
410 void wxDocument::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
412 wxNode
*node
= m_documentViews
.First();
415 wxView
*view
= (wxView
*)node
->Data();
416 view
->OnUpdate(sender
, hint
);
421 void wxDocument::SetFilename(const wxString
& filename
, bool notifyViews
)
423 m_documentFile
= filename
;
426 // Notify the views that the filename has changed
427 wxNode
*node
= m_documentViews
.First();
430 wxView
*view
= (wxView
*)node
->Data();
431 view
->OnChangeFilename();
442 wxView::wxView(wxDocument
*doc
)
450 wxView::~wxView(void)
452 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
453 m_viewDocument
->RemoveView(this);
456 // Extend event processing to search the document's event table
457 bool wxView::ProcessEvent(wxEvent
& event
)
459 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
460 return wxEvtHandler::ProcessEvent(event
);
465 void wxView::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
469 void wxView::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
474 void wxView::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
478 void wxView::OnChangeFilename(void)
480 if (GetFrame() && GetDocument())
483 GetDocument()->GetPrintableName(name
);
485 // If the frame is an MDI child, just set the title
487 // Otherwise, append the document name to the name of the application
489 if (GetFrame()->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
494 GetFrame()->SetTitle(name
);
498 if (wxTheApp
->GetAppName() != "")
501 sprintf(buf
, "%s - %s", (const char *)wxTheApp
->GetAppName(), (const char *)name
);
502 GetFrame()->SetTitle(buf
);
505 GetFrame()->SetTitle(name
);
510 void wxView::SetDocument(wxDocument
*doc
)
512 m_viewDocument
= doc
;
517 bool wxView::Close(bool deleteWindow
)
519 if (OnClose(deleteWindow
))
525 void wxView::Activate(bool activate
)
527 if (GetDocumentManager())
529 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
530 GetDocumentManager()->ActivateView(this, activate
);
534 bool wxView::OnClose(bool WXUNUSED(deleteWindow
))
536 return GetDocument() ? GetDocument()->Close() : TRUE
;
539 #if USE_PRINTING_ARCHITECTURE
540 wxPrintout
*wxView::OnCreatePrintout(void)
542 return new wxDocPrintout(this);
551 wxDocTemplate::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
552 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
553 const wxString
& docTypeName
, const wxString
& viewTypeName
,
554 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
556 m_documentManager
= manager
;
558 m_description
= descr
;
561 m_fileFilter
= filter
;
563 m_docTypeName
= docTypeName
;
564 m_viewTypeName
= viewTypeName
;
565 m_documentManager
->AssociateTemplate(this);
567 m_docClassInfo
= docClassInfo
;
568 m_viewClassInfo
= viewClassInfo
;
571 wxDocTemplate::~wxDocTemplate(void)
573 m_documentManager
->DisassociateTemplate(this);
576 // Tries to dynamically construct an object of the right
578 wxDocument
*wxDocTemplate::CreateDocument(const wxString
& path
, long flags
)
582 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
583 doc
->SetFilename(path
);
584 doc
->SetDocumentTemplate(this);
585 GetDocumentManager()->AddDocument(doc
);
586 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
588 if (doc
->OnCreate(path
, flags
))
597 wxView
*wxDocTemplate::CreateView(wxDocument
*doc
, long flags
)
599 if (!m_viewClassInfo
)
601 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
602 view
->SetDocument(doc
);
603 if (view
->OnCreate(doc
, flags
))
614 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
615 EVT_MENU(wxID_OPEN
, wxDocManager::OnFileOpen
)
616 EVT_MENU(wxID_CLOSE
, wxDocManager::OnFileClose
)
617 EVT_MENU(wxID_REVERT
, wxDocManager::OnFileRevert
)
618 EVT_MENU(wxID_NEW
, wxDocManager::OnFileNew
)
619 EVT_MENU(wxID_SAVE
, wxDocManager::OnFileSave
)
620 EVT_MENU(wxID_SAVEAS
, wxDocManager::OnFileSaveAs
)
621 EVT_MENU(wxID_UNDO
, wxDocManager::OnUndo
)
622 EVT_MENU(wxID_REDO
, wxDocManager::OnRedo
)
623 EVT_MENU(wxID_PRINT
, wxDocManager::OnPrint
)
624 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager::OnPrintSetup
)
625 EVT_MENU(wxID_PREVIEW
, wxDocManager::OnPreview
)
628 wxDocManager::wxDocManager(long flags
, bool initialize
)
630 m_defaultDocumentNameCounter
= 1;
632 m_currentView
= NULL
;
633 m_maxDocsOpen
= 10000;
634 m_fileHistory
= NULL
;
639 wxDocManager::~wxDocManager(void)
643 delete m_fileHistory
;
646 bool wxDocManager::Clear(bool force
)
648 wxNode
*node
= m_docs
.First();
651 wxDocument
*doc
= (wxDocument
*)node
->Data();
652 wxNode
*next
= node
->Next();
654 if (!doc
->Close() && !force
)
657 // Implicitly deletes the document when the last
658 // view is removed (deleted)
659 doc
->DeleteAllViews();
661 // Check document is deleted
662 if (m_docs
.Member(doc
))
665 // This assumes that documents are not connected in
666 // any way, i.e. deleting one document does NOT
670 node
= m_templates
.First();
673 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
674 wxNode
* next
= node
->Next();
681 bool wxDocManager::Initialize(void)
683 m_fileHistory
= OnCreateFileHistory();
687 wxFileHistory
*wxDocManager::OnCreateFileHistory(void)
689 return new wxFileHistory
;
692 void wxDocManager::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
694 wxDocument
*doc
= GetCurrentDocument();
699 doc
->DeleteAllViews();
700 if (m_docs
.Member(doc
))
705 void wxDocManager::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
707 CreateDocument(wxString(""), wxDOC_NEW
);
710 void wxDocManager::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
712 CreateDocument(wxString(""), 0);
715 void wxDocManager::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
717 wxDocument
*doc
= GetCurrentDocument();
723 void wxDocManager::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
725 wxDocument
*doc
= GetCurrentDocument();
731 void wxDocManager::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
733 wxDocument
*doc
= GetCurrentDocument();
739 void wxDocManager::OnPrint(wxCommandEvent
& WXUNUSED(event
))
741 wxView
*view
= GetCurrentView();
745 wxPrintout
*printout
= view
->OnCreatePrintout();
748 // TODO: trouble about this is that it pulls in the postscript
751 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
753 wxWindowsPrinter printer
;
754 printer
.Print(view
->GetFrame(), printout
, TRUE
);
759 wxPostScriptPrinter printer
;
760 printer
.Print(view
->GetFrame(), printout
, TRUE
);
767 void wxDocManager::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
769 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
770 wxView
*view
= GetCurrentView();
772 parentWin
= view
->GetFrame();
777 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
779 wxPrintDialog
printerDialog(parentWin
, & data
);
780 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
781 printerDialog
.ShowModal();
786 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
787 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
788 printerDialog
.ShowModal();
792 void wxDocManager::OnPreview(wxCommandEvent
& WXUNUSED(event
))
794 wxView
*view
= GetCurrentView();
798 wxPrintout
*printout
= view
->OnCreatePrintout();
801 // Pass two printout objects: for preview, and possible printing.
802 wxPrintPreviewBase
*preview
= NULL
;
804 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
805 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
808 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
810 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), "Print Preview",
811 wxPoint(100, 100), wxSize(600, 650));
812 frame
->Centre(wxBOTH
);
818 void wxDocManager::OnUndo(wxCommandEvent
& WXUNUSED(event
))
820 wxDocument
*doc
= GetCurrentDocument();
823 if (doc
->GetCommandProcessor())
824 doc
->GetCommandProcessor()->Undo();
827 void wxDocManager::OnRedo(wxCommandEvent
& WXUNUSED(event
))
829 wxDocument
*doc
= GetCurrentDocument();
832 if (doc
->GetCommandProcessor())
833 doc
->GetCommandProcessor()->Redo();
836 wxView
*wxDocManager::GetCurrentView(void) const
839 return m_currentView
;
840 if (m_docs
.Number() == 1)
842 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
843 return doc
->GetFirstView();
848 // Extend event processing to search the view's event table
849 bool wxDocManager::ProcessEvent(wxEvent
& event
)
851 wxView
* view
= GetCurrentView();
854 if (view
->ProcessEvent(event
))
857 return wxEvtHandler::ProcessEvent(event
);
860 wxDocument
*wxDocManager::CreateDocument(const wxString
& path
, long flags
)
862 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
865 for (i
= 0; i
< m_templates
.Number(); i
++)
867 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
868 if (temp
->IsVisible())
880 // If we've reached the max number of docs, close the
882 if (GetDocuments().Number() >= m_maxDocsOpen
)
884 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
887 // Implicitly deletes the document when
888 // the last view is deleted
889 doc
->DeleteAllViews();
891 // Check we're really deleted
892 if (m_docs
.Member(doc
))
899 // New document: user chooses a template, unless there's only one.
900 if (flags
& wxDOC_NEW
)
904 wxDocTemplate
*temp
= templates
[0];
906 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
909 newDoc
->SetDocumentName(temp
->GetDocumentName());
910 newDoc
->SetDocumentTemplate(temp
);
911 newDoc
->OnNewDocument();
916 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
920 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
923 newDoc
->SetDocumentName(temp
->GetDocumentName());
924 newDoc
->SetDocumentTemplate(temp
);
925 newDoc
->OnNewDocument();
934 wxDocTemplate
*temp
= NULL
;
940 if (flags
& wxDOC_SILENT
)
941 temp
= FindTemplateForPath(path2
);
943 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
949 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
952 newDoc
->SetDocumentName(temp
->GetDocumentName());
953 newDoc
->SetDocumentTemplate(temp
);
954 if (!newDoc
->OnOpenDocument(path2
))
959 AddFileToHistory(path2
);
967 wxView
*wxDocManager::CreateView(wxDocument
*doc
, long flags
)
969 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
972 for (i
= 0; i
< m_templates
.Number(); i
++)
974 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
975 if (temp
->IsVisible())
977 if (temp
->GetDocumentName() == doc
->GetDocumentName())
991 wxDocTemplate
*temp
= templates
[0];
993 wxView
*view
= temp
->CreateView(doc
, flags
);
995 view
->SetViewName(temp
->GetViewName());
999 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1003 wxView
*view
= temp
->CreateView(doc
, flags
);
1005 view
->SetViewName(temp
->GetViewName());
1012 // Not yet implemented
1013 void wxDocManager::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1017 // Not yet implemented
1018 bool wxDocManager::FlushDoc(wxDocument
*WXUNUSED(doc
))
1023 wxDocument
*wxDocManager::GetCurrentDocument(void) const
1026 return m_currentView
->GetDocument();
1031 // Make a default document name
1032 bool wxDocManager::MakeDefaultName(wxString
& name
)
1035 sprintf(buf
, "unnamed%d", m_defaultDocumentNameCounter
);
1036 m_defaultDocumentNameCounter
++;
1041 // Not yet implemented
1042 wxDocTemplate
*wxDocManager::MatchTemplate(const wxString
& WXUNUSED(path
))
1047 // File history management
1048 void wxDocManager::AddFileToHistory(const wxString
& file
)
1051 m_fileHistory
->AddFileToHistory(file
);
1054 wxString
wxDocManager::GetHistoryFile(int i
) const
1057 return wxString(m_fileHistory
->GetHistoryFile(i
));
1059 return wxString("");
1062 void wxDocManager::FileHistoryUseMenu(wxMenu
*menu
)
1065 m_fileHistory
->FileHistoryUseMenu(menu
);
1068 void wxDocManager::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1071 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1074 void wxDocManager::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1077 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1080 int wxDocManager::GetNoHistoryFiles(void) const
1083 return m_fileHistory
->GetNoHistoryFiles();
1088 static char *FindExtension(char *path
)
1090 static char ext
[10];
1091 int len
= strlen(path
);
1095 for (i
= (len
-1); i
> 0; i
--)
1101 for (j
= i
+1; j
< len
; j
++)
1102 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1113 // Given a path, try to find a matching template. Won't
1114 // always work, of course.
1115 wxDocTemplate
*wxDocManager::FindTemplateForPath(const wxString
& path
)
1117 char *theExt
= FindExtension((char *)(const char *)path
);
1120 wxDocTemplate
*theTemplate
= NULL
;
1122 if (m_templates
.Number() == 1)
1123 return (wxDocTemplate
*)m_templates
.First()->Data();
1125 // Find the template which this extension corresponds to
1127 for (i
= 0; i
< m_templates
.Number(); i
++)
1129 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1130 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1139 // Prompts user to open a file, using file specs in templates.
1140 // How to implement in wxWindows? Must extend the file selector
1141 // dialog or implement own; OR match the extension to the
1142 // template extension.
1143 wxDocTemplate
*wxDocManager::SelectDocumentPath(wxDocTemplate
**templates
,
1144 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1146 // We can only have multiple filters in Windows
1148 char *descrBuf
= new char[1000];
1151 for (i
= 0; i
< noTemplates
; i
++)
1153 if (templates
[i
]->IsVisible())
1155 strcat(descrBuf
, templates
[i
]->GetDescription());
1156 strcat(descrBuf
, " (");
1157 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1158 strcat(descrBuf
, ") ");
1159 strcat(descrBuf
, "|");
1160 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1161 strcat(descrBuf
, "|");
1164 int len
= strlen(descrBuf
);
1167 descrBuf
[len
-1] = 0;
1169 char *pathTmp
= wxFileSelector("Select a file", "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1174 char *theExt
= FindExtension((char *)(const char *)path
);
1178 // This is dodgy in that we're selecting the template on the
1179 // basis of the file extension, which may not be a standard
1180 // one. We really want to know exactly which template was
1181 // chosen by using a more advanced file selector.
1182 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1191 // In all other windowing systems, until we have more advanced
1192 // file selectors, we must select the document type (template) first, and
1193 // _then_ pop up the file selector.
1194 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1198 char *pathTmp
= wxFileSelector("Select a file", "", "",
1199 temp
->GetDefaultExtension(),
1200 temp
->GetFileFilter(),
1201 0, wxTheApp
->GetTopWindow());
1213 wxDocTemplate
*wxDocManager::SelectDocumentType(wxDocTemplate
**templates
,
1216 char **strings
= new char *[noTemplates
];
1217 char **data
= new char *[noTemplates
];
1220 for (i
= 0; i
< noTemplates
; i
++)
1222 if (templates
[i
]->IsVisible())
1224 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1225 data
[n
] = (char *)templates
[i
];
1237 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1243 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document template", "Templates", n
,
1250 wxDocTemplate
*wxDocManager::SelectViewType(wxDocTemplate
**templates
,
1253 char **strings
= new char *[noTemplates
];
1254 char **data
= new char *[noTemplates
];
1257 for (i
= 0; i
< noTemplates
; i
++)
1259 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1261 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1262 data
[n
] = (char *)templates
[i
];
1266 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document view", "Views", n
,
1273 void wxDocManager::AssociateTemplate(wxDocTemplate
*temp
)
1275 if (!m_templates
.Member(temp
))
1276 m_templates
.Append(temp
);
1279 void wxDocManager::DisassociateTemplate(wxDocTemplate
*temp
)
1281 m_templates
.DeleteObject(temp
);
1284 // Add and remove a document from the manager's list
1285 void wxDocManager::AddDocument(wxDocument
*doc
)
1287 if (!m_docs
.Member(doc
))
1291 void wxDocManager::RemoveDocument(wxDocument
*doc
)
1293 m_docs
.DeleteObject(doc
);
1296 // Views or windows should inform the document manager
1297 // when a view is going in or out of focus
1298 void wxDocManager::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1300 // If we're deactiving, and if we're not actually deleting the view, then
1301 // don't reset the current view because we may be going to
1302 // a window without a view.
1303 // WHAT DID I MEAN BY THAT EXACTLY?
1307 if (m_currentView == view)
1308 m_currentView = NULL;
1314 m_currentView
= view
;
1316 m_currentView
= NULL
;
1321 * Default document child frame
1324 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1325 EVT_ACTIVATE(wxDocChildFrame::OnActivate
)
1328 wxDocChildFrame::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1329 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1330 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1332 m_childDocument
= doc
;
1335 view
->SetFrame(this);
1338 wxDocChildFrame::~wxDocChildFrame(void)
1342 // Extend event processing to search the view's event table
1343 bool wxDocChildFrame::ProcessEvent(wxEvent
& event
)
1346 m_childView
->Activate(TRUE
);
1348 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1350 // Only hand up to the parent if it's a menu command
1351 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1352 return wxEvtHandler::ProcessEvent(event
);
1360 void wxDocChildFrame::OnActivate(wxActivateEvent
& event
)
1362 wxFrame::OnActivate(event
);
1365 m_childView
->Activate(event
.GetActive());
1368 bool wxDocChildFrame::OnClose(void)
1370 // Close view but don't delete the frame while doing so!
1371 // ...since it will be deleted by wxWindows if we return TRUE.
1374 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1377 m_childView
->Activate(FALSE
);
1380 m_childDocument
= NULL
;
1389 * Default parent frame
1392 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1393 EVT_MENU(wxID_EXIT
, wxDocParentFrame::OnExit
)
1394 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE2
, wxDocParentFrame::OnMRUFile
)
1397 wxDocParentFrame::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1398 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1399 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1401 m_docManager
= manager
;
1404 void wxDocParentFrame::OnExit(wxCommandEvent
& WXUNUSED(event
))
1409 void wxDocParentFrame::OnMRUFile(wxCommandEvent
& event
)
1411 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1413 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1416 // Extend event processing to search the view's event table
1417 bool wxDocParentFrame::ProcessEvent(wxEvent
& event
)
1419 // Try the document manager, then do default processing
1420 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1421 return wxEvtHandler::ProcessEvent(event
);
1426 // Define the behaviour for the frame closing
1427 // - must delete all frames except for the main one.
1428 bool wxDocParentFrame::OnClose(void)
1430 return m_docManager
->Clear(FALSE
);
1433 #if USE_PRINTING_ARCHITECTURE
1435 wxDocPrintout::wxDocPrintout(wxView
*view
, const wxString
& title
):
1436 wxPrintout(WXSTRINGCAST title
)
1438 m_printoutView
= view
;
1441 bool wxDocPrintout::OnPrintPage(int WXUNUSED(page
))
1445 // Get the logical pixels per inch of screen and printer
1446 int ppiScreenX
, ppiScreenY
;
1447 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1448 int ppiPrinterX
, ppiPrinterY
;
1449 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1451 // This scales the DC so that the printout roughly represents the
1452 // the screen scaling. The text point size _should_ be the right size
1453 // but in fact is too small for some reason. This is a detail that will
1454 // need to be addressed at some point but can be fudged for the
1456 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1458 // Now we have to check in case our real page size is reduced
1459 // (e.g. because we're drawing to a print preview memory DC)
1460 int pageWidth
, pageHeight
;
1462 dc
->GetSize(&w
, &h
);
1463 GetPageSizePixels(&pageWidth
, &pageHeight
);
1465 // If printer pageWidth == current DC width, then this doesn't
1466 // change. But w might be the preview bitmap width, so scale down.
1467 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1468 dc
->SetUserScale(overallScale
, overallScale
);
1472 m_printoutView
->OnDraw(dc
);
1477 bool wxDocPrintout::HasPage(int pageNum
)
1479 return (pageNum
== 1);
1482 bool wxDocPrintout::OnBeginDocument(int startPage
, int endPage
)
1484 if (!wxPrintout::OnBeginDocument(startPage
, endPage
))
1490 void wxDocPrintout::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1501 * Command processing framework
1504 wxCommand::wxCommand(bool canUndoIt
, const wxString
& name
)
1506 m_canUndo
= canUndoIt
;
1507 m_commandName
= name
;
1510 wxCommand::~wxCommand(void)
1514 // Command processor
1515 wxCommandProcessor::wxCommandProcessor(int maxCommands
)
1517 m_maxNoCommands
= maxCommands
;
1518 m_currentCommand
= NULL
;
1519 m_commandEditMenu
= NULL
;
1522 wxCommandProcessor::~wxCommandProcessor(void)
1527 // Pass a command to the processor. The processor calls Do();
1528 // if successful, is appended to the command history unless
1529 // storeIt is FALSE.
1530 bool wxCommandProcessor::Submit(wxCommand
*command
, bool storeIt
)
1532 bool success
= command
->Do();
1533 if (success
&& storeIt
)
1535 if (m_commands
.Number() == m_maxNoCommands
)
1537 wxNode
*firstNode
= m_commands
.First();
1538 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1539 delete firstCommand
;
1543 // Correct a bug: we must chop off the current 'branch'
1544 // so that we're at the end of the command list.
1545 if (!m_currentCommand
)
1549 wxNode
*node
= m_currentCommand
->Next();
1552 wxNode
*next
= node
->Next();
1553 delete (wxCommand
*)node
->Data();
1559 m_commands
.Append(command
);
1560 m_currentCommand
= m_commands
.Last();
1566 bool wxCommandProcessor::Undo(void)
1568 if (m_currentCommand
)
1570 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1571 if (command
->CanUndo())
1573 bool success
= command
->Undo();
1576 m_currentCommand
= m_currentCommand
->Previous();
1585 bool wxCommandProcessor::Redo(void)
1587 wxCommand
*redoCommand
= NULL
;
1588 wxNode
*redoNode
= NULL
;
1589 if (m_currentCommand
&& m_currentCommand
->Next())
1591 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1592 redoNode
= m_currentCommand
->Next();
1596 if (m_commands
.Number() > 0)
1598 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1599 redoNode
= m_commands
.First();
1605 bool success
= redoCommand
->Do();
1608 m_currentCommand
= redoNode
;
1616 bool wxCommandProcessor::CanUndo(void)
1618 if (m_currentCommand
)
1619 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1623 void wxCommandProcessor::Initialize(void)
1625 m_currentCommand
= m_commands
.Last();
1629 void wxCommandProcessor::SetMenuStrings(void)
1631 if (m_commandEditMenu
)
1634 if (m_currentCommand
)
1636 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1637 wxString
commandName(command
->GetName());
1638 if (commandName
== "") commandName
= "Unnamed command";
1639 bool canUndo
= command
->CanUndo();
1641 buf
= wxString("&Undo ") + commandName
;
1643 buf
= wxString("Can't &Undo ") + commandName
;
1645 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1646 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1648 // We can redo, if we're not at the end of the history.
1649 if (m_currentCommand
->Next())
1651 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1652 wxString
redoCommandName(redoCommand
->GetName());
1653 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1654 buf
= wxString("&Redo ") + redoCommandName
;
1655 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1656 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1660 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1661 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1666 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1667 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1669 if (m_commands
.Number() == 0)
1671 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1672 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1676 // currentCommand is NULL but there are commands: this means that
1677 // we've undone to the start of the list, but can redo the first.
1678 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1679 wxString
redoCommandName(redoCommand
->GetName());
1680 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1681 buf
= wxString("&Redo ") + redoCommandName
;
1682 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1683 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1689 void wxCommandProcessor::ClearCommands(void)
1691 wxNode
*node
= m_commands
.First();
1694 wxCommand
*command
= (wxCommand
*)node
->Data();
1697 node
= m_commands
.First();
1699 m_currentCommand
= NULL
;
1704 * File history processor
1707 wxFileHistory::wxFileHistory(int maxFiles
)
1709 m_fileMaxFiles
= maxFiles
;
1712 m_fileHistory
= new char *[m_fileMaxFiles
];
1715 wxFileHistory::~wxFileHistory(void)
1718 for (i
= 0; i
< m_fileHistoryN
; i
++)
1719 delete[] m_fileHistory
[i
];
1720 delete[] m_fileHistory
;
1723 // File history management
1724 void wxFileHistory::AddFileToHistory(const wxString
& file
)
1731 // Check we don't already have this file
1732 for (i
= 0; i
< m_fileHistoryN
; i
++)
1734 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1738 // Add to the project file history:
1739 // Move existing files (if any) down so we can insert file at beginning.
1741 // First delete filename that has popped off the end of the array (if any)
1742 if (m_fileHistoryN
== m_fileMaxFiles
)
1744 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1745 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1747 if (m_fileHistoryN
< m_fileMaxFiles
)
1749 if (m_fileHistoryN
== 0)
1750 m_fileMenu
->AppendSeparator();
1751 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1754 // Shuffle filenames down
1755 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1757 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1759 m_fileHistory
[0] = copystring(file
);
1761 for (i
= 0; i
< m_fileHistoryN
; i
++)
1762 if (m_fileHistory
[i
])
1765 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1766 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1770 wxString
wxFileHistory::GetHistoryFile(int i
) const
1772 if (i
< m_fileHistoryN
)
1773 return wxString(m_fileHistory
[i
]);
1775 return wxString("");
1778 void wxFileHistory::FileHistoryUseMenu(wxMenu
*menu
)
1783 void wxFileHistory::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1788 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1789 char *historyFile
= NULL
;
1790 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1792 // wxGetResource allocates memory so this is o.k.
1793 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1795 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1801 void wxFileHistory::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1806 for (i
= 0; i
< m_fileHistoryN
; i
++)
1808 sprintf(buf
, "file%d", i
+1);
1809 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1819 wxPrintInfo::wxPrintInfo(void)
1824 wxPrintInfo::~wxPrintInfo(void)
1830 * Permits compatibility with existing file formats and functions
1831 * that manipulate files directly
1834 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1839 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1842 while ((ch
= getc (fd1
)) != EOF
)
1843 stream
<< (unsigned char)ch
;
1849 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1854 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1859 while (!stream
.eof())
1870 // End USE_DOC_VIEW_ARCHITECTURE