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