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 "ControlDocument.h"
17 #include "buffer_funcs.h"
18 #include "bufferparams.h"
19 #include "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "errorlist.h"
23 #include "iterators.h"
25 #include "lyxtextclasslist.h"
26 #include "paragraph.h"
27 #include "funcrequest.h"
31 #include "frontends/Alert.h"
32 #include "frontends/LyXView.h"
34 #include "support/filetools.h"
35 #include "support/path_defines.h"
37 using lyx::support::AddName;
38 using lyx::support::AddPath;
39 using lyx::support::bformat;
40 using lyx::support::user_lyxdir;
45 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
46 : ControlDialogBD(lv, d), bp_(0)
50 ControlDocument::~ControlDocument()
54 BufferParams & ControlDocument::params()
56 BOOST_ASSERT(bp_.get());
61 LyXTextClass ControlDocument::textClass()
63 return textclasslist[bp_->textclass];
67 void ControlDocument::apply()
69 if (!bufferIsAvailable())
74 // this must come first so that a language change
75 // is correctly noticed
80 buffer()->params() = *bp_;
82 lv_.view()->redoCurrentBuffer();
84 buffer()->markDirty();
86 lv_.message(_("Document settings applied"));
88 // Open insets of selected branches, close deselected ones
89 // Currently only top-level insets in buffer handled (bug).
90 ParIterator pit = buffer()->par_iterator_begin();
91 ParIterator pend = buffer()->par_iterator_end();
92 for (; pit != pend; ++pit) {
93 pit->insetlist.insetsOpenCloseBranch(*buffer());
98 void ControlDocument::setParams()
101 bp_.reset(new BufferParams);
103 /// Set the buffer parameters
104 *bp_ = buffer()->params();
108 void ControlDocument::setLanguage()
110 Language const * oldL = buffer()->params().language;
111 Language const * newL = bp_->language;
115 if (oldL->RightToLeft() == newL->RightToLeft()
116 && !lv_.buffer()->isMultiLingual())
117 lv_.buffer()->changeLanguage(oldL, newL);
119 lv_.buffer()->updateDocLang(newL);
124 void ControlDocument::setBranchColor(string const & branch, string const & hex)
126 lcolor.setColor(branch, hex);
127 string const s = branch + ' ' + hex;
128 lv_.dispatch(FuncRequest(LFUN_SET_COLOR, s));
132 void ControlDocument::classApply()
134 BufferParams & params = buffer()->params();
135 lyx::textclass_type const old_class = params.textclass;
136 lyx::textclass_type const new_class = bp_->textclass;
138 // exit if nothing changes or if unable to load the new class
139 if (new_class == old_class || !loadTextclass(new_class))
142 // successfully loaded
143 buffer()->params() = *bp_;
145 lv_.message(_("Converting document to new document class..."));
148 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
149 lv_.buffer()->paragraphs(),
151 bufferErrors(*buffer(), el);
152 bufferview()->showErrorList(_("Class switch"));
156 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
158 bool const success = textclasslist[tc].load();
162 string s = bformat(_("The document could not be converted\n"
163 "into the document class %1$s."),
164 textclasslist[tc].name());
165 Alert::error(_("Could not change class"), s);
171 void ControlDocument::saveAsDefault()
173 // Can somebody justify this ? I think it should be removed - jbl
175 if (!Alert::askQuestion(_("Do you want to save the current settings"),
176 _("for the document layout as default?"),
177 _("(they will be valid for any new document)")))
181 lv_.buffer()->params().preamble = bp_->preamble;
183 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
185 Buffer defaults(fname);
186 defaults.params() = params();
188 // add an empty paragraph. Is this enough?
190 par.layout(params().getLyXTextClass().defaultLayout());
191 defaults.paragraphs().push_back(par);
193 defaults.writeFile(defaults.fileName());