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