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