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
14 #include "BufferView.h"
15 #include "ControlDocument.h"
22 #include "errorlist.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"
39 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
40 : ControlDialogBD(lv, d), bp_(0)
44 ControlDocument::~ControlDocument()
48 BufferParams & ControlDocument::params()
50 lyx::Assert(bp_.get());
55 LyXTextClass ControlDocument::textClass()
57 return textclasslist[bp_->textclass];
61 void ControlDocument::apply()
63 if (!bufferIsAvailable())
70 buffer()->params = *bp_;
72 lv_.view()->redoCurrentBuffer();
74 buffer()->markDirty();
76 lv_.message(_("Document settings applied"));
80 void ControlDocument::setParams()
83 bp_.reset(new BufferParams());
85 /// Set the buffer parameters
86 *bp_ = buffer()->params;
90 void ControlDocument::setLanguage()
92 Language const * oldL = buffer()->params.language;
93 Language const * newL = bp_->language;
97 if (oldL->RightToLeft() == newL->RightToLeft()
98 && !lv_.buffer()->isMultiLingual())
99 lv_.buffer()->changeLanguage(oldL, newL);
101 lv_.buffer()->updateDocLang(newL);
106 void ControlDocument::classApply()
108 BufferParams & params = buffer()->params;
109 lyx::textclass_type const old_class = params.textclass;
110 lyx::textclass_type const new_class = bp_->textclass;
112 // exit if nothing changes or if unable to load the new class
113 if (new_class == old_class || !loadTextclass(new_class))
116 // successfully loaded
118 buffer()->params = *bp_;
120 lv_.message(_("Converting document to new document class..."));
123 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
124 lv_.buffer()->paragraphs,
127 bufferview()->setErrorList(el);
128 bufferview()->showErrorList(_("Class switch"));
132 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
134 bool const success = textclasslist[tc].load();
138 string s = bformat(_("The document could not be converted\n"
139 "into the document class %1$s."),
140 textclasslist[tc].name());
141 Alert::error(_("Could not change class"), s);
147 void ControlDocument::saveAsDefault()
149 // Can somebody justify this ? I think it should be removed - jbl
151 if (!Alert::askQuestion(_("Do you want to save the current settings"),
152 _("for the document layout as default?"),
153 _("(they will be valid for any new document)")))
157 lv_.buffer()->params.preamble = bp_->preamble;
159 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
161 Buffer defaults(fname);
162 defaults.params = params();
164 // add an empty paragraph. Is this enough?
165 Paragraph * par = new Paragraph;
166 par->layout(params().getLyXTextClass().defaultLayout());
167 defaults.paragraphs.push_back(par);
169 defaults.writeFile(defaults.fileName());