]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
53007f604bfbd715971d2f5449a2df7ddbb4f5e9
[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 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
284 {
285         if (isBranchActive())
286                 InsetText::latex(os, runparams);
287 }
288
289
290 int InsetBranch::plaintext(odocstringstream & os,
291                            OutputParams const & runparams, size_t max_length) const
292 {
293         if (!isBranchActive())
294                 return 0;
295
296         int len = InsetText::plaintext(os, runparams, max_length);
297         return len;
298 }
299
300
301 int InsetBranch::docbook(odocstream & os,
302                          OutputParams const & runparams) const
303 {
304         return isBranchActive() ?  InsetText::docbook(os, runparams) : 0;
305 }
306
307
308 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
309 {
310         if (isBranchActive()) {
311                 OutputParams newrp = rp;
312                 newrp.par_begin = 0;
313                 newrp.par_end = text().paragraphs().size();
314                 xhtmlParagraphs(text(), buffer(), xs, newrp);
315         }
316         return docstring();
317 }
318
319
320 void InsetBranch::toString(odocstream & os) const
321 {
322         if (isBranchActive())
323                 InsetCollapsable::toString(os);
324 }
325
326
327 void InsetBranch::forOutliner(docstring & os, size_t const maxlen,
328                                                           bool const shorten) const
329 {
330         if (isBranchActive())
331                 InsetCollapsable::forOutliner(os, maxlen, shorten);
332 }
333
334
335 void InsetBranch::validate(LaTeXFeatures & features) const
336 {
337         if (isBranchActive())
338                 InsetCollapsable::validate(features);
339 }
340
341
342 string InsetBranch::contextMenuName() const
343 {
344         return "context-branch";
345 }
346
347
348 bool InsetBranch::isMacroScope() const 
349 {
350         // Its own scope if not selected by buffer
351         return !isBranchActive();
352 }
353
354
355 string InsetBranch::params2string(InsetBranchParams const & params)
356 {
357         ostringstream data;
358         params.write(data);
359         return data.str();
360 }
361
362
363 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
364 {
365         params = InsetBranchParams();
366         if (in.empty())
367                 return;
368
369         istringstream data(in);
370         Lexer lex;
371         lex.setStream(data);
372         lex.setContext("InsetBranch::string2params");
373         params.read(lex);
374 }
375
376
377 void InsetBranch::addToToc(DocIterator const & cpit, bool output_active,
378                                                    UpdateType utype) const
379 {
380         DocIterator pit = cpit;
381         pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
382
383         docstring str;
384         text().forOutliner(str, TOC_ENTRY_LENGTH);
385         str = params_.branch + (params_.inverted ? " (-):" : ": ") + str;
386
387         shared_ptr<Toc> toc = buffer().tocBackend().toc("branch");
388         toc->push_back(TocItem(pit, 0, str, output_active));
389
390         // Proceed with the rest of the inset.
391         bool const doing_output = output_active && isBranchActive();
392         InsetCollapsable::addToToc(cpit, doing_output, utype);
393 }
394
395
396 void InsetBranchParams::write(ostream & os) const
397 {
398         os << to_utf8(branch) 
399            << '\n' 
400            << "inverted " 
401            << inverted;
402 }
403
404
405 void InsetBranchParams::read(Lexer & lex)
406 {
407         lex >> branch;
408         lex >> "inverted" >> inverted;
409 }
410
411 } // namespace lyx