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