]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
Please Andre and make InsetBranch::getStatus more compact
[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(Open);
151                 else if (cmd.argument == "close") {
152                         setStatus(Collapsed);
153                         leaveInset(cur, *this);
154                 } else if (cmd.argument == "toggle") {
155                         if (isOpen()) {
156                                 setStatus(Collapsed);
157                                 leaveInset(cur, *this);
158                         } else
159                         setStatus(Open);
160
161                 // The branch inset uses "assign".
162                 } else if (cmd.argument == "assign"
163                            || cmd.argument.empty()) {
164                         BranchList const & branchlist =
165                                 cur.buffer().params().branchlist();
166                         if (isBranchSelected(branchlist)) {
167                                 if (status() != Open)
168                                         setStatus(Open);
169                                 else
170                                         cur.undispatched();
171                         } else {
172                                 if (status() != Collapsed) {
173                                         setStatus(Collapsed);
174                                         leaveInset(cur, *this);
175                                 } else
176                                         cur.undispatched();
177                         }
178                 }
179                 break;
180
181         default:
182                 InsetCollapsable::doDispatch(cur, cmd);
183                 break;
184         }
185 }
186
187
188 bool InsetBranch::getStatus(LCursor & cur, FuncRequest const & cmd,
189                 FuncStatus & flag) const
190 {
191         switch (cmd.action) {
192         case LFUN_INSET_MODIFY:
193         case LFUN_INSET_DIALOG_UPDATE:
194                 flag.enabled(true);
195                 break;
196
197         case LFUN_INSET_TOGGLE:
198                 if (cmd.argument == "open" || cmd.argument == "close" ||
199                     cmd.argument == "toggle")
200                         flag.enabled(true);
201                 else if (cmd.argument == "assign"
202                            || cmd.argument.empty()) {
203                         BranchList const & branchlist =
204                                 cur.buffer().params().branchlist();
205                         if (isBranchSelected(branchlist))
206                                 flag.enabled(status() != Open);
207                         else
208                                 flag.enabled(status() != Collapsed);
209                 } else
210                         flag.enabled(true);
211                 break;
212
213         default:
214                 return InsetCollapsable::getStatus(cur, cmd, flag);
215         }
216         return true;
217 }
218
219
220 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
221 {
222         BranchList::const_iterator const end = branchlist.end();
223         BranchList::const_iterator it =
224                 std::find_if(branchlist.begin(), end,
225                              BranchNamesEqual(params_.branch));
226         if (it == end)
227                 return false;
228         return it->getSelected();
229 }
230
231
232 int InsetBranch::latex(Buffer const & buf, ostream & os,
233                        OutputParams const & runparams) const
234 {
235         return isBranchSelected(buf.params().branchlist()) ?
236                 InsetText::latex(buf, os, runparams) : 0;
237 }
238
239
240 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
241                           OutputParams const & runparams) const
242 {
243         return isBranchSelected(buf.params().branchlist()) ?
244                 InsetText::linuxdoc(buf, os, runparams) : 0;
245 }
246
247
248 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
249                          OutputParams const & runparams) const
250 {
251         return isBranchSelected(buf.params().branchlist()) ?
252                 InsetText::docbook(buf, os, runparams) : 0;
253 }
254
255
256 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
257                            OutputParams const & runparams) const
258 {
259         return isBranchSelected(buf.params().branchlist()) ?
260                 InsetText::plaintext(buf, os, runparams): 0;
261 }
262
263
264 void InsetBranch::validate(LaTeXFeatures & features) const
265 {
266         InsetText::validate(features);
267 }
268
269
270
271 string const InsetBranchMailer::name_("branch");
272
273 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
274         : inset_(inset)
275 {}
276
277
278 string const InsetBranchMailer::inset2string(Buffer const &) const
279 {
280         return params2string(inset_.params());
281 }
282
283
284 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
285 {
286         ostringstream data;
287         data << name_ << ' ';
288         params.write(data);
289         return data.str();
290 }
291
292
293 void InsetBranchMailer::string2params(string const & in,
294                                       InsetBranchParams & params)
295 {
296         params = InsetBranchParams();
297         if (in.empty())
298                 return;
299
300         istringstream data(in);
301         LyXLex lex(0,0);
302         lex.setStream(data);
303
304         string name;
305         lex >> name;
306         if (name != name_)
307                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
308
309         // This is part of the inset proper that is usually swallowed
310         // by LyXText::readInset
311         string id;
312         lex >> id;
313         if (!lex || id != "Branch")
314                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
315
316         params.read(lex);
317 }
318
319
320 void InsetBranchParams::write(ostream & os) const
321 {
322         os << "Branch " << branch << '\n';
323 }
324
325
326 void InsetBranchParams::read(LyXLex & lex)
327 {
328         lex >> branch;
329 }