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