]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
correct bug with using uninitialized end variable (unused variable warnings are not...
[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{
983 // use the system-dependent default location for the document files if
984 // we're being opened for the first time
985 if ( m_lastDirectory.empty() )
986 {
987 wxDocManager * const self = const_cast<wxDocManager *>(this);
988 self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir();
989 }
990
991 return m_lastDirectory;
992}
993
0fb67cd1 994wxFileHistory *wxDocManager::OnCreateFileHistory()
c801d85f 995{
0fb67cd1 996 return new wxFileHistory;
c801d85f
KB
997}
998
999void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
1000{
0fb67cd1
VZ
1001 wxDocument *doc = GetCurrentDocument();
1002 if (!doc)
1003 return;
1004 if (doc->Close())
1005 {
1006 doc->DeleteAllViews();
1007 if (m_docs.Member(doc))
1008 delete doc;
1009 }
c801d85f
KB
1010}
1011
33a20136
VZ
1012void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
1013{
68379eaf 1014 CloseDocuments(false);
33a20136
VZ
1015}
1016
c801d85f
KB
1017void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
1018{
b5412983 1019 CreateNewDocument();
c801d85f
KB
1020}
1021
1022void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
1023{
89d94e04 1024 if ( !CreateDocument("") )
5f170f33
VZ
1025 {
1026 OnOpenFileFailure();
1027 }
c801d85f
KB
1028}
1029
1030void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
1031{
0fb67cd1
VZ
1032 wxDocument *doc = GetCurrentDocument();
1033 if (!doc)
1034 return;
1035 doc->Revert();
c801d85f
KB
1036}
1037
1038void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
1039{
0fb67cd1
VZ
1040 wxDocument *doc = GetCurrentDocument();
1041 if (!doc)
1042 return;
1043 doc->Save();
c801d85f
KB
1044}
1045
1046void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
1047{
0fb67cd1
VZ
1048 wxDocument *doc = GetCurrentDocument();
1049 if (!doc)
1050 return;
1051 doc->SaveAs();
c801d85f
KB
1052}
1053
1054void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
1055{
88ac883a 1056#if wxUSE_PRINTING_ARCHITECTURE
575e4cd6 1057 wxView *view = GetActiveView();
0fb67cd1
VZ
1058 if (!view)
1059 return;
c801d85f 1060
0fb67cd1
VZ
1061 wxPrintout *printout = view->OnCreatePrintout();
1062 if (printout)
1063 {
1064 wxPrinter printer;
68379eaf 1065 printer.Print(view->GetFrame(), printout, true);
c801d85f 1066
0fb67cd1
VZ
1067 delete printout;
1068 }
88ac883a 1069#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1070}
1071
c801d85f
KB
1072void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
1073{
88ac883a 1074#if wxUSE_PRINTING_ARCHITECTURE
575e4cd6 1075 wxView *view = GetActiveView();
0fb67cd1
VZ
1076 if (!view)
1077 return;
c801d85f 1078
0fb67cd1
VZ
1079 wxPrintout *printout = view->OnCreatePrintout();
1080 if (printout)
1081 {
1082 // Pass two printout objects: for preview, and possible printing.
89d94e04
VZ
1083 wxPrintPreviewBase *
1084 preview = new wxPrintPreview(printout, view->OnCreatePrintout());
3f8b4a3f
JS
1085 if ( !preview->Ok() )
1086 {
1087 delete preview;
89d94e04 1088 wxLogError(_("Print preview creation failed."));
3f8b4a3f
JS
1089 return;
1090 }
0fb67cd1 1091
89d94e04
VZ
1092 wxPreviewFrame *
1093 frame = new wxPreviewFrame(preview, wxTheApp->GetTopWindow(),
1094 _("Print Preview"));
0fb67cd1
VZ
1095 frame->Centre(wxBOTH);
1096 frame->Initialize();
68379eaf 1097 frame->Show(true);
0fb67cd1 1098 }
88ac883a 1099#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1100}
1101
34aa6140 1102void wxDocManager::OnUndo(wxCommandEvent& event)
c801d85f 1103{
89d94e04
VZ
1104 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1105 if ( !cmdproc )
1106 {
34aa6140 1107 event.Skip();
89d94e04
VZ
1108 return;
1109 }
1110
1111 cmdproc->Undo();
c801d85f
KB
1112}
1113
34aa6140 1114void wxDocManager::OnRedo(wxCommandEvent& event)
c801d85f 1115{
89d94e04
VZ
1116 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1117 if ( !cmdproc )
1118 {
34aa6140 1119 event.Skip();
89d94e04
VZ
1120 return;
1121 }
1122
1123 cmdproc->Redo();
c801d85f
KB
1124}
1125
f2506310
JS
1126// Handlers for UI update commands
1127
1128void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1129{
84cae69a
FM
1130 // CreateDocument() (which is called from OnFileOpen) may succeed
1131 // only when there is at least a template:
1132 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1133}
1134
c77049a0 1135void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
f2506310 1136{
c77049a0 1137 event.Enable( GetCurrentDocument() != NULL );
f2506310
JS
1138}
1139
1140void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1141{
84cae69a
FM
1142 // CreateDocument() (which is called from OnFileNew) may succeed
1143 // only when there is at least a template:
1144 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1145}
1146
1147void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1148{
de56f240
VZ
1149 wxDocument * const doc = GetCurrentDocument();
1150 event.Enable( doc && !doc->AlreadySaved() );
f2506310
JS
1151}
1152
f2506310
JS
1153void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1154{
89d94e04
VZ
1155 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1156 if ( !cmdproc )
34aa6140 1157 {
89d94e04
VZ
1158 event.Enable(false);
1159 return;
34aa6140 1160 }
89d94e04
VZ
1161
1162 event.Enable(cmdproc->CanUndo());
1163 cmdproc->SetMenuStrings();
f2506310
JS
1164}
1165
1166void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1167{
89d94e04
VZ
1168 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1169 if ( !cmdproc )
34aa6140 1170 {
89d94e04
VZ
1171 event.Enable(false);
1172 return;
34aa6140 1173 }
89d94e04
VZ
1174
1175 event.Enable(cmdproc->CanRedo());
1176 cmdproc->SetMenuStrings();
f2506310
JS
1177}
1178
575e4cd6 1179wxView *wxDocManager::GetActiveView() const
637f467a 1180{
575e4cd6
VZ
1181 wxView *view = GetCurrentView();
1182
1183 if ( !view && !m_docs.empty() )
637f467a 1184 {
575e4cd6
VZ
1185 // if we have exactly one document, consider its view to be the current
1186 // one
1187 //
1188 // VZ: I'm not exactly sure why is this needed but this is how this
1189 // code used to behave before the bug #9518 was fixed and it seems
1190 // safer to preserve the old logic
1191 wxList::compatibility_iterator node = m_docs.GetFirst();
1192 if ( !node->GetNext() )
1193 {
1194 wxDocument *doc = static_cast<wxDocument *>(node->GetData());
1195 view = doc->GetFirstView();
1196 }
1197 //else: we have more than one document
637f467a 1198 }
575e4cd6
VZ
1199
1200 return view;
637f467a
JS
1201}
1202
8cc208e3 1203bool wxDocManager::TryBefore(wxEvent& event)
637f467a 1204{
575e4cd6 1205 wxView * const view = GetActiveView();
bba5e72a 1206 return view && view->ProcessEventHere(event);
637f467a
JS
1207}
1208
c77049a0 1209namespace
c801d85f 1210{
b1d4dd7a 1211
c77049a0
VZ
1212// helper function: return only the visible templates
1213wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
1214{
1215 // select only the visible templates
1216 const size_t totalNumTemplates = allTemplates.GetCount();
1217 wxDocTemplates templates;
1218 if ( totalNumTemplates )
c801d85f 1219 {
c77049a0
VZ
1220 templates.reserve(totalNumTemplates);
1221
1222 for ( wxList::const_iterator i = allTemplates.begin(),
1223 end = allTemplates.end();
1224 i != end;
1225 ++i )
0fb67cd1 1226 {
c77049a0
VZ
1227 wxDocTemplate * const temp = (wxDocTemplate *)*i;
1228 if ( temp->IsVisible() )
1229 templates.push_back(temp);
0fb67cd1 1230 }
c801d85f 1231 }
42771621 1232
c77049a0
VZ
1233 return templates;
1234}
0fb67cd1 1235
c77049a0
VZ
1236} // anonymous namespace
1237
1238wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
1239{
1240 // this ought to be const but SelectDocumentType/Path() are not
1241 // const-correct and can't be changed as, being virtual, this risks
1242 // breaking user code overriding them
1243 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1244 const size_t numTemplates = templates.size();
1245 if ( !numTemplates )
c801d85f 1246 {
c77049a0
VZ
1247 // no templates can be used, can't create document
1248 return NULL;
c801d85f
KB
1249 }
1250
42771621 1251
c77049a0
VZ
1252 // normally user should select the template to use but wxDOC_SILENT flag we
1253 // choose one ourselves
1254 wxString path = pathOrig; // may be modified below
1255 wxDocTemplate *temp;
1256 if ( flags & wxDOC_SILENT )
1257 {
1258 wxASSERT_MSG( !path.empty(),
1259 "using empty path with wxDOC_SILENT doesn't make sense" );
42771621 1260
c77049a0
VZ
1261 temp = FindTemplateForPath(path);
1262 if ( !temp )
1263 {
1264 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1265 path);
0fb67cd1 1266 }
c77049a0
VZ
1267 }
1268 else // not silent, ask the user
1269 {
1270 // for the new file we need just the template, for an existing one we
1271 // need the template and the path, unless it's already specified
1272 if ( (flags & wxDOC_NEW) || !path.empty() )
1273 temp = SelectDocumentType(&templates[0], numTemplates);
1274 else
1275 temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
1276 }
0fb67cd1 1277
c77049a0
VZ
1278 if ( !temp )
1279 return NULL;
42771621 1280
c77049a0
VZ
1281 // check whether the document with this path is already opened
1282 if ( !path.empty() )
1283 {
1284 const wxFileName fn(path);
1285 for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
1286 {
1287 wxDocument * const doc = (wxDocument*)*i;
b72b1920 1288
c77049a0 1289 if ( fn == doc->GetFilename() )
0fb67cd1 1290 {
c77049a0
VZ
1291 // file already open, just activate it and return
1292 if ( doc->GetFirstView() )
f5729a6b 1293 {
c77049a0
VZ
1294 ActivateView(doc->GetFirstView());
1295 if ( doc->GetDocumentWindow() )
1296 doc->GetDocumentWindow()->SetFocus();
1297 return doc;
f5729a6b 1298 }
0fb67cd1 1299 }
0fb67cd1 1300 }
c801d85f 1301 }
c801d85f 1302
c801d85f 1303
c77049a0 1304 // no, we need to create a new document
c801d85f 1305
c77049a0
VZ
1306
1307 // if we've reached the max number of docs, close the first one.
1308 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
9d4a05be 1309 {
c77049a0 1310 if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
9d4a05be 1311 {
c77049a0
VZ
1312 // can't open the new document if closing the old one failed
1313 return NULL;
9d4a05be
JS
1314 }
1315 }
c801d85f 1316
c801d85f 1317
c77049a0
VZ
1318 // do create and initialize the new document finally
1319 wxDocument * const docNew = temp->CreateDocument(path, flags);
1320 if ( !docNew )
1321 return NULL;
42771621 1322
c77049a0
VZ
1323 docNew->SetDocumentName(temp->GetDocumentName());
1324 docNew->SetDocumentTemplate(temp);
6489e7ee 1325
7339d7bd 1326 wxTRY
c77049a0 1327 {
7339d7bd
VZ
1328 // call the appropriate function depending on whether we're creating a
1329 // new file or opening an existing one
1330 if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
1331 : docNew->OnOpenDocument(path)) )
1332 {
1333 docNew->DeleteAllViews();
1334 return NULL;
1335 }
c801d85f 1336 }
7339d7bd 1337 wxCATCH_ALL( docNew->DeleteAllViews(); throw; )
3ca6a5f0 1338
c77049a0
VZ
1339 // add the successfully opened file to MRU, but only if we're going to be
1340 // able to reopen it successfully later which requires the template for
1341 // this document to be retrievable from the file extension
1342 if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
1343 AddFileToHistory(path);
1344
1345 return docNew;
c801d85f
KB
1346}
1347
1348wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1349{
c77049a0
VZ
1350 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1351 const size_t numTemplates = templates.size();
b1d4dd7a 1352
c77049a0
VZ
1353 if ( numTemplates == 0 )
1354 return NULL;
0fb67cd1 1355
c77049a0
VZ
1356 wxDocTemplate * const
1357 temp = numTemplates == 1 ? templates[0]
1358 : SelectViewType(&templates[0], numTemplates);
1359
1360 if ( !temp )
1361 return NULL;
1362
1363 wxView *view = temp->CreateView(doc, flags);
1364 if ( view )
1365 view->SetViewName(temp->GetViewName());
1366 return view;
c801d85f
KB
1367}
1368
1369// Not yet implemented
c77049a0
VZ
1370void
1371wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
c801d85f
KB
1372{
1373}
1374
1375// Not yet implemented
1376bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1377{
68379eaf 1378 return false;
c801d85f
KB
1379}
1380
6de2f8b9 1381wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1382{
575e4cd6 1383 wxView * const view = GetActiveView();
89d94e04
VZ
1384 return view ? view->GetDocument() : NULL;
1385}
1386
1387wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const
1388{
1389 wxDocument * const doc = GetCurrentDocument();
1390 return doc ? doc->GetCommandProcessor() : NULL;
c801d85f
KB
1391}
1392
724b119a
VZ
1393// Make a default name for a new document
1394#if WXWIN_COMPATIBILITY_2_8
1395bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1396{
1397 // we consider that this function can only be overridden by the user code,
1398 // not called by it as it only makes sense to call it internally, so we
1399 // don't bother to return anything from here
1400 return false;
1401}
1402#endif // WXWIN_COMPATIBILITY_2_8
1403
1404wxString wxDocManager::MakeNewDocumentName()
c801d85f 1405{
724b119a 1406 wxString name;
53c6e7cc 1407
724b119a
VZ
1408#if WXWIN_COMPATIBILITY_2_8
1409 if ( !MakeDefaultName(name) )
1410#endif // WXWIN_COMPATIBILITY_2_8
1411 {
1412 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1413 m_defaultDocumentNameCounter++;
1414 }
1415
1416 return name;
c801d85f
KB
1417}
1418
f2506310
JS
1419// Make a frame title (override this to do something different)
1420// If docName is empty, a document is not currently active.
1421wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1422{
9cf3d218 1423 wxString appName = wxTheApp->GetAppDisplayName();
f2506310
JS
1424 wxString title;
1425 if (!doc)
1426 title = appName;
1427 else
1428 {
724b119a 1429 wxString docName = doc->GetUserReadableName();
f2506310
JS
1430 title = docName + wxString(_(" - ")) + appName;
1431 }
1432 return title;
1433}
1434
1435
c801d85f
KB
1436// Not yet implemented
1437wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1438{
c77049a0 1439 return NULL;
c801d85f
KB
1440}
1441
1442// File history management
1443void wxDocManager::AddFileToHistory(const wxString& file)
1444{
0fb67cd1
VZ
1445 if (m_fileHistory)
1446 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1447}
1448
e49c85af 1449void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1450{
1451 if (m_fileHistory)
1452 m_fileHistory->RemoveFileFromHistory(i);
1453}
1454
e49c85af 1455wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1456{
0fb67cd1
VZ
1457 wxString histFile;
1458
1459 if (m_fileHistory)
1460 histFile = m_fileHistory->GetHistoryFile(i);
1461
1462 return histFile;
c801d85f
KB
1463}
1464
1465void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1466{
0fb67cd1
VZ
1467 if (m_fileHistory)
1468 m_fileHistory->UseMenu(menu);
c801d85f
KB
1469}
1470
7f555861 1471void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1472{
0fb67cd1
VZ
1473 if (m_fileHistory)
1474 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1475}
1476
702ca7c0 1477#if wxUSE_CONFIG
9c8116f8 1478void wxDocManager::FileHistoryLoad(const wxConfigBase& config)
c801d85f 1479{
0fb67cd1
VZ
1480 if (m_fileHistory)
1481 m_fileHistory->Load(config);
7f555861
JS
1482}
1483
1484void wxDocManager::FileHistorySave(wxConfigBase& config)
1485{
0fb67cd1
VZ
1486 if (m_fileHistory)
1487 m_fileHistory->Save(config);
7f555861 1488}
ac57418f 1489#endif
7f555861
JS
1490
1491void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1492{
0fb67cd1
VZ
1493 if (m_fileHistory)
1494 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1495}
1496
1497void wxDocManager::FileHistoryAddFilesToMenu()
1498{
0fb67cd1
VZ
1499 if (m_fileHistory)
1500 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1501}
1502
7af6b69e 1503size_t wxDocManager::GetHistoryFilesCount() const
c801d85f 1504{
7af6b69e 1505 return m_fileHistory ? m_fileHistory->GetCount() : 0;
c801d85f
KB
1506}
1507
1508
6de2f8b9
VZ
1509// Find out the document template via matching in the document file format
1510// against that of the template
c801d85f
KB
1511wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1512{
c77049a0 1513 wxDocTemplate *theTemplate = NULL;
c801d85f 1514
0fb67cd1 1515 // Find the template which this extension corresponds to
b1d4dd7a 1516 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1517 {
b1d4dd7a 1518 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1519 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1520 {
1521 theTemplate = temp;
1522 break;
1523 }
c801d85f 1524 }
0fb67cd1 1525 return theTemplate;
c801d85f
KB
1526}
1527
1528// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1529// Must extend the file selector dialog or implement own; OR
1530// match the extension to the template extension.
df875e59 1531
c801d85f 1532wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
0fb67cd1
VZ
1533 int noTemplates,
1534 wxString& path,
1535 long WXUNUSED(flags),
1536 bool WXUNUSED(save))
c801d85f 1537{
89d94e04 1538#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
ba681060
VZ
1539 wxString descrBuf;
1540
89d94e04 1541 for (int i = 0; i < noTemplates; i++)
c801d85f 1542 {
ba681060
VZ
1543 if (templates[i]->IsVisible())
1544 {
1545 // add a '|' to separate this filter from the previous one
b494c48b 1546 if ( !descrBuf.empty() )
223d09f6 1547 descrBuf << wxT('|');
ba681060
VZ
1548
1549 descrBuf << templates[i]->GetDescription()
223d09f6 1550 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1551 << templates[i]->GetFileFilter();
ba681060 1552 }
c801d85f 1553 }
a4294b78 1554#else
223d09f6 1555 wxString descrBuf = wxT("*.*");
c77049a0 1556 wxUnusedVar(noTemplates);
a4294b78 1557#endif
c801d85f 1558
3ca6a5f0 1559 int FilterIndex = -1;
f6bcfd97 1560
f6fe9f9c 1561 wxString pathTmp = wxFileSelectorEx(_("Open File"),
d8efd219 1562 GetLastDirectory(),
b494c48b 1563 wxEmptyString,
6de2f8b9 1564 &FilterIndex,
b6e96faf 1565 descrBuf);
ba681060 1566
c77049a0 1567 wxDocTemplate *theTemplate = NULL;
b494c48b 1568 if (!pathTmp.empty())
0fb67cd1 1569 {
f6bcfd97
BP
1570 if (!wxFileExists(pathTmp))
1571 {
1572 wxString msgTitle;
9cf3d218
VZ
1573 if (!wxTheApp->GetAppDisplayName().empty())
1574 msgTitle = wxTheApp->GetAppDisplayName();
f6bcfd97
BP
1575 else
1576 msgTitle = wxString(_("File error"));
02718e6a 1577
89d94e04
VZ
1578 wxMessageBox(_("Sorry, could not open this file."),
1579 msgTitle,
b6e96faf 1580 wxOK | wxICON_EXCLAMATION | wxCENTRE);
f6bcfd97 1581
b494c48b 1582 path = wxEmptyString;
c77049a0 1583 return NULL;
f6bcfd97 1584 }
d8efd219
VZ
1585
1586 SetLastDirectory(wxPathOnly(pathTmp));
ac0ac824 1587
0fb67cd1 1588 path = pathTmp;
0fb67cd1 1589
3ca6a5f0
BP
1590 // first choose the template using the extension, if this fails (i.e.
1591 // wxFileSelectorEx() didn't fill it), then use the path
1592 if ( FilterIndex != -1 )
6de2f8b9 1593 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1594 if ( !theTemplate )
1595 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1596 if ( !theTemplate )
1597 {
89d94e04
VZ
1598 // Since we do not add files with non-default extensions to the
1599 // file history this can only happen if the application changes the
1600 // allowed templates in runtime.
1601 wxMessageBox(_("Sorry, the format for this file is unknown."),
1602 _("Open File"),
b6e96faf 1603 wxOK | wxICON_EXCLAMATION | wxCENTRE);
9d4a05be 1604 }
0fb67cd1
VZ
1605 }
1606 else
1607 {
89d94e04 1608 path.clear();
0fb67cd1 1609 }
3ca6a5f0
BP
1610
1611 return theTemplate;
c801d85f
KB
1612}
1613
1614wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1615 int noTemplates, bool sort)
0fb67cd1 1616{
222ed1d6 1617 wxArrayString strings;
89d94e04 1618 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
0fb67cd1
VZ
1619 int i;
1620 int n = 0;
42771621
VZ
1621
1622 for (i = 0; i < noTemplates; i++)
1623 {
1624 if (templates[i]->IsVisible())
1625 {
1626 int j;
68379eaf 1627 bool want = true;
42771621
VZ
1628 for (j = 0; j < n; j++)
1629 {
bb28b477 1630 //filter out NOT unique documents + view combinations
42771621 1631 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
bb28b477
JS
1632 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1633 )
68379eaf 1634 want = false;
42771621 1635 }
bb28b477
JS
1636
1637 if ( want )
42771621
VZ
1638 {
1639 strings.Add(templates[i]->m_description);
1640
1641 data[n] = templates[i];
1642 n ++;
1643 }
1644 }
1645 } // for
1646
1647 if (sort)
1648 {
a2a03d78 1649 strings.Sort(); // ascending sort
42771621
VZ
1650 // Yes, this will be slow, but template lists
1651 // are typically short.
1652 int j;
1653 n = strings.Count();
1654 for (i = 0; i < n; i++)
1655 {
1656 for (j = 0; j < noTemplates; j++)
1657 {
1658 if (strings[i] == templates[j]->m_description)
1659 data[i] = templates[j];
1660 }
1661 }
1662 }
17260efd
VZ
1663
1664 wxDocTemplate *theTemplate;
1665
1666 switch ( n )
0fb67cd1 1667 {
17260efd
VZ
1668 case 0:
1669 // no visible templates, hence nothing to choose from
1670 theTemplate = NULL;
1671 break;
0fb67cd1 1672
17260efd 1673 case 1:
c77049a0 1674 // don't propose the user to choose if he has no choice
17260efd
VZ
1675 theTemplate = data[0];
1676 break;
1677
1678 default:
1679 // propose the user to choose one of several
1680 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1681 (
1682 _("Select a document template"),
1683 _("Templates"),
1684 strings,
b6e96faf 1685 (void **)data.get()
17260efd
VZ
1686 );
1687 }
f6bcfd97 1688
0fb67cd1 1689 return theTemplate;
c801d85f
KB
1690}
1691
1692wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1693 int noTemplates, bool sort)
c801d85f 1694{
222ed1d6 1695 wxArrayString strings;
89d94e04 1696 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
0fb67cd1
VZ
1697 int i;
1698 int n = 0;
42771621 1699
0fb67cd1 1700 for (i = 0; i < noTemplates; i++)
c801d85f 1701 {
17260efd
VZ
1702 wxDocTemplate *templ = templates[i];
1703 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1704 {
42771621 1705 int j;
68379eaf 1706 bool want = true;
42771621
VZ
1707 for (j = 0; j < n; j++)
1708 {
bb28b477 1709 //filter out NOT unique views
42771621 1710 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
68379eaf 1711 want = false;
42771621 1712 }
bb28b477
JS
1713
1714 if ( want )
1715 {
42771621
VZ
1716 strings.Add(templ->m_viewTypeName);
1717 data[n] = templ;
1718 n ++;
1719 }
0fb67cd1 1720 }
c801d85f 1721 }
f6bcfd97 1722
42771621
VZ
1723 if (sort)
1724 {
a2a03d78 1725 strings.Sort(); // ascending sort
42771621
VZ
1726 // Yes, this will be slow, but template lists
1727 // are typically short.
1728 int j;
1729 n = strings.Count();
1730 for (i = 0; i < n; i++)
1731 {
1732 for (j = 0; j < noTemplates; j++)
1733 {
1734 if (strings[i] == templates[j]->m_viewTypeName)
1735 data[i] = templates[j];
1736 }
1737 }
1738 }
8658ef93 1739
17260efd
VZ
1740 wxDocTemplate *theTemplate;
1741
1742 // the same logic as above
1743 switch ( n )
1744 {
1745 case 0:
c77049a0 1746 theTemplate = NULL;
17260efd
VZ
1747 break;
1748
1749 case 1:
1750 theTemplate = data[0];
1751 break;
1752
1753 default:
1754 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1755 (
1756 _("Select a document view"),
1757 _("Views"),
1758 strings,
b6e96faf 1759 (void **)data.get()
17260efd
VZ
1760 );
1761
1762 }
1763
0fb67cd1 1764 return theTemplate;
c801d85f
KB
1765}
1766
1767void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1768{
0fb67cd1
VZ
1769 if (!m_templates.Member(temp))
1770 m_templates.Append(temp);
c801d85f
KB
1771}
1772
1773void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1774{
0fb67cd1 1775 m_templates.DeleteObject(temp);
c801d85f
KB
1776}
1777
1778// Add and remove a document from the manager's list
1779void wxDocManager::AddDocument(wxDocument *doc)
1780{
0fb67cd1
VZ
1781 if (!m_docs.Member(doc))
1782 m_docs.Append(doc);
c801d85f
KB
1783}
1784
1785void wxDocManager::RemoveDocument(wxDocument *doc)
1786{
0fb67cd1 1787 m_docs.DeleteObject(doc);
c801d85f
KB
1788}
1789
1790// Views or windows should inform the document manager
1791// when a view is going in or out of focus
18759f78
VZ
1792void wxDocManager::ActivateView(wxView *view, bool activate)
1793{
1794 if ( activate )
0fb67cd1 1795 {
18759f78
VZ
1796 m_currentView = view;
1797 }
1798 else // deactivate
1799 {
1800 if ( m_currentView == view )
1801 {
1802 // don't keep stale pointer
c77049a0 1803 m_currentView = NULL;
18759f78 1804 }
0fb67cd1 1805 }
c801d85f
KB
1806}
1807
0fb67cd1 1808// ----------------------------------------------------------------------------
a9e2e6e5 1809// wxDocChildFrameAnyBase
0fb67cd1 1810// ----------------------------------------------------------------------------
c801d85f 1811
a9e2e6e5 1812bool wxDocChildFrameAnyBase::CloseView(wxCloseEvent& event)
c801d85f 1813{
8cc208e3
VZ
1814 if ( m_childView )
1815 {
a9e2e6e5
VZ
1816 if ( event.CanVeto() && !m_childView->Close(false) )
1817 {
1818 event.Veto();
1819 return false;
1820 }
89d94e04 1821
a9e2e6e5 1822 m_childView->Activate(false);
ada1ff0d
VZ
1823
1824 // it is important to reset m_childView frame pointer to NULL before
1825 // deleting it because while normally it is the frame which deletes the
1826 // view when it's closed, the view also closes the frame if it is
1827 // deleted directly not by us as indicated by its doc child frame
1828 // pointer still being set
1829 m_childView->SetDocChildFrame(NULL);
a9e2e6e5
VZ
1830 delete m_childView;
1831 m_childView = NULL;
89d94e04 1832 }
387a3b02 1833
89d94e04 1834 m_childDocument = NULL;
0fb67cd1 1835
a9e2e6e5 1836 return true;
c801d85f
KB
1837}
1838
0fb67cd1
VZ
1839// ----------------------------------------------------------------------------
1840// Default parent frame
1841// ----------------------------------------------------------------------------
c801d85f
KB
1842
1843BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1844 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1845 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1846 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1847END_EVENT_TABLE()
1848
0c246b3c
PC
1849wxDocParentFrame::wxDocParentFrame()
1850{
1851 m_docManager = NULL;
1852}
1853
0fb67cd1
VZ
1854wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1855 wxFrame *frame,
1856 wxWindowID id,
1857 const wxString& title,
1858 const wxPoint& pos,
1859 const wxSize& size,
1860 long style,
1861 const wxString& name)
1862 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1863{
0fb67cd1 1864 m_docManager = manager;
c801d85f
KB
1865}
1866
0c246b3c
PC
1867bool wxDocParentFrame::Create(wxDocManager *manager,
1868 wxFrame *frame,
1869 wxWindowID id,
1870 const wxString& title,
1871 const wxPoint& pos,
1872 const wxSize& size,
1873 long style,
1874 const wxString& name)
1875{
1876 m_docManager = manager;
1877 return base_type::Create(frame, id, title, pos, size, style, name);
1878}
1879
c801d85f
KB
1880void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1881{
1882 Close();
1883}
1884
1885void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1886{
1fc88785 1887 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c 1888 wxString filename(m_docManager->GetHistoryFile(n));
c77049a0
VZ
1889 if ( filename.empty() )
1890 return;
42771621 1891
c77049a0
VZ
1892 wxString errMsg; // must contain exactly one "%s" if non-empty
1893 if ( wxFile::Exists(filename) )
1894 {
1895 // try to open it
1896 if ( m_docManager->CreateDocument(filename, wxDOC_SILENT) )
1897 return;
0c5d3e1c 1898
c77049a0
VZ
1899 errMsg = _("The file '%s' couldn't be opened.");
1900 }
1901 else // file doesn't exist
1902 {
1903 errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
0c5d3e1c 1904 }
c77049a0
VZ
1905
1906
1907 wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
1908
1909 // remove the file which we can't open from the MRU list
1910 m_docManager->RemoveFileFromHistory(n);
1911
1912 // and tell the user about it
1913 wxLogError(errMsg + '\n' +
1914 _("It has been removed from the most recently used files list."),
1915 filename);
c801d85f
KB
1916}
1917
1918// Extend event processing to search the view's event table
8cc208e3 1919bool wxDocParentFrame::TryBefore(wxEvent& event)
c801d85f 1920{
8cc208e3
VZ
1921 if ( m_docManager && m_docManager->ProcessEventHere(event) )
1922 return true;
1923
1924 return wxFrame::TryBefore(event);
c801d85f
KB
1925}
1926
c801d85f
KB
1927// Define the behaviour for the frame closing
1928// - must delete all frames except for the main one.
387a3b02 1929void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1930{
0fb67cd1
VZ
1931 if (m_docManager->Clear(!event.CanVeto()))
1932 {
89d94e04 1933 Destroy();
0fb67cd1
VZ
1934 }
1935 else
1936 event.Veto();
c801d85f
KB
1937}
1938
47d67540 1939#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 1940
0fb67cd1 1941wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 1942 : wxPrintout(title)
c801d85f 1943{
0fb67cd1 1944 m_printoutView = view;
c801d85f
KB
1945}
1946
1947bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1948{
0fb67cd1
VZ
1949 wxDC *dc = GetDC();
1950
1951 // Get the logical pixels per inch of screen and printer
1952 int ppiScreenX, ppiScreenY;
1953 GetPPIScreen(&ppiScreenX, &ppiScreenY);
999836aa 1954 wxUnusedVar(ppiScreenY);
0fb67cd1
VZ
1955 int ppiPrinterX, ppiPrinterY;
1956 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
999836aa 1957 wxUnusedVar(ppiPrinterY);
0fb67cd1
VZ
1958
1959 // This scales the DC so that the printout roughly represents the
1960 // the screen scaling. The text point size _should_ be the right size
1961 // but in fact is too small for some reason. This is a detail that will
1962 // need to be addressed at some point but can be fudged for the
1963 // moment.
1964 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1965
1966 // Now we have to check in case our real page size is reduced
1967 // (e.g. because we're drawing to a print preview memory DC)
1968 int pageWidth, pageHeight;
1969 int w, h;
1970 dc->GetSize(&w, &h);
1971 GetPageSizePixels(&pageWidth, &pageHeight);
999836aa 1972 wxUnusedVar(pageHeight);
0fb67cd1
VZ
1973
1974 // If printer pageWidth == current DC width, then this doesn't
1975 // change. But w might be the preview bitmap width, so scale down.
1976 float overallScale = scale * (float)(w/(float)pageWidth);
1977 dc->SetUserScale(overallScale, overallScale);
1978
1979 if (m_printoutView)
1980 {
1981 m_printoutView->OnDraw(dc);
1982 }
68379eaf 1983 return true;
c801d85f
KB
1984}
1985
1986bool wxDocPrintout::HasPage(int pageNum)
1987{
0fb67cd1 1988 return (pageNum == 1);
c801d85f
KB
1989}
1990
1991bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1992{
0fb67cd1 1993 if (!wxPrintout::OnBeginDocument(startPage, endPage))
68379eaf 1994 return false;
c801d85f 1995
68379eaf 1996 return true;
c801d85f
KB
1997}
1998
89d94e04
VZ
1999void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage,
2000 int *selPageFrom, int *selPageTo)
c801d85f 2001{
0fb67cd1
VZ
2002 *minPage = 1;
2003 *maxPage = 1;
2004 *selPageFrom = 1;
2005 *selPageTo = 1;
c801d85f
KB
2006}
2007
0fb67cd1 2008#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 2009
0fb67cd1 2010// ----------------------------------------------------------------------------
c77049a0 2011// File history (a.k.a. MRU, most recently used, files list)
0fb67cd1 2012// ----------------------------------------------------------------------------
c801d85f 2013
e49c85af 2014wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
c801d85f 2015{
0fb67cd1 2016 m_fileMaxFiles = maxFiles;
e49c85af 2017 m_idBase = idBase;
c801d85f
KB
2018}
2019
c801d85f
KB
2020void wxFileHistory::AddFileToHistory(const wxString& file)
2021{
c77049a0
VZ
2022 // check if we don't already have this file
2023 const wxFileName fnNew(file);
2024 size_t i,
2025 numFiles = m_fileHistory.size();
2026 for ( i = 0; i < numFiles; i++ )
7f555861 2027 {
c77049a0 2028 if ( fnNew == m_fileHistory[i] )
02718e6a
VZ
2029 {
2030 // we do have it, move it to the top of the history
c77049a0
VZ
2031 RemoveFileFromHistory(i);
2032 numFiles--;
2033 break;
02718e6a 2034 }
7f555861 2035 }
0fb67cd1 2036
02718e6a 2037 // if we already have a full history, delete the one at the end
c77049a0 2038 if ( numFiles == m_fileMaxFiles )
0fb67cd1 2039 {
c77049a0 2040 RemoveFileFromHistory(--numFiles);
c801d85f 2041 }
838dd956
VZ
2042
2043 // add a new menu item to all file menus (they will be updated below)
2044 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2045 node;
2046 node = node->GetNext() )
0fb67cd1 2047 {
838dd956 2048 wxMenu * const menu = (wxMenu *)node->GetData();
c77049a0 2049
838dd956
VZ
2050 if ( !numFiles && menu->GetMenuItemCount() )
2051 menu->AppendSeparator();
c77049a0 2052
838dd956
VZ
2053 // label doesn't matter, it will be set below anyhow, but it can't
2054 // be empty (this is supposed to indicate a stock item)
2055 menu->Append(m_idBase + numFiles, " ");
0fb67cd1 2056 }
b4a980f4 2057
c77049a0
VZ
2058 // insert the new file in the beginning of the file history
2059 m_fileHistory.insert(m_fileHistory.begin(), file);
2060 numFiles++;
02718e6a 2061
c77049a0
VZ
2062 // update the labels in all menus
2063 for ( i = 0; i < numFiles; i++ )
2064 {
2065 // if in same directory just show the filename; otherwise the full path
2066 const wxFileName fnOld(m_fileHistory[i]);
b4a980f4 2067
c77049a0
VZ
2068 wxString pathInMenu;
2069 if ( fnOld.GetPath() == fnNew.GetPath() )
2070 {
2071 pathInMenu = fnOld.GetFullName();
2072 }
2073 else // file in different directory
2074 {
2075 // absolute path; could also set relative path
2076 pathInMenu = m_fileHistory[i];
2077 }
b4a980f4 2078
c77049a0
VZ
2079 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2080 node;
2081 node = node->GetNext() )
2082 {
2083 wxMenu * const menu = (wxMenu *)node->GetData();
2084
2085 menu->SetLabel(m_idBase + i, GetMRUEntryLabel(i, pathInMenu));
0fb67cd1 2086 }
c77049a0 2087 }
c801d85f
KB
2088}
2089
e49c85af 2090void wxFileHistory::RemoveFileFromHistory(size_t i)
0c5d3e1c 2091{
c77049a0
VZ
2092 size_t numFiles = m_fileHistory.size();
2093 wxCHECK_RET( i < numFiles,
223d09f6 2094 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c 2095
f6fe9f9c 2096 // delete the element from the array
b4a980f4 2097 m_fileHistory.RemoveAt(i);
c77049a0 2098 numFiles--;
0c5d3e1c 2099
c77049a0
VZ
2100 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2101 node;
2102 node = node->GetNext() )
00e6d8ab 2103 {
c77049a0 2104 wxMenu * const menu = (wxMenu *) node->GetData();
00e6d8ab 2105
c77049a0
VZ
2106 // shift filenames up
2107 for ( size_t j = i; j < numFiles; j++ )
4a10ea8b 2108 {
c77049a0 2109 menu->SetLabel(m_idBase + j, GetMRUEntryLabel(j, m_fileHistory[j]));
4a10ea8b 2110 }
0c5d3e1c 2111
2b273975 2112 // delete the last menu item which is unused now
c77049a0
VZ
2113 const wxWindowID lastItemId = m_idBase + numFiles;
2114 if ( menu->FindItem(lastItemId) )
e49c85af 2115 menu->Delete(lastItemId);
2b273975 2116
717a57c2 2117 // delete the last separator too if no more files are left
c77049a0 2118 if ( m_fileHistory.empty() )
717a57c2 2119 {
c77049a0
VZ
2120 const wxMenuItemList::compatibility_iterator
2121 nodeLast = menu->GetMenuItems().GetLast();
f1afd2e0 2122 if ( nodeLast )
717a57c2 2123 {
c77049a0
VZ
2124 wxMenuItem * const lastMenuItem = nodeLast->GetData();
2125 if ( lastMenuItem->IsSeparator() )
2126 menu->Delete(lastMenuItem);
717a57c2
VZ
2127 }
2128 //else: menu is empty somehow
2129 }
0c5d3e1c 2130 }
c801d85f
KB
2131}
2132
7f555861 2133void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 2134{
c77049a0 2135 if ( !m_fileMenus.Member(menu) )
0fb67cd1 2136 m_fileMenus.Append(menu);
c801d85f
KB
2137}
2138
7f555861
JS
2139void wxFileHistory::RemoveMenu(wxMenu *menu)
2140{
0fb67cd1 2141 m_fileMenus.DeleteObject(menu);
7f555861
JS
2142}
2143
702ca7c0 2144#if wxUSE_CONFIG
9c8116f8 2145void wxFileHistory::Load(const wxConfigBase& config)
c801d85f 2146{
b4a980f4
VZ
2147 m_fileHistory.Clear();
2148
0fb67cd1 2149 wxString buf;
b4a980f4
VZ
2150 buf.Printf(wxT("file%d"), 1);
2151
0fb67cd1 2152 wxString historyFile;
f6fe9f9c 2153 while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
b4a980f4 2154 config.Read(buf, &historyFile) && !historyFile.empty())
0fb67cd1 2155 {
b4a980f4
VZ
2156 m_fileHistory.Add(historyFile);
2157
2158 buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
b494c48b 2159 historyFile = wxEmptyString;
0fb67cd1 2160 }
b4a980f4 2161
0fb67cd1 2162 AddFilesToMenu();
c801d85f
KB
2163}
2164
7f555861 2165void wxFileHistory::Save(wxConfigBase& config)
c801d85f 2166{
e49c85af 2167 size_t i;
9d4a05be 2168 for (i = 0; i < m_fileMaxFiles; i++)
0fb67cd1
VZ
2169 {
2170 wxString buf;
8c9564b3 2171 buf.Printf(wxT("file%d"), (int)i+1);
b4a980f4 2172 if (i < m_fileHistory.GetCount())
9d4a05be
JS
2173 config.Write(buf, wxString(m_fileHistory[i]));
2174 else
42771621 2175 config.Write(buf, wxEmptyString);
0fb67cd1 2176 }
7f555861 2177}
0fb67cd1 2178#endif // wxUSE_CONFIG
7f555861
JS
2179
2180void wxFileHistory::AddFilesToMenu()
2181{
c77049a0
VZ
2182 if ( m_fileHistory.empty() )
2183 return;
c8c5c7f6 2184
c77049a0
VZ
2185 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2186 node;
2187 node = node->GetNext() )
2188 {
2189 AddFilesToMenu((wxMenu *) node->GetData());
7f555861
JS
2190 }
2191}
2192
2193void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2194{
c77049a0
VZ
2195 if ( m_fileHistory.empty() )
2196 return;
c8c5c7f6 2197
c77049a0
VZ
2198 if ( menu->GetMenuItemCount() )
2199 menu->AppendSeparator();
2200
2201 for ( size_t i = 0; i < m_fileHistory.GetCount(); i++ )
2202 {
2203 menu->Append(m_idBase + i, GetMRUEntryLabel(i, m_fileHistory[i]));
2204 }
c801d85f
KB
2205}
2206
0fb67cd1
VZ
2207// ----------------------------------------------------------------------------
2208// Permits compatibility with existing file formats and functions that
2209// manipulate files directly
2210// ----------------------------------------------------------------------------
c801d85f 2211
a533f5c1 2212#if wxUSE_STD_IOSTREAM
fb6e5b17 2213
dd107c50 2214bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2215{
fb6e5b17
VZ
2216 wxFFile file(filename, _T("rb"));
2217 if ( !file.IsOpened() )
68379eaf 2218 return false;
c801d85f 2219
fb6e5b17
VZ
2220 char buf[4096];
2221
2222 size_t nRead;
2223 do
2224 {
2225 nRead = file.Read(buf, WXSIZEOF(buf));
2226 if ( file.Error() )
68379eaf 2227 return false;
fb6e5b17
VZ
2228
2229 stream.write(buf, nRead);
2230 if ( !stream )
68379eaf 2231 return false;
fb6e5b17
VZ
2232 }
2233 while ( !file.Eof() );
c801d85f 2234
68379eaf 2235 return true;
c801d85f
KB
2236}
2237
dd107c50 2238bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2239{
fb6e5b17
VZ
2240 wxFFile file(filename, _T("wb"));
2241 if ( !file.IsOpened() )
68379eaf 2242 return false;
c801d85f 2243
fb6e5b17
VZ
2244 char buf[4096];
2245 do
0fb67cd1 2246 {
fb6e5b17
VZ
2247 stream.read(buf, WXSIZEOF(buf));
2248 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2249 {
2250 if ( !file.Write(buf, stream.gcount()) )
68379eaf 2251 return false;
fb6e5b17 2252 }
0fb67cd1 2253 }
fb6e5b17
VZ
2254 while ( !stream.eof() );
2255
68379eaf 2256 return true;
c801d85f 2257}
fb6e5b17
VZ
2258
2259#else // !wxUSE_STD_IOSTREAM
2260
dc1efb1d
JS
2261bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2262{
fb6e5b17
VZ
2263 wxFFile file(filename, _T("rb"));
2264 if ( !file.IsOpened() )
68379eaf 2265 return false;
dc1efb1d 2266
fb6e5b17
VZ
2267 char buf[4096];
2268
2269 size_t nRead;
2270 do
2271 {
2272 nRead = file.Read(buf, WXSIZEOF(buf));
2273 if ( file.Error() )
68379eaf 2274 return false;
fb6e5b17
VZ
2275
2276 stream.Write(buf, nRead);
2277 if ( !stream )
68379eaf 2278 return false;
fb6e5b17
VZ
2279 }
2280 while ( !file.Eof() );
dc1efb1d 2281
68379eaf 2282 return true;
dc1efb1d
JS
2283}
2284
2285bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2286{
fb6e5b17
VZ
2287 wxFFile file(filename, _T("wb"));
2288 if ( !file.IsOpened() )
68379eaf 2289 return false;
dc1efb1d 2290
fb6e5b17 2291 char buf[4096];
07f87b6b 2292 for ( ;; )
dc1efb1d 2293 {
fb6e5b17
VZ
2294 stream.Read(buf, WXSIZEOF(buf));
2295
2296 const size_t nRead = stream.LastRead();
07f87b6b
VZ
2297 if ( !nRead )
2298 {
2299 if ( stream.Eof() )
2300 break;
2301
2302 return false;
2303 }
2304
2305 if ( !file.Write(buf, nRead) )
68379eaf 2306 return false;
dc1efb1d 2307 }
fb6e5b17 2308
68379eaf 2309 return true;
dc1efb1d 2310}
fb6e5b17
VZ
2311
2312#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
c801d85f 2313
0fb67cd1 2314#endif // wxUSE_DOC_VIEW_ARCHITECTURE