]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
88d00e641e486a7b76507d0ae40ee652badf9844
[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                 break;
194         }
195         case LFUN_BRANCH_INVERT:
196                 cur.recordUndoInset(this);
197                 params_.inverted = !params_.inverted;
198                 // what we really want here is a TOC update, but that means
199                 // a full buffer update
200                 cur.forceBufferUpdate();
201                 break;
202         case LFUN_BRANCH_ADD:
203                 lyx::dispatch(FuncRequest(LFUN_BRANCH_ADD, params_.branch));
204                 break;
205         case LFUN_INSET_TOGGLE:
206                 if (cmd.argument() == "assign")
207                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
208                 else
209                         InsetCollapsible::doDispatch(cur, cmd);
210                 break;
211
212         default:
213                 InsetCollapsible::doDispatch(cur, cmd);
214                 break;
215         }
216 }
217
218
219 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
220                 FuncStatus & flag) const
221 {
222         bool const known_branch =
223                 buffer().params().branchlist().find(params_.branch);
224
225         switch (cmd.action()) {
226         case LFUN_INSET_MODIFY:
227                 flag.setEnabled(true);
228                 break;
229
230         case LFUN_BRANCH_ACTIVATE:
231                 flag.setEnabled(known_branch && !isBranchSelected(true));
232                 break;
233
234         case LFUN_BRANCH_INVERT:
235                 flag.setEnabled(true);
236                 flag.setOnOff(params_.inverted);
237                 break;
238
239         case LFUN_BRANCH_ADD:
240                 flag.setEnabled(!known_branch);
241                 break;
242
243         case LFUN_BRANCH_DEACTIVATE:
244                 flag.setEnabled(isBranchSelected(true));
245                 break;
246
247         case LFUN_BRANCH_MASTER_ACTIVATE:
248                 flag.setEnabled(buffer().parent()
249                                 && buffer().masterBuffer()->params().branchlist().find(params_.branch)
250                                 && !isBranchSelected());
251                 break;
252
253         case LFUN_BRANCH_MASTER_DEACTIVATE:
254                 flag.setEnabled(buffer().parent() && isBranchSelected());
255                 break;
256
257         case LFUN_INSET_TOGGLE:
258                 if (cmd.argument() == "assign")
259                         flag.setEnabled(true);
260                 else
261                         return InsetCollapsible::getStatus(cur, cmd, flag);
262                 break;
263
264         default:
265                 return InsetCollapsible::getStatus(cur, cmd, flag);
266         }
267         return true;
268 }
269
270
271 bool InsetBranch::isBranchSelected(bool const child) const
272 {
273         Buffer const & realbuffer = child ? buffer() : *buffer().masterBuffer();
274         BranchList const & branchlist = realbuffer.params().branchlist();
275         Branch const * ourBranch = branchlist.find(params_.branch);
276
277         if (!ourBranch) {
278                 // this branch is defined in child only
279                 ourBranch = buffer().params().branchlist().find(params_.branch);
280                 if (!ourBranch)
281                         return false;
282         }
283         return ourBranch->isSelected();
284 }
285
286
287 bool InsetBranch::producesOutput() const
288 {
289         return isBranchSelected() != params_.inverted;
290 }
291
292
293 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
294 {
295         if (producesOutput()) {
296                 OutputParams rp = runparams;
297                 rp.inbranch = true;
298                 InsetText::latex(os, rp);
299         }
300 }
301
302
303 int InsetBranch::plaintext(odocstringstream & os,
304                            OutputParams const & runparams, size_t max_length) const
305 {
306         if (!producesOutput())
307                 return 0;
308
309         int len = InsetText::plaintext(os, runparams, max_length);
310         return len;
311 }
312
313
314 int InsetBranch::docbook(odocstream & os,
315                          OutputParams const & runparams) const
316 {
317         return producesOutput() ?  InsetText::docbook(os, runparams) : 0;
318 }
319
320
321 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
322 {
323         if (producesOutput()) {
324                 OutputParams newrp = rp;
325                 newrp.par_begin = 0;
326                 newrp.par_end = text().paragraphs().size();
327                 xhtmlParagraphs(text(), buffer(), xs, newrp);
328         }
329         return docstring();
330 }
331
332
333 void InsetBranch::toString(odocstream & os) const
334 {
335         if (producesOutput())
336                 InsetCollapsible::toString(os);
337 }
338
339
340 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
341                                                           bool const shorten) const
342 {
343         if (producesOutput())
344                 InsetCollapsible::forOutliner(os, maxlen, shorten);
345 }
346
347
348 void InsetBranch::validate(LaTeXFeatures & features) const
349 {
350         if (producesOutput())
351                 InsetCollapsible::validate(features);
352 }
353
354
355 string InsetBranch::contextMenuName() const
356 {
357         return "context-branch";
358 }
359
360
361 bool InsetBranch::isMacroScope() const
362 {
363         // Its own scope if not selected by buffer
364         return !producesOutput();
365 }
366
367
368 string InsetBranch::params2string(InsetBranchParams const & params)
369 {
370         ostringstream data;
371         params.write(data);
372         return data.str();
373 }
374
375
376 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
377 {
378         params = InsetBranchParams();
379         if (in.empty())
380                 return;
381
382         istringstream data(in);
383         Lexer lex;
384         lex.setStream(data);
385         lex.setContext("InsetBranch::string2params");
386         params.read(lex);
387 }
388
389
390 void InsetBranch::updateBuffer(ParIterator const & it, UpdateType utype)
391 {
392         setLabel(params_.branch + (params_.inverted ? " (-)" : ""));
393         InsetCollapsible::updateBuffer(it, utype);
394 }
395
396
397 void InsetBranchParams::write(ostream & os) const
398 {
399         os << to_utf8(branch)
400            << '\n'
401            << "inverted "
402            << inverted;
403 }
404
405
406 void InsetBranchParams::read(Lexer & lex)
407 {
408         // There may be a space in branch name
409         // if we wanted to use lex>>, the branch name should be properly in quotes
410         lex.eatLine();
411         branch = lex.getDocString();
412         lex >> "inverted" >> inverted;
413 }
414
415 } // namespace lyx