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