]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
76cb30720e084a14ddd4c5d52e0da6f8644a3b0d
[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 "BufferView.h"
18 #include "BranchList.h"
19 #include "ColorSet.h"
20 #include "Counters.h"
21 #include "Cursor.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "Lexer.h"
26 #include "OutputParams.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
30
31 #include "support/debug.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34
35 #include "frontends/Application.h"
36
37 #include <sstream>
38
39 using namespace std;
40
41
42 namespace lyx {
43
44 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
45         : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
46 {}
47
48
49 void InsetBranch::write(ostream & os) const
50 {
51         os << "Branch ";
52         params_.write(os);
53         os << '\n';
54         InsetCollapsable::write(os);
55 }
56
57
58 void InsetBranch::read(Lexer & lex)
59 {
60         params_.read(lex);
61         InsetCollapsable::read(lex);
62 }
63
64
65 docstring InsetBranch::toolTip(BufferView const & bv, int x, int y) const
66 {
67         docstring const status = isBranchSelected() ? 
68                 _("active") : _("non-active");
69         docstring const heading = 
70                 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
71         docstring const contents = InsetCollapsable::toolTip(bv, x, y);
72         if (isOpen(bv) || contents.empty())
73                 return heading;
74         else
75                 return heading + from_ascii("\n") + contents;
76 }
77
78
79 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
80 {
81         docstring s = _("Branch: ") + params_.branch;
82         Buffer const & realbuffer = *buffer().masterBuffer();
83         BranchList const & branchlist = realbuffer.params().branchlist();
84         if (!branchlist.find(params_.branch)
85             && buffer().params().branchlist().find(params_.branch))
86                 s = _("Branch (child only): ") + params_.branch;
87         else if (!branchlist.find(params_.branch))
88                 s = _("Branch (undefined): ") + params_.branch;
89         if (!params_.branch.empty()) {
90                 // FIXME UNICODE
91                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
92                 if (c == Color_none)
93                         s = _("Undef: ") + s;
94         }
95         s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
96         if (decoration() == InsetLayout::CLASSIC)
97                 return isOpen(bv) ? s : getNewLabel(s);
98         else
99                 return params_.branch + ": " + getNewLabel(s);
100 }
101
102
103 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
104 {
105         if (params_.branch.empty())
106                 return Inset::backgroundColor(pi);
107         // FIXME UNICODE
108         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
109         if (c == Color_none)
110                 c = Color_error;
111         return c;
112 }
113
114
115 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
116 {
117         switch (cmd.action()) {
118         case LFUN_INSET_MODIFY: {
119                 InsetBranchParams params;
120                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
121                 params_.branch = params.branch;
122                 break;
123         }
124         case LFUN_BRANCH_ACTIVATE:
125         case LFUN_BRANCH_DEACTIVATE: {
126                 // FIXME: I do not like this cast, but have no other idea...
127                 Buffer const * buf = buffer().masterBuffer();
128                 BranchList const & branchlist = buf->params().branchlist();
129                 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
130                 if (!our_branch) {
131                         // child only?
132                         our_branch = buffer().params().branchlist().find(params_.branch);
133                         if (!our_branch)
134                                 break;
135                 }
136                 our_branch->setSelected(cmd.action() == LFUN_BRANCH_ACTIVATE);
137                 break;
138         }
139         case LFUN_INSET_TOGGLE:
140                 if (cmd.argument() == "assign")
141                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
142                 else
143                         InsetCollapsable::doDispatch(cur, cmd);
144                 break;
145
146         default:
147                 InsetCollapsable::doDispatch(cur, cmd);
148                 break;
149         }
150 }
151
152
153 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
154                 FuncStatus & flag) const
155 {
156         switch (cmd.action()) {
157         case LFUN_INSET_MODIFY:
158                 flag.setEnabled(true);
159                 break;
160
161         case LFUN_BRANCH_ACTIVATE:
162                 flag.setEnabled(!isBranchSelected());
163                 break;
164
165         case LFUN_BRANCH_DEACTIVATE:
166                 flag.setEnabled(isBranchSelected());
167                 break;
168
169         case LFUN_INSET_TOGGLE:
170                 if (cmd.argument() == "assign")
171                         flag.setEnabled(true);
172                 else
173                         return InsetCollapsable::getStatus(cur, cmd, flag);     
174
175         default:
176                 return InsetCollapsable::getStatus(cur, cmd, flag);
177         }
178         return true;
179 }
180
181
182 bool InsetBranch::isBranchSelected() const
183 {
184         Buffer const & realbuffer = *buffer().masterBuffer();
185         BranchList const & branchlist = realbuffer.params().branchlist();
186         Branch const * ourBranch = branchlist.find(params_.branch);
187
188         if (!ourBranch) {
189                 // this branch is defined in child only
190                 ourBranch = buffer().params().branchlist().find(params_.branch);
191                 if (!ourBranch)
192                         return false;
193         }
194         return ourBranch->isSelected();
195 }
196
197
198 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
199 {
200         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
201 }
202
203
204 int InsetBranch::plaintext(odocstream & os,
205                            OutputParams const & runparams) const
206 {
207         if (!isBranchSelected())
208                 return 0;
209
210         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
211         InsetText::plaintext(os, runparams);
212         os << "\n]";
213
214         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
215 }
216
217
218 int InsetBranch::docbook(odocstream & os,
219                          OutputParams const & runparams) const
220 {
221         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
222 }
223
224
225 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
226 {
227         if (isBranchSelected())
228                  return InsetText::xhtml(xs, rp);
229         return docstring();
230 }
231
232
233 void InsetBranch::tocString(odocstream & os) const
234 {
235         if (isBranchSelected())
236                 InsetCollapsable::tocString(os);
237 }
238
239
240 void InsetBranch::validate(LaTeXFeatures & features) const
241 {
242         if (isBranchSelected())
243                 InsetCollapsable::validate(features);
244 }
245
246
247 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
248 {
249         return from_ascii("context-branch");
250 }
251
252
253 bool InsetBranch::isMacroScope() const 
254 {
255         // Its own scope if not selected by buffer
256         return !isBranchSelected();
257 }
258
259
260 string InsetBranch::params2string(InsetBranchParams const & params)
261 {
262         ostringstream data;
263         params.write(data);
264         return data.str();
265 }
266
267
268 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
269 {
270         params = InsetBranchParams();
271         if (in.empty())
272                 return;
273
274         istringstream data(in);
275         Lexer lex;
276         lex.setStream(data);
277         lex.setContext("InsetBranch::string2params");
278         params.read(lex);
279 }
280
281
282 void InsetBranch::addToToc(DocIterator const & cpit)
283 {
284         DocIterator pit = cpit;
285         pit.push_back(CursorSlice(*this));
286
287         Toc & toc = buffer().tocBackend().toc("branch");
288         docstring const str = params_.branch + ": " + text().getPar(0).asString();
289         toc.push_back(TocItem(pit, 0, str));
290         // Proceed with the rest of the inset.
291         InsetCollapsable::addToToc(cpit);
292 }
293
294
295 void InsetBranchParams::write(ostream & os) const
296 {
297         os << to_utf8(branch);
298 }
299
300
301 void InsetBranchParams::read(Lexer & lex)
302 {
303         lex >> branch;
304 }
305
306 } // namespace lyx