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
15 #include "BufferView.h"
16 #include "ControlDocument.h"
25 #include "lyxtextclass.h"
26 #include "lyxtextclasslist.h"
27 #include "CutAndPaste.h"
29 #include "frontends/LyXView.h"
30 #include "frontends/Alert.h"
32 #include "support/LAssert.h"
33 #include "support/lstrings.h"
34 #include "support/filetools.h"
36 #include "support/BoostFormat.h"
41 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
42 : ControlDialogBD(lv, d), bp_(0)
46 ControlDocument::~ControlDocument()
50 BufferParams & ControlDocument::params()
52 lyx::Assert(bp_.get());
57 LyXTextClass ControlDocument::textClass()
59 return textclasslist[bp_->textclass];
63 void ControlDocument::apply()
65 if (!bufferIsAvailable())
73 buffer()->params = *bp_;
75 lv_.view()->redoCurrentBuffer();
77 buffer()->markDirty();
79 lv_.message(_("Document settings applied"));
83 void ControlDocument::setParams()
86 bp_.reset(new BufferParams());
88 /// Set the buffer parameters
89 *bp_ = buffer()->params;
93 void ControlDocument::setLanguage()
95 Language const * oldL = buffer()->params.language;
96 Language const * newL = bp_->language;
100 if (oldL->RightToLeft() == newL->RightToLeft()
101 && !lv_.buffer()->isMultiLingual())
102 lv_.buffer()->changeLanguage(oldL, newL);
104 lv_.buffer()->updateDocLang(newL);
109 void ControlDocument::classApply()
111 BufferParams & params = buffer()->params;
112 lyx::textclass_type const old_class = params.textclass;
113 lyx::textclass_type const new_class = bp_->textclass;
115 // exit if nothing changes or if unable to load the new class
116 if (new_class == old_class || !loadTextclass(new_class))
119 // successfully loaded
121 buffer()->params = *bp_;
123 lv_.message(_("Converting document to new document class..."));
124 int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
125 old_class, new_class,
126 &*(lv_.buffer()->paragraphs.begin()),
127 lv_.buffer()->params);
135 boost::format fmt(_("One paragraph could not be converted\n"
136 "into the document class %2$s."));
137 fmt % textclasslist[new_class].name();
140 boost::format fmt(_("%1$s paragraphs could not be converted\n"
141 "into the document class %2$s."));
143 fmt % textclasslist[new_class].name();
148 s += _("One paragraph could not be converted\n"
149 "into the document class ");
150 s += textclasslist[new_class].name() + ".";
153 s += _(" paragraphs could not be converted\n"
154 "into the document class ");
155 s += textclasslist[new_class].name() + ".";
158 Alert::warning(_("Class conversion errors"), s);
162 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
164 bool const success = textclasslist[tc].load();
171 boost::format fmt(_("The document could not be converted\n"
172 "into the document class %1$s."));
173 fmt % textclasslist[tc].name();
176 s += _("The document could not be converted\n"
177 "into the document class ");
178 s += textclasslist[tc].name() + ".";
180 Alert::error(_("Could not change class"), s);
186 void ControlDocument::saveAsDefault()
188 // Can somebody justify this ? I think it should be removed - jbl
190 if (!Alert::askQuestion(_("Do you want to save the current settings"),
191 _("for the document layout as default?"),
192 _("(they will be valid for any new document)")))
196 lv_.buffer()->params.preamble = bp_->preamble;
198 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
200 Buffer defaults(fname);
201 defaults.params = params();
203 // add an empty paragraph. Is this enough?
204 Paragraph * par = new Paragraph;
205 par->layout(params().getLyXTextClass().defaultLayout());
206 defaults.paragraphs.set(par);
208 defaults.writeFile(defaults.fileName());