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"
40 #include "wx/msgdlg.h"
41 #include "wx/choicdlg.h"
42 #include "wx/docview.h"
43 #include "wx/printdlg.h"
44 #include "wx/generic/prntdlgg.h"
45 #include "wx/generic/printps.h"
58 #if !USE_SHARED_LIBRARY
59 IMPLEMENT_ABSTRACT_CLASS(wxDocument
, wxEvtHandler
)
60 IMPLEMENT_ABSTRACT_CLASS(wxView
, wxEvtHandler
)
61 IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate
, wxObject
)
62 IMPLEMENT_DYNAMIC_CLASS(wxDocManager
, wxEvtHandler
)
63 IMPLEMENT_CLASS(wxDocChildFrame
, wxFrame
)
64 IMPLEMENT_CLASS(wxDocParentFrame
, wxFrame
)
65 #if USE_PRINTING_ARCHITECTURE
66 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout
, wxPrintout
)
68 IMPLEMENT_CLASS(wxCommand
, wxObject
)
69 IMPLEMENT_DYNAMIC_CLASS(wxCommandProcessor
, wxObject
)
70 IMPLEMENT_DYNAMIC_CLASS(wxFileHistory
, wxObject
)
71 // IMPLEMENT_DYNAMIC_CLASS(wxPrintInfo, wxObject)
75 * Definition of wxDocument
78 wxDocument
::wxDocument(wxDocument
*parent
)
80 m_documentModified
=FALSE
;
83 m_documentParent
=parent
;
84 m_documentTemplate
= NULL
;
85 m_documentTypeName
= "";
89 bool wxDocument
::DeleteContents(void)
94 wxDocument
::~wxDocument(void)
98 if (m_commandProcessor
)
99 delete m_commandProcessor
;
101 GetDocumentManager()->RemoveDocument(this);
103 // Not safe to do here, since it'll
104 // invoke virtual view functions expecting to see
105 // valid derived objects: and by the time we get
106 // here, we've called destructors higher up.
110 bool wxDocument
::Close(void)
112 if (OnSaveModified())
113 return OnCloseDocument();
118 bool wxDocument
::OnCloseDocument(void)
125 // Note that this implicitly deletes the document when
126 // the last view is deleted.
127 bool wxDocument
::DeleteAllViews(void)
129 wxNode
*node
= m_documentViews
.First();
132 wxView
*view
= (wxView
*)node
->Data();
136 wxNode
*next
= node
->Next();
138 delete view
; // Deletes node implicitly
144 wxView
*wxDocument
::GetFirstView(void) const
146 if (m_documentViews
.Number() == 0)
148 return (wxView
*)m_documentViews
.First()->Data();
151 wxDocManager
*wxDocument
::GetDocumentManager(void) const
153 return m_documentTemplate
->GetDocumentManager();
156 bool wxDocument
::OnNewDocument(void)
158 if (!OnSaveModified())
161 if (OnCloseDocument()==FALSE
) return FALSE
;
164 SetDocumentSaved(FALSE
);
167 GetDocumentManager()->MakeDefaultName(name
);
169 SetFilename(name
, TRUE
);
174 bool wxDocument
::Save(void)
178 if (!IsModified()) return TRUE
;
179 if (m_documentFile
== "" || !m_savedYet
)
182 ret
= OnSaveDocument(m_documentFile
);
184 SetDocumentSaved(TRUE
);
188 bool wxDocument
::SaveAs(void)
190 wxDocTemplate
*docTemplate
= GetDocumentTemplate();
194 char *tmp
= wxFileSelector("Save as", docTemplate
->GetDirectory(), GetFilename(),
195 docTemplate
->GetDefaultExtension(), docTemplate
->GetFileFilter(),
196 0, GetDocumentWindow());
203 SetTitle(wxFileNameFromPath(tmp
));
205 GetDocumentManager()->AddFileToHistory(tmp
);
207 // Notify the views that the filename has changed
208 wxNode
*node
= m_documentViews
.First();
211 wxView
*view
= (wxView
*)node
->Data();
212 view
->OnChangeFilename();
216 return OnSaveDocument(m_documentFile
);
219 bool wxDocument
::OnSaveDocument(const wxString
& file
)
225 if (wxTheApp
->GetAppName() != "")
226 msgTitle
= wxTheApp
->GetAppName();
228 msgTitle
= wxString("File error");
230 ofstream
store(file
);
231 if (store
.fail() || store
.bad())
233 (void)wxMessageBox("Sorry, could not open this file for saving.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
234 GetDocumentWindow());
238 if (SaveObject(store
)==FALSE
)
240 (void)wxMessageBox("Sorry, could not save this file.", msgTitle
, wxOK
| wxICON_EXCLAMATION
,
241 GetDocumentWindow());
250 bool wxDocument
::OnOpenDocument(const wxString
& file
)
252 if (!OnSaveModified())
256 if (wxTheApp
->GetAppName() != "")
257 msgTitle
= wxTheApp
->GetAppName();
259 msgTitle
= wxString("File error");
261 ifstream
store(file
);
262 if (store
.fail() || store
.bad())
264 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
265 GetDocumentWindow());
268 if (LoadObject(store
)==FALSE
)
270 (void)wxMessageBox("Sorry, could not open this file.", msgTitle
, wxOK
|wxICON_EXCLAMATION
,
271 GetDocumentWindow());
274 SetFilename(file
, TRUE
);
282 istream
& wxDocument
::LoadObject(istream
& stream
)
284 // wxObject::LoadObject(stream);
289 ostream
& wxDocument
::SaveObject(ostream
& stream
)
291 // wxObject::SaveObject(stream);
296 bool wxDocument
::Revert(void)
302 // Get title, or filename if no title, else unnamed
303 bool wxDocument
::GetPrintableName(wxString
& buf
) const
305 if (m_documentTitle
!= "")
307 buf
= m_documentTitle
;
310 else if (m_documentFile
!= "")
312 buf
= wxFileNameFromPath(m_documentFile
);
322 wxWindow
*wxDocument
::GetDocumentWindow(void) const
324 wxView
*view
= GetFirstView();
326 return view
->GetFrame();
328 return wxTheApp
->GetTopWindow();
331 wxCommandProcessor
*wxDocument
::OnCreateCommandProcessor(void)
333 return new wxCommandProcessor
;
336 // TRUE if safe to close
337 bool wxDocument
::OnSaveModified(void)
343 GetPrintableName(title
);
346 if (wxTheApp
->GetAppName() != "")
347 msgTitle
= wxTheApp
->GetAppName();
349 msgTitle
= wxString("Warning");
351 sprintf(buf
, "Do you want to save changes to document %s?", (const char *)title
);
352 int res
= wxMessageBox(buf
, msgTitle
, wxYES_NO
|wxCANCEL
|wxICON_QUESTION
,
353 GetDocumentWindow());
359 else if (res
== wxYES
)
361 else if (res
== wxCANCEL
)
367 bool wxDocument
::Draw(wxDC
& WXUNUSED(context
))
372 bool wxDocument
::AddView(wxView
*view
)
374 if (!m_documentViews
.Member(view
))
376 m_documentViews
.Append(view
);
382 bool wxDocument
::RemoveView(wxView
*view
)
384 (void)m_documentViews
.DeleteObject(view
);
389 bool wxDocument
::OnCreate(const wxString
& WXUNUSED(path
), long flags
)
391 if (GetDocumentTemplate()->CreateView(this, flags
))
397 // Called after a view is added or removed.
398 // The default implementation deletes the document if
399 // there are no more views.
400 void wxDocument
::OnChangedViewList(void)
402 if (m_documentViews
.Number() == 0)
404 if (OnSaveModified())
411 void wxDocument
::UpdateAllViews(wxView
*sender
, wxObject
*hint
)
413 wxNode
*node
= m_documentViews
.First();
416 wxView
*view
= (wxView
*)node
->Data();
417 view
->OnUpdate(sender
, hint
);
422 void wxDocument
::SetFilename(const wxString
& filename
, bool notifyViews
)
424 m_documentFile
= filename
;
427 // Notify the views that the filename has changed
428 wxNode
*node
= m_documentViews
.First();
431 wxView
*view
= (wxView
*)node
->Data();
432 view
->OnChangeFilename();
443 wxView
::wxView(wxDocument
*doc
)
451 wxView
::~wxView(void)
453 GetDocumentManager()->ActivateView(this, FALSE
, TRUE
);
454 m_viewDocument
->RemoveView(this);
457 // Extend event processing to search the document's event table
458 bool wxView
::ProcessEvent(wxEvent
& event
)
460 if ( !GetDocument() || !GetDocument()->ProcessEvent(event
) )
461 return wxEvtHandler
::ProcessEvent(event
);
466 void wxView
::OnActivateView(bool WXUNUSED(activate
), wxView
*WXUNUSED(activeView
), wxView
*WXUNUSED(deactiveView
))
470 void wxView
::OnPrint(wxDC
*dc
, wxObject
*WXUNUSED(info
))
475 void wxView
::OnUpdate(wxView
*WXUNUSED(sender
), wxObject
*WXUNUSED(hint
))
479 void wxView
::OnChangeFilename(void)
481 if (GetFrame() && GetDocument())
484 GetDocument()->GetPrintableName(name
);
486 // If the frame is an MDI child, just set the title to the name.
487 // Otherwise, append the document name to the name of the application
488 // I have to do an illegal cast because in wxGTK, wxMDIChildFrame
489 // doesn't inherited from wxFrame, Robert Roebling
491 wxFrame
*frame
= NULL
;
492 wxMDIChildFrame
*mdi_frame
= NULL
;
493 if (GetFrame()->IsKindOf(CLASSINFO(wxMDIChildFrame
)))
494 mdi_frame
= (wxMDIChildFrame
*)GetFrame();
500 frame
->SetTitle(name
);
506 if (wxTheApp
->GetAppName() != "")
509 sprintf(buf
, "%s - %s", (const char *)wxTheApp
->GetAppName(), (const char *)name
);
510 mdi_frame
->SetTitle(buf
);
513 mdi_frame
->SetTitle(name
);
518 void wxView
::SetDocument(wxDocument
*doc
)
520 m_viewDocument
= doc
;
525 bool wxView
::Close(bool deleteWindow
)
527 if (OnClose(deleteWindow
))
533 void wxView
::Activate(bool activate
)
535 if (GetDocumentManager())
537 OnActivateView(activate
, this, GetDocumentManager()->GetCurrentView());
538 GetDocumentManager()->ActivateView(this, activate
);
542 bool wxView
::OnClose(bool WXUNUSED(deleteWindow
))
544 return GetDocument() ?
GetDocument()->Close() : TRUE
;
547 #if USE_PRINTING_ARCHITECTURE
548 wxPrintout
*wxView
::OnCreatePrintout(void)
550 return new wxDocPrintout(this);
559 wxDocTemplate
::wxDocTemplate(wxDocManager
*manager
, const wxString
& descr
,
560 const wxString
& filter
, const wxString
& dir
, const wxString
& ext
,
561 const wxString
& docTypeName
, const wxString
& viewTypeName
,
562 wxClassInfo
*docClassInfo
, wxClassInfo
*viewClassInfo
, long flags
)
564 m_documentManager
= manager
;
566 m_description
= descr
;
569 m_fileFilter
= filter
;
571 m_docTypeName
= docTypeName
;
572 m_viewTypeName
= viewTypeName
;
573 m_documentManager
->AssociateTemplate(this);
575 m_docClassInfo
= docClassInfo
;
576 m_viewClassInfo
= viewClassInfo
;
579 wxDocTemplate
::~wxDocTemplate(void)
581 m_documentManager
->DisassociateTemplate(this);
584 // Tries to dynamically construct an object of the right
586 wxDocument
*wxDocTemplate
::CreateDocument(const wxString
& path
, long flags
)
590 wxDocument
*doc
= (wxDocument
*)m_docClassInfo
->CreateObject();
591 doc
->SetFilename(path
);
592 doc
->SetDocumentTemplate(this);
593 GetDocumentManager()->AddDocument(doc
);
594 doc
->SetCommandProcessor(doc
->OnCreateCommandProcessor());
596 if (doc
->OnCreate(path
, flags
))
605 wxView
*wxDocTemplate
::CreateView(wxDocument
*doc
, long flags
)
607 if (!m_viewClassInfo
)
609 wxView
*view
= (wxView
*)m_viewClassInfo
->CreateObject();
610 view
->SetDocument(doc
);
611 if (view
->OnCreate(doc
, flags
))
622 BEGIN_EVENT_TABLE(wxDocManager
, wxEvtHandler
)
623 EVT_MENU(wxID_OPEN
, wxDocManager
::OnFileOpen
)
624 EVT_MENU(wxID_CLOSE
, wxDocManager
::OnFileClose
)
625 EVT_MENU(wxID_REVERT
, wxDocManager
::OnFileRevert
)
626 EVT_MENU(wxID_NEW
, wxDocManager
::OnFileNew
)
627 EVT_MENU(wxID_SAVE
, wxDocManager
::OnFileSave
)
628 EVT_MENU(wxID_SAVEAS
, wxDocManager
::OnFileSaveAs
)
629 EVT_MENU(wxID_UNDO
, wxDocManager
::OnUndo
)
630 EVT_MENU(wxID_REDO
, wxDocManager
::OnRedo
)
631 EVT_MENU(wxID_PRINT
, wxDocManager
::OnPrint
)
632 EVT_MENU(wxID_PRINT_SETUP
, wxDocManager
::OnPrintSetup
)
633 EVT_MENU(wxID_PREVIEW
, wxDocManager
::OnPreview
)
636 wxDocManager
::wxDocManager(long flags
, bool initialize
)
638 m_defaultDocumentNameCounter
= 1;
640 m_currentView
= NULL
;
641 m_maxDocsOpen
= 10000;
642 m_fileHistory
= NULL
;
647 wxDocManager
::~wxDocManager(void)
651 delete m_fileHistory
;
654 bool wxDocManager
::Clear(bool force
)
656 wxNode
*node
= m_docs
.First();
659 wxDocument
*doc
= (wxDocument
*)node
->Data();
660 wxNode
*next
= node
->Next();
662 if (!doc
->Close() && !force
)
665 // Implicitly deletes the document when the last
666 // view is removed (deleted)
667 doc
->DeleteAllViews();
669 // Check document is deleted
670 if (m_docs
.Member(doc
))
673 // This assumes that documents are not connected in
674 // any way, i.e. deleting one document does NOT
678 node
= m_templates
.First();
681 wxDocTemplate
*templ
= (wxDocTemplate
*) node
->Data();
682 wxNode
* next
= node
->Next();
689 bool wxDocManager
::Initialize(void)
691 m_fileHistory
= OnCreateFileHistory();
695 wxFileHistory
*wxDocManager
::OnCreateFileHistory(void)
697 return new wxFileHistory
;
700 void wxDocManager
::OnFileClose(wxCommandEvent
& WXUNUSED(event
))
702 wxDocument
*doc
= GetCurrentDocument();
707 doc
->DeleteAllViews();
708 if (m_docs
.Member(doc
))
713 void wxDocManager
::OnFileNew(wxCommandEvent
& WXUNUSED(event
))
715 CreateDocument(wxString(""), wxDOC_NEW
);
718 void wxDocManager
::OnFileOpen(wxCommandEvent
& WXUNUSED(event
))
720 CreateDocument(wxString(""), 0);
723 void wxDocManager
::OnFileRevert(wxCommandEvent
& WXUNUSED(event
))
725 wxDocument
*doc
= GetCurrentDocument();
731 void wxDocManager
::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
733 wxDocument
*doc
= GetCurrentDocument();
739 void wxDocManager
::OnFileSaveAs(wxCommandEvent
& WXUNUSED(event
))
741 wxDocument
*doc
= GetCurrentDocument();
747 void wxDocManager
::OnPrint(wxCommandEvent
& WXUNUSED(event
))
749 wxView
*view
= GetCurrentView();
753 wxPrintout
*printout
= view
->OnCreatePrintout();
756 // TODO: trouble about this is that it pulls in the postscript
759 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
761 wxWindowsPrinter printer
;
762 printer
.Print(view
->GetFrame(), printout
, TRUE
);
767 wxPostScriptPrinter printer
;
768 printer
.Print(view
->GetFrame(), printout
, TRUE
);
775 void wxDocManager
::OnPrintSetup(wxCommandEvent
& WXUNUSED(event
))
777 wxWindow
*parentWin
= wxTheApp
->GetTopWindow();
778 wxView
*view
= GetCurrentView();
780 parentWin
= view
->GetFrame();
785 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
787 wxPrintDialog
printerDialog(parentWin
, & data
);
788 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
789 printerDialog
.ShowModal();
794 wxGenericPrintDialog
printerDialog(parentWin
, & data
);
795 printerDialog
.GetPrintData().SetSetupDialog(TRUE
);
796 printerDialog
.ShowModal();
800 void wxDocManager
::OnPreview(wxCommandEvent
& WXUNUSED(event
))
802 wxView
*view
= GetCurrentView();
806 wxPrintout
*printout
= view
->OnCreatePrintout();
809 // Pass two printout objects: for preview, and possible printing.
810 wxPrintPreviewBase
*preview
= NULL
;
812 if ( wxTheApp
->GetPrintMode() == wxPRINT_WINDOWS
)
813 preview
= new wxWindowsPrintPreview(printout
, view
->OnCreatePrintout());
816 preview
= new wxPostScriptPrintPreview(printout
, view
->OnCreatePrintout());
818 wxPreviewFrame
*frame
= new wxPreviewFrame(preview
, (wxFrame
*)wxTheApp
->GetTopWindow(), "Print Preview",
819 wxPoint(100, 100), wxSize(600, 650));
820 frame
->Centre(wxBOTH
);
826 void wxDocManager
::OnUndo(wxCommandEvent
& WXUNUSED(event
))
828 wxDocument
*doc
= GetCurrentDocument();
831 if (doc
->GetCommandProcessor())
832 doc
->GetCommandProcessor()->Undo();
835 void wxDocManager
::OnRedo(wxCommandEvent
& WXUNUSED(event
))
837 wxDocument
*doc
= GetCurrentDocument();
840 if (doc
->GetCommandProcessor())
841 doc
->GetCommandProcessor()->Redo();
844 wxView
*wxDocManager
::GetCurrentView(void) const
847 return m_currentView
;
848 if (m_docs
.Number() == 1)
850 wxDocument
* doc
= (wxDocument
*) m_docs
.First()->Data();
851 return doc
->GetFirstView();
856 // Extend event processing to search the view's event table
857 bool wxDocManager
::ProcessEvent(wxEvent
& event
)
859 wxView
* view
= GetCurrentView();
862 if (view
->ProcessEvent(event
))
865 return wxEvtHandler
::ProcessEvent(event
);
868 wxDocument
*wxDocManager
::CreateDocument(const wxString
& path
, long flags
)
870 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
873 for (i
= 0; i
< m_templates
.Number(); i
++)
875 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
876 if (temp
->IsVisible())
888 // If we've reached the max number of docs, close the
890 if (GetDocuments().Number() >= m_maxDocsOpen
)
892 wxDocument
*doc
= (wxDocument
*)GetDocuments().First()->Data();
895 // Implicitly deletes the document when
896 // the last view is deleted
897 doc
->DeleteAllViews();
899 // Check we're really deleted
900 if (m_docs
.Member(doc
))
907 // New document: user chooses a template, unless there's only one.
908 if (flags
& wxDOC_NEW
)
912 wxDocTemplate
*temp
= templates
[0];
914 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
917 newDoc
->SetDocumentName(temp
->GetDocumentName());
918 newDoc
->SetDocumentTemplate(temp
);
919 newDoc
->OnNewDocument();
924 wxDocTemplate
*temp
= SelectDocumentType(templates
, n
);
928 wxDocument
*newDoc
= temp
->CreateDocument(path
, flags
);
931 newDoc
->SetDocumentName(temp
->GetDocumentName());
932 newDoc
->SetDocumentTemplate(temp
);
933 newDoc
->OnNewDocument();
942 wxDocTemplate
*temp
= NULL
;
948 if (flags
& wxDOC_SILENT
)
949 temp
= FindTemplateForPath(path2
);
951 temp
= SelectDocumentPath(templates
, n
, path2
, flags
);
957 wxDocument
*newDoc
= temp
->CreateDocument(path2
, flags
);
960 newDoc
->SetDocumentName(temp
->GetDocumentName());
961 newDoc
->SetDocumentTemplate(temp
);
962 if (!newDoc
->OnOpenDocument(path2
))
967 AddFileToHistory(path2
);
975 wxView
*wxDocManager
::CreateView(wxDocument
*doc
, long flags
)
977 wxDocTemplate
**templates
= new wxDocTemplate
*[m_templates
.Number()];
980 for (i
= 0; i
< m_templates
.Number(); i
++)
982 wxDocTemplate
*temp
= (wxDocTemplate
*)(m_templates
.Nth(i
)->Data());
983 if (temp
->IsVisible())
985 if (temp
->GetDocumentName() == doc
->GetDocumentName())
999 wxDocTemplate
*temp
= templates
[0];
1001 wxView
*view
= temp
->CreateView(doc
, flags
);
1003 view
->SetViewName(temp
->GetViewName());
1007 wxDocTemplate
*temp
= SelectViewType(templates
, n
);
1011 wxView
*view
= temp
->CreateView(doc
, flags
);
1013 view
->SetViewName(temp
->GetViewName());
1020 // Not yet implemented
1021 void wxDocManager
::DeleteTemplate(wxDocTemplate
*WXUNUSED(temp
), long WXUNUSED(flags
))
1025 // Not yet implemented
1026 bool wxDocManager
::FlushDoc(wxDocument
*WXUNUSED(doc
))
1031 wxDocument
*wxDocManager
::GetCurrentDocument(void) const
1034 return m_currentView
->GetDocument();
1039 // Make a default document name
1040 bool wxDocManager
::MakeDefaultName(wxString
& name
)
1043 sprintf(buf
, "unnamed%d", m_defaultDocumentNameCounter
);
1044 m_defaultDocumentNameCounter
++;
1049 // Not yet implemented
1050 wxDocTemplate
*wxDocManager
::MatchTemplate(const wxString
& WXUNUSED(path
))
1055 // File history management
1056 void wxDocManager
::AddFileToHistory(const wxString
& file
)
1059 m_fileHistory
->AddFileToHistory(file
);
1062 wxString wxDocManager
::GetHistoryFile(int i
) const
1065 return wxString(m_fileHistory
->GetHistoryFile(i
));
1067 return wxString("");
1070 void wxDocManager
::FileHistoryUseMenu(wxMenu
*menu
)
1073 m_fileHistory
->FileHistoryUseMenu(menu
);
1076 void wxDocManager
::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1079 m_fileHistory
->FileHistoryLoad(resourceFile
, section
);
1082 void wxDocManager
::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1085 m_fileHistory
->FileHistorySave(resourceFile
, section
);
1088 int wxDocManager
::GetNoHistoryFiles(void) const
1091 return m_fileHistory
->GetNoHistoryFiles();
1096 static char *FindExtension(char *path
)
1098 static char ext
[10];
1099 int len
= strlen(path
);
1103 for (i
= (len
-1); i
> 0; i
--)
1109 for (j
= i
+1; j
< len
; j
++)
1110 ext
[(int)(j
-(i
+1))] = (char)wxToLower(path
[j
]); // NOTE Should not use tolower under UNIX
1121 // Given a path, try to find a matching template. Won't
1122 // always work, of course.
1123 wxDocTemplate
*wxDocManager
::FindTemplateForPath(const wxString
& path
)
1125 char *theExt
= FindExtension((char *)(const char *)path
);
1128 wxDocTemplate
*theTemplate
= NULL
;
1130 if (m_templates
.Number() == 1)
1131 return (wxDocTemplate
*)m_templates
.First()->Data();
1133 // Find the template which this extension corresponds to
1135 for (i
= 0; i
< m_templates
.Number(); i
++)
1137 wxDocTemplate
*temp
= (wxDocTemplate
*)m_templates
.Nth(i
)->Data();
1138 if (strcmp(temp
->GetDefaultExtension(), theExt
) == 0)
1147 // Prompts user to open a file, using file specs in templates.
1148 // How to implement in wxWindows? Must extend the file selector
1149 // dialog or implement own; OR match the extension to the
1150 // template extension.
1151 wxDocTemplate
*wxDocManager
::SelectDocumentPath(wxDocTemplate
**templates
,
1152 int noTemplates
, wxString
& path
, long WXUNUSED(flags
), bool WXUNUSED(save
))
1154 // We can only have multiple filters in Windows
1156 char *descrBuf
= new char[1000];
1159 for (i
= 0; i
< noTemplates
; i
++)
1161 if (templates
[i
]->IsVisible())
1163 strcat(descrBuf
, templates
[i
]->GetDescription());
1164 strcat(descrBuf
, " (");
1165 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1166 strcat(descrBuf
, ") ");
1167 strcat(descrBuf
, "|");
1168 strcat(descrBuf
, templates
[i
]->GetFileFilter());
1169 strcat(descrBuf
, "|");
1172 int len
= strlen(descrBuf
);
1175 descrBuf
[len
-1] = 0;
1177 char *pathTmp
= wxFileSelector("Select a file", "", "", "", descrBuf
, 0, wxTheApp
->GetTopWindow());
1182 char *theExt
= FindExtension((char *)(const char *)path
);
1186 // This is dodgy in that we're selecting the template on the
1187 // basis of the file extension, which may not be a standard
1188 // one. We really want to know exactly which template was
1189 // chosen by using a more advanced file selector.
1190 wxDocTemplate
*theTemplate
= FindTemplateForPath(path
);
1199 // In all other windowing systems, until we have more advanced
1200 // file selectors, we must select the document type (template) first, and
1201 // _then_ pop up the file selector.
1202 wxDocTemplate
*temp
= SelectDocumentType(templates
, noTemplates
);
1206 char *pathTmp
= wxFileSelector("Select a file", "", "",
1207 temp
->GetDefaultExtension(),
1208 temp
->GetFileFilter(),
1209 0, wxTheApp
->GetTopWindow());
1221 wxDocTemplate
*wxDocManager
::SelectDocumentType(wxDocTemplate
**templates
,
1224 char **strings
= new char *[noTemplates
];
1225 char **data
= new char *[noTemplates
];
1228 for (i
= 0; i
< noTemplates
; i
++)
1230 if (templates
[i
]->IsVisible())
1232 strings
[n
] = WXSTRINGCAST templates
[i
]->m_description
;
1233 data
[n
] = (char *)templates
[i
];
1245 wxDocTemplate
*temp
= (wxDocTemplate
*)data
[0];
1251 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document template", "Templates", n
,
1258 wxDocTemplate
*wxDocManager
::SelectViewType(wxDocTemplate
**templates
,
1261 char **strings
= new char *[noTemplates
];
1262 char **data
= new char *[noTemplates
];
1265 for (i
= 0; i
< noTemplates
; i
++)
1267 if (templates
[i
]->IsVisible() && templates
[i
]->GetViewName())
1269 strings
[n
] = WXSTRINGCAST templates
[i
]->m_viewTypeName
;
1270 data
[n
] = (char *)templates
[i
];
1274 wxDocTemplate
*theTemplate
= (wxDocTemplate
*)wxGetSingleChoiceData("Select a document view", "Views", n
,
1281 void wxDocManager
::AssociateTemplate(wxDocTemplate
*temp
)
1283 if (!m_templates
.Member(temp
))
1284 m_templates
.Append(temp
);
1287 void wxDocManager
::DisassociateTemplate(wxDocTemplate
*temp
)
1289 m_templates
.DeleteObject(temp
);
1292 // Add and remove a document from the manager's list
1293 void wxDocManager
::AddDocument(wxDocument
*doc
)
1295 if (!m_docs
.Member(doc
))
1299 void wxDocManager
::RemoveDocument(wxDocument
*doc
)
1301 m_docs
.DeleteObject(doc
);
1304 // Views or windows should inform the document manager
1305 // when a view is going in or out of focus
1306 void wxDocManager
::ActivateView(wxView
*view
, bool activate
, bool WXUNUSED(deleting
))
1308 // If we're deactiving, and if we're not actually deleting the view, then
1309 // don't reset the current view because we may be going to
1310 // a window without a view.
1311 // WHAT DID I MEAN BY THAT EXACTLY?
1315 if (m_currentView == view)
1316 m_currentView = NULL;
1322 m_currentView
= view
;
1324 m_currentView
= NULL
;
1329 * Default document child frame
1332 BEGIN_EVENT_TABLE(wxDocChildFrame
, wxFrame
)
1333 EVT_ACTIVATE(wxDocChildFrame
::OnActivate
)
1336 wxDocChildFrame
::wxDocChildFrame(wxDocument
*doc
, wxView
*view
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1337 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1338 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1340 m_childDocument
= doc
;
1343 view
->SetFrame(this);
1346 wxDocChildFrame
::~wxDocChildFrame(void)
1350 // Extend event processing to search the view's event table
1351 bool wxDocChildFrame
::ProcessEvent(wxEvent
& event
)
1354 m_childView
->Activate(TRUE
);
1356 if ( !m_childView
|| ! m_childView
->ProcessEvent(event
) )
1358 // Only hand up to the parent if it's a menu command
1359 if (!event
.IsKindOf(CLASSINFO(wxCommandEvent
)) || !GetParent() || !GetParent()->ProcessEvent(event
))
1360 return wxEvtHandler
::ProcessEvent(event
);
1368 void wxDocChildFrame
::OnActivate(wxActivateEvent
& event
)
1370 wxFrame
::OnActivate(event
);
1373 m_childView
->Activate(event
.GetActive());
1376 bool wxDocChildFrame
::OnClose(void)
1378 // Close view but don't delete the frame while doing so!
1379 // ...since it will be deleted by wxWindows if we return TRUE.
1382 bool ans
= m_childView
->Close(FALSE
); // FALSE means don't delete associated window
1385 m_childView
->Activate(FALSE
);
1388 m_childDocument
= NULL
;
1397 * Default parent frame
1400 BEGIN_EVENT_TABLE(wxDocParentFrame
, wxFrame
)
1401 EVT_MENU(wxID_EXIT
, wxDocParentFrame
::OnExit
)
1402 EVT_MENU_RANGE(wxID_FILE1
, wxID_FILE2
, wxDocParentFrame
::OnMRUFile
)
1405 wxDocParentFrame
::wxDocParentFrame(wxDocManager
*manager
, wxFrame
*frame
, wxWindowID id
, const wxString
& title
,
1406 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxString
& name
):
1407 wxFrame(frame
, id
, title
, pos
, size
, style
, name
)
1409 m_docManager
= manager
;
1412 void wxDocParentFrame
::OnExit(wxCommandEvent
& WXUNUSED(event
))
1417 void wxDocParentFrame
::OnMRUFile(wxCommandEvent
& event
)
1419 wxString
f(m_docManager
->GetHistoryFile(event
.GetSelection() - wxID_FILE1
));
1421 (void)m_docManager
->CreateDocument(f
, wxDOC_SILENT
);
1424 // Extend event processing to search the view's event table
1425 bool wxDocParentFrame
::ProcessEvent(wxEvent
& event
)
1427 // Try the document manager, then do default processing
1428 if (!m_docManager
|| !m_docManager
->ProcessEvent(event
))
1429 return wxEvtHandler
::ProcessEvent(event
);
1434 // Define the behaviour for the frame closing
1435 // - must delete all frames except for the main one.
1436 bool wxDocParentFrame
::OnClose(void)
1438 return m_docManager
->Clear(FALSE
);
1441 #if USE_PRINTING_ARCHITECTURE
1443 wxDocPrintout
::wxDocPrintout(wxView
*view
, const wxString
& title
):
1444 wxPrintout(WXSTRINGCAST title
)
1446 m_printoutView
= view
;
1449 bool wxDocPrintout
::OnPrintPage(int WXUNUSED(page
))
1453 // Get the logical pixels per inch of screen and printer
1454 int ppiScreenX
, ppiScreenY
;
1455 GetPPIScreen(&ppiScreenX
, &ppiScreenY
);
1456 int ppiPrinterX
, ppiPrinterY
;
1457 GetPPIPrinter(&ppiPrinterX
, &ppiPrinterY
);
1459 // This scales the DC so that the printout roughly represents the
1460 // the screen scaling. The text point size _should_ be the right size
1461 // but in fact is too small for some reason. This is a detail that will
1462 // need to be addressed at some point but can be fudged for the
1464 float scale
= (float)((float)ppiPrinterX
/(float)ppiScreenX
);
1466 // Now we have to check in case our real page size is reduced
1467 // (e.g. because we're drawing to a print preview memory DC)
1468 int pageWidth
, pageHeight
;
1470 dc
->GetSize(&w
, &h
);
1471 GetPageSizePixels(&pageWidth
, &pageHeight
);
1473 // If printer pageWidth == current DC width, then this doesn't
1474 // change. But w might be the preview bitmap width, so scale down.
1475 float overallScale
= scale
* (float)(w
/(float)pageWidth
);
1476 dc
->SetUserScale(overallScale
, overallScale
);
1480 m_printoutView
->OnDraw(dc
);
1485 bool wxDocPrintout
::HasPage(int pageNum
)
1487 return (pageNum
== 1);
1490 bool wxDocPrintout
::OnBeginDocument(int startPage
, int endPage
)
1492 if (!wxPrintout
::OnBeginDocument(startPage
, endPage
))
1498 void wxDocPrintout
::GetPageInfo(int *minPage
, int *maxPage
, int *selPageFrom
, int *selPageTo
)
1509 * Command processing framework
1512 wxCommand
::wxCommand(bool canUndoIt
, const wxString
& name
)
1514 m_canUndo
= canUndoIt
;
1515 m_commandName
= name
;
1518 wxCommand
::~wxCommand(void)
1522 // Command processor
1523 wxCommandProcessor
::wxCommandProcessor(int maxCommands
)
1525 m_maxNoCommands
= maxCommands
;
1526 m_currentCommand
= NULL
;
1527 m_commandEditMenu
= NULL
;
1530 wxCommandProcessor
::~wxCommandProcessor(void)
1535 // Pass a command to the processor. The processor calls Do();
1536 // if successful, is appended to the command history unless
1537 // storeIt is FALSE.
1538 bool wxCommandProcessor
::Submit(wxCommand
*command
, bool storeIt
)
1540 bool success
= command
->Do();
1541 if (success
&& storeIt
)
1543 if (m_commands
.Number() == m_maxNoCommands
)
1545 wxNode
*firstNode
= m_commands
.First();
1546 wxCommand
*firstCommand
= (wxCommand
*)firstNode
->Data();
1547 delete firstCommand
;
1551 // Correct a bug: we must chop off the current 'branch'
1552 // so that we're at the end of the command list.
1553 if (!m_currentCommand
)
1557 wxNode
*node
= m_currentCommand
->Next();
1560 wxNode
*next
= node
->Next();
1561 delete (wxCommand
*)node
->Data();
1567 m_commands
.Append(command
);
1568 m_currentCommand
= m_commands
.Last();
1574 bool wxCommandProcessor
::Undo(void)
1576 if (m_currentCommand
)
1578 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1579 if (command
->CanUndo())
1581 bool success
= command
->Undo();
1584 m_currentCommand
= m_currentCommand
->Previous();
1593 bool wxCommandProcessor
::Redo(void)
1595 wxCommand
*redoCommand
= NULL
;
1596 wxNode
*redoNode
= NULL
;
1597 if (m_currentCommand
&& m_currentCommand
->Next())
1599 redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1600 redoNode
= m_currentCommand
->Next();
1604 if (m_commands
.Number() > 0)
1606 redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1607 redoNode
= m_commands
.First();
1613 bool success
= redoCommand
->Do();
1616 m_currentCommand
= redoNode
;
1624 bool wxCommandProcessor
::CanUndo(void)
1626 if (m_currentCommand
)
1627 return ((wxCommand
*)m_currentCommand
->Data())->CanUndo();
1631 void wxCommandProcessor
::Initialize(void)
1633 m_currentCommand
= m_commands
.Last();
1637 void wxCommandProcessor
::SetMenuStrings(void)
1639 if (m_commandEditMenu
)
1642 if (m_currentCommand
)
1644 wxCommand
*command
= (wxCommand
*)m_currentCommand
->Data();
1645 wxString
commandName(command
->GetName());
1646 if (commandName
== "") commandName
= "Unnamed command";
1647 bool canUndo
= command
->CanUndo();
1649 buf
= wxString("&Undo ") + commandName
;
1651 buf
= wxString("Can't &Undo ") + commandName
;
1653 m_commandEditMenu
->SetLabel(wxID_UNDO
, buf
);
1654 m_commandEditMenu
->Enable(wxID_UNDO
, canUndo
);
1656 // We can redo, if we're not at the end of the history.
1657 if (m_currentCommand
->Next())
1659 wxCommand
*redoCommand
= (wxCommand
*)m_currentCommand
->Next()->Data();
1660 wxString
redoCommandName(redoCommand
->GetName());
1661 if (redoCommandName
== "") redoCommandName
= "Unnamed command";
1662 buf
= wxString("&Redo ") + redoCommandName
;
1663 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1664 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1668 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1669 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1674 m_commandEditMenu
->SetLabel(wxID_UNDO
, "&Undo");
1675 m_commandEditMenu
->Enable(wxID_UNDO
, FALSE
);
1677 if (m_commands
.Number() == 0)
1679 m_commandEditMenu
->SetLabel(wxID_REDO
, "&Redo");
1680 m_commandEditMenu
->Enable(wxID_REDO
, FALSE
);
1684 // currentCommand is NULL but there are commands: this means that
1685 // we've undone to the start of the list, but can redo the first.
1686 wxCommand
*redoCommand
= (wxCommand
*)m_commands
.First()->Data();
1687 wxString
redoCommandName(redoCommand
->GetName());
1688 if (!redoCommandName
) redoCommandName
= "Unnamed command";
1689 buf
= wxString("&Redo ") + redoCommandName
;
1690 m_commandEditMenu
->SetLabel(wxID_REDO
, buf
);
1691 m_commandEditMenu
->Enable(wxID_REDO
, TRUE
);
1697 void wxCommandProcessor
::ClearCommands(void)
1699 wxNode
*node
= m_commands
.First();
1702 wxCommand
*command
= (wxCommand
*)node
->Data();
1705 node
= m_commands
.First();
1707 m_currentCommand
= NULL
;
1712 * File history processor
1715 wxFileHistory
::wxFileHistory(int maxFiles
)
1717 m_fileMaxFiles
= maxFiles
;
1720 m_fileHistory
= new char *[m_fileMaxFiles
];
1723 wxFileHistory
::~wxFileHistory(void)
1726 for (i
= 0; i
< m_fileHistoryN
; i
++)
1727 delete[] m_fileHistory
[i
];
1728 delete[] m_fileHistory
;
1731 // File history management
1732 void wxFileHistory
::AddFileToHistory(const wxString
& file
)
1739 // Check we don't already have this file
1740 for (i
= 0; i
< m_fileHistoryN
; i
++)
1742 if (m_fileHistory
[i
] && wxString(m_fileHistory
[i
]) == file
)
1746 // Add to the project file history:
1747 // Move existing files (if any) down so we can insert file at beginning.
1749 // First delete filename that has popped off the end of the array (if any)
1750 if (m_fileHistoryN
== m_fileMaxFiles
)
1752 delete[] m_fileHistory
[m_fileMaxFiles
-1];
1753 m_fileHistory
[m_fileMaxFiles
-1] = NULL
;
1755 if (m_fileHistoryN
< m_fileMaxFiles
)
1757 if (m_fileHistoryN
== 0)
1758 m_fileMenu
->AppendSeparator();
1759 m_fileMenu
->Append(wxID_FILE1
+m_fileHistoryN
, "[EMPTY]");
1762 // Shuffle filenames down
1763 for (i
= (m_fileHistoryN
-1); i
> 0; i
--)
1765 m_fileHistory
[i
] = m_fileHistory
[i
-1];
1767 m_fileHistory
[0] = copystring(file
);
1769 for (i
= 0; i
< m_fileHistoryN
; i
++)
1770 if (m_fileHistory
[i
])
1773 sprintf(buf
, "&%d %s", i
+1, m_fileHistory
[i
]);
1774 m_fileMenu
->SetLabel(wxID_FILE1
+i
, buf
);
1778 wxString wxFileHistory
::GetHistoryFile(int i
) const
1780 if (i
< m_fileHistoryN
)
1781 return wxString(m_fileHistory
[i
]);
1783 return wxString("");
1786 void wxFileHistory
::FileHistoryUseMenu(wxMenu
*menu
)
1791 void wxFileHistory
::FileHistoryLoad(const wxString
& resourceFile
, const wxString
& section
)
1796 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1797 char *historyFile
= NULL
;
1798 while ((m_fileHistoryN
<= m_fileMaxFiles
) && wxGetResource(section
, buf
, &historyFile
, resourceFile
) && historyFile
)
1800 // wxGetResource allocates memory so this is o.k.
1801 m_fileHistory
[m_fileHistoryN
] = historyFile
;
1803 sprintf(buf
, "file%d", m_fileHistoryN
+1);
1809 void wxFileHistory
::FileHistorySave(const wxString
& resourceFile
, const wxString
& section
)
1814 for (i
= 0; i
< m_fileHistoryN
; i
++)
1816 sprintf(buf
, "file%d", i
+1);
1817 wxWriteResource(section
, buf
, m_fileHistory
[i
], resourceFile
);
1827 wxPrintInfo
::wxPrintInfo(void)
1832 wxPrintInfo
::~wxPrintInfo(void)
1838 * Permits compatibility with existing file formats and functions
1839 * that manipulate files directly
1842 bool wxTransferFileToStream(const wxString
& filename
, ostream
& stream
)
1847 if ((fd1
= fopen (WXSTRINGCAST filename
, "rb")) == NULL
)
1850 while ((ch
= getc (fd1
)) != EOF
)
1851 stream
<< (unsigned char)ch
;
1857 bool wxTransferStreamToFile(istream
& stream
, const wxString
& filename
)
1862 if ((fd1
= fopen (WXSTRINGCAST filename
, "wb")) == NULL
)
1867 while (!stream
.eof())
1878 // End USE_DOC_VIEW_ARCHITECTURE