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