]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/docview.cpp
Various fixes for wxFilename.
[wxWidgets.git] / src / common / docview.cpp
index 538bb871f45898b5e2cbc1cf8441cb6a75166dca..50bd316bea94b5e576e0c1db2394f58df9260215 100644 (file)
@@ -130,6 +130,7 @@ wxDocument::wxDocument(wxDocument *parent)
     m_documentModified = FALSE;
     m_documentParent = parent;
     m_documentTemplate = (wxDocTemplate *) NULL;
+    m_commandProcessor = (wxCommandProcessor*) NULL;
     m_savedYet = FALSE;
 }
 
@@ -145,7 +146,8 @@ wxDocument::~wxDocument()
     if (m_commandProcessor)
         delete m_commandProcessor;
 
-    GetDocumentManager()->RemoveDocument(this);
+    if (GetDocumentManager())
+        GetDocumentManager()->RemoveDocument(this);
 
     // Not safe to do here, since it'll invoke virtual view functions
     // expecting to see valid derived objects: and by the time we get here,
@@ -172,6 +174,8 @@ bool wxDocument::OnCloseDocument()
 // deleted.
 bool wxDocument::DeleteAllViews()
 {
+    wxDocManager* manager = GetDocumentManager();
+
     wxNode *node = m_documentViews.First();
     while (node)
     {
@@ -184,6 +188,11 @@ bool wxDocument::DeleteAllViews()
         delete view; // Deletes node implicitly
         node = next;
     }
+    // If we haven't yet deleted the document (for example
+    // if there were no views) then delete it.
+    if (manager && manager->GetDocuments().Member(this))
+        delete this;
+
     return TRUE;
 }
 
@@ -196,7 +205,7 @@ wxView *wxDocument::GetFirstView() const
 
 wxDocManager *wxDocument::GetDocumentManager() const
 {
-    return m_documentTemplate->GetDocumentManager();
+    return (m_documentTemplate ? m_documentTemplate->GetDocumentManager() : (wxDocManager*) NULL);
 }
 
 bool wxDocument::OnNewDocument()
@@ -287,7 +296,7 @@ bool wxDocument::OnSaveDocument(const wxString& file)
         msgTitle = wxString(_("File error"));
 
 #if wxUSE_STD_IOSTREAM
-    ofstream store(wxString(file.fn_str()));
+    ofstream store(wxString(file.fn_str()).mb_str());
     if (store.fail() || store.bad())
 #else
     wxFileOutputStream store(wxString(file.fn_str()));
@@ -323,7 +332,7 @@ bool wxDocument::OnOpenDocument(const wxString& file)
         msgTitle = wxString(_("File error"));
 
 #if wxUSE_STD_IOSTREAM
-    ifstream store(wxString(file.fn_str()));
+    ifstream store(wxString(file.fn_str()).mb_str());
     if (store.fail() || store.bad())
 #else
     wxFileInputStream store(wxString(file.fn_str()));
@@ -655,7 +664,8 @@ wxDocument *wxDocTemplate::CreateDocument(const wxString& path, long flags)
         return doc;
     else
     {
-        delete doc;
+        if (GetDocumentManager()->GetDocuments().Member(doc))
+            doc->DeleteAllViews();
         return (wxDocument *) NULL;
     }
 }
@@ -1096,15 +1106,16 @@ wxDocument *wxDocManager::CreateDocument(const wxString& path, long flags)
             newDoc->SetDocumentTemplate(temp);
             if (!newDoc->OnOpenDocument(path2))
             {
-                delete newDoc;
+                newDoc->DeleteAllViews();
+                // delete newDoc; // Implicitly deleted by DeleteAllViews
                 return (wxDocument *) NULL;
             }
             AddFileToHistory(path2);
         }
         return newDoc;
     }
-    else
-        return (wxDocument *) NULL;
+
+    return (wxDocument *) NULL;
 }
 
 wxView *wxDocManager::CreateView(wxDocument *doc, long flags)
@@ -1294,6 +1305,28 @@ wxDocTemplate *wxDocManager::FindTemplateForPath(const wxString& path)
     return theTemplate;
 }
 
