]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
* Lazy MathData to avoid unneeded interpretation of macro definitions
[lyx.git] / src / insets / InsetBranch.cpp
1 /**
2  * \file InsetBranch.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetBranch.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BranchList.h"
18 #include "Color.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "support/gettext.h"
25 #include "Lexer.h"
26 #include "OutputParams.h"
27 #include "TextClass.h"
28
29 #include <sstream>
30
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetBranch::InsetBranch(BufferParams const & bp,
37                          InsetBranchParams const & params)
38         : InsetCollapsable(bp), params_(params)
39 {}
40
41
42 InsetBranch::InsetBranch(InsetBranch const & in)
43         : InsetCollapsable(in), params_(in.params_)
44 {}
45
46
47 InsetBranch::~InsetBranch()
48 {
49         InsetBranchMailer(*this).hideDialog();
50 }
51
52
53 Inset * InsetBranch::clone() const
54 {
55         return new InsetBranch(*this);
56 }
57
58
59 docstring const InsetBranch::editMessage() const
60 {
61         return _("Opened Branch Inset");
62 }
63
64
65 void InsetBranch::write(Buffer const & buf, ostream & os) const
66 {
67         params_.write(os);
68         InsetCollapsable::write(buf, os);
69 }
70
71
72 void InsetBranch::read(Buffer const & buf, Lexer & lex)
73 {
74         params_.read(lex);
75         InsetCollapsable::read(buf, lex);
76 }
77
78
79 void InsetBranch::setButtonLabel()
80 {
81         docstring s = _("Branch: ") + params_.branch;
82         if (!params_.branch.empty()) {
83                 // FIXME UNICODE
84                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
85                 if (c == Color_none) {
86                         s = _("Undef: ") + s;
87                 }
88         }
89         if (decoration() == Classic)
90                 setLabel(isOpen() ? s : getNewLabel(s) );
91         else
92                 setLabel(params_.branch + ": " + getNewLabel(s));
93 }
94
95
96 ColorCode InsetBranch::backgroundColor() const
97 {
98         if (!params_.branch.empty()) {
99                 // FIXME UNICODE
100                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
101                 if (c == Color_none) {
102                         c = Color_error;
103                 }
104                 return c;
105         } else
106                 return Inset::backgroundColor();
107 }
108
109
110 bool InsetBranch::showInsetDialog(BufferView * bv) const
111 {
112         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
113         return true;
114 }
115
116
117 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
118 {
119         switch (cmd.action) {
120         case LFUN_INSET_MODIFY: {
121                 InsetBranchParams params;
122                 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
123                 params_.branch = params.branch;
124                 setLayout(cur.buffer().params());
125                 break;
126         }
127
128         case LFUN_MOUSE_PRESS:
129                 if (cmd.button() != mouse_button::button3)
130                         InsetCollapsable::doDispatch(cur, cmd);
131                 else
132                         cur.undispatched();
133                 break;
134
135         case LFUN_INSET_DIALOG_UPDATE:
136                 InsetBranchMailer(*this).updateDialog(&cur.bv());
137                 break;
138
139         case LFUN_MOUSE_RELEASE:
140                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
141                         InsetBranchMailer(*this).showDialog(&cur.bv());
142                 else
143                         InsetCollapsable::doDispatch(cur, cmd);
144                 break;
145
146
147         case LFUN_INSET_TOGGLE:
148                 if (cmd.argument() == "assign") {
149                         // The branch inset uses "assign".
150                         if (isBranchSelected(cur.buffer())) {
151                                 if (status() != Open)
152                                         setStatus(cur, Open);
153                                 else
154                                         cur.undispatched();
155                         } else {
156                                 if (status() != Collapsed)
157                                         setStatus(cur, Collapsed);
158                                 else
159                                         cur.undispatched();
160                         }
161                 }
162                 else
163                         InsetCollapsable::doDispatch(cur, cmd);
164                 break;
165
166         default:
167                 InsetCollapsable::doDispatch(cur, cmd);
168                 break;
169         }
170 }
171
172
173 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
174                 FuncStatus & flag) const
175 {
176         switch (cmd.action) {
177         case LFUN_INSET_MODIFY:
178         case LFUN_INSET_DIALOG_UPDATE:
179                 flag.enabled(true);
180                 break;
181
182         case LFUN_INSET_TOGGLE:
183                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
184                     cmd.argument() == "toggle")
185                         flag.enabled(true);
186                 else if (cmd.argument() == "assign"
187                            || cmd.argument().empty()) {
188                         if (isBranchSelected(cur.buffer()))
189                                 flag.enabled(status() != Open);
190                         else
191                                 flag.enabled(status() != Collapsed);
192                 } else
193                         flag.enabled(true);
194                 break;
195
196         default:
197                 return InsetCollapsable::getStatus(cur, cmd, flag);
198         }
199         return true;
200 }
201
202
203 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
204 {
205         Buffer const & realbuffer = *buffer.masterBuffer();
206         BranchList const & branchlist = realbuffer.params().branchlist();
207         BranchList::const_iterator const end = branchlist.end();
208         BranchList::const_iterator it =
209                 find_if(branchlist.begin(), end,
210                              BranchNamesEqual(params_.branch));
211         if (it == end)
212                 return false;
213         return it->getSelected();
214 }
215
216
217 void InsetBranch::updateLabels(Buffer const & buf, ParIterator const & it)
218 {
219         if (isBranchSelected(buf))
220                 InsetCollapsable::updateLabels(buf, it);
221         else {
222                 TextClass const & tclass = buf.params().getTextClass();
223                 Counters savecnt = tclass.counters();
224                 InsetCollapsable::updateLabels(buf, it);
225                 tclass.counters() = savecnt;
226         }
227 }
228
229
230 int InsetBranch::latex(Buffer const & buf, odocstream & os,
231                        OutputParams const & runparams) const
232 {
233         return isBranchSelected(buf) ?
234                 InsetText::latex(buf, os, runparams) : 0;
235 }
236
237
238 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
239                            OutputParams const & runparams) const
240 {
241         if (!isBranchSelected(buf))
242                 return 0;
243
244         os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
245         InsetText::plaintext(buf, os, runparams);
246         os << "\n]";
247
248         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
249 }
250
251
252 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
253                          OutputParams const & runparams) const
254 {
255         return isBranchSelected(buf) ?
256                 InsetText::docbook(buf, os, runparams) : 0;
257 }
258
259
260 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
261 {
262         if (isBranchSelected(buf))
263                 os << paragraphs().begin()->asString(buf, true);
264 }
265
266
267 void InsetBranch::validate(LaTeXFeatures & features) const
268 {
269         InsetText::validate(features);
270 }
271
272
273 bool InsetBranch::isMacroScope(Buffer const & buf) const 
274 {
275         // Its own scope if not selected by buffer
276         return !isBranchSelected(buf);
277 }
278
279
280 string const InsetBranchMailer::name_("branch");
281
282 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
283         : inset_(inset)
284 {}
285
286
287 string const InsetBranchMailer::inset2string(Buffer const &) const
288 {
289         return params2string(inset_.params());
290 }
291
292
293 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
294 {
295         ostringstream data;
296         data << name_ << ' ';
297         params.write(data);
298         return data.str();
299 }
300
301
302 void InsetBranchMailer::string2params(string const & in,
303                                       InsetBranchParams & params)
304 {
305         params = InsetBranchParams();
306         if (in.empty())
307                 return;
308
309         istringstream data(in);
310         Lexer lex(0,0);
311         lex.setStream(data);
312
313         string name;
314         lex >> name;
315         if (name != name_)
316                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
317
318         // This is part of the inset proper that is usually swallowed
319         // by Text::readInset
320         string id;
321         lex >> id;
322         if (!lex || id != "Branch")
323                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
324
325         params.read(lex);
326 }
327
328
329 void InsetBranchParams::write(ostream & os) const
330 {
331         os << "Branch " << to_utf8(branch) << '\n';
332 }
333
334
335 void InsetBranchParams::read(Lexer & lex)
336 {
337         lex >> branch;
338 }
339
340 } // namespace lyx