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