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