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