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