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