+// Try to get a more suitable parent frame than the top window,
+// for selection dialogs. Otherwise you may get an unexpected
+// window being activated when a dialog is shown.
+static wxWindow* wxFindSuitableParent()
+{
+    wxWindow* parent = wxTheApp->GetTopWindow();
+
+    wxWindow* focusWindow = wxWindow::FindFocus();
+    if (focusWindow)
+    {
+        while (focusWindow &&
+                !focusWindow->IsKindOf(CLASSINFO(wxDialog)) &&
+                !focusWindow->IsKindOf(CLASSINFO(wxFrame)))
+
+            focusWindow = focusWindow->GetParent();
+
+        if (focusWindow)
+            parent = focusWindow;
+    }
+    return parent;
+}
+
 // Prompts user to open a file, using file specs in templates.
 // How to implement in wxWindows? Must extend the file selector
 // dialog or implement own; OR match the extension to the
@@ -1331,36 +1364,53 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
     wxString descrBuf = wxT("*.*");
 #endif
 
-    int FilterIndex = 0;
+    int FilterIndex = -1;
+
+    wxWindow* parent = wxFindSuitableParent();
+
     wxString pathTmp = wxFileSelectorEx(_("Select a file"),
                                         m_lastDirectory,
                                         wxT(""),
                                         &FilterIndex,
                                         descrBuf,
                                         0,
-                                        wxTheApp->GetTopWindow());
+                                        parent);
 
+    wxDocTemplate *theTemplate = (wxDocTemplate *)NULL;
     if (!pathTmp.IsEmpty())
     {
+        if (!wxFileExists(pathTmp))
+        {
+            wxString msgTitle;
+            if (!wxTheApp->GetAppName().IsEmpty())
+                msgTitle = wxTheApp->GetAppName();
+            else
+                msgTitle = wxString(_("File error"));
+            
+            (void)wxMessageBox(_("Sorry, could not open this file."), msgTitle, wxOK | wxICON_EXCLAMATION,
+                parent);
+
+            path = wxT("");
+            return (wxDocTemplate *) NULL;
+        }
         m_lastDirectory = wxPathOnly(pathTmp);
 
         path = pathTmp;
 
-        // This is dodgy in that we're selecting the template on the
-        // basis of the file extension, which may not be a standard
-        // one. We really want to know exactly which template was
-        // chosen by using a more advanced file selector.
-        wxDocTemplate *theTemplate = FindTemplateForPath(path);
-        if ( !theTemplate )
+        // first choose the template using the extension, if this fails (i.e.
+        // wxFileSelectorEx() didn't fill it), then use the path
+        if ( FilterIndex != -1 )
             theTemplate = templates[FilterIndex];
-
-        return theTemplate;
+        if ( !theTemplate )
+            theTemplate = FindTemplateForPath(path);
     }
     else
     {
         path = wxT("");
-        return (wxDocTemplate *) NULL;
     }
+
+    return theTemplate;
+
 #if 0
     // In all other windowing systems, until we have more advanced
     // file selectors, we must select the document type (template) first, and
