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())
71 buffer()->params = *bp_;
73 lv_.view()->redoCurrentBuffer();
75 buffer()->markDirty();
77 lv_.message(_("Document settings applied"));
81 void ControlDocument::setParams()
84 bp_.reset(new BufferParams());
86 /// Set the buffer parameters
87 *bp_ = buffer()->params;
91 void ControlDocument::setLanguage()
93 Language const * oldL = buffer()->params.language;
94 Language const * newL = bp_->language;
97 && oldL->RightToLeft() == newL->RightToLeft()
98 && !lv_.buffer()->isMultiLingual())
99 lv_.buffer()->changeLanguage(oldL, newL);
103 void ControlDocument::classApply()
105 BufferParams & params = buffer()->params;
106 lyx::textclass_type const old_class = params.textclass;
107 lyx::textclass_type const new_class = bp_->textclass;
109 // exit if nothing changes or if unable to load the new class
110 if (new_class == old_class || !loadTextclass(new_class))
113 // successfully loaded
115 buffer()->params = *bp_;
116 lv_.message(_("Converting document to new document class..."));
117 int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
118 old_class, new_class,
119 &*(lv_.buffer()->paragraphs.begin()),
120 lv_.buffer()->params);
124 s = _("One paragraph couldn't be converted");
127 s += _(" paragraphs couldn't be converted");
129 Alert::alert(_("Conversion Errors!"),s,
130 _("into chosen document class"));
135 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
137 bool const success = textclasslist[tc].load();
139 // problem changing class
140 // -- warn user (to retain old style)
141 Alert::alert(_("Conversion Errors!"),
142 _("Errors loading new document class."),
143 _("Reverting to original document class."));
149 void ControlDocument::saveAsDefault()
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)")))
156 lv_.buffer()->params.preamble = bp_->preamble;
158 string const fname = AddName(AddPath(user_lyxdir, "templates/"),
160 Buffer defaults(fname);
161 defaults.params = params();
163 // add an empty paragraph. Is this enough?
164 Paragraph * par = new Paragraph;
165 par->layout(params().getLyXTextClass().defaultLayout());
166 defaults.paragraphs.set(par);
168 defaults.writeFile(defaults.fileName());