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