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