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())
68 // this must come first so that a language change
69 // is correctly noticed
74 buffer()->params = *bp_;
76 lv_.view()->redoCurrentBuffer();
78 buffer()->markDirty();
80 lv_.message(_("Document settings applied"));
84 void ControlDocument::setParams()
87 bp_.reset(new BufferParams());
89 /// Set the buffer parameters
90 *bp_ = buffer()->params;
94 void ControlDocument::setLanguage()
96 Language const * oldL = buffer()->params.language;
97 Language const * newL = bp_->language;
101 if (oldL->RightToLeft() == newL->RightToLeft()
102 && !lv_.buffer()->isMultiLingual())
103 lv_.buffer()->changeLanguage(oldL, newL);
105 lv_.buffer()->updateDocLang(newL);
110 void ControlDocument::classApply()
112 BufferParams & params = buffer()->params;
113 lyx::textclass_type const old_class = params.textclass;
114 lyx::textclass_type const new_class = bp_->textclass;
116 // exit if nothing changes or if unable to load the new class
117 if (new_class == old_class || !loadTextclass(new_class))
120 // successfully loaded
121 buffer()->params = *bp_;
123 lv_.message(_("Converting document to new document class..."));
126 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
127 lv_.buffer()->paragraphs,
130 bufferview()->setErrorList(el);
131 bufferview()->showErrorList(_("Class switch"));
135 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
137 bool const success = textclasslist[tc].load();
141 string s = bformat(_("The document could not be converted\n"
142 "into the document class %1$s."),
143 textclasslist[tc].name());
144 Alert::error(_("Could not change class"), s);
150 void ControlDocument::saveAsDefault()
152 // Can somebody justify this ? I think it should be removed - jbl
154 if (!Alert::askQuestion(_("Do you want to save the current settings"),
155 _("for the document layout as default?"),
156 _("(they will be valid for any new document)")))
160 lv_.buffer()->params.preamble = bp_->preamble;
162 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
164 Buffer defaults(fname);
165 defaults.params = params();
167 // add an empty paragraph. Is this enough?
169 par.layout(params().getLyXTextClass().defaultLayout());
170 defaults.paragraphs.push_back(par);
172 defaults.writeFile(defaults.fileName());