2 * \file ControlDocument.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "BufferView.h"
14 #include "ControlDocument.h"
20 #include "buffer_funcs.h"
21 #include "iterators.h"
23 #include "lyxtextclasslist.h"
24 #include "CutAndPaste.h"
26 #include "frontends/LyXView.h"
27 #include "frontends/Alert.h"
29 #include "support/LAssert.h"
30 #include "support/filetools.h"
31 #include "support/path_defines.h"
33 using namespace lyx::support;
38 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
39 : ControlDialogBD(lv, d), bp_(0)
43 ControlDocument::~ControlDocument()
47 BufferParams & ControlDocument::params()
54 LyXTextClass ControlDocument::textClass()
56 return textclasslist[bp_->textclass];
60 void ControlDocument::apply()
62 if (!bufferIsAvailable())
67 // this must come first so that a language change
68 // is correctly noticed
73 buffer()->params = *bp_;
75 lv_.view()->redoCurrentBuffer();
77 buffer()->markDirty();
79 lv_.message(_("Document settings applied"));
81 // Open insets of selected branches, close deselected ones
82 // Currently only top-level insets in buffer handled (bug).
83 ParIterator pit = buffer()->par_iterator_begin();
84 ParIterator pend = buffer()->par_iterator_end();
85 for (; pit != pend; ++pit) {
86 pit->insetlist.insetsOpenCloseBranch(bufferview());
91 void ControlDocument::setParams()
94 bp_.reset(new BufferParams());
96 /// Set the buffer parameters
97 *bp_ = buffer()->params;
101 void ControlDocument::setLanguage()
103 Language const * oldL = buffer()->params.language;
104 Language const * newL = bp_->language;
108 if (oldL->RightToLeft() == newL->RightToLeft()
109 && !lv_.buffer()->isMultiLingual())
110 lv_.buffer()->changeLanguage(oldL, newL);
112 lv_.buffer()->updateDocLang(newL);
117 void ControlDocument::classApply()
119 BufferParams & params = buffer()->params;
120 lyx::textclass_type const old_class = params.textclass;
121 lyx::textclass_type const new_class = bp_->textclass;
123 // exit if nothing changes or if unable to load the new class
124 if (new_class == old_class || !loadTextclass(new_class))
127 // successfully loaded
128 buffer()->params = *bp_;
130 lv_.message(_("Converting document to new document class..."));
133 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
134 lv_.buffer()->paragraphs,
136 bufferErrors(*buffer(), el);
137 bufferview()->showErrorList(_("Class switch"));
141 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
143 bool const success = textclasslist[tc].load();
147 string s = bformat(_("The document could not be converted\n"
148 "into the document class %1$s."),
149 textclasslist[tc].name());
150 Alert::error(_("Could not change class"), s);
156 void ControlDocument::saveAsDefault()
158 // Can somebody justify this ? I think it should be removed - jbl
160 if (!Alert::askQuestion(_("Do you want to save the current settings"),
161 _("for the document layout as default?"),
162 _("(they will be valid for any new document)")))
166 lv_.buffer()->params.preamble = bp_->preamble;
168 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
170 Buffer defaults(fname);
171 defaults.params = params();
173 // add an empty paragraph. Is this enough?
175 par.layout(params().getLyXTextClass().defaultLayout());
176 defaults.paragraphs.push_back(par);
178 defaults.writeFile(defaults.fileName());