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