]> git.saurik.com Git - wxWidgets.git/blame - src/common/docview.cpp
wxMotif compilation fixes for wxDataObject and PROCESS_EVENTS (wxSocket)
[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
223d09f6 106static const char *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{
0fb67cd1
VZ
531 GetDocumentManager()->ActivateView(this, FALSE, TRUE);
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;
ac0ac824 709 m_lastDirectory = wxT("") ;
0fb67cd1
VZ
710 if (initialize)
711 Initialize();
c801d85f
KB
712}
713
0fb67cd1 714wxDocManager::~wxDocManager()
c801d85f 715{
0fb67cd1
VZ
716 Clear();
717 if (m_fileHistory)
718 delete m_fileHistory;
c801d85f
KB
719}
720
721bool wxDocManager::Clear(bool force)
722{
0fb67cd1
VZ
723 wxNode *node = m_docs.First();
724 while (node)
725 {
726 wxDocument *doc = (wxDocument *)node->Data();
727 wxNode *next = node->Next();
728
729 if (!doc->Close() && !force)
730 return FALSE;
731
732 // Implicitly deletes the document when the last
733 // view is removed (deleted)
734 doc->DeleteAllViews();
735
736 // Check document is deleted
737 if (m_docs.Member(doc))
738 delete doc;
739
740 // This assumes that documents are not connected in
741 // any way, i.e. deleting one document does NOT
742 // delete another.
743 node = next;
744 }
745 node = m_templates.First();
746 while (node)
747 {
c801d85f
KB
748 wxDocTemplate *templ = (wxDocTemplate*) node->Data();
749 wxNode* next = node->Next();
750 delete templ;
751 node = next;
0fb67cd1
VZ
752 }
753 return TRUE;
c801d85f
KB
754}
755
0fb67cd1 756bool wxDocManager::Initialize()
c801d85f 757{
0fb67cd1
VZ
758 m_fileHistory = OnCreateFileHistory();
759 return TRUE;
c801d85f
KB
760}
761
0fb67cd1 762wxFileHistory *wxDocManager::OnCreateFileHistory()
c801d85f 763{
0fb67cd1 764 return new wxFileHistory;
c801d85f
KB
765}
766
767void wxDocManager::OnFileClose(wxCommandEvent& WXUNUSED(event))
768{
0fb67cd1
VZ
769 wxDocument *doc = GetCurrentDocument();
770 if (!doc)
771 return;
772 if (doc->Close())
773 {
774 doc->DeleteAllViews();
775 if (m_docs.Member(doc))
776 delete doc;
777 }
c801d85f
KB
778}
779
780void wxDocManager::OnFileNew(wxCommandEvent& WXUNUSED(event))
781{
0fb67cd1 782 CreateDocument(wxString(""), wxDOC_NEW);
c801d85f
KB
783}
784
785void wxDocManager::OnFileOpen(wxCommandEvent& WXUNUSED(event))
786{
0fb67cd1 787 CreateDocument(wxString(""), 0);
c801d85f
KB
788}
789
790void wxDocManager::OnFileRevert(wxCommandEvent& WXUNUSED(event))
791{
0fb67cd1
VZ
792 wxDocument *doc = GetCurrentDocument();
793 if (!doc)
794 return;
795 doc->Revert();
c801d85f
KB
796}
797
798void wxDocManager::OnFileSave(wxCommandEvent& WXUNUSED(event))
799{
0fb67cd1
VZ
800 wxDocument *doc = GetCurrentDocument();
801 if (!doc)
802 return;
803 doc->Save();
c801d85f
KB
804}
805
806void wxDocManager::OnFileSaveAs(wxCommandEvent& WXUNUSED(event))
807{
0fb67cd1
VZ
808 wxDocument *doc = GetCurrentDocument();
809 if (!doc)
810 return;
811 doc->SaveAs();
c801d85f
KB
812}
813
814void wxDocManager::OnPrint(wxCommandEvent& WXUNUSED(event))
815{
88ac883a 816#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
817 wxView *view = GetCurrentView();
818 if (!view)
819 return;
c801d85f 820
0fb67cd1
VZ
821 wxPrintout *printout = view->OnCreatePrintout();
822 if (printout)
823 {
824 wxPrinter printer;
825 printer.Print(view->GetFrame(), printout, TRUE);
c801d85f 826
0fb67cd1
VZ
827 delete printout;
828 }
88ac883a 829#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
830}
831
832void wxDocManager::OnPrintSetup(wxCommandEvent& WXUNUSED(event))
833{
ce4169a4 834#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
835 wxWindow *parentWin = wxTheApp->GetTopWindow();
836 wxView *view = GetCurrentView();
837 if (view)
838 parentWin = view->GetFrame();
c801d85f 839
0fb67cd1 840 wxPrintDialogData data;
c801d85f 841
c2ff79b1 842 wxPrintDialog printerDialog(parentWin, &data);
0fb67cd1
VZ
843 printerDialog.GetPrintDialogData().SetSetupDialog(TRUE);
844 printerDialog.ShowModal();
ce4169a4 845#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
846}
847
848void wxDocManager::OnPreview(wxCommandEvent& WXUNUSED(event))
849{
88ac883a 850#if wxUSE_PRINTING_ARCHITECTURE
0fb67cd1
VZ
851 wxView *view = GetCurrentView();
852 if (!view)
853 return;
c801d85f 854
0fb67cd1
VZ
855 wxPrintout *printout = view->OnCreatePrintout();
856 if (printout)
857 {
858 // Pass two printout objects: for preview, and possible printing.
859 wxPrintPreviewBase *preview = (wxPrintPreviewBase *) NULL;
860 preview = new wxPrintPreview(printout, view->OnCreatePrintout());
861
862 wxPreviewFrame *frame = new wxPreviewFrame(preview, (wxFrame *)wxTheApp->GetTopWindow(), _("Print Preview"),
863 wxPoint(100, 100), wxSize(600, 650));
864 frame->Centre(wxBOTH);
865 frame->Initialize();
866 frame->Show(TRUE);
867 }
88ac883a 868#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f
KB
869}
870
871void wxDocManager::OnUndo(wxCommandEvent& WXUNUSED(event))
872{
0fb67cd1
VZ
873 wxDocument *doc = GetCurrentDocument();
874 if (!doc)
875 return;
876 if (doc->GetCommandProcessor())
877 doc->GetCommandProcessor()->Undo();
c801d85f
KB
878}
879
880void wxDocManager::OnRedo(wxCommandEvent& WXUNUSED(event))
881{
0fb67cd1
VZ
882 wxDocument *doc = GetCurrentDocument();
883 if (!doc)
884 return;
885 if (doc->GetCommandProcessor())
886 doc->GetCommandProcessor()->Redo();
c801d85f
KB
887}
888
6de2f8b9 889wxView *wxDocManager::GetCurrentView() const
637f467a
JS
890{
891 if (m_currentView)
892 return m_currentView;
893 if (m_docs.Number() == 1)
894 {
895 wxDocument* doc = (wxDocument*) m_docs.First()->Data();
896 return doc->GetFirstView();
897 }
c67daf87 898 return (wxView *) NULL;
637f467a
JS
899}
900
901// Extend event processing to search the view's event table
902bool wxDocManager::ProcessEvent(wxEvent& event)
903{
904 wxView* view = GetCurrentView();
905 if (view)
906 {
907 if (view->ProcessEvent(event))
908 return TRUE;
909 }
910 return wxEvtHandler::ProcessEvent(event);
911}
912
c801d85f
KB
913wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
914{
0fb67cd1
VZ
915 wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
916 int i;
917 int n = 0;
918 for (i = 0; i < m_templates.Number(); i++)
c801d85f 919 {
0fb67cd1
VZ
920 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
921 if (temp->IsVisible())
922 {
923 templates[n] = temp;
924 n ++;
925 }
c801d85f 926 }
0fb67cd1 927 if (n == 0)
c801d85f 928 {
0fb67cd1
VZ
929 delete[] templates;
930 return (wxDocument *) NULL;
c801d85f 931 }
0fb67cd1
VZ
932
933 // If we've reached the max number of docs, close the
934 // first one.
935 if (GetDocuments().Number() >= m_maxDocsOpen)
c801d85f 936 {
0fb67cd1
VZ
937 wxDocument *doc = (wxDocument *)GetDocuments().First()->Data();
938 if (doc->Close())
939 {
940 // Implicitly deletes the document when
941 // the last view is deleted
942 doc->DeleteAllViews();
943
944 // Check we're really deleted
945 if (m_docs.Member(doc))
946 delete doc;
947 }
948 else
949 return (wxDocument *) NULL;
c801d85f
KB
950 }
951
0fb67cd1
VZ
952 // New document: user chooses a template, unless there's only one.
953 if (flags & wxDOC_NEW)
c801d85f 954 {
0fb67cd1
VZ
955 if (n == 1)
956 {
957 wxDocTemplate *temp = templates[0];
958 delete[] templates;
959 wxDocument *newDoc = temp->CreateDocument(path, flags);
960 if (newDoc)
961 {
962 newDoc->SetDocumentName(temp->GetDocumentName());
963 newDoc->SetDocumentTemplate(temp);
964 newDoc->OnNewDocument();
965 }
966 return newDoc;
967 }
968
969 wxDocTemplate *temp = SelectDocumentType(templates, n);
970 delete[] templates;
971 if (temp)
972 {
973 wxDocument *newDoc = temp->CreateDocument(path, flags);
974 if (newDoc)
975 {
976 newDoc->SetDocumentName(temp->GetDocumentName());
977 newDoc->SetDocumentTemplate(temp);
978 newDoc->OnNewDocument();
979 }
980 return newDoc;
981 }
982 else
983 return (wxDocument *) NULL;
c801d85f 984 }
c801d85f 985
0fb67cd1
VZ
986 // Existing document
987 wxDocTemplate *temp = (wxDocTemplate *) NULL;
c801d85f 988
223d09f6
KB
989 wxString path2(wxT(""));
990 if (path != wxT(""))
0fb67cd1 991 path2 = path;
c801d85f 992
0fb67cd1
VZ
993 if (flags & wxDOC_SILENT)
994 temp = FindTemplateForPath(path2);
995 else
996 temp = SelectDocumentPath(templates, n, path2, flags);
c801d85f 997
0fb67cd1 998 delete[] templates;
c801d85f 999
0fb67cd1 1000 if (temp)
c801d85f 1001 {
0fb67cd1
VZ
1002 wxDocument *newDoc = temp->CreateDocument(path2, flags);
1003 if (newDoc)
1004 {
1005 newDoc->SetDocumentName(temp->GetDocumentName());
1006 newDoc->SetDocumentTemplate(temp);
1007 if (!newDoc->OnOpenDocument(path2))
1008 {
1009 delete newDoc;
1010 return (wxDocument *) NULL;
1011 }
1012 AddFileToHistory(path2);
1013 }
1014 return newDoc;
c801d85f 1015 }
0fb67cd1
VZ
1016 else
1017 return (wxDocument *) NULL;
c801d85f
KB
1018}
1019
1020wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
1021{
0fb67cd1
VZ
1022 wxDocTemplate **templates = new wxDocTemplate *[m_templates.Number()];
1023 int n =0;
1024 int i;
1025 for (i = 0; i < m_templates.Number(); i++)
1026 {
1027 wxDocTemplate *temp = (wxDocTemplate *)(m_templates.Nth(i)->Data());
1028 if (temp->IsVisible())
1029 {
1030 if (temp->GetDocumentName() == doc->GetDocumentName())
1031 {
1032 templates[n] = temp;
1033 n ++;
1034 }
1035 }
1036 }
1037 if (n == 0)
1038 {
1039 delete[] templates;
1040 return (wxView *) NULL;
1041 }
1042 if (n == 1)
1043 {
1044 wxDocTemplate *temp = templates[0];
1045 delete[] templates;
1046 wxView *view = temp->CreateView(doc, flags);
1047 if (view)
1048 view->SetViewName(temp->GetViewName());
1049 return view;
1050 }
1051
1052 wxDocTemplate *temp = SelectViewType(templates, n);
c801d85f 1053 delete[] templates;
0fb67cd1
VZ
1054 if (temp)
1055 {
1056 wxView *view = temp->CreateView(doc, flags);
1057 if (view)
1058 view->SetViewName(temp->GetViewName());
1059 return view;
1060 }
1061 else
1062 return (wxView *) NULL;
c801d85f
KB
1063}
1064
1065// Not yet implemented
1066void wxDocManager::DeleteTemplate(wxDocTemplate *WXUNUSED(temp), long WXUNUSED(flags))
1067{
1068}
1069
1070// Not yet implemented
1071bool wxDocManager::FlushDoc(wxDocument *WXUNUSED(doc))
1072{
0fb67cd1 1073 return FALSE;
c801d85f
KB
1074}
1075
6de2f8b9 1076wxDocument *wxDocManager::GetCurrentDocument() const
c801d85f 1077{
0fb67cd1
VZ
1078 if (m_currentView)
1079 return m_currentView->GetDocument();
1080 else
1081 return (wxDocument *) NULL;
c801d85f
KB
1082}
1083
1084// Make a default document name
1085bool wxDocManager::MakeDefaultName(wxString& name)
1086{
0fb67cd1
VZ
1087 name.Printf(_("unnamed%d"), m_defaultDocumentNameCounter);
1088 m_defaultDocumentNameCounter++;
53c6e7cc 1089
0fb67cd1 1090 return TRUE;
c801d85f
KB
1091}
1092
1093// Not yet implemented
1094wxDocTemplate *wxDocManager::MatchTemplate(const wxString& WXUNUSED(path))
1095{
0fb67cd1 1096 return (wxDocTemplate *) NULL;
c801d85f
KB
1097}
1098
1099// File history management
1100void wxDocManager::AddFileToHistory(const wxString& file)
1101{
0fb67cd1
VZ
1102 if (m_fileHistory)
1103 m_fileHistory->AddFileToHistory(file);
c801d85f
KB
1104}
1105
0c5d3e1c
VZ
1106void wxDocManager::RemoveFileFromHistory(int i)
1107{
1108 if (m_fileHistory)
1109 m_fileHistory->RemoveFileFromHistory(i);
1110}
1111
c801d85f
KB
1112wxString wxDocManager::GetHistoryFile(int i) const
1113{
0fb67cd1
VZ
1114 wxString histFile;
1115
1116 if (m_fileHistory)
1117 histFile = m_fileHistory->GetHistoryFile(i);
1118
1119 return histFile;
c801d85f
KB
1120}
1121
1122void wxDocManager::FileHistoryUseMenu(wxMenu *menu)
1123{
0fb67cd1
VZ
1124 if (m_fileHistory)
1125 m_fileHistory->UseMenu(menu);
c801d85f
KB
1126}
1127
7f555861 1128void wxDocManager::FileHistoryRemoveMenu(wxMenu *menu)
c801d85f 1129{
0fb67cd1
VZ
1130 if (m_fileHistory)
1131 m_fileHistory->RemoveMenu(menu);
c801d85f
KB
1132}
1133
702ca7c0 1134#if wxUSE_CONFIG
7f555861 1135void wxDocManager::FileHistoryLoad(wxConfigBase& config)
c801d85f 1136{
0fb67cd1
VZ
1137 if (m_fileHistory)
1138 m_fileHistory->Load(config);
7f555861
JS
1139}
1140
1141void wxDocManager::FileHistorySave(wxConfigBase& config)
1142{
0fb67cd1
VZ
1143 if (m_fileHistory)
1144 m_fileHistory->Save(config);
7f555861 1145}
ac57418f 1146#endif
7f555861
JS
1147
1148void wxDocManager::FileHistoryAddFilesToMenu(wxMenu* menu)
1149{
0fb67cd1
VZ
1150 if (m_fileHistory)
1151 m_fileHistory->AddFilesToMenu(menu);
7f555861
JS
1152}
1153
1154void wxDocManager::FileHistoryAddFilesToMenu()
1155{
0fb67cd1
VZ
1156 if (m_fileHistory)
1157 m_fileHistory->AddFilesToMenu();
c801d85f
KB
1158}
1159
6de2f8b9 1160int wxDocManager::GetNoHistoryFiles() const
c801d85f 1161{
0fb67cd1
VZ
1162 if (m_fileHistory)
1163 return m_fileHistory->GetNoHistoryFiles();
c801d85f 1164 else
0fb67cd1 1165 return 0;
c801d85f
KB
1166}
1167
1168
6de2f8b9
VZ
1169// Find out the document template via matching in the document file format
1170// against that of the template
c801d85f
KB
1171wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
1172{
0fb67cd1 1173 wxDocTemplate *theTemplate = (wxDocTemplate *) NULL;
c801d85f 1174
0fb67cd1
VZ
1175 // Find the template which this extension corresponds to
1176 int i;
1177 for (i = 0; i < m_templates.Number(); i++)
c801d85f 1178 {
0fb67cd1 1179 wxDocTemplate *temp = (wxDocTemplate *)m_templates.Nth(i)->Data();
6de2f8b9 1180 if ( temp->FileMatchesTemplate(path) )
0fb67cd1
VZ
1181 {
1182 theTemplate = temp;
1183 break;
1184 }
c801d85f 1185 }
0fb67cd1 1186 return theTemplate;
c801d85f
KB
1187}
1188
1189// Prompts user to open a file, using file specs in templates.
1190// How to implement in wxWindows? Must extend the file selector
1191// dialog or implement own; OR match the extension to the
1192// template extension.
df875e59 1193
c801d85f 1194wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
f6147cfc 1195#ifdef __WXMSW__
0fb67cd1 1196 int noTemplates,
f6147cfc
VZ
1197#else
1198 int WXUNUSED(noTemplates),
1199#endif
0fb67cd1
VZ
1200 wxString& path,
1201 long WXUNUSED(flags),
1202 bool WXUNUSED(save))
c801d85f 1203{
0fb67cd1 1204 // We can only have multiple filters in Windows
2049ba38 1205#ifdef __WXMSW__
ba681060
VZ
1206 wxString descrBuf;
1207
1208 int i;
1209 for (i = 0; i < noTemplates; i++)
c801d85f 1210 {
ba681060
VZ
1211 if (templates[i]->IsVisible())
1212 {
1213 // add a '|' to separate this filter from the previous one
1214 if ( !descrBuf.IsEmpty() )
223d09f6 1215 descrBuf << wxT('|');
ba681060
VZ
1216
1217 descrBuf << templates[i]->GetDescription()
223d09f6 1218 << wxT(" (") << templates[i]->GetFileFilter() << wxT(") |")
0fb67cd1 1219 << templates[i]->GetFileFilter();
ba681060 1220 }
c801d85f 1221 }
a4294b78 1222#else
223d09f6 1223 wxString descrBuf = wxT("*.*");
a4294b78 1224#endif
c801d85f 1225
6de2f8b9
VZ
1226 int FilterIndex = 0;
1227 wxString pathTmp = wxFileSelectorEx(_("Select a file"),
ac0ac824 1228 m_lastDirectory,
223d09f6 1229 wxT(""),
6de2f8b9 1230 &FilterIndex,
caf0debf
VZ
1231 descrBuf,
1232 0,
1233 wxTheApp->GetTopWindow());
ba681060 1234
0fb67cd1
VZ
1235 if (!pathTmp.IsEmpty())
1236 {
ac0ac824
JS
1237 m_lastDirectory = wxPathOnly(pathTmp);
1238
0fb67cd1
VZ
1239 path = pathTmp;
1240 wxString theExt = FindExtension(path);
1241 if (!theExt)
1242 return (wxDocTemplate *) NULL;
1243
1244 // This is dodgy in that we're selecting the template on the
1245 // basis of the file extension, which may not be a standard
1246 // one. We really want to know exactly which template was
1247 // chosen by using a more advanced file selector.
1248 wxDocTemplate *theTemplate = FindTemplateForPath(path);
6de2f8b9
VZ
1249 if ( !theTemplate )
1250 theTemplate = templates[FilterIndex];
1251
0fb67cd1
VZ
1252 return theTemplate;
1253 }
1254 else
1255 {
223d09f6 1256 path = wxT("");
0fb67cd1
VZ
1257 return (wxDocTemplate *) NULL;
1258 }
a4294b78 1259#if 0
0fb67cd1
VZ
1260 // In all other windowing systems, until we have more advanced
1261 // file selectors, we must select the document type (template) first, and
1262 // _then_ pop up the file selector.
1263 wxDocTemplate *temp = SelectDocumentType(templates, noTemplates);
1264 if (!temp)
1265 return (wxDocTemplate *) NULL;
1266
223d09f6 1267 wxChar *pathTmp = wxFileSelector(_("Select a file"), wxT(""), wxT(""),
0fb67cd1
VZ
1268 temp->GetDefaultExtension(),
1269 temp->GetFileFilter(),
1270 0, wxTheApp->GetTopWindow());
1271
1272 if (pathTmp)
1273 {
1274 path = pathTmp;
1275 return temp;
1276 }
1277 else
1278 return (wxDocTemplate *) NULL;
1279#endif // 0
c801d85f
KB
1280}
1281
1282wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
0fb67cd1
VZ
1283 int noTemplates)
1284{
73974df1 1285 wxChar **strings = new wxChar *[noTemplates];
50920146 1286 wxChar **data = new wxChar *[noTemplates];
0fb67cd1
VZ
1287 int i;
1288 int n = 0;
1289 for (i = 0; i < noTemplates; i++)
1290 {
1291 if (templates[i]->IsVisible())
1292 {
73974df1 1293 strings[n] = (wxChar *)templates[i]->m_description.c_str();
50920146 1294 data[n] = (wxChar *)templates[i];
0fb67cd1
VZ
1295 n ++;
1296 }
1297 }
1298 if (n == 0)
1299 {
1300 delete[] strings;
1301 delete[] data;
1302 return (wxDocTemplate *) NULL;
1303 }
1304 else if (n == 1)
1305 {
1306 wxDocTemplate *temp = (wxDocTemplate *)data[0];
1307 delete[] strings;
1308 delete[] data;
1309 return temp;
1310 }
1311
1312 wxDocTemplate *theTemplate = (wxDocTemplate *)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n,
c980c992 1313 strings, (char **)data);
c801d85f
KB
1314 delete[] strings;
1315 delete[] data;
0fb67cd1 1316 return theTemplate;
c801d85f
KB
1317}
1318
1319wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
0fb67cd1 1320 int noTemplates)
c801d85f 1321{
73974df1 1322 wxChar **strings = new wxChar *[noTemplates];
50920146 1323 wxChar **data = new wxChar *[noTemplates];
0fb67cd1
VZ
1324 int i;
1325 int n = 0;
1326 for (i = 0; i < noTemplates; i++)
c801d85f 1327 {
223d09f6 1328 if (templates[i]->IsVisible() && (templates[i]->GetViewName() != wxT("")))
0fb67cd1 1329 {
73974df1 1330 strings[n] = (wxChar *)templates[i]->m_viewTypeName.c_str();
50920146 1331 data[n] = (wxChar *)templates[i];
0fb67cd1
VZ
1332 n ++;
1333 }
c801d85f 1334 }
0fb67cd1 1335 wxDocTemplate *theTemplate = (wxDocTemplate *)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n,
c980c992 1336 strings, (char **)data);
0fb67cd1
VZ
1337 delete[] strings;
1338 delete[] data;
1339 return theTemplate;
c801d85f
KB
1340}
1341
1342void wxDocManager::AssociateTemplate(wxDocTemplate *temp)
1343{
0fb67cd1
VZ
1344 if (!m_templates.Member(temp))
1345 m_templates.Append(temp);
c801d85f
KB
1346}
1347
1348void wxDocManager::DisassociateTemplate(wxDocTemplate *temp)
1349{
0fb67cd1 1350 m_templates.DeleteObject(temp);
c801d85f
KB
1351}
1352
1353// Add and remove a document from the manager's list
1354void wxDocManager::AddDocument(wxDocument *doc)
1355{
0fb67cd1
VZ
1356 if (!m_docs.Member(doc))
1357 m_docs.Append(doc);
c801d85f
KB
1358}
1359
1360void wxDocManager::RemoveDocument(wxDocument *doc)
1361{
0fb67cd1 1362 m_docs.DeleteObject(doc);
c801d85f
KB
1363}
1364
1365// Views or windows should inform the document manager
1366// when a view is going in or out of focus
1367void wxDocManager::ActivateView(wxView *view, bool activate, bool WXUNUSED(deleting))
1368{
0fb67cd1
VZ
1369 // If we're deactiving, and if we're not actually deleting the view, then
1370 // don't reset the current view because we may be going to
1371 // a window without a view.
1372 // WHAT DID I MEAN BY THAT EXACTLY?
1373 /*
1374 if (deleting)
1375 {
1376 if (m_currentView == view)
1377 m_currentView = NULL;
1378 }
1379 else
1380 */
1381 {
1382 if (activate)
1383 m_currentView = view;
1384 else
1385 m_currentView = (wxView *) NULL;
1386 }
c801d85f
KB
1387}
1388
0fb67cd1
VZ
1389// ----------------------------------------------------------------------------
1390// Default document child frame
1391// ----------------------------------------------------------------------------
c801d85f
KB
1392
1393BEGIN_EVENT_TABLE(wxDocChildFrame, wxFrame)
1394 EVT_ACTIVATE(wxDocChildFrame::OnActivate)
387a3b02 1395 EVT_CLOSE(wxDocChildFrame::OnCloseWindow)
c801d85f
KB
1396END_EVENT_TABLE()
1397
0fb67cd1
VZ
1398wxDocChildFrame::wxDocChildFrame(wxDocument *doc,
1399 wxView *view,
1400 wxFrame *frame,
1401 wxWindowID id,
1402 const wxString& title,
1403 const wxPoint& pos,
1404 const wxSize& size,
1405 long style,
1406 const wxString& name)
1407 : wxFrame(frame, id, title, pos, size, style, name)
1408{
1409 m_childDocument = doc;
1410 m_childView = view;
1411 if (view)
1412 view->SetFrame(this);
c801d85f
KB
1413}
1414
0fb67cd1 1415wxDocChildFrame::~wxDocChildFrame()
c801d85f
KB
1416{
1417}
1418
1419// Extend event processing to search the view's event table
1420bool wxDocChildFrame::ProcessEvent(wxEvent& event)
1421{
1422 if (m_childView)
1423 m_childView->Activate(TRUE);
1424
0fb67cd1 1425 if ( !m_childView || ! m_childView->ProcessEvent(event) )
c801d85f
KB
1426 {
1427 // Only hand up to the parent if it's a menu command
1428 if (!event.IsKindOf(CLASSINFO(wxCommandEvent)) || !GetParent() || !GetParent()->ProcessEvent(event))
0fb67cd1 1429 return wxEvtHandler::ProcessEvent(event);
c801d85f
KB
1430 else
1431 return TRUE;
1432 }
0fb67cd1
VZ
1433 else
1434 return TRUE;
c801d85f
KB
1435}
1436
c801d85f
KB
1437void wxDocChildFrame::OnActivate(wxActivateEvent& event)
1438{
0fb67cd1 1439 wxFrame::OnActivate(event);
c801d85f 1440
0fb67cd1
VZ
1441 if (m_childView)
1442 m_childView->Activate(event.GetActive());
c801d85f
KB
1443}
1444
387a3b02 1445void wxDocChildFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1446{
0fb67cd1 1447 if (m_childView)
c801d85f 1448 {
0fb67cd1
VZ
1449 bool ans = FALSE;
1450 if (!event.CanVeto())
1451 ans = TRUE; // Must delete.
1452 else
1453 ans = m_childView->Close(FALSE); // FALSE means don't delete associated window
387a3b02 1454
0fb67cd1
VZ
1455 if (ans)
1456 {
1457 m_childView->Activate(FALSE);
1458 delete m_childView;
1459 m_childView = (wxView *) NULL;
1460 m_childDocument = (wxDocument *) NULL;
1461
1462 this->Destroy();
1463 }
1464 else
1465 event.Veto();
c801d85f 1466 }
387a3b02 1467 else
0fb67cd1 1468 event.Veto();
c801d85f
KB
1469}
1470
0fb67cd1
VZ
1471// ----------------------------------------------------------------------------
1472// Default parent frame
1473// ----------------------------------------------------------------------------
c801d85f
KB
1474
1475BEGIN_EVENT_TABLE(wxDocParentFrame, wxFrame)
1476 EVT_MENU(wxID_EXIT, wxDocParentFrame::OnExit)
f7bd2698 1477 EVT_MENU_RANGE(wxID_FILE1, wxID_FILE9, wxDocParentFrame::OnMRUFile)
387a3b02 1478 EVT_CLOSE(wxDocParentFrame::OnCloseWindow)
c801d85f
KB
1479END_EVENT_TABLE()
1480
0fb67cd1
VZ
1481wxDocParentFrame::wxDocParentFrame(wxDocManager *manager,
1482 wxFrame *frame,
1483 wxWindowID id,
1484 const wxString& title,
1485 const wxPoint& pos,
1486 const wxSize& size,
1487 long style,
1488 const wxString& name)
1489 : wxFrame(frame, id, title, pos, size, style, name)
c801d85f 1490{
0fb67cd1 1491 m_docManager = manager;
c801d85f
KB
1492}
1493
1494void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
1495{
1496 Close();
1497}
1498
1499void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
1500{
0c5d3e1c
VZ
1501 int n = event.GetSelection() - wxID_FILE1; // the index in MRU list
1502 wxString filename(m_docManager->GetHistoryFile(n));
1503 if ( !filename.IsEmpty() )
1504 {
1505 // verify that the file exists before doing anything else
1506 if ( wxFile::Exists(filename) )
1507 {
1508 // try to open it
1509 (void)m_docManager->CreateDocument(filename, wxDOC_SILENT);
1510 }
1511 else
1512 {
1513 // remove the bogus filename from the MRU list and notify the user
1514 // about it
1515 m_docManager->RemoveFileFromHistory(n);
1516
1517 wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
1518 "It has been also removed from the MRU files list."),
1519 filename.c_str());
1520 }
1521 }
c801d85f
KB
1522}
1523
1524// Extend event processing to search the view's event table
1525bool wxDocParentFrame::ProcessEvent(wxEvent& event)
1526{
1527 // Try the document manager, then do default processing
1528 if (!m_docManager || !m_docManager->ProcessEvent(event))
1529 return wxEvtHandler::ProcessEvent(event);
1530 else
1531 return TRUE;
1532}
1533
c801d85f
KB
1534// Define the behaviour for the frame closing
1535// - must delete all frames except for the main one.
387a3b02 1536void wxDocParentFrame::OnCloseWindow(wxCloseEvent& event)
c801d85f 1537{
0fb67cd1
VZ
1538 if (m_docManager->Clear(!event.CanVeto()))
1539 {
1540 this->Destroy();
1541 }
1542 else
1543 event.Veto();
c801d85f
KB
1544}
1545
47d67540 1546#if wxUSE_PRINTING_ARCHITECTURE
c801d85f 1547
0fb67cd1 1548wxDocPrintout::wxDocPrintout(wxView *view, const wxString& title)
e90c1d2a 1549 : wxPrintout(title)
c801d85f 1550{
0fb67cd1 1551 m_printoutView = view;
c801d85f
KB
1552}
1553
1554bool wxDocPrintout::OnPrintPage(int WXUNUSED(page))
1555{
0fb67cd1
VZ
1556 wxDC *dc = GetDC();
1557
1558 // Get the logical pixels per inch of screen and printer
1559 int ppiScreenX, ppiScreenY;
1560 GetPPIScreen(&ppiScreenX, &ppiScreenY);
1561 int ppiPrinterX, ppiPrinterY;
1562 GetPPIPrinter(&ppiPrinterX, &ppiPrinterY);
1563
1564 // This scales the DC so that the printout roughly represents the
1565 // the screen scaling. The text point size _should_ be the right size
1566 // but in fact is too small for some reason. This is a detail that will
1567 // need to be addressed at some point but can be fudged for the
1568 // moment.
1569 float scale = (float)((float)ppiPrinterX/(float)ppiScreenX);
1570
1571 // Now we have to check in case our real page size is reduced
1572 // (e.g. because we're drawing to a print preview memory DC)
1573 int pageWidth, pageHeight;
1574 int w, h;
1575 dc->GetSize(&w, &h);
1576 GetPageSizePixels(&pageWidth, &pageHeight);
1577
1578 // If printer pageWidth == current DC width, then this doesn't
1579 // change. But w might be the preview bitmap width, so scale down.
1580 float overallScale = scale * (float)(w/(float)pageWidth);
1581 dc->SetUserScale(overallScale, overallScale);
1582
1583 if (m_printoutView)
1584 {
1585 m_printoutView->OnDraw(dc);
1586 }
1587 return TRUE;
c801d85f
KB
1588}
1589
1590bool wxDocPrintout::HasPage(int pageNum)
1591{
0fb67cd1 1592 return (pageNum == 1);
c801d85f
KB
1593}
1594
1595bool wxDocPrintout::OnBeginDocument(int startPage, int endPage)
1596{
0fb67cd1
VZ
1597 if (!wxPrintout::OnBeginDocument(startPage, endPage))
1598 return FALSE;
c801d85f 1599
0fb67cd1 1600 return TRUE;
c801d85f
KB
1601}
1602
1603void wxDocPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo)
1604{
0fb67cd1
VZ
1605 *minPage = 1;
1606 *maxPage = 1;
1607 *selPageFrom = 1;
1608 *selPageTo = 1;
c801d85f
KB
1609}
1610
0fb67cd1 1611#endif // wxUSE_PRINTING_ARCHITECTURE
c801d85f 1612
0fb67cd1
VZ
1613// ----------------------------------------------------------------------------
1614// Command processing framework
1615// ----------------------------------------------------------------------------
c801d85f
KB
1616
1617wxCommand::wxCommand(bool canUndoIt, const wxString& name)
1618{
0fb67cd1
VZ
1619 m_canUndo = canUndoIt;
1620 m_commandName = name;
c801d85f
KB
1621}
1622
0fb67cd1 1623wxCommand::~wxCommand()
c801d85f
KB
1624{
1625}
1626
1627// Command processor
1628wxCommandProcessor::wxCommandProcessor(int maxCommands)
1629{
0fb67cd1
VZ
1630 m_maxNoCommands = maxCommands;
1631 m_currentCommand = (wxNode *) NULL;
1632 m_commandEditMenu = (wxMenu *) NULL;
c801d85f
KB
1633}
1634
0fb67cd1 1635wxCommandProcessor::~wxCommandProcessor()
c801d85f 1636{
0fb67cd1 1637 ClearCommands();
c801d85f
KB
1638}
1639
1640// Pass a command to the processor. The processor calls Do();
1641// if successful, is appended to the command history unless
1642// storeIt is FALSE.
1643bool wxCommandProcessor::Submit(wxCommand *command, bool storeIt)
1644{
0fb67cd1
VZ
1645 bool success = command->Do();
1646 if (success && storeIt)
c801d85f 1647 {
0fb67cd1
VZ
1648 if (m_commands.Number() == m_maxNoCommands)
1649 {
1650 wxNode *firstNode = m_commands.First();
1651 wxCommand *firstCommand = (wxCommand *)firstNode->Data();
1652 delete firstCommand;
1653 delete firstNode;
1654 }
c801d85f 1655
0fb67cd1
VZ
1656 // Correct a bug: we must chop off the current 'branch'
1657 // so that we're at the end of the command list.
1658 if (!m_currentCommand)
1659 ClearCommands();
1660 else
1661 {
1662 wxNode *node = m_currentCommand->Next();
1663 while (node)
1664 {
1665 wxNode *next = node->Next();
1666 delete (wxCommand *)node->Data();
1667 delete node;
1668 node = next;
1669 }
1670 }
1671
1672 m_commands.Append(command);
1673 m_currentCommand = m_commands.Last();
1674 SetMenuStrings();
c801d85f 1675 }
0fb67cd1 1676 return success;
c801d85f
KB
1677}
1678
0fb67cd1 1679bool wxCommandProcessor::Undo()
c801d85f 1680{
0fb67cd1 1681 if (m_currentCommand)
c801d85f 1682 {
0fb67cd1
VZ
1683 wxCommand *command = (wxCommand *)m_currentCommand->Data();
1684 if (command->CanUndo())
1685 {
1686 bool success = command->Undo();
1687 if (success)
1688 {
1689 m_currentCommand = m_currentCommand->Previous();
1690 SetMenuStrings();
1691 return TRUE;
1692 }
1693 }
c801d85f 1694 }
0fb67cd1 1695 return FALSE;
c801d85f
KB
1696}
1697
0fb67cd1 1698bool wxCommandProcessor::Redo()
c801d85f 1699{
0fb67cd1
VZ
1700 wxCommand *redoCommand = (wxCommand *) NULL;
1701 wxNode *redoNode = (wxNode *) NULL;
1702 if (m_currentCommand && m_currentCommand->Next())
1703 {
1704 redoCommand = (wxCommand *)m_currentCommand->Next()->Data();
1705 redoNode = m_currentCommand->Next();
1706 }
1707 else
c801d85f 1708 {
0fb67cd1
VZ
1709 if (m_commands.Number() > 0)
1710 {
1711 redoCommand = (wxCommand *)m_commands.First()->Data();
1712 redoNode = m_commands.First();
1713 }
c801d85f 1714 }
c801d85f 1715
0fb67cd1 1716 if (redoCommand)
c801d85f 1717 {
0fb67cd1
VZ
1718 bool success = redoCommand->Do();
1719 if (success)
1720 {
1721 m_currentCommand = redoNode;
1722 SetMenuStrings();
1723 return TRUE;
1724 }
c801d85f 1725 }
0fb67cd1 1726 return FALSE;
c801d85f
KB
1727}
1728
6de2f8b9 1729bool wxCommandProcessor::CanUndo() const
c801d85f 1730{
0fb67cd1
VZ
1731 if (m_currentCommand)
1732 return ((wxCommand *)m_currentCommand->Data())->CanUndo();
1733 return FALSE;
c801d85f
KB
1734}
1735
6de2f8b9 1736bool wxCommandProcessor::CanRedo() const
7f555861 1737{
f97c9854
JS
1738 if ((m_currentCommand != (wxNode*) NULL) && (m_currentCommand->Next() == (wxNode*) NULL))
1739 return FALSE;
1740
1741 if ((m_currentCommand != (wxNode*) NULL) && (m_currentCommand->Next() != (wxNode*) NULL))
1742 return TRUE;
1743
1744 if ((m_currentCommand == (wxNode*) NULL) && (m_commands.Number() > 0))
1745 return TRUE;
1746
1747 return FALSE;
7f555861
JS
1748}
1749
0fb67cd1 1750void wxCommandProcessor::Initialize()
c801d85f 1751{
0fb67cd1
VZ
1752 m_currentCommand = m_commands.Last();
1753 SetMenuStrings();
c801d85f
KB
1754}
1755
0fb67cd1 1756void wxCommandProcessor::SetMenuStrings()
c801d85f 1757{
0fb67cd1 1758 if (m_commandEditMenu)
c801d85f 1759 {
0fb67cd1
VZ
1760 wxString buf;
1761 if (m_currentCommand)
1762 {
1763 wxCommand *command = (wxCommand *)m_currentCommand->Data();
1764 wxString commandName(command->GetName());
223d09f6 1765 if (commandName == wxT("")) commandName = _("Unnamed command");
0fb67cd1
VZ
1766 bool canUndo = command->CanUndo();
1767 if (canUndo)
1768 buf = wxString(_("&Undo ")) + commandName;
1769 else
1770 buf = wxString(_("Can't &Undo ")) + commandName;
1771
1772 m_commandEditMenu->SetLabel(wxID_UNDO, buf);
1773 m_commandEditMenu->Enable(wxID_UNDO, canUndo);
1774
1775 // We can redo, if we're not at the end of the history.
1776 if (m_currentCommand->Next())
1777 {
1778 wxCommand *redoCommand = (wxCommand *)m_currentCommand->Next()->Data();
1779 wxString redoCommandName(redoCommand->GetName());
223d09f6 1780 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
0fb67cd1
VZ
1781 buf = wxString(_("&Redo ")) + redoCommandName;
1782 m_commandEditMenu->SetLabel(wxID_REDO, buf);
1783 m_commandEditMenu->Enable(wxID_REDO, TRUE);
1784 }
1785 else
1786 {
1787 m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
1788 m_commandEditMenu->Enable(wxID_REDO, FALSE);
1789 }
1790 }
1791 else
1792 {
1793 m_commandEditMenu->SetLabel(wxID_UNDO, _("&Undo"));
1794 m_commandEditMenu->Enable(wxID_UNDO, FALSE);
c801d85f 1795
0fb67cd1
VZ
1796 if (m_commands.Number() == 0)
1797 {
1798 m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
1799 m_commandEditMenu->Enable(wxID_REDO, FALSE);
1800 }
1801 else
1802 {
1803 // currentCommand is NULL but there are commands: this means that
1804 // we've undone to the start of the list, but can redo the first.
1805 wxCommand *redoCommand = (wxCommand *)m_commands.First()->Data();
1806 wxString redoCommandName(redoCommand->GetName());
223d09f6 1807 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
0fb67cd1
VZ
1808 buf = wxString(_("&Redo ")) + redoCommandName;
1809 m_commandEditMenu->SetLabel(wxID_REDO, buf);
1810 m_commandEditMenu->Enable(wxID_REDO, TRUE);
1811 }
1812 }
c801d85f 1813 }
c801d85f
KB
1814}
1815
0fb67cd1 1816void wxCommandProcessor::ClearCommands()
c801d85f 1817{
0fb67cd1
VZ
1818 wxNode *node = m_commands.First();
1819 while (node)
1820 {
1821 wxCommand *command = (wxCommand *)node->Data();
1822 delete command;
1823 delete node;
1824 node = m_commands.First();
1825 }
1826 m_currentCommand = (wxNode *) NULL;
c801d85f
KB
1827}
1828
0fb67cd1
VZ
1829// ----------------------------------------------------------------------------
1830// File history processor
1831// ----------------------------------------------------------------------------
c801d85f
KB
1832
1833wxFileHistory::wxFileHistory(int maxFiles)
1834{
0fb67cd1
VZ
1835 m_fileMaxFiles = maxFiles;
1836 m_fileHistoryN = 0;
50920146 1837 m_fileHistory = new wxChar *[m_fileMaxFiles];
c801d85f
KB
1838}
1839
0fb67cd1 1840wxFileHistory::~wxFileHistory()
c801d85f 1841{
0fb67cd1
VZ
1842 int i;
1843 for (i = 0; i < m_fileHistoryN; i++)
1844 delete[] m_fileHistory[i];
1845 delete[] m_fileHistory;
c801d85f
KB
1846}
1847
1848// File history management
1849void wxFileHistory::AddFileToHistory(const wxString& file)
1850{
0fb67cd1
VZ
1851 int i;
1852 // Check we don't already have this file
1853 for (i = 0; i < m_fileHistoryN; i++)
7f555861 1854 {
0fb67cd1
VZ
1855 if (m_fileHistory[i] && wxString(m_fileHistory[i]) == file)
1856 return;
7f555861 1857 }
0fb67cd1
VZ
1858
1859 // Add to the project file history:
1860 // Move existing files (if any) down so we can insert file at beginning.
1861
1862 // First delete filename that has popped off the end of the array (if any)
1863 if (m_fileHistoryN == m_fileMaxFiles)
1864 {
1865 delete[] m_fileHistory[m_fileMaxFiles-1];
50920146 1866 m_fileHistory[m_fileMaxFiles-1] = (wxChar *) NULL;
c801d85f 1867 }
0fb67cd1
VZ
1868 if (m_fileHistoryN < m_fileMaxFiles)
1869 {
1870 wxNode* node = m_fileMenus.First();
1871 while (node)
1872 {
1873 wxMenu* menu = (wxMenu*) node->Data();
1874 if (m_fileHistoryN == 0)
1875 menu->AppendSeparator();
1876 menu->Append(wxID_FILE1+m_fileHistoryN, _("[EMPTY]"));
1877 node = node->Next();
1878 }
1879 m_fileHistoryN ++;
1880 }
1881 // Shuffle filenames down
1882 for (i = (m_fileHistoryN-1); i > 0; i--)
1883 {
1884 m_fileHistory[i] = m_fileHistory[i-1];
1885 }
1886 m_fileHistory[0] = copystring(file);
1887
1888 for (i = 0; i < m_fileHistoryN; i++)
1889 if (m_fileHistory[i])
1890 {
1891 wxString buf;
0c5d3e1c 1892 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
0fb67cd1
VZ
1893 wxNode* node = m_fileMenus.First();
1894 while (node)
1895 {
1896 wxMenu* menu = (wxMenu*) node->Data();
1897 menu->SetLabel(wxID_FILE1+i, buf);
1898 node = node->Next();
1899 }
1900 }
c801d85f
KB
1901}
1902
0c5d3e1c
VZ
1903void wxFileHistory::RemoveFileFromHistory(int i)
1904{
1905 wxCHECK_RET( i < m_fileHistoryN,
223d09f6 1906 wxT("invalid index in wxFileHistory::RemoveFileFromHistory") );
0c5d3e1c
VZ
1907
1908 wxNode* node = m_fileMenus.First();
1909 while ( node )
1910 {
1911 wxMenu* menu = (wxMenu*) node->Data();
1912
1913 // wxMenu::Delete() is missing from wxGTK, so this can't be done :-(
1914#if 0
1915 // delete the menu items
1916 menu->Delete(wxID_FILE1 + i);
1917#endif
1918
1919 // delete the element from the array (could use memmove() too...)
1920 delete [] m_fileHistory[i];
1921
1922 int j;
1923 for ( j = i; j < m_fileHistoryN - 1; j++ )
1924 {
1925 m_fileHistory[j] = m_fileHistory[j + 1];
1926 }
1927
1928 // shuffle filenames up
1929 wxString buf;
1930 for ( j = i; j < m_fileHistoryN - 1; j++ )
1931 {
1932 buf.Printf(s_MRUEntryFormat, j + 1, m_fileHistory[j]);
1933 menu->SetLabel(wxID_FILE1 + j, buf);
1934 }
1935
1936 // to be removed as soon as wxMenu::Delete() is implemented
1937#if 1
223d09f6 1938 menu->SetLabel(wxID_FILE1 + m_fileHistoryN - 1, wxT(""));
0c5d3e1c
VZ
1939#endif
1940
1941 node = node->Next();
1942 }
1943 m_fileHistoryN--;
1944}
1945
c801d85f
KB
1946wxString wxFileHistory::GetHistoryFile(int i) const
1947{
0fb67cd1
VZ
1948 if (i < m_fileHistoryN)
1949 return wxString(m_fileHistory[i]);
1950 else
1951 return wxString("");
c801d85f
KB
1952}
1953
7f555861 1954void wxFileHistory::UseMenu(wxMenu *menu)
c801d85f 1955{
0fb67cd1
VZ
1956 if (!m_fileMenus.Member(menu))
1957 m_fileMenus.Append(menu);
c801d85f
KB
1958}
1959
7f555861
JS
1960void wxFileHistory::RemoveMenu(wxMenu *menu)
1961{
0fb67cd1 1962 m_fileMenus.DeleteObject(menu);
7f555861
JS
1963}
1964
702ca7c0 1965#if wxUSE_CONFIG
7f555861 1966void wxFileHistory::Load(wxConfigBase& config)
c801d85f 1967{
0fb67cd1
VZ
1968 m_fileHistoryN = 0;
1969 wxString buf;
223d09f6 1970 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
0fb67cd1 1971 wxString historyFile;
223d09f6 1972 while ((m_fileHistoryN <= m_fileMaxFiles) && config.Read(buf, &historyFile) && (historyFile != wxT("")))
0fb67cd1 1973 {
50920146 1974 m_fileHistory[m_fileHistoryN] = copystring((const wxChar*) historyFile);
0fb67cd1 1975 m_fileHistoryN ++;
223d09f6 1976 buf.Printf(wxT("file%d"), m_fileHistoryN+1);
0fb67cd1
VZ
1977 historyFile = "";
1978 }
1979 AddFilesToMenu();
c801d85f
KB
1980}
1981
7f555861 1982void wxFileHistory::Save(wxConfigBase& config)
c801d85f 1983{
0fb67cd1
VZ
1984 int i;
1985 for (i = 0; i < m_fileHistoryN; i++)
1986 {
1987 wxString buf;
223d09f6 1988 buf.Printf(wxT("file%d"), i+1);
0fb67cd1
VZ
1989 config.Write(buf, wxString(m_fileHistory[i]));
1990 }
7f555861 1991}
0fb67cd1 1992#endif // wxUSE_CONFIG
7f555861
JS
1993
1994void wxFileHistory::AddFilesToMenu()
1995{
1996 if (m_fileHistoryN > 0)
1997 {
1998 wxNode* node = m_fileMenus.First();
1999 while (node)
2000 {
2001 wxMenu* menu = (wxMenu*) node->Data();
2002 menu->AppendSeparator();
2003 int i;
2004 for (i = 0; i < m_fileHistoryN; i++)
2005 {
2006 if (m_fileHistory[i])
2007 {
2008 wxString buf;
0c5d3e1c 2009 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
7f555861
JS
2010 menu->Append(wxID_FILE1+i, buf);
2011 }
2012 }
2013 node = node->Next();
2014 }
2015 }
2016}
2017
2018void wxFileHistory::AddFilesToMenu(wxMenu* menu)
2019{
2020 if (m_fileHistoryN > 0)
2021 {
2022 menu->AppendSeparator();
2023 int i;
2024 for (i = 0; i < m_fileHistoryN; i++)
2025 {
2026 if (m_fileHistory[i])
2027 {
2028 wxString buf;
0c5d3e1c 2029 buf.Printf(s_MRUEntryFormat, i+1, m_fileHistory[i]);
7f555861
JS
2030 menu->Append(wxID_FILE1+i, buf);
2031 }
2032 }
2033 }
c801d85f
KB
2034}
2035
0fb67cd1
VZ
2036// ----------------------------------------------------------------------------
2037// Permits compatibility with existing file formats and functions that
2038// manipulate files directly
2039// ----------------------------------------------------------------------------
c801d85f 2040
a533f5c1 2041#if wxUSE_STD_IOSTREAM
c801d85f
KB
2042bool wxTransferFileToStream(const wxString& filename, ostream& stream)
2043{
0fb67cd1
VZ
2044 FILE *fd1;
2045 int ch;
c801d85f 2046
50920146 2047 if ((fd1 = fopen (filename.fn_str(), "rb")) == NULL)
0fb67cd1 2048 return FALSE;
c801d85f 2049
0fb67cd1
VZ
2050 while ((ch = getc (fd1)) != EOF)
2051 stream << (unsigned char)ch;
c801d85f 2052
0fb67cd1
VZ
2053 fclose (fd1);
2054 return TRUE;
c801d85f
KB
2055}
2056
2057bool wxTransferStreamToFile(istream& stream, const wxString& filename)
2058{
0fb67cd1
VZ
2059 FILE *fd1;
2060 int ch;
c801d85f 2061
50920146 2062 if ((fd1 = fopen (filename.fn_str(), "wb")) == NULL)
c801d85f 2063 {
0fb67cd1 2064 return FALSE;
c801d85f
KB
2065 }
2066
0fb67cd1
VZ
2067 while (!stream.eof())
2068 {
2069 ch = stream.get();
2070 if (!stream.eof())
2071 putc (ch, fd1);
2072 }
2073 fclose (fd1);
2074 return TRUE;
c801d85f 2075}
a533f5c1 2076#endif
c801d85f 2077
0fb67cd1
VZ
2078#endif // wxUSE_DOC_VIEW_ARCHITECTURE
2079