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