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