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