]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Show symbol name and description in plaintext export of nomenclature entries.
[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 "LyX.h"
27 #include "OutputParams.h"
28 #include "output_xhtml.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
31
32 #include "support/debug.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 #include "frontends/alert.h"
37 #include "frontends/Application.h"
38
39 #include <sstream>
40
41 using namespace std;
42
43
44 namespace lyx {
45
46 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
47         : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
48 {}
49
50
51 void InsetBranch::write(ostream & os) const
52 {
53         os << "Branch ";
54         params_.write(os);
55         os << '\n';
56         InsetCollapsable::write(os);
57 }
58
59
60 void InsetBranch::read(Lexer & lex)
61 {
62         params_.read(lex);
63         InsetCollapsable::read(lex);
64 }
65
66
67 docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
68 {
69         docstring const masterstatus = isBranchSelected() ?
70                 _("active") : _("non-active");
71         docstring const childstatus = isBranchSelected(true) ?
72                 _("active") : _("non-active");
73         docstring const status = (masterstatus == childstatus) ?
74                 masterstatus :
75                 support::bformat(_("master: %1$s, child: %2$s"),
76                                                  masterstatus, childstatus);
77         docstring const heading = 
78                 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
79         if (isOpen(bv))
80                 return heading;
81         return toolTipText(heading + from_ascii("\n"));
82 }
83
84
85 docstring const InsetBranch::buttonLabel(BufferView const &) const
86 {
87         static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
88         static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
89
90         Buffer const & realbuffer = *buffer().masterBuffer();
91         BranchList const & branchlist = realbuffer.params().branchlist();
92         bool const inmaster = branchlist.find(params_.branch);
93         bool const inchild = buffer().params().branchlist().find(params_.branch);
94
95         bool const master_selected = isBranchSelected();
96         bool const child_selected = isBranchSelected(true);
97
98         docstring symb = docstring(1, master_selected ? tick : cross);
99         if (inchild && master_selected != child_selected)
100                 symb += child_selected ? tick : cross;
101
102         if (decoration() == InsetLayout::MINIMALISTIC)
103                 return symb + params_.branch;
104
105         docstring s;
106         if (inmaster && inchild)
107                 s = _("Branch: ");
108         else if (inchild) // && !inmaster
109                 s = _("Branch (child only): ");
110         else if (inmaster) // && !inchild
111                 s = _("Branch (master only): ");
112         else // !inmaster && !inchild
113                 s = _("Branch (undefined): ");
114         s += params_.branch;
115
116     return symb + s;
117 }
118
119
120 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
121 {
122         if (params_.branch.empty())
123                 return Inset::backgroundColor(pi);
124         // FIXME UNICODE
125         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
126         if (c == Color_none)
127                 c = Color_error;
128         return c;
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
139                 cur.recordUndoInset(this);
140                 params_.branch = params.branch;
141                 // what we really want here is a TOC update, but that means
142                 // a full buffer update
143                 cur.forceBufferUpdate();
144                 break;
145         }
146         case LFUN_BRANCH_ACTIVATE:
147         case LFUN_BRANCH_DEACTIVATE:
148         case LFUN_BRANCH_MASTER_ACTIVATE:
149         case LFUN_BRANCH_MASTER_DEACTIVATE: {
150                 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
151                                      || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
152                 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
153                                       : &buffer();
154
155                 Branch * our_branch = buf->params().branchlist().find(params_.branch);
156                 if (!our_branch)
157                         break;
158
159                 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
160                                        || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
161                 if (our_branch->isSelected() != activate) {
162                         // FIXME If the branch is in the master document, we cannot
163                         // call recordUndo..., because the master may be hidden, and
164                         // the code presently assumes that hidden documents can never
165                         // be dirty. See GuiView::closeBufferAll(), for example.
166                         // An option would be to check if the master is hidden.
167                         // If it is, unhide.
168                         if (!master)
169                                 buffer().undo().recordUndoBufferParams(cur);
170                         else
171                                 // at least issue a warning for now (ugly, but better than dataloss).
172                                 frontend::Alert::warning(_("Branch state changes in master document"),
173                                     lyx::support::bformat(_("The state of the branch '%1$s' "
174                                         "was changed in the master file. "
175                                         "Please make sure to save the master."), params_.branch), true);
176                         our_branch->setSelected(activate);
177                         // cur.forceBufferUpdate() is not enough
178                         buf->updateBuffer();
179                 }
180                 break;
181         }
182         case LFUN_BRANCH_ADD:
183                 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
184                 break;
185         case LFUN_INSET_TOGGLE:
186                 if (cmd.argument() == "assign")
187                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
188                 else
189                         InsetCollapsable::doDispatch(cur, cmd);
190                 break;
191
192         default:
193                 InsetCollapsable::doDispatch(cur, cmd);
194                 break;
195         }
196 }
197
198
199 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
200                 FuncStatus & flag) const
201 {
202         bool const known_branch =
203                 buffer().params().branchlist().find(params_.branch);
204
205         switch (cmd.action()) {
206         case LFUN_INSET_MODIFY:
207                 flag.setEnabled(true);
208                 break;
209
210         case LFUN_BRANCH_ACTIVATE:
211                 flag.setEnabled(known_branch && !isBranchSelected(true));
212                 break;
213
214         case LFUN_BRANCH_ADD:
215                 flag.setEnabled(!known_branch);
216                 break;
217
218         case LFUN_BRANCH_DEACTIVATE:
219                 flag.setEnabled(isBranchSelected(true));
220                 break;
221
222         case LFUN_BRANCH_MASTER_ACTIVATE:
223                 flag.setEnabled(buffer().parent()
224                                 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
225                                 && !isBranchSelected());
226                 break;
227
228         case LFUN_BRANCH_MASTER_DEACTIVATE:
229                 flag.setEnabled(buffer().parent() && isBranchSelected());
230                 break;
231
232         case LFUN_INSET_TOGGLE:
233                 if (cmd.argument() == "assign")
234                         flag.setEnabled(true);
235                 else
236                         return InsetCollapsable::getStatus(cur, cmd, flag);     
237                 break;
238
239         default:
240                 return InsetCollapsable::getStatus(cur, cmd, flag);
241         }
242         return true;
243 }
244
245
246 bool InsetBranch::isBranchSelected(bool const child) const
247 {
248         Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
249         BranchList const & branchlist = realbuffer.params().branchlist();
250         Branch const * ourBranch = branchlist.find(params_.branch);
251
252         if (!ourBranch) {
253                 // this branch is defined in child only
254                 ourBranch = buffer().params().branchlist().find(params_.branch);
255                 if (!ourBranch)
256                         return false;
257         }
258         return ourBranch->isSelected();
259 }
260
261
262 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
263 {
264         if (isBranchSelected())
265                 InsetText::latex(os, runparams);
266 }
267
268
269 int InsetBranch::plaintext(odocstringstream & os,
270                            OutputParams const & runparams, size_t max_length) const
271 {
272         if (!isBranchSelected())
273                 return 0;
274
275         int len = InsetText::plaintext(os, runparams, max_length);
276         return len;
277 }
278
279
280 int InsetBranch::docbook(odocstream & os,
281                          OutputParams const & runparams) const
282 {
283         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
284 }
285
286
287 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
288 {
289         if (isBranchSelected()) {
290                 OutputParams newrp = rp;
291                 newrp.par_begin = 0;
292                 newrp.par_end = text().paragraphs().size();
293                 xhtmlParagraphs(text(), buffer(), xs, newrp);
294         }
295         return docstring();
296 }
297
298
299 void InsetBranch::toString(odocstream & os) const
300 {
301         if (isBranchSelected())
302                 InsetCollapsable::toString(os);
303 }
304
305
306 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
307                                                           bool const shorten) const
308 {
309         if (isBranchSelected())
310                 InsetCollapsable::forOutliner(os, maxlen, shorten);
311 }
312
313
314 void InsetBranch::validate(LaTeXFeatures & features) const
315 {
316         if (isBranchSelected())
317                 InsetCollapsable::validate(features);
318 }
319
320
321 string InsetBranch::contextMenuName() const
322 {
323         return "context-branch";
324 }
325
326
327 bool InsetBranch::isMacroScope() const 
328 {
329         // Its own scope if not selected by buffer
330         return !isBranchSelected();
331 }
332
333
334 string InsetBranch::params2string(InsetBranchParams const & params)
335 {
336         ostringstream data;
337         params.write(data);
338         return data.str();
339 }
340
341
342 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
343 {
344         params = InsetBranchParams();
345         if (in.empty())
346                 return;
347
348         istringstream data(in);
349         Lexer lex;
350         lex.setStream(data);
351         lex.setContext("InsetBranch::string2params");
352         params.read(lex);
353 }
354
355
356 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
357                                                    UpdateType utype) const
358 {
359         DocIterator pit = cpit;
360         pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
361         
362         docstring tooltip;
363         text().forOutliner(tooltip, TOC_ENTRY_LENGTH);
364         docstring str = params_.branch + ": " + tooltip;
365         tooltip = support::wrapParas(tooltip, 0, 60, 2);
366         
367         shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
368         toc->push_back(TocItem(pit, 0, str, output_active, tooltip));
369         
370         // Proceed with the rest of the inset.
371         bool const doing_output = output_active && isBranchSelected();
372         InsetCollapsable::addToToc(cpit, doing_output, utype);
373 }
374
375
376 void InsetBranchParams::write(ostream & os) const
377 {
378         os << to_utf8(branch);
379 }
380
381
382 void InsetBranchParams::read(Lexer & lex)
383 {
384         lex.eatLine();
385         branch = lex.getDocString();
386 }
387
388 } // namespace lyx