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