]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Make the insets accept LFUN_INSET_SETTINGS. For these insets, LFUN_INSET_SETTINGS...
[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
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, InsetText::DefaultLayout), params_(params)
44 {}
45
46
47 InsetBranch::~InsetBranch()
48 {
49         hideDialogs("branch", this);
50 }
51
52
53 docstring InsetBranch::editMessage() const
54 {
55         return _("Opened Branch Inset");
56 }
57
58
59 void InsetBranch::write(ostream & os) const
60 {
61         params_.write(os);
62         InsetCollapsable::write(os);
63 }
64
65
66 void InsetBranch::read(Lexer & lex)
67 {
68         params_.read(lex);
69         InsetCollapsable::read(lex);
70 }
71
72
73 docstring InsetBranch::toolTip(BufferView const &, int, int) const
74 {
75         return _("Branch: ") + params_.branch;
76 }
77
78
79 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
80 {
81         docstring s = _("Branch: ") + params_.branch;
82         Buffer const & realbuffer = *buffer().masterBuffer();
83         BranchList const & branchlist = realbuffer.params().branchlist();
84         if (!branchlist.find(params_.branch))
85                 s = _("Branch (child only): ") + params_.branch;
86         if (!params_.branch.empty()) {
87                 // FIXME UNICODE
88                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
89                 if (c == Color_none)
90                         s = _("Undef: ") + s;
91         }
92         s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
93         if (decoration() == InsetLayout::CLASSIC)
94                 return isOpen(bv) ? s : getNewLabel(s);
95         else
96                 return 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_BRANCH_ACTIVATE:
143         case LFUN_BRANCH_DEACTIVATE: {
144                 // FIXME: I do not like this cast, but have no other idea...
145                 Buffer * realbuffer = const_cast<Buffer *>(buffer().masterBuffer());
146                 BranchList & branchlist = realbuffer->params().branchlist();
147                 Branch * ourBranch = branchlist.find(params_.branch);
148                 if (!ourBranch)
149                         break;
150                 ourBranch->setSelected(cmd.action == LFUN_BRANCH_ACTIVATE);
151                 break;
152         }
153
154         case LFUN_INSET_TOGGLE:
155                 if (cmd.argument() == "assign")
156                         setStatus(cur, isBranchSelected() ? Open : Collapsed);
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         case LFUN_INSET_SETTINGS:
175                 flag.setEnabled(true);
176                 break;
177
178         case LFUN_BRANCH_ACTIVATE:
179                 flag.setEnabled(!isBranchSelected());
180                 break;
181
182         case LFUN_BRANCH_DEACTIVATE:
183                 flag.setEnabled(isBranchSelected());
184                 break;
185
186         case LFUN_INSET_TOGGLE:
187                 if (cmd.argument() == "assign")
188                         flag.setEnabled(true);
189                 else
190                         return InsetCollapsable::getStatus(cur, cmd, flag);     
191
192         default:
193                 return InsetCollapsable::getStatus(cur, cmd, flag);
194         }
195         return true;
196 }
197
198
199 bool InsetBranch::isBranchSelected() const
200 {
201         Buffer const & realbuffer = *buffer().masterBuffer();
202         BranchList const & branchlist = realbuffer.params().branchlist();
203         Branch const * ourBranch = branchlist.find(params_.branch);
204         if (!ourBranch)
205                 return false;
206         return ourBranch->isSelected();
207 }
208
209
210 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
211 {
212         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
213 }
214
215
216 int InsetBranch::plaintext(odocstream & os,
217                            OutputParams const & runparams) const
218 {
219         if (!isBranchSelected())
220                 return 0;
221
222         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
223         InsetText::plaintext(os, runparams);
224         os << "\n]";
225
226         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
227 }
228
229
230 int InsetBranch::docbook(odocstream & os,
231                          OutputParams const & runparams) const
232 {
233         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
234 }
235
236
237 void InsetBranch::tocString(odocstream & os) const
238 {
239         if (isBranchSelected())
240                 InsetCollapsable::tocString(os);
241 }
242
243
244 void InsetBranch::validate(LaTeXFeatures & features) const
245 {
246         if (isBranchSelected())
247                 InsetCollapsable::validate(features);
248 }
249
250
251 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
252 {
253         return from_ascii("context-branch");
254 }
255
256
257 bool InsetBranch::isMacroScope() const 
258 {
259         // Its own scope if not selected by buffer
260         return !isBranchSelected();
261 }
262
263
264 string InsetBranch::params2string(InsetBranchParams const & params)
265 {
266         ostringstream data;
267         data << "branch" << ' ';
268         params.write(data);
269         return data.str();
270 }
271
272
273 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
274 {
275         params = InsetBranchParams();
276         if (in.empty())
277                 return;
278
279         istringstream data(in);
280         Lexer lex;
281         lex.setStream(data);
282         lex.setContext("InsetBranch::string2params");
283         lex >> "branch" >> "Branch";
284         params.read(lex);
285 }
286
287
288 void InsetBranch::addToToc(DocIterator const & cpit)
289 {
290         DocIterator pit = cpit;
291         pit.push_back(CursorSlice(*this));
292
293         Toc & toc = buffer().tocBackend().toc("branch");
294         docstring const str = params_.branch + ": " + text().getPar(0).asString();
295         toc.push_back(TocItem(pit, 0, str));
296         // Proceed with the rest of the inset.
297         InsetCollapsable::addToToc(cpit);
298 }
299
300
301 void InsetBranchParams::write(ostream & os) const
302 {
303         os << "Branch " << to_utf8(branch) << '\n';
304 }
305
306
307 void InsetBranchParams::read(Lexer & lex)
308 {
309         lex.eatLine();
310         branch = lex.getDocString();
311 }
312
313 } // namespace lyx