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