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