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