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