]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
replace insetVerbatim with InsetText methods (bug 1731), fix LaTeX output problems
[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 "sgml.h"
28
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
31
32 #include <sstream>
33
34
35 using std::string;
36 using std::auto_ptr;
37 using std::istringstream;
38 using std::ostream;
39 using std::ostringstream;
40
41
42 void InsetCharStyle::init()
43 {
44         setInsetName("CharStyle");
45         setStatus(Inlined);
46 }
47
48
49 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
50                                 CharStyles::iterator cs)
51         : InsetCollapsable(bp), has_label_(true)
52 {
53         params_.type = cs->name;
54         params_.latextype = cs->latextype;
55         params_.latexname = cs->latexname;
56         params_.latexparam = cs->latexparam;
57         params_.font = cs->font;
58         params_.labelfont = cs->labelfont;
59         init();
60 }
61
62
63 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
64         : InsetCollapsable(in), params_(in.params_), has_label_(true)
65 {
66         init();
67 }
68
69
70 auto_ptr<InsetBase> InsetCharStyle::clone() const
71 {
72         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
73 }
74
75
76 string const InsetCharStyle::editMessage() const
77 {
78         return _("Opened CharStyle Inset");
79 }
80
81
82 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
83 {
84         params_.write(os);
85         InsetCollapsable::write(buf, os);
86 }
87
88
89 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
90 {
91         InsetCollapsable::read(buf, lex);
92         setStatus(Inlined);
93 }
94
95
96 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
97 {
98         LyXFont tmpfont = mi.base.font;
99         getDrawFont(mi.base.font);
100         InsetCollapsable::metrics(mi, dim);
101         mi.base.font = tmpfont;
102         dim_ = dim;
103         if (has_label_)
104                 dim_.des += ascent();
105 }
106
107
108 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
109 {
110         setPosCache(pi, x, y);
111
112         // FIXME: setStatus(Inlined); this is not a const operation
113         LyXFont tmpfont = pi.base.font;
114         //setDrawFrame(InsetText::NEVER);
115         getDrawFont(pi.base.font);
116         InsetText::draw(pi, x, y);
117         pi.base.font = tmpfont;
118
119         pi.pain.line(x + 2, y + InsetText::descent() - 4, x + 2,
120                 y + InsetText::descent(), params_.labelfont.color());
121         pi.pain.line(x + 2, y + InsetText::descent(), x + dim_.wid - 2,
122                 y + InsetText::descent(), params_.labelfont.color());
123         pi.pain.line(x + dim_.wid - 2, y + InsetText::descent(), x + dim_.wid - 2,
124                 y + InsetText::descent() - 4, params_.labelfont.color());
125
126         if (has_label_) {
127                 LyXFont font(params_.labelfont);
128                 font.realize(LyXFont(LyXFont::ALL_SANE));
129                 font.decSize();
130                 font.decSize();
131                 int w = 0;
132                 int a = 0;
133                 int d = 0;
134                 font_metrics::rectText(params_.type, font, w, a, d);
135                 pi.pain.rectText(x + (dim_.wid - w) / 2,
136                         y + InsetText::descent() + a,
137                         params_.type, font, LColor::none, LColor::none);
138         }
139 }
140
141
142 void InsetCharStyle::getDrawFont(LyXFont & font) const
143 {
144         font = params_.font;
145 }
146
147
148 void InsetCharStyle::priv_dispatch(LCursor & cur, FuncRequest & cmd)
149 {
150         setStatus(Inlined);
151         switch (cmd.action) {
152                 case LFUN_MOUSE_PRESS:
153                         if (cmd.button() == mouse_button::button3)
154                                 has_label_ = !has_label_;
155                         else
156                                 InsetText::dispatch(cur, cmd);
157                         break;
158
159                 default:
160                         InsetCollapsable::priv_dispatch(cur, cmd);
161                         break;
162         }
163 }
164
165
166 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
167                      OutputParams const & runparams) const
168 {
169         os << "\\" << params_.latexname;
170         if (!params_.latexparam.empty())
171                 os << params_.latexparam;
172         os << "{";
173         int i = InsetText::latex(buf, os, runparams);
174         os << "}";
175         return i;
176 }
177
178
179 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
180                              OutputParams const & runparams) const
181 {
182         sgml::openTag(os, params_.latexname, params_.latexparam);
183         int i = InsetText::linuxdoc(buf, os, runparams);
184         sgml::closeTag(os, params_.latexname);
185         return i;
186 }
187
188
189 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
190                             OutputParams const & runparams) const
191 {
192         sgml::openTag(os, params_.latexname, params_.latexparam);
193         int i = InsetText::docbook(buf, os, runparams);
194         sgml::closeTag(os, params_.latexname);
195         return i;
196 }
197
198
199 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
200                               OutputParams const & runparams) const
201 {
202         return InsetText::plaintext(buf, os, runparams);
203 }
204
205
206 void InsetCharStyle::validate(LaTeXFeatures & features) const
207 {
208         features.require(params_.type);
209 }
210
211
212 void InsetCharStyleParams::write(ostream & os) const
213 {
214         os << "CharStyle " << type << "\n";
215 }
216
217
218 void InsetCharStyleParams::read(LyXLex & lex)
219 {
220         if (lex.isOK()) {
221                 lex.next();
222                 string token = lex.getString();
223         }
224
225         if (lex.isOK()) {
226                 lex.next();
227                 type = lex.getString();
228         }
229 }