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