]> git.lyx.org Git - lyx.git/blob - src/insets/insetbranch.C
Rename getOutOfInset as leaveInset.
[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 "bufferparams.h"
18 #include "BranchList.h"
19 #include "cursor.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "LColor.h"
24 #include "lyxlex.h"
25 #include "paragraph.h"
26
27 #include "support/std_sstream.h"
28
29 using std::string;
30 using std::auto_ptr;
31 using std::istringstream;
32 using std::ostream;
33 using std::ostringstream;
34
35
36 void InsetBranch::init()
37 {
38         setInsetName("Branch");
39         setButtonLabel();
40 }
41
42
43 InsetBranch::InsetBranch(BufferParams const & bp,
44                          InsetBranchParams const & params)
45         : InsetCollapsable(bp), params_(params)
46 {
47         init();
48 }
49
50
51 InsetBranch::InsetBranch(InsetBranch const & in)
52         : InsetCollapsable(in), params_(in.params_)
53 {
54         init();
55 }
56
57
58 InsetBranch::~InsetBranch()
59 {
60         InsetBranchMailer(*this).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         params_.read(lex);
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         if (!params_.branch.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(const_cast<InsetBranch &>(*this)).showDialog(bv);
110         return true;
111 }
112
113
114 void InsetBranch::priv_dispatch(LCursor & cur, FuncRequest & cmd)
115 {
116         switch (cmd.action) {
117         case LFUN_INSET_MODIFY: {
118                 InsetBranchParams params;
119                 InsetBranchMailer::string2params(cmd.argument, params);
120                 params_.branch = params.branch;
121                 setButtonLabel();
122                 break;
123         }
124
125         case LFUN_MOUSE_PRESS:
126                 if (cmd.button() != mouse_button::button3)
127                         InsetCollapsable::priv_dispatch(cur, cmd);
128                 else
129                         cur.undispatched();
130                 break;
131
132         case LFUN_INSET_DIALOG_UPDATE:
133                 InsetBranchMailer(*this).updateDialog(&cur.bv());
134                 break;
135
136         case LFUN_MOUSE_RELEASE:
137                 if (cmd.button() == mouse_button::button3 && hitButton(cmd))
138                         InsetBranchMailer(*this).showDialog(&cur.bv());
139                 else
140                         InsetCollapsable::priv_dispatch(cur, cmd);
141                 break;
142
143
144         case LFUN_INSET_TOGGLE:
145                 // We assume that this lfun is indeed going to be dispatched.
146                 cur.dispatched();
147
148                 if (cmd.argument == "open")
149                         setStatus(Open);
150                 else if (cmd.argument == "close") {
151                         setStatus(Collapsed);
152                         leaveInset(cur, *this);
153
154                 // The branch inset specialises its behaviour on "toggle".
155                 } else if (cmd.argument == "toggle"
156                            || cmd.argument.empty()) {
157                         BranchList const & branchlist =
158                                 cur.bv().buffer()->params().branchlist();
159                         if (isBranchSelected(branchlist)) {
160                                 if (status() != Open)
161                                         setStatus(Open);
162                                 else
163                                         cur.undispatched();
164                         } else {
165                                 if (status() != Collapsed) {
166                                         setStatus(Collapsed);
167                                         leaveInset(cur, *this);
168                                 } else
169                                         cur.undispatched();
170                         }
171                 }
172                 break;
173
174         default:
175                 InsetCollapsable::priv_dispatch(cur, cmd);
176                 break;
177         }
178 }
179
180
181 bool InsetBranch::isBranchSelected(BranchList const & branchlist) const
182 {
183         BranchList::const_iterator const end = branchlist.end();
184         BranchList::const_iterator it =
185                 std::find_if(branchlist.begin(), end,
186                              BranchNamesEqual(params_.branch));
187         if (it == end)
188                 return false;
189         return it->getSelected();
190 }
191
192
193 int InsetBranch::latex(Buffer const & buf, ostream & os,
194                        OutputParams const & runparams) const
195 {
196         return isBranchSelected(buf.params().branchlist()) ?
197                 InsetText::latex(buf, os, runparams) : 0;
198 }
199
200
201 int InsetBranch::linuxdoc(Buffer const & buf, std::ostream & os,
202                           OutputParams const & runparams) const
203 {
204         return isBranchSelected(buf.params().branchlist()) ?
205                 InsetText::linuxdoc(buf, os, runparams) : 0;
206 }
207
208
209 int InsetBranch::docbook(Buffer const & buf, std::ostream & os,
210                          OutputParams const & runparams) const
211 {
212         return isBranchSelected(buf.params().branchlist()) ?
213                 InsetText::docbook(buf, os, runparams) : 0;
214 }
215
216
217 int InsetBranch::plaintext(Buffer const & buf, std::ostream & os,
218                            OutputParams const & runparams) const
219 {
220         return isBranchSelected(buf.params().branchlist()) ?
221                 InsetText::plaintext(buf, os, runparams): 0;
222 }
223
224
225 void InsetBranch::validate(LaTeXFeatures & features) const
226 {
227         InsetText::validate(features);
228 }
229
230
231
232 string const InsetBranchMailer::name_("branch");
233
234 InsetBranchMailer::InsetBranchMailer(InsetBranch & inset)
235         : inset_(inset)
236 {}
237
238
239 string const InsetBranchMailer::inset2string(Buffer const &) const
240 {
241         return params2string(inset_.params());
242 }
243
244
245 string const InsetBranchMailer::params2string(InsetBranchParams const & params)
246 {
247         ostringstream data;
248         data << name_ << ' ';
249         params.write(data);
250         return data.str();
251 }
252
253
254 void InsetBranchMailer::string2params(string const & in,
255                                       InsetBranchParams & params)
256 {
257         params = InsetBranchParams();
258         if (in.empty())
259                 return;
260
261         istringstream data(in);
262         LyXLex lex(0,0);
263         lex.setStream(data);
264
265         string name;
266         lex >> name;
267         if (name != name_)
268                 return print_mailer_error("InsetBranchMailer", in, 1, name_);
269
270         // This is part of the inset proper that is usually swallowed
271         // by LyXText::readInset
272         string id;
273         lex >> id;
274         if (!lex || id != "Branch")
275                 return print_mailer_error("InsetBranchMailer", in, 2, "Branch");
276
277         params.read(lex);
278 }
279
280
281 void InsetBranchParams::write(ostream & os) const
282 {
283         os << "Branch " << branch << '\n';
284 }
285
286
287 void InsetBranchParams::read(LyXLex & lex)
288 {
289         lex >> branch;
290 }