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