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;
99 && oldL->RightToLeft() == newL->RightToLeft()
100 && !lv_.buffer()->isMultiLingual())
101 lv_.buffer()->changeLanguage(oldL, 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.begin()),
123 lv_.buffer()->params);
131 boost::format fmt(_("One paragraph could not be converted\n"
132 "into the document class %2$s."));
133 fmt % textclasslist[new_class].name();
136 boost::format fmt(_("%1$s paragraphs could not be converted\n"
137 "into the document class %2$s."));
139 fmt % textclasslist[new_class].name();
144 s += _("One paragraph could not be converted\n"
145 "into the document class ");
146 s += textclasslist[new_class].name() + ".";
149 s += _(" paragraphs could not be converted\n"
150 "into the document class ");
151 s += textclasslist[new_class].name() + ".";
154 Alert::warning(_("Class conversion errors"), s);
158 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
160 bool const success = textclasslist[tc].load();
167 boost::format fmt(_("The document could not be converted\n"
168 "into the document class %1$s."));
169 fmt % textclasslist[tc].name();
172 s += _("The document could not be converted\n"
173 "into the document class ");
174 s += textclasslist[tc].name() + ".";
176 Alert::error(_("Could not change class"), s);
182 void ControlDocument::saveAsDefault()
184 // Can somebody justify this ? I think it should be removed - jbl
186 if (!Alert::askQuestion(_("Do you want to save the current settings"),
187 _("for the document layout as default?"),
188 _("(they will be valid for any new document)")))
192 lv_.buffer()->params.preamble = bp_->preamble;
194 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
196 Buffer defaults(fname);
197 defaults.params = params();
199 // add an empty paragraph. Is this enough?
200 Paragraph * par = new Paragraph;
201 par->layout(params().getLyXTextClass().defaultLayout());
202 defaults.paragraphs.set(par);
204 defaults.writeFile(defaults.fileName());