]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
061d9dac3e573ae1a0b63650b0a1a8d1a98aae7d
[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 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                 s = _("Branch (child only): ") + params_.branch;
95         if (!params_.branch.empty()) {
96                 // FIXME UNICODE
97                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
98                 if (c == Color_none)
99                         s = _("Undef: ") + s;
100         }
101         s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
102         if (decoration() == InsetLayout::CLASSIC)
103                 return isOpen(bv) ? s : getNewLabel(s);
104         else
105                 return params_.branch + ": " + getNewLabel(s);
106 }
107
108
109 ColorCode InsetBranch::backgroundColor() const
110 {
111         if (params_.branch.empty())
112                 return Inset::backgroundColor();
113         // FIXME UNICODE
114         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
115         if (c == Color_none)
116                 c = Color_error;
117         return c;
118 }
119
120
121 bool InsetBranch::showInsetDialog(BufferView * bv) const
122 {
123         bv->showDialog("branch", params2string(params()),
124                         const_cast<InsetBranch *>(this));
125         return true;
126 }
127
128
129 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
130 {
131         switch (cmd.action) {
132         case LFUN_INSET_MODIFY: {
133                 InsetBranchParams params;
134                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
135                 params_.branch = params.branch;
136                 setLayout(cur.buffer()->params());
137                 break;
138         }
139
140         case LFUN_INSET_DIALOG_UPDATE:
141                 cur.bv().updateDialog("branch", params2string(params()));
142                 break;
143
144         case LFUN_BRANCH_ACTIVATE:
145         case LFUN_BRANCH_DEACTIVATE: {
146                 // FIXME: I do not like this cast, but have no other idea...
147                 Buffer const * buf = buffer().masterBuffer();
148                 BranchList const & branchlist = buf->params().branchlist();
149                 Branch * our_branch = const_cast<Branch *>(branchlist.find(params_.branch));
150                 if (!our_branch)
151                         break;
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         if (!ourBranch)
206                 return false;
207         return ourBranch->isSelected();
208 }
209
210
211 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
212 {
213         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
214 }
215
216
217 int InsetBranch::plaintext(odocstream & os,
218                            OutputParams const & runparams) const
219 {
220         if (!isBranchSelected())
221                 return 0;
222
223         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
224         InsetText::plaintext(os, runparams);
225         os << "\n]";
226
227         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
228 }
229
230
231 int InsetBranch::docbook(odocstream & os,
232                          OutputParams const & runparams) const
233 {
234         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
235 }
236
237
238 void InsetBranch::tocString(odocstream & os) const
239 {
240         if (isBranchSelected())
241                 InsetCollapsable::tocString(os);
242 }
243
244
245 void InsetBranch::validate(LaTeXFeatures & features) const
246 {
247         if (isBranchSelected())
248                 InsetCollapsable::validate(features);
249 }
250
251
252 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
253 {
254         return from_ascii("context-branch");
255 }
256
257
258 bool InsetBranch::isMacroScope() const 
259 {
260         // Its own scope if not selected by buffer
261         return !isBranchSelected();
262 }
263
264
265 string InsetBranch::params2string(InsetBranchParams const & params)
266 {
267         ostringstream data;
268         data << "branch" << ' ';
269         params.write(data);
270         return data.str();
271 }
272
273
274 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
275 {
276         params = InsetBranchParams();
277         if (in.empty())
278                 return;
279
280         istringstream data(in);
281         Lexer lex;
282         lex.setStream(data);
283         lex.setContext("InsetBranch::string2params");
284         lex >> "branch" >> "Branch";
285         params.read(lex);
286 }
287
288
289 void InsetBranch::addToToc(DocIterator const & cpit)
290 {
291         DocIterator pit = cpit;
292         pit.push_back(CursorSlice(*this));
293
294         Toc & toc = buffer().tocBackend().toc("branch");
295         docstring const str = params_.branch + ": " + text().getPar(0).asString();
296         toc.push_back(TocItem(pit, 0, str));
297         // Proceed with the rest of the inset.
298         InsetCollapsable::addToToc(cpit);
299 }
300
301
302 void InsetBranchParams::write(ostream & os) const
303 {
304         os << "Branch " << to_utf8(branch) << '\n';
305 }
306
307
308 void InsetBranchParams::read(Lexer & lex)
309 {
310         lex.eatLine();
311         branch = lex.getDocString();
312 }
313
314 } // namespace lyx