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