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