]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
Added inline setters for wxTreeEvent so we don't need to add new
[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
967void wxDocManager::OnUndo(wxCommandEvent& WXUNUSED(event))
968{
0fb67cd1
VZ
969 wxDocument *doc = GetCurrentDocument();
970 if (!doc)
971 return;
972 if (doc->GetCommandProcessor())
973 doc->GetCommandProcessor()->Undo();
c801d85f
KB
974}
975
976void wxDocManager::OnRedo(wxCommandEvent& WXUNUSED(event))
977{
0fb67cd1
VZ
978 wxDocument *doc = GetCurrentDocument();
979 if (!doc)
980 return;
981 if (doc->GetCommandProcessor())
982 doc->GetCommandProcessor()->Redo();
c801d85f
KB
983}
984
f2506310
JS
985// Handlers for UI update commands
986
987void wxDocManager::OnUpdateFileOpen(wxUpdateUIEvent& event)
988{
989 event.Enable( TRUE );
990}
991
992void wxDocManager::OnUpdateFileClose(wxUpdateUIEvent& event)
993{
994 wxDocument *doc = GetCurrentDocument();
995 event.Enable( (doc != (wxDocument*) NULL) );
996}
997
998void wxDocManager::OnUpdateFileRevert(wxUpdateUIEvent& event)
999{
1000 wxDocument *doc = GetCurrentDocument();
1001 event.Enable( (doc != (wxDocument*) NULL) );
1002}
1003
1004void wxDocManager::OnUpdateFileNew(wxUpdateUIEvent& event)
1005{
1006 event.Enable( TRUE );
1007}
1008
1009void wxDocManager::OnUpdateFileSave(wxUpdateUIEvent& event)
1010{
1011 wxDocument *doc = GetCurrentDocument();
9cb0033c 1012 event.Enable( doc && doc->IsModified() );
f2506310
JS
1013}
1014
1015void wxDocManager::OnUpdateFileSaveAs(wxUpdateUIEvent& event)
1016{
1017 wxDocument *doc = GetCurrentDocument();
1018 event.Enable( (doc != (wxDocument*) NULL) );
1019}
1020
1021void wxDocManager::OnUpdateUndo(wxUpdateUIEvent& event)
1022{
1023 wxDocument *doc = GetCurrentDocument();
1024 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanUndo()) );
2599ee02
JS
1025 if (doc && doc->GetCommandProcessor())
1026 doc->GetCommandProcessor()->SetMenuStrings();
f2506310
JS
1027}
1028
1029void wxDocManager::OnUpdateRedo(wxUpdateUIEvent& event)
1030{
1031 wxDocument *doc = GetCurrentDocument();
1032 event.Enable( (doc && doc->GetCommandProcessor() && doc->GetCommandProcessor()->CanRedo()) );
2599ee02
JS
1033 if (doc && doc->GetCommandProcessor())
1034 doc->GetCommandProcessor()->SetMenuStrings();
f2506310
JS
1035}
1036
1037void wxDocManager::OnUpdatePrint(wxUpdateUIEvent& event)
1038{
1039 wxDocument *doc = GetCurrentDocument();
1040 event.Enable( (doc != (wxDocument*) NULL) );
1041}
1042
1043void wxDocManager::OnUpdatePrintSetup(wxUpdateUIEvent& event)
1044{
1045 event.Enable( TRUE );
1046}
1047
1048void wxDocManager::OnUpdatePreview(wxUpdateUIEvent& event)
1049{
1050 wxDocument *doc = GetCurrentDocument();
1051 event.Enable( (doc != (wxDocument*) NULL) );
1052}
1053
6de2f8b9 1054wxView *wxDocManager::GetCurrentView() const
637f467a
JS
1055{
1056 if (m_currentView)
1057 return m_currentView;
b1d4dd7a 1058 if (m_docs.GetCount() == 1)
637f467a 1059 {
b1d4dd7a 1060 wxDocument* doc = (wxDocument*) m_docs.GetFirst()->GetData();
637f467a
JS
1061 return doc->GetFirstView();
1062 }
c67daf87 1063 return (wxView *) NULL;
637f467a
JS
1064}
1065
1066// Extend event processing to search the view's event table
1067bool wxDocManager::ProcessEvent(wxEvent& event)
1068{
1069 wxView* view = GetCurrentView();
1070 if (view)
1071 {
1072 if (view->ProcessEvent(event))
1073 return TRUE;
1074 }
1075 return wxEvtHandler::ProcessEvent(event);
1076}
1077
c801d85f
KB
1078wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
1079{
b1d4dd7a
RL
1080 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1081 int n = 0;
1082
1083 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1084 {
b1d4dd7a 1085 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
0fb67cd1
VZ
1086 if (temp->IsVisible())
1087 {
1088 templates[n] = temp;
1089 n ++;
1090 }
c801d85f 1091 }
0fb67cd1 1092 if (n == 0)
c801d85f 1093 {
0fb67cd1
VZ
1094 delete[] templates;
1095 return (wxDocument *) NULL;
c801d85f 1096 }
b72b1920
JS
1097
1098 wxDocument* docToClose = NULL;
0fb67cd1
VZ
1099
1100 // If we've reached the max number of docs, close the
1101 // first one.
b1d4dd7a 1102 if ( (int)GetDocuments().GetCount() >= m_maxDocsOpen )
c801d85f 1103 {
b1d4dd7a 1104 wxDocument *doc = (wxDocument *)GetDocuments().GetFirst()->GetData();
b72b1920 1105 docToClose = doc;
c801d85f
KB
1106 }
1107
0fb67cd1
VZ
1108 // New document: user chooses a template, unless there's only one.
1109 if (flags & wxDOC_NEW)
c801d85f 1110 {
0fb67cd1
VZ
1111 if (n == 1)
1112 {
b72b1920
JS
1113 if (docToClose)
1114 {
1115 if (!CloseDocument(docToClose, FALSE))
1116 {
7a311019 1117 delete[] templates;
b72b1920
JS
1118 return NULL;
1119 }
1120 }
1121
0fb67cd1
VZ
1122 wxDocTemplate *temp = templates[0];
1123 delete[] templates;
1124 wxDocument *newDoc = temp->CreateDocument(path, flags);
b72b1920 1125
0fb67cd1
VZ
1126 if (newDoc)
1127 {
1128 newDoc->SetDocumentName(temp->GetDocumentName());
1129 newDoc->SetDocumentTemplate(temp);
1130 newDoc->OnNewDocument();
1131 }
1132 return newDoc;
1133 }
1134
1135 wxDocTemplate *temp = SelectDocumentType(templates, n);
1136 delete[] templates;
1137 if (temp)
1138 {
b72b1920
JS
1139 if (docToClose)
1140 {
1141 if (!CloseDocument(docToClose, FALSE))
1142 {
1143 return NULL;
1144 }
1145 }
1146
0fb67cd1 1147 wxDocument *newDoc = temp->CreateDocument(path, flags);
b72b1920 1148
0fb67cd1
VZ
1149 if (newDoc)
1150 {
1151 newDoc->SetDocumentName(temp->GetDocumentName());
1152 newDoc->SetDocumentTemplate(temp);
1153 newDoc->OnNewDocument();
1154 }
1155 return newDoc;
1156 }
1157 else
1158 return (wxDocument *) NULL;
c801d85f 1159 }
c801d85f 1160
0fb67cd1
VZ
1161 // Existing document
1162 wxDocTemplate *temp = (wxDocTemplate *) NULL;
c801d85f 1163
223d09f6
KB
1164 wxString path2(wxT(""));
1165 if (path != wxT(""))
0fb67cd1 1166 path2 = path;
c801d85f 1167
0fb67cd1 1168 if (flags & wxDOC_SILENT)
9d4a05be 1169 {
0fb67cd1 1170 temp = FindTemplateForPath(path2);
9d4a05be
JS
1171 if (!temp)
1172 {
1173 // Since we do not add files with non-default extensions to the FileHistory this
1174 // can only happen if the application changes the allowed templates in runtime.
1175 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1176 _("Open File"),
1177 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1178 }
1179 }
0fb67cd1
VZ
1180 else
1181 temp = SelectDocumentPath(templates, n, path2, flags);
c801d85f 1182
0fb67cd1 1183 delete[] templates;
c801d85f 1184
0fb67cd1 1185 if (temp)
c801d85f 1186 {
b72b1920
JS
1187 if (docToClose)
1188 {
1189 if (!CloseDocument(docToClose, FALSE))
1190 {
1191 return NULL;
1192 }
1193 }
1194
0fb67cd1
VZ
1195 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1196 if (newDoc)
1197 {
1198 newDoc->SetDocumentName(temp->GetDocumentName());
1199 newDoc->SetDocumentTemplate(temp);
1200 if (!newDoc->OnOpenDocument(path2))
1201 {
f6bcfd97
BP
1202 newDoc->DeleteAllViews();
1203 // delete newDoc; // Implicitly deleted by DeleteAllViews
0fb67cd1
VZ
1204 return (wxDocument *) NULL;
1205 }
9d4a05be
JS
1206 // A file that doesn't use the default extension of its document
1207 // template cannot be opened via the FileHistory, so we do not
1208 // add it.
1209 if (temp->FileMatchesTemplate(path2))
1210 AddFileToHistory(path2);
0fb67cd1
VZ
1211 }
1212 return newDoc;
c801d85f 1213 }
3ca6a5f0
BP
1214
1215 return (wxDocument *) NULL;
c801d85f
KB
1216}
1217
1218wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1219{
b1d4dd7a
RL
1220 wxDocTemplate **templates = new wxDocTemplate *[m_templates.GetCount()];
1221 int n =0;
1222
1223 for (size_t i = 0; i < m_templates.GetCount(); i++)
0fb67cd1 1224 {
b1d4dd7a 1225 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Item(i)->GetData());
0fb67cd1
VZ
1226 if (temp->IsVisible())
1227 {
1228 if (temp->GetDocumentName() == doc->GetDocumentName())
1229 {
1230 templates[n] = temp;
1231 n ++;
1232 }
1233 }
1234 }
1235 if (n == 0)
1236 {
1237 delete[] templates;
1238 return (wxView *) NULL;
1239 }
1240 if (n == 1)
1241 {
1242 wxDocTemplate *temp = templates[0];
1243 delete[] templates;
1244 wxView *view = temp->CreateView(doc, flags);
1245 if (view)
1246 view->SetViewName(temp->GetViewName());
1247 return view;
1248 }
1249
1250 wxDocTemplate *temp = SelectViewType(templates, n);
c801d85f 1251 delete[] templates;
0fb67cd1
VZ
1252 if (temp)
1253 {
1254 wxView *view = temp->CreateView(doc, flags);
1255 if (view)
1256 view->SetViewName(temp->GetViewName());
1257 return view;
1258 }
1259 else
1260 return (wxView *) NULL;
c801d85f
KB
1261}
1262
1263// Not yet implemented
1264void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1265{
1266}
1267
1268// Not yet implemented
1269bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1270{
0fb67cd1 1271 return FALSE;
c801d85f
KB
1272}
1273
6de2f8b9 1274wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1275{
9cb0033c
VZ
1276 wxView *view = GetCurrentView();
1277 if (view)
1278 return view->GetDocument();
0fb67cd1
VZ
1279 else
1280 return (wxDocument *) NULL;
c801d85f
KB
1281}
1282
1283// Make a default document name
1284bool wxDocManager::MakeDefaultName(wxString& name)
1285{
0fb67cd1
VZ
1286 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1287 m_defaultDocumentNameCounter++;
53c6e7cc 1288
0fb67cd1 1289 return TRUE;
c801d85f
KB
1290}
1291
f2506310
JS
1292// Make a frame title (override this to do something different)
1293// If docName is empty, a document is not currently active.
1294wxString wxDocManager::MakeFrameTitle(wxDocument* doc)
1295{
1296 wxString appName = wxTheApp->GetAppName();
1297 wxString title;
1298 if (!doc)
1299 title = appName;
1300 else
1301 {
1302 wxString docName;
1303 doc->GetPrintableName(docName);
1304 title = docName + wxString(_(" - ")) + appName;
1305 }
1306 return title;
1307}
1308
1309
c801d85f
KB
1310// Not yet implemented
1311wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1312{
0fb67cd1 1313 return (wxDocTemplate *) NULL;
c801d85f
KB
1314}
1315
1316// File history management
1317void wxDocManager::AddFileToHistory(const wxString& file)
1318{
0fb67cd1
VZ
1319 if (m_fileHistory)
1320 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1321}
1322
e49c85af 1323void wxDocManager::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
1324{
1325 if (m_fileHistory)
1326 m_fileHistory->RemoveFileFromHistory(i);
1327}
1328
e49c85af 1329wxString wxDocManager::GetHistoryFile(size_t i) const
c801d85f 1330{
0fb67cd1
VZ
1331 wxString histFile;
1332
1333 if (m_fileHistory)
1334 histFile = m_fileHistory->GetHistoryFile(i);
1335
1336 return histFile;
c801d85f
KB
1337}
1338
1339void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1340{
0fb67cd1
VZ
1341 if (m_fileHistory)
1342 m_fileHistory->UseMenu(menu);
c801d85f
KB
1343}
1344
7f555861 1345void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1346{
0fb67cd1
VZ
1347 if (m_fileHistory)
1348 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1349}
1350
702ca7c0 1351#if wxUSE_CONFIG
7f555861 1352void wxDocManager::FileHistoryLoad(wxConfigBase& config)
c801d85f 1353{
0fb67cd1
VZ
1354 if (m_fileHistory)
1355 m_fileHistory->Load(config);
7f555861
JS
1356}
1357
1358void wxDocManager::FileHistorySave(wxConfigBase& config)
1359{
0fb67cd1
VZ
1360 if (m_fileHistory)
1361 m_fileHistory->Save(config);
7f555861 1362}
ac57418f 1363#endif
7f555861
JS
1364
1365void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1366{
0fb67cd1
VZ
1367 if (m_fileHistory)
1368 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1369}
1370
1371void wxDocManager::FileHistoryAddFilesToMenu()
1372{
0fb67cd1
VZ
1373 if (m_fileHistory)
1374 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1375}
1376
e49c85af 1377size_t wxDocManager::GetNoHistoryFiles() const
c801d85f 1378{
0fb67cd1
VZ
1379 if (m_fileHistory)
1380 return m_fileHistory->GetNoHistoryFiles();
c801d85f 1381 else
0fb67cd1 1382 return 0;
c801d85f
KB
1383}
1384
1385
6de2f8b9
VZ
1386// Find out the document template via matching in the document file format
1387// against that of the template
c801d85f
KB
1388wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1389{
0fb67cd1 1390 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
c801d85f 1391
0fb67cd1 1392 // Find the template which this extension corresponds to
b1d4dd7a 1393 for (size_t i = 0; i < m_templates.GetCount(); i++)
c801d85f 1394 {
b1d4dd7a 1395 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Item(i)->GetData();
6de2f8b9 1396 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1397 {
1398 theTemplate = temp;
1399 break;
1400 }
c801d85f 1401 }
0fb67cd1 1402 return theTemplate;
c801d85f
KB
1403}
1404
f6bcfd97
BP
1405// Try to get a more suitable parent frame than the top window,
1406// for selection dialogs. Otherwise you may get an unexpected
1407// window being activated when a dialog is shown.
1408static wxWindow* wxFindSuitableParent()
1409{
1410 wxWindow* parent = wxTheApp->GetTopWindow();
1411
1412 wxWindow* focusWindow = wxWindow::FindFocus();
1413 if (focusWindow)
1414 {
1415 while (focusWindow &&
1416 !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
1417 !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
1418
1419 focusWindow = focusWindow->GetParent();
1420
1421 if (focusWindow)
1422 parent = focusWindow;
1423 }
1424 return parent;
1425}
1426
c801d85f 1427// Prompts user to open a file, using file specs in templates.
9d4a05be
JS
1428// Must extend the file selector dialog or implement own; OR
1429// match the extension to the template extension.
df875e59 1430
c801d85f 1431wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
ce68ad25 1432#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
0fb67cd1 1433 int noTemplates,
f6147cfc
VZ
1434#else
1435 int WXUNUSED(noTemplates),
1436#endif
0fb67cd1
VZ
1437 wxString& path,
1438 long WXUNUSED(flags),
1439 bool WXUNUSED(save))
c801d85f 1440{
bf9e3e73 1441 // We can only have multiple filters in Windows and GTK
ce68ad25 1442#if defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXMAC__)
ba681060
VZ
1443 wxString descrBuf;
1444
1445 int i;
1446 for (i = 0; i < noTemplates; i++)
c801d85f 1447 {
ba681060
VZ
1448 if (templates[i]->IsVisible())
1449 {
1450 // add a '|' to separate this filter from the previous one
1451 if ( !descrBuf.IsEmpty() )
223d09f6 1452 descrBuf << wxT('|');
ba681060
VZ
1453
1454 descrBuf << templates[i]->GetDescription()
223d09f6 1455 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1456 << templates[i]->GetFileFilter();
ba681060 1457 }
c801d85f 1458 }
a4294b78 1459#else
223d09f6 1460 wxString descrBuf = wxT("*.*");
a4294b78 1461#endif
c801d85f 1462
3ca6a5f0 1463 int FilterIndex = -1;
f6bcfd97
BP
1464
1465 wxWindow* parent = wxFindSuitableParent();
1466
6de2f8b9 1467 wxString pathTmp = wxFileSelectorEx(_("Select a file"),
ac0ac824 1468 m_lastDirectory,
223d09f6 1469 wxT(""),
6de2f8b9 1470 &FilterIndex,
caf0debf
VZ
1471 descrBuf,
1472 0,
f6bcfd97 1473 parent);
ba681060 1474
3ca6a5f0 1475 wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
0fb67cd1
VZ
1476 if (!pathTmp.IsEmpty())
1477 {
f6bcfd97
BP
1478 if (!wxFileExists(pathTmp))
1479 {
1480 wxString msgTitle;
1481 if (!wxTheApp->GetAppName().IsEmpty())
1482 msgTitle = wxTheApp->GetAppName();
1483 else
1484 msgTitle = wxString(_("File error"));
02718e6a 1485
f6bcfd97
BP
1486 (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
1487 parent);
1488
1489 path = wxT("");
1490 return (wxDocTemplate *) NULL;
1491 }
ac0ac824
JS
1492 m_lastDirectory = wxPathOnly(pathTmp);
1493
0fb67cd1 1494 path = pathTmp;
0fb67cd1 1495
3ca6a5f0
BP
1496 // first choose the template using the extension, if this fails (i.e.
1497 // wxFileSelectorEx() didn't fill it), then use the path
1498 if ( FilterIndex != -1 )
6de2f8b9 1499 theTemplate = templates[FilterIndex];
3ca6a5f0
BP
1500 if ( !theTemplate )
1501 theTemplate = FindTemplateForPath(path);
9d4a05be
JS
1502 if ( !theTemplate )
1503 {
1504 // Since we do not add files with non-default extensions to the FileHistory this
1505 // can only happen if the application changes the allowed templates in runtime.
1506 (void)wxMessageBox(_("Sorry, the format for this file is unknown."),
1507 _("Open File"),
1508 wxOK | wxICON_EXCLAMATION, wxFindSuitableParent());
1509 }
0fb67cd1
VZ
1510 }
1511 else
1512 {
223d09f6 1513 path = wxT("");
0fb67cd1 1514 }
3ca6a5f0
BP
1515
1516 return theTemplate;
1517
a4294b78 1518#if 0
0fb67cd1
VZ
1519 // In all other windowing systems, until we have more advanced
1520 // file selectors, we must select the document type (template) first, and
1521 // _then_ pop up the file selector.
1522 wxDocTemplate *temp = SelectDocumentType(templates, noTemplates);
1523 if (!temp)
1524 return (wxDocTemplate *) NULL;
1525
223d09f6 1526 wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""),
0fb67cd1
VZ
1527 temp->GetDefaultExtension(),
1528 temp->GetFileFilter(),
1529 0, wxTheApp->GetTopWindow());
1530
1531 if (pathTmp)
1532 {
1533 path = pathTmp;
1534 return temp;
1535 }
1536 else
1537 return (wxDocTemplate *) NULL;
1538#endif // 0
c801d85f
KB
1539}
1540
1541wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
52b9ca21 1542 int noTemplates, bool sort)
0fb67cd1 1543{
52b9ca21 1544 wxArrayString strings(sort);
17260efd 1545 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1546 int i;
1547 int n = 0;
bb28b477 1548
8658ef93
GT
1549 for (i = 0; i < noTemplates; i++)
1550 {
1551 if (templates[i]->IsVisible())
1552 {
bb28b477
JS
1553 int j;
1554 bool want = TRUE;
1555 for (j = 0; j < n; j++)
1556 {
1557 //filter out NOT unique documents + view combinations
1558 if ( templates[i]->m_docTypeName == data[j]->m_docTypeName &&
1559 templates[i]->m_viewTypeName == data[j]->m_viewTypeName
1560 )
1561 want = FALSE;
1562 }
1563
1564 if ( want )
8658ef93 1565 {
bb28b477
JS
1566 strings.Add(templates[i]->m_description);
1567
8658ef93
GT
1568 data[n] = templates[i];
1569 n ++;
1570 }
1571 }
1572 } // for
1573
1574 if (sort)
1575 {
1576 // Yes, this will be slow, but template lists
1577 // are typically short.
1578 int j;
1579 n = strings.Count();
1580 for (i = 0; i < n; i++)
1581 {
1582 for (j = 0; j < noTemplates; j++)
1583 {
1584 if (strings[i] == templates[j]->m_description)
1585 data[i] = templates[j];
1586 }
1587 }
1588 }
17260efd
VZ
1589
1590 wxDocTemplate *theTemplate;
1591
1592 switch ( n )
0fb67cd1 1593 {
17260efd
VZ
1594 case 0:
1595 // no visible templates, hence nothing to choose from
1596 theTemplate = NULL;
1597 break;
0fb67cd1 1598
17260efd
VZ
1599 case 1:
1600 // don't propose the user to choose if he heas no choice
1601 theTemplate = data[0];
1602 break;
1603
1604 default:
1605 // propose the user to choose one of several
1606 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1607 (
1608 _("Select a document template"),
1609 _("Templates"),
1610 strings,
1611 (void **)data,
1612 wxFindSuitableParent()
1613 );
1614 }
f6bcfd97 1615
c801d85f 1616 delete[] data;
17260efd 1617
0fb67cd1 1618 return theTemplate;
c801d85f
KB
1619}
1620
1621wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
52b9ca21 1622 int noTemplates, bool sort)
c801d85f 1623{
52b9ca21 1624 wxArrayString strings(sort);
17260efd 1625 wxDocTemplate **data = new wxDocTemplate *[noTemplates];
0fb67cd1
VZ
1626 int i;
1627 int n = 0;
bb28b477 1628
0fb67cd1 1629 for (i = 0; i < noTemplates; i++)
c801d85f 1630 {
17260efd
VZ
1631 wxDocTemplate *templ = templates[i];
1632 if ( templ->IsVisible() && !templ->GetViewName().empty() )
0fb67cd1 1633 {
bb28b477
JS
1634 int j;
1635 bool want = TRUE;
1636 for (j = 0; j < n; j++)
8658ef93 1637 {
bb28b477
JS
1638 //filter out NOT unique views
1639 if ( templates[i]->m_viewTypeName == data[j]->m_viewTypeName )
1640 want = FALSE;
1641 }
1642
1643 if ( want )
1644 {
1645 strings.Add(templ->m_viewTypeName);
8658ef93
GT
1646 data[n] = templ;
1647 n ++;
1648 }
0fb67cd1 1649 }
c801d85f 1650 }
f6bcfd97 1651
8658ef93
GT
1652 if (sort)
1653 {
1654 // Yes, this will be slow, but template lists
1655 // are typically short.
1656 int j;
1657 n = strings.Count();
1658 for (i = 0; i < n; i++)
1659 {
1660 for (j = 0; j < noTemplates; j++)
1661 {
1662 if (strings[i] == templates[j]->m_viewTypeName)
1663 data[i] = templates[j];
1664 }
1665 }
1666 }
1667
17260efd
VZ
1668 wxDocTemplate *theTemplate;
1669
1670 // the same logic as above
1671 switch ( n )
1672 {
1673 case 0:
1674 theTemplate = (wxDocTemplate *)NULL;
1675 break;
1676
1677 case 1:
1678 theTemplate = data[0];
1679 break;
1680
1681 default:
1682 theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
1683 (
1684 _("Select a document view"),
1685 _("Views"),
1686 strings,
1687 (void **)data,
1688 wxFindSuitableParent()
1689 );
1690
1691 }
1692
0fb67cd1
VZ
1693 delete[] data;
1694 return theTemplate;
c801d85f
KB
1695}
1696
1697void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1698{
0fb67cd1
VZ
1699 if (!m_templates.Member(temp))
1700 m_templates.Append(temp);
c801d85f
KB
1701}
1702
1703void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1704{
0fb67cd1 1705 m_templates.DeleteObject(temp);
c801d85f
KB
1706}
1707
1708// Add and remove a document from the manager's list
1709void wxDocManager::AddDocument(wxDocument *doc)
1710{
0fb67cd1
VZ
1711 if (!m_docs.Member(doc))
1712 m_docs.Append(doc);
c801d85f
KB
1713}
1714
1715void wxDocManager::RemoveDocument(wxDocument *doc)
1716{
0fb67cd1 1717 m_docs.DeleteObject(doc);
c801d85f
KB
1718}
1719
1720// Views or windows should inform the document manager
1721// when a view is going in or out of focus
1722void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting))
1723{
0fb67cd1
VZ
1724 // If we're deactiving, and if we're not actually deleting the view, then
1725 // don't reset the current view because we may be going to
1726 // a window without a view.
1727 // WHAT DID I MEAN BY THAT EXACTLY?
1728 /*
1729 if (deleting)
1730 {
1731 if (m_currentView == view)
1732 m_currentView = NULL;
1733 }
1734 else
1735 */
1736 {
1737 if (activate)
1738 m_currentView = view;
1739 else
1740 m_currentView = (wxView *) NULL;
1741 }
c801d85f
KB
1742}
1743
0fb67cd1
VZ
1744// ----------------------------------------------------------------------------
1745// Default document child frame
1746// ----------------------------------------------------------------------------
c801d85f
KB
1747
1748BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1749 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1750 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1751END_EVENT_TABLE()
1752
0fb67cd1
VZ
1753wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1754 wxView *view,
1755 wxFrame *frame,
1756 wxWindowID id,
1757 const wxString& title,
1758 const wxPoint& pos,
1759 const wxSize& size,
1760 long style,
1761 const wxString& name)
1762 : wxFrame(frame, id, title, pos, size, style, name)
1763{
1764 m_childDocument = doc;
1765 m_childView = view;
1766 if (view)
1767 view->SetFrame(this);
c801d85f
KB
1768}
1769
0fb67cd1 1770wxDocChildFrame::~wxDocChildFrame()
c801d85f
KB
1771{
1772}
1773
1774// Extend event processing to search the view's event table
1775bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1776{
1777 if (m_childView)
1778 m_childView->Activate(TRUE);
1779
0fb67cd1 1780 if ( !m_childView || ! m_childView->ProcessEvent(event) )
c801d85f
KB
1781 {
1782 // Only hand up to the parent if it's a menu command
1783 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
0fb67cd1 1784 return wxEvtHandler::ProcessEvent(event);
c801d85f
KB
1785 else
1786 return TRUE;
1787 }
0fb67cd1
VZ
1788 else
1789 return TRUE;
c801d85f
KB
1790}
1791
c801d85f
KB
1792void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1793{
0fb67cd1 1794 wxFrame::OnActivate(event);
c801d85f 1795
0fb67cd1
VZ
1796 if (m_childView)
1797 m_childView->Activate(event.GetActive());
c801d85f
KB
1798}
1799
387a3b02 1800void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1801{
0fb67cd1 1802 if (m_childView)
c801d85f 1803 {
0fb67cd1
VZ
1804 bool ans = FALSE;
1805 if (!event.CanVeto())
1806 ans = TRUE; // Must delete.
1807 else
1808 ans = m_childView->Close(FALSE); // FALSE means don't delete associated window
387a3b02 1809
0fb67cd1
VZ
1810 if (ans)
1811 {
1812 m_childView->Activate(FALSE);
1813 delete m_childView;
1814 m_childView = (wxView *) NULL;
1815 m_childDocument = (wxDocument *) NULL;
1816
1817 this->Destroy();
1818 }
1819 else
1820 event.Veto();
c801d85f 1821 }
387a3b02 1822 else
0fb67cd1 1823 event.Veto();
c801d85f
KB
1824}
1825
0fb67cd1
VZ
1826// ----------------------------------------------------------------------------
1827// Default parent frame
1828// ----------------------------------------------------------------------------
c801d85f
KB
1829
1830BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1831 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1832 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1833 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1834END_EVENT_TABLE()
1835
0fb67cd1
VZ
1836wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1837 wxFrame *frame,
1838 wxWindowID id,
1839 const wxString& title,
1840 const wxPoint& pos,
1841 const wxSize& size,
1842 long style,
1843 const wxString& name)
1844 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1845{
0fb67cd1 1846 m_docManager = manager;
c801d85f
KB
1847}
1848
1849void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1850{
1851 Close();
1852}
1853
1854void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1855{
1fc88785 1856 int n = event.GetId() - wxID_FILE1; // the index in MRU list
0c5d3e1c
VZ
1857 wxString filename(m_docManager->GetHistoryFile(n));
1858 if ( !filename.IsEmpty() )
1859 {
1860 // verify that the file exists before doing anything else
1861 if ( wxFile::Exists(filename) )
1862 {
1863 // try to open it
9d4a05be
JS
1864 if (!m_docManager->CreateDocument(filename, wxDOC_SILENT))
1865 {
1866 // remove the file from the MRU list. The user should already be notified.
1867 m_docManager->RemoveFileFromHistory(n);
1868
1869 wxLogError(_("The file '%s' couldn't be opened.\nIt has been removed from the most recently used files list."),
1870 filename.c_str());
1871 }
0c5d3e1c
VZ
1872 }
1873 else
1874 {
1875 // remove the bogus filename from the MRU list and notify the user
1876 // about it
1877 m_docManager->RemoveFileFromHistory(n);
1878
9806a0e7 1879 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
1880 filename.c_str());
1881 }
1882 }
c801d85f
KB
1883}
1884
1885// Extend event processing to search the view's event table
1886bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1887{
1888 // Try the document manager, then do default processing
1889 if (!m_docManager || !m_docManager->ProcessEvent(event))
1890 return wxEvtHandler::ProcessEvent(event);
1891 else
1892 return TRUE;
1893}
1894
c801d85f
KB
1895// Define the behaviour for the frame closing
1896// - must delete all frames except for the main one.
387a3b02 1897void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1898{
0fb67cd1
VZ
1899 if (m_docManager->Clear(!event.CanVeto()))
1900 {
1901 this->Destroy();
1902 }
1903 else
1904 event.Veto();
c801d85f
KB
1905}
1906
47d67540 1907#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 1908
0fb67cd1 1909wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 1910 : wxPrintout(title)
c801d85f 1911{
0fb67cd1 1912 m_printoutView = view;
c801d85f
KB
1913}
1914
1915bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1916{
0fb67cd1
VZ
1917 wxDC *dc = GetDC();
1918
1919 // Get the logical pixels per inch of screen and printer
1920 int ppiScreenX, ppiScreenY;
1921 GetPPIScreen(&ppiScreenX, &ppiScreenY);
1922 int ppiPrinterX, ppiPrinterY;
1923 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
1924
1925 // This scales the DC so that the printout roughly represents the
1926 // the screen scaling. The text point size _should_ be the right size
1927 // but in fact is too small for some reason. This is a detail that will
1928 // need to be addressed at some point but can be fudged for the
1929 // moment.
1930 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1931
1932 // Now we have to check in case our real page size is reduced
1933 // (e.g. because we're drawing to a print preview memory DC)
1934 int pageWidth, pageHeight;
1935 int w, h;
1936 dc->GetSize(&w, &h);
1937 GetPageSizePixels(&pageWidth, &pageHeight);
1938
1939 // If printer pageWidth == current DC width, then this doesn't
1940 // change. But w might be the preview bitmap width, so scale down.
1941 float overallScale = scale * (float)(w/(float)pageWidth);
1942 dc->SetUserScale(overallScale, overallScale);
1943
1944 if (m_printoutView)
1945 {
1946 m_printoutView->OnDraw(dc);
1947 }
1948 return TRUE;
c801d85f
KB
1949}
1950
1951bool wxDocPrintout::HasPage(int pageNum)
1952{
0fb67cd1 1953 return (pageNum == 1);
c801d85f
KB
1954}
1955
1956bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1957{
0fb67cd1
VZ
1958 if (!wxPrintout::OnBeginDocument(startPage, endPage))
1959 return FALSE;
c801d85f 1960
0fb67cd1 1961 return TRUE;
c801d85f
KB
1962}
1963
1964void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1965{
0fb67cd1
VZ
1966 *minPage = 1;
1967 *maxPage = 1;
1968 *selPageFrom = 1;
1969 *selPageTo = 1;
c801d85f
KB
1970}
1971
0fb67cd1 1972#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 1973
0fb67cd1
VZ
1974// ----------------------------------------------------------------------------
1975// File history processor
1976// ----------------------------------------------------------------------------
c801d85f 1977
e49c85af 1978wxFileHistory::wxFileHistory(size_t maxFiles, wxWindowID idBase)
c801d85f 1979{
0fb67cd1 1980 m_fileMaxFiles = maxFiles;
e49c85af 1981 m_idBase = idBase;
0fb67cd1 1982 m_fileHistoryN = 0;
50920146 1983 m_fileHistory = new wxChar *[m_fileMaxFiles];
c801d85f
KB
1984}
1985
0fb67cd1 1986wxFileHistory::~wxFileHistory()
c801d85f 1987{
e49c85af 1988 size_t i;
0fb67cd1
VZ
1989 for (i = 0; i < m_fileHistoryN; i++)
1990 delete[] m_fileHistory[i];
1991 delete[] m_fileHistory;
c801d85f
KB
1992}
1993
1994// File history management
1995void wxFileHistory::AddFileToHistory(const wxString& file)
1996{
e49c85af 1997 size_t i;
02718e6a 1998
0fb67cd1
VZ
1999 // Check we don't already have this file
2000 for (i = 0; i < m_fileHistoryN; i++)
7f555861 2001 {
9d4a05be
JS
2002#if defined( __WXMSW__ ) // Add any other OSes with case insensitive file names
2003 wxString testString;
2004 if ( m_fileHistory[i] )
2005 testString = m_fileHistory[i];
2006 if ( m_fileHistory[i] && ( file.Lower() == testString.Lower() ) )
2007#else
2008 if ( m_fileHistory[i] && ( file == m_fileHistory[i] ) )
2009#endif
02718e6a
VZ
2010 {
2011 // we do have it, move it to the top of the history
2012 RemoveFileFromHistory (i);
2013 AddFileToHistory (file);
0fb67cd1 2014 return;
02718e6a 2015 }
7f555861 2016 }
0fb67cd1 2017
02718e6a
VZ
2018 // if we already have a full history, delete the one at the end
2019 if ( m_fileMaxFiles == m_fileHistoryN )
0fb67cd1 2020 {
02718e6a
VZ
2021 RemoveFileFromHistory (m_fileHistoryN - 1);
2022 AddFileToHistory (file);
2023 return;
c801d85f 2024 }
02718e6a
VZ
2025
2026 // Add to the project file history:
2027 // Move existing files (if any) down so we can insert file at beginning.
0fb67cd1
VZ
2028 if (m_fileHistoryN < m_fileMaxFiles)
2029 {
b1d4dd7a 2030 wxNode* node = m_fileMenus.GetFirst();
0fb67cd1
VZ
2031 while (node)
2032 {
b1d4dd7a 2033 wxMenu* menu = (wxMenu*) node->GetData();
c8c5c7f6
JS
2034 if ( m_fileHistoryN == 0 && menu->GetMenuItemCount() )
2035 {
0fb67cd1 2036 menu->AppendSeparator();
c8c5c7f6 2037 }
e49c85af 2038 menu->Append(m_idBase+m_fileHistoryN, _("[EMPTY]"));
b1d4dd7a 2039 node = node->GetNext();
0fb67cd1
VZ
2040 }
2041 m_fileHistoryN ++;
2042 }
2043 // Shuffle filenames down
2044 for (i = (m_fileHistoryN-1); i > 0; i--)
2045 {
2046 m_fileHistory[i] = m_fileHistory[i-1];
2047 }
2048 m_fileHistory[0] = copystring(file);
2049
02718e6a
VZ
2050 // this is the directory of the last opened file
2051 wxString pathCurrent;
2052 wxSplitPath( m_fileHistory[0], &pathCurrent, NULL, NULL );
0fb67cd1 2053 for (i = 0; i < m_fileHistoryN; i++)
02718e6a
VZ
2054 {
2055 if ( m_fileHistory[i] )
0fb67cd1 2056 {
02718e6a
VZ
2057 // if in same directory just show the filename; otherwise the full
2058 // path
2059 wxString pathInMenu, path, filename, ext;
2060 wxSplitPath( m_fileHistory[i], &path, &filename, &ext );
2061 if ( path == pathCurrent )
2062 {
2063 pathInMenu = filename;
2064 if ( !ext.empty() )
2065 pathInMenu = pathInMenu + wxFILE_SEP_EXT + ext;
2066 }
2067 else
2068 {
2069 // absolute path; could also set relative path
2070 pathInMenu = m_fileHistory[i];
2071 }
2072
0fb67cd1 2073 wxString buf;
02718e6a 2074 buf.Printf(s_MRUEntryFormat, i + 1, pathInMenu.c_str());
b1d4dd7a 2075 wxNode* node = m_fileMenus.GetFirst();
0fb67cd1
VZ
2076 while (node)
2077 {
b1d4dd7a 2078 wxMenu* menu = (wxMenu*) node->GetData();
e49c85af 2079 menu->SetLabel(m_idBase + i, buf);
b1d4dd7a 2080 node = node->GetNext();
0fb67cd1
VZ
2081 }
2082 }
02718e6a 2083 }
c801d85f
KB
2084}
2085
e49c85af 2086void wxFileHistory::RemoveFileFromHistory(size_t i)
0c5d3e1c
VZ
2087{
2088 wxCHECK_RET( i < m_fileHistoryN,
223d09f6 2089 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c 2090
e49c85af
VZ
2091 // delete the element from the array (could use memmove() too...)
2092 delete [] m_fileHistory[i];
0c5d3e1c 2093
e49c85af
VZ
2094 size_t j;
2095 for ( j = i; j < m_fileHistoryN - 1; j++ )
2096 {
2097 m_fileHistory[j] = m_fileHistory[j + 1];
2098 }
0c5d3e1c 2099
b1d4dd7a 2100 wxNode* node = m_fileMenus.GetFirst();
00e6d8ab
VZ
2101 while ( node )
2102 {
e49c85af 2103 wxMenu* menu = (wxMenu*) node->GetData();
00e6d8ab 2104
e49c85af
VZ
2105 // shuffle filenames up
2106 wxString buf;
2107 for ( j = i; j < m_fileHistoryN - 1; j++ )
2108 {
2109 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
2110 menu->SetLabel(m_idBase + j, buf);
2111 }
0c5d3e1c 2112
e49c85af 2113 node = node->GetNext();
2b273975
VZ
2114
2115 // delete the last menu item which is unused now
e49c85af
VZ
2116 wxWindowID lastItemId = m_idBase + m_fileHistoryN - 1;
2117 if (menu->FindItem(lastItemId))
2118 {
2119 menu->Delete(lastItemId);
2120 }
2b273975 2121
717a57c2
VZ
2122 // delete the last separator too if no more files are left
2123 if ( m_fileHistoryN == 1 )
2124 {
2125 wxMenuItemList::Node *node = menu->GetMenuItems().GetLast();
2126 if ( node )
2127 {
2128 wxMenuItem *menuItem = node->GetData();
2129 if ( menuItem->IsSeparator() )
2130 {
2131 menu->Delete(menuItem);
2132 }
2133 //else: should we search backwards for the last separator?
2134 }
2135 //else: menu is empty somehow
2136 }
0c5d3e1c 2137 }
2b273975 2138
0c5d3e1c
VZ
2139 m_fileHistoryN--;
2140}
2141
e49c85af 2142wxString wxFileHistory::GetHistoryFile(size_t i) const
c801d85f 2143{
8ee9d618
VZ
2144 wxString s;
2145 if ( i < m_fileHistoryN )
2146 {
2147 s = m_fileHistory[i];
2148 }
0fb67cd1 2149 else
8ee9d618
VZ
2150 {
2151 wxFAIL_MSG( wxT("bad index in wxFileHistory::GetHistoryFile") );
2152 }
2153
2154 return s;
c801d85f
KB
2155}
2156
7f555861 2157void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 2158{
0fb67cd1
VZ
2159 if (!m_fileMenus.Member(menu))
2160 m_fileMenus.Append(menu);
c801d85f
KB
2161}
2162
7f555861
JS
2163void wxFileHistory::RemoveMenu(wxMenu *menu)
2164{
0fb67cd1 2165 m_fileMenus.DeleteObject(menu);
7f555861
JS
2166}
2167
702ca7c0 2168#if wxUSE_CONFIG
7f555861 2169void wxFileHistory::Load(wxConfigBase& config)
c801d85f 2170{
0fb67cd1
VZ
2171 m_fileHistoryN = 0;
2172 wxString buf;
a25d51af 2173 buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
0fb67cd1 2174 wxString historyFile;
d07ce169 2175 while ((m_fileHistoryN < m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
0fb67cd1 2176 {
50920146 2177 m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
0fb67cd1 2178 m_fileHistoryN ++;
a25d51af 2179 buf.Printf(wxT("file%d"), (int)m_fileHistoryN+1);
58c837a4 2180 historyFile = wxT("");
0fb67cd1
VZ
2181 }
2182 AddFilesToMenu();
c801d85f
KB
2183}
2184
7f555861 2185void wxFileHistory::Save(wxConfigBase& config)
c801d85f 2186{
e49c85af 2187 size_t i;
9d4a05be 2188 for (i = 0; i < m_fileMaxFiles; i++)
0fb67cd1
VZ
2189 {
2190 wxString buf;
8c9564b3 2191 buf.Printf(wxT("file%d"), (int)i+1);
9d4a05be
JS
2192 if (i < m_fileHistoryN)
2193 config.Write(buf, wxString(m_fileHistory[i]));
2194 else
2195 config.Write(buf, wxEmptyString);
0fb67cd1 2196 }
7f555861 2197}
0fb67cd1 2198#endif // wxUSE_CONFIG
7f555861
JS
2199
2200void wxFileHistory::AddFilesToMenu()
2201{
2202 if (m_fileHistoryN > 0)
2203 {
b1d4dd7a 2204 wxNode* node = m_fileMenus.GetFirst();
7f555861
JS
2205 while (node)
2206 {
b1d4dd7a 2207 wxMenu* menu = (wxMenu*) node->GetData();
c8c5c7f6
JS
2208 if (menu->GetMenuItemCount())
2209 {
2210 menu->AppendSeparator();
2211 }
2212
e49c85af 2213 size_t i;
7f555861
JS
2214 for (i = 0; i < m_fileHistoryN; i++)
2215 {
2216 if (m_fileHistory[i])
2217 {
2218 wxString buf;
0c5d3e1c 2219 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
e49c85af 2220 menu->Append(m_idBase+i, buf);
7f555861
JS
2221 }
2222 }
b1d4dd7a 2223 node = node->GetNext();
7f555861
JS
2224 }
2225 }
2226}
2227
2228void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2229{
2230 if (m_fileHistoryN > 0)
2231 {
c8c5c7f6
JS
2232 if (menu->GetMenuItemCount())
2233 {
2234 menu->AppendSeparator();
2235 }
2236
e49c85af 2237 size_t i;
7f555861
JS
2238 for (i = 0; i < m_fileHistoryN; i++)
2239 {
2240 if (m_fileHistory[i])
2241 {
2242 wxString buf;
0c5d3e1c 2243 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
e49c85af 2244 menu->Append(m_idBase+i, buf);
7f555861
JS
2245 }
2246 }
2247 }
c801d85f
KB
2248}
2249
0fb67cd1
VZ
2250// ----------------------------------------------------------------------------
2251// Permits compatibility with existing file formats and functions that
2252// manipulate files directly
2253// ----------------------------------------------------------------------------
c801d85f 2254
a533f5c1 2255#if wxUSE_STD_IOSTREAM
fb6e5b17 2256
dd107c50 2257bool wxTransferFileToStream(const wxString& filename, wxSTD ostream& stream)
c801d85f 2258{
fb6e5b17
VZ
2259 wxFFile file(filename, _T("rb"));
2260 if ( !file.IsOpened() )
0fb67cd1 2261 return FALSE;
c801d85f 2262
fb6e5b17
VZ
2263 char buf[4096];
2264
2265 size_t nRead;
2266 do
2267 {
2268 nRead = file.Read(buf, WXSIZEOF(buf));
2269 if ( file.Error() )
2270 return FALSE;
2271
2272 stream.write(buf, nRead);
2273 if ( !stream )
2274 return FALSE;
2275 }
2276 while ( !file.Eof() );
c801d85f 2277
0fb67cd1 2278 return TRUE;
c801d85f
KB
2279}
2280
dd107c50 2281bool wxTransferStreamToFile(wxSTD istream& stream, const wxString& filename)
c801d85f 2282{
fb6e5b17
VZ
2283 wxFFile file(filename, _T("wb"));
2284 if ( !file.IsOpened() )
0fb67cd1 2285 return FALSE;
c801d85f 2286
fb6e5b17
VZ
2287 char buf[4096];
2288 do
0fb67cd1 2289 {
fb6e5b17
VZ
2290 stream.read(buf, WXSIZEOF(buf));
2291 if ( !stream.bad() ) // fail may be set on EOF, don't use operator!()
2292 {
2293 if ( !file.Write(buf, stream.gcount()) )
2294 return FALSE;
2295 }
0fb67cd1 2296 }
fb6e5b17
VZ
2297 while ( !stream.eof() );
2298
0fb67cd1 2299 return TRUE;
c801d85f 2300}
fb6e5b17
VZ
2301
2302#else // !wxUSE_STD_IOSTREAM
2303
dc1efb1d
JS
2304bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
2305{
fb6e5b17
VZ
2306 wxFFile file(filename, _T("rb"));
2307 if ( !file.IsOpened() )
dc1efb1d
JS
2308 return FALSE;
2309
fb6e5b17
VZ
2310 char buf[4096];
2311
2312 size_t nRead;
2313 do
2314 {
2315 nRead = file.Read(buf, WXSIZEOF(buf));
2316 if ( file.Error() )
2317 return FALSE;
2318
2319 stream.Write(buf, nRead);
2320 if ( !stream )
2321 return FALSE;
2322 }
2323 while ( !file.Eof() );
dc1efb1d 2324
dc1efb1d
JS
2325 return TRUE;
2326}
2327
2328bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
2329{
fb6e5b17
VZ
2330 wxFFile file(filename, _T("wb"));
2331 if ( !file.IsOpened() )
dc1efb1d 2332 return FALSE;
dc1efb1d 2333
fb6e5b17
VZ
2334 char buf[4096];
2335 do
dc1efb1d 2336 {
fb6e5b17
VZ
2337 stream.Read(buf, WXSIZEOF(buf));
2338
2339 const size_t nRead = stream.LastRead();
2340 if ( !nRead || !file.Write(buf, nRead) )
2341 return FALSE;
dc1efb1d 2342 }
fb6e5b17
VZ
2343 while ( !stream.Eof() );
2344
dc1efb1d
JS
2345 return TRUE;
2346}
fb6e5b17
VZ
2347
2348#endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
c801d85f 2349
0fb67cd1
VZ
2350#endif // wxUSE_DOC_VIEW_ARCHITECTURE
2351