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"
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 // FIXME: do we need to use return value from classApply() here? (Lgb)
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 bool ControlDocument::classApply()
106 BufferParams & params = buffer()->params;
107 unsigned int const old_class = params.textclass;
108 unsigned int const new_class = bp_->textclass;
110 // exit if nothing changes
111 if (new_class == old_class) {
115 // try to load new_class
116 if (!textclasslist[new_class].load()) {
117 // problem changing class
118 // -- warn user (to retain old style)
119 Alert::alert(_("Conversion Errors!"),
120 _("Errors loading new document class."),
121 _("Reverting to original document class."));
125 // successfully loaded
127 buffer()->params = *bp_;
128 lv_.message(_("Converting document to new document class..."));
129 int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
130 old_class, new_class,
131 &*(lv_.buffer()->paragraphs.begin()),
132 lv_.buffer()->params);
136 s = _("One paragraph couldn't be converted");
139 s += _(" paragraphs couldn't be converted");
141 Alert::alert(_("Conversion Errors!"),s,
142 _("into chosen document class"));
148 void ControlDocument::saveAsDefault()
150 if (!Alert::askQuestion(_("Do you want to save the current settings"),
151 _("for the document layout as default?"),
152 _("(they will be valid for any new document)")))
155 lv_.buffer()->params.preamble = bp_->preamble;
157 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
159 Buffer defaults(fname);
160 defaults.params = params();
162 // add an empty paragraph. Is this enough?
163 Paragraph * par = new Paragraph;
164 par->layout(params().getLyXTextClass().defaultLayout());
165 defaults.paragraphs.set(par);
167 defaults.writeFile(defaults.fileName());