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 "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);
127 s = _("One paragraph couldn't be converted");
130 boost::format fmt(_("%1$s paragraphs couldn't be converted"));
135 s += _(" paragraphs couldn't be converted");
138 Alert::alert(_("Conversion Errors!"),s,
139 _("into chosen document class"));
144 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
146 bool const success = textclasslist[tc].load();
148 // problem changing class
149 // -- warn user (to retain old style)
150 Alert::alert(_("Conversion Errors!"),
151 _("Errors loading new document class."),
152 _("Reverting to original document class."));
158 void ControlDocument::saveAsDefault()
160 // Can somebody justify this ? I think it should be removed - jbl
162 if (!Alert::askQuestion(_("Do you want to save the current settings"),
163 _("for the document layout as default?"),
164 _("(they will be valid for any new document)")))
168 lv_.buffer()->params.preamble = bp_->preamble;
170 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
172 Buffer defaults(fname);
173 defaults.params = params();
175 // add an empty paragraph. Is this enough?
176 Paragraph * par = new Paragraph;
177 par->layout(params().getLyXTextClass().defaultLayout());
178 defaults.paragraphs.set(par);
180 defaults.writeFile(defaults.fileName());