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