]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
small fix
[lyx.git] / src / frontends / controllers / ControlDocument.C
1 /**
2  * \file ControlDocument.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlDocument.h"
14 #include "ViewBase.h"
15
16 #include "buffer.h"
17 #include "buffer_funcs.h"
18 #include "bufferparams.h"
19 #include "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "errorlist.h"
22 #include "gettext.h"
23 #include "iterators.h"
24 #include "language.h"
25 #include "lyxtextclasslist.h"
26 #include "paragraph.h"
27 #include "funcrequest.h"
28 #include "lfuns.h"
29 #include "LColor.h"
30
31 #include "frontends/Alert.h"
32 #include "frontends/LyXView.h"
33
34 #include "support/filetools.h"
35 #include "support/path_defines.h"
36
37 using lyx::support::AddName;
38 using lyx::support::AddPath;
39 using lyx::support::bformat;
40 using lyx::support::user_lyxdir;
41
42 using std::string;
43
44
45 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
46         : ControlDialogBD(lv, d), bp_(0)
47 {}
48
49
50 ControlDocument::~ControlDocument()
51 {}
52
53
54 BufferParams & ControlDocument::params()
55 {
56         BOOST_ASSERT(bp_.get());
57         return *bp_;
58 }
59
60
61 LyXTextClass ControlDocument::textClass()
62 {
63         return textclasslist[bp_->textclass];
64 }
65
66
67 void ControlDocument::apply()
68 {
69         if (!bufferIsAvailable())
70                 return;
71
72         view().apply();
73
74         // this must come first so that a language change
75         // is correctly noticed
76         setLanguage();
77
78         classApply();
79
80         buffer()->params() = *bp_;
81
82         lv_.view()->redoCurrentBuffer();
83
84         buffer()->markDirty();
85
86         lv_.message(_("Document settings applied"));
87
88         // Open insets of selected branches, close deselected ones
89         // Currently only top-level insets in buffer handled (bug).
90         ParIterator pit = buffer()->par_iterator_begin();
91         ParIterator pend = buffer()->par_iterator_end();
92         for (; pit != pend; ++pit) {
93                 pit->insetlist.insetsOpenCloseBranch(bufferview());
94         }
95 }
96
97
98 void ControlDocument::setParams()
99 {
100         if (!bp_.get())
101                 bp_.reset(new BufferParams);
102
103         /// Set the buffer parameters
104         *bp_ = buffer()->params();
105 }
106
107
108 void ControlDocument::setLanguage()
109 {
110         Language const * oldL = buffer()->params().language;
111         Language const * newL = bp_->language;
112
113         if (oldL != newL) {
114
115                 if (oldL->RightToLeft() == newL->RightToLeft()
116                     && !lv_.buffer()->isMultiLingual())
117                         lv_.buffer()->changeLanguage(oldL, newL);
118                 else
119                     lv_.buffer()->updateDocLang(newL);
120         }
121 }
122
123
124 void ControlDocument::setBranchColor(string const & branch, string const & hex)
125 {
126         lcolor.setColor(branch, hex);
127         string const s = branch + ' ' + hex;
128         lv_.dispatch(FuncRequest(LFUN_SET_COLOR, s));
129 }
130
131
132 void ControlDocument::classApply()
133 {
134         BufferParams & params = buffer()->params();
135         lyx::textclass_type const old_class = params.textclass;
136         lyx::textclass_type const new_class = bp_->textclass;
137
138         // exit if nothing changes or if unable to load the new class
139         if (new_class == old_class || !loadTextclass(new_class))
140                 return;
141
142         // successfully loaded
143         buffer()->params() = *bp_;
144
145         lv_.message(_("Converting document to new document class..."));
146
147         ErrorList el;
148         CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
149                                                  lv_.buffer()->paragraphs(),
150                                                  el);
151         bufferErrors(*buffer(), el);
152         bufferview()->showErrorList(_("Class switch"));
153 }
154
155
156 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
157 {
158         bool const success = textclasslist[tc].load();
159         if (success)
160                 return success;
161
162         string s = bformat(_("The document could not be converted\n"
163                         "into the document class %1$s."),
164                         textclasslist[tc].name());
165         Alert::error(_("Could not change class"), s);
166
167         return success;
168 }
169
170
171 void ControlDocument::saveAsDefault()
172 {
173 // Can somebody justify this ? I think it should be removed - jbl
174 #if 0
175         if (!Alert::askQuestion(_("Do you want to save the current settings"),
176                                 _("for the document layout as default?"),
177                                 _("(they will be valid for any new document)")))
178                 return;
179 #endif
180
181         lv_.buffer()->params().preamble = bp_->preamble;
182
183         string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
184                                      "defaults.lyx");
185         Buffer defaults(fname);
186         defaults.params() = params();
187
188         // add an empty paragraph. Is this enough?
189         Paragraph par;
190         par.layout(params().getLyXTextClass().defaultLayout());
191         defaults.paragraphs().push_back(par);
192
193         defaults.writeFile(defaults.fileName());
194
195 }