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