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