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"
37 using namespace lyx::support;
42 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
43 : ControlDialogBD(lv, d), bp_(0)
47 ControlDocument::~ControlDocument()
51 BufferParams & ControlDocument::params()
58 LyXTextClass ControlDocument::textClass()
60 return textclasslist[bp_->textclass];
64 void ControlDocument::apply()
66 if (!bufferIsAvailable())
71 // this must come first so that a language change
72 // is correctly noticed
77 buffer()->params = *bp_;
79 lv_.view()->redoCurrentBuffer();
81 buffer()->markDirty();
83 lv_.message(_("Document settings applied"));
87 void ControlDocument::setParams()
90 bp_.reset(new BufferParams());
92 /// Set the buffer parameters
93 *bp_ = buffer()->params;
97 void ControlDocument::setLanguage()
99 Language const * oldL = buffer()->params.language;
100 Language const * newL = bp_->language;
104 if (oldL->RightToLeft() == newL->RightToLeft()
105 && !lv_.buffer()->isMultiLingual())
106 lv_.buffer()->changeLanguage(oldL, newL);
108 lv_.buffer()->updateDocLang(newL);
113 void ControlDocument::classApply()
115 BufferParams & params = buffer()->params;
116 lyx::textclass_type const old_class = params.textclass;
117 lyx::textclass_type const new_class = bp_->textclass;
119 // exit if nothing changes or if unable to load the new class
120 if (new_class == old_class || !loadTextclass(new_class))
123 // successfully loaded
124 buffer()->params = *bp_;
126 lv_.message(_("Converting document to new document class..."));
129 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
130 lv_.buffer()->paragraphs,
132 parseErrors(*buffer(), el);
133 bufferview()->showErrorList(_("Class switch"));
137 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
139 bool const success = textclasslist[tc].load();
143 string s = bformat(_("The document could not be converted\n"
144 "into the document class %1$s."),
145 textclasslist[tc].name());
146 Alert::error(_("Could not change class"), s);
152 void ControlDocument::saveAsDefault()
154 // Can somebody justify this ? I think it should be removed - jbl
156 if (!Alert::askQuestion(_("Do you want to save the current settings"),
157 _("for the document layout as default?"),
158 _("(they will be valid for any new document)")))
162 lv_.buffer()->params.preamble = bp_->preamble;
164 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
166 Buffer defaults(fname);
167 defaults.params = params();
169 // add an empty paragraph. Is this enough?
171 par.layout(params().getLyXTextClass().defaultLayout());
172 defaults.paragraphs.push_back(par);
174 defaults.writeFile(defaults.fileName());