]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[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
30 namespace lyx {
31
32 using std::string;
33 using std::auto_ptr;
34 using std::istringstream;
35 using std::ostream;
36 using std::ostringstream;
37
38
39 void InsetBranch::init()
40 {
41         setInsetName(from_utf8("Branch"));
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<InsetBase> InsetBranch::doClone() const
68 {
69         return auto_ptr<InsetBase>(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, LyXLex & lex)
87 {
88         params_.read(lex);
89         InsetCollapsable::read(buf, lex);
90         setButtonLabel();
91 }
92
93
94 void InsetBranch::setButtonLabel()
95 {
96         LyXFont font(LyXFont::ALL_SANE);
97         font.decSize();
98         font.decSize();
99
100         docstring s = _("Branch: ") + params_.branch;
101         font.setColor(LColor::foreground);
102         if (!params_.branch.empty()) {
103                 // FIXME UNICODE
104                 LColor_color c = lcolor.getFromLyXName(to_utf8(params_.branch));
105                 if (c == LColor::none) {
106                         c = LColor::error;
107                         s = _("Undef: ") + s;
108                 }
109                 setBackgroundColor(c);
110         } else
111                 setBackgroundColor(LColor::background);
112         setLabel(isOpen() ? s : getNewLabel(s) );
113         setLabelFont(font);
114 }
115
116
117 bool InsetBranch::showInsetDialog(BufferView * bv) const
118 {
119         InsetBranchMailer(const_cast<InsetBranch &>(*this)).showDialog(bv);
120         return true;
121 }
122
123
124 void InsetBranch::doDispatch(LCursor & cur, FuncRequest & cmd)
125 {
126         switch (cmd.action) {
127         case LFUN_INSET_MODIFY: {
128                 InsetBranchParams params;
129                 InsetBranchMailer::string2params(to_utf8(cmd.argument()), params);
130                 params_.branch = params.branch;
131                 setButtonLabel();
132                 break;
133         }
134
135         case LFUN_MOUSE_PRESS:
136                 if (cmd.button() != mouse_button::button3)
137                         InsetCollapsable::doDispatch(cur, cmd);
138                 else
139                         cur.undispatched();
140                 break;
141
142         case LFUN_INSET_DIALOG_UPDATE:
143                 InsetBranchMailer(*this).updateDialog(&cur.bv());
144                 break;
145
146         case LFUN_MOUSE_RELEASE:
147                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
148                         InsetBranchMailer(*this).showDialog(&cur.bv());
149                 else
150                         InsetCollapsable::doDispatch(cur, cmd);
151                 break;
152
153
154         case LFUN_INSET_TOGGLE:
155                 if (cmd.argument() == "assign" || cmd.argument().empty()) {
156                         // The branch inset uses "assign".
157                         if (isBranchSelected(cur.buffer())) {
158                                 if (status() != Open)
159                                         setStatus(cur, Open);
160                                 else
161                                         cur.undispatched();
162                         } else {
163                                 if (status() != Collapsed)
164                                         setStatus(cur, Collapsed);
165                                 else
166                                         cur.undispatched();
167                         }
168                 }
169                 else
170                         InsetCollapsable::doDispatch(cur, cmd);
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                         if (isBranchSelected(cur.buffer()))
196                                 flag.enabled(status() != Open);
197                         else
198                                 flag.enabled(status() != Collapsed);
199                 } else
200                         flag.enabled(true);
201                 break;
202
203         default:
204                 return InsetCollapsable::getStatus(cur, cmd, flag);
205         }
206         return true;
207 }
208
209
210 bool InsetBranch::isBranchSelected(Buffer const & buffer) const
211 {
212         Buffer const & realbuffer = *buffer.getMasterBuffer();
213         BranchList const & branchlist = realbuffer.params().branchlist();
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, odocstream & os,
225                        OutputParams const & runparams) const
226 {
227         return isBranchSelected(buf) ?
228                 InsetText::latex(buf, os, runparams) : 0;
229 }
230
231
232 int InsetBranch::docbook(Buffer const & buf, odocstream & os,
233                          OutputParams const & runparams) const
234 {
235         return isBranchSelected(buf) ?
236                 InsetText::docbook(buf, os, runparams) : 0;
237 }
238
239
240 int InsetBranch::plaintext(Buffer const & buf, odocstream & os,
241                            OutputParams const & runparams) const
242 {
243         return isBranchSelected(buf) ?
244                 InsetText::plaintext(buf, os, runparams): 0;
245 }
246
247
248 void InsetBranch::textString(Buffer const & buf, odocstream & os) const
249 {
250         if (isBranchSelected(buf))
251                 os << paragraphs().begin()->asString(buf, true);
252 }
253
254
255 void InsetBranch::validate(LaTeXFeatures & features) const
256 {
257         InsetText::validate(features);
258 }
259
260
261
262 string const InsetBranchMailer::name_("branch");
263
264 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
265         : inset_(inset)
266 {}
267
268
269 string const InsetBranchMailer::inset2string(Buffer const &) const
270 {
271         return params2string(inset_.params());
272 }
273
274
275 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
276 {
277         ostringstream data;
278         data << name_ << ' ';
279         params.write(data);
280         return data.str();
281 }
282
283
284 void InsetBranchMailer::string2params(string const & in,
285                                       InsetBranchParams & params)
286 {
287         params = InsetBranchParams();
288         if (in.empty())
289                 return;
290
291         istringstream data(in);
292         LyXLex lex(0,0);
293         lex.setStream(data);
294
295         string name;
296         lex >> name;
297         if (name != name_)
298                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
299
300         // This is part of the inset proper that is usually swallowed
301         // by LyXText::readInset
302         string id;
303         lex >> id;
304         if (!lex || id != "Branch")
305                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
306
307         params.read(lex);
308 }
309
310
311 void InsetBranchParams::write(ostream & os) const
312 {
313         os << "Branch " << to_utf8(branch) << '\n';
314 }
315
316
317 void InsetBranchParams::read(LyXLex & lex)
318 {
319         lex >> branch;
320 }
321
322 } // namespace lyx