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