]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
don't list SetSelection() as being deprecated, it isn't
[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"
7047d798 61#include "wx/ptr_scpd.h"
c801d85f 62
a533f5c1 63#if wxUSE_STD_IOSTREAM
8ecff181 64 #include "wx/ioswrap.h"
968454db 65 #include "wx/beforestd.h"
8ecff181
WS
66 #if wxUSE_IOSTREAMH
67 #include <fstream.h>
68 #else
69 #include <fstream>
70 #endif
968454db 71 #include "wx/afterstd.h"
a533f5c1 72#else
8ecff181 73 #include "wx/wfstream.h"
c801d85f
KB
74#endif
75
c77049a0
VZ
76typedef wxVector<wxDocTemplate *> wxDocTemplates;
77
0fb67cd1 78// ----------------------------------------------------------------------------
77ffb593 79// wxWidgets macros
0fb67cd1
VZ
80// ----------------------------------------------------------------------------
81
1e6feb95
VZ
82IMPLEMENT_ABSTRACT_CLASS(wxDocument, wxEvtHandler)
83IMPLEMENT_ABSTRACT_CLASS(wxView, wxEvtHandler)
84IMPLEMENT_ABSTRACT_CLASS(wxDocTemplate, wxObject)
85IMPLEMENT_DYNAMIC_CLASS(wxDocManager, wxEvtHandler)
86IMPLEMENT_CLASS(wxDocChildFrame, wxFrame)
87IMPLEMENT_CLASS(wxDocParentFrame, wxFrame)
0fb67cd1 88
1e6feb95
VZ
89#if wxUSE_PRINTING_ARCHITECTURE
90 IMPLEMENT_DYNAMIC_CLASS(wxDocPrintout, wxPrintout)
91#endif
0fb67cd1 92
1e6feb95 93IMPLEMENT_DYNAMIC_CLASS(wxFileHistory, wxObject)
c801d85f 94
0fb67cd1
VZ
95// ============================================================================
96// implementation
97// ============================================================================
98
99// ----------------------------------------------------------------------------
c77049a0 100// private helpers
0fb67cd1
VZ
101// ----------------------------------------------------------------------------
102
c77049a0
VZ
103namespace
104{
105
106wxWindow *wxFindSuitableParent()
107{
108 wxWindow * const win = wxGetTopLevelParent(wxWindow::FindFocus());
109
110 return win ? win : wxTheApp->GetTopWindow();
111}
112
113wxString FindExtension(const wxString& path)
0fb67cd1
VZ
114{
115 wxString ext;
47d281e6 116 wxFileName::SplitPath(path, NULL, NULL, &ext);
0fb67cd1
VZ
117
118 // VZ: extensions are considered not case sensitive - is this really a good
119 // idea?
120 return ext.MakeLower();
121}
122
c77049a0
VZ
123// return the string used for the MRU list items in the menu
124//
125// NB: the index n is 0-based, as usual, but the strings start from 1
126wxString GetMRUEntryLabel(int n, const wxString& path)
127{
128 // we need to quote '&' characters which are used for mnemonics
129 wxString pathInMenu(path);
130 pathInMenu.Replace("&", "&&");
131
132 return wxString::Format("&%d %s", n + 1, pathInMenu);
133}
134
135} // anonymous namespace
136
0fb67cd1
VZ
137// ----------------------------------------------------------------------------
138// Definition of wxDocument
139// ----------------------------------------------------------------------------
c801d85f
KB
140
141wxDocument::wxDocument(wxDocument *parent)
142{
68379eaf 143 m_documentModified = false;
0fb67cd1 144 m_documentParent = parent;
c77049a0
VZ
145 m_documentTemplate = NULL;
146 m_commandProcessor = NULL;
68379eaf 147 m_savedYet = false;
c801d85f
KB
148}
149
0fb67cd1 150bool wxDocument::DeleteContents()
c801d85f 151{
68379eaf 152 return true;
c801d85f
KB
153}
154
0fb67cd1 155wxDocument::~wxDocument()
c801d85f 156{
0fb67cd1 157 DeleteContents();
c801d85f 158
89d94e04 159 delete m_commandProcessor;
c801d85f 160
f6bcfd97
BP
161 if (GetDocumentManager())
162 GetDocumentManager()->RemoveDocument(this);
c801d85f 163
0fb67cd1
VZ
164 // Not safe to do here, since it'll invoke virtual view functions
165 // expecting to see valid derived objects: and by the time we get here,
166 // we've called destructors higher up.
167 //DeleteAllViews();
c801d85f 168}
0fb67cd1
VZ
169
170bool wxDocument::Close()
c801d85f 171{
89d94e04 172 if ( !OnSaveModified() )
68379eaf 173 return false;
89d94e04
VZ
174
175 return OnCloseDocument();
c801d85f 176}
0fb67cd1
VZ
177
178bool wxDocument::OnCloseDocument()
c801d85f 179{
b23e843b 180 // Tell all views that we're about to close
42771621 181 NotifyClosing();
0fb67cd1 182 DeleteContents();
68379eaf
WS
183 Modify(false);
184 return true;
c801d85f
KB
185}
186
0fb67cd1
VZ
187// Note that this implicitly deletes the document when the last view is
188// deleted.
189bool wxDocument::DeleteAllViews()
c801d85f 190{
f6bcfd97
BP
191 wxDocManager* manager = GetDocumentManager();
192
40702099
VZ
193 // first check if all views agree to be closed
194 const wxList::iterator end = m_documentViews.end();
195 for ( wxList::iterator i = m_documentViews.begin(); i != end; ++i )
0fb67cd1 196 {
40702099
VZ
197 wxView *view = (wxView *)*i;
198 if ( !view->Close() )
68379eaf 199 return false;
0fb67cd1 200 }
d5a93fda 201
40702099
VZ
202 // all views agreed to close, now do close them
203 if ( m_documentViews.empty() )
204 {
205 // normally the document would be implicitly deleted when the last view
206 // is, but if don't have any views, do it here instead
207 if ( manager && manager->GetDocuments().Member(this) )
208 delete this;
209 }
210 else // have views
211 {
212 // as we delete elements we iterate over, don't use the usual "from
213 // begin to end" loop
214 for ( ;; )
215 {
216 wxView *view = (wxView *)*m_documentViews.begin();
217
218 bool isLastOne = m_documentViews.size() == 1;
219
220 // this always deletes the node implicitly and if this is the last
221 // view also deletes this object itself (also implicitly, great),
222 // so we can't test for m_documentViews.empty() after calling this!
223 delete view;
224
225 if ( isLastOne )
226 break;
227 }
228 }
f6bcfd97 229
68379eaf 230 return true;
c801d85f
KB
231}
232
6de2f8b9 233wxView *wxDocument::GetFirstView() const
c801d85f 234{
89d94e04 235 if ( m_documentViews.empty() )
c77049a0 236 return NULL;
89d94e04
VZ
237
238 return static_cast<wxView *>(m_documentViews.GetFirst()->GetData());
c801d85f
KB
239}
240
6de2f8b9 241wxDocManager *wxDocument::GetDocumentManager() const
c801d85f 242{
c77049a0 243 return m_documentTemplate ? m_documentTemplate->GetDocumentManager() : NULL;
c801d85f
KB
244}
245
0fb67cd1 246bool wxDocument::OnNewDocument()
c801d85f 247{
c77049a0 248 if ( !OnSaveModified() )
68379eaf 249 return false;
c801d85f 250
0fb67cd1 251 DeleteContents();
68379eaf
WS
252 Modify(false);
253 SetDocumentSaved(false);
c801d85f 254
724b119a 255 const wxString name = GetDocumentManager()->MakeNewDocumentName();
0fb67cd1 256 SetTitle(name);
68379eaf 257 SetFilename(name, true);
0fb67cd1 258
68379eaf 259 return true;
c801d85f
KB
260}
261
0fb67cd1 262bool wxDocument::Save()
c801d85f 263{
de56f240 264 if ( AlreadySaved() )
68379eaf 265 return true;
c801d85f 266
d06a66f5
VZ
267 if ( m_documentFile.empty() || !m_savedYet )
268 return SaveAs();
269
270 return OnSaveDocument(m_documentFile);
c801d85f 271}
0fb67cd1
VZ
272
273bool wxDocument::SaveAs()
c801d85f 274{
ba681060
VZ
275 wxDocTemplate *docTemplate = GetDocumentTemplate();
276 if (!docTemplate)
68379eaf 277 return false;
0fb67cd1 278
89d94e04
VZ
279#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
280 wxString filter = docTemplate->GetDescription() + wxT(" (") +
281 docTemplate->GetFileFilter() + wxT(")|") +
282 docTemplate->GetFileFilter();
04129514
JS
283
284 // Now see if there are some other template with identical view and document
285 // classes, whose filters may also be used.
04129514
JS
286 if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
287 {
89d94e04
VZ
288 wxList::compatibility_iterator
289 node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
04129514
JS
290 while (node)
291 {
292 wxDocTemplate *t = (wxDocTemplate*) node->GetData();
68379eaf 293
04129514
JS
294 if (t->IsVisible() && t != docTemplate &&
295 t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
296 t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
297 {
298 // add a '|' to separate this filter from the previous one
b494c48b 299 if ( !filter.empty() )
04129514 300 filter << wxT('|');
68379eaf 301
89d94e04
VZ
302 filter << t->GetDescription()
303 << wxT(" (") << t->GetFileFilter() << wxT(") |")
04129514
JS
304 << t->GetFileFilter();
305 }
306
307 node = node->GetNext();
308 }
309 }
310#else
311 wxString filter = docTemplate->GetFileFilter() ;
312#endif
89d94e04 313
e0214f1f 314 wxString defaultDir = docTemplate->GetDirectory();
d8efd219
VZ
315 if ( defaultDir.empty() )
316 {
e0214f1f 317 defaultDir = wxPathOnly(GetFilename());
d8efd219
VZ
318 if ( defaultDir.empty() )
319 defaultDir = GetDocumentManager()->GetLastDirectory();
320 }
e0214f1f 321
00e3ea1c 322 wxString fileName = wxFileSelector(_("Save As"),
e0214f1f 323 defaultDir,
015e69f3 324 wxFileNameFromPath(GetFilename()),
0fb67cd1 325 docTemplate->GetDefaultExtension(),
04129514 326 filter,
ff3e84ff 327 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
0fb67cd1
VZ
328 GetDocumentWindow());
329
00e3ea1c 330 if (fileName.empty())
89d94e04 331 return false; // cancelled by user
ba681060 332
00e3ea1c
FM
333 wxString ext;
334 wxFileName::SplitPath(fileName, NULL, NULL, &ext);
2bb0cd28 335
b494c48b 336 if (ext.empty())
2bb0cd28 337 {
2b5f62a0 338 fileName += wxT(".");
2bb0cd28
JS
339 fileName += docTemplate->GetDefaultExtension();
340 }
341
9d4a05be 342 // Files that were not saved correctly are not added to the FileHistory.
00e3ea1c 343 if (!OnSaveDocument(fileName))
68379eaf 344 return false;
9d4a05be 345
00e3ea1c
FM
346 SetTitle(wxFileNameFromPath(fileName));
347 SetFilename(fileName, true); // will call OnChangeFileName automatically
348
89d94e04
VZ
349 // A file that doesn't use the default extension of its document template
350 // cannot be opened via the FileHistory, so we do not add it.
9d4a05be
JS
351 if (docTemplate->FileMatchesTemplate(fileName))
352 {
353 GetDocumentManager()->AddFileToHistory(fileName);
354 }
89d94e04
VZ
355 //else: the user will probably not be able to open the file again, so we
356 // could warn about the wrong file-extension here
357
68379eaf 358 return true;
c801d85f 359}
0fb67cd1 360
c801d85f
KB
361bool wxDocument::OnSaveDocument(const wxString& file)
362{
0fb67cd1 363 if ( !file )
68379eaf 364 return false;
c801d85f 365
69936aea 366 if ( !DoSaveDocument(file) )
68379eaf 367 return false;
0fb67cd1 368
68379eaf 369 Modify(false);
0fb67cd1 370 SetFilename(file);
68379eaf 371 SetDocumentSaved(true);
26eef304 372#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
b8afeb43
SC
373 wxFileName fn(file) ;
374 fn.MacSetDefaultTypeAndCreator() ;
375#endif
68379eaf 376 return true;
c801d85f 377}
0fb67cd1 378
c801d85f
KB
379bool wxDocument::OnOpenDocument(const wxString& file)
380{
c77049a0 381 if ( !OnSaveModified() )
68379eaf 382 return false;
c801d85f 383
69936aea 384 if ( !DoOpenDocument(file) )
68379eaf 385 return false;
0fb67cd1 386
68379eaf
WS
387 SetFilename(file, true);
388 Modify(false);
389 m_savedYet = true;
0fb67cd1
VZ
390
391 UpdateAllViews();
c801d85f 392
68379eaf 393 return true;
c801d85f 394}
0fb67cd1 395
a533f5c1 396#if wxUSE_STD_IOSTREAM
dd107c50 397wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
23a54e14
RR
398#else
399wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
400#endif
c801d85f 401{
0fb67cd1 402 return stream;
c801d85f
KB
403}
404
23a54e14 405#if wxUSE_STD_IOSTREAM
dd107c50 406wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
a533f5c1 407#else
23a54e14
RR
408wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
409#endif
a533f5c1 410{
23a54e14 411 return stream;
a533f5c1 412}
c801d85f 413
0fb67cd1 414bool wxDocument::Revert()
c801d85f 415{
68379eaf 416 return false;
c801d85f
KB
417}
418
419
420// Get title, or filename if no title, else unnamed
724b119a 421#if WXWIN_COMPATIBILITY_2_8
c801d85f
KB
422bool wxDocument::GetPrintableName(wxString& buf) const
423{
724b119a
VZ
424 // this function can not only be overridden by the user code but also
425 // called by it so we need to ensure that we return the same thing as
426 // GetUserReadableName() but we can't call it because this would result in
427 // an infinite recursion, hence we use the helper DoGetUserReadableName()
428 buf = DoGetUserReadableName();
429
430 return true;
431}
432#endif // WXWIN_COMPATIBILITY_2_8
433
434wxString wxDocument::GetUserReadableName() const
435{
436#if WXWIN_COMPATIBILITY_2_8
437 // we need to call the old virtual function to ensure that the overridden
438 // version of it is still called
439 wxString name;
440 if ( GetPrintableName(name) )
441 return name;
442#endif // WXWIN_COMPATIBILITY_2_8
443
444 return DoGetUserReadableName();
445}
446
447wxString wxDocument::DoGetUserReadableName() const
448{
449 if ( !m_documentTitle.empty() )
450 return m_documentTitle;
451
452 if ( !m_documentFile.empty() )
453 return wxFileNameFromPath(m_documentFile);
454
455 return _("unnamed");
c801d85f
KB
456}
457
6de2f8b9 458wxWindow *wxDocument::GetDocumentWindow() const
c801d85f 459{
89d94e04
VZ
460 wxView * const view = GetFirstView();
461
462 return view ? view->GetFrame() : wxTheApp->GetTopWindow();
c801d85f
KB
463}
464
0fb67cd1 465wxCommandProcessor *wxDocument::OnCreateCommandProcessor()
c801d85f 466{
0fb67cd1 467 return new wxCommandProcessor;
c801d85f
KB
468}
469
68379eaf 470// true if safe to close
0fb67cd1 471bool wxDocument::OnSaveModified()
c801d85f 472{
c77049a0
VZ
473 if ( IsModified() )
474 {
475 switch ( wxMessageBox
476 (
477 wxString::Format
478 (
4f57a7bf 479 _("Do you want to save changes to %s?"),
c77049a0
VZ
480 GetUserReadableName()
481 ),
482 wxTheApp->GetAppDisplayName(),
00e3ea1c
FM
483 wxYES_NO | wxCANCEL | wxICON_QUESTION | wxCENTRE,
484 wxFindSuitableParent()
c77049a0 485 ) )
0fb67cd1 486 {
c77049a0
VZ
487 case wxNO:
488 Modify(false);
489 break;
490
491 case wxYES:
492 return Save();
493
494 case wxCANCEL:
495 return false;
0fb67cd1 496 }
c801d85f 497 }
c77049a0 498
68379eaf 499 return true;
c801d85f
KB
500}
501
502bool wxDocument::Draw(wxDC& WXUNUSED(context))
503{
68379eaf 504 return true;
c801d85f
KB
505}
506
507bool wxDocument::AddView(wxView *view)
508{
c77049a0 509 if ( !m_documentViews.Member(view) )
0fb67cd1
VZ
510 {
511 m_documentViews.Append(view);
512 OnChangedViewList();
513 }
68379eaf 514 return true;
c801d85f
KB
515}
516
517bool wxDocument::RemoveView(wxView *view)
518{
0fb67cd1
VZ
519 (void)m_documentViews.DeleteObject(view);
520 OnChangedViewList();
68379eaf 521 return true;
c801d85f
KB
522}
523
524bool wxDocument::OnCreate(const wxString& WXUNUSED(path), long flags)
525{
c77049a0 526 return GetDocumentTemplate()->CreateView(this, flags) != NULL;
c801d85f
KB
527}
528
529// Called after a view is added or removed.
530// The default implementation deletes the document if
531// there are no more views.
0fb67cd1 532void wxDocument::OnChangedViewList()
c801d85f 533{
c77049a0
VZ
534 if ( m_documentViews.empty() && OnSaveModified() )
535 delete this;
c801d85f
KB
536}
537
538void wxDocument::UpdateAllViews(wxView *sender, wxObject *hint)
539{
222ed1d6 540 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
541 while (node)
542 {
b1d4dd7a 543 wxView *view = (wxView *)node->GetData();
42771621 544 if (view != sender)
2b5f62a0 545 view->OnUpdate(sender, hint);
b1d4dd7a 546 node = node->GetNext();
0fb67cd1 547 }
c801d85f
KB
548}
549
b23e843b
JS
550void wxDocument::NotifyClosing()
551{
222ed1d6 552 wxList::compatibility_iterator node = m_documentViews.GetFirst();
b23e843b
JS
553 while (node)
554 {
b1d4dd7a 555 wxView *view = (wxView *)node->GetData();
b23e843b 556 view->OnClosingDocument();
b1d4dd7a 557 node = node->GetNext();
b23e843b
JS
558 }
559}
560
c801d85f
KB
561void wxDocument::SetFilename(const wxString& filename, bool notifyViews)
562{
0fb67cd1 563 m_documentFile = filename;
00e3ea1c
FM
564 OnChangeFilename(notifyViews);
565}
566
567void wxDocument::OnChangeFilename(bool notifyViews)
568{
0fb67cd1 569 if ( notifyViews )
c801d85f 570 {
0fb67cd1 571 // Notify the views that the filename has changed
222ed1d6 572 wxList::compatibility_iterator node = m_documentViews.GetFirst();
0fb67cd1
VZ
573 while (node)
574 {
b1d4dd7a 575 wxView *view = (wxView *)node->GetData();
0fb67cd1 576 view->OnChangeFilename();
b1d4dd7a 577 node = node->GetNext();
0fb67cd1 578 }
c801d85f 579 }
c801d85f
KB
580}
581
69936aea
VZ
582bool wxDocument::DoSaveDocument(const wxString& file)
583{
69936aea 584#if wxUSE_STD_IOSTREAM
53e2e87c 585 wxSTD ofstream store(file.mb_str(), wxSTD ios::binary);
c09b7217 586 if ( !store )
69936aea
VZ
587#else
588 wxFileOutputStream store(file);
c09b7217 589 if ( store.GetLastError() != wxSTREAM_NO_ERROR )
69936aea
VZ
590#endif
591 {
e58eaff7 592 wxLogError(_("File \"%s\" could not be opened for writing."), file);
68379eaf 593 return false;
69936aea 594 }
e58eaff7 595
69936aea
VZ
596 if (!SaveObject(store))
597 {
e58eaff7 598 wxLogError(_("Failed to save document to the file \"%s\"."), file);
68379eaf 599 return false;
69936aea
VZ
600 }
601
68379eaf 602 return true;
69936aea
VZ
603}
604
605bool wxDocument::DoOpenDocument(const wxString& file)
606{
69936aea 607#if wxUSE_STD_IOSTREAM
53e2e87c 608 wxSTD ifstream store(file.mb_str(), wxSTD ios::binary);
c09b7217 609 if ( !store )
69936aea
VZ
610#else
611 wxFileInputStream store(file);
9690b006 612 if (store.GetLastError() != wxSTREAM_NO_ERROR || !store.IsOk())
69936aea
VZ
613#endif
614 {
e58eaff7
VZ
615 wxLogError(_("File \"%s\" could not be opened for reading."), file);
616 return false;
617 }
618
69936aea 619#if wxUSE_STD_IOSTREAM
e58eaff7 620 LoadObject(store);
c09b7217 621 if ( !store )
69936aea 622#else
e58eaff7
VZ
623 int res = LoadObject(store).GetLastError();
624 if ( res != wxSTREAM_NO_ERROR && res != wxSTREAM_EOF )
69936aea 625#endif
e58eaff7
VZ
626 {
627 wxLogError(_("Failed to read document from the file \"%s\"."), file);
628 return false;
69936aea
VZ
629 }
630
e58eaff7 631 return true;
69936aea
VZ
632}
633
634
0fb67cd1
VZ
635// ----------------------------------------------------------------------------
636// Document view
637// ----------------------------------------------------------------------------
c801d85f 638
dbdb39b2 639wxView::wxView()
c801d85f 640{
c77049a0 641 m_viewDocument = NULL;
0fb67cd1 642
c77049a0 643 m_viewFrame = NULL;
c801d85f
KB
644}
645
0fb67cd1 646wxView::~wxView()
c801d85f 647{
68379eaf 648 GetDocumentManager()->ActivateView(this, false);
0fb67cd1 649 m_viewDocument->RemoveView(this);
c801d85f
KB
650}
651
bba5e72a 652bool wxView::TryValidator(wxEvent& event)
c801d85f 653{
bba5e72a
VZ
654 wxDocument * const doc = GetDocument();
655 return doc && doc->ProcessEventHere(event);
c801d85f
KB
656}
657
89d94e04
VZ
658void wxView::OnActivateView(bool WXUNUSED(activate),
659 wxView *WXUNUSED(activeView),
660 wxView *WXUNUSED(deactiveView))
c801d85f
KB
661{
662}
663
664void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
665{
0fb67cd1 666 OnDraw(dc);
c801d85f
KB
667}
668
669void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
670{
671}
672
0fb67cd1 673void wxView::OnChangeFilename()
c801d85f 674{
faa49bfd
WS
675 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
676 // generic MDI implementation so use SetLabel rather than SetTitle.
677 // It should cause SetTitle() for top level windows.
678 wxWindow *win = GetFrame();
679 if (!win) return;
f2506310 680
faa49bfd
WS
681 wxDocument *doc = GetDocument();
682 if (!doc) return;
c801d85f 683
724b119a 684 win->SetLabel(doc->GetUserReadableName());
c801d85f
KB
685}
686
687void wxView::SetDocument(wxDocument *doc)
688{
0fb67cd1
VZ
689 m_viewDocument = doc;
690 if (doc)
691 doc->AddView(this);
c801d85f
KB
692}
693
694bool wxView::Close(bool deleteWindow)
695{
c77049a0 696 return OnClose(deleteWindow);
c801d85f
KB
697}
698
699void wxView::Activate(bool activate)
700{
bb28b477 701 if (GetDocument() && GetDocumentManager())
0fb67cd1
VZ
702 {
703 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
704 GetDocumentManager()->ActivateView(this, activate);
705 }
c801d85f
KB
706}
707
708bool wxView::OnClose(bool WXUNUSED(deleteWindow))
709{
68379eaf 710 return GetDocument() ? GetDocument()->Close() : true;
c801d85f
KB
711}
712
47d67540 713#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1 714wxPrintout *wxView::OnCreatePrintout()
c801d85f 715{
0fb67cd1 716 return new wxDocPrintout(this);
c801d85f 717}
6de2f8b9 718#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 719
0fb67cd1
VZ
720// ----------------------------------------------------------------------------
721// wxDocTemplate
722// ----------------------------------------------------------------------------
c801d85f 723
0fb67cd1
VZ
724wxDocTemplate::wxDocTemplate(wxDocManager *manager,
725 const wxString& descr,
726 const wxString& filter,
727 const wxString& dir,
728 const wxString& ext,
729 const wxString& docTypeName,
730 const wxString& viewTypeName,
731 wxClassInfo *docClassInfo,
732 wxClassInfo *viewClassInfo,
733 long flags)
c801d85f 734{
0fb67cd1 735 m_documentManager = manager;
0fb67cd1
VZ
736 m_description = descr;
737 m_directory = dir;
738 m_defaultExt = ext;
739 m_fileFilter = filter;
740 m_flags = flags;
741 m_docTypeName = docTypeName;
742 m_viewTypeName = viewTypeName;
743 m_documentManager->AssociateTemplate(this);
c801d85f 744
0fb67cd1
VZ
745 m_docClassInfo = docClassInfo;
746 m_viewClassInfo = viewClassInfo;
c801d85f
KB
747}
748
0fb67cd1 749wxDocTemplate::~wxDocTemplate()
c801d85f 750{
0fb67cd1 751 m_documentManager->DisassociateTemplate(this);
c801d85f 752}
0fb67cd1
VZ
753
754// Tries to dynamically construct an object of the right class.
c801d85f
KB
755wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
756{
c77049a0 757 wxDocument * const doc = DoCreateDocument();
68379eaf 758
c77049a0
VZ
759 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
760 // fails, is this intentional?
761
762 return doc && InitDocument(doc, path, flags) ? doc : NULL;
217b7140
JS
763}
764
c77049a0
VZ
765bool
766wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
217b7140 767{
0fb67cd1
VZ
768 doc->SetFilename(path);
769 doc->SetDocumentTemplate(this);
770 GetDocumentManager()->AddDocument(doc);
771 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
772
773 if (doc->OnCreate(path, flags))
217b7140 774 return true;
89d94e04
VZ
775
776 if (GetDocumentManager()->GetDocuments().Member(doc))
777 doc->DeleteAllViews();
778 return false;
c801d85f
KB
779}
780
781wxView *wxDocTemplate::CreateView(wxDocument *doc, long flags)
782{
7047d798
VZ
783 wxScopedPtr<wxView> view(DoCreateView());
784 if ( !view )
c77049a0 785 return NULL;
69936aea 786
0fb67cd1 787 view->SetDocument(doc);
7047d798 788 if ( !view->OnCreate(doc, flags) )
c77049a0 789 return NULL;
7047d798
VZ
790
791 return view.release();
c801d85f
KB
792}
793
6de2f8b9
VZ
794// The default (very primitive) format detection: check is the extension is
795// that of the template
796bool wxDocTemplate::FileMatchesTemplate(const wxString& path)
797{
0f30d8e3
WS
798 wxStringTokenizer parser (GetFileFilter(), wxT(";"));
799 wxString anything = wxT ("*");
800 while (parser.HasMoreTokens())
801 {
802 wxString filter = parser.GetNextToken();
803 wxString filterExt = FindExtension (filter);
804 if ( filter.IsSameAs (anything) ||
805 filterExt.IsSameAs (anything) ||
806 filterExt.IsSameAs (FindExtension (path)) )
807 return true;
808 }
6de2f8b9
VZ
809 return GetDefaultExtension().IsSameAs(FindExtension(path));
810}
811
69936aea
VZ
812wxDocument *wxDocTemplate::DoCreateDocument()
813{
814 if (!m_docClassInfo)
c77049a0 815 return NULL;
69936aea 816
89d94e04 817 return static_cast<wxDocument *>(m_docClassInfo->CreateObject());
69936aea
VZ
818}
819
820wxView *wxDocTemplate::DoCreateView()
821{
822 if (!m_viewClassInfo)
c77049a0 823 return NULL;
69936aea 824
89d94e04 825 return static_cast<wxView *>(m_viewClassInfo->CreateObject());
69936aea
VZ
826}
827
0fb67cd1
VZ
828// ----------------------------------------------------------------------------
829// wxDocManager
830// ----------------------------------------------------------------------------
831
c801d85f
KB
832BEGIN_EVENT_TABLE(wxDocManager, wxEvtHandler)
833 EVT_MENU(wxID_OPEN, wxDocManager::OnFileOpen)
834 EVT_MENU(wxID_CLOSE, wxDocManager::OnFileClose)
33a20136 835 EVT_MENU(wxID_CLOSE_ALL, wxDocManager::OnFileCloseAll)
c801d85f
KB
836 EVT_MENU(wxID_REVERT, wxDocManager::OnFileRevert)
837 EVT_MENU(wxID_NEW, wxDocManager::OnFileNew)
838 EVT_MENU(wxID_SAVE, wxDocManager::OnFileSave)
839 EVT_MENU(wxID_SAVEAS, wxDocManager::OnFileSaveAs)
840 EVT_MENU(wxID_UNDO, wxDocManager::OnUndo)
841 EVT_MENU(wxID_REDO, wxDocManager::OnRedo)
f2506310
JS
842
843 EVT_UPDATE_UI(wxID_OPEN, wxDocManager::OnUpdateFileOpen)
c77049a0
VZ
844 EVT_UPDATE_UI(wxID_CLOSE, wxDocManager::OnUpdateDisableIfNoDoc)
845 EVT_UPDATE_UI(wxID_CLOSE_ALL, wxDocManager::OnUpdateDisableIfNoDoc)
846 EVT_UPDATE_UI(wxID_REVERT, wxDocManager::OnUpdateDisableIfNoDoc)
f2506310
JS
847 EVT_UPDATE_UI(wxID_NEW, wxDocManager::OnUpdateFileNew)
848 EVT_UPDATE_UI(wxID_SAVE, wxDocManager::OnUpdateFileSave)
c77049a0 849 EVT_UPDATE_UI(wxID_SAVEAS, wxDocManager::OnUpdateDisableIfNoDoc)
f2506310
JS
850 EVT_UPDATE_UI(wxID_UNDO, wxDocManager::OnUpdateUndo)
851 EVT_UPDATE_UI(wxID_REDO, wxDocManager::OnUpdateRedo)
852
ce4169a4 853#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 854 EVT_MENU(wxID_PRINT, wxDocManager::OnPrint)
c801d85f 855 EVT_MENU(wxID_PREVIEW, wxDocManager::OnPreview)
f2506310 856
c77049a0
VZ
857 EVT_UPDATE_UI(wxID_PRINT, wxDocManager::OnUpdateDisableIfNoDoc)
858 EVT_UPDATE_UI(wxID_PREVIEW, wxDocManager::OnUpdateDisableIfNoDoc)
ce4169a4 859#endif
c801d85f
KB
860END_EVENT_TABLE()
861
c77049a0 862wxDocManager* wxDocManager::sm_docManager = NULL;
f2506310 863
c77049a0 864wxDocManager::wxDocManager(long WXUNUSED(flags), bool initialize)
c801d85f 865{
c77049a0
VZ
866 wxASSERT_MSG( !sm_docManager, "multiple wxDocManagers not allowed" );
867
868 sm_docManager = this;
869
0fb67cd1 870 m_defaultDocumentNameCounter = 1;
c77049a0
VZ
871 m_currentView = NULL;
872 m_maxDocsOpen = INT_MAX;
873 m_fileHistory = NULL;
874 if ( initialize )
0fb67cd1 875 Initialize();
c801d85f
KB
876}
877
0fb67cd1 878wxDocManager::~wxDocManager()
c801d85f 879{
0fb67cd1 880 Clear();
c77049a0
VZ
881 delete m_fileHistory;
882 sm_docManager = NULL;
c801d85f
KB
883}
884
b72b1920
JS
885// closes the specified document
886bool wxDocManager::CloseDocument(wxDocument* doc, bool force)
887{
89d94e04
VZ
888 if ( !doc->Close() && !force )
889 return false;
b72b1920 890
89d94e04
VZ
891 // Implicitly deletes the document when
892 // the last view is deleted
893 doc->DeleteAllViews();
42771621 894
89d94e04
VZ
895 // Check we're really deleted
896 if (m_docs.Member(doc))
897 delete doc;
898
899 return true;
b72b1920
JS
900}
901
33a20136 902bool wxDocManager::CloseDocuments(bool force)
c801d85f 903{
222ed1d6 904 wxList::compatibility_iterator node = m_docs.GetFirst();
0fb67cd1
VZ
905 while (node)
906 {
b1d4dd7a 907 wxDocument *doc = (wxDocument *)node->GetData();
222ed1d6 908 wxList::compatibility_iterator next = node->GetNext();
68379eaf 909
b72b1920 910 if (!CloseDocument(doc, force))
68379eaf 911 return false;
0fb67cd1 912
0fb67cd1
VZ
913 // This assumes that documents are not connected in
914 // any way, i.e. deleting one document does NOT
915 // delete another.
916 node = next;
917 }
68379eaf 918 return true;
33a20136
VZ
919}
920
921bool wxDocManager::Clear(bool force)
922{
923 if (!CloseDocuments(force))
68379eaf 924 return false;
33a20136 925
8af94d3b
VZ
926 m_currentView = NULL;
927
222ed1d6 928 wxList::compatibility_iterator node = m_templates.GetFirst();
0fb67cd1
VZ
929 while (node)
930 {
b1d4dd7a 931 wxDocTemplate *templ = (wxDocTemplate*) node->GetData();
222ed1d6 932 wxList::compatibility_iterator next = node->GetNext();
c801d85f
KB
933 delete templ;
934 node = next;
0fb67cd1 935 }
68379eaf 936 return true;
c801d85f
KB
937}
938
0fb67cd1 939bool wxDocManager::Initialize()
c801d85f 940{
0fb67cd1 941 m_fileHistory = OnCreateFileHistory();
68379eaf 942 return true;
c801d85f
KB
943}
944
d8efd219
VZ
945wxString wxDocManager::GetLastDirectory() const
946{
947 // use the system-dependent default location for the document files if
948 // we're being opened for the first time
949 if ( m_lastDirectory.empty() )
950 {
951 wxDocManager * const self = const_cast<wxDocManager *>(this);
952 self->m_lastDirectory = wxStandardPaths::Get().GetAppDocumentsDir();
953 }
954
955 return m_lastDirectory;
956}
957
0fb67cd1 958wxFileHistory *wxDocManager::OnCreateFileHistory()
c801d85f 959{
0fb67cd1 960 return new wxFileHistory;
c801d85f
KB
961}
962
963void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
964{
0fb67cd1
VZ
965 wxDocument *doc = GetCurrentDocument();
966 if (!doc)
967 return;
968 if (doc->Close())
969 {
970 doc->DeleteAllViews();
971 if (m_docs.Member(doc))
972 delete doc;
973 }
c801d85f
KB
974}
975
33a20136
VZ
976void wxDocManager::OnFileCloseAll(wxCommandEvent& WXUNUSED(event))
977{
68379eaf 978 CloseDocuments(false);
33a20136
VZ
979}
980
c801d85f
KB
981void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
982{
b5412983 983 CreateNewDocument();
c801d85f
KB
984}
985
986void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
987{
89d94e04 988 if ( !CreateDocument("") )
5f170f33
VZ
989 {
990 OnOpenFileFailure();
991 }
c801d85f
KB
992}
993
994void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
995{
0fb67cd1
VZ
996 wxDocument *doc = GetCurrentDocument();
997 if (!doc)
998 return;
999 doc->Revert();
c801d85f
KB
1000}
1001
1002void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
1003{
0fb67cd1
VZ
1004 wxDocument *doc = GetCurrentDocument();
1005 if (!doc)
1006 return;
1007 doc->Save();
c801d85f
KB
1008}
1009
1010void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
1011{
0fb67cd1
VZ
1012 wxDocument *doc = GetCurrentDocument();
1013 if (!doc)
1014 return;
1015 doc->SaveAs();
c801d85f
KB
1016}
1017
1018void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
1019{
88ac883a 1020#if wxUSE_PRINTING_ARCHITECTURE
575e4cd6 1021 wxView *view = GetActiveView();
0fb67cd1
VZ
1022 if (!view)
1023 return;
c801d85f 1024
0fb67cd1
VZ
1025 wxPrintout *printout = view->OnCreatePrintout();
1026 if (printout)
1027 {
1028 wxPrinter printer;
68379eaf 1029 printer.Print(view->GetFrame(), printout, true);
c801d85f 1030
0fb67cd1
VZ
1031 delete printout;
1032 }
88ac883a 1033#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1034}
1035
c801d85f
KB
1036void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
1037{
88ac883a 1038#if wxUSE_PRINTING_ARCHITECTURE
575e4cd6 1039 wxView *view = GetActiveView();
0fb67cd1
VZ
1040 if (!view)
1041 return;
c801d85f 1042
0fb67cd1
VZ
1043 wxPrintout *printout = view->OnCreatePrintout();
1044 if (printout)
1045 {
1046 // Pass two printout objects: for preview, and possible printing.
89d94e04
VZ
1047 wxPrintPreviewBase *
1048 preview = new wxPrintPreview(printout, view->OnCreatePrintout());
3f8b4a3f
JS
1049 if ( !preview->Ok() )
1050 {
1051 delete preview;
89d94e04 1052 wxLogError(_("Print preview creation failed."));
3f8b4a3f
JS
1053 return;
1054 }
0fb67cd1 1055
89d94e04
VZ
1056 wxPreviewFrame *
1057 frame = new wxPreviewFrame(preview, wxTheApp->GetTopWindow(),
1058 _("Print Preview"));
0fb67cd1
VZ
1059 frame->Centre(wxBOTH);
1060 frame->Initialize();
68379eaf 1061 frame->Show(true);
0fb67cd1 1062 }
88ac883a 1063#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1064}
1065
34aa6140 1066void wxDocManager::OnUndo(wxCommandEvent& event)
c801d85f 1067{
89d94e04
VZ
1068 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1069 if ( !cmdproc )
1070 {
34aa6140 1071 event.Skip();
89d94e04
VZ
1072 return;
1073 }
1074
1075 cmdproc->Undo();
c801d85f
KB
1076}
1077
34aa6140 1078void wxDocManager::OnRedo(wxCommandEvent& event)
c801d85f 1079{
89d94e04
VZ
1080 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1081 if ( !cmdproc )
1082 {
34aa6140 1083 event.Skip();
89d94e04
VZ
1084 return;
1085 }
1086
1087 cmdproc->Redo();
c801d85f
KB
1088}
1089
f2506310
JS
1090// Handlers for UI update commands
1091
1092void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1093{
84cae69a
FM
1094 // CreateDocument() (which is called from OnFileOpen) may succeed
1095 // only when there is at least a template:
1096 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1097}
1098
c77049a0 1099void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
f2506310 1100{
c77049a0 1101 event.Enable( GetCurrentDocument() != NULL );
f2506310
JS
1102}
1103
1104void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1105{
84cae69a
FM
1106 // CreateDocument() (which is called from OnFileNew) may succeed
1107 // only when there is at least a template:
1108 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1109}
1110
1111void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1112{
de56f240
VZ
1113 wxDocument * const doc = GetCurrentDocument();
1114 event.Enable( doc && !doc->AlreadySaved() );
f2506310
JS
1115}
1116
f2506310
JS
1117void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1118{
89d94e04
VZ
1119 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1120 if ( !cmdproc )
34aa6140 1121 {
89d94e04
VZ
1122 event.Enable(false);
1123 return;
34aa6140 1124 }
89d94e04
VZ
1125
1126 event.Enable(cmdproc->CanUndo());
1127 cmdproc->SetMenuStrings();
f2506310
JS
1128}
1129
1130void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1131{
89d94e04
VZ
1132 wxCommandProcessor * const cmdproc = GetCurrentCommandProcessor();
1133 if ( !cmdproc )
34aa6140 1134 {
89d94e04
VZ
1135 event.Enable(false);
1136 return;
34aa6140 1137 }
89d94e04
VZ
1138
1139 event.Enable(cmdproc->CanRedo());
1140 cmdproc->SetMenuStrings();
f2506310
JS
1141}
1142
575e4cd6 1143wxView *wxDocManager::GetActiveView() const
637f467a 1144{
575e4cd6
VZ
1145 wxView *view = GetCurrentView();
1146
1147 if ( !view && !m_docs.empty() )
637f467a 1148 {
575e4cd6
VZ
1149 // if we have exactly one document, consider its view to be the current
1150 // one
1151 //
1152 // VZ: I'm not exactly sure why is this needed but this is how this
1153 // code used to behave before the bug #9518 was fixed and it seems
1154 // safer to preserve the old logic
1155 wxList::compatibility_iterator node = m_docs.GetFirst();
1156 if ( !node->GetNext() )
1157 {
1158 wxDocument *doc = static_cast<wxDocument *>(node->GetData());
1159 view = doc->GetFirstView();
1160 }
1161 //else: we have more than one document
637f467a 1162 }
575e4cd6
VZ
1163
1164 return view;
637f467a
JS
1165}
1166
bba5e72a 1167bool wxDocManager::TryValidator(wxEvent& event)
637f467a 1168{
575e4cd6 1169 wxView * const view = GetActiveView();
bba5e72a 1170 return view && view->ProcessEventHere(event);
637f467a
JS
1171}
1172
c77049a0 1173namespace
c801d85f 1174{
b1d4dd7a 1175
c77049a0
VZ
1176// helper function: return only the visible templates
1177wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
1178{
1179 // select only the visible templates
1180 const size_t totalNumTemplates = allTemplates.GetCount();
1181 wxDocTemplates templates;
1182 if ( totalNumTemplates )
c801d85f 1183 {
c77049a0
VZ
1184 templates.reserve(totalNumTemplates);
1185
1186 for ( wxList::const_iterator i = allTemplates.begin(),
1187 end = allTemplates.end();
1188 i != end;
1189 ++i )
0fb67cd1 1190 {
c77049a0
VZ
1191 wxDocTemplate * const temp = (wxDocTemplate *)*i;
1192 if ( temp->IsVisible() )
1193 templates.push_back(temp);
0fb67cd1 1194 }
c801d85f 1195 }
42771621 1196
c77049a0
VZ
1197 return templates;
1198}
0fb67cd1 1199
c77049a0
VZ
1200} // anonymous namespace
1201
1202wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
1203{
1204 // this ought to be const but SelectDocumentType/Path() are not
1205 // const-correct and can't be changed as, being virtual, this risks
1206 // breaking user code overriding them
1207 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1208 const size_t numTemplates = templates.size();
1209 if ( !numTemplates )
c801d85f 1210 {
c77049a0
VZ
1211 // no templates can be used, can't create document
1212 return NULL;
c801d85f
KB
1213 }
1214
42771621 1215
c77049a0
VZ
1216 // normally user should select the template to use but wxDOC_SILENT flag we
1217 // choose one ourselves
1218 wxString path = pathOrig; // may be modified below
1219 wxDocTemplate *temp;
1220 if ( flags & wxDOC_SILENT )
1221 {
1222 wxASSERT_MSG( !path.empty(),
1223 "using empty path with wxDOC_SILENT doesn't make sense" );
42771621 1224
c77049a0
VZ
1225 temp = FindTemplateForPath(path);
1226 if ( !temp )
1227 {
1228 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1229 path);
0fb67cd1 1230 }
c77049a0
VZ
1231 }
1232 else // not silent, ask the user
1233 {
1234 // for the new file we need just the template, for an existing one we
1235 // need the template and the path, unless it's already specified
1236 if ( (flags & wxDOC_NEW) || !path.empty() )
1237 temp = SelectDocumentType(&templates[0], numTemplates);
1238 else
1239 temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
1240 }
0fb67cd1 1241
c77049a0
VZ
1242 if ( !temp )
1243 return NULL;
42771621 1244
c77049a0
VZ
1245 // check whether the document with this path is already opened
1246 if ( !path.empty() )
1247 {
1248 const wxFileName fn(path);
1249 for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
1250 {
1251 wxDocument * const doc = (wxDocument*)*i;
b72b1920 1252
c77049a0 1253 if ( fn == doc->GetFilename() )
0fb67cd1 1254 {
c77049a0
VZ
1255 // file already open, just activate it and return
1256 if ( doc->GetFirstView() )
f5729a6b 1257 {
c77049a0
VZ
1258 ActivateView(doc->GetFirstView());
1259 if ( doc->GetDocumentWindow() )
1260 doc->GetDocumentWindow()->SetFocus();
1261 return doc;
f5729a6b 1262 }
0fb67cd1 1263 }
0fb67cd1 1264 }
c801d85f 1265 }
c801d85f 1266
c801d85f 1267
c77049a0 1268 // no, we need to create a new document
c801d85f 1269
c77049a0
VZ
1270
1271 // if we've reached the max number of docs, close the first one.
1272 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
9d4a05be 1273 {
c77049a0 1274 if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
9d4a05be 1275 {
c77049a0
VZ
1276 // can't open the new document if closing the old one failed
1277 return NULL;
9d4a05be
JS
1278 }
1279 }
c801d85f 1280
c801d85f 1281
c77049a0
VZ
1282 // do create and initialize the new document finally
1283 wxDocument * const docNew = temp->CreateDocument(path, flags);
1284 if ( !docNew )
1285 return NULL;
42771621 1286
c77049a0
VZ
1287 docNew->SetDocumentName(temp->GetDocumentName());
1288 docNew->SetDocumentTemplate(temp);
6489e7ee 1289
c77049a0
VZ
1290 // call the appropriate function depending on whether we're creating a new
1291 // file or opening an existing one
1292 if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
1293 : docNew->OnOpenDocument(path)) )
1294 {
1295 // Document is implicitly deleted by DeleteAllViews
1296 docNew->DeleteAllViews();
1297 return NULL;
c801d85f 1298 }
3ca6a5f0 1299
c77049a0
VZ
1300 // add the successfully opened file to MRU, but only if we're going to be
1301 // able to reopen it successfully later which requires the template for
1302 // this document to be retrievable from the file extension
1303 if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
1304 AddFileToHistory(path);
1305
1306 return docNew;
c801d85f
KB
1307}
1308
1309wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1310{
c77049a0
VZ
1311 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1312 const size_t numTemplates = templates.size();
b1d4dd7a 1313
c77049a0
VZ
1314 if ( numTemplates == 0 )
1315 return NULL;
0fb67cd1 1316
c77049a0
VZ
1317 wxDocTemplate * const
1318 temp = numTemplates == 1 ? templates[0]
1319 : SelectViewType(&templates[0], numTemplates);
1320
1321 if ( !temp )
1322 return NULL;
1323
1324 wxView *view = temp->CreateView(doc, flags);
1325 if ( view )
1326 view->SetViewName(temp->GetViewName());
1327 return view;
c801d85f
KB
1328}
1329
1330// Not yet implemented
c77049a0
VZ
1331void
1332wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
c801d85f
KB
1333{
1334}
1335
1336// Not yet implemented
1337bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1338{
68379eaf 1339 return false;
c801d85f
KB
1340}
1341
6de2f8b9 1342wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1343{
575e4cd6 1344 wxView * const view = GetActiveView();
89d94e04
VZ
1345 return view ? view->GetDocument() : NULL;
1346}
1347
1348wxCommandProcessor *wxDocManager::GetCurrentCommandProcessor() const
1349{
1350 wxDocument * const doc = GetCurrentDocument();
1351 return doc ? doc->GetCommandProcessor() : NULL;
c801d85f
KB
1352}
1353
724b119a
VZ
1354// Make a default name for a new document
1355#if WXWIN_COMPATIBILITY_2_8
1356bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1357{
1358 // we consider that this function can only be overridden by the user code,
1359 // not called by it as it only makes sense to call it internally, so we
1360 // don't bother to return anything from here
1361 return false;
1362}
1363#endif // WXWIN_COMPATIBILITY_2_8
1364
1365wxString wxDocManager::MakeNewDocumentName()
c801d85f 1366{
724b119a 1367 wxString name;
53c6e7cc 1368
724b119a
VZ
1369#if WXWIN_COMPATIBILITY_2_8
1370 if ( !MakeDefaultName(name) )
1371#endif // WXWIN_COMPATIBILITY_2_8
1372 {
1373 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1374 m_defaultDocumentNameCounter++;
1375 }
1376
1377 return name;
c801d85f
KB
1378}
1379
f2506310
JS
1380// Make a frame title (override this to do something different)
1381// If docName is empty, a document is not currently active.
1382wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1383{
9cf3d218 1384 wxString appName = wxTheApp->GetAppDisplayName();
f2506310
JS
1385 wxString title;
1386 if (!doc)
1387 title = appName;
1388 else
1389 {
724b119a 1390 wxString docName = doc->GetUserReadableName();
f2506310
JS
1391 title = docName + wxString(_(" - ")) + appName;
1392 }
1393 return title;
1394}
1395
1396
c801d85f
KB
1397// Not yet implemented
1398wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1399{
c77049a0 1400 return NULL;
c801d85f
KB
1401}
1402
1403// File history management
1404void wxDocManager::AddFileToHistory(const wxString& file)
1405{
0fb67cd1
VZ
1406 if (m_fileHistory)
1407 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1408}
1409
e49c85af 1410void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1411{
1412 if (m_fileHistory)
1413 m_fileHistory->RemoveFileFromHistory(i);
1414}
1415
e49c85af 1416wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1417{
0fb67cd1
VZ
1418 wxString histFile;
1419
1420 if (m_fileHistory)
1421 histFile = m_fileHistory->GetHistoryFile(i);
1422
1423 return histFile;
c801d85f
KB
1424}
1425
1426void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1427{
0fb67cd1
VZ
1428 if (m_fileHistory)
1429 m_fileHistory->UseMenu(menu);
c801d85f
KB
1430}
1431
7f555861 1432void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1433{
0fb67cd1
VZ
1434 if (m_fileHistory)
1435 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1436}
1437
702ca7c0 1438#if wxUSE_CONFIG
9c8116f8 1439void wxDocManager::FileHistoryLoad(const wxConfigBase& config)
c801d85f 1440{
0fb67cd1
VZ
1441 if (m_fileHistory)
1442 m_fileHistory->Load(config);
7f555861
JS
1443}
1444
1445void wxDocManager::FileHistorySave(wxConfigBase& config)
1446{
0fb67cd1
VZ
1447 if (m_fileHistory)
1448 m_fileHistory->Save(config);
7f555861 1449}
ac57418f 1450#endif
7f555861
JS
1451
1452void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1453{
0fb67cd1
VZ
1454 if (m_fileHistory)
1455 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1456}
1457
1458void wxDocManager::FileHistoryAddFilesToMenu()
1459{
0fb67cd1
VZ
1460 if (m_fileHistory)
1461 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1462}
1463
7af6b69e 1464size_t wxDocManager::GetHistoryFilesCount() const
c801d85f 1465{
7af6b69e 1466 return m_fileHistory ? m_fileHistory->GetCount() : 0;
c801d85f
KB
1467}
1468
1469
6de2f8b9
VZ
1470// Find out the document template via matching in the document file format
1471// against that of the template
c801d85f
KB
1472wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1473{
c77049a0 1474 wxDocTemplate *theTemplate = NULL;
c801d85f 1475
0fb67cd1 1476 // Find the template which this extension corresponds to
b1d4dd7a 1477 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1478 {
b1d4dd7a 1479 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1480 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1481 {
1482 theTemplate = temp;
1483 break;
1484 }
c801d85f 1485 }
0fb67cd1 1486 return theTemplate;
c801d85f
KB
1487}
1488
1489// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1490// Must extend the file selector dialog or implement own; OR
1491// match the extension to the template extension.
df875e59 1492
c801d85f 1493wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
0fb67cd1
VZ
1494 int noTemplates,
1495 wxString& path,
1496 long WXUNUSED(flags),
1497 bool WXUNUSED(save))
c801d85f 1498{
89d94e04 1499#ifdef wxHAS_MULTIPLE_FILEDLG_FILTERS
ba681060
VZ
1500 wxString descrBuf;
1501
89d94e04 1502 for (int i = 0; i < noTemplates; i++)
c801d85f 1503 {
ba681060
VZ
1504 if (templates[i]->IsVisible())
1505 {
1506 // add a '|' to separate this filter from the previous one
b494c48b 1507 if ( !descrBuf.empty() )
223d09f6 1508 descrBuf << wxT('|');
ba681060
VZ
1509
1510 descrBuf << templates[i]->GetDescription()
223d09f6 1511 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1512 << templates[i]->GetFileFilter();
ba681060 1513 }
c801d85f 1514 }
a4294b78 1515#else
223d09f6 1516 wxString descrBuf = wxT("*.*");
c77049a0 1517 wxUnusedVar(noTemplates);
a4294b78 1518#endif
c801d85f 1519
3ca6a5f0 1520 int FilterIndex = -1;
f6bcfd97
BP
1521
1522 wxWindow* parent = wxFindSuitableParent();
1523
f6fe9f9c 1524 wxString pathTmp = wxFileSelectorEx(_("Open File"),
d8efd219 1525 GetLastDirectory(),
b494c48b 1526 wxEmptyString,
6de2f8b9 1527 &FilterIndex,
caf0debf
VZ
1528 descrBuf,
1529 0,
f6bcfd97 1530 parent);
ba681060 1531
c77049a0 1532 wxDocTemplate *theTemplate = NULL;
b494c48b 1533 if (!pathTmp.empty())
0fb67cd1 1534 {
f6bcfd97
BP
1535 if (!wxFileExists(pathTmp))
1536 {
1537 wxString msgTitle;
9cf3d218
VZ
1538 if (!wxTheApp->GetAppDisplayName().empty())
1539 msgTitle = wxTheApp->GetAppDisplayName();
f6bcfd97
BP
1540 else
1541 msgTitle = wxString(_("File error"));
02718e6a 1542
89d94e04
VZ
1543 wxMessageBox(_("Sorry, could not open this file."),
1544 msgTitle,
1545 wxOK | wxICON_EXCLAMATION | wxCENTRE,
1546 parent);
f6bcfd97 1547
b494c48b 1548 path = wxEmptyString;
c77049a0 1549 return NULL;
f6bcfd97 1550 }
d8efd219
VZ
1551
1552 SetLastDirectory(wxPathOnly(pathTmp));
ac0ac824 1553
0fb67cd1 1554 path = pathTmp;
0fb67cd1 1555
3ca6a5f0
BP
1556 // first choose the template using the extension, if this fails (i.e.
1557 // wxFileSelectorEx() didn't fill it), then use the path
1558 if ( FilterIndex != -1 )
6de2f8b9 1559 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1560 if ( !theTemplate )
1561 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1562 if ( !theTemplate )
1563 {
89d94e04
VZ
1564 // Since we do not add files with non-default extensions to the
1565 // file history this can only happen if the application changes the
1566 // allowed templates in runtime.
1567 wxMessageBox(_("Sorry, the format for this file is unknown."),
1568 _("Open File"),
1569 wxOK | wxICON_EXCLAMATION | wxCENTRE,
1570 parent);
9d4a05be 1571 }
0fb67cd1
VZ
1572 }
1573 else
1574 {
89d94e04 1575 path.clear();
0fb67cd1 1576 }
3ca6a5f0
BP
1577
1578 return theTemplate;
c801d85f
KB
1579}
1580
1581wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1582 int noTemplates, bool sort)
0fb67cd1 1583{
222ed1d6 1584 wxArrayString strings;
89d94e04 1585 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
0fb67cd1
VZ
1586 int i;
1587 int n = 0;
42771621
VZ
1588
1589 for (i = 0; i < noTemplates; i++)
1590 {
1591 if (templates[i]->IsVisible())
1592 {
1593 int j;
68379eaf 1594 bool want = true;
42771621
VZ
1595 for (j = 0; j < n; j++)
1596 {
bb28b477 1597 //filter out NOT unique documents + view combinations
42771621 1598 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
bb28b477
JS
1599 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1600 )
68379eaf 1601 want = false;
42771621 1602 }
bb28b477
JS
1603
1604 if ( want )
42771621
VZ
1605 {
1606 strings.Add(templates[i]->m_description);
1607
1608 data[n] = templates[i];
1609 n ++;
1610 }
1611 }
1612 } // for
1613
1614 if (sort)
1615 {
a2a03d78 1616 strings.Sort(); // ascending sort
42771621
VZ
1617 // Yes, this will be slow, but template lists
1618 // are typically short.
1619 int j;
1620 n = strings.Count();
1621 for (i = 0; i < n; i++)
1622 {
1623 for (j = 0; j < noTemplates; j++)
1624 {
1625 if (strings[i] == templates[j]->m_description)
1626 data[i] = templates[j];
1627 }
1628 }
1629 }
17260efd
VZ
1630
1631 wxDocTemplate *theTemplate;
1632
1633 switch ( n )
0fb67cd1 1634 {
17260efd
VZ
1635 case 0:
1636 // no visible templates, hence nothing to choose from
1637 theTemplate = NULL;
1638 break;
0fb67cd1 1639
17260efd 1640 case 1:
c77049a0 1641 // don't propose the user to choose if he has no choice
17260efd
VZ
1642 theTemplate = data[0];
1643 break;
1644
1645 default:
1646 // propose the user to choose one of several
1647 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1648 (
1649 _("Select a document template"),
1650 _("Templates"),
1651 strings,
89d94e04 1652 (void **)data.get(),
17260efd
VZ
1653 wxFindSuitableParent()
1654 );
1655 }
f6bcfd97 1656
0fb67cd1 1657 return theTemplate;
c801d85f
KB
1658}
1659
1660wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1661 int noTemplates, bool sort)
c801d85f 1662{
222ed1d6 1663 wxArrayString strings;
89d94e04 1664 wxScopedArray<wxDocTemplate *> data(new wxDocTemplate *[noTemplates]);
0fb67cd1
VZ
1665 int i;
1666 int n = 0;
42771621 1667
0fb67cd1 1668 for (i = 0; i < noTemplates; i++)
c801d85f 1669 {
17260efd
VZ
1670 wxDocTemplate *templ = templates[i];
1671 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1672 {
42771621 1673 int j;
68379eaf 1674 bool want = true;
42771621
VZ
1675 for (j = 0; j < n; j++)
1676 {
bb28b477 1677 //filter out NOT unique views
42771621 1678 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
68379eaf 1679 want = false;
42771621 1680 }
bb28b477
JS
1681
1682 if ( want )
1683 {
42771621
VZ
1684 strings.Add(templ->m_viewTypeName);
1685 data[n] = templ;
1686 n ++;
1687 }
0fb67cd1 1688 }
c801d85f 1689 }
f6bcfd97 1690
42771621
VZ
1691 if (sort)
1692 {
a2a03d78 1693 strings.Sort(); // ascending sort
42771621
VZ
1694 // Yes, this will be slow, but template lists
1695 // are typically short.
1696 int j;
1697 n = strings.Count();
1698 for (i = 0; i < n; i++)
1699 {
1700 for (j = 0; j < noTemplates; j++)
1701 {
1702 if (strings[i] == templates[j]->m_viewTypeName)
1703 data[i] = templates[j];
1704 }
1705 }
1706 }
8658ef93 1707
17260efd
VZ
1708 wxDocTemplate *theTemplate;
1709
1710 // the same logic as above
1711 switch ( n )
1712 {
1713 case 0:
c77049a0 1714 theTemplate = NULL;
17260efd
VZ
1715 break;
1716
1717 case 1:
1718 theTemplate = data[0];
1719 break;
1720
1721 default:
1722 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1723 (
1724 _("Select a document view"),
1725 _("Views"),
1726 strings,
89d94e04 1727 (void **)data.get(),
17260efd
VZ
1728 wxFindSuitableParent()
1729 );
1730
1731 }
1732
0fb67cd1 1733 return theTemplate;
c801d85f
KB
1734}
1735
1736void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1737{
0fb67cd1
VZ
1738 if (!m_templates.Member(temp))
1739 m_templates.Append(temp);
c801d85f
KB
1740}
1741
1742void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1743{
0fb67cd1 1744 m_templates.DeleteObject(temp);
c801d85f
KB
1745}
1746
1747// Add and remove a document from the manager's list
1748void wxDocManager::AddDocument(wxDocument *doc)
1749{
0fb67cd1
VZ
1750 if (!m_docs.Member(doc))
1751 m_docs.Append(doc);
c801d85f
KB
1752}
1753
1754void wxDocManager::RemoveDocument(wxDocument *doc)
1755{
0fb67cd1 1756 m_docs.DeleteObject(doc);
c801d85f
KB
1757}
1758
1759// Views or windows should inform the document manager
1760// when a view is going in or out of focus
18759f78
VZ
1761void wxDocManager::ActivateView(wxView *view, bool activate)
1762{
1763 if ( activate )
0fb67cd1 1764 {
18759f78
VZ
1765 m_currentView = view;
1766 }
1767 else // deactivate
1768 {
1769 if ( m_currentView == view )
1770 {
1771 // don't keep stale pointer
c77049a0 1772 m_currentView = NULL;
18759f78 1773 }
0fb67cd1 1774 }
c801d85f
KB
1775}
1776
0fb67cd1
VZ
1777// ----------------------------------------------------------------------------
1778// Default document child frame
1779// ----------------------------------------------------------------------------
c801d85f
KB
1780
1781BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1782 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1783 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1784END_EVENT_TABLE()
1785
0fb67cd1
VZ
1786wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1787 wxView *view,
1788 wxFrame *frame,
1789 wxWindowID id,
1790 const wxString& title,
1791 const wxPoint& pos,
1792 const wxSize& size,
1793 long style,
1794 const wxString& name)
1795 : wxFrame(frame, id, title, pos, size, style, name)
1796{
1797 m_childDocument = doc;
1798 m_childView = view;
1799 if (view)
1800 view->SetFrame(this);
c801d85f
KB
1801}
1802
bba5e72a 1803bool wxDocChildFrame::TryValidator(wxEvent& event)
c801d85f 1804{
bba5e72a
VZ
1805 if ( !m_childView )
1806 return false;
c801d85f 1807
bba5e72a
VZ
1808 // FIXME: why is this needed here?
1809 m_childView->Activate(true);
1810
1811 return m_childView->ProcessEventHere(event);
c801d85f
KB
1812}
1813
c801d85f
KB
1814void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1815{
0fb67cd1 1816 wxFrame::OnActivate(event);
c801d85f 1817
0fb67cd1
VZ
1818 if (m_childView)
1819 m_childView->Activate(event.GetActive());
c801d85f
KB
1820}
1821
387a3b02 1822void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1823{
89d94e04
VZ
1824 if ( !m_childView )
1825 return;
1826
1827 // passing false to Close() means to not delete associated window
1828 if ( event.CanVeto() && !m_childView->Close(false) )
c801d85f 1829 {
89d94e04
VZ
1830 event.Veto();
1831 return;
1832 }
387a3b02 1833
89d94e04
VZ
1834 m_childView->Activate(false);
1835 delete m_childView;
1836 m_childView = NULL;
1837 m_childDocument = NULL;
0fb67cd1 1838
89d94e04 1839 Destroy();
c801d85f
KB
1840}
1841
0fb67cd1
VZ
1842// ----------------------------------------------------------------------------
1843// Default parent frame
1844// ----------------------------------------------------------------------------
c801d85f
KB
1845
1846BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1847 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1848 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1849 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1850END_EVENT_TABLE()
1851
0c246b3c
PC
1852wxDocParentFrame::wxDocParentFrame()
1853{
1854 m_docManager = NULL;
1855}
1856
0fb67cd1
VZ
1857wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1858 wxFrame *frame,
1859 wxWindowID id,
1860 const wxString& title,
1861 const wxPoint& pos,
1862 const wxSize& size,
1863 long style,
1864 const wxString& name)
1865 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1866{
0fb67cd1 1867 m_docManager = manager;
c801d85f
KB
1868}
1869
0c246b3c
PC
1870bool wxDocParentFrame::Create(wxDocManager *manager,
1871 wxFrame *frame,
1872 wxWindowID id,
1873 const wxString& title,
1874 const wxPoint& pos,
1875 const wxSize& size,
1876 long style,
1877 const wxString& name)
1878{
1879 m_docManager = manager;
1880 return base_type::Create(frame, id, title, pos, size, style, name);
1881}
1882
c801d85f
KB
1883void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1884{
1885 Close();
1886}
1887
1888void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1889{
1fc88785 1890 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c 1891 wxString filename(m_docManager->GetHistoryFile(n));
c77049a0
VZ
1892 if ( filename.empty() )
1893 return;
42771621 1894
c77049a0
VZ
1895 wxString errMsg; // must contain exactly one "%s" if non-empty
1896 if ( wxFile::Exists(filename) )
1897 {
1898 // try to open it
1899 if ( m_docManager->CreateDocument(filename, wxDOC_SILENT) )
1900 return;
0c5d3e1c 1901
c77049a0
VZ
1902 errMsg = _("The file '%s' couldn't be opened.");
1903 }
1904 else // file doesn't exist
1905 {
1906 errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
0c5d3e1c 1907 }
c77049a0
VZ
1908
1909
1910 wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
1911
1912 // remove the file which we can't open from the MRU list
1913 m_docManager->RemoveFileFromHistory(n);
1914
1915 // and tell the user about it
1916 wxLogError(errMsg + '\n' +
1917 _("It has been removed from the most recently used files list."),
1918 filename);
c801d85f
KB
1919}
1920
1921// Extend event processing to search the view's event table
bba5e72a 1922bool wxDocParentFrame::TryValidator(wxEvent& event)
c801d85f 1923{
bba5e72a 1924 return m_docManager && m_docManager->ProcessEventHere(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