]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / insets / insetbranch.C
1 /**
2  * \file insetbranch.C
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 "Lsstream.h"
14
15 #include "insetbranch.h"
16 #include "gettext.h"
17 #include "lyxfont.h"
18 #include "language.h"
19 #include "buffer.h"
20 #include "BufferView.h"
21 #include "LaTeXFeatures.h"
22 #include "latexrunparams.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "insets/insettext.h"
26 #include "support/LOstream.h"
27 #include "support/lstrings.h"
28 #include "debug.h"
29
30 using std::ostream;
31 using std::auto_ptr;
32
33 void InsetBranch::init()
34 {
35         setInsetName("Branch");
36 }
37
38
39 InsetBranch::InsetBranch(BufferParams const & bp, string const & label)
40         : InsetCollapsable(bp)
41 {
42         params_.branch = label;
43         // Hack: stash the list of all allowable branch labels from this
44         // buffer into inset's parm list as a "stowaway":
45         params_.branchlist = bp.branchlist;
46         init();
47 }
48
49
50 InsetBranch::InsetBranch(InsetBranch const & in)
51         : InsetCollapsable(in), params_(in.params_)
52 {
53         init();
54 }
55
56
57 InsetBranch::~InsetBranch()
58 {
59         InsetBranchMailer mailer("branch", *this);
60         mailer.hideDialog();
61 }
62
63
64 auto_ptr<InsetBase> InsetBranch::clone() const
65 {
66         return auto_ptr<InsetBase>(new InsetBranch(*this));
67 }
68
69
70 string const InsetBranch::editMessage() const
71 {
72         return _("Opened Branch Inset");
73 }
74
75
76 void InsetBranch::write(Buffer const & buf, ostream & os) const
77 {
78         params_.write(os);
79         InsetCollapsable::write(buf, os);
80 }
81
82
83 void InsetBranch::read(Buffer const & buf, LyXLex & lex)
84 {
85         if (lex.isOK()) {
86                 lex.next();
87                 params_.branch = lex.getString();
88         }
89         InsetCollapsable::read(buf, lex);
90         setButtonLabel();
91 }
92
93
94 void InsetBranch::setButtonLabel()
95 {
96         LyXFont font(LyXFont::ALL_SANE);
97         font.decSize();
98         font.decSize();
99
100         setLabel(params_.branch);
101         font.setColor(LColor::foreground);
102         string const color = params_.branchlist.getColor(params_.branch);
103         if (!color.empty()) {
104                 setBackgroundColor(lcolor.getFromLyXName(params_.branch));
105         } else
106                 setBackgroundColor(LColor::background);
107         setLabelFont(font);
108 }
109
110
111 bool InsetBranch::showInsetDialog(BufferView * bv) const
112 {
113         InsetBranchMailer("branch", const_cast<InsetBranch &>(*this)).showDialog(bv);
114         return true;
115 }
116
117
118 dispatch_result InsetBranch::localDispatch(FuncRequest const & cmd)
119 {
120         BufferView * bv = cmd.view();
121         switch (cmd.action) {
122         case LFUN_INSET_MODIFY:
123                 {
124                 InsetBranchParams params;
125                 InsetBranchMailer::string2params(cmd.argument, params);
126                 params_.branch = params.branch;
127                 setButtonLabel();
128                 bv->updateInset(this);
129                 return DISPATCHED;
130                 }
131         case LFUN_INSET_EDIT:
132                 if (cmd.button() != mouse_button::button3)
133                         return InsetCollapsable::localDispatch(cmd);
134                 return UNDISPATCHED;
135         case LFUN_INSET_DIALOG_UPDATE:
136                 InsetBranchMailer("branch", *this).updateDialog(bv);
137                 return DISPATCHED;
138         case LFUN_MOUSE_RELEASE:
139                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
140                     InsetBranchMailer("branch", *this).showDialog(bv);
141                         return DISPATCHED;
142                 }
143                 // fallthrough:
144         default:
145                 return InsetCollapsable::localDispatch(cmd);
146         }
147 }
148
149
150
151 int InsetBranch::latex(Buffer const & buf, ostream & os,
152         LatexRunParams const & runparams) const
153 {
154         string const branch_sel = buf.params.branchlist.allSelected();
155         if (branch_sel.find(params_.branch, 0) != string::npos)
156                 return inset.latex(buf, os, runparams);
157         return 0;
158 }
159
160
161 int InsetBranch::linuxdoc(Buffer const &, std::ostream &) const
162 {
163         return 0;
164 }
165
166
167 int InsetBranch::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
168 {
169         // untested - MV
170         string const branch_sel = buf.params.branchlist.allSelected();
171         if (branch_sel.find(params_.branch, 0) != string::npos)
172                 return inset.docbook(buf, os, mixcont);
173         return 0;
174 }
175
176
177 int InsetBranch::ascii(Buffer const & buf, std::ostream & os, int ll) const
178 {
179         string const branch_sel = buf.params.branchlist.allSelected();
180         if (branch_sel.find(params_.branch, 0) != string::npos) {
181                 return inset.ascii(buf, os, ll);
182         }
183         return 0;
184 }
185
186
187 void InsetBranch::validate(LaTeXFeatures & features) const
188 {
189         inset.validate(features);
190 }
191
192
193
194 InsetBranchMailer::InsetBranchMailer(string const & name,
195                                                 InsetBranch & inset)
196         : name_(name), inset_(inset)
197 {
198 }
199
200
201 string const InsetBranchMailer::inset2string(Buffer const & buf) const
202 {
203         InsetBranchParams params = inset_.params();
204         params.branchlist = buf.params.branchlist;
205         inset_.setParams(params);
206         return params2string(name_, params);
207 }
208
209
210 string const InsetBranchMailer::params2string(string const & name,
211                                 InsetBranchParams const & params)
212 {
213         ostringstream data;
214         data << name << ' ';
215         params.write(data);
216         // Add all_branches parameter to data:
217         data << params.branchlist.allBranches() << "\n";
218         return STRCONV(data.str());
219 }
220
221
222 void InsetBranchMailer::string2params(string const & in,
223                                      InsetBranchParams & params)
224 {
225         params = InsetBranchParams();
226
227         if (in.empty())
228                 return;
229
230         istringstream data(STRCONV(in));
231         LyXLex lex(0,0);
232         lex.setStream(data);
233         params.read(lex);
234         // Process all_branches here:
235         if (lex.isOK()) {
236                 lex.next();
237                 params.branchlist.add(lex.getString());
238         }
239 }
240
241
242 void InsetBranchParams::write(ostream & os) const
243 {
244         os << "Branch" << " " << branch << "\n";
245 }
246
247
248 void InsetBranchParams::read(LyXLex & lex)
249 {
250         if (lex.isOK()) {
251                 lex.next();
252                 string token = lex.getString();
253         }
254         if (lex.isOK()) {
255                 lex.next();
256                 string token = lex.getString();
257         }
258         if (lex.isOK()) {
259                 lex.next();
260                 branch = lex.getString();
261         }
262 }