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"
21 #include "buffer_funcs.h"
22 #include "errorlist.h"
23 #include "iterators.h"
26 #include "lyxtextclass.h"
27 #include "lyxtextclasslist.h"
28 #include "CutAndPaste.h"
30 #include "frontends/LyXView.h"
31 #include "frontends/Alert.h"
33 #include "support/LAssert.h"
34 #include "support/lstrings.h"
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
38 using namespace lyx::support;
43 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
44 : ControlDialogBD(lv, d), bp_(0)
48 ControlDocument::~ControlDocument()
52 BufferParams & ControlDocument::params()
59 LyXTextClass ControlDocument::textClass()
61 return textclasslist[bp_->textclass];
65 void ControlDocument::apply()
67 if (!bufferIsAvailable())
72 // this must come first so that a language change
73 // is correctly noticed
78 buffer()->params = *bp_;
80 lv_.view()->redoCurrentBuffer();
82 buffer()->markDirty();
84 lv_.message(_("Document settings applied"));
86 // Open insets of selected branches, close deselected ones
87 // Currently only top-level insets in buffer handled (bug).
88 ParIterator pit = buffer()->par_iterator_begin();
89 ParIterator pend = buffer()->par_iterator_end();
90 for (; pit != pend; ++pit) {
91 pit->insetlist.insetsOpenCloseBranch(bufferview());
96 void ControlDocument::setParams()
99 bp_.reset(new BufferParams());
101 /// Set the buffer parameters
102 *bp_ = buffer()->params;
106 void ControlDocument::setLanguage()
108 Language const * oldL = buffer()->params.language;
109 Language const * newL = bp_->language;
113 if (oldL->RightToLeft() == newL->RightToLeft()
114 && !lv_.buffer()->isMultiLingual())
115 lv_.buffer()->changeLanguage(oldL, newL);
117 lv_.buffer()->updateDocLang(newL);
122 void ControlDocument::classApply()
124 BufferParams & params = buffer()->params;
125 lyx::textclass_type const old_class = params.textclass;
126 lyx::textclass_type const new_class = bp_->textclass;
128 // exit if nothing changes or if unable to load the new class
129 if (new_class == old_class || !loadTextclass(new_class))
132 // successfully loaded
133 buffer()->params = *bp_;
135 lv_.message(_("Converting document to new document class..."));
138 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
139 lv_.buffer()->paragraphs,
141 bufferErrors(*buffer(), el);
142 bufferview()->showErrorList(_("Class switch"));
146 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
148 bool const success = textclasslist[tc].load();
152 string s = bformat(_("The document could not be converted\n"
153 "into the document class %1$s."),
154 textclasslist[tc].name());
155 Alert::error(_("Could not change class"), s);
161 void ControlDocument::saveAsDefault()
163 // Can somebody justify this ? I think it should be removed - jbl
165 if (!Alert::askQuestion(_("Do you want to save the current settings"),
166 _("for the document layout as default?"),
167 _("(they will be valid for any new document)")))
171 lv_.buffer()->params.preamble = bp_->preamble;
173 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
175 Buffer defaults(fname);
176 defaults.params = params();
178 // add an empty paragraph. Is this enough?
180 par.layout(params().getLyXTextClass().defaultLayout());
181 defaults.paragraphs.push_back(par);
183 defaults.writeFile(defaults.fileName());