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