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