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