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