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