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