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/lstrings.h"
33 #include "support/filetools.h"
35 #include "BoostFormat.h"
40 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
41 : ControlDialogBD(lv, d), bp_(0)
45 ControlDocument::~ControlDocument()
49 BufferParams & ControlDocument::params()
51 lyx::Assert(bp_.get());
56 LyXTextClass ControlDocument::textClass()
58 return textclasslist[bp_->textclass];
62 void ControlDocument::apply()
64 if (!bufferIsAvailable())
72 buffer()->params = *bp_;
74 lv_.view()->redoCurrentBuffer();
76 buffer()->markDirty();
78 lv_.message(_("Document settings applied"));
82 void ControlDocument::setParams()
85 bp_.reset(new BufferParams());
87 /// Set the buffer parameters
88 *bp_ = buffer()->params;
92 void ControlDocument::setLanguage()
94 Language const * oldL = buffer()->params.language;
95 Language const * newL = bp_->language;
98 && oldL->RightToLeft() == newL->RightToLeft()
99 && !lv_.buffer()->isMultiLingual())
100 lv_.buffer()->changeLanguage(oldL, newL);
104 void ControlDocument::classApply()
106 BufferParams & params = buffer()->params;
107 lyx::textclass_type const old_class = params.textclass;
108 lyx::textclass_type const new_class = bp_->textclass;
110 // exit if nothing changes or if unable to load the new class
111 if (new_class == old_class || !loadTextclass(new_class))
114 // successfully loaded
116 buffer()->params = *bp_;
118 lv_.message(_("Converting document to new document class..."));
119 int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
120 old_class, new_class,
121 &*(lv_.buffer()->paragraphs.begin()),
122 lv_.buffer()->params);
126 s = _("One paragraph couldn't be converted");
129 boost::format fmt(_("%1$s paragraphs couldn't be converted"));
134 s += _(" paragraphs couldn't be converted");
137 Alert::alert(_("Conversion Errors!"),s,
138 _("into chosen document class"));
143 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
145 bool const success = textclasslist[tc].load();
147 // problem changing class
148 // -- warn user (to retain old style)
149 Alert::alert(_("Conversion Errors!"),
150 _("Errors loading new document class."),
151 _("Reverting to original document class."));
157 void ControlDocument::saveAsDefault()
159 if (!Alert::askQuestion(_("Do you want to save the current settings"),
160 _("for the document layout as default?"),
161 _("(they will be valid for any new document)")))
164 lv_.buffer()->params.preamble = bp_->preamble;
166 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
168 Buffer defaults(fname);
169 defaults.params = params();
171 // add an empty paragraph. Is this enough?
172 Paragraph * par = new Paragraph;
173 par->layout(params().getLyXTextClass().defaultLayout());
174 defaults.paragraphs.set(par);
176 defaults.writeFile(defaults.fileName());