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