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