]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
Final touch 'inset display()'; fix 'is a bit silly' bug
[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
120 InsetBranch::priv_dispatch(FuncRequest const & cmd,
121                            idx_type & idx, pos_type & pos)
122 {
123         BufferView * bv = cmd.view();
124         switch (cmd.action) {
125         case LFUN_INSET_MODIFY:
126                 {
127                 InsetBranchParams params;
128                 InsetBranchMailer::string2params(cmd.argument, params);
129                 params_.branch = params.branch;
130                 setButtonLabel();
131                 bv->updateInset(this);
132                 return DISPATCHED;
133                 }
134         case LFUN_INSET_EDIT:
135                 if (cmd.button() != mouse_button::button3)
136                         return InsetCollapsable::priv_dispatch(cmd, idx, pos);
137
138                 return UNDISPATCHED;
139         case LFUN_INSET_DIALOG_UPDATE:
140                 InsetBranchMailer("branch", *this).updateDialog(bv);
141                 return DISPATCHED;
142         case LFUN_MOUSE_RELEASE:
143                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
144                     InsetBranchMailer("branch", *this).showDialog(bv);
145                         return DISPATCHED;
146                 }
147                 // fallthrough:
148         default:
149                 return InsetCollapsable::priv_dispatch(cmd, idx, pos);
150         }
151 }
152
153
154 int InsetBranch::latex(Buffer const & buf, ostream & os,
155         LatexRunParams const & runparams) const
156 {
157         string const branch_sel = buf.params().branchlist().allSelected();
158         if (branch_sel.find(params_.branch, 0) != string::npos)
159                 return inset.latex(buf, os, runparams);
160         return 0;
161 }
162
163
164 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os) const
165 {
166         string const branch_sel = buf.params().branchlist().allSelected();
167         if (branch_sel.find(params_.branch, 0) != string::npos)
168                 return inset.linuxdoc(buf, os);
169         return 0;
170 }
171
172
173 int InsetBranch::docbook(Buffer const & buf, std::ostream & os, bool mixcont) const
174 {
175         string const branch_sel = buf.params().branchlist().allSelected();
176         if (branch_sel.find(params_.branch, 0) != string::npos)
177                 return inset.docbook(buf, os, mixcont);
178         return 0;
179 }
180
181
182 int InsetBranch::ascii(Buffer const & buf, std::ostream & os, int ll) const
183 {
184         string const branch_sel = buf.params().branchlist().allSelected();
185         if (branch_sel.find(params_.branch, 0) != string::npos) {
186                 return inset.ascii(buf, os, ll);
187         }
188         return 0;
189 }
190
191
192 void InsetBranch::validate(LaTeXFeatures & features) const
193 {
194         inset.validate(features);
195 }
196
197
198
199 InsetBranchMailer::InsetBranchMailer(string const & name,
200                                                 InsetBranch & inset)
201         : name_(name), inset_(inset)
202 {
203 }
204
205
206 string const InsetBranchMailer::inset2string(Buffer const & buf) const
207 {
208         InsetBranchParams params = inset_.params();
209         params.branchlist = buf.params().branchlist();
210         inset_.setParams(params);
211         return params2string(name_, params);
212 }
213
214
215 string const InsetBranchMailer::params2string(string const & name,
216                                               InsetBranchParams const & params)
217 {
218         ostringstream data;
219         data << name << ' ';
220         params.write(data);
221         // Add all_branches parameter to data:
222         data << params.branchlist.allBranches() << "\n";
223         return data.str();
224 }
225
226
227 void InsetBranchMailer::string2params(string const & in,
228                                      InsetBranchParams & params)
229 {
230         params = InsetBranchParams();
231
232         if (in.empty())
233                 return;
234
235         istringstream data(in);
236         LyXLex lex(0,0);
237         lex.setStream(data);
238         params.read(lex);
239         // Process all_branches here:
240         if (lex.isOK()) {
241                 lex.next();
242                 params.branchlist.add(lex.getString());
243         }
244 }
245
246
247 void InsetBranchParams::write(ostream & os) const
248 {
249         os << "Branch" << " " << branch << "\n";
250 }
251
252
253 void InsetBranchParams::read(LyXLex & lex)
254 {
255         if (lex.isOK()) {
256                 lex.next();
257                 string token = lex.getString();
258         }
259         if (lex.isOK()) {
260                 lex.next();
261                 string token = lex.getString();
262         }
263         if (lex.isOK()) {
264                 lex.next();
265                 branch = lex.getString();
266         }
267 }