]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBranch.cpp
Fix bug 5851: Branch definitions not shown in Insert>Branch menu if master is set.
[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), 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 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         if (decoration() == InsetLayout::CLASSIC)
98                 return isOpen(bv) ? s : getNewLabel(s);
99         else
100                 return params_.branch + ": " + getNewLabel(s);
101 }
102
103
104 ColorCode InsetBranch::backgroundColor() const
105 {
106         if (params_.branch.empty())
107                 return Inset::backgroundColor();
108         // FIXME UNICODE
109         ColorCode c = lcolor.getFromLyXName(to_utf8(params_.branch));
110         if (c == Color_none)
111                 c = Color_error;
112         return c;
113 }
114
115
116 bool InsetBranch::showInsetDialog(BufferView * bv) const
117 {
118         bv->showDialog("branch", params2string(params()),
119                         const_cast<InsetBranch *>(this));
120         return true;
121 }
122
123
124 void InsetBranch::doDispatch(Cursor & cur, FuncRequest & cmd)
125 {
126         switch (cmd.action) {
127         case LFUN_INSET_MODIFY: {
128                 InsetBranchParams params;
129                 InsetBranch::string2params(to_utf8(cmd.argument()), params);
130                 params_.branch = params.branch;
131                 setLayout(cur.buffer()->params());
132                 break;
133         }
134
135         case LFUN_MOUSE_PRESS:
136                 if (cmd.button() != mouse_button::button3)
137                         InsetCollapsable::doDispatch(cur, cmd);
138                 else
139                         cur.undispatched();
140                 break;
141
142         case LFUN_INSET_DIALOG_UPDATE:
143                 cur.bv().updateDialog("branch", params2string(params()));
144                 break;
145
146         case LFUN_INSET_TOGGLE:
147                 if (cmd.argument() == "assign") {
148                         // The branch inset uses "assign".
149                         if (isBranchSelected()) {
150                                 if (status(cur.bv()) != Open)
151                                         setStatus(cur, Open);
152                                 else
153                                         cur.undispatched();
154                         } else {
155                                 if (status(cur.bv()) != Collapsed)
156                                         setStatus(cur, Collapsed);
157                                 else
158                                         cur.undispatched();
159                         }
160                 }
161                 else
162                         InsetCollapsable::doDispatch(cur, cmd);
163                 break;
164
165         default:
166                 InsetCollapsable::doDispatch(cur, cmd);
167                 break;
168         }
169 }
170
171
172 bool InsetBranch::getStatus(Cursor & cur, FuncRequest const & cmd,
173                 FuncStatus & flag) const
174 {
175         switch (cmd.action) {
176         case LFUN_INSET_MODIFY:
177         case LFUN_INSET_DIALOG_UPDATE:
178                 flag.setEnabled(true);
179                 break;
180
181         case LFUN_INSET_TOGGLE:
182                 if (cmd.argument() == "open" || cmd.argument() == "close" ||
183                     cmd.argument() == "toggle")
184                         flag.setEnabled(true);
185                 else if (cmd.argument() == "assign" || cmd.argument().empty()) {
186                         if (isBranchSelected())
187                                 flag.setEnabled(status(cur.bv()) != Open);
188                         else
189                                 flag.setEnabled(status(cur.bv()) != Collapsed);
190                 } else
191                         flag.setEnabled(true);
192                 break;
193
194         default:
195                 return InsetCollapsable::getStatus(cur, cmd, flag);
196         }
197         return true;
198 }
199
200
201 bool InsetBranch::isBranchSelected() const
202 {
203         Buffer const & realbuffer = *buffer().masterBuffer();
204         BranchList const & branchlist = realbuffer.params().branchlist();
205         Branch const * ourBranch = branchlist.find(params_.branch);
206         if (!ourBranch)
207                 return false;
208         return ourBranch->isSelected();
209 }
210
211
212 int InsetBranch::latex(odocstream & os, OutputParams const & runparams) const
213 {
214         return isBranchSelected() ?  InsetText::latex(os, runparams) : 0;
215 }
216
217
218 int InsetBranch::plaintext(odocstream & os,
219                            OutputParams const & runparams) const
220 {
221         if (!isBranchSelected())
222                 return 0;
223
224         os << '[' << buffer().B_("branch") << ' ' << params_.branch << ":\n";
225         InsetText::plaintext(os, runparams);
226         os << "\n]";
227
228         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
229 }
230
231
232 int InsetBranch::docbook(odocstream & os,
233                          OutputParams const & runparams) const
234 {
235         return isBranchSelected() ?  InsetText::docbook(os, runparams) : 0;
236 }
237
238
239 void InsetBranch::tocString(odocstream & os) const
240 {
241         if (isBranchSelected())
242                 InsetCollapsable::tocString(os);
243 }
244
245
246 void InsetBranch::validate(LaTeXFeatures & features) const
247 {
248         if (isBranchSelected())
249                 InsetCollapsable::validate(features);
250 }
251
252
253 bool InsetBranch::isMacroScope() const 
254 {
255         // Its own scope if not selected by buffer
256         return !isBranchSelected();
257 }
258
259
260 string InsetBranch::params2string(InsetBranchParams const & params)
261 {
262         ostringstream data;
263         data << "branch" << ' ';
264         params.write(data);
265         return data.str();
266 }
267
268
269 void InsetBranch::string2params(string const & in, InsetBranchParams & params)
270 {
271         params = InsetBranchParams();
272         if (in.empty())
273                 return;
274
275         istringstream data(in);
276         Lexer lex;
277         lex.setStream(data);
278         lex.setContext("InsetBranch::string2params");
279         lex >> "branch" >> "Branch";
280         params.read(lex);
281 }
282
283
284 void InsetBranch::addToToc(DocIterator const & cpit)
285 {
286         DocIterator pit = cpit;
287         pit.push_back(CursorSlice(*this));
288
289         Toc & toc = buffer().tocBackend().toc("branch");
290         docstring const str = params_.branch + ": " + text().getPar(0).asString();
291         toc.push_back(TocItem(pit, 0, str));
292         // Proceed with the rest of the inset.
293         InsetCollapsable::addToToc(cpit);
294 }
295
296
297 void InsetBranchParams::write(ostream & os) const
298 {
299         os << "Branch " << to_utf8(branch) << '\n';
300 }
301
302
303 void InsetBranchParams::read(Lexer & lex)
304 {
305         lex.eatLine();
306         branch = lex.getDocString();
307 }
308
309 } // namespace lyx