]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
tex2lyx/text.cpp: fix typos
[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 "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/Application.h"
36
37 #include <sstream>
38
39 using namespace std;
40
41
42 namespace lyx {
43
44 InsetBranch::InsetBranch(Buffer * buf, InsetBranchParams const & params)
45         : InsetCollapsable(buf, InsetText::DefaultLayout), params_(params)
46 {}
47
48
49 void InsetBranch::write(ostream & os) const
50 {
51         os << "Branch ";
52         params_.write(os);
53         os << '\n';
54         InsetCollapsable::write(os);
55 }
56
57
58 void InsetBranch::read(Lexer & lex)
59 {
60         params_.read(lex);
61         InsetCollapsable::read(lex);
62 }
63
64
65 docstring InsetBranch::toolTip(BufferView const & bv, int, int) const
66 {
67         docstring const status = isBranchSelected() ? 
68                 _("active") : _("non-active");
69         docstring const heading = 
70                 support::bformat(_("Branch (%1$s): %2$s"), status, params_.branch);
71         if (isOpen(bv))
72                 return heading;
73         return toolTipText(heading + from_ascii("\n"));
74 }
75
76
77 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
78 {
79         docstring s = _("Branch: ") + params_.branch;
80         Buffer const & realbuffer = *buffer().masterBuffer();
81         BranchList const & branchlist = realbuffer.params().branchlist();
82         if (!branchlist.find(params_.branch)
83             && buffer().params().branchlist().find(params_.branch))
84                 s = _("Branch (child only): ") + params_.branch;
85         else if (!branchlist.find(params_.branch))
86                 s = _("Branch (undefined): ") + params_.branch;
87         if (!params_.branch.empty()) {
88                 // FIXME UNICODE
89                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
90                 if (c == Color_none)
91                         s = _("Undef: ") + s;
92         }
93         s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
94         if (decoration() == InsetLayout::CLASSIC)
95                 return isOpen(bv) ? s : getNewLabel(s);
96         else
97                 return params_.branch + ": " + getNewLabel(s);
98 }
99
100
101 ColorCode InsetBranch::backgroundColor(PainterInfo const & pi) const
102 {
103         if (params_.branch.empty())
104                 return Inset::backgroundColor(pi);
105         // FIXME UNICODE
106         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
107         if (c == Color_none)
108                 c = Color_error;
109         return c;
110 }
111
112
113 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
114 {
115         switch (cmd.action()) {
116         case LFUN_INSET_MODIFY: {
117                 InsetBranchParams params;
118                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
119
120                 cur.recordUndoInset(ATOMIC_UNDO, this);
121                 params_.branch = params.branch;
122                 // what we really want here is a TOC update, but that means
123                 // a full buffer update
124                 cur.forceBufferUpdate();
125                 break;
126         }
127         case LFUN_BRANCH_ACTIVATE:
128         case LFUN_BRANCH_DEACTIVATE: {
129                 Buffer * buf = const_cast<Buffer *>(buffer().masterBuffer());
130                 // is the branch in our master buffer?
131                 bool branch_in_master = (buf != &buffer());
132
133                 Branch * our_branch = buf->params().branchlist().find(params_.branch);
134                 if (branch_in_master && !our_branch) {
135                         // child only?
136                         our_branch = buffer().params().branchlist().find(params_.branch);
137                         if (!our_branch)
138                                 break;
139                         branch_in_master = false;
140                 }
141                 bool const activate = (cmd.action() == LFUN_BRANCH_ACTIVATE);
142                 if (our_branch->isSelected() != activate) {
143                         // FIXME If the branch is in the master document, we cannot
144                         // call recordUndo..., becuase the master may be hidden, and
145                         // the code presently assumes that hidden documents can never
146                         // be dirty. See GuiView::closeBufferAll(), for example.
147                         if (!branch_in_master)
148                                 buffer().undo().recordUndoFullDocument(cur);
149                         our_branch->setSelected(activate);
150                         cur.forceBufferUpdate();
151                 }
152                 break;
153         }
154         case LFUN_INSET_TOGGLE:
155                 if (cmd.argument() == "assign")
156                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
157                 else
158                         InsetCollapsable::doDispatch(cur, cmd);
159                 break;
160
161         default:
162                 InsetCollapsable::doDispatch(cur, cmd);
163                 break;
164         }
165 }
166
167
168 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
169                 FuncStatus & flag) const
170 {
171         switch (cmd.action()) {
172         case LFUN_INSET_MODIFY:
173                 flag.setEnabled(true);
174                 break;
175
176         case LFUN_BRANCH_ACTIVATE:
177                 flag.setEnabled(!isBranchSelected());
178                 break;
179
180         case LFUN_BRANCH_DEACTIVATE:
181                 flag.setEnabled(isBranchSelected());
182                 break;
183
184         case LFUN_INSET_TOGGLE:
185                 if (cmd.argument() == "assign")
186                         flag.setEnabled(true);
187                 else
188                         return InsetCollapsable::getStatus(cur, cmd, flag);     
189                 break;
190
191         default:
192                 return InsetCollapsable::getStatus(cur, cmd, flag);
193         }
194         return true;
195 }
196
197
198 bool InsetBranch::isBranchSelected() const
199 {
200         Buffer const & realbuffer = *buffer().masterBuffer();
201         BranchList const & branchlist = realbuffer.params().branchlist();
202         Branch const * ourBranch = branchlist.find(params_.branch);
203
204         if (!ourBranch) {
205                 // this branch is defined in child only
206                 ourBranch = buffer().params().branchlist().find(params_.branch);
207                 if (!ourBranch)
208                         return false;
209         }
210         return ourBranch->isSelected();
211 }
212
213
214 void InsetBranch::latex(otexstream & os, OutputParams const & runparams) const
215 {
216         if (isBranchSelected())
217                 InsetText::latex(os, runparams);
218 }
219
220
221 int InsetBranch::plaintext(odocstream & os,
222                            OutputParams const & runparams) const
223 {
224         if (!isBranchSelected())
225                 return 0;
226
227         int len = InsetText::plaintext(os, runparams);
228         return len;
229 }
230
231
232 int InsetBranch::docbook(odocstream & os,
233                          OutputParams const & runparams) const
234 {
235         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
236 }
237
238
239 docstring InsetBranch::xhtml(XHTMLStream & xs, OutputParams const & rp) const
240 {
241         if (isBranchSelected()) {
242                 OutputParams newrp = rp;
243                 newrp.par_begin = 0;
244                 newrp.par_end = text().paragraphs().size();
245                 xhtmlParagraphs(text(), buffer(), xs, newrp);
246         }
247         return docstring();
248 }
249
250
251 void InsetBranch::toString(odocstream & os) const
252 {
253         if (isBranchSelected())
254                 InsetCollapsable::toString(os);
255 }
256
257
258 void InsetBranch::forToc(docstring & os, size_t maxlen) const
259 {
260         if (isBranchSelected())
261                 InsetCollapsable::forToc(os, maxlen);
262 }
263
264
265 void InsetBranch::validate(LaTeXFeatures & features) const
266 {
267         if (isBranchSelected())
268                 InsetCollapsable::validate(features);
269 }
270
271
272 string InsetBranch::contextMenuName() const
273 {
274         return "context-branch";
275 }
276
277
278 bool InsetBranch::isMacroScope() const 
279 {
280         // Its own scope if not selected by buffer
281         return !isBranchSelected();
282 }
283
284
285 string InsetBranch::params2string(InsetBranchParams const & params)
286 {
287         ostringstream data;
288         params.write(data);
289         return data.str();
290 }
291
292
293 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
294 {
295         params = InsetBranchParams();
296         if (in.empty())
297                 return;
298
299         istringstream data(in);
300         Lexer lex;
301         lex.setStream(data);
302         lex.setContext("InsetBranch::string2params");
303         params.read(lex);
304 }
305
306
307 void InsetBranch::addToToc(DocIterator const & cpit) const
308 {
309         DocIterator pit = cpit;
310         pit.push_back(CursorSlice(const_cast<InsetBranch &>(*this)));
311
312         Toc & toc = buffer().tocBackend().toc("branch");
313         docstring str = params_.branch + ": ";
314         text().forToc(str, TOC_ENTRY_LENGTH);
315         toc.push_back(TocItem(pit, 0, str, toolTipText(docstring(), 3, 60)));
316         // Proceed with the rest of the inset.
317         InsetCollapsable::addToToc(cpit);
318 }
319
320
321 void InsetBranchParams::write(ostream & os) const
322 {
323         os << to_utf8(branch);
324 }
325
326
327 void InsetBranchParams::read(Lexer & lex)
328 {
329         lex.eatLine();
330         branch = lex.getDocString();
331 }
332
333 } // namespace lyx