]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlDocument.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlDocument.C
index b3c1c5d994d0a845825ecd4ab9ee579e97e4fe37..593b664643cc2b7d258a6069692ce6a7ed919b6d 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /**
  * \file ControlDocument.C
  * This file is part of LyX, the document processor.
  *
  * \author Edwin Leuven
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include "BufferView.h"
 #include "ControlDocument.h"
-#include "ViewBase.h"
-
-#include "gettext.h"
-#include "lyxfind.h"
+#include "Kernel.h"
 
+#include "BranchList.h"
 #include "buffer.h"
+#include "bufferparams.h"
+#include "BufferView.h"
+#include "buffer_funcs.h"
+#include "funcrequest.h"
 #include "language.h"
-#include "lyx_main.h"
-#include "lyxtextclass.h"
+#include "LColor.h"
 #include "lyxtextclasslist.h"
-#include "CutAndPaste.h"
 
-#include "frontends/LyXView.h"
-#include "frontends/Alert.h"
+#include <sstream>
 
-#include "support/lstrings.h"
-#include "support/filetools.h"
+using std::ostringstream;
+using std::string;
 
+namespace lyx {
+namespace frontend {
+
+ControlDocument::ControlDocument(Dialog & parent)
+       : Dialog::Controller(parent)
+{}
 
-ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
-       : ControlDialogBD(lv, d), bp_(0)
-{
-}
 
 ControlDocument::~ControlDocument()
 {}
 
 
-BufferParams & ControlDocument::params()
+bool ControlDocument::initialiseParams(std::string const &)
 {
-       lyx::Assert(bp_.get());
-       return *bp_;
+       bp_.reset(new BufferParams);
+       *bp_ = kernel().buffer().params();
+       return true;
 }
 
 
-LyXTextClass ControlDocument::textClass()
+void ControlDocument::clearParams()
 {
-       return textclasslist[bp_->textclass];
+       bp_.reset();
 }
 
 
-void ControlDocument::apply()
+BufferParams & ControlDocument::params() const
 {
-       if (!bufferIsAvailable())
-               return;
-
-       setLanguage();
-
-       classApply();
-
-       view().apply();
-       buffer()->params = *bp_;
-       
-       lv_.view()->redoCurrentBuffer();
-
-       buffer()->markDirty();
-
-       lv_.message(_("Document settings applied"));
+       BOOST_ASSERT(bp_.get());
+       return *bp_;
 }
 
 
-void ControlDocument::setParams()
+LyXTextClass const & ControlDocument::textClass() const
 {
-       if (!bp_.get())
-               bp_.reset(new BufferParams());
-
-       /// Set the buffer parameters
-       *bp_ = buffer()->params;
+       return textclasslist[bp_->textclass];
 }
 
 
-void ControlDocument::setLanguage()
-{
-       Language const * oldL = buffer()->params.language;
-       Language const * newL = bp_->language;
+namespace {
 
-       if (oldL != newL
-           && oldL->RightToLeft() == newL->RightToLeft()
-           && !lv_.buffer()->isMultiLingual())
-               lv_.buffer()->changeLanguage(oldL, newL);
+void dispatch_bufferparams(Kernel const & kernel, BufferParams const & bp,
+                          kb_action lfun)
+{
+       ostringstream ss;
+       ss << "\\begin_header\n";
+       bp.writeFile(ss);
+       ss << "\\end_header\n";
+       kernel.dispatch(FuncRequest(lfun, ss.str()));
 }
 
+} // namespace anon
+
 
-void ControlDocument::classApply()
+void ControlDocument::dispatchParams()
 {
-       BufferParams & params = buffer()->params;
-       lyx::textclass_type const old_class = params.textclass;
-       lyx::textclass_type const new_class = bp_->textclass;
+       // This must come first so that a language change is correctly noticed
+       setLanguage();
 
-       // exit if nothing changes or if unable to load the new class
-       if (new_class == old_class || !loadTextclass(new_class))
-               return;
+       // Set the document class.
+       textclass_type const old_class =
+               kernel().buffer().params().textclass;
+       textclass_type const new_class = bp_->textclass;
+       if (new_class != old_class) {
+               string const name = textclasslist[new_class].name();
+               kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
+       }
 
-       // 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");
+       int const old_secnumdepth = kernel().buffer().params().secnumdepth;
+       int const new_secnumdepth = bp_->secnumdepth;
+
+       // Apply the BufferParams.
+       dispatch_bufferparams(kernel(), params(), LFUN_BUFFERPARAMS_APPLY);
+
+       // redo the numbering if necessary
+       if (new_secnumdepth != old_secnumdepth)
+               updateCounters(kernel().buffer());
+
+       // Generate the colours requested by each new branch.
+       BranchList & branchlist = params().branchlist();
+       if (!branchlist.empty()) {
+               BranchList::const_iterator it = branchlist.begin();
+               BranchList::const_iterator const end = branchlist.end();
+               for (; it != end; ++it) {
+                       string const & current_branch = it->getBranch();
+                       Branch const * branch = branchlist.find(current_branch);
+                       string x11hexname = branch->getColor();
+                       // check that we have a valid color!
+                       if (x11hexname.empty() || x11hexname[0] != '#')
+                               x11hexname = 
+                                       lcolor.getX11Name(LColor::background);
+                       // display the new color
+                       string const str = current_branch  + ' ' + x11hexname;
+                       kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
                }
-               Alert::alert(_("Conversion Errors!"),s,
-                            _("into chosen document class"));
+       
+               // Open insets of selected branches, close deselected ones
+               kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, 
+                       "assign branch"));
        }
+       // update the bufferview
+       kernel().bufferview()->update();
 }
 
 
-bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
+void ControlDocument::setLanguage() const
 {
-       bool const success = textclasslist[tc].load();
-       if (!success) {
-               // problem changing class
-               // -- warn user (to retain old style)
-               Alert::alert(_("Conversion Errors!"),
-                            _("Errors loading new document class."),
-                            _("Reverting to original document class."));
-       }
-       return success;
+       Language const * const newL = bp_->language;
+       if (kernel().buffer().params().language == newL)
+               return;
+
+       string const lang_name = newL->lang();
+       kernel().dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
 }
 
 
-void ControlDocument::saveAsDefault()
+bool ControlDocument::loadTextclass(textclass_type tc) const
 {
-       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;
-               
-       lv_.buffer()->params.preamble = bp_->preamble;
-
-       string const fname = AddName(AddPath(user_lyxdir, "templates/"),
-                                    "defaults.lyx");
-       Buffer defaults(fname);
-       defaults.params = params();
+       string const name = textclasslist[tc].name();
+       kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
 
-       // add an empty paragraph. Is this enough?
-       Paragraph * par = new Paragraph;
-       par->layout(params().getLyXTextClass().defaultLayout());
-       defaults.paragraphs.set(par);
+       // Report back whether we were able to change the class.
+       bool const success = textclasslist[tc].loaded();
+       return success;
+}
 
-       defaults.writeFile(defaults.fileName());
 
+void ControlDocument::saveAsDefault() const
+{
+       dispatch_bufferparams(kernel(), params(), LFUN_SAVE_AS_DEFAULT);
 }
+
+} // namespace frontend
+} // namespace lyx