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