]> 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 &, int, int) const
75 {
76         docstring const status = isBranchSelected() ? 
77                 _("active") : _("non-active");
78         return support::bformat(_("Branch (%1$s): %2$s"),
79                                   status,
80                                   params_.branch);
81 }
82
83
84 docstring const InsetBranch::buttonLabel(BufferView const & bv) const
85 {
86         docstring s = _("Branch: ") + params_.branch;
87         Buffer const & realbuffer = *buffer().masterBuffer();
88         BranchList const & branchlist = realbuffer.params().branchlist();
89         if (!branchlist.find(params_.branch))
90                 s = _("Branch (child only): ") + params_.branch;
91         if (!params_.branch.empty()) {
92                 // FIXME UNICODE
93                 ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
94                 if (c == Color_none)
95                         s = _("Undef: ") + s;
96         }
97         s = char_type(isBranchSelected() ? 0x2714 : 0x2716) + s;
98         if (decoration() == InsetLayout::CLASSIC)
99                 return isOpen(bv) ? s : getNewLabel(s);
100         else
101                 return params_.branch + ": " + getNewLabel(s);
102 }
103
104
105 ColorCode InsetBranch::backgroundColor() const
106 {
107         if (params_.branch.empty())
108                 return Inset::backgroundColor();
109         // FIXME UNICODE
110         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
111         if (c == Color_none)
112                 c = Color_error;
113         return c;
114 }
115
116
117 bool InsetBranch::showInsetDialog(BufferView * bv) const
118 {
119         bv->showDialog("branch", params2string(params()),
120                         const_cast<InsetBranch *>(this));
121         return true;
122 }
123
124
125 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
126 {
127         switch (cmd.action) {
128         case LFUN_INSET_MODIFY: {
129                 InsetBranchParams params;
130                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
131                 params_.branch = params.branch;
132                 setLayout(cur.buffer()->params());
133                 break;
134         }
135
136         case LFUN_MOUSE_PRESS:
137                 if (cmd.button() != mouse_button::button3)
138                         InsetCollapsable::doDispatch(cur, cmd);
139                 else
140                         cur.undispatched();
141                 break;
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 * realbuffer = const_cast<Buffer *>(buffer().masterBuffer());
151                 BranchList & branchlist = realbuffer->params().branchlist();
152                 Branch * ourBranch = branchlist.find(params_.branch);
153                 if (!ourBranch)
154                         break;
155                 ourBranch->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         case LFUN_INSET_SETTINGS:
180                 flag.setEnabled(true);
181                 break;
182
183         case LFUN_BRANCH_ACTIVATE:
184                 flag.setEnabled(!isBranchSelected());
185                 break;
186
187         case LFUN_BRANCH_DEACTIVATE:
188                 flag.setEnabled(isBranchSelected());
189                 break;
190
191         case LFUN_INSET_TOGGLE:
192                 if (cmd.argument() == "assign")
193                         flag.setEnabled(true);
194                 else
195                         return InsetCollapsable::getStatus(cur, cmd, flag);     
196
197         default:
198                 return InsetCollapsable::getStatus(cur, cmd, flag);
199         }
200         return true;
201 }
202
203
204 bool InsetBranch::isBranchSelected() const
205 {
206         Buffer const & realbuffer = *buffer().masterBuffer();
207         BranchList const & branchlist = realbuffer.params().branchlist();
208         Branch const * ourBranch = branchlist.find(params_.branch);
209         if (!ourBranch)
210                 return false;
211         return ourBranch->isSelected();
212 }
213
214
215 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
216 {
217         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
218 }
219
220
221 int InsetBranch::plaintext(odocstream & os,
222                            OutputParams const & runparams) const
223 {
224         if (!isBranchSelected())
225                 return 0;
226
227         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
228         InsetText::plaintext(os, runparams);
229         os << "\n]";
230
231         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
232 }
233
234
235 int InsetBranch::docbook(odocstream & os,
236                          OutputParams const & runparams) const
237 {
238         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
239 }
240
241
242 void InsetBranch::tocString(odocstream & os) const
243 {
244         if (isBranchSelected())
245                 InsetCollapsable::tocString(os);
246 }
247
248
249 void InsetBranch::validate(LaTeXFeatures & features) const
250 {
251         if (isBranchSelected())
252                 InsetCollapsable::validate(features);
253 }
254
255
256 docstring InsetBranch::contextMenu(BufferView const &, int, int) const
257 {
258         return from_ascii("context-branch");
259 }
260
261
262 bool InsetBranch::isMacroScope() const 
263 {
264         // Its own scope if not selected by buffer
265         return !isBranchSelected();
266 }
267
268
269 string InsetBranch::params2string(InsetBranchParams const & params)
270 {
271         ostringstream data;
272         data << "branch" << ' ';
273         params.write(data);
274         return data.str();
275 }
276
277
278 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
279 {
280         params = InsetBranchParams();
281         if (in.empty())
282                 return;
283
284         istringstream data(in);
285         Lexer lex;
286         lex.setStream(data);
287         lex.setContext("InsetBranch::string2params");
288         lex >> "branch" >> "Branch";
289         params.read(lex);
290 }
291
292
293 void InsetBranch::addToToc(DocIterator const & cpit)
294 {
295         DocIterator pit = cpit;
296         pit.push_back(CursorSlice(*this));
297
298         Toc & toc = buffer().tocBackend().toc("branch");
299         docstring const str = params_.branch + ": " + text().getPar(0).asString();
300         toc.push_back(TocItem(pit, 0, str));
301         // Proceed with the rest of the inset.
302         InsetCollapsable::addToToc(cpit);
303 }
304
305
306 void InsetBranchParams::write(ostream & os) const
307 {
308         os << "Branch " << to_utf8(branch) << '\n';
309 }
310
311
312 void InsetBranchParams::read(Lexer & lex)
313 {
314         lex.eatLine();
315         branch = lex.getDocString();
316 }
317
318 } // namespace lyx