]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
Forgot header with OSX prefix
[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
0fb67cd1
VZ
159 if (m_commandProcessor)
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{
0fb67cd1
VZ
173 if (OnSaveModified())
174 return OnCloseDocument();
175 else
68379eaf 176 return false;
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{
b1d4dd7a 236 if (m_documentViews.GetCount() == 0)
c77049a0 237 return NULL;
b1d4dd7a 238 return (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
04129514
JS
279#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
280 wxString filter = docTemplate->GetDescription() + wxT(" (") + docTemplate->GetFileFilter() + wxT(")|") + docTemplate->GetFileFilter();
281
282 // Now see if there are some other template with identical view and document
283 // classes, whose filters may also be used.
284
285 if (docTemplate->GetViewClassInfo() && docTemplate->GetDocClassInfo())
286 {
e7b21b01 287 wxList::compatibility_iterator node = docTemplate->GetDocumentManager()->GetTemplates().GetFirst();
04129514
JS
288 while (node)
289 {
290 wxDocTemplate *t = (wxDocTemplate*) node->GetData();
68379eaf 291
04129514
JS
292 if (t->IsVisible() && t != docTemplate &&
293 t->GetViewClassInfo() == docTemplate->GetViewClassInfo() &&
294 t->GetDocClassInfo() == docTemplate->GetDocClassInfo())
295 {
296 // add a '|' to separate this filter from the previous one
b494c48b 297 if ( !filter.empty() )
04129514 298 filter << wxT('|');
68379eaf 299
04129514
JS
300 filter << t->GetDescription() << wxT(" (") << t->GetFileFilter() << wxT(") |")
301 << t->GetFileFilter();
302 }
303
304 node = node->GetNext();
305 }
306 }
307#else
308 wxString filter = docTemplate->GetFileFilter() ;
309#endif
e0214f1f 310 wxString defaultDir = docTemplate->GetDirectory();
d8efd219
VZ
311 if ( defaultDir.empty() )
312 {
e0214f1f 313 defaultDir = wxPathOnly(GetFilename());
d8efd219
VZ
314 if ( defaultDir.empty() )
315 defaultDir = GetDocumentManager()->GetLastDirectory();
316 }
e0214f1f 317
00e3ea1c 318 wxString fileName = wxFileSelector(_("Save As"),
e0214f1f 319 defaultDir,
015e69f3 320 wxFileNameFromPath(GetFilename()),
0fb67cd1 321 docTemplate->GetDefaultExtension(),
04129514 322 filter,
ff3e84ff 323 wxFD_SAVE | wxFD_OVERWRITE_PROMPT,
0fb67cd1
VZ
324 GetDocumentWindow());
325
00e3ea1c 326 if (fileName.empty())
68379eaf 327 return false;
ba681060 328
00e3ea1c
FM
329 wxString ext;
330 wxFileName::SplitPath(fileName, NULL, NULL, &ext);
2bb0cd28 331
b494c48b 332 if (ext.empty())
2bb0cd28 333 {
2b5f62a0 334 fileName += wxT(".");
2bb0cd28
JS
335 fileName += docTemplate->GetDefaultExtension();
336 }
337
9d4a05be 338 // Files that were not saved correctly are not added to the FileHistory.
00e3ea1c 339 if (!OnSaveDocument(fileName))
68379eaf 340 return false;
9d4a05be 341
00e3ea1c
FM
342 SetTitle(wxFileNameFromPath(fileName));
343 SetFilename(fileName, true); // will call OnChangeFileName automatically
344
42771621 345 // A file that doesn't use the default extension of its document template cannot be opened
9d4a05be
JS
346 // via the FileHistory, so we do not add it.
347 if (docTemplate->FileMatchesTemplate(fileName))
348 {
349 GetDocumentManager()->AddFileToHistory(fileName);
350 }
351 else
352 {
353 // The user will probably not be able to open the file again, so
354 // we could warn about the wrong file-extension here.
355 }
68379eaf 356 return true;
c801d85f 357}
0fb67cd1 358
c801d85f
KB
359bool wxDocument::OnSaveDocument(const wxString& file)
360{
0fb67cd1 361 if ( !file )
68379eaf 362 return false;
c801d85f 363
69936aea 364 if ( !DoSaveDocument(file) )
68379eaf 365 return false;
0fb67cd1 366
68379eaf 367 Modify(false);
0fb67cd1 368 SetFilename(file);
68379eaf 369 SetDocumentSaved(true);
26eef304 370#if defined( __WXOSX_MAC__ ) && wxOSX_USE_CARBON
b8afeb43
SC
371 wxFileName fn(file) ;
372 fn.MacSetDefaultTypeAndCreator() ;
373#endif
68379eaf 374 return true;
c801d85f 375}
0fb67cd1 376
c801d85f
KB
377bool wxDocument::OnOpenDocument(const wxString& file)
378{
c77049a0 379 if ( !OnSaveModified() )
68379eaf 380 return false;
c801d85f 381
69936aea 382 if ( !DoOpenDocument(file) )
68379eaf 383 return false;
0fb67cd1 384
68379eaf
WS
385 SetFilename(file, true);
386 Modify(false);
387 m_savedYet = true;
0fb67cd1
VZ
388
389 UpdateAllViews();
c801d85f 390
68379eaf 391 return true;
c801d85f 392}
0fb67cd1 393
a533f5c1 394#if wxUSE_STD_IOSTREAM
dd107c50 395wxSTD istream& wxDocument::LoadObject(wxSTD istream& stream)
23a54e14
RR
396#else
397wxInputStream& wxDocument::LoadObject(wxInputStream& stream)
398#endif
c801d85f 399{
0fb67cd1 400 return stream;
c801d85f
KB
401}
402
23a54e14 403#if wxUSE_STD_IOSTREAM
dd107c50 404wxSTD ostream& wxDocument::SaveObject(wxSTD ostream& stream)
a533f5c1 405#else
23a54e14
RR
406wxOutputStream& wxDocument::SaveObject(wxOutputStream& stream)
407#endif
a533f5c1 408{
23a54e14 409 return stream;
a533f5c1 410}
c801d85f 411
0fb67cd1 412bool wxDocument::Revert()
c801d85f 413{
68379eaf 414 return false;
c801d85f
KB
415}
416
417
418// Get title, or filename if no title, else unnamed
724b119a 419#if WXWIN_COMPATIBILITY_2_8
c801d85f
KB
420bool wxDocument::GetPrintableName(wxString& buf) const
421{
724b119a
VZ
422 // this function can not only be overridden by the user code but also
423 // called by it so we need to ensure that we return the same thing as
424 // GetUserReadableName() but we can't call it because this would result in
425 // an infinite recursion, hence we use the helper DoGetUserReadableName()
426 buf = DoGetUserReadableName();
427
428 return true;
429}
430#endif // WXWIN_COMPATIBILITY_2_8
431
432wxString wxDocument::GetUserReadableName() const
433{
434#if WXWIN_COMPATIBILITY_2_8
435 // we need to call the old virtual function to ensure that the overridden
436 // version of it is still called
437 wxString name;
438 if ( GetPrintableName(name) )
439 return name;
440#endif // WXWIN_COMPATIBILITY_2_8
441
442 return DoGetUserReadableName();
443}
444
445wxString wxDocument::DoGetUserReadableName() const
446{
447 if ( !m_documentTitle.empty() )
448 return m_documentTitle;
449
450 if ( !m_documentFile.empty() )
451 return wxFileNameFromPath(m_documentFile);
452
453 return _("unnamed");
c801d85f
KB
454}
455
6de2f8b9 456wxWindow *wxDocument::GetDocumentWindow() const
c801d85f 457{
0fb67cd1
VZ
458 wxView *view = GetFirstView();
459 if (view)
460 return view->GetFrame();
461 else
462 return 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 (
479 _("Do you want to save changes to document %s?"),
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
658void wxView::OnActivateView(bool WXUNUSED(activate), wxView *WXUNUSED(activeView), wxView *WXUNUSED(deactiveView))
659{
660}
661
662void wxView::OnPrint(wxDC *dc, wxObject *WXUNUSED(info))
663{
0fb67cd1 664 OnDraw(dc);
c801d85f
KB
665}
666
667void wxView::OnUpdate(wxView *WXUNUSED(sender), wxObject *WXUNUSED(hint))
668{
669}
670
0fb67cd1 671void wxView::OnChangeFilename()
c801d85f 672{
faa49bfd
WS
673 // GetFrame can return wxWindow rather than wxTopLevelWindow due to
674 // generic MDI implementation so use SetLabel rather than SetTitle.
675 // It should cause SetTitle() for top level windows.
676 wxWindow *win = GetFrame();
677 if (!win) return;
f2506310 678
faa49bfd
WS
679 wxDocument *doc = GetDocument();
680 if (!doc) return;
c801d85f 681
724b119a 682 win->SetLabel(doc->GetUserReadableName());
c801d85f
KB
683}
684
685void wxView::SetDocument(wxDocument *doc)
686{
0fb67cd1
VZ
687 m_viewDocument = doc;
688 if (doc)
689 doc->AddView(this);
c801d85f
KB
690}
691
692bool wxView::Close(bool deleteWindow)
693{
c77049a0 694 return OnClose(deleteWindow);
c801d85f
KB
695}
696
697void wxView::Activate(bool activate)
698{
bb28b477 699 if (GetDocument() && GetDocumentManager())
0fb67cd1
VZ
700 {
701 OnActivateView(activate, this, GetDocumentManager()->GetCurrentView());
702 GetDocumentManager()->ActivateView(this, activate);
703 }
c801d85f
KB
704}
705
706bool wxView::OnClose(bool WXUNUSED(deleteWindow))
707{
68379eaf 708 return GetDocument() ? GetDocument()->Close() : true;
c801d85f
KB
709}
710
47d67540 711#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1 712wxPrintout *wxView::OnCreatePrintout()
c801d85f 713{
0fb67cd1 714 return new wxDocPrintout(this);
c801d85f 715}
6de2f8b9 716#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 717
0fb67cd1
VZ
718// ----------------------------------------------------------------------------
719// wxDocTemplate
720// ----------------------------------------------------------------------------
c801d85f 721
0fb67cd1
VZ
722wxDocTemplate::wxDocTemplate(wxDocManager *manager,
723 const wxString& descr,
724 const wxString& filter,
725 const wxString& dir,
726 const wxString& ext,
727 const wxString& docTypeName,
728 const wxString& viewTypeName,
729 wxClassInfo *docClassInfo,
730 wxClassInfo *viewClassInfo,
731 long flags)
c801d85f 732{
0fb67cd1 733 m_documentManager = manager;
0fb67cd1
VZ
734 m_description = descr;
735 m_directory = dir;
736 m_defaultExt = ext;
737 m_fileFilter = filter;
738 m_flags = flags;
739 m_docTypeName = docTypeName;
740 m_viewTypeName = viewTypeName;
741 m_documentManager->AssociateTemplate(this);
c801d85f 742
0fb67cd1
VZ
743 m_docClassInfo = docClassInfo;
744 m_viewClassInfo = viewClassInfo;
c801d85f
KB
745}
746
0fb67cd1 747wxDocTemplate::~wxDocTemplate()
c801d85f 748{
0fb67cd1 749 m_documentManager->DisassociateTemplate(this);
c801d85f 750}
0fb67cd1
VZ
751
752// Tries to dynamically construct an object of the right class.
c801d85f
KB
753wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
754{
c77049a0 755 wxDocument * const doc = DoCreateDocument();
68379eaf 756
c77049a0
VZ
757 // VZ: this code doesn't delete doc if InitDocument() (i.e. doc->OnCreate())
758 // fails, is this intentional?
759
760 return doc && InitDocument(doc, path, flags) ? doc : NULL;
217b7140
JS
761}
762
c77049a0
VZ
763bool
764wxDocTemplate::InitDocument(wxDocument* doc, const wxString& path, long flags)
217b7140 765{
0fb67cd1
VZ
766 doc->SetFilename(path);
767 doc->SetDocumentTemplate(this);
768 GetDocumentManager()->AddDocument(doc);
769 doc->SetCommandProcessor(doc->OnCreateCommandProcessor());
770
771 if (doc->OnCreate(path, flags))
217b7140 772 return true;
0fb67cd1
VZ
773 else
774 {
f6bcfd97
BP
775 if (GetDocumentManager()->GetDocuments().Member(doc))
776 doc->DeleteAllViews();
217b7140 777 return false;
0fb67cd1 778 }
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
VZ
816
817 return (wxDocument *)m_docClassInfo->CreateObject();
818}
819
820wxView *wxDocTemplate::DoCreateView()
821{
822 if (!m_viewClassInfo)
c77049a0 823 return NULL;
69936aea
VZ
824
825 return (wxView *)m_viewClassInfo->CreateObject();
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{
888 if (doc->Close() || force)
889 {
890 // Implicitly deletes the document when
891 // the last view is deleted
892 doc->DeleteAllViews();
893
894 // Check we're really deleted
895 if (m_docs.Member(doc))
896 delete doc;
42771621 897
68379eaf 898 return true;
b72b1920 899 }
68379eaf 900 return false;
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{
b494c48b 989 if ( !CreateDocument( wxEmptyString, 0) )
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
0fb67cd1
VZ
1022 wxView *view = GetCurrentView();
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
0fb67cd1
VZ
1040 wxView *view = GetCurrentView();
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.
999836aa 1048 wxPrintPreviewBase *preview = new wxPrintPreview(printout, view->OnCreatePrintout());
3f8b4a3f
JS
1049 if ( !preview->Ok() )
1050 {
1051 delete preview;
1052 wxMessageBox( _("Sorry, print preview needs a printer to be installed.") );
1053 return;
1054 }
0fb67cd1
VZ
1055
1056 wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
1057 wxPoint(100, 100), wxSize(600, 650));
1058 frame->Centre(wxBOTH);
1059 frame->Initialize();
68379eaf 1060 frame->Show(true);
0fb67cd1 1061 }
88ac883a 1062#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
1063}
1064
34aa6140 1065void wxDocManager::OnUndo(wxCommandEvent& event)
c801d85f 1066{
0fb67cd1
VZ
1067 wxDocument *doc = GetCurrentDocument();
1068 if (!doc)
1069 return;
1070 if (doc->GetCommandProcessor())
1071 doc->GetCommandProcessor()->Undo();
34aa6140
JS
1072 else
1073 event.Skip();
c801d85f
KB
1074}
1075
34aa6140 1076void wxDocManager::OnRedo(wxCommandEvent& event)
c801d85f 1077{
0fb67cd1
VZ
1078 wxDocument *doc = GetCurrentDocument();
1079 if (!doc)
1080 return;
1081 if (doc->GetCommandProcessor())
1082 doc->GetCommandProcessor()->Redo();
34aa6140
JS
1083 else
1084 event.Skip();
c801d85f
KB
1085}
1086
f2506310
JS
1087// Handlers for UI update commands
1088
1089void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
1090{
84cae69a
FM
1091 // CreateDocument() (which is called from OnFileOpen) may succeed
1092 // only when there is at least a template:
1093 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1094}
1095
c77049a0 1096void wxDocManager::OnUpdateDisableIfNoDoc(wxUpdateUIEvent& event)
f2506310 1097{
c77049a0 1098 event.Enable( GetCurrentDocument() != NULL );
f2506310
JS
1099}
1100
1101void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1102{
84cae69a
FM
1103 // CreateDocument() (which is called from OnFileNew) may succeed
1104 // only when there is at least a template:
1105 event.Enable( GetTemplates().GetCount()>0 );
f2506310
JS
1106}
1107
1108void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1109{
de56f240
VZ
1110 wxDocument * const doc = GetCurrentDocument();
1111 event.Enable( doc && !doc->AlreadySaved() );
f2506310
JS
1112}
1113
f2506310
JS
1114void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1115{
1116 wxDocument *doc = GetCurrentDocument();
34aa6140 1117 if (!doc)
68379eaf 1118 event.Enable(false);
34aa6140
JS
1119 else if (!doc->GetCommandProcessor())
1120 event.Skip();
1121 else
1122 {
1123 event.Enable( doc->GetCommandProcessor()->CanUndo() );
2599ee02 1124 doc->GetCommandProcessor()->SetMenuStrings();
34aa6140 1125 }
f2506310
JS
1126}
1127
1128void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1129{
1130 wxDocument *doc = GetCurrentDocument();
34aa6140 1131 if (!doc)
68379eaf 1132 event.Enable(false);
34aa6140
JS
1133 else if (!doc->GetCommandProcessor())
1134 event.Skip();
1135 else
1136 {
1137 event.Enable( doc->GetCommandProcessor()->CanRedo() );
2599ee02 1138 doc->GetCommandProcessor()->SetMenuStrings();
34aa6140 1139 }
f2506310
JS
1140}
1141
6de2f8b9 1142wxView *wxDocManager::GetCurrentView() const
637f467a
JS
1143{
1144 if (m_currentView)
1145 return m_currentView;
b1d4dd7a 1146 if (m_docs.GetCount() == 1)
637f467a 1147 {
b1d4dd7a 1148 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
637f467a
JS
1149 return doc->GetFirstView();
1150 }
c77049a0 1151 return NULL;
637f467a
JS
1152}
1153
bba5e72a 1154bool wxDocManager::TryValidator(wxEvent& event)
637f467a 1155{
c77049a0 1156 wxView * const view = GetCurrentView();
bba5e72a 1157 return view && view->ProcessEventHere(event);
637f467a
JS
1158}
1159
c77049a0 1160namespace
c801d85f 1161{
b1d4dd7a 1162
c77049a0
VZ
1163// helper function: return only the visible templates
1164wxDocTemplates GetVisibleTemplates(const wxList& allTemplates)
1165{
1166 // select only the visible templates
1167 const size_t totalNumTemplates = allTemplates.GetCount();
1168 wxDocTemplates templates;
1169 if ( totalNumTemplates )
c801d85f 1170 {
c77049a0
VZ
1171 templates.reserve(totalNumTemplates);
1172
1173 for ( wxList::const_iterator i = allTemplates.begin(),
1174 end = allTemplates.end();
1175 i != end;
1176 ++i )
0fb67cd1 1177 {
c77049a0
VZ
1178 wxDocTemplate * const temp = (wxDocTemplate *)*i;
1179 if ( temp->IsVisible() )
1180 templates.push_back(temp);
0fb67cd1 1181 }
c801d85f 1182 }
42771621 1183
c77049a0
VZ
1184 return templates;
1185}
0fb67cd1 1186
c77049a0
VZ
1187} // anonymous namespace
1188
1189wxDocument *wxDocManager::CreateDocument(const wxString& pathOrig, long flags)
1190{
1191 // this ought to be const but SelectDocumentType/Path() are not
1192 // const-correct and can't be changed as, being virtual, this risks
1193 // breaking user code overriding them
1194 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1195 const size_t numTemplates = templates.size();
1196 if ( !numTemplates )
c801d85f 1197 {
c77049a0
VZ
1198 // no templates can be used, can't create document
1199 return NULL;
c801d85f
KB
1200 }
1201
42771621 1202
c77049a0
VZ
1203 // normally user should select the template to use but wxDOC_SILENT flag we
1204 // choose one ourselves
1205 wxString path = pathOrig; // may be modified below
1206 wxDocTemplate *temp;
1207 if ( flags & wxDOC_SILENT )
1208 {
1209 wxASSERT_MSG( !path.empty(),
1210 "using empty path with wxDOC_SILENT doesn't make sense" );
42771621 1211
c77049a0
VZ
1212 temp = FindTemplateForPath(path);
1213 if ( !temp )
1214 {
1215 wxLogWarning(_("The format of file '%s' couldn't be determined."),
1216 path);
0fb67cd1 1217 }
c77049a0
VZ
1218 }
1219 else // not silent, ask the user
1220 {
1221 // for the new file we need just the template, for an existing one we
1222 // need the template and the path, unless it's already specified
1223 if ( (flags & wxDOC_NEW) || !path.empty() )
1224 temp = SelectDocumentType(&templates[0], numTemplates);
1225 else
1226 temp = SelectDocumentPath(&templates[0], numTemplates, path, flags);
1227 }
0fb67cd1 1228
c77049a0
VZ
1229 if ( !temp )
1230 return NULL;
42771621 1231
c77049a0
VZ
1232 // check whether the document with this path is already opened
1233 if ( !path.empty() )
1234 {
1235 const wxFileName fn(path);
1236 for ( wxList::const_iterator i = m_docs.begin(); i != m_docs.end(); ++i )
1237 {
1238 wxDocument * const doc = (wxDocument*)*i;
b72b1920 1239
c77049a0 1240 if ( fn == doc->GetFilename() )
0fb67cd1 1241 {
c77049a0
VZ
1242 // file already open, just activate it and return
1243 if ( doc->GetFirstView() )
f5729a6b 1244 {
c77049a0
VZ
1245 ActivateView(doc->GetFirstView());
1246 if ( doc->GetDocumentWindow() )
1247 doc->GetDocumentWindow()->SetFocus();
1248 return doc;
f5729a6b 1249 }
0fb67cd1 1250 }
0fb67cd1 1251 }
c801d85f 1252 }
c801d85f 1253
c801d85f 1254
c77049a0 1255 // no, we need to create a new document
c801d85f 1256
c77049a0
VZ
1257
1258 // if we've reached the max number of docs, close the first one.
1259 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
9d4a05be 1260 {
c77049a0 1261 if ( !CloseDocument((wxDocument *)GetDocuments().GetFirst()->GetData()) )
9d4a05be 1262 {
c77049a0
VZ
1263 // can't open the new document if closing the old one failed
1264 return NULL;
9d4a05be
JS
1265 }
1266 }
c801d85f 1267
c801d85f 1268
c77049a0
VZ
1269 // do create and initialize the new document finally
1270 wxDocument * const docNew = temp->CreateDocument(path, flags);
1271 if ( !docNew )
1272 return NULL;
42771621 1273
c77049a0
VZ
1274 docNew->SetDocumentName(temp->GetDocumentName());
1275 docNew->SetDocumentTemplate(temp);
6489e7ee 1276
c77049a0
VZ
1277 // call the appropriate function depending on whether we're creating a new
1278 // file or opening an existing one
1279 if ( !(flags & wxDOC_NEW ? docNew->OnNewDocument()
1280 : docNew->OnOpenDocument(path)) )
1281 {
1282 // Document is implicitly deleted by DeleteAllViews
1283 docNew->DeleteAllViews();
1284 return NULL;
c801d85f 1285 }
3ca6a5f0 1286
c77049a0
VZ
1287 // add the successfully opened file to MRU, but only if we're going to be
1288 // able to reopen it successfully later which requires the template for
1289 // this document to be retrievable from the file extension
1290 if ( !(flags & wxDOC_NEW) && temp->FileMatchesTemplate(path) )
1291 AddFileToHistory(path);
1292
1293 return docNew;
c801d85f
KB
1294}
1295
1296wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1297{
c77049a0
VZ
1298 wxDocTemplates templates(GetVisibleTemplates(m_templates));
1299 const size_t numTemplates = templates.size();
b1d4dd7a 1300
c77049a0
VZ
1301 if ( numTemplates == 0 )
1302 return NULL;
0fb67cd1 1303
c77049a0
VZ
1304 wxDocTemplate * const
1305 temp = numTemplates == 1 ? templates[0]
1306 : SelectViewType(&templates[0], numTemplates);
1307
1308 if ( !temp )
1309 return NULL;
1310
1311 wxView *view = temp->CreateView(doc, flags);
1312 if ( view )
1313 view->SetViewName(temp->GetViewName());
1314 return view;
c801d85f
KB
1315}
1316
1317// Not yet implemented
c77049a0
VZ
1318void
1319wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
c801d85f
KB
1320{
1321}
1322
1323// Not yet implemented
1324bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1325{
68379eaf 1326 return false;
c801d85f
KB
1327}
1328
6de2f8b9 1329wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1330{
9cb0033c
VZ
1331 wxView *view = GetCurrentView();
1332 if (view)
1333 return view->GetDocument();
0fb67cd1 1334 else
c77049a0 1335 return NULL;
c801d85f
KB
1336}
1337
724b119a
VZ
1338// Make a default name for a new document
1339#if WXWIN_COMPATIBILITY_2_8
1340bool wxDocManager::MakeDefaultName(wxString& WXUNUSED(name))
1341{
1342 // we consider that this function can only be overridden by the user code,
1343 // not called by it as it only makes sense to call it internally, so we
1344 // don't bother to return anything from here
1345 return false;
1346}
1347#endif // WXWIN_COMPATIBILITY_2_8
1348
1349wxString wxDocManager::MakeNewDocumentName()
c801d85f 1350{
724b119a 1351 wxString name;
53c6e7cc 1352
724b119a
VZ
1353#if WXWIN_COMPATIBILITY_2_8
1354 if ( !MakeDefaultName(name) )
1355#endif // WXWIN_COMPATIBILITY_2_8
1356 {
1357 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1358 m_defaultDocumentNameCounter++;
1359 }
1360
1361 return name;
c801d85f
KB
1362}
1363
f2506310
JS
1364// Make a frame title (override this to do something different)
1365// If docName is empty, a document is not currently active.
1366wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1367{
9cf3d218 1368 wxString appName = wxTheApp->GetAppDisplayName();
f2506310
JS
1369 wxString title;
1370 if (!doc)
1371 title = appName;
1372 else
1373 {
724b119a 1374 wxString docName = doc->GetUserReadableName();
f2506310
JS
1375 title = docName + wxString(_(" - ")) + appName;
1376 }
1377 return title;
1378}
1379
1380
c801d85f
KB
1381// Not yet implemented
1382wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1383{
c77049a0 1384 return NULL;
c801d85f
KB
1385}
1386
1387// File history management
1388void wxDocManager::AddFileToHistory(const wxString& file)
1389{
0fb67cd1
VZ
1390 if (m_fileHistory)
1391 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1392}
1393
e49c85af 1394void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1395{
1396 if (m_fileHistory)
1397 m_fileHistory->RemoveFileFromHistory(i);
1398}
1399
e49c85af 1400wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1401{
0fb67cd1
VZ
1402 wxString histFile;
1403
1404 if (m_fileHistory)
1405 histFile = m_fileHistory->GetHistoryFile(i);
1406
1407 return histFile;
c801d85f
KB
1408}
1409
1410void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1411{
0fb67cd1
VZ
1412 if (m_fileHistory)
1413 m_fileHistory->UseMenu(menu);
c801d85f
KB
1414}
1415
7f555861 1416void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1417{
0fb67cd1
VZ
1418 if (m_fileHistory)
1419 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1420}
1421
702ca7c0 1422#if wxUSE_CONFIG
9c8116f8 1423void wxDocManager::FileHistoryLoad(const wxConfigBase& config)
c801d85f 1424{
0fb67cd1
VZ
1425 if (m_fileHistory)
1426 m_fileHistory->Load(config);
7f555861
JS
1427}
1428
1429void wxDocManager::FileHistorySave(wxConfigBase& config)
1430{
0fb67cd1
VZ
1431 if (m_fileHistory)
1432 m_fileHistory->Save(config);
7f555861 1433}
ac57418f 1434#endif
7f555861
JS
1435
1436void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1437{
0fb67cd1
VZ
1438 if (m_fileHistory)
1439 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1440}
1441
1442void wxDocManager::FileHistoryAddFilesToMenu()
1443{
0fb67cd1
VZ
1444 if (m_fileHistory)
1445 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1446}
1447
7af6b69e 1448size_t wxDocManager::GetHistoryFilesCount() const
c801d85f 1449{
7af6b69e 1450 return m_fileHistory ? m_fileHistory->GetCount() : 0;
c801d85f
KB
1451}
1452
1453
6de2f8b9
VZ
1454// Find out the document template via matching in the document file format
1455// against that of the template
c801d85f
KB
1456wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1457{
c77049a0 1458 wxDocTemplate *theTemplate = NULL;
c801d85f 1459
0fb67cd1 1460 // Find the template which this extension corresponds to
b1d4dd7a 1461 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1462 {
b1d4dd7a 1463 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1464 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1465 {
1466 theTemplate = temp;
1467 break;
1468 }
c801d85f 1469 }
0fb67cd1 1470 return theTemplate;
c801d85f
KB
1471}
1472
1473// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1474// Must extend the file selector dialog or implement own; OR
1475// match the extension to the template extension.
df875e59 1476
c801d85f 1477wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
0fb67cd1
VZ
1478 int noTemplates,
1479 wxString& path,
1480 long WXUNUSED(flags),
1481 bool WXUNUSED(save))
c801d85f 1482{
bf9e3e73 1483 // We can only have multiple filters in Windows and GTK
ce68ad25 1484#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
ba681060
VZ
1485 wxString descrBuf;
1486
1487 int i;
1488 for (i = 0; i < noTemplates; i++)
c801d85f 1489 {
ba681060
VZ
1490 if (templates[i]->IsVisible())
1491 {
1492 // add a '|' to separate this filter from the previous one
b494c48b 1493 if ( !descrBuf.empty() )
223d09f6 1494 descrBuf << wxT('|');
ba681060
VZ
1495
1496 descrBuf << templates[i]->GetDescription()
223d09f6 1497 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1498 << templates[i]->GetFileFilter();
ba681060 1499 }
c801d85f 1500 }
a4294b78 1501#else
223d09f6 1502 wxString descrBuf = wxT("*.*");
c77049a0 1503 wxUnusedVar(noTemplates);
a4294b78 1504#endif
c801d85f 1505
3ca6a5f0 1506 int FilterIndex = -1;
f6bcfd97
BP
1507
1508 wxWindow* parent = wxFindSuitableParent();
1509
f6fe9f9c 1510 wxString pathTmp = wxFileSelectorEx(_("Open File"),
d8efd219 1511 GetLastDirectory(),
b494c48b 1512 wxEmptyString,
6de2f8b9 1513 &FilterIndex,
caf0debf
VZ
1514 descrBuf,
1515 0,
f6bcfd97 1516 parent);
ba681060 1517
c77049a0 1518 wxDocTemplate *theTemplate = NULL;
b494c48b 1519 if (!pathTmp.empty())
0fb67cd1 1520 {
f6bcfd97
BP
1521 if (!wxFileExists(pathTmp))
1522 {
1523 wxString msgTitle;
9cf3d218
VZ
1524 if (!wxTheApp->GetAppDisplayName().empty())
1525 msgTitle = wxTheApp->GetAppDisplayName();
f6bcfd97
BP
1526 else
1527 msgTitle = wxString(_("File error"));
02718e6a 1528
00e3ea1c 1529 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION | wxCENTRE,
f6bcfd97
BP
1530 parent);
1531
b494c48b 1532 path = wxEmptyString;
c77049a0 1533 return NULL;
f6bcfd97 1534 }
d8efd219
VZ
1535
1536 SetLastDirectory(wxPathOnly(pathTmp));
ac0ac824 1537
0fb67cd1 1538 path = pathTmp;
0fb67cd1 1539
3ca6a5f0
BP
1540 // first choose the template using the extension, if this fails (i.e.
1541 // wxFileSelectorEx() didn't fill it), then use the path
1542 if ( FilterIndex != -1 )
6de2f8b9 1543 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1544 if ( !theTemplate )
1545 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1546 if ( !theTemplate )
1547 {
42771621 1548 // Since we do not add files with non-default extensions to the FileHistory this
9d4a05be
JS
1549 // can only happen if the application changes the allowed templates in runtime.
1550 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
42771621 1551 _("Open File"),
00e3ea1c 1552 wxOK | wxICON_EXCLAMATION | wxCENTRE, wxFindSuitableParent());
9d4a05be 1553 }
0fb67cd1
VZ
1554 }
1555 else
1556 {
b494c48b 1557 path = wxEmptyString;
0fb67cd1 1558 }
3ca6a5f0
BP
1559
1560 return theTemplate;
c801d85f
KB
1561}
1562
1563wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1564 int noTemplates, bool sort)
0fb67cd1 1565{
222ed1d6 1566 wxArrayString strings;
17260efd 1567 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1568 int i;
1569 int n = 0;
42771621
VZ
1570
1571 for (i = 0; i < noTemplates; i++)
1572 {
1573 if (templates[i]->IsVisible())
1574 {
1575 int j;
68379eaf 1576 bool want = true;
42771621
VZ
1577 for (j = 0; j < n; j++)
1578 {
bb28b477 1579 //filter out NOT unique documents + view combinations
42771621 1580 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
bb28b477
JS
1581 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1582 )
68379eaf 1583 want = false;
42771621 1584 }
bb28b477
JS
1585
1586 if ( want )
42771621
VZ
1587 {
1588 strings.Add(templates[i]->m_description);
1589
1590 data[n] = templates[i];
1591 n ++;
1592 }
1593 }
1594 } // for
1595
1596 if (sort)
1597 {
a2a03d78 1598 strings.Sort(); // ascending sort
42771621
VZ
1599 // Yes, this will be slow, but template lists
1600 // are typically short.
1601 int j;
1602 n = strings.Count();
1603 for (i = 0; i < n; i++)
1604 {
1605 for (j = 0; j < noTemplates; j++)
1606 {
1607 if (strings[i] == templates[j]->m_description)
1608 data[i] = templates[j];
1609 }
1610 }
1611 }
17260efd
VZ
1612
1613 wxDocTemplate *theTemplate;
1614
1615 switch ( n )
0fb67cd1 1616 {
17260efd
VZ
1617 case 0:
1618 // no visible templates, hence nothing to choose from
1619 theTemplate = NULL;
1620 break;
0fb67cd1 1621
17260efd 1622 case 1:
c77049a0 1623 // don't propose the user to choose if he has no choice
17260efd
VZ
1624 theTemplate = data[0];
1625 break;
1626
1627 default:
1628 // propose the user to choose one of several
1629 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1630 (
1631 _("Select a document template"),
1632 _("Templates"),
1633 strings,
1634 (void **)data,
1635 wxFindSuitableParent()
1636 );
1637 }
f6bcfd97 1638
c801d85f 1639 delete[] data;
17260efd 1640
0fb67cd1 1641 return theTemplate;
c801d85f
KB
1642}
1643
1644wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1645 int noTemplates, bool sort)
c801d85f 1646{
222ed1d6 1647 wxArrayString strings;
17260efd 1648 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1649 int i;
1650 int n = 0;
42771621 1651
0fb67cd1 1652 for (i = 0; i < noTemplates; i++)
c801d85f 1653 {
17260efd
VZ
1654 wxDocTemplate *templ = templates[i];
1655 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1656 {
42771621 1657 int j;
68379eaf 1658 bool want = true;
42771621
VZ
1659 for (j = 0; j < n; j++)
1660 {
bb28b477 1661 //filter out NOT unique views
42771621 1662 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
68379eaf 1663 want = false;
42771621 1664 }
bb28b477
JS
1665
1666 if ( want )
1667 {
42771621
VZ
1668 strings.Add(templ->m_viewTypeName);
1669 data[n] = templ;
1670 n ++;
1671 }
0fb67cd1 1672 }
c801d85f 1673 }
f6bcfd97 1674
42771621
VZ
1675 if (sort)
1676 {
a2a03d78 1677 strings.Sort(); // ascending sort
42771621
VZ
1678 // Yes, this will be slow, but template lists
1679 // are typically short.
1680 int j;
1681 n = strings.Count();
1682 for (i = 0; i < n; i++)
1683 {
1684 for (j = 0; j < noTemplates; j++)
1685 {
1686 if (strings[i] == templates[j]->m_viewTypeName)
1687 data[i] = templates[j];
1688 }
1689 }
1690 }
8658ef93 1691
17260efd
VZ
1692 wxDocTemplate *theTemplate;
1693
1694 // the same logic as above
1695 switch ( n )
1696 {
1697 case 0:
c77049a0 1698 theTemplate = NULL;
17260efd
VZ
1699 break;
1700
1701 case 1:
1702 theTemplate = data[0];
1703 break;
1704
1705 default:
1706 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1707 (
1708 _("Select a document view"),
1709 _("Views"),
1710 strings,
1711 (void **)data,
1712 wxFindSuitableParent()
1713 );
1714
1715 }
1716
0fb67cd1
VZ
1717 delete[] data;
1718 return theTemplate;
c801d85f
KB
1719}
1720
1721void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1722{
0fb67cd1
VZ
1723 if (!m_templates.Member(temp))
1724 m_templates.Append(temp);
c801d85f
KB
1725}
1726
1727void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1728{
0fb67cd1 1729 m_templates.DeleteObject(temp);
c801d85f
KB
1730}
1731
1732// Add and remove a document from the manager's list
1733void wxDocManager::AddDocument(wxDocument *doc)
1734{
0fb67cd1
VZ
1735 if (!m_docs.Member(doc))
1736 m_docs.Append(doc);
c801d85f
KB
1737}
1738
1739void wxDocManager::RemoveDocument(wxDocument *doc)
1740{
0fb67cd1 1741 m_docs.DeleteObject(doc);
c801d85f
KB
1742}
1743
1744// Views or windows should inform the document manager
1745// when a view is going in or out of focus
18759f78
VZ
1746void wxDocManager::ActivateView(wxView *view, bool activate)
1747{
1748 if ( activate )
0fb67cd1 1749 {
18759f78
VZ
1750 m_currentView = view;
1751 }
1752 else // deactivate
1753 {
1754 if ( m_currentView == view )
1755 {
1756 // don't keep stale pointer
c77049a0 1757 m_currentView = NULL;
18759f78 1758 }
0fb67cd1 1759 }
c801d85f
KB
1760}
1761
0fb67cd1
VZ
1762// ----------------------------------------------------------------------------
1763// Default document child frame
1764// ----------------------------------------------------------------------------
c801d85f
KB
1765
1766BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1767 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1768 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1769END_EVENT_TABLE()
1770
0fb67cd1
VZ
1771wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1772 wxView *view,
1773 wxFrame *frame,
1774 wxWindowID id,
1775 const wxString& title,
1776 const wxPoint& pos,
1777 const wxSize& size,
1778 long style,
1779 const wxString& name)
1780 : wxFrame(frame, id, title, pos, size, style, name)
1781{
1782 m_childDocument = doc;
1783 m_childView = view;
1784 if (view)
1785 view->SetFrame(this);
c801d85f
KB
1786}
1787
bba5e72a 1788bool wxDocChildFrame::TryValidator(wxEvent& event)
c801d85f 1789{
bba5e72a
VZ
1790 if ( !m_childView )
1791 return false;
c801d85f 1792
bba5e72a
VZ
1793 // FIXME: why is this needed here?
1794 m_childView->Activate(true);
1795
1796 return m_childView->ProcessEventHere(event);
c801d85f
KB
1797}
1798
c801d85f
KB
1799void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1800{
0fb67cd1 1801 wxFrame::OnActivate(event);
c801d85f 1802
0fb67cd1
VZ
1803 if (m_childView)
1804 m_childView->Activate(event.GetActive());
c801d85f
KB
1805}
1806
387a3b02 1807void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1808{
0fb67cd1 1809 if (m_childView)
c801d85f 1810 {
999836aa 1811 bool ans = event.CanVeto()
68379eaf
WS
1812 ? m_childView->Close(false) // false means don't delete associated window
1813 : true; // Must delete.
387a3b02 1814
0fb67cd1
VZ
1815 if (ans)
1816 {
68379eaf 1817 m_childView->Activate(false);
0fb67cd1 1818 delete m_childView;
c77049a0
VZ
1819 m_childView = NULL;
1820 m_childDocument = NULL;
0fb67cd1
VZ
1821
1822 this->Destroy();
1823 }
1824 else
1825 event.Veto();
c801d85f 1826 }
387a3b02 1827 else
0fb67cd1 1828 event.Veto();
c801d85f
KB
1829}
1830
0fb67cd1
VZ
1831// ----------------------------------------------------------------------------
1832// Default parent frame
1833// ----------------------------------------------------------------------------
c801d85f
KB
1834
1835BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1836 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1837 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1838 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1839END_EVENT_TABLE()
1840
0c246b3c
PC
1841wxDocParentFrame::wxDocParentFrame()
1842{
1843 m_docManager = NULL;
1844}
1845
0fb67cd1
VZ
1846wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1847 wxFrame *frame,
1848 wxWindowID id,
1849 const wxString& title,
1850 const wxPoint& pos,
1851 const wxSize& size,
1852 long style,
1853 const wxString& name)
1854 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1855{
0fb67cd1 1856 m_docManager = manager;
c801d85f
KB
1857}
1858
0c246b3c
PC
1859bool wxDocParentFrame::Create(wxDocManager *manager,
1860 wxFrame *frame,
1861 wxWindowID id,
1862 const wxString& title,
1863 const wxPoint& pos,
1864 const wxSize& size,
1865 long style,
1866 const wxString& name)
1867{
1868 m_docManager = manager;
1869 return base_type::Create(frame, id, title, pos, size, style, name);
1870}
1871
c801d85f
KB
1872void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1873{
1874 Close();
1875}
1876
1877void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1878{
1fc88785 1879 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c 1880 wxString filename(m_docManager->GetHistoryFile(n));
c77049a0
VZ
1881 if ( filename.empty() )
1882 return;
42771621 1883
c77049a0
VZ
1884 wxString errMsg; // must contain exactly one "%s" if non-empty
1885 if ( wxFile::Exists(filename) )
1886 {
1887 // try to open it
1888 if ( m_docManager->CreateDocument(filename, wxDOC_SILENT) )
1889 return;
0c5d3e1c 1890
c77049a0
VZ
1891 errMsg = _("The file '%s' couldn't be opened.");
1892 }
1893 else // file doesn't exist
1894 {
1895 errMsg = _("The file '%s' doesn't exist and couldn't be opened.");
0c5d3e1c 1896 }
c77049a0
VZ
1897
1898
1899 wxASSERT_MSG( !errMsg.empty(), "should have an error message" );
1900
1901 // remove the file which we can't open from the MRU list
1902 m_docManager->RemoveFileFromHistory(n);
1903
1904 // and tell the user about it
1905 wxLogError(errMsg + '\n' +
1906 _("It has been removed from the most recently used files list."),
1907 filename);
c801d85f
KB
1908}
1909
1910// Extend event processing to search the view's event table
bba5e72a 1911bool wxDocParentFrame::TryValidator(wxEvent& event)
c801d85f 1912{
bba5e72a 1913 return m_docManager && m_docManager->ProcessEventHere(event);
c801d85f
KB
1914}
1915
c801d85f
KB
1916// Define the behaviour for the frame closing
1917// - must delete all frames except for the main one.
387a3b02 1918void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1919{
0fb67cd1
VZ
1920 if (m_docManager->Clear(!event.CanVeto()))
1921 {
1922 this->Destroy();
1923 }
1924 else
1925 event.Veto();
c801d85f
KB
1926}
1927
47d67540 1928#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 1929
0fb67cd1 1930wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 1931 : wxPrintout(title)
c801d85f 1932{
0fb67cd1 1933 m_printoutView = view;
c801d85f
KB
1934}
1935
1936bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1937{
0fb67cd1
VZ
1938 wxDC *dc = GetDC();
1939
1940 // Get the logical pixels per inch of screen and printer
1941 int ppiScreenX, ppiScreenY;
1942 GetPPIScreen(&ppiScreenX, &ppiScreenY);
999836aa 1943 wxUnusedVar(ppiScreenY);
0fb67cd1
VZ
1944 int ppiPrinterX, ppiPrinterY;
1945 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
999836aa 1946 wxUnusedVar(ppiPrinterY);
0fb67cd1
VZ
1947
1948 // This scales the DC so that the printout roughly represents the
1949 // the screen scaling. The text point size _should_ be the right size
1950 // but in fact is too small for some reason. This is a detail that will
1951 // need to be addressed at some point but can be fudged for the
1952 // moment.
1953 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1954
1955 // Now we have to check in case our real page size is reduced
1956 // (e.g. because we're drawing to a print preview memory DC)
1957 int pageWidth, pageHeight;
1958 int w, h;
1959 dc->GetSize(&w, &h);
1960 GetPageSizePixels(&pageWidth, &pageHeight);
999836aa 1961 wxUnusedVar(pageHeight);
0fb67cd1
VZ
1962
1963 // If printer pageWidth == current DC width, then this doesn't
1964 // change. But w might be the preview bitmap width, so scale down.
1965 float overallScale = scale * (float)(w/(float)pageWidth);
1966 dc->SetUserScale(overallScale, overallScale);
1967
1968 if (m_printoutView)
1969 {
1970 m_printoutView->OnDraw(dc);
1971 }
68379eaf 1972 return true;
c801d85f
KB
1973}
1974
1975bool wxDocPrintout::HasPage(int pageNum)
1976{
0fb67cd1 1977 return (pageNum == 1);
c801d85f
KB
1978}
1979
1980bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1981{
0fb67cd1 1982 if (!wxPrintout::OnBeginDocument(startPage, endPage))
68379eaf 1983 return false;
c801d85f 1984
68379eaf 1985 return true;
c801d85f
KB
1986}
1987
1988void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1989{
0fb67cd1
VZ
1990 *minPage = 1;
1991 *maxPage = 1;
1992 *selPageFrom = 1;
1993 *selPageTo = 1;
c801d85f
KB
1994}
1995
0fb67cd1 1996#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 1997
0fb67cd1 1998// ----------------------------------------------------------------------------
c77049a0 1999// File history (a.k.a. MRU, most recently used, files list)
0fb67cd1 2000// ----------------------------------------------------------------------------
c801d85f 2001
e49c85af 2002wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
c801d85f 2003{
0fb67cd1 2004 m_fileMaxFiles = maxFiles;
e49c85af 2005 m_idBase = idBase;
c801d85f
KB
2006}
2007
c801d85f
KB
2008void wxFileHistory::AddFileToHistory(const wxString& file)
2009{
c77049a0
VZ
2010 // check if we don't already have this file
2011 const wxFileName fnNew(file);
2012 size_t i,
2013 numFiles = m_fileHistory.size();
2014 for ( i = 0; i < numFiles; i++ )
7f555861 2015 {
c77049a0 2016 if ( fnNew == m_fileHistory[i] )
02718e6a
VZ
2017 {
2018 // we do have it, move it to the top of the history
c77049a0
VZ
2019 RemoveFileFromHistory(i);
2020 numFiles--;
2021 break;
02718e6a 2022 }
7f555861 2023 }
0fb67cd1 2024
02718e6a 2025 // if we already have a full history, delete the one at the end
c77049a0 2026 if ( numFiles == m_fileMaxFiles )
0fb67cd1 2027 {
c77049a0 2028 RemoveFileFromHistory(--numFiles);
c801d85f 2029 }
838dd956
VZ
2030
2031 // add a new menu item to all file menus (they will be updated below)
2032 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2033 node;
2034 node = node->GetNext() )
0fb67cd1 2035 {
838dd956 2036 wxMenu * const menu = (wxMenu *)node->GetData();
c77049a0 2037
838dd956
VZ
2038 if ( !numFiles && menu->GetMenuItemCount() )
2039 menu->AppendSeparator();
c77049a0 2040
838dd956
VZ
2041 // label doesn't matter, it will be set below anyhow, but it can't
2042 // be empty (this is supposed to indicate a stock item)
2043 menu->Append(m_idBase + numFiles, " ");
0fb67cd1 2044 }
b4a980f4 2045
c77049a0
VZ
2046 // insert the new file in the beginning of the file history
2047 m_fileHistory.insert(m_fileHistory.begin(), file);
2048 numFiles++;
02718e6a 2049
c77049a0
VZ
2050 // update the labels in all menus
2051 for ( i = 0; i < numFiles; i++ )
2052 {
2053 // if in same directory just show the filename; otherwise the full path
2054 const wxFileName fnOld(m_fileHistory[i]);
b4a980f4 2055
c77049a0
VZ
2056 wxString pathInMenu;
2057 if ( fnOld.GetPath() == fnNew.GetPath() )
2058 {
2059 pathInMenu = fnOld.GetFullName();
2060 }
2061 else // file in different directory
2062 {
2063 // absolute path; could also set relative path
2064 pathInMenu = m_fileHistory[i];
2065 }
b4a980f4 2066
c77049a0
VZ
2067 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2068 node;
2069 node = node->GetNext() )
2070 {
2071 wxMenu * const menu = (wxMenu *)node->GetData();
2072
2073 menu->SetLabel(m_idBase + i, GetMRUEntryLabel(i, pathInMenu));
0fb67cd1 2074 }
c77049a0 2075 }
c801d85f
KB
2076}
2077
e49c85af 2078void wxFileHistory::RemoveFileFromHistory(size_t i)
0c5d3e1c 2079{
c77049a0
VZ
2080 size_t numFiles = m_fileHistory.size();
2081 wxCHECK_RET( i < numFiles,
223d09f6 2082 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c 2083
f6fe9f9c 2084 // delete the element from the array
b4a980f4 2085 m_fileHistory.RemoveAt(i);
c77049a0 2086 numFiles--;
0c5d3e1c 2087
c77049a0
VZ
2088 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2089 node;
2090 node = node->GetNext() )
00e6d8ab 2091 {
c77049a0 2092 wxMenu * const menu = (wxMenu *) node->GetData();
00e6d8ab 2093
c77049a0
VZ
2094 // shift filenames up
2095 for ( size_t j = i; j < numFiles; j++ )
4a10ea8b 2096 {
c77049a0 2097 menu->SetLabel(m_idBase + j, GetMRUEntryLabel(j, m_fileHistory[j]));
4a10ea8b 2098 }
0c5d3e1c 2099
2b273975 2100 // delete the last menu item which is unused now
c77049a0
VZ
2101 const wxWindowID lastItemId = m_idBase + numFiles;
2102 if ( menu->FindItem(lastItemId) )
e49c85af 2103 menu->Delete(lastItemId);
2b273975 2104
717a57c2 2105 // delete the last separator too if no more files are left
c77049a0 2106 if ( m_fileHistory.empty() )
717a57c2 2107 {
c77049a0
VZ
2108 const wxMenuItemList::compatibility_iterator
2109 nodeLast = menu->GetMenuItems().GetLast();
f1afd2e0 2110 if ( nodeLast )
717a57c2 2111 {
c77049a0
VZ
2112 wxMenuItem * const lastMenuItem = nodeLast->GetData();
2113 if ( lastMenuItem->IsSeparator() )
2114 menu->Delete(lastMenuItem);
717a57c2
VZ
2115 }
2116 //else: menu is empty somehow
2117 }
0c5d3e1c 2118 }
c801d85f
KB
2119}
2120
7f555861 2121void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 2122{
c77049a0 2123 if ( !m_fileMenus.Member(menu) )
0fb67cd1 2124 m_fileMenus.Append(menu);
c801d85f
KB
2125}
2126
7f555861
JS
2127void wxFileHistory::RemoveMenu(wxMenu *menu)
2128{
0fb67cd1 2129 m_fileMenus.DeleteObject(menu);
7f555861
JS
2130}
2131
702ca7c0 2132#if wxUSE_CONFIG
9c8116f8 2133void wxFileHistory::Load(const wxConfigBase& config)
c801d85f 2134{
b4a980f4
VZ
2135 m_fileHistory.Clear();
2136
0fb67cd1 2137 wxString buf;
b4a980f4
VZ
2138 buf.Printf(wxT("file%d"), 1);
2139
0fb67cd1 2140 wxString historyFile;
f6fe9f9c 2141 while ((m_fileHistory.GetCount() < m_fileMaxFiles) &&
b4a980f4 2142 config.Read(buf, &historyFile) && !historyFile.empty())
0fb67cd1 2143 {
b4a980f4
VZ
2144 m_fileHistory.Add(historyFile);
2145
2146 buf.Printf(wxT("file%d"), (int)m_fileHistory.GetCount()+1);
b494c48b 2147 historyFile = wxEmptyString;
0fb67cd1 2148 }
b4a980f4 2149
0fb67cd1 2150 AddFilesToMenu();
c801d85f
KB
2151}
2152
7f555861 2153void wxFileHistory::Save(wxConfigBase& config)
c801d85f 2154{
e49c85af 2155 size_t i;
9d4a05be 2156 for (i = 0; i < m_fileMaxFiles; i++)
0fb67cd1
VZ
2157 {
2158 wxString buf;
8c9564b3 2159 buf.Printf(wxT("file%d"), (int)i+1);
b4a980f4 2160 if (i < m_fileHistory.GetCount())
9d4a05be
JS
2161 config.Write(buf, wxString(m_fileHistory[i]));
2162 else
42771621 2163 config.Write(buf, wxEmptyString);
0fb67cd1 2164 }
7f555861 2165}
0fb67cd1 2166#endif // wxUSE_CONFIG
7f555861
JS
2167
2168void wxFileHistory::AddFilesToMenu()
2169{
c77049a0
VZ
2170 if ( m_fileHistory.empty() )
2171 return;
c8c5c7f6 2172
c77049a0
VZ
2173 for ( wxList::compatibility_iterator node = m_fileMenus.GetFirst();
2174 node;
2175 node = node->GetNext() )
2176 {
2177 AddFilesToMenu((wxMenu *) node->GetData());
7f555861
JS
2178 }
2179}
2180
2181void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2182{
c77049a0
VZ
2183 if ( m_fileHistory.empty() )
2184 return;
c8c5c7f6 2185
c77049a0
VZ
2186 if ( menu->GetMenuItemCount() )
2187 menu->AppendSeparator();
2188
2189 for ( size_t i = 0; i < m_fileHistory.GetCount(); i++ )
2190 {
2191 menu->Append(m_idBase + i, GetMRUEntryLabel(i, m_fileHistory[i]));
2192 }
c801d85f
KB
2193}
2194
0fb67cd1
VZ
2195// ----------------------------------------------------------------------------
2196// Permits compatibility with existing file formats and functions that
2197// manipulate files directly
2198// ----------------------------------------------------------------------------
c801d85f 2199
a533f5c1 2200#if wxUSE_STD_IOSTREAM
fb6e5b17 2201
dd107c50 2202bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2203{
fb6e5b17
VZ
2204 wxFFile file(filename, _T("rb"));
2205 if ( !file.IsOpened() )
68379eaf 2206 return false;
c801d85f 2207
fb6e5b17
VZ
2208 char buf[4096];
2209
2210 size_t nRead;
2211 do
2212 {
2213 nRead = file.Read(buf, WXSIZEOF(buf));
2214 if ( file.Error() )
68379eaf 2215 return false;
fb6e5b17
VZ
2216
2217 stream.write(buf, nRead);
2218 if ( !stream )
68379eaf 2219 return false;
fb6e5b17
VZ
2220 }
2221 while ( !file.Eof() );
c801d85f 2222
68379eaf 2223 return true;
c801d85f
KB
2224}
2225
dd107c50 2226bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2227{
fb6e5b17
VZ
2228 wxFFile file(filename, _T("wb"));
2229 if ( !file.IsOpened() )
68379eaf 2230 return false;
c801d85f 2231
fb6e5b17
VZ
2232 char buf[4096];
2233 do
0fb67cd1 2234 {
fb6e5b17
VZ
2235 stream.read(buf, WXSIZEOF(buf));
2236 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2237 {
2238 if ( !file.Write(buf, stream.gcount()) )
68379eaf 2239 return false;
fb6e5b17 2240 }
0fb67cd1 2241 }
fb6e5b17
VZ
2242 while ( !stream.eof() );
2243
68379eaf 2244 return true;
c801d85f 2245}
fb6e5b17
VZ
2246
2247#else // !wxUSE_STD_IOSTREAM
2248
dc1efb1d
JS
2249bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2250{
fb6e5b17
VZ
2251 wxFFile file(filename, _T("rb"));
2252 if ( !file.IsOpened() )
68379eaf 2253 return false;
dc1efb1d 2254
fb6e5b17
VZ
2255 char buf[4096];
2256
2257 size_t nRead;
2258 do
2259 {
2260 nRead = file.Read(buf, WXSIZEOF(buf));
2261 if ( file.Error() )
68379eaf 2262 return false;
fb6e5b17
VZ
2263
2264 stream.Write(buf, nRead);
2265 if ( !stream )
68379eaf 2266 return false;
fb6e5b17
VZ
2267 }
2268 while ( !file.Eof() );
dc1efb1d 2269
68379eaf 2270 return true;
dc1efb1d
JS
2271}
2272
2273bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2274{
fb6e5b17
VZ
2275 wxFFile file(filename, _T("wb"));
2276 if ( !file.IsOpened() )
68379eaf 2277 return false;
dc1efb1d 2278
fb6e5b17 2279 char buf[4096];
07f87b6b 2280 for ( ;; )
dc1efb1d 2281 {
fb6e5b17
VZ
2282 stream.Read(buf, WXSIZEOF(buf));
2283
2284 const size_t nRead = stream.LastRead();
07f87b6b
VZ
2285 if ( !nRead )
2286 {
2287 if ( stream.Eof() )
2288 break;
2289
2290 return false;
2291 }
2292
2293 if ( !file.Write(buf, nRead) )
68379eaf 2294 return false;
dc1efb1d 2295 }
fb6e5b17 2296
68379eaf 2297 return true;
dc1efb1d 2298}
fb6e5b17
VZ
2299
2300#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
c801d85f 2301
0fb67cd1 2302#endif // wxUSE_DOC_VIEW_ARCHITECTURE