]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Minor corrections and elaborations
[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 "Cursor.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "Lexer.h"
25 #include "LyX.h"
26 #include "output_docbook.h"
27 #include "output_xhtml.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
30
31 #include "support/convert.h"
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         : InsetCollapsible(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         InsetCollapsible::write(os);
57 }
58
59
60 void InsetBranch::read(Lexer & lex)
61 {
62         params_.read(lex);
63         InsetCollapsible::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
78         docstring const masteron = producesOutput() ?
79                 _("on") : _("off");
80         docstring const childon =
81                 (isBranchSelected(true) != params_.inverted) ?
82                         _("on") : _("off");
83         docstring const onoff = (masteron == childon) ?
84                 masteron :
85                 support::bformat(_("master %1$s, child %2$s"),
86                                                  masteron, childon);
87
88         docstring const heading =
89                 support::bformat(_("Branch Name: %1$s\nBranch Status: %2$s\nInset Status: %3$s"),
90                                                  params_.branch, status, onoff);
91
92         if (isOpen(bv))
93                 return heading;
94         return toolTipText(heading + from_ascii("\n"));
95 }
96
97
98 docstring const InsetBranch::buttonLabel(BufferView const &) const
99 {
100         static char_type const tick = 0x2714; // ✔ U+2714 HEAVY CHECK MARK
101         static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
102
103         Buffer const & realbuffer = *buffer().masterBuffer();
104         BranchList const & branchlist = realbuffer.params().branchlist();
105         bool const inmaster = branchlist.find(params_.branch);
106         bool const inchild = buffer().params().branchlist().find(params_.branch);
107
108         bool const master_selected = producesOutput();
109         bool const child_selected = isBranchSelected(true) != params_.inverted;
110
111         docstring symb = docstring(1, master_selected ? tick : cross);
112         if (inchild && master_selected != child_selected)
113                 symb += (child_selected ? tick : cross);
114
115         docstring inv_symb = from_ascii(params_.inverted ? "~" : "");
116
117         if (decoration() == InsetDecoration::MINIMALISTIC)
118                 return symb + inv_symb + params_.branch;
119
120         docstring s;
121         if (inmaster && inchild)
122                 s = _("Branch: ");
123         else if (inchild) // && !inmaster
124                 s = _("Branch (child): ");
125         else if (inmaster) // && !inchild
126                 s = _("Branch (master): ");
127         else // !inmaster && !inchild
128                 s = _("Branch (undefined): ");
129         s += inv_symb + params_.branch;
130
131         return symb + s;
132 }
133
134
135 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
136 {
137         if (params_.branch.empty())
138                 return Inset::backgroundColor(pi);
139         string const branch_id = (buffer().masterParams().branchlist().find(params_.branch))
140                         ? convert<string>(buffer().masterParams().branchlist().id())
141                         : convert<string>(buffer().params().branchlist().id());
142         // FIXME UNICODE
143         string const branchcol = "branch" + branch_id + to_utf8(params_.branch);
144         ColorCode c = lcolor.getFromLyXName(branchcol);
145         if (c == Color_none)
146                 c = Color_error;
147         return c;
148 }
149
150
151 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
152 {
153         switch (cmd.action()) {
154         case LFUN_INSET_MODIFY: {
155                 InsetBranchParams params;
156                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
157
158                 cur.recordUndoInset(this);
159                 params_.branch = params.branch;
160                 params_.inverted = params.inverted;
161                 // what we really want here is a TOC update, but that means
162                 // a full buffer update
163                 cur.forceBufferUpdate();
164                 break;
165         }
166         case LFUN_BRANCH_ACTIVATE:
167         case LFUN_BRANCH_DEACTIVATE:
168         case LFUN_BRANCH_MASTER_ACTIVATE:
169         case LFUN_BRANCH_MASTER_DEACTIVATE: {
170                 bool const master = (cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE
171                                      || cmd.action() == LFUN_BRANCH_MASTER_DEACTIVATE);
172                 Buffer * buf = master ? const_cast<Buffer *>(buffer().masterBuffer())
173                                       : &buffer();
174
175                 Branch * our_branch = buf->params().branchlist().find(params_.branch);
176                 if (!our_branch)
177                         break;
178
179                 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE
180                                        || cmd.action() == LFUN_BRANCH_MASTER_ACTIVATE);
181                 if (our_branch->isSelected() != activate) {
182                         // FIXME If the branch is in the master document, we cannot
183                         // call recordUndo..., because the master may be hidden, and
184                         // the code presently assumes that hidden documents can never
185                         // be dirty. See GuiView::closeBufferAll(), for example.
186                         // An option would be to check if the master is hidden.
187                         // If it is, unhide.
188                         if (!master)
189                                 buffer().undo().recordUndoBufferParams(cur);
190                         else
191                                 // at least issue a warning for now (ugly, but better than dataloss).
192                                 frontend::Alert::warning(_("Branch state changes in master document"),
193                                     lyx::support::bformat(_("The state of the branch '%1$s' "
194                                         "was changed in the master file. "
195                                         "Please make sure to save the master."), params_.branch), true);
196                         our_branch->setSelected(activate);
197                         // cur.forceBufferUpdate() is not enough
198                         buf->updateBuffer();
199                 }
200
201                 // if branch exists in a descendant, update previews.
202                 // TODO: only needed if "Show preview" is enabled in the included inset.
203                 bool exists_in_desc = false;
204                 for (auto const & it : buf->getDescendants()) {
205                         if (it->params().branchlist().find(params_.branch))
206                                 exists_in_desc = true;
207                 }
208                 if (exists_in_desc) {
209                         // TODO: ideally we would only update the previews of the
210                         // specific children that have this branch directly or
211                         // in one of their descendants
212                         buf->removePreviews();
213                         buf->updatePreviews();
214                 }
215                 break;
216         }
217         case LFUN_BRANCH_INVERT:
218                 cur.recordUndoInset(this);
219                 params_.inverted = !params_.inverted;
220                 // what we really want here is a TOC update, but that means
221                 // a full buffer update
222                 cur.forceBufferUpdate();
223                 break;
224         case LFUN_BRANCH_ADD:
225                 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
226                 break;
227         case LFUN_BRANCH_SYNC_ALL:
228                 lyx::dispatch(FuncRequest(LFUN_INSET_FORALL, "Branch:" + params_.branch + " inset-toggle assign"));
229                 break;
230         case LFUN_INSET_TOGGLE:
231                 if (cmd.argument() == "assign")
232                         setStatus(cur, (isBranchSelected(true) != params_.inverted) ? Open : Collapsed);
233                 else
234                         InsetCollapsible::doDispatch(cur, cmd);
235                 break;
236
237         default:
238                 InsetCollapsible::doDispatch(cur, cmd);
239                 break;
240         }
241 }
242
243
244 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
245                 FuncStatus & flag) const
246 {
247         bool const known_branch =
248                 buffer().params().branchlist().find(params_.branch);
249
250         switch (cmd.action()) {
251         case LFUN_INSET_MODIFY:
252                 flag.setEnabled(true);
253                 break;
254
255         case LFUN_BRANCH_ACTIVATE:
256                 flag.setEnabled(known_branch && !isBranchSelected(true));
257                 break;
258
259         case LFUN_BRANCH_INVERT:
260                 flag.setEnabled(true);
261                 flag.setOnOff(params_.inverted);
262                 break;
263
264         case LFUN_BRANCH_ADD:
265                 flag.setEnabled(!known_branch);
266                 break;
267
268         case LFUN_BRANCH_DEACTIVATE:
269                 flag.setEnabled(isBranchSelected(true));
270                 break;
271
272         case LFUN_BRANCH_MASTER_ACTIVATE:
273                 flag.setEnabled(buffer().parent()
274                                 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
275                                 && !isBranchSelected());
276                 break;
277
278         case LFUN_BRANCH_MASTER_DEACTIVATE:
279                 flag.setEnabled(buffer().parent() && isBranchSelected());
280                 break;
281
282         case LFUN_BRANCH_SYNC_ALL:
283                 flag.setEnabled(known_branch);
284                 break;
285
286         case LFUN_INSET_TOGGLE:
287                 if (cmd.argument() == "assign")
288                         flag.setEnabled(true);
289                 else
290                         return InsetCollapsible::getStatus(cur, cmd, flag);
291                 break;
292
293         default:
294                 return InsetCollapsible::getStatus(cur, cmd, flag);
295         }
296         return true;
297 }
298
299
300 bool InsetBranch::isBranchSelected(bool const child) const
301 {
302         Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
303         BranchList const & branchlist = realbuffer.params().branchlist();
304         Branch const * ourBranch = branchlist.find(params_.branch);
305
306         if (!ourBranch) {
307                 // this branch is defined in child only
308                 ourBranch = buffer().params().branchlist().find(params_.branch);
309                 if (!ourBranch)
310                         return false;
311         }
312         return ourBranch->isSelected();
313 }
314
315
316 bool InsetBranch::producesOutput() const
317 {
318         return isBranchSelected() != params_.inverted;
319 }
320
321
322 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
323 {
324         if (producesOutput() || runparams.find_with_non_output()) {
325                 OutputParams rp = runparams;
326                 rp.inbranch = true;
327                 InsetText::latex(os, rp);
328                 // These need to be passed upstream
329                 runparams.need_maketitle = rp.need_maketitle;
330                 runparams.have_maketitle = rp.have_maketitle;
331         }
332 }
333
334
335 int InsetBranch::plaintext(odocstringstream & os,
336                            OutputParams const & runparams, size_t max_length) const
337 {
338         if (!producesOutput() && !runparams.find_with_non_output())
339                 return 0;
340
341         int len = InsetText::plaintext(os, runparams, max_length);
342         return len;
343 }
344
345
346 void InsetBranch::docbook(XMLStream & xs, OutputParams const & runparams) const
347 {
348         if (producesOutput()) {
349                 OutputParams rp = runparams;
350                 rp.par_begin = 0;
351                 rp.par_end = text().paragraphs().size();
352                 docbookParagraphs(text(), buffer(), xs, rp);
353         }
354 }
355
356
357 docstring InsetBranch::xhtml(XMLStream & xs, OutputParams const & rp) const
358 {
359         if (producesOutput()) {
360                 OutputParams newrp = rp;
361                 newrp.par_begin = 0;
362                 newrp.par_end = text().paragraphs().size();
363                 xhtmlParagraphs(text(), buffer(), xs, newrp);
364         }
365         return docstring();
366 }
367
368
369 void InsetBranch::toString(odocstream & os) const
370 {
371         if (producesOutput())
372                 InsetCollapsible::toString(os);
373 }
374
375
376 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
377                                                           bool const shorten) const
378 {
379         if (producesOutput())
380                 InsetCollapsible::forOutliner(os, maxlen, shorten);
381 }
382
383
384 void InsetBranch::validate(LaTeXFeatures & features) const
385 {
386         if (producesOutput())
387                 InsetCollapsible::validate(features);
388 }
389
390
391 string InsetBranch::contextMenuName() const
392 {
393         return "context-branch";
394 }
395
396
397 bool InsetBranch::isMacroScope() const
398 {
399         // Its own scope if not selected by buffer
400         return !producesOutput();
401 }
402
403
404 string InsetBranch::params2string(InsetBranchParams const & params)
405 {
406         ostringstream data;
407         params.write(data);
408         return data.str();
409 }
410
411
412 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
413 {
414         params = InsetBranchParams();
415         if (in.empty())
416                 return;
417
418         istringstream data(in);
419         Lexer lex;
420         lex.setStream(data);
421         lex.setContext("InsetBranch::string2params");
422         params.read(lex);
423 }
424
425
426 void InsetBranch::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
427 {
428         setLabel(params_.branch + (params_.inverted ? " (-)" : ""));
429         InsetCollapsible::updateBuffer(it, utype, deleted);
430 }
431
432
433 void InsetBranchParams::write(ostream & os) const
434 {
435         os << to_utf8(branch)
436            << '\n'
437            << "inverted "
438            << inverted;
439 }
440
441
442 void InsetBranchParams::read(Lexer & lex)
443 {
444         // There may be a space in branch name
445         // if we wanted to use lex>>, the branch name should be properly in quotes
446         lex.eatLine();
447         branch = lex.getDocString();
448         lex >> "inverted" >> inverted;
449 }
450
451 } // namespace lyx