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