2 * \file ControlDocument.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "ControlDocument.h"
17 #include "buffer_funcs.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "errorlist.h"
22 #include "iterators.h"
24 #include "lyxtextclasslist.h"
25 #include "paragraph.h"
27 #include "frontends/Alert.h"
28 #include "frontends/LyXView.h"
30 #include "support/filetools.h"
31 #include "support/LAssert.h"
32 #include "support/path_defines.h"
34 using namespace lyx::support;
37 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
38 : ControlDialogBD(lv, d), bp_(0)
42 ControlDocument::~ControlDocument()
46 BufferParams & ControlDocument::params()
53 LyXTextClass ControlDocument::textClass()
55 return textclasslist[bp_->textclass];
59 void ControlDocument::apply()
61 if (!bufferIsAvailable())
66 // this must come first so that a language change
67 // is correctly noticed
72 buffer()->params = *bp_;
74 lv_.view()->redoCurrentBuffer();
76 buffer()->markDirty();
78 lv_.message(_("Document settings applied"));
80 // Open insets of selected branches, close deselected ones
81 // Currently only top-level insets in buffer handled (bug).
82 ParIterator pit = buffer()->par_iterator_begin();
83 ParIterator pend = buffer()->par_iterator_end();
84 for (; pit != pend; ++pit) {
85 pit->insetlist.insetsOpenCloseBranch(bufferview());
90 void ControlDocument::setParams()
93 bp_.reset(new BufferParams());
95 /// Set the buffer parameters
96 *bp_ = buffer()->params;
100 void ControlDocument::setLanguage()
102 Language const * oldL = buffer()->params.language;
103 Language const * newL = bp_->language;
107 if (oldL->RightToLeft() == newL->RightToLeft()
108 && !lv_.buffer()->isMultiLingual())
109 lv_.buffer()->changeLanguage(oldL, newL);
111 lv_.buffer()->updateDocLang(newL);
116 void ControlDocument::classApply()
118 BufferParams & params = buffer()->params;
119 lyx::textclass_type const old_class = params.textclass;
120 lyx::textclass_type const new_class = bp_->textclass;
122 // exit if nothing changes or if unable to load the new class
123 if (new_class == old_class || !loadTextclass(new_class))
126 // successfully loaded
127 buffer()->params = *bp_;
129 lv_.message(_("Converting document to new document class..."));
132 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
133 lv_.buffer()->paragraphs,
135 bufferErrors(*buffer(), el);
136 bufferview()->showErrorList(_("Class switch"));
140 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
142 bool const success = textclasslist[tc].load();
146 string s = bformat(_("The document could not be converted\n"
147 "into the document class %1$s."),
148 textclasslist[tc].name());
149 Alert::error(_("Could not change class"), s);
155 void ControlDocument::saveAsDefault()
157 // Can somebody justify this ? I think it should be removed - jbl
159 if (!Alert::askQuestion(_("Do you want to save the current settings"),
160 _("for the document layout as default?"),
161 _("(they will be valid for any new document)")))
165 lv_.buffer()->params.preamble = bp_->preamble;
167 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
169 Buffer defaults(fname);
170 defaults.params = params();
172 // add an empty paragraph. Is this enough?
174 par.layout(params().getLyXTextClass().defaultLayout());
175 defaults.paragraphs.push_back(par);
177 defaults.writeFile(defaults.fileName());