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