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