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"
16 #include "BranchList.h"
18 #include "buffer_funcs.h"
19 #include "bufferparams.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "errorlist.h"
24 #include "iterators.h"
27 #include "lyxtextclasslist.h"
28 #include "paragraph.h"
29 #include "funcrequest.h"
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
38 using lyx::support::AddName;
39 using lyx::support::AddPath;
40 using lyx::support::bformat;
41 using lyx::support::user_lyxdir;
46 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
47 : ControlDialogBD(lv, d), bp_(0)
51 ControlDocument::~ControlDocument()
55 BufferParams & ControlDocument::params()
57 BOOST_ASSERT(bp_.get());
62 LyXTextClass ControlDocument::textClass()
64 return textclasslist[bp_->textclass];
68 void ControlDocument::apply()
70 if (!bufferIsAvailable())
75 // this must come first so that a language change
76 // is correctly noticed
81 buffer()->params() = *bp_;
83 lv_.view()->redoCurrentBuffer();
85 buffer()->markDirty();
87 lv_.message(_("Document settings applied"));
90 BranchList & branchlist = params().branchlist();
91 BranchList::const_iterator it = branchlist.begin();
92 BranchList::const_iterator const end = branchlist.end();
93 for (; it != end; ++it) {
94 string const & current_branch = it->getBranch();
95 Branch const * branch = branchlist.find(current_branch);
96 string x11hexname = branch->getColor();
97 // check that we have a valid color!
98 if (x11hexname.empty() || x11hexname[0] != '#')
99 x11hexname = lcolor.getX11Name(LColor::background);
100 // display the new color
101 string const str = current_branch + ' ' + x11hexname;
102 lv_.dispatch(FuncRequest(LFUN_SET_COLOR, str));
105 // Open insets of selected branches, close deselected ones
106 // Currently only top-level insets in buffer handled (bug).
107 ParIterator pit = buffer()->par_iterator_begin();
108 ParIterator pend = buffer()->par_iterator_end();
109 for (; pit != pend; ++pit) {
110 pit->insetlist.insetsOpenCloseBranch(*buffer());
115 void ControlDocument::setParams()
118 bp_.reset(new BufferParams);
120 /// Set the buffer parameters
121 *bp_ = buffer()->params();
125 void ControlDocument::setLanguage()
127 Language const * oldL = buffer()->params().language;
128 Language const * newL = bp_->language;
132 if (oldL->RightToLeft() == newL->RightToLeft()
133 && !lv_.buffer()->isMultiLingual())
134 lv_.buffer()->changeLanguage(oldL, newL);
136 lv_.buffer()->updateDocLang(newL);
141 void ControlDocument::classApply()
143 BufferParams & params = buffer()->params();
144 lyx::textclass_type const old_class = params.textclass;
145 lyx::textclass_type const new_class = bp_->textclass;
147 // exit if nothing changes or if unable to load the new class
148 if (new_class == old_class || !loadTextclass(new_class))
151 // successfully loaded
152 buffer()->params() = *bp_;
154 lv_.message(_("Converting document to new document class..."));
157 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
158 lv_.buffer()->paragraphs(),
160 bufferErrors(*buffer(), el);
161 bufferview()->showErrorList(_("Class switch"));
165 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
167 bool const success = textclasslist[tc].load();
171 string s = bformat(_("The document could not be converted\n"
172 "into the document class %1$s."),
173 textclasslist[tc].name());
174 Alert::error(_("Could not change class"), s);
180 void ControlDocument::saveAsDefault()
182 // Can somebody justify this ? I think it should be removed - jbl
184 if (!Alert::askQuestion(_("Do you want to save the current settings"),
185 _("for the document layout as default?"),
186 _("(they will be valid for any new document)")))
190 lv_.buffer()->params().preamble = bp_->preamble;
192 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
194 Buffer defaults(fname);
195 defaults.params() = params();
197 // add an empty paragraph. Is this enough?
199 par.layout(params().getLyXTextClass().defaultLayout());
200 defaults.paragraphs().push_back(par);
202 defaults.writeFile(defaults.fileName());