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