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 "BufferView.h"
14 #include "ControlDocument.h"
21 #include "buffer_funcs.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"
35 #include "support/path_defines.h"
37 using namespace lyx::support;
42 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
43 : ControlDialogBD(lv, d), bp_(0)
47 ControlDocument::~ControlDocument()
51 BufferParams & ControlDocument::params()
58 LyXTextClass ControlDocument::textClass()
60 return textclasslist[bp_->textclass];
64 void ControlDocument::apply()
66 if (!bufferIsAvailable())
71 // this must come first so that a language change
72 // is correctly noticed
77 buffer()->params = *bp_;
79 lv_.view()->redoCurrentBuffer();
81 buffer()->markDirty();
83 lv_.message(_("Document settings applied"));
85 // Open insets of selected branches, close deselected ones
86 // Currently only top-level insets in buffer handled (bug).
87 ParIterator pit = buffer()->par_iterator_begin();
88 ParIterator pend = buffer()->par_iterator_end();
89 for (; pit != pend; ++pit) {
90 pit->insetlist.insetsOpenCloseBranch(bufferview());
95 void ControlDocument::setParams()
98 bp_.reset(new BufferParams());
100 /// Set the buffer parameters
101 *bp_ = buffer()->params;
105 void ControlDocument::setLanguage()
107 Language const * oldL = buffer()->params.language;
108 Language const * newL = bp_->language;
112 if (oldL->RightToLeft() == newL->RightToLeft()
113 && !lv_.buffer()->isMultiLingual())
114 lv_.buffer()->changeLanguage(oldL, newL);
116 lv_.buffer()->updateDocLang(newL);
121 void ControlDocument::classApply()
123 BufferParams & params = buffer()->params;
124 lyx::textclass_type const old_class = params.textclass;
125 lyx::textclass_type const new_class = bp_->textclass;
127 // exit if nothing changes or if unable to load the new class
128 if (new_class == old_class || !loadTextclass(new_class))
131 // successfully loaded
132 buffer()->params = *bp_;
134 lv_.message(_("Converting document to new document class..."));
137 CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
138 lv_.buffer()->paragraphs,
140 bufferErrors(*buffer(), el);
141 bufferview()->showErrorList(_("Class switch"));
145 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
147 bool const success = textclasslist[tc].load();
151 string s = bformat(_("The document could not be converted\n"
152 "into the document class %1$s."),
153 textclasslist[tc].name());
154 Alert::error(_("Could not change class"), s);
160 void ControlDocument::saveAsDefault()
162 // Can somebody justify this ? I think it should be removed - jbl
164 if (!Alert::askQuestion(_("Do you want to save the current settings"),
165 _("for the document layout as default?"),
166 _("(they will be valid for any new document)")))
170 lv_.buffer()->params.preamble = bp_->preamble;
172 string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
174 Buffer defaults(fname);
175 defaults.params = params();
177 // add an empty paragraph. Is this enough?
179 par.layout(params().getLyXTextClass().defaultLayout());
180 defaults.paragraphs.push_back(par);
182 defaults.writeFile(defaults.fileName());