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"
28 #include "frontends/Alert.h"
29 #include "frontends/LyXView.h"
31 #include "support/filetools.h"
32 #include "support/path_defines.h"
34 using lyx::support::AddName;
35 using lyx::support::AddPath;
36 using lyx::support::bformat;
37 using lyx::support::user_lyxdir;
40 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
41 : ControlDialogBD(lv, d), bp_(0)
45 ControlDocument::~ControlDocument()
49 BufferParams & ControlDocument::params()
51 BOOST_ASSERT(bp_.get());
56 LyXTextClass ControlDocument::textClass()
58 return textclasslist[bp_->textclass];
62 void ControlDocument::apply()
64 if (!bufferIsAvailable())
69 // this must come first so that a language change
70 // is correctly noticed
75 buffer()->params() = *bp_;
77 lv_.view()->redoCurrentBuffer();
79 buffer()->markDirty();
81 lv_.message(_("Document settings applied"));
83 // Open insets of selected branches, close deselected ones
84 // Currently only top-level insets in buffer handled (bug).
85 ParIterator pit = buffer()->par_iterator_begin();
86 ParIterator pend = buffer()->par_iterator_end();
87 for (; pit != pend; ++pit) {
88 pit->insetlist.insetsOpenCloseBranch(bufferview());
93 void ControlDocument::setParams()
96 bp_.reset(new BufferParams);
98 /// Set the buffer parameters
99 *bp_ = buffer()->params();
103 void ControlDocument::setLanguage()
105 Language const * oldL = buffer()->params().language;
106 Language const * newL = bp_->language;
110 if (oldL->RightToLeft() == newL->RightToLeft()
111 && !lv_.buffer()->isMultiLingual())
112 lv_.buffer()->changeLanguage(oldL, newL);
114 lv_.buffer()->updateDocLang(newL);
119 void ControlDocument::classApply()
121 BufferParams & params = buffer()->params();
122 lyx::textclass_type const old_class = params.textclass;
123 lyx::textclass_type const new_class = bp_->textclass;
125 // exit if nothing changes or if unable to load the new class
126 if (new_class == old_class || !loadTextclass(new_class))
129 // successfully loaded
130 buffer()->params() = *bp_;
132 lv_.message(_("Converting document to new document class..."));
135 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
136 lv_.buffer()->paragraphs(),
138 bufferErrors(*buffer(), el);
139 bufferview()->showErrorList(_("Class switch"));
143 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
145 bool const success = textclasslist[tc].load();
149 string s = bformat(_("The document could not be converted\n"
150 "into the document class %1$s."),
151 textclasslist[tc].name());
152 Alert::error(_("Could not change class"), s);
158 void ControlDocument::saveAsDefault()
160 // Can somebody justify this ? I think it should be removed - jbl
162 if (!Alert::askQuestion(_("Do you want to save the current settings"),
163 _("for the document layout as default?"),
164 _("(they will be valid for any new document)")))
168 lv_.buffer()->params().preamble = bp_->preamble;
170 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
172 Buffer defaults(fname);
173 defaults.params() = params();
175 // add an empty paragraph. Is this enough?
177 par.layout(params().getLyXTextClass().defaultLayout());
178 defaults.paragraphs().push_back(par);
180 defaults.writeFile(defaults.fileName());