]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
prevent paragraph breaks in charstyle insets
[lyx.git] / src / insets / insetcharstyle.C
1 /**
2  * \file insetcharstyle.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "insetcharstyle.h"
16
17 #include "BufferView.h"
18 #include "dispatchresult.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LaTeXFeatures.h"
22 #include "LColor.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
27 #include "paragraph_funcs.h"
28 #include "sgml.h"
29
30 #include "frontends/font_metrics.h"
31 #include "frontends/Painter.h"
32
33 #include <sstream>
34
35
36 using std::string;
37 using std::auto_ptr;
38 using std::istringstream;
39 using std::ostream;
40 using std::ostringstream;
41
42
43 void InsetCharStyle::init()
44 {
45         setInsetName("CharStyle");
46         setStatus(Inlined);
47         setDrawFrame(false);
48         has_label_ = true;
49 }
50
51
52 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
53                                 CharStyles::iterator cs)
54         : InsetCollapsable(bp)
55 {
56         params_.type = cs->name;
57         params_.latextype = cs->latextype;
58         params_.latexname = cs->latexname;
59         params_.latexparam = cs->latexparam;
60         params_.font = cs->font;
61         params_.labelfont = cs->labelfont;
62         init();
63 }
64
65
66 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
67         : InsetCollapsable(in), params_(in.params_)
68 {
69         init();
70 }
71
72
73 auto_ptr<InsetBase> InsetCharStyle::clone() const
74 {
75         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
76 }
77
78
79 string const InsetCharStyle::editMessage() const
80 {
81         return _("Opened CharStyle Inset");
82 }
83
84
85 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
86 {
87         params_.write(os);
88         InsetCollapsable::write(buf, os);
89 }
90
91
92 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
93 {
94         InsetCollapsable::read(buf, lex);
95         setStatus(Inlined);
96 }
97
98
99 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
100 {
101         LyXFont tmpfont = mi.base.font;
102         getDrawFont(mi.base.font);
103         InsetText::metrics(mi, dim);
104         mi.base.font = tmpfont;
105         dim_ = dim;
106         if (has_label_)
107                 dim_.des += ascent();
108 }
109
110
111 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
112 {
113         setPosCache(pi, x, y);
114
115         LyXFont tmpfont = pi.base.font;
116         getDrawFont(pi.base.font);
117         InsetText::draw(pi, x, y);
118         pi.base.font = tmpfont;
119
120         int desc = InsetText::descent();
121         if (has_label_)
122                 desc -= ascent();
123         
124         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
125         pi.pain.line(x, y + desc, x + dim_.wid - 2, y + desc, 
126                 params_.labelfont.color());
127         pi.pain.line(x + dim_.wid - 2, y + desc, x + dim_.wid - 2, y + desc - 4, 
128                 params_.labelfont.color());
129                 
130         if (has_label_) {
131                 LyXFont font(params_.labelfont);
132                 font.realize(LyXFont(LyXFont::ALL_SANE));
133                 font.decSize();
134                 font.decSize();
135                 int w = 0;
136                 int a = 0;
137                 int d = 0;
138                 font_metrics::rectText(params_.type, font, w, a, d);
139                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
140                         params_.type, font, LColor::none, LColor::none);
141         }
142 }
143
144
145 void InsetCharStyle::getDrawFont(LyXFont & font) const
146 {
147         font = params_.font;
148 }
149
150
151 void InsetCharStyle::priv_dispatch(LCursor & cur, FuncRequest & cmd)
152 {
153         setStatus(Inlined);
154         switch (cmd.action) {
155                 case LFUN_MOUSE_PRESS:
156                         if (cmd.button() == mouse_button::button3)
157                                 has_label_ = !has_label_;
158                         else
159                                 InsetText::priv_dispatch(cur, cmd);
160                         break;
161                 // supress these
162                 // paragraph breaks not allowed in charstyle insets!
163                 case LFUN_BREAKPARAGRAPH:
164                 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
165                 case LFUN_BREAKPARAGRAPH_SKIP:
166                         break;
167
168                 default:
169                         InsetCollapsable::priv_dispatch(cur, cmd);
170                         break;
171         }
172 }
173
174
175 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
176                      OutputParams const & runparams) const
177 {
178         os << "\\" << params_.latexname;
179         if (!params_.latexparam.empty())
180                 os << params_.latexparam;
181         os << "{";
182         int i = InsetText::latex(buf, os, runparams);
183         os << "}";
184         return i;
185 }
186
187
188 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
189                              OutputParams const & runparams) const
190 {
191         sgml::openTag(os, params_.latexname, params_.latexparam);
192         int i = InsetText::linuxdoc(buf, os, runparams);
193         sgml::closeTag(os, params_.latexname);
194         return i;
195 }
196
197
198 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
199                             OutputParams const & runparams) const
200 {
201         ParagraphList::const_iterator par = paragraphs().begin();
202         ParagraphList::const_iterator end = paragraphs().end();
203
204         sgml::openTag(os, params_.latexname, par->getID() + params_.latexparam);
205
206         for (; par != end; ++par) {
207                 par->simpleDocBookOnePar(buf, os, runparams,
208                                          outerFont(par - paragraphs().begin(),
209                                                    paragraphs()));
210         }
211
212         sgml::closeTag(os, params_.latexname);
213         return 0;
214 }
215
216
217 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
218                               OutputParams const & runparams) const
219 {
220         return InsetText::plaintext(buf, os, runparams);
221 }
222
223
224 void InsetCharStyle::validate(LaTeXFeatures & features) const
225 {
226         features.require(params_.type);
227 }
228
229
230 void InsetCharStyleParams::write(ostream & os) const
231 {
232         os << "CharStyle " << type << "\n";
233 }
234
235
236 void InsetCharStyleParams::read(LyXLex & lex)
237 {
238         if (lex.isOK()) {
239                 lex.next();
240                 string token = lex.getString();
241         }
242
243         if (lex.isOK()) {
244                 lex.next();
245                 type = lex.getString();
246         }
247 }