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