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