]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
insetcharstyle drawing cosmetics
[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
162                 default:
163                         InsetCollapsable::priv_dispatch(cur, cmd);
164                         break;
165         }
166 }
167
168
169 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
170                      OutputParams const & runparams) const
171 {
172         os << "\\" << params_.latexname;
173         if (!params_.latexparam.empty())
174                 os << params_.latexparam;
175         os << "{";
176         int i = InsetText::latex(buf, os, runparams);
177         os << "}";
178         return i;
179 }
180
181
182 int InsetCharStyle::linuxdoc(Buffer const & buf, ostream & os,
183                              OutputParams const & runparams) const
184 {
185         sgml::openTag(os, params_.latexname, params_.latexparam);
186         int i = InsetText::linuxdoc(buf, os, runparams);
187         sgml::closeTag(os, params_.latexname);
188         return i;
189 }
190
191
192 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
193                             OutputParams const & runparams) const
194 {
195         ParagraphList::const_iterator par = paragraphs().begin();
196         ParagraphList::const_iterator end = paragraphs().end();
197
198         sgml::openTag(os, params_.latexname, par->getID() + params_.latexparam);
199
200         for (; par != end; ++par) {
201                 par->simpleDocBookOnePar(buf, os, runparams,
202                                          outerFont(par - paragraphs().begin(),
203                                                    paragraphs()));
204         }
205
206         sgml::closeTag(os, params_.latexname);
207         return 0;
208 }
209
210
211 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
212                               OutputParams const & runparams) const
213 {
214         return InsetText::plaintext(buf, os, runparams);
215 }
216
217
218 void InsetCharStyle::validate(LaTeXFeatures & features) const
219 {
220         features.require(params_.type);
221 }
222
223
224 void InsetCharStyleParams::write(ostream & os) const
225 {
226         os << "CharStyle " << type << "\n";
227 }
228
229
230 void InsetCharStyleParams::read(LyXLex & lex)
231 {
232         if (lex.isOK()) {
233                 lex.next();
234                 string token = lex.getString();
235         }
236
237         if (lex.isOK()) {
238                 lex.next();
239                 type = lex.getString();
240         }
241 }