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