]> git.lyx.org Git - features.git/blob - src/insets/InsetBranch.cpp
Remove background_color_ in Insets: it takes a lot of unnecessary memory,
[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 "Cursor.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "Color.h"
24 #include "Lexer.h"
25 #include "Paragraph.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 int InsetBranch::latex(Buffer const & buf, odocstream & os,
236                        OutputParams const & runparams) const
237 {
238         return isBranchSelected(buf) ?
239                 InsetText::latex(buf, os, runparams) : 0;
240 }
241
242
243 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
244                            OutputParams const & runparams) const
245 {
246         if (!isBranchSelected(buf))
247                 return 0;
248
249         os << '[' << buf.B_("branch") << ' ' << params_.branch << ":\n";
250         InsetText::plaintext(buf, os, runparams);
251         os << "\n]";
252
253         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
254 }
255
256
257 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
258                          OutputParams const & runparams) const
259 {
260         return isBranchSelected(buf) ?
261                 InsetText::docbook(buf, os, runparams) : 0;
262 }
263
264
265 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
266 {
267         if (isBranchSelected(buf))
268                 os << paragraphs().begin()->asString(buf, true);
269 }
270
271
272 void InsetBranch::validate(LaTeXFeatures & features) const
273 {
274         InsetText::validate(features);
275 }
276
277
278
279 string const InsetBranchMailer::name_("branch");
280
281 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
282         : inset_(inset)
283 {}
284
285
286 string const InsetBranchMailer::inset2string(Buffer const &) const
287 {
288         return params2string(inset_.params());
289 }
290
291
292 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
293 {
294         ostringstream data;
295         data << name_ << ' ';
296         params.write(data);
297         return data.str();
298 }
299
300
301 void InsetBranchMailer::string2params(string const & in,
302                                       InsetBranchParams & params)
303 {
304         params = InsetBranchParams();
305         if (in.empty())
306                 return;
307
308         istringstream data(in);
309         Lexer lex(0,0);
310         lex.setStream(data);
311
312         string name;
313         lex >> name;
314         if (name != name_)
315                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
316
317         // This is part of the inset proper that is usually swallowed
318         // by Text::readInset
319         string id;
320         lex >> id;
321         if (!lex || id != "Branch")
322                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
323
324         params.read(lex);
325 }
326
327
328 void InsetBranchParams::write(ostream & os) const
329 {
330         os << "Branch " << to_utf8(branch) << '\n';
331 }
332
333
334 void InsetBranchParams::read(Lexer & lex)
335 {
336         lex >> branch;
337 }
338
339 } // namespace lyx