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