]> git.lyx.org Git - features.git/blobdiff - src/frontends/controllers/ControlDocument.C
make ParagraphList::push_back take a reference instead of a pointer.
[features.git] / src / frontends / controllers / ControlDocument.C
index 8af1b90e8d6afd83f508904a4f1564da35262ca8..58b6877adf0e15d97617e63721ce37c039da633f 100644 (file)
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "BufferView.h"
 #include "ControlDocument.h"
 #include "ViewBase.h"
@@ -23,6 +19,7 @@
 #include "lyxfind.h"
 
 #include "buffer.h"
+#include "errorlist.h"
 #include "language.h"
 #include "lyx_main.h"
 #include "lyxtextclass.h"
 #include "frontends/LyXView.h"
 #include "frontends/Alert.h"
 
+#include "support/LAssert.h"
 #include "support/lstrings.h"
 #include "support/filetools.h"
 
+using std::endl;
+
 
 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
        : ControlDialogBD(lv, d), bp_(0)
-{
-}
-
-ControlDocument::~ControlDocument()
 {}
 
 
-void ControlDocument::showPreamble()
-{
-}
+ControlDocument::~ControlDocument()
+{}
 
 
 BufferParams & ControlDocument::params()
@@ -68,14 +63,12 @@ void ControlDocument::apply()
        if (!bufferIsAvailable())
                return;
 
-       setLanguage();
-
-       // FIXME: do we need to use return value from classApply() here? (Lgb)
        classApply();
 
        view().apply();
+       setLanguage();
        buffer()->params = *bp_;
-       
+
        lv_.view()->redoCurrentBuffer();
 
        buffer()->markDirty();
@@ -99,64 +92,68 @@ void ControlDocument::setLanguage()
        Language const * oldL = buffer()->params.language;
        Language const * newL = bp_->language;
 
-       if (oldL != newL
-           && oldL->RightToLeft() == newL->RightToLeft()
-           && !lv_.buffer()->isMultiLingual())
-               lv_.buffer()->changeLanguage(oldL, newL);
+       if (oldL != newL) {
+
+               if (oldL->RightToLeft() == newL->RightToLeft()
+                   && !lv_.buffer()->isMultiLingual())
+                       lv_.buffer()->changeLanguage(oldL, newL);
+               else
+                   lv_.buffer()->updateDocLang(newL);
+       }
 }
 
 
-bool ControlDocument::classApply()
+void ControlDocument::classApply()
 {
        BufferParams & params = buffer()->params;
-       unsigned int const old_class = params.textclass;
-       unsigned int const new_class = bp_->textclass;
+       lyx::textclass_type const old_class = params.textclass;
+       lyx::textclass_type const new_class = bp_->textclass;
 
-       // exit if nothing changes
-       if (new_class == old_class) {
-                return true;
-       }
-
-       // try to load new_class
-       if (!textclasslist[new_class].load()) {
-               // problem changing class
-               // -- warn user (to retain old style)
-               Alert::alert(_("Conversion Errors!"),
-                            _("Errors loading new document class."),
-                            _("Reverting to original document class."));
-               return false;
-       }
+       // exit if nothing changes or if unable to load the new class
+       if (new_class == old_class || !loadTextclass(new_class))
+               return;
 
        // successfully loaded
        view().apply();
        buffer()->params = *bp_;
+
        lv_.message(_("Converting document to new document class..."));
-       int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
-               old_class, new_class,
-               &*(lv_.buffer()->paragraphs.begin()),
-               lv_.buffer()->params);
-       if (ret) {
-               string s;
-               if (ret == 1) {
-                       s = _("One paragraph couldn't be converted");
-               } else {
-                       s += tostr(ret);
-                       s += _(" paragraphs couldn't be converted");
-               }
-               Alert::alert(_("Conversion Errors!"),s,
-                            _("into chosen document class"));
-       }
-       return true;
+
+       ErrorList el;
+       CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
+                                                lv_.buffer()->paragraphs,
+                                                el);
+
+       bufferview()->setErrorList(el);
+       bufferview()->showErrorList(_("Class switch"));
+}
+
+
+bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
+{
+       bool const success = textclasslist[tc].load();
+       if (success)
+               return success;
+
+       string s = bformat(_("The document could not be converted\n"
+                       "into the document class %1$s."),
+                       textclasslist[tc].name());
+       Alert::error(_("Could not change class"), s);
+
+       return success;
 }
 
 
 void ControlDocument::saveAsDefault()
 {
+// Can somebody justify this ? I think it should be removed - jbl
+#if 0
        if (!Alert::askQuestion(_("Do you want to save the current settings"),
                                _("for the document layout as default?"),
                                _("(they will be valid for any new document)")))
                return;
-               
+#endif
+
        lv_.buffer()->params.preamble = bp_->preamble;
 
        string const fname = AddName(AddPath(user_lyxdir, "templates/"),
@@ -165,9 +162,9 @@ void ControlDocument::saveAsDefault()
        defaults.params = params();
 
        // add an empty paragraph. Is this enough?
-       Paragraph * par = new Paragraph;
-       par->layout(params().getLyXTextClass().defaultLayout());
-       defaults.paragraphs.set(par);
+       Paragraph par;
+       par.layout(params().getLyXTextClass().defaultLayout());
+       defaults.paragraphs.push_back(par);
 
        defaults.writeFile(defaults.fileName());