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