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