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