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"
23 #include "funcrequest.h"
27 #include "lyxtextclasslist.h"
29 #include "paragraph.h"
30 #include "ParagraphList_fwd.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 lv_.dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "toggle branch"));
110 void ControlDocument::setParams()
113 bp_.reset(new BufferParams);
115 /// Set the buffer parameters
116 *bp_ = buffer()->params();
120 void ControlDocument::setLanguage()
122 Language const * oldL = buffer()->params().language;
123 Language const * newL = bp_->language;
127 if (oldL->RightToLeft() == newL->RightToLeft()
128 && !lv_.buffer()->isMultiLingual())
129 lv_.buffer()->changeLanguage(oldL, newL);
131 lv_.buffer()->updateDocLang(newL);
136 void ControlDocument::classApply()
138 BufferParams & params = buffer()->params();
139 lyx::textclass_type const old_class = params.textclass;
140 lyx::textclass_type const new_class = bp_->textclass;
142 // exit if nothing changes or if unable to load the new class
143 if (new_class == old_class || !loadTextclass(new_class))
146 // successfully loaded
147 buffer()->params() = *bp_;
149 lv_.message(_("Converting document to new document class..."));
152 lyx::cap::SwitchLayoutsBetweenClasses(old_class, new_class,
153 lv_.buffer()->paragraphs(),
155 bufferErrors(*buffer(), el);
156 bufferview()->showErrorList(_("Class switch"));
160 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
162 bool const success = textclasslist[tc].load();
166 string s = bformat(_("The document could not be converted\n"
167 "into the document class %1$s."),
168 textclasslist[tc].name());
169 Alert::error(_("Could not change class"), s);
175 void ControlDocument::saveAsDefault()
177 // Can somebody justify this ? I think it should be removed - jbl
179 if (!Alert::askQuestion(_("Do you want to save the current settings"),
180 _("for the document layout as default?"),
181 _("(they will be valid for any new document)")))
185 lv_.buffer()->params().preamble = bp_->preamble;
187 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
189 Buffer defaults(fname);
190 defaults.params() = params();
192 // add an empty paragraph. Is this enough?
194 par.layout(params().getLyXTextClass().defaultLayout());
195 defaults.paragraphs().push_back(par);
197 defaults.writeFile(defaults.fileName());