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