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 #pragma implementation
18 #include "BufferView.h"
19 #include "ControlDocument.h"
28 #include "lyxtextclass.h"
29 #include "lyxtextclasslist.h"
30 #include "CutAndPaste.h"
32 #include "frontends/LyXView.h"
33 #include "frontends/Alert.h"
35 #include "support/lstrings.h"
36 #include "support/filetools.h"
38 #include "BoostFormat.h"
43 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
44 : ControlDialogBD(lv, d), bp_(0)
48 ControlDocument::~ControlDocument()
52 BufferParams & ControlDocument::params()
54 lyx::Assert(bp_.get());
59 LyXTextClass ControlDocument::textClass()
61 return textclasslist[bp_->textclass];
65 void ControlDocument::apply()
67 if (!bufferIsAvailable())
75 buffer()->params = *bp_;
77 lv_.view()->redoCurrentBuffer();
79 buffer()->markDirty();
81 lv_.message(_("Document settings applied"));
85 void ControlDocument::setParams()
88 bp_.reset(new BufferParams());
90 /// Set the buffer parameters
91 *bp_ = buffer()->params;
95 void ControlDocument::setLanguage()
97 Language const * oldL = buffer()->params.language;
98 Language const * newL = bp_->language;
101 && oldL->RightToLeft() == newL->RightToLeft()
102 && !lv_.buffer()->isMultiLingual())
103 lv_.buffer()->changeLanguage(oldL, newL);
107 void ControlDocument::classApply()
109 BufferParams & params = buffer()->params;
110 lyx::textclass_type const old_class = params.textclass;
111 lyx::textclass_type const new_class = bp_->textclass;
113 // exit if nothing changes or if unable to load the new class
114 if (new_class == old_class || !loadTextclass(new_class))
117 // successfully loaded
119 buffer()->params = *bp_;
121 lv_.message(_("Converting document to new document class..."));
122 int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
123 old_class, new_class,
124 &*(lv_.buffer()->paragraphs.begin()),
125 lv_.buffer()->params);
129 s = _("One paragraph couldn't be converted");
132 boost::format fmt(_("%1$s paragraphs couldn't be converted"));
137 s += _(" paragraphs couldn't be converted");
140 Alert::alert(_("Conversion Errors!"),s,
141 _("into chosen document class"));
146 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
148 bool const success = textclasslist[tc].load();
150 // problem changing class
151 // -- warn user (to retain old style)
152 Alert::alert(_("Conversion Errors!"),
153 _("Errors loading new document class."),
154 _("Reverting to original document class."));
160 void ControlDocument::saveAsDefault()
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)")))
167 lv_.buffer()->params.preamble = bp_->preamble;
169 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
171 Buffer defaults(fname);
172 defaults.params = params();
174 // add an empty paragraph. Is this enough?
175 Paragraph * par = new Paragraph;
176 par->layout(params().getLyXTextClass().defaultLayout());
177 defaults.paragraphs.set(par);
179 defaults.writeFile(defaults.fileName());