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