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