@@ -1387,59 +1437,94 @@ wxDocTemplate *wxDocManager::SelectDocumentPath(wxDocTemplate **templates,
 wxDocTemplate *wxDocManager::SelectDocumentType(wxDocTemplate **templates,
                                                 int noTemplates)
 {
-    wxChar **strings = new wxChar *[noTemplates];
-    wxChar **data = new wxChar *[noTemplates];
+    wxArrayString strings;
+    wxDocTemplate **data = new wxDocTemplate *[noTemplates];
     int i;
     int n = 0;
     for (i = 0; i < noTemplates; i++)
     {
         if (templates[i]->IsVisible())
         {
-            strings[n] = (wxChar *)templates[i]->m_description.c_str();
-            data[n] = (wxChar *)templates[i];
+            strings.Add(templates[i]->m_description);
+            data[n] = templates[i];
             n ++;
         }
     }
-    if (n == 0)
-    {
-        delete[] strings;
-        delete[] data;
-        return (wxDocTemplate *) NULL;
-    }
-    else if (n == 1)
+
+    wxDocTemplate *theTemplate;
+
+    switch ( n )
     {
-        wxDocTemplate *temp = (wxDocTemplate *)data[0];
-        delete[] strings;
-        delete[] data;
-        return temp;
+        case 0:
+            // no visible templates, hence nothing to choose from
+            theTemplate = NULL;
+            break;
+
+        case 1:
+            // don't propose the user to choose if he heas no choice
+            theTemplate = data[0];
+            break;
+
+        default:
+            // propose the user to choose one of several
+            theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
+                          (
+                            _("Select a document template"),
+                            _("Templates"),
+                            strings,
+                            (void **)data,
+                            wxFindSuitableParent()
+                          );
     }
 
-    wxDocTemplate *theTemplate = (wxDocTemplate *)wxGetSingleChoiceData(_("Select a document template"), _("Templates"), n,
-            strings, (void **)data);
-    delete[] strings;
     delete[] data;
+
     return theTemplate;
 }
 
 wxDocTemplate *wxDocManager::SelectViewType(wxDocTemplate **templates,
-        int noTemplates)
+                                            int noTemplates)
 {
-    wxChar **strings = new wxChar *[noTemplates];
-    wxChar **data = new wxChar *[noTemplates];
+    wxArrayString strings;
+    wxDocTemplate **data = new wxDocTemplate *[noTemplates];
     int i;
     int n = 0;
     for (i = 0; i < noTemplates; i++)
     {
-        if (templates[i]->IsVisible() && (templates[i]->GetViewName() != wxT("")))
+        wxDocTemplate *templ = templates[i];
+        if ( templ->IsVisible() && !templ->GetViewName().empty() )
         {
-            strings[n] = (wxChar *)templates[i]->m_viewTypeName.c_str();
-            data[n] = (wxChar *)templates[i];
+            strings.Add(templ->m_viewTypeName);
+            data[n] = templ;
             n ++;
         }
     }
-    wxDocTemplate *theTemplate = (wxDocTemplate *)wxGetSingleChoiceData(_("Select a document view"), _("Views"), n,
-            strings, (void **)data);
-    delete[] strings;
+
+    wxDocTemplate *theTemplate;
+
+    // the same logic as above
+    switch ( n )
+    {
+        case 0:
+            theTemplate = (wxDocTemplate *)NULL;
+            break;
+
+        case 1:
+            theTemplate = data[0];
+            break;
+
+        default:
+            theTemplate = (wxDocTemplate *)wxGetSingleChoiceData
+                          (
+                            _("Select a document view"),
+                            _("Views"),
+                            strings,
+                            (void **)data,
+                            wxFindSuitableParent()
+                          );
+
+    }
+
     delete[] data;
     return theTemplate;
 }
@@ -1603,7 +1688,7 @@ void wxDocParentFrame::OnExit(wxCommandEvent& WXUNUSED(event))
 
 void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
 {
-    int n = event.GetSelection() - wxID_FILE1;  // the index in MRU list
+    int n = event.GetId() - wxID_FILE1;  // the index in MRU list
     wxString filename(m_docManager->GetHistoryFile(n));
     if ( !filename.IsEmpty() )
     {
@@ -1619,8 +1704,7 @@ void wxDocParentFrame::OnMRUFile(wxCommandEvent& event)
             // about it
             m_docManager->RemoveFileFromHistory(n);
 
-            wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\n"
-                         "It has been also removed from the MRU files list."),
+            wxLogError(_("The file '%s' doesn't exist and couldn't be opened.\nIt has been also removed from the MRU files list."),
                        filename.c_str());
         }
     }
@@ -1735,6 +1819,8 @@ wxCommandProcessor::wxCommandProcessor(int maxCommands)
     m_maxNoCommands = maxCommands;
     m_currentCommand = (wxNode *) NULL;
     m_commandEditMenu = (wxMenu *) NULL;
+    m_undoAccelerator = wxT("\tCtrl+Z");
+    m_redoAccelerator = wxT("\tCtrl+Y");
 }
 
 wxCommandProcessor::~wxCommandProcessor()
