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 "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "errorlist.h"
22 #include "iterators.h"
24 #include "lyxtextclasslist.h"
25 #include "paragraph.h"
27 #include "frontends/Alert.h"
28 #include "frontends/LyXView.h"
30 #include "support/filetools.h"
31 #include "support/LAssert.h"
32 #include "support/path_defines.h"
34 using namespace lyx::support;
39 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
40 : ControlDialogBD(lv, d), bp_(0)
44 ControlDocument::~ControlDocument()
48 BufferParams & ControlDocument::params()
55 LyXTextClass ControlDocument::textClass()
57 return textclasslist[bp_->textclass];
61 void ControlDocument::apply()
63 if (!bufferIsAvailable())
68 // this must come first so that a language change
69 // is correctly noticed
74 buffer()->params = *bp_;
76 lv_.view()->redoCurrentBuffer();
78 buffer()->markDirty();
80 lv_.message(_("Document settings applied"));
82 // Open insets of selected branches, close deselected ones
83 // Currently only top-level insets in buffer handled (bug).
84 ParIterator pit = buffer()->par_iterator_begin();
85 ParIterator pend = buffer()->par_iterator_end();
86 for (; pit != pend; ++pit) {
87 pit->insetlist.insetsOpenCloseBranch(bufferview());
92 void ControlDocument::setParams()
95 bp_.reset(new BufferParams());
97 /// Set the buffer parameters
98 *bp_ = buffer()->params;
102 void ControlDocument::setLanguage()
104 Language const * oldL = buffer()->params.language;
105 Language const * newL = bp_->language;
109 if (oldL->RightToLeft() == newL->RightToLeft()
110 && !lv_.buffer()->isMultiLingual())
111 lv_.buffer()->changeLanguage(oldL, newL);
113 lv_.buffer()->updateDocLang(newL);
118 void ControlDocument::classApply()
120 BufferParams & params = buffer()->params;
121 lyx::textclass_type const old_class = params.textclass;
122 lyx::textclass_type const new_class = bp_->textclass;
124 // exit if nothing changes or if unable to load the new class
125 if (new_class == old_class || !loadTextclass(new_class))
128 // successfully loaded
129 buffer()->params = *bp_;
131 lv_.message(_("Converting document to new document class..."));
134 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
135 lv_.buffer()->paragraphs,
137 bufferErrors(*buffer(), el);
138 bufferview()->showErrorList(_("Class switch"));
142 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
144 bool const success = textclasslist[tc].load();
148 string s = bformat(_("The document could not be converted\n"
149 "into the document class %1$s."),
150 textclasslist[tc].name());
151 Alert::error(_("Could not change class"), s);
157 void ControlDocument::saveAsDefault()
159 // Can somebody justify this ? I think it should be removed - jbl
161 if (!Alert::askQuestion(_("Do you want to save the current settings"),
162 _("for the document layout as default?"),
163 _("(they will be valid for any new document)")))
167 lv_.buffer()->params.preamble = bp_->preamble;
169 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
171 Buffer defaults(fname);
172 defaults.params = params();
174 // add an empty paragraph. Is this enough?
176 par.layout(params().getLyXTextClass().defaultLayout());
177 defaults.paragraphs.push_back(par);
179 defaults.writeFile(defaults.fileName());