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