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