3 * \file ControlDocument.C
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS
14 #include "BufferView.h"
15 #include "ControlDocument.h"
22 #include "buffer_funcs.h"
23 #include "errorlist.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"));
88 void ControlDocument::setParams()
91 bp_.reset(new BufferParams());
93 /// Set the buffer parameters
94 *bp_ = buffer()->params;
98 void ControlDocument::setLanguage()
100 Language const * oldL = buffer()->params.language;
101 Language const * newL = bp_->language;
105 if (oldL->RightToLeft() == newL->RightToLeft()
106 && !lv_.buffer()->isMultiLingual())
107 lv_.buffer()->changeLanguage(oldL, newL);
109 lv_.buffer()->updateDocLang(newL);
114 void ControlDocument::classApply()
116 BufferParams & params = buffer()->params;
117 lyx::textclass_type const old_class = params.textclass;
118 lyx::textclass_type const new_class = bp_->textclass;
120 // exit if nothing changes or if unable to load the new class
121 if (new_class == old_class || !loadTextclass(new_class))
124 // successfully loaded
125 buffer()->params = *bp_;
127 lv_.message(_("Converting document to new document class..."));
130 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
131 lv_.buffer()->paragraphs,
133 bufferErrors(*buffer(), el);
134 bufferview()->showErrorList(_("Class switch"));
138 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
140 bool const success = textclasslist[tc].load();
144 string s = bformat(_("The document could not be converted\n"
145 "into the document class %1$s."),
146 textclasslist[tc].name());
147 Alert::error(_("Could not change class"), s);
153 void ControlDocument::saveAsDefault()
155 // Can somebody justify this ? I think it should be removed - jbl
157 if (!Alert::askQuestion(_("Do you want to save the current settings"),
158 _("for the document layout as default?"),
159 _("(they will be valid for any new document)")))
163 lv_.buffer()->params.preamble = bp_->preamble;
165 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
167 Buffer defaults(fname);
168 defaults.params = params();
170 // add an empty paragraph. Is this enough?
172 par.layout(params().getLyXTextClass().defaultLayout());
173 defaults.paragraphs.push_back(par);
175 defaults.writeFile(defaults.fileName());