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"
24 #include "lyxtextclass.h"
25 #include "lyxtextclasslist.h"
26 #include "CutAndPaste.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/Alert.h"
31 #include "support/LAssert.h"
32 #include "support/lstrings.h"
33 #include "support/filetools.h"
38 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
39 : ControlDialogBD(lv, d), bp_(0)
43 ControlDocument::~ControlDocument()
47 BufferParams & ControlDocument::params()
49 lyx::Assert(bp_.get());
54 LyXTextClass ControlDocument::textClass()
56 return textclasslist[bp_->textclass];
60 void ControlDocument::apply()
62 if (!bufferIsAvailable())
69 buffer()->params = *bp_;
71 lv_.view()->redoCurrentBuffer();
73 buffer()->markDirty();
75 lv_.message(_("Document settings applied"));
79 void ControlDocument::setParams()
82 bp_.reset(new BufferParams());
84 /// Set the buffer parameters
85 *bp_ = buffer()->params;
89 void ControlDocument::setLanguage()
91 Language const * oldL = buffer()->params.language;
92 Language const * newL = bp_->language;
96 if (oldL->RightToLeft() == newL->RightToLeft()
97 && !lv_.buffer()->isMultiLingual())
98 lv_.buffer()->changeLanguage(oldL, newL);
100 lv_.buffer()->updateDocLang(newL);
105 void ControlDocument::classApply()
107 BufferParams & params = buffer()->params;
108 lyx::textclass_type const old_class = params.textclass;
109 lyx::textclass_type const new_class = bp_->textclass;
111 // exit if nothing changes or if unable to load the new class
112 if (new_class == old_class || !loadTextclass(new_class))
115 // successfully loaded
117 buffer()->params = *bp_;
119 lv_.message(_("Converting document to new document class..."));
120 int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
121 old_class, new_class,
122 lv_.buffer()->paragraphs);
129 s = bformat(_("One paragraph could not be converted\n"
130 "into the document class %1$s."),
131 textclasslist[new_class].name());
133 s = bformat(_("%1$s paragraphs could not be converted\n"
134 "into the document class %2$s."),
135 textclasslist[new_class].name());
137 Alert::warning(_("Class conversion errors"), s);
141 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
143 bool const success = textclasslist[tc].load();
147 string s = bformat(_("The document could not be converted\n"
148 "into the document class %1$s."),
149 textclasslist[tc].name());
150 Alert::error(_("Could not change class"), s);
156 void ControlDocument::saveAsDefault()
158 // Can somebody justify this ? I think it should be removed - jbl
160 if (!Alert::askQuestion(_("Do you want to save the current settings"),
161 _("for the document layout as default?"),
162 _("(they will be valid for any new document)")))
166 lv_.buffer()->params.preamble = bp_->preamble;
168 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
170 Buffer defaults(fname);
171 defaults.params = params();
173 // add an empty paragraph. Is this enough?
174 Paragraph * par = new Paragraph;
175 par->layout(params().getLyXTextClass().defaultLayout());
176 defaults.paragraphs.push_back(par);
178 defaults.writeFile(defaults.fileName());