@@ -1870,9 +1956,9 @@ void wxCommandProcessor::SetMenuStrings()
             if (commandName == wxT("")) commandName = _("Unnamed command");
             bool canUndo = command->CanUndo();
             if (canUndo)
-                buf = wxString(_("&Undo ")) + commandName;
+                buf = wxString(_("&Undo ")) + commandName + m_undoAccelerator;
             else
-                buf = wxString(_("Can't &Undo ")) + commandName;
+                buf = wxString(_("Can't &Undo ")) + commandName + m_undoAccelerator;
 
             m_commandEditMenu->SetLabel(wxID_UNDO, buf);
             m_commandEditMenu->Enable(wxID_UNDO, canUndo);
@@ -1883,24 +1969,24 @@ void wxCommandProcessor::SetMenuStrings()
                 wxCommand *redoCommand = (wxCommand *)m_currentCommand->Next()->Data();
                 wxString redoCommandName(redoCommand->GetName());
                 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
-                buf = wxString(_("&Redo ")) + redoCommandName;
+                buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
                 m_commandEditMenu->SetLabel(wxID_REDO, buf);
                 m_commandEditMenu->Enable(wxID_REDO, TRUE);
             }
             else
             {
-                m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
+                m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo") + m_redoAccelerator);
                 m_commandEditMenu->Enable(wxID_REDO, FALSE);
             }
         }
         else
         {
-            m_commandEditMenu->SetLabel(wxID_UNDO, _("&Undo"));
+            m_commandEditMenu->SetLabel(wxID_UNDO, _("&Undo") + m_undoAccelerator);
             m_commandEditMenu->Enable(wxID_UNDO, FALSE);
 
             if (m_commands.Number() == 0)
             {
-                m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo"));
+                m_commandEditMenu->SetLabel(wxID_REDO, _("&Redo") + m_redoAccelerator);
                 m_commandEditMenu->Enable(wxID_REDO, FALSE);
             }
             else
@@ -1910,7 +1996,7 @@ void wxCommandProcessor::SetMenuStrings()
                 wxCommand *redoCommand = (wxCommand *)m_commands.First()->Data();
                 wxString redoCommandName(redoCommand->GetName());
                 if (redoCommandName == wxT("")) redoCommandName = _("Unnamed command");
-                buf = wxString(_("&Redo ")) + redoCommandName;
+                buf = wxString(_("&Redo ")) + redoCommandName + m_redoAccelerator;
                 m_commandEditMenu->SetLabel(wxID_REDO, buf);
                 m_commandEditMenu->Enable(wxID_REDO, TRUE);
             }
@@ -2165,7 +2251,7 @@ bool wxTransferFileToStream(const wxString& filename, ostream& stream)
     FILE *fd1;
     int ch;
 
-    if ((fd1 = fopen (filename.fn_str(), "rb")) == NULL)
+    if ((fd1 = wxFopen (filename.fn_str(), _T("rb"))) == NULL)
         return FALSE;
 
     while ((ch = getc (fd1)) != EOF)
@@ -2180,7 +2266,7 @@ bool wxTransferStreamToFile(istream& stream, const wxString& filename)
     FILE *fd1;
     int ch;
 
-    if ((fd1 = fopen (filename.fn_str(), "wb")) == NULL)
+    if ((fd1 = wxFopen (filename.fn_str(), _T("wb"))) == NULL)
     {
         return FALSE;
     }
@@ -2200,7 +2286,7 @@ bool wxTransferFileToStream(const wxString& filename, wxOutputStream& stream)
     FILE *fd1;
     int ch;
 
-    if ((fd1 = fopen (filename.fn_str(), "rb")) == NULL)
+    if ((fd1 = wxFopen (filename, wxT("rb"))) == NULL)
         return FALSE;
 
     while ((ch = getc (fd1)) != EOF)
@@ -2215,7 +2301,7 @@ bool wxTransferStreamToFile(wxInputStream& stream, const wxString& filename)
     FILE *fd1;
     char ch;
 
-    if ((fd1 = fopen (filename.fn_str(), "wb")) == NULL)
+    if ((fd1 = wxFopen (filename, wxT("wb"))) == NULL)
     {
         return FALSE;
     }