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