]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
OpenVMS chokes on external templates
[wxWidgets.git] / src / common / docview.cpp
CommitLineData
5d9ea849 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/common/docview.cpp
c801d85f
KB
3// Purpose: Document/view classes
4// Author: Julian Smart
c77049a0 5// Modified by: Vadim Zeitlin
c801d85f
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
55d99c7a 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
0fb67cd1
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
8ecff181 24 #pragma hdrstop
c801d85f
KB
25#endif
26
47d67540 27#if wxUSE_DOC_VIEW_ARCHITECTURE
c801d85f 28
246c5004
WS
29#include "wx/docview.h"
30
c801d85f 31#ifndef WX_PRECOMP
8ecff181 32 #include "wx/list.h"
0fb67cd1
VZ
33 #include "wx/string.h"
34 #include "wx/utils.h"
35 #include "wx/app.h"
36 #include "wx/dc.h"
d0bdc3ca 37 #include "wx/dialog.h"
0fb67cd1 38 #include "wx/menu.h"
0fb67cd1 39 #include "wx/filedlg.h"
6de2f8b9 40 #include "wx/intl.h"
c0369005 41 #include "wx/log.h"
246c5004 42 #include "wx/msgdlg.h"
59cf2e49 43 #include "wx/mdi.h"
949c9f74 44 #include "wx/choicdlg.h"
f7bd2698
JS
45#endif
46
ce4169a4 47#if wxUSE_PRINTING_ARCHITECTURE
8ecff181
WS
48 #include "wx/prntbase.h"
49 #include "wx/printdlg.h"
ce4169a4
RR
50#endif
51
7f555861 52#include "wx/confbase.h"
c77049a0 53#include "wx/filename.h"
74b31181 54#include "wx/file.h"
c77049a0 55#include "wx/ffile.h"
1e6feb95 56#include "wx/cmdproc.h"
0f30d8e3 57#include "wx/tokenzr.h"
b4a980f4 58#include "wx/filename.h"
c77049a0 59#include "wx/vector.h"
c801d85f 60
a533f5c1 61#if wxUSE_STD_IOSTREAM
8ecff181 62 #include "wx/ioswrap.h"
968454db 63 #include "wx/beforestd.h"
8ecff181
WS
64 #if wxUSE_IOSTREAMH
65 #include <fstream.h>
66 #else
67 #include <fstream>
68 #endif
968454db 69 #include "wx/afterstd.h"
a533f5c1 70#else
8ecff181 71 #include "wx/wfstream.h"
c801d85f
KB
72#endif
73
c77049a0
VZ
74typedef wxVector<wxDocTemplate *> wxDocTemplates;
75
0fb67cd1 76// ----------------------------------------------------------------------------
77ffb593 77// wxWidgets macros
0fb67cd1
VZ
78// ----------------------------------------------------------------------------
79
1e6feb95
VZ
80IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler)
81IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler)
82IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject)
83IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler)
84IMPLEMENT_CLASS(wxDocChildFrame, wxFrame)
85IMPLEMENT_CLASS(wxDocParentFrame, wxFrame)
0fb67cd1 86
1e6feb95
VZ
87#if wxUSE_PRINTING_ARCHITECTURE
88 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
89#endif
0fb67cd1 90
1e6feb95 91IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject)
c801d85f 92
0fb67cd1
VZ
93// ============================================================================
94// implementation
95// ============================================================================
96
97// ----------------------------------------------------------------------------
c77049a0 98// private helpers
0fb67cd1
VZ
99// ----------------------------------------------------------------------------
100
c77049a0
VZ
101namespace
102{
103
104wxWindow *wxFindSuitableParent()
105{
106 wxWindow * const win = wxGetTopLevelParent(wxWindow::FindFocus());
107
108 return win ? win : wxTheApp->GetTopWindow();
109}
110
111wxString FindExtension(const wxString& path)
0fb67cd1
VZ
112{
113 wxString ext;
114 wxSplitPath(path, NULL, NULL, &ext);
115
116 // VZ: extensions are considered not case sensitive - is this really a good
117 // idea?
118 return ext.MakeLower();
119}
120
c77049a0
VZ
121// return the string used for the MRU list items in the menu
122//
123// NB: the index n is 0-based, as usual, but the strings start from 1
124wxString GetMRUEntryLabel(int n, const wxString& path)
125{
126 // we need to quote '&' characters which are used for mnemonics
127 wxString pathInMenu(path);
128 pathInMenu.Replace("&", "&&");
129
130 return wxString::Format("&%d %s", n + 1, pathInMenu);
131}
132
133} // anonymous namespace
134
0fb67cd1
VZ
135// ----------------------------------------------------------------------------
136// Definition of wxDocument
137// ----------------------------------------------------------------------------
c801d85f
KB
138
139wxDocument::wxDocument(wxDocument *parent)
140{
68379eaf 141 m_documentModified = false;
0fb67cd1 142 m_documentParent = parent;
c77049a0
VZ
143 m_documentTemplate = NULL;
144 m_commandProcessor = NULL;
68379eaf 145 m_savedYet = false;
c801d85f
KB
146}
147
0fb67cd1 148bool wxDocument::DeleteContents()
c801d85f 149{
68379eaf 150 return true;
c801d85f
KB
151}
152
0fb67cd1 153wxDocument::~wxDocument()
c801d85f 154{
0fb67cd1 155 DeleteContents();
c801d85f 156
0fb67cd1
VZ
157 if (m_commandProcessor)
158 delete m_commandProcessor;
c801d85f 159
f6bcfd97
BP
160 if (GetDocumentManager())
161 GetDocumentManager()->RemoveDocument(this);
c801d85f 162
0fb67cd1
VZ
163 // Not safe to do here, since it'll invoke virtual view functions
164 // expecting to see valid derived objects: and by the time we get here,
165 // we've called destructors higher up.
166 //DeleteAllViews();
c801d85f 167}
0fb67cd1
VZ
168
169bool wxDocument::Close()
c801d85f 170{
0fb67cd1
VZ
171 if (OnSaveModified())
172 return OnCloseDocument();
173 else
68379eaf 174 return false;
c801d85f 175}
0fb67cd1
VZ
176
177bool wxDocument::OnCloseDocument()
c801d85f 178{
b23e843b 179 // Tell all views that we're about to close
42771621 180 NotifyClosing();
0fb67cd1 181 DeleteContents();
68379eaf
WS
182 Modify(false);
183 return true;
c801d85f
KB
184}
185
0fb67cd1
VZ
186// Note that this implicitly deletes the document when the last view is
187// deleted.
188bool wxDocument::DeleteAllViews()
c801d85f 189{
f6bcfd97
BP
190 wxDocManager* manager = GetDocumentManager();
191
40702099
VZ
192 // first check if all views agree to be closed
193 const wxList::iterator end = m_documentViews.end();
194 for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
0fb67cd1 195 {
40702099
VZ
196 wxView *view = (wxView *)*i;
197 if ( !view->Close() )
68379eaf 198 return false;
0fb67cd1 199 }
d5a93fda 200
40702099
VZ
201 // all views agreed to close, now do close them
202 if ( m_documentViews.empty() )
203 {
204 // normally the document would be implicitly deleted when the last view
205 // is, but if don't have any views, do it here instead
206 if ( manager && manager->GetDocuments().Member(this) )
207 delete this;
208 }
209 else // have views
210 {
211 // as we delete elements we iterate over, don't use the usual "from
212 // begin to end" loop
213 for ( ;; )
214 {
215 wxView *view = (wxView *)*m_documentViews.begin();
216
217 bool isLastOne = m_documentViews.size() == 1;
218
219 // this always deletes the node implicitly and if this is the last
220 // view also deletes this object itself (also implicitly, great),
221 // so we can't test for m_documentViews.empty() after calling this!
222 delete view;
223
224 if ( isLastOne )
225 break;
226 }
227 }
f6bcfd97 228
68379eaf 229 return true;
c801d85f
KB
230}
231
6de2f8b9 232wxView *wxDocument::GetFirstView() const
c801d85f 233{
b1d4dd7a 234 if (m_documentViews.GetCount() == 0)
c77049a0 235 return NULL;
b1d4dd7a 236 return (wxView *)m_documentViews.GetFirst()->GetData();
c801d85f
KB
237}
238
6de2f8b9 239wxDocManager *wxDocument::GetDocumentManager() const
c801d85f 240{
c77049a0 241 return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL;
c801d85f
KB
242}
243
0fb67cd1 244bool wxDocument::OnNewDocument()
c801d85f 245{
c77049a0 246 if ( !OnSaveModified() )
68379eaf 247 return false;
c801d85f 248
0fb67cd1 249 DeleteContents();
68379eaf
WS
250 Modify(false);
251 SetDocumentSaved(false);
c801d85f 252
724b119a 253 const wxString name = GetDocumentManager()->MakeNewDocumentName();
0fb67cd1 254 SetTitle(name);
68379eaf 255 SetFilename(name, true);
0fb67cd1 256
68379eaf 257 return true;
c801d85f
KB
258}
259
0fb67cd1 260bool wxDocument::Save()
c801d85f 261{
d06a66f5 262 if (!IsModified() && m_savedYet)
68379eaf 263 return true;
c801d85f 264
d06a66f5
VZ
265 if ( m_documentFile.empty() || !m_savedYet )
266 return SaveAs();
267
268 return OnSaveDocument(m_documentFile);
c801d85f 269}
0fb67cd1
VZ
270
271bool wxDocument::SaveAs()
c801d85f 272{
ba681060
VZ
273 wxDocTemplate *docTemplate = GetDocumentTemplate();
274 if (!docTemplate)
68379eaf 275 return false;
0fb67cd1 276
04129514
JS
277#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
278 wxString filter = docTemplate->GetDescription() + wxT(" (") + docTemplate->GetFileFilter() + wxT(")|") + docTemplate->GetFileFilter();
279
280 // Now see if there are some other template with identical view and document
281 // classes, whose filters may also be used.
282
283 if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
284 {
e7b21b01 285 wxList::compatibility_iterator node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
04129514
JS
286 while (node)
287 {
288 wxDocTemplate *t = (wxDocTemplate*) node->GetData();
68379eaf 289
04129514
JS
290 if (t->IsVisible() && t != docTemplate &&
291 t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
292 t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
293 {
294 // add a '|' to separate this filter from the previous one
b494c48b 295 if ( !filter.empty() )
04129514 296 filter << wxT('|');
68379eaf 297
04129514
JS
298 filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
299 << t->GetFileFilter();
300 }
301
302 node = node->GetNext();
303 }
304 }
305#else
306 wxString filter = docTemplate->GetFileFilter() ;
307#endif
e0214f1f
JS
308 wxString defaultDir = docTemplate->GetDirectory();
309 if (defaultDir.IsEmpty())
310 defaultDir = wxPathOnly(GetFilename());
311
f6fe9f9c 312 wxString tmp = wxFileSelector(_("Save As"),
e0214f1f 313 defaultDir,
015e69f3 314 wxFileNameFromPath(GetFilename()),
0fb67cd1 315 docTemplate->GetDefaultExtension(),
04129514 316 filter,
ff3e84ff 317 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
0fb67cd1
VZ
318 GetDocumentWindow());
319
b494c48b 320 if (tmp.empty())
68379eaf 321 return false;
ba681060 322
2bb0cd28 323 wxString fileName(tmp);
88ac883a 324 wxString path, name, ext;
2bb0cd28
JS
325 wxSplitPath(fileName, & path, & name, & ext);
326
b494c48b 327 if (ext.empty())
2bb0cd28 328 {
2b5f62a0 329 fileName += wxT(".");
2bb0cd28
JS
330 fileName += docTemplate->GetDefaultExtension();
331 }
332
333 SetFilename(fileName);
334 SetTitle(wxFileNameFromPath(fileName));
0fb67cd1 335
c801d85f 336 // Notify the views that the filename has changed
222ed1d6 337 wxList::compatibility_iterator node = m_documentViews.GetFirst();
c801d85f
KB
338 while (node)
339 {
b1d4dd7a 340 wxView *view = (wxView *)node->GetData();
0fb67cd1 341 view->OnChangeFilename();
b1d4dd7a 342 node = node->GetNext();
c801d85f 343 }
ba681060 344
9d4a05be
JS
345 // Files that were not saved correctly are not added to the FileHistory.
346 if (!OnSaveDocument(m_documentFile))
68379eaf 347 return false;
9d4a05be 348
42771621 349 // A file that doesn't use the default extension of its document template cannot be opened
9d4a05be
JS
350 // via the FileHistory, so we do not add it.
351 if (docTemplate->FileMatchesTemplate(fileName))
352 {
353 GetDocumentManager()->AddFileToHistory(fileName);
354 }
355 else
356 {
357 // The user will probably not be able to open the file again, so
358 // we could warn about the wrong file-extension here.
359 }
68379eaf 360 return true;
c801d85f 361}
0fb67cd1 362
c801d85f
KB
363bool wxDocument::OnSaveDocument(const wxString& file)
364{
0fb67cd1 365 if ( !file )
68379eaf 366 return false;
c801d85f 367
69936aea 368 if ( !DoSaveDocument(file) )
68379eaf 369 return false;
0fb67cd1 370
68379eaf 371 Modify(false);
0fb67cd1 372 SetFilename(file);
68379eaf 373 SetDocumentSaved(true);
26eef304 374#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
b8afeb43
SC
375 wxFileName fn(file) ;
376 fn.MacSetDefaultTypeAndCreator() ;
377#endif
68379eaf 378 return true;
c801d85f 379}
0fb67cd1 380
c801d85f
KB
381bool wxDocument::OnOpenDocument(const wxString& file)
382{
c77049a0 383 if ( !OnSaveModified() )
68379eaf 384 return false;
c801d85f 385
69936aea 386 if ( !DoOpenDocument(file) )
68379eaf 387 return false;
0fb67cd1 388
68379eaf
WS
389 SetFilename(file, true);
390 Modify(false);
391 m_savedYet = true;
0fb67cd1
VZ
392
393 UpdateAllViews();
c801d85f 394
68379eaf 395 return true;
c801d85f 396}
0fb67cd1 397
a533f5c1 398#if wxUSE_STD_IOSTREAM
dd107c50 399wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
23a54e14
RR
400#else
401wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
402#endif
c801d85f 403{
0fb67cd1 404 return stream;
c801d85f
KB
405}
406
23a54e14 407#if wxUSE_STD_IOSTREAM
dd107c50 408wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
a533f5c1 409#else
23a54e14
RR
410wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
411#endif
a533f5c1 412{
23a54e14 413 return stream;
a533f5c1 414}
c801d85f 415
0fb67cd1 416bool wxDocument::Revert()
c801d85f 417{
68379eaf 418 return false;
c801d85f
KB
419}
420
421
422// Get title, or filename if no title, else unnamed
724b119a 423#if WXWIN_COMPATIBILITY_2_8
c801d85f
KB
424bool wxDocument::GetPrintableName(wxString& buf) const
425{
724b119a
VZ
426 // this function can not only be overridden by the user code but also
427 // called by it so we need to ensure that we return the same thing as
428 // GetUserReadableName() but we can't call it because this would result in
429 // an infinite recursion, hence we use the helper DoGetUserReadableName()
430 buf = DoGetUserReadableName();
431
432 return true;
433}
434#endif // WXWIN_COMPATIBILITY_2_8
435
436wxString wxDocument::GetUserReadableName() const
437{
438#if WXWIN_COMPATIBILITY_2_8
439 // we need to call the old virtual function to ensure that the overridden
440 // version of it is still called
441 wxString name;
442 if ( GetPrintableName(name) )
443 return name;
444#endif // WXWIN_COMPATIBILITY_2_8
445
446 return DoGetUserReadableName();
447}
448
449wxString wxDocument::DoGetUserReadableName() const
450{
451 if ( !m_documentTitle.empty() )
452 return m_documentTitle;
453
454 if ( !m_documentFile.empty() )
455 return wxFileNameFromPath(m_documentFile);
456
457 return _("unnamed");
c801d85f
KB
458}
459
6de2f8b9 460wxWindow *wxDocument::GetDocumentWindow() const
c801d85f 461{
0fb67cd1
VZ
462 wxView *view = GetFirstView();
463 if (view)
464 return view->GetFrame();
465 else
466 return wxTheApp->GetTopWindow();
c801d85f
KB
467}
468
0fb67cd1 469wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
c801d85f 470{
0fb67cd1 471 return new wxCommandProcessor;
c801d85f
KB
472}
473
68379eaf 474// true if safe to close
0fb67cd1 475bool wxDocument::OnSaveModified()
c801d85f 476{
c77049a0
VZ
477 if ( IsModified() )
478 {
479 switch ( wxMessageBox
480 (
481 wxString::Format
482 (
483 _("Do you want to save changes to document %s?"),
484 GetUserReadableName()
485 ),
486 wxTheApp->GetAppDisplayName(),
487 wxYES_NO | wxCANCEL | wxICON_QUESTION,
488 GetDocumentWindow()
489 ) )
0fb67cd1 490 {
c77049a0
VZ
491 case wxNO:
492 Modify(false);
493 break;
494
495 case wxYES:
496 return Save();
497
498 case wxCANCEL:
499 return false;
0fb67cd1 500 }
c801d85f 501 }
c77049a0 502
68379eaf 503 return true;
c801d85f
KB
504}
505
506bool wxDocument::Draw(wxDC& WXUNUSED(context))
507{
68379eaf 508 return true;
c801d85f
KB
509}
510
511bool wxDocument::AddView(wxView *view)
512{
c77049a0 513 if ( !m_documentViews.Member(view) )
0fb67cd1
VZ
514 {
515 m_documentViews.Append(view);
516 OnChangedViewList();
517 }
68379eaf 518 return true;
c801d85f
KB
519}
520
521bool wxDocument::RemoveView(wxView *view)
522{
0fb67cd1
VZ
523 (void)m_documentViews.DeleteObject(view);
524 OnChangedViewList();
68379eaf 525 return true;
c801d85f
KB
526}
527
528bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags)
529{
c77049a0 530 return GetDocumentTemplate()->CreateView(this, flags) != NULL;
c801d85f
KB
531}
532
533// Called after a view is added or removed.
534// The default implementation deletes the document if
535// there are no more views.
0fb67cd1 536void wxDocument::OnChangedViewList()
c801d85f 537{
c77049a0
VZ
538 if ( m_documentViews.empty() && OnSaveModified() )
539 delete this;
c801d85f
KB
540}
541
542void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
543{
222ed1d6 544 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
545 while (node)
546 {
b1d4dd7a 547 wxView *view = (wxView *)node->GetData();
42771621 548 if (view != sender)
2b5f62a0 549 view->OnUpdate(sender, hint);
b1d4dd7a 550 node = node->GetNext();
0fb67cd1 551 }
c801d85f
KB
552}
553
b23e843b
JS
554void wxDocument::NotifyClosing()
555{
222ed1d6 556 wxList::compatibility_iterator node = m_documentViews.GetFirst();
b23e843b
JS
557 while (node)
558 {
b1d4dd7a 559 wxView *view = (wxView *)node->GetData();
b23e843b 560 view->OnClosingDocument();
b1d4dd7a 561 node = node->GetNext();
b23e843b
JS
562 }
563}
564
c801d85f
KB
565void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
566{
0fb67cd1
VZ
567 m_documentFile = filename;
568 if ( notifyViews )
c801d85f 569 {
0fb67cd1 570 // Notify the views that the filename has changed
222ed1d6 571 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
572 while (node)
573 {
b1d4dd7a 574 wxView *view = (wxView *)node->GetData();
0fb67cd1 575 view->OnChangeFilename();
b1d4dd7a 576 node = node->GetNext();
0fb67cd1 577 }
c801d85f 578 }
c801d85f
KB
579}
580
69936aea
VZ
581bool wxDocument::DoSaveDocument(const wxString& file)
582{
583 wxString msgTitle;
9cf3d218
VZ
584 if (!wxTheApp->GetAppDisplayName().empty())
585 msgTitle = wxTheApp->GetAppDisplayName();
69936aea
VZ
586 else
587 msgTitle = wxString(_("File error"));
588
589#if wxUSE_STD_IOSTREAM
53e2e87c 590 wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
69936aea
VZ
591 if (store.fail() || store.bad())
592#else
593 wxFileOutputStream store(file);
594 if (store.GetLastError() != wxSTREAM_NO_ERROR)
595#endif
596 {
597 (void)wxMessageBox(_("Sorry, could not open this file for saving."), msgTitle, wxOK | wxICON_EXCLAMATION,
598 GetDocumentWindow());
599 // Saving error
68379eaf 600 return false;
69936aea
VZ
601 }
602 if (!SaveObject(store))
603 {
604 (void)wxMessageBox(_("Sorry, could not save this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
605 GetDocumentWindow());
606 // Saving error
68379eaf 607 return false;
69936aea
VZ
608 }
609
68379eaf 610 return true;
69936aea
VZ
611}
612
613bool wxDocument::DoOpenDocument(const wxString& file)
614{
69936aea 615#if wxUSE_STD_IOSTREAM
53e2e87c 616 wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
64c12a6d 617 if (!store.fail() && !store.bad())
69936aea
VZ
618#else
619 wxFileInputStream store(file);
64c12a6d 620 if (store.GetLastError() == wxSTREAM_NO_ERROR)
69936aea
VZ
621#endif
622 {
69936aea 623#if wxUSE_STD_IOSTREAM
64c12a6d
VZ
624 LoadObject(store);
625 if ( !!store || store.eof() )
69936aea 626#else
64c12a6d
VZ
627 int res = LoadObject(store).GetLastError();
628 if ( res == wxSTREAM_NO_ERROR || res == wxSTREAM_EOF )
69936aea 629#endif
64c12a6d 630 return true;
69936aea
VZ
631 }
632
64c12a6d
VZ
633 wxLogError(_("Sorry, could not open this file."));
634 return false;
69936aea
VZ
635}
636
637
0fb67cd1
VZ
638// ----------------------------------------------------------------------------
639// Document view
640// ----------------------------------------------------------------------------
c801d85f 641
dbdb39b2 642wxView::wxView()
c801d85f 643{
c77049a0 644 m_viewDocument = NULL;
0fb67cd1 645
c77049a0 646 m_viewFrame = NULL;
c801d85f
KB
647}
648
0fb67cd1 649wxView::~wxView()
c801d85f 650{
68379eaf 651 GetDocumentManager()->ActivateView(this, false);
0fb67cd1 652 m_viewDocument->RemoveView(this);
c801d85f
KB
653}
654
655// Extend event processing to search the document's event table
656bool wxView::ProcessEvent(wxEvent& event)
657{
0fb67cd1
VZ
658 if ( !GetDocument() || !GetDocument()->ProcessEvent(event) )
659 return wxEvtHandler::ProcessEvent(event);
18759f78 660
68379eaf 661 return true;
c801d85f
KB
662}
663
664void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView))
665{
666}
667
668void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
669{
0fb67cd1 670 OnDraw(dc);
c801d85f
KB
671}
672
673void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
674{
675}
676
0fb67cd1 677void wxView::OnChangeFilename()
c801d85f 678{
faa49bfd
WS
679 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
680 // generic MDI implementation so use SetLabel rather than SetTitle.
681 // It should cause SetTitle() for top level windows.
682 wxWindow *win = GetFrame();
683 if (!win) return;
f2506310 684
faa49bfd
WS
685 wxDocument *doc = GetDocument();
686 if (!doc) return;
c801d85f 687
724b119a 688 win->SetLabel(doc->GetUserReadableName());
c801d85f
KB
689}
690
691void wxView::SetDocument(wxDocument *doc)
692{
0fb67cd1
VZ
693 m_viewDocument = doc;
694 if (doc)
695 doc->AddView(this);
c801d85f
KB
696}
697
698bool wxView::Close(bool deleteWindow)
699{
c77049a0 700 return OnClose(deleteWindow);
c801d85f
KB
701}
702
703void wxView::Activate(bool activate)
704{
bb28b477 705 if (GetDocument() && GetDocumentManager())
0fb67cd1
VZ
706 {
707 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
708 GetDocumentManager()->ActivateView(this, activate);
709 }
c801d85f
KB
710}
711
712bool wxView::OnClose(bool WXUNUSED(deleteWindow))
713{
68379eaf 714 return GetDocument() ? GetDocument()->Close() : true;
c801d85f
KB
715}
716
47d67540 717#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1 718wxPrintout *wxView::OnCreatePrintout()
c801d85f 719{
0fb67cd1 720 return new wxDocPrintout(this);
c801d85f 721}
6de2f8b9 722#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 723
0fb67cd1
VZ
724// ----------------------------------------------------------------------------
725// wxDocTemplate
726// ----------------------------------------------------------------------------
c801d85f 727
0fb67cd1
VZ
728wxDocTemplate::wxDocTemplate(wxDocManager *manager,
729 const wxString& descr,
730 const wxString& filter,
731 const wxString& dir,
732 const wxString& ext,
733 const wxString& docTypeName,
734 const wxString& viewTypeName,
735 wxClassInfo *docClassInfo,
736 wxClassInfo *viewClassInfo,
737 long flags)
c801d85f 738{
0fb67cd1 739 m_documentManager = manager;
0fb67cd1
VZ
740 m_description = descr;
741 m_directory = dir;
742 m_defaultExt = ext;
743 m_fileFilter = filter;
744 m_flags = flags;
745 m_docTypeName = docTypeName;
746 m_viewTypeName = viewTypeName;
747 m_documentManager->AssociateTemplate(this);
c801d85f 748
0fb67cd1
VZ
749 m_docClassInfo = docClassInfo;
750 m_viewClassInfo = viewClassInfo;
c801d85f
KB
751}
752
0fb67cd1 753wxDocTemplate::~wxDocTemplate()
c801d85f 754{
0fb67cd1 755 m_documentManager->DisassociateTemplate(this);
c801d85f 756}
0fb67cd1
VZ
757
758// Tries to dynamically construct an object of the right class.
c801d85f
KB
759wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
760{
c77049a0 761 wxDocument * const doc = DoCreateDocument();
68379eaf 762
c77049a0
VZ
763 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
764 // fails, is this intentional?
765
766 return doc && InitDocument(doc, path, flags) ? doc : NULL;
217b7140
JS
767}
768
c77049a0
VZ
769bool
770wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
217b7140 771{
0fb67cd1
VZ
772 doc->SetFilename(path);
773 doc->SetDocumentTemplate(this);
774 GetDocumentManager()->AddDocument(doc);
775 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
776
777 if (doc->OnCreate(path, flags))
217b7140 778 return true;
0fb67cd1
VZ
779 else
780 {
f6bcfd97
BP
781 if (GetDocumentManager()->GetDocuments().Member(doc))
782 doc->DeleteAllViews();
217b7140 783 return false;
0fb67cd1 784 }
c801d85f
KB
785}
786
787wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
788{
69936aea
VZ
789 wxView *view = DoCreateView();
790 if ( view == NULL )
c77049a0 791 return NULL;
69936aea 792
0fb67cd1
VZ
793 view->SetDocument(doc);
794 if (view->OnCreate(doc, flags))
795 {
796 return view;
797 }
798 else
799 {
800 delete view;
c77049a0 801 return NULL;
0fb67cd1 802 }
c801d85f
KB
803}
804
6de2f8b9
VZ
805// The default (very primitive) format detection: check is the extension is
806// that of the template
807bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
808{
0f30d8e3
WS
809 wxStringTokenizer parser (GetFileFilter(), wxT(";"));
810 wxString anything = wxT ("*");
811 while (parser.HasMoreTokens())
812 {
813 wxString filter = parser.GetNextToken();
814 wxString filterExt = FindExtension (filter);
815 if ( filter.IsSameAs (anything) ||
816 filterExt.IsSameAs (anything) ||
817 filterExt.IsSameAs (FindExtension (path)) )
818 return true;
819 }
6de2f8b9
VZ
820 return GetDefaultExtension().IsSameAs(FindExtension(path));
821}
822
69936aea
VZ
823wxDocument *wxDocTemplate::DoCreateDocument()
824{
825 if (!m_docClassInfo)
c77049a0 826 return NULL;
69936aea
VZ
827
828 return (wxDocument *)m_docClassInfo->CreateObject();
829}
830
831wxView *wxDocTemplate::DoCreateView()
832{
833 if (!m_viewClassInfo)
c77049a0 834 return NULL;
69936aea
VZ
835
836 return (wxView *)m_viewClassInfo->CreateObject();
837}
838
0fb67cd1
VZ
839// ----------------------------------------------------------------------------
840// wxDocManager
841// ----------------------------------------------------------------------------
842
c801d85f
KB
843BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
844 EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen)
845 EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose)
33a20136 846 EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll)
c801d85f
KB
847 EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert)
848 EVT_MENU(wxID_NEW, wxDocManager::OnFileNew)
849 EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave)
850 EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs)
851 EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
852 EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
f2506310
JS
853
854 EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
c77049a0
VZ
855 EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc)
856 EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc)
857 EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateDisableIfNoDoc)
f2506310
JS
858 EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
859 EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
c77049a0 860 EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateDisableIfNoDoc)
f2506310
JS
861 EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
862 EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
863
ce4169a4 864#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 865 EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
c801d85f 866 EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
f2506310 867
c77049a0
VZ
868 EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdateDisableIfNoDoc)
869 EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdateDisableIfNoDoc)
ce4169a4 870#endif
c801d85f
KB
871END_EVENT_TABLE()
872
c77049a0 873wxDocManager* wxDocManager::sm_docManager = NULL;
f2506310 874
c77049a0 875wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize)
c801d85f 876{
c77049a0
VZ
877 wxASSERT_MSG( !sm_docManager, "multiple wxDocManagers not allowed" );
878
879 sm_docManager = this;
880
0fb67cd1 881 m_defaultDocumentNameCounter = 1;
c77049a0
VZ
882 m_currentView = NULL;
883 m_maxDocsOpen = INT_MAX;
884 m_fileHistory = NULL;
885 if ( initialize )
0fb67cd1 886 Initialize();
c801d85f
KB
887}
888
0fb67cd1 889wxDocManager::~wxDocManager()
c801d85f 890{
0fb67cd1 891 Clear();
c77049a0
VZ
892 delete m_fileHistory;
893 sm_docManager = NULL;
c801d85f
KB
894}
895
b72b1920
JS
896// closes the specified document
897bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
898{
899 if (doc->Close() || force)
900 {
901 // Implicitly deletes the document when
902 // the last view is deleted
903 doc->DeleteAllViews();
904
905 // Check we're really deleted
906 if (m_docs.Member(doc))
907 delete doc;
42771621 908
68379eaf 909 return true;
b72b1920 910 }
68379eaf 911 return false;
b72b1920
JS
912}
913
33a20136 914bool wxDocManager::CloseDocuments(bool force)
c801d85f 915{
222ed1d6 916 wxList::compatibility_iterator node = m_docs.GetFirst();
0fb67cd1
VZ
917 while (node)
918 {
b1d4dd7a 919 wxDocument *doc = (wxDocument *)node->GetData();
222ed1d6 920 wxList::compatibility_iterator next = node->GetNext();
68379eaf 921
b72b1920 922 if (!CloseDocument(doc, force))
68379eaf 923 return false;
0fb67cd1 924
0fb67cd1
VZ
925 // This assumes that documents are not connected in
926 // any way, i.e. deleting one document does NOT
927 // delete another.
928 node = next;
929 }
68379eaf 930 return true;
33a20136
VZ
931}
932
933bool wxDocManager::Clear(bool force)
934{
935 if (!CloseDocuments(force))
68379eaf 936 return false;
33a20136 937
8af94d3b
VZ
938 m_currentView = NULL;
939
222ed1d6 940 wxList::compatibility_iterator node = m_templates.GetFirst();
0fb67cd1
VZ
941 while (node)
942 {
b1d4dd7a 943 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
222ed1d6 944 wxList::compatibility_iterator next = node->GetNext();
c801d85f
KB
945 delete templ;
946 node = next;
0fb67cd1 947 }
68379eaf 948 return true;
c801d85f
KB
949}
950
0fb67cd1 951bool wxDocManager::Initialize()
c801d85f 952{
0fb67cd1 953 m_fileHistory = OnCreateFileHistory();
68379eaf 954 return true;
c801d85f
KB
955}
956
0fb67cd1 957wxFileHistory *wxDocManager::OnCreateFileHistory()
c801d85f 958{
0fb67cd1 959 return new wxFileHistory;
c801d85f
KB
960}
961
962void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
963{
0fb67cd1
VZ
964 wxDocument *doc = GetCurrentDocument();
965 if (!doc)
966 return;
967 if (doc->Close())
968 {
969 doc->DeleteAllViews();
970 if (m_docs.Member(doc))
971 delete doc;
972 }
c801d85f
KB
973}
974
33a20136
VZ
975void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
976{
68379eaf 977 CloseDocuments(false);
33a20136
VZ
978}
979
c801d85f
KB
980void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
981{
b494c48b 982 CreateDocument( wxEmptyString, wxDOC_NEW );
c801d85f
KB
983}
984
985void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
986{
b494c48b 987 if ( !CreateDocument( wxEmptyString, 0) )
5f170f33
VZ
988 {
989 OnOpenFileFailure();
990 }
c801d85f
KB
991}
992
993void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
994{
0fb67cd1
VZ
995 wxDocument *doc = GetCurrentDocument();
996 if (!doc)
997 return;
998 doc->Revert();
c801d85f
KB
999}
1000
1001void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
1002{
0fb67cd1
VZ
1003 wxDocument *doc = GetCurrentDocument();
1004 if (!doc)
1005 return;
1006 doc->Save();
c801d85f
KB
1007}
1008
1009void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
1010{
0fb67cd1
VZ
1011 wxDocument *doc = GetCurrentDocument();
1012 if (!doc)
1013 return;
1014 doc->SaveAs();
c801d85f
KB
1015}
1016
1017void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
1018{
88ac883a 1019#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
1020 wxView *view = GetCurrentView();
1021 if (!view)
1022 return;
c801d85f 1023
0fb67cd1
VZ
1024 wxPrintout *printout = view->OnCreatePrintout();
1025 if (printout)
1026 {
1027 wxPrinter printer;
68379eaf 1028 printer.Print(view->GetFrame(), printout, true);
c801d85f 1029
0fb67cd1
VZ
1030 delete printout;
1031 }
88ac883a 1032#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1033}
1034
c801d85f
KB
1035void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
1036{
88ac883a 1037#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
1038 wxView *view = GetCurrentView();
1039 if (!view)
1040 return;
c801d85f 1041
0fb67cd1
VZ
1042 wxPrintout *printout = view->OnCreatePrintout();
1043 if (printout)
1044 {
1045 // Pass two printout objects: for preview, and possible printing.
999836aa 1046 wxPrintPreviewBase *preview = new wxPrintPreview(printout, view->OnCreatePrintout());
3f8b4a3f
JS
1047 if ( !preview->Ok() )
1048 {
1049 delete preview;
1050 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1051 return;
1052 }
0fb67cd1
VZ
1053
1054 wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
1055 wxPoint(100, 100), wxSize(600, 650));
1056 frame->Centre(wxBOTH);
1057 frame->Initialize();
68379eaf 1058 frame->Show(true);
0fb67cd1 1059 }
88ac883a 1060#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1061}
1062
34aa6140 1063void wxDocManager::OnUndo(wxCommandEvent& event)
c801d85f 1064{
0fb67cd1
VZ
1065 wxDocument *doc = GetCurrentDocument();
1066 if (!doc)
1067 return;
1068 if (doc->GetCommandProcessor())
1069 doc->GetCommandProcessor()->Undo();
34aa6140
JS
1070 else
1071 event.Skip();
c801d85f
KB
1072}
1073
34aa6140 1074void wxDocManager::OnRedo(wxCommandEvent& event)
c801d85f 1075{
0fb67cd1
VZ
1076 wxDocument *doc = GetCurrentDocument();
1077 if (!doc)
1078 return;
1079 if (doc->GetCommandProcessor())
1080 doc->GetCommandProcessor()->Redo();
34aa6140
JS
1081 else
1082 event.Skip();
c801d85f
KB
1083}
1084
f2506310
JS
1085// Handlers for UI update commands
1086
1087void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1088{
68379eaf 1089 event.Enable( true );
f2506310
JS
1090}
1091
c77049a0 1092void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
f2506310 1093{
c77049a0 1094 event.Enable( GetCurrentDocument() != NULL );
f2506310
JS
1095}
1096
1097void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1098{
68379eaf 1099 event.Enable( true );
f2506310
JS
1100}
1101
1102void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1103{
1104 wxDocument *doc = GetCurrentDocument();
9cb0033c 1105 event.Enable( doc && doc->IsModified() );
f2506310
JS
1106}
1107
f2506310
JS
1108void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1109{
1110 wxDocument *doc = GetCurrentDocument();
34aa6140 1111 if (!doc)
68379eaf 1112 event.Enable(false);
34aa6140
JS
1113 else if (!doc->GetCommandProcessor())
1114 event.Skip();
1115 else
1116 {
1117 event.Enable( doc->GetCommandProcessor()->CanUndo() );
2599ee02 1118 doc->GetCommandProcessor()->SetMenuStrings();
34aa6140 1119 }
f2506310
JS
1120}
1121
1122void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1123{
1124 wxDocument *doc = GetCurrentDocument();
34aa6140 1125 if (!doc)
68379eaf 1126 event.Enable(false);
34aa6140
JS
1127 else if (!doc->GetCommandProcessor())
1128 event.Skip();
1129 else
1130 {
1131 event.Enable( doc->GetCommandProcessor()->CanRedo() );
2599ee02 1132 doc->GetCommandProcessor()->SetMenuStrings();
34aa6140 1133 }
f2506310
JS
1134}
1135
6de2f8b9 1136wxView *wxDocManager::GetCurrentView() const
637f467a
JS
1137{
1138 if (m_currentView)
1139 return m_currentView;
b1d4dd7a 1140 if (m_docs.GetCount() == 1)
637f467a 1141 {
b1d4dd7a 1142 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
637f467a
JS
1143 return doc->GetFirstView();
1144 }
c77049a0 1145 return NULL;
637f467a
JS
1146}
1147
1148// Extend event processing to search the view's event table
1149bool wxDocManager::ProcessEvent(wxEvent& event)
1150{
c77049a0
VZ
1151 wxView * const view = GetCurrentView();
1152 if ( view && view->ProcessEvent(event) )
1153 return true;
1154
637f467a
JS
1155 return wxEvtHandler::ProcessEvent(event);
1156}
1157
c77049a0 1158namespace
c801d85f 1159{
b1d4dd7a 1160
c77049a0
VZ
1161// helper function: return only the visible templates
1162wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
1163{
1164 // select only the visible templates
1165 const size_t totalNumTemplates = allTemplates.GetCount();
1166 wxDocTemplates templates;
1167 if ( totalNumTemplates )
c801d85f 1168 {
c77049a0
VZ
1169 templates.reserve(totalNumTemplates);
1170
1171 for ( wxList::const_iterator i = allTemplates.begin(),
1172 end = allTemplates.end();
1173 i != end;
1174 ++i )
0fb67cd1 1175 {
c77049a0
VZ
1176 wxDocTemplate * const temp = (wxDocTemplate *)*i;
1177 if ( temp->IsVisible() )
1178 templates.push_back(temp);
0fb67cd1 1179 }
c801d85f 1180 }
42771621 1181
c77049a0
VZ
1182 return templates;
1183}
0fb67cd1 1184
c77049a0
VZ
1185} // anonymous namespace
1186
1187wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
1188{
1189 // this ought to be const but SelectDocumentType/Path() are not
1190 // const-correct and can't be changed as, being virtual, this risks
1191 // breaking user code overriding them
1192 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1193 const size_t numTemplates = templates.size();
1194 if ( !numTemplates )
c801d85f 1195 {
c77049a0
VZ
1196 // no templates can be used, can't create document
1197 return NULL;
c801d85f
KB
1198 }
1199
42771621 1200
c77049a0
VZ
1201 // normally user should select the template to use but wxDOC_SILENT flag we
1202 // choose one ourselves
1203 wxString path = pathOrig; // may be modified below
1204 wxDocTemplate *temp;
1205 if ( flags & wxDOC_SILENT )
1206 {
1207 wxASSERT_MSG( !path.empty(),
1208 "using empty path with wxDOC_SILENT doesn't make sense" );
42771621 1209
c77049a0
VZ
1210 temp = FindTemplateForPath(path);
1211 if ( !temp )
1212 {
1213 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1214 path);
0fb67cd1 1215 }
c77049a0
VZ
1216 }
1217 else // not silent, ask the user
1218 {
1219 // for the new file we need just the template, for an existing one we
1220 // need the template and the path, unless it's already specified
1221 if ( (flags & wxDOC_NEW) || !path.empty() )
1222 temp = SelectDocumentType(&templates[0], numTemplates);
1223 else
1224 temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
1225 }
0fb67cd1 1226
c77049a0
VZ
1227 if ( !temp )
1228 return NULL;
42771621 1229
c77049a0
VZ
1230 // check whether the document with this path is already opened
1231 if ( !path.empty() )
1232 {
1233 const wxFileName fn(path);
1234 for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
1235 {
1236 wxDocument * const doc = (wxDocument*)*i;
b72b1920 1237
c77049a0 1238 if ( fn == doc->GetFilename() )
0fb67cd1 1239 {
c77049a0
VZ
1240 // file already open, just activate it and return
1241 if ( doc->GetFirstView() )
f5729a6b 1242 {
c77049a0
VZ
1243 ActivateView(doc->GetFirstView());
1244 if ( doc->GetDocumentWindow() )
1245 doc->GetDocumentWindow()->SetFocus();
1246 return doc;
f5729a6b 1247 }
0fb67cd1 1248 }
0fb67cd1 1249 }
c801d85f 1250 }
c801d85f 1251
c801d85f 1252
c77049a0 1253 // no, we need to create a new document
c801d85f 1254
c77049a0
VZ
1255
1256 // if we've reached the max number of docs, close the first one.
1257 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
9d4a05be 1258 {
c77049a0 1259 if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
9d4a05be 1260 {
c77049a0
VZ
1261 // can't open the new document if closing the old one failed
1262 return NULL;
9d4a05be
JS
1263 }
1264 }
c801d85f 1265
c801d85f 1266
c77049a0
VZ
1267 // do create and initialize the new document finally
1268 wxDocument * const docNew = temp->CreateDocument(path, flags);
1269 if ( !docNew )
1270 return NULL;
42771621 1271
c77049a0
VZ
1272 docNew->SetDocumentName(temp->GetDocumentName());
1273 docNew->SetDocumentTemplate(temp);
6489e7ee 1274
c77049a0
VZ
1275 // call the appropriate function depending on whether we're creating a new
1276 // file or opening an existing one
1277 if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
1278 : docNew->OnOpenDocument(path)) )
1279 {
1280 // Document is implicitly deleted by DeleteAllViews
1281 docNew->DeleteAllViews();
1282 return NULL;
c801d85f 1283 }
3ca6a5f0 1284
c77049a0
VZ
1285 // add the successfully opened file to MRU, but only if we're going to be
1286 // able to reopen it successfully later which requires the template for
1287 // this document to be retrievable from the file extension
1288 if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
1289 AddFileToHistory(path);
1290
1291 return docNew;
c801d85f
KB
1292}
1293
1294wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1295{
c77049a0
VZ
1296 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1297 const size_t numTemplates = templates.size();
b1d4dd7a 1298
c77049a0
VZ
1299 if ( numTemplates == 0 )
1300 return NULL;
0fb67cd1 1301
c77049a0
VZ
1302 wxDocTemplate * const
1303 temp = numTemplates == 1 ? templates[0]
1304 : SelectViewType(&templates[0], numTemplates);
1305
1306 if ( !temp )
1307 return NULL;
1308
1309 wxView *view = temp->CreateView(doc, flags);
1310 if ( view )
1311 view->SetViewName(temp->GetViewName());
1312 return view;
c801d85f
KB
1313}
1314
1315// Not yet implemented
c77049a0
VZ
1316void
1317wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
c801d85f
KB
1318{
1319}
1320
1321// Not yet implemented
1322bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1323{
68379eaf 1324 return false;
c801d85f
KB
1325}
1326
6de2f8b9 1327wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1328{
9cb0033c
VZ
1329 wxView *view = GetCurrentView();
1330 if (view)
1331 return view->GetDocument();
0fb67cd1 1332 else
c77049a0 1333 return NULL;
c801d85f
KB
1334}
1335
724b119a
VZ
1336// Make a default name for a new document
1337#if WXWIN_COMPATIBILITY_2_8
1338bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1339{
1340 // we consider that this function can only be overridden by the user code,
1341 // not called by it as it only makes sense to call it internally, so we
1342 // don't bother to return anything from here
1343 return false;
1344}
1345#endif // WXWIN_COMPATIBILITY_2_8
1346
1347wxString wxDocManager::MakeNewDocumentName()
c801d85f 1348{
724b119a 1349 wxString name;
53c6e7cc 1350
724b119a
VZ
1351#if WXWIN_COMPATIBILITY_2_8
1352 if ( !MakeDefaultName(name) )
1353#endif // WXWIN_COMPATIBILITY_2_8
1354 {
1355 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1356 m_defaultDocumentNameCounter++;
1357 }
1358
1359 return name;
c801d85f
KB
1360}
1361
f2506310
JS
1362// Make a frame title (override this to do something different)
1363// If docName is empty, a document is not currently active.
1364wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1365{
9cf3d218 1366 wxString appName = wxTheApp->GetAppDisplayName();
f2506310
JS
1367 wxString title;
1368 if (!doc)
1369 title = appName;
1370 else
1371 {
724b119a 1372 wxString docName = doc->GetUserReadableName();
f2506310
JS
1373 title = docName + wxString(_(" - ")) + appName;
1374 }
1375 return title;
1376}
1377
1378
c801d85f
KB
1379// Not yet implemented
1380wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1381{
c77049a0 1382 return NULL;
c801d85f
KB
1383}
1384
1385// File history management
1386void wxDocManager::AddFileToHistory(const wxString& file)
1387{
0fb67cd1
VZ
1388 if (m_fileHistory)
1389 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1390}
1391
e49c85af 1392void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1393{
1394 if (m_fileHistory)
1395 m_fileHistory->RemoveFileFromHistory(i);
1396}
1397
e49c85af 1398wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1399{
0fb67cd1
VZ
1400 wxString histFile;
1401
1402 if (m_fileHistory)
1403 histFile = m_fileHistory->GetHistoryFile(i);
1404
1405 return histFile;
c801d85f
KB
1406}
1407
1408void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1409{
0fb67cd1
VZ
1410 if (m_fileHistory)
1411 m_fileHistory->UseMenu(menu);
c801d85f
KB
1412}
1413
7f555861 1414void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1415{
0fb67cd1
VZ
1416 if (m_fileHistory)
1417 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1418}
1419
702ca7c0 1420#if wxUSE_CONFIG
9c8116f8 1421void wxDocManager::FileHistoryLoad(const wxConfigBase& config)
c801d85f 1422{
0fb67cd1
VZ
1423 if (m_fileHistory)
1424 m_fileHistory->Load(config);
7f555861
JS
1425}
1426
1427void wxDocManager::FileHistorySave(wxConfigBase& config)
1428{
0fb67cd1
VZ
1429 if (m_fileHistory)
1430 m_fileHistory->Save(config);
7f555861 1431}
ac57418f 1432#endif
7f555861
JS
1433
1434void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1435{
0fb67cd1
VZ
1436 if (m_fileHistory)
1437 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1438}
1439
1440void wxDocManager::FileHistoryAddFilesToMenu()
1441{
0fb67cd1
VZ
1442 if (m_fileHistory)
1443 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1444}
1445
7af6b69e 1446size_t wxDocManager::GetHistoryFilesCount() const
c801d85f 1447{
7af6b69e 1448 return m_fileHistory ? m_fileHistory->GetCount() : 0;
c801d85f
KB
1449}
1450
1451
6de2f8b9
VZ
1452// Find out the document template via matching in the document file format
1453// against that of the template
c801d85f
KB
1454wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1455{
c77049a0 1456 wxDocTemplate *theTemplate = NULL;
c801d85f 1457
0fb67cd1 1458 // Find the template which this extension corresponds to
b1d4dd7a 1459 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1460 {
b1d4dd7a 1461 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1462 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1463 {
1464 theTemplate = temp;
1465 break;
1466 }
c801d85f 1467 }
0fb67cd1 1468 return theTemplate;
c801d85f
KB
1469}
1470
1471// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1472// Must extend the file selector dialog or implement own; OR
1473// match the extension to the template extension.
df875e59 1474
c801d85f 1475wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
0fb67cd1
VZ
1476 int noTemplates,
1477 wxString& path,
1478 long WXUNUSED(flags),
1479 bool WXUNUSED(save))
c801d85f 1480{
bf9e3e73 1481 // We can only have multiple filters in Windows and GTK
ce68ad25 1482#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
ba681060
VZ
1483 wxString descrBuf;
1484
1485 int i;
1486 for (i = 0; i < noTemplates; i++)
c801d85f 1487 {
ba681060
VZ
1488 if (templates[i]->IsVisible())
1489 {
1490 // add a '|' to separate this filter from the previous one
b494c48b 1491 if ( !descrBuf.empty() )
223d09f6 1492 descrBuf << wxT('|');
ba681060
VZ
1493
1494 descrBuf << templates[i]->GetDescription()
223d09f6 1495 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1496 << templates[i]->GetFileFilter();
ba681060 1497 }
c801d85f 1498 }
a4294b78 1499#else
223d09f6 1500 wxString descrBuf = wxT("*.*");
c77049a0 1501 wxUnusedVar(noTemplates);
a4294b78 1502#endif
c801d85f 1503
3ca6a5f0 1504 int FilterIndex = -1;
f6bcfd97
BP
1505
1506 wxWindow* parent = wxFindSuitableParent();
1507
f6fe9f9c 1508 wxString pathTmp = wxFileSelectorEx(_("Open File"),
ac0ac824 1509 m_lastDirectory,
b494c48b 1510 wxEmptyString,
6de2f8b9 1511 &FilterIndex,
caf0debf
VZ
1512 descrBuf,
1513 0,
f6bcfd97 1514 parent);
ba681060 1515
c77049a0 1516 wxDocTemplate *theTemplate = NULL;
b494c48b 1517 if (!pathTmp.empty())
0fb67cd1 1518 {
f6bcfd97
BP
1519 if (!wxFileExists(pathTmp))
1520 {
1521 wxString msgTitle;
9cf3d218
VZ
1522 if (!wxTheApp->GetAppDisplayName().empty())
1523 msgTitle = wxTheApp->GetAppDisplayName();
f6bcfd97
BP
1524 else
1525 msgTitle = wxString(_("File error"));
02718e6a 1526
f6bcfd97
BP
1527 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1528 parent);
1529
b494c48b 1530 path = wxEmptyString;
c77049a0 1531 return NULL;
f6bcfd97 1532 }
ac0ac824
JS
1533 m_lastDirectory = wxPathOnly(pathTmp);
1534
0fb67cd1 1535 path = pathTmp;
0fb67cd1 1536
3ca6a5f0
BP
1537 // first choose the template using the extension, if this fails (i.e.
1538 // wxFileSelectorEx() didn't fill it), then use the path
1539 if ( FilterIndex != -1 )
6de2f8b9 1540 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1541 if ( !theTemplate )
1542 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1543 if ( !theTemplate )
1544 {
42771621 1545 // Since we do not add files with non-default extensions to the FileHistory this
9d4a05be
JS
1546 // can only happen if the application changes the allowed templates in runtime.
1547 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
42771621 1548 _("Open File"),
9d4a05be
JS
1549 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1550 }
0fb67cd1
VZ
1551 }
1552 else
1553 {
b494c48b 1554 path = wxEmptyString;
0fb67cd1 1555 }
3ca6a5f0
BP
1556
1557 return theTemplate;
c801d85f
KB
1558}
1559
1560wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1561 int noTemplates, bool sort)
0fb67cd1 1562{
222ed1d6 1563 wxArrayString strings;
17260efd 1564 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1565 int i;
1566 int n = 0;
42771621
VZ
1567
1568 for (i = 0; i < noTemplates; i++)
1569 {
1570 if (templates[i]->IsVisible())
1571 {
1572 int j;
68379eaf 1573 bool want = true;
42771621
VZ
1574 for (j = 0; j < n; j++)
1575 {
bb28b477 1576 //filter out NOT unique documents + view combinations
42771621 1577 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
bb28b477
JS
1578 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1579 )
68379eaf 1580 want = false;
42771621 1581 }
bb28b477
JS
1582
1583 if ( want )
42771621
VZ
1584 {
1585 strings.Add(templates[i]->m_description);
1586
1587 data[n] = templates[i];
1588 n ++;
1589 }
1590 }
1591 } // for
1592
1593 if (sort)
1594 {
a2a03d78 1595 strings.Sort(); // ascending sort
42771621
VZ
1596 // Yes, this will be slow, but template lists
1597 // are typically short.
1598 int j;
1599 n = strings.Count();
1600 for (i = 0; i < n; i++)
1601 {
1602 for (j = 0; j < noTemplates; j++)
1603 {
1604 if (strings[i] == templates[j]->m_description)
1605 data[i] = templates[j];
1606 }
1607 }
1608 }
17260efd
VZ
1609
1610 wxDocTemplate *theTemplate;
1611
1612 switch ( n )
0fb67cd1 1613 {
17260efd
VZ
1614 case 0:
1615 // no visible templates, hence nothing to choose from
1616 theTemplate = NULL;
1617 break;
0fb67cd1 1618
17260efd 1619 case 1:
c77049a0 1620 // don't propose the user to choose if he has no choice
17260efd
VZ
1621 theTemplate = data[0];
1622 break;
1623
1624 default:
1625 // propose the user to choose one of several
1626 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1627 (
1628 _("Select a document template"),
1629 _("Templates"),
1630 strings,
1631 (void **)data,
1632 wxFindSuitableParent()
1633 );
1634 }
f6bcfd97 1635
c801d85f 1636 delete[] data;
17260efd 1637
0fb67cd1 1638 return theTemplate;
c801d85f
KB
1639}
1640
1641wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1642 int noTemplates, bool sort)
c801d85f 1643{
222ed1d6 1644 wxArrayString strings;
17260efd 1645 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1646 int i;
1647 int n = 0;
42771621 1648
0fb67cd1 1649 for (i = 0; i < noTemplates; i++)
c801d85f 1650 {
17260efd
VZ
1651 wxDocTemplate *templ = templates[i];
1652 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1653 {
42771621 1654 int j;
68379eaf 1655 bool want = true;
42771621
VZ
1656 for (j = 0; j < n; j++)
1657 {
bb28b477 1658 //filter out NOT unique views
42771621 1659 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
68379eaf 1660 want = false;
42771621 1661 }
bb28b477
JS
1662
1663 if ( want )
1664 {
42771621
VZ
1665 strings.Add(templ->m_viewTypeName);
1666 data[n] = templ;
1667 n ++;
1668 }
0fb67cd1 1669 }
c801d85f 1670 }
f6bcfd97 1671
42771621
VZ
1672 if (sort)
1673 {
a2a03d78 1674 strings.Sort(); // ascending sort
42771621
VZ
1675 // Yes, this will be slow, but template lists
1676 // are typically short.
1677 int j;
1678 n = strings.Count();
1679 for (i = 0; i < n; i++)
1680 {
1681 for (j = 0; j < noTemplates; j++)
1682 {
1683 if (strings[i] == templates[j]->m_viewTypeName)
1684 data[i] = templates[j];
1685 }
1686 }
1687 }
8658ef93 1688
17260efd
VZ
1689 wxDocTemplate *theTemplate;
1690
1691 // the same logic as above
1692 switch ( n )
1693 {
1694 case 0:
c77049a0 1695 theTemplate = NULL;
17260efd
VZ
1696 break;
1697
1698 case 1:
1699 theTemplate = data[0];
1700 break;
1701
1702 default:
1703 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1704 (
1705 _("Select a document view"),
1706 _("Views"),
1707 strings,
1708 (void **)data,
1709 wxFindSuitableParent()
1710 );
1711
1712 }
1713
0fb67cd1
VZ
1714 delete[] data;
1715 return theTemplate;
c801d85f
KB
1716}
1717
1718void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1719{
0fb67cd1
VZ
1720 if (!m_templates.Member(temp))
1721 m_templates.Append(temp);
c801d85f
KB
1722}
1723
1724void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1725{
0fb67cd1 1726 m_templates.DeleteObject(temp);
c801d85f
KB
1727}
1728
1729// Add and remove a document from the manager's list
1730void wxDocManager::AddDocument(wxDocument *doc)
1731{
0fb67cd1
VZ
1732 if (!m_docs.Member(doc))
1733 m_docs.Append(doc);
c801d85f
KB
1734}
1735
1736void wxDocManager::RemoveDocument(wxDocument *doc)
1737{
0fb67cd1 1738 m_docs.DeleteObject(doc);
c801d85f
KB
1739}
1740
1741// Views or windows should inform the document manager
1742// when a view is going in or out of focus
18759f78
VZ
1743void wxDocManager::ActivateView(wxView *view, bool activate)
1744{
1745 if ( activate )
0fb67cd1 1746 {
18759f78
VZ
1747 m_currentView = view;
1748 }
1749 else // deactivate
1750 {
1751 if ( m_currentView == view )
1752 {
1753 // don't keep stale pointer
c77049a0 1754 m_currentView = NULL;
18759f78 1755 }
0fb67cd1 1756 }
c801d85f
KB
1757}
1758
0fb67cd1
VZ
1759// ----------------------------------------------------------------------------
1760// Default document child frame
1761// ----------------------------------------------------------------------------
c801d85f
KB
1762
1763BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1764 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1765 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1766END_EVENT_TABLE()
1767
0fb67cd1
VZ
1768wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1769 wxView *view,
1770 wxFrame *frame,
1771 wxWindowID id,
1772 const wxString& title,
1773 const wxPoint& pos,
1774 const wxSize& size,
1775 long style,
1776 const wxString& name)
1777 : wxFrame(frame, id, title, pos, size, style, name)
1778{
1779 m_childDocument = doc;
1780 m_childView = view;
1781 if (view)
1782 view->SetFrame(this);
c801d85f
KB
1783}
1784
c801d85f
KB
1785// Extend event processing to search the view's event table
1786bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1787{
1788 if (m_childView)
68379eaf 1789 m_childView->Activate(true);
c801d85f 1790
0fb67cd1 1791 if ( !m_childView || ! m_childView->ProcessEvent(event) )
c801d85f
KB
1792 {
1793 // Only hand up to the parent if it's a menu command
1794 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
0fb67cd1 1795 return wxEvtHandler::ProcessEvent(event);
c801d85f 1796 else
68379eaf 1797 return true;
c801d85f 1798 }
0fb67cd1 1799 else
68379eaf 1800 return true;
c801d85f
KB
1801}
1802
c801d85f
KB
1803void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1804{
0fb67cd1 1805 wxFrame::OnActivate(event);
c801d85f 1806
0fb67cd1
VZ
1807 if (m_childView)
1808 m_childView->Activate(event.GetActive());
c801d85f
KB
1809}
1810
387a3b02 1811void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1812{
0fb67cd1 1813 if (m_childView)
c801d85f 1814 {
999836aa 1815 bool ans = event.CanVeto()
68379eaf
WS
1816 ? m_childView->Close(false) // false means don't delete associated window
1817 : true; // Must delete.
387a3b02 1818
0fb67cd1
VZ
1819 if (ans)
1820 {
68379eaf 1821 m_childView->Activate(false);
0fb67cd1 1822 delete m_childView;
c77049a0
VZ
1823 m_childView = NULL;
1824 m_childDocument = NULL;
0fb67cd1
VZ
1825
1826 this->Destroy();
1827 }
1828 else
1829 event.Veto();
c801d85f 1830 }
387a3b02 1831 else
0fb67cd1 1832 event.Veto();
c801d85f
KB
1833}
1834
0fb67cd1
VZ
1835// ----------------------------------------------------------------------------
1836// Default parent frame
1837// ----------------------------------------------------------------------------
c801d85f
KB
1838
1839BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1840 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1841 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1842 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1843END_EVENT_TABLE()
1844
0c246b3c
PC
1845wxDocParentFrame::wxDocParentFrame()
1846{
1847 m_docManager = NULL;
1848}
1849
0fb67cd1
VZ
1850wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1851 wxFrame *frame,
1852 wxWindowID id,
1853 const wxString& title,
1854 const wxPoint& pos,
1855 const wxSize& size,
1856 long style,
1857 const wxString& name)
1858 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1859{
0fb67cd1 1860 m_docManager = manager;
c801d85f
KB
1861}
1862
0c246b3c
PC
1863bool wxDocParentFrame::Create(wxDocManager *manager,
1864 wxFrame *frame,
1865 wxWindowID id,
1866 const wxString& title,
1867 const wxPoint& pos,
1868 const wxSize& size,
1869 long style,
1870 const wxString& name)
1871{
1872 m_docManager = manager;
1873 return base_type::Create(frame, id, title, pos, size, style, name);
1874}
1875
c801d85f
KB
1876void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1877{
1878 Close();
1879}
1880
1881void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1882{
1fc88785 1883 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c 1884 wxString filename(m_docManager->GetHistoryFile(n));
c77049a0
VZ
1885 if ( filename.empty() )
1886 return;
42771621 1887
c77049a0
VZ
1888 wxString errMsg; // must contain exactly one "%s" if non-empty
1889 if ( wxFile::Exists(filename) )
1890 {
1891 // try to open it
1892 if ( m_docManager->CreateDocument(filename, wxDOC_SILENT) )
1893 return;
0c5d3e1c 1894
c77049a0
VZ
1895 errMsg = _("The file '%s' couldn't be opened.");
1896 }
1897 else // file doesn't exist
1898 {
1899 errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
0c5d3e1c 1900 }
c77049a0
VZ
1901
1902
1903 wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
1904
1905 // remove the file which we can't open from the MRU list
1906 m_docManager->RemoveFileFromHistory(n);
1907
1908 // and tell the user about it
1909 wxLogError(errMsg + '\n' +
1910 _("It has been removed from the most recently used files list."),
1911 filename);
c801d85f
KB
1912}
1913
1914// Extend event processing to search the view's event table
1915bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1916{
1917 // Try the document manager, then do default processing
1918 if (!m_docManager || !m_docManager->ProcessEvent(event))
1919 return wxEvtHandler::ProcessEvent(event);
1920 else
68379eaf 1921 return true;
c801d85f
KB
1922}
1923
c801d85f
KB
1924// Define the behaviour for the frame closing
1925// - must delete all frames except for the main one.
387a3b02 1926void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1927{
0fb67cd1
VZ
1928 if (m_docManager->Clear(!event.CanVeto()))
1929 {
1930 this->Destroy();
1931 }
1932 else
1933 event.Veto();
c801d85f
KB
1934}
1935
47d67540 1936#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 1937
0fb67cd1 1938wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 1939 : wxPrintout(title)
c801d85f 1940{
0fb67cd1 1941 m_printoutView = view;
c801d85f
KB
1942}
1943
1944bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1945{
0fb67cd1
VZ
1946 wxDC *dc = GetDC();
1947
1948 // Get the logical pixels per inch of screen and printer
1949 int ppiScreenX, ppiScreenY;
1950 GetPPIScreen(&ppiScreenX, &ppiScreenY);
999836aa 1951 wxUnusedVar(ppiScreenY);
0fb67cd1
VZ
1952 int ppiPrinterX, ppiPrinterY;
1953 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
999836aa 1954 wxUnusedVar(ppiPrinterY);
0fb67cd1
VZ
1955
1956 // This scales the DC so that the printout roughly represents the
1957 // the screen scaling. The text point size _should_ be the right size
1958 // but in fact is too small for some reason. This is a detail that will
1959 // need to be addressed at some point but can be fudged for the
1960 // moment.
1961 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1962
1963 // Now we have to check in case our real page size is reduced
1964 // (e.g. because we're drawing to a print preview memory DC)
1965 int pageWidth, pageHeight;
1966 int w, h;
1967 dc->GetSize(&w, &h);
1968 GetPageSizePixels(&pageWidth, &pageHeight);
999836aa 1969 wxUnusedVar(pageHeight);
0fb67cd1
VZ
1970
1971 // If printer pageWidth == current DC width, then this doesn't
1972 // change. But w might be the preview bitmap width, so scale down.
1973 float overallScale = scale * (float)(w/(float)pageWidth);
1974 dc->SetUserScale(overallScale, overallScale);
1975
1976 if (m_printoutView)
1977 {
1978 m_printoutView->OnDraw(dc);
1979 }
68379eaf 1980 return true;
c801d85f
KB
1981}
1982
1983bool wxDocPrintout::HasPage(int pageNum)
1984{
0fb67cd1 1985 return (pageNum == 1);
c801d85f
KB
1986}
1987
1988bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1989{
0fb67cd1 1990 if (!wxPrintout::OnBeginDocument(startPage, endPage))
68379eaf 1991 return false;
c801d85f 1992
68379eaf 1993 return true;
c801d85f
KB
1994}
1995
1996void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1997{
0fb67cd1
VZ
1998 *minPage = 1;
1999 *maxPage = 1;
2000 *selPageFrom = 1;
2001 *selPageTo = 1;
c801d85f
KB
2002}
2003
0fb67cd1 2004#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 2005
0fb67cd1 2006// ----------------------------------------------------------------------------
c77049a0 2007// File history (a.k.a. MRU, most recently used, files list)
0fb67cd1 2008// ----------------------------------------------------------------------------
c801d85f 2009
e49c85af 2010wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
c801d85f 2011{
0fb67cd1 2012 m_fileMaxFiles = maxFiles;
e49c85af 2013 m_idBase = idBase;
c801d85f
KB
2014}
2015
c801d85f
KB
2016void wxFileHistory::AddFileToHistory(const wxString& file)
2017{
c77049a0
VZ
2018 // check if we don't already have this file
2019 const wxFileName fnNew(file);
2020 size_t i,
2021 numFiles = m_fileHistory.size();
2022 for ( i = 0; i < numFiles; i++ )
7f555861 2023 {
c77049a0 2024 if ( fnNew == m_fileHistory[i] )
02718e6a
VZ
2025 {
2026 // we do have it, move it to the top of the history
c77049a0
VZ
2027 RemoveFileFromHistory(i);
2028 numFiles--;
2029 break;
02718e6a 2030 }
7f555861 2031 }
0fb67cd1 2032
02718e6a 2033 // if we already have a full history, delete the one at the end
c77049a0 2034 if ( numFiles == m_fileMaxFiles )
0fb67cd1 2035 {
c77049a0 2036 RemoveFileFromHistory(--numFiles);
c801d85f 2037 }
c77049a0 2038 else // add a new menu item to all file menus (will be updated below)
0fb67cd1 2039 {
c77049a0
VZ
2040 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2041 node;
2042 node = node->GetNext() )
0fb67cd1 2043 {
c77049a0
VZ
2044 wxMenu * const menu = (wxMenu *)node->GetData();
2045
2046 if ( !numFiles && menu->GetMenuItemCount() )
0fb67cd1 2047 menu->AppendSeparator();
c77049a0
VZ
2048
2049 // label doesn't matter, it will be set below anyhow, but it can't
2050 // be empty (this is supposed to indicate a stock item)
2051 menu->Append(m_idBase + numFiles, " ");
0fb67cd1 2052 }
0fb67cd1 2053 }
b4a980f4 2054
0fb67cd1 2055
c77049a0
VZ
2056 // insert the new file in the beginning of the file history
2057 m_fileHistory.insert(m_fileHistory.begin(), file);
2058 numFiles++;
02718e6a 2059
c77049a0
VZ
2060 // update the labels in all menus
2061 for ( i = 0; i < numFiles; i++ )
2062 {
2063 // if in same directory just show the filename; otherwise the full path
2064 const wxFileName fnOld(m_fileHistory[i]);
b4a980f4 2065
c77049a0
VZ
2066 wxString pathInMenu;
2067 if ( fnOld.GetPath() == fnNew.GetPath() )
2068 {
2069 pathInMenu = fnOld.GetFullName();
2070 }
2071 else // file in different directory
2072 {
2073 // absolute path; could also set relative path
2074 pathInMenu = m_fileHistory[i];
2075 }
b4a980f4 2076
c77049a0
VZ
2077 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2078 node;
2079 node = node->GetNext() )
2080 {
2081 wxMenu * const menu = (wxMenu *)node->GetData();
2082
2083 menu->SetLabel(m_idBase + i, GetMRUEntryLabel(i, pathInMenu));
0fb67cd1 2084 }
c77049a0 2085 }
c801d85f
KB
2086}
2087
e49c85af 2088void wxFileHistory::RemoveFileFromHistory(size_t i)
0c5d3e1c 2089{
c77049a0
VZ
2090 size_t numFiles = m_fileHistory.size();
2091 wxCHECK_RET( i < numFiles,
223d09f6 2092 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c 2093
f6fe9f9c 2094 // delete the element from the array
b4a980f4 2095 m_fileHistory.RemoveAt(i);
c77049a0 2096 numFiles--;
0c5d3e1c 2097
c77049a0
VZ
2098 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2099 node;
2100 node = node->GetNext() )
00e6d8ab 2101 {
c77049a0 2102 wxMenu * const menu = (wxMenu *) node->GetData();
00e6d8ab 2103
c77049a0
VZ
2104 // shift filenames up
2105 for ( size_t j = i; j < numFiles; j++ )
4a10ea8b 2106 {
c77049a0 2107 menu->SetLabel(m_idBase + j, GetMRUEntryLabel(j, m_fileHistory[j]));
4a10ea8b 2108 }
0c5d3e1c 2109
2b273975 2110 // delete the last menu item which is unused now
c77049a0
VZ
2111 const wxWindowID lastItemId = m_idBase + numFiles;
2112 if ( menu->FindItem(lastItemId) )
e49c85af 2113 menu->Delete(lastItemId);
2b273975 2114
717a57c2 2115 // delete the last separator too if no more files are left
c77049a0 2116 if ( m_fileHistory.empty() )
717a57c2 2117 {
c77049a0
VZ
2118 const wxMenuItemList::compatibility_iterator
2119 nodeLast = menu->GetMenuItems().GetLast();
f1afd2e0 2120 if ( nodeLast )
717a57c2 2121 {
c77049a0
VZ
2122 wxMenuItem * const lastMenuItem = nodeLast->GetData();
2123 if ( lastMenuItem->IsSeparator() )
2124 menu->Delete(lastMenuItem);
717a57c2
VZ
2125 }
2126 //else: menu is empty somehow
2127 }
0c5d3e1c 2128 }
c801d85f
KB
2129}
2130
7f555861 2131void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 2132{
c77049a0 2133 if ( !m_fileMenus.Member(menu) )
0fb67cd1 2134 m_fileMenus.Append(menu);
c801d85f
KB
2135}
2136
7f555861
JS
2137void wxFileHistory::RemoveMenu(wxMenu *menu)
2138{
0fb67cd1 2139 m_fileMenus.DeleteObject(menu);
7f555861
JS
2140}
2141
702ca7c0 2142#if wxUSE_CONFIG
9c8116f8 2143void wxFileHistory::Load(const wxConfigBase& config)
c801d85f 2144{
b4a980f4
VZ
2145 m_fileHistory.Clear();
2146
0fb67cd1 2147 wxString buf;
b4a980f4
VZ
2148 buf.Printf(wxT("file%d"), 1);
2149
0fb67cd1 2150 wxString historyFile;
f6fe9f9c 2151 while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
b4a980f4 2152 config.Read(buf, &historyFile) && !historyFile.empty())
0fb67cd1 2153 {
b4a980f4
VZ
2154 m_fileHistory.Add(historyFile);
2155
2156 buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
b494c48b 2157 historyFile = wxEmptyString;
0fb67cd1 2158 }
b4a980f4 2159
0fb67cd1 2160 AddFilesToMenu();
c801d85f
KB
2161}
2162
7f555861 2163void wxFileHistory::Save(wxConfigBase& config)
c801d85f 2164{
e49c85af 2165 size_t i;
9d4a05be 2166 for (i = 0; i < m_fileMaxFiles; i++)
0fb67cd1
VZ
2167 {
2168 wxString buf;
8c9564b3 2169 buf.Printf(wxT("file%d"), (int)i+1);
b4a980f4 2170 if (i < m_fileHistory.GetCount())
9d4a05be
JS
2171 config.Write(buf, wxString(m_fileHistory[i]));
2172 else
42771621 2173 config.Write(buf, wxEmptyString);
0fb67cd1 2174 }
7f555861 2175}
0fb67cd1 2176#endif // wxUSE_CONFIG
7f555861
JS
2177
2178void wxFileHistory::AddFilesToMenu()
2179{
c77049a0
VZ
2180 if ( m_fileHistory.empty() )
2181 return;
c8c5c7f6 2182
c77049a0
VZ
2183 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2184 node;
2185 node = node->GetNext() )
2186 {
2187 AddFilesToMenu((wxMenu *) node->GetData());
7f555861
JS
2188 }
2189}
2190
2191void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2192{
c77049a0
VZ
2193 if ( m_fileHistory.empty() )
2194 return;
c8c5c7f6 2195
c77049a0
VZ
2196 if ( menu->GetMenuItemCount() )
2197 menu->AppendSeparator();
2198
2199 for ( size_t i = 0; i < m_fileHistory.GetCount(); i++ )
2200 {
2201 menu->Append(m_idBase + i, GetMRUEntryLabel(i, m_fileHistory[i]));
2202 }
c801d85f
KB
2203}
2204
0fb67cd1
VZ
2205// ----------------------------------------------------------------------------
2206// Permits compatibility with existing file formats and functions that
2207// manipulate files directly
2208// ----------------------------------------------------------------------------
c801d85f 2209
a533f5c1 2210#if wxUSE_STD_IOSTREAM
fb6e5b17 2211
dd107c50 2212bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2213{
fb6e5b17
VZ
2214 wxFFile file(filename, _T("rb"));
2215 if ( !file.IsOpened() )
68379eaf 2216 return false;
c801d85f 2217
fb6e5b17
VZ
2218 char buf[4096];
2219
2220 size_t nRead;
2221 do
2222 {
2223 nRead = file.Read(buf, WXSIZEOF(buf));
2224 if ( file.Error() )
68379eaf 2225 return false;
fb6e5b17
VZ
2226
2227 stream.write(buf, nRead);
2228 if ( !stream )
68379eaf 2229 return false;
fb6e5b17
VZ
2230 }
2231 while ( !file.Eof() );
c801d85f 2232
68379eaf 2233 return true;
c801d85f
KB
2234}
2235
dd107c50 2236bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2237{
fb6e5b17
VZ
2238 wxFFile file(filename, _T("wb"));
2239 if ( !file.IsOpened() )
68379eaf 2240 return false;
c801d85f 2241
fb6e5b17
VZ
2242 char buf[4096];
2243 do
0fb67cd1 2244 {
fb6e5b17
VZ
2245 stream.read(buf, WXSIZEOF(buf));
2246 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2247 {
2248 if ( !file.Write(buf, stream.gcount()) )
68379eaf 2249 return false;
fb6e5b17 2250 }
0fb67cd1 2251 }
fb6e5b17
VZ
2252 while ( !stream.eof() );
2253
68379eaf 2254 return true;
c801d85f 2255}
fb6e5b17
VZ
2256
2257#else // !wxUSE_STD_IOSTREAM
2258
dc1efb1d
JS
2259bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2260{
fb6e5b17
VZ
2261 wxFFile file(filename, _T("rb"));
2262 if ( !file.IsOpened() )
68379eaf 2263 return false;
dc1efb1d 2264
fb6e5b17
VZ
2265 char buf[4096];
2266
2267 size_t nRead;
2268 do
2269 {
2270 nRead = file.Read(buf, WXSIZEOF(buf));
2271 if ( file.Error() )
68379eaf 2272 return false;
fb6e5b17
VZ
2273
2274 stream.Write(buf, nRead);
2275 if ( !stream )
68379eaf 2276 return false;
fb6e5b17
VZ
2277 }
2278 while ( !file.Eof() );
dc1efb1d 2279
68379eaf 2280 return true;
dc1efb1d
JS
2281}
2282
2283bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2284{
fb6e5b17
VZ
2285 wxFFile file(filename, _T("wb"));
2286 if ( !file.IsOpened() )
68379eaf 2287 return false;
dc1efb1d 2288
fb6e5b17 2289 char buf[4096];
07f87b6b 2290 for ( ;; )
dc1efb1d 2291 {
fb6e5b17
VZ
2292 stream.Read(buf, WXSIZEOF(buf));
2293
2294 const size_t nRead = stream.LastRead();
07f87b6b
VZ
2295 if ( !nRead )
2296 {
2297 if ( stream.Eof() )
2298 break;
2299
2300 return false;
2301 }
2302
2303 if ( !file.Write(buf, nRead) )
68379eaf 2304 return false;
dc1efb1d 2305 }
fb6e5b17 2306
68379eaf 2307 return true;
dc1efb1d 2308}
fb6e5b17
VZ
2309
2310#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
c801d85f 2311
0fb67cd1 2312#endif // wxUSE_DOC_VIEW_ARCHITECTURE