]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
This is a conservative fix for the layout setting bug discussed in
[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 "Color.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
33 #include "frontends/Application.h"
34
35 #include <sstream>
36
37 using namespace std;
38
39
40 namespace lyx {
41
42 InsetBranch::InsetBranch(Buffer const & buf, InsetBranchParams const & params)
43         : InsetCollapsable(buf), params_(params)
44 {
45         // override the default for InsetCollapsable, which is to
46         // use the plain layout.
47         DocumentClass const & dc = buf.params().documentClass();
48         paragraphs().back().setDefaultLayout(dc);
49 }
50
51
52 InsetBranch::~InsetBranch()
53 {
54         hideDialogs("branch", this);
55 }
56
57
58 docstring InsetBranch::editMessage() const
59 {
60         return _("Opened Branch Inset");
61 }
62
63
64 void InsetBranch::write(ostream & os) const
65 {
66         params_.write(os);
67         InsetCollapsable::write(os);
68 }
69
70
71 void InsetBranch::read(Lexer & lex)
72 {
73         params_.read(lex);
74         InsetCollapsable::read(lex);
75 }
76
77
78 docstring InsetBranch::toolTip(BufferView const &, int, int) const
79 {
80         return _("Branch: ") + params_.branch;
81 }
82
83
84 void InsetBranch::setButtonLabel()
85 {
86         docstring s = _("Branch: ") + 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         if (decoration() == InsetLayout::CLASSIC)
94                 setLabel(isOpen() ? s : getNewLabel(s) );
95         else
96                 setLabel(params_.branch + ": " + getNewLabel(s));
97 }
98
99
100 ColorCode InsetBranch::backgroundColor() const
101 {
102         if (params_.branch.empty())
103                 return Inset::backgroundColor();
104         // FIXME UNICODE
105         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
106         if (c == Color_none)
107                 c = Color_error;
108         return c;
109 }
110
111
112 bool InsetBranch::showInsetDialog(BufferView * bv) const
113 {
114         bv->showDialog("branch", params2string(params()),
115                         const_cast<InsetBranch *>(this));
116         return true;
117 }
118
119
120 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
121 {
122         switch (cmd.action) {
123         case LFUN_INSET_MODIFY: {
124                 InsetBranchParams params;
125                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
126                 params_.branch = params.branch;
127                 setLayout(cur.buffer().params());
128                 break;
129         }
130
131         case LFUN_MOUSE_PRESS:
132                 if (cmd.button() != mouse_button::button3)
133                         InsetCollapsable::doDispatch(cur, cmd);
134                 else
135                         cur.undispatched();
136                 break;
137
138         case LFUN_INSET_DIALOG_UPDATE:
139                 cur.bv().updateDialog("branch", params2string(params()));
140                 break;
141
142         case LFUN_INSET_TOGGLE:
143                 if (cmd.argument() == "assign") {
144                         // The branch inset uses "assign".
145                         if (isBranchSelected()) {
146                                 if (status() != Open)
147                                         setStatus(cur, Open);
148                                 else
149                                         cur.undispatched();
150                         } else {
151                                 if (status() != Collapsed)
152                                         setStatus(cur, Collapsed);
153                                 else
154                                         cur.undispatched();
155                         }
156                 }
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         case LFUN_INSET_DIALOG_UPDATE:
174                 flag.setEnabled(true);
175                 break;
176
177         case LFUN_INSET_TOGGLE:
178                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
179                     cmd.argument() == "toggle")
180                         flag.setEnabled(true);
181                 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
182                         if (isBranchSelected())
183                                 flag.setEnabled(status() != Open);
184                         else
185                                 flag.setEnabled(status() != Collapsed);
186                 } else
187                         flag.setEnabled(true);
188                 break;
189
190         default:
191                 return InsetCollapsable::getStatus(cur, cmd, flag);
192         }
193         return true;
194 }
195
196
197 bool InsetBranch::isBranchSelected() const
198 {
199         Buffer const & realbuffer = *buffer().masterBuffer();
200         BranchList const & branchlist = realbuffer.params().branchlist();
201         Branch const * ourBranch = branchlist.find(params_.branch);
202         if (!ourBranch)
203                 return false;
204         return ourBranch->isSelected();
205 }
206
207
208 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
209 {
210         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
211 }
212
213
214 int InsetBranch::plaintext(odocstream & os,
215                            OutputParams const & runparams) const
216 {
217         if (!isBranchSelected())
218                 return 0;
219
220         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
221         InsetText::plaintext(os, runparams);
222         os << "\n]";
223
224         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
225 }
226
227
228 int InsetBranch::docbook(odocstream & os,
229                          OutputParams const & runparams) const
230 {
231         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
232 }
233
234
235 void InsetBranch::textString(odocstream & os) const
236 {
237         if (isBranchSelected())
238                 os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
239 }
240
241
242 void InsetBranch::validate(LaTeXFeatures & features) const
243 {
244         InsetText::validate(features);
245 }
246
247
248 bool InsetBranch::isMacroScope() const 
249 {
250         // Its own scope if not selected by buffer
251         return !isBranchSelected();
252 }
253
254
255 string InsetBranch::params2string(InsetBranchParams const & params)
256 {
257         ostringstream data;
258         data << "branch" << ' ';
259         params.write(data);
260         return data.str();
261 }
262
263
264 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
265 {
266         params = InsetBranchParams();
267         if (in.empty())
268                 return;
269
270         istringstream data(in);
271         Lexer lex;
272         lex.setStream(data);
273         lex.setContext("InsetBranch::string2params");
274         lex >> "branch" >> "Branch";
275         params.read(lex);
276 }
277
278
279 void InsetBranch::addToToc(DocIterator const & cpit)
280 {
281         DocIterator pit = cpit;
282         pit.push_back(CursorSlice(*this));
283
284         Toc & toc = buffer().tocBackend().toc("branch");
285         docstring const str = params_.branch + ": " + text().getPar(0).asString();
286         toc.push_back(TocItem(pit, 0, str));
287         // Proceed with the rest of the inset.
288         InsetCollapsable::addToToc(cpit);
289 }
290
291
292 void InsetBranchParams::write(ostream & os) const
293 {
294         os << "Branch " << to_utf8(branch) << '\n';
295 }
296
297
298 void InsetBranchParams::read(Lexer & lex)
299 {
300         lex >> branch;
301 }
302
303 } // namespace lyx