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