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