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