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