]> git.lyx.org Git - lyx.git/blob - src/insets/insetcharstyle.C
This commit is a big rework of the FontLoader/FontMetrics interaction. Only Qt4 for...
[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 "buffer.h"
18 #include "bufferparams.h"
19 #include "BufferView.h"
20 #include "dispatchresult.h"
21 #include "funcrequest.h"
22 #include "FuncStatus.h"
23 #include "cursor.h"
24 #include "gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "lyxtext.h"
29 #include "metricsinfo.h"
30 #include "paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "sgml.h"
33
34 #include "frontends/Application.h"
35 #include "frontends/FontLoader.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
38
39 #include <sstream>
40
41 using lyx::docstring;
42
43 using std::string;
44 using std::auto_ptr;
45 using std::istringstream;
46 using std::max;
47 using std::ostream;
48 using std::ostringstream;
49
50
51 void InsetCharStyle::init()
52 {
53         setInsetName("CharStyle");
54         setInlined();
55         setDrawFrame(false);
56         has_label_ = true;
57 }
58
59
60 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
61         : InsetCollapsable(bp)
62 {
63         params_.type = s;
64         setUndefined();
65         init();
66 }
67
68
69 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
70                                 CharStyles::iterator cs)
71         : InsetCollapsable(bp)
72 {
73         params_.type = cs->name;
74         setDefined(cs);
75         init();
76 }
77
78
79 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
80         : InsetCollapsable(in), params_(in.params_)
81 {
82         init();
83 }
84
85
86 auto_ptr<InsetBase> InsetCharStyle::doClone() const
87 {
88         return auto_ptr<InsetBase>(new InsetCharStyle(*this));
89 }
90
91
92 bool InsetCharStyle::undefined() const
93 {
94         return params_.latexname.empty();
95 }
96
97
98 void InsetCharStyle::setUndefined()
99 {
100         params_.latextype.clear();
101         params_.latexname.clear();
102         params_.latexparam.clear();
103         params_.font = LyXFont(LyXFont::ALL_INHERIT);
104         params_.labelfont = LyXFont(LyXFont::ALL_INHERIT);
105         params_.labelfont.setColor(LColor::error);
106 }
107
108
109 void InsetCharStyle::setDefined(CharStyles::iterator cs)
110 {
111         params_.latextype = cs->latextype;
112         params_.latexname = cs->latexname;
113         params_.latexparam = cs->latexparam;
114         params_.font = cs->font;
115         params_.labelfont = cs->labelfont;
116 }
117
118
119 docstring const InsetCharStyle::editMessage() const
120 {
121         return _("Opened CharStyle Inset");
122 }
123
124
125 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
126 {
127         params_.write(os);
128         InsetCollapsable::write(buf, os);
129 }
130
131
132 void InsetCharStyle::read(Buffer const & buf, LyXLex & lex)
133 {
134         InsetCollapsable::read(buf, lex);
135         setInlined();
136 }
137
138
139 void InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
140 {
141         LyXFont tmpfont = mi.base.font;
142         getDrawFont(mi.base.font);
143         mi.base.font.reduce(LyXFont(LyXFont::ALL_SANE));
144         mi.base.font.realize(tmpfont);
145         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
146         InsetText::metrics(mi, dim);
147         mi.base.font = tmpfont;
148         if (has_label_) {
149                 // consider width of the inset label
150                 LyXFont font(params_.labelfont);
151                 font.realize(LyXFont(LyXFont::ALL_SANE));
152                 font.decSize();
153                 font.decSize();
154                 int w = 0;
155                 int a = 0;
156                 int d = 0;
157                 string s(params_.type);
158                 if (undefined())
159                         // FIXME UNICODE
160                         s = lyx::to_utf8(_("Undef: ")) + s;
161                 docstring ds(s.begin(), s.end());
162                 theApp->fontLoader().metrics(font).rectText(ds, w, a, d);
163                 dim.wid = max(dim.wid, w);
164         }
165         dim.asc += TEXT_TO_INSET_OFFSET;
166         dim.des += TEXT_TO_INSET_OFFSET;
167         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
168         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
169         dim_ = dim;
170         if (has_label_)
171                 dim_.des += ascent();
172 }
173
174
175 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
176 {
177         setPosCache(pi, x, y);
178
179         LyXFont tmpfont = pi.base.font;
180         getDrawFont(pi.base.font);
181         // I don't understand why the above .reduce and .realize aren't
182         //needed, or even wanted, here. It just works. -- MV 10.04.2005
183         InsetText::draw(pi, x, y);
184         pi.base.font = tmpfont;
185
186         int desc = InsetText::descent();
187         if (has_label_)
188                 desc -= ascent();
189
190         pi.pain.line(x, y + desc - 4, x, y + desc, params_.labelfont.color());
191         pi.pain.line(x, y + desc, x + dim_.wid - 3, y + desc,
192                 params_.labelfont.color());
193         pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
194                 params_.labelfont.color());
195
196         // the name of the charstyle. Can be toggled.
197         if (has_label_) {
198                 LyXFont font(params_.labelfont);
199                 font.realize(LyXFont(LyXFont::ALL_SANE));
200                 font.decSize();
201                 font.decSize();
202                 int w = 0;
203                 int a = 0;
204                 int d = 0;
205                 string s(params_.type);
206                 if (undefined())
207                         // FIXME UNICODE
208                         s = lyx::to_utf8(_("Undef: ")) + s;
209                 docstring ds(s.begin(), s.end());
210                 theApp->fontLoader().metrics(font).rectText(ds, w, a, d);
211                 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
212                         ds, font, LColor::none, LColor::none);
213         }
214
215         // a visual clue when the cursor is inside the inset
216         LCursor & cur = pi.base.bv->cursor();
217         if (cur.isInside(this)) {
218                 y -= ascent();
219                 pi.pain.line(x, y + 4, x, y, params_.labelfont.color());
220                 pi.pain.line(x + 4, y, x, y, params_.labelfont.color());
221                 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
222                         params_.labelfont.color());
223                 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
224                         params_.labelfont.color());
225         }
226 }
227
228
229 void InsetCharStyle::getDrawFont(LyXFont & font) const
230 {
231         font = params_.font;
232 }
233
234
235 void InsetCharStyle::doDispatch(LCursor & cur, FuncRequest & cmd)
236 {
237         setInlined();
238         switch (cmd.action) {
239         
240         case LFUN_MOUSE_PRESS:
241                         if (cmd.button() == mouse_button::button3)
242                                 has_label_ = !has_label_;
243                         else
244                                 InsetText::doDispatch(cur, cmd);
245                         break;
246
247         default:
248                 InsetCollapsable::doDispatch(cur, cmd);
249                 break;
250         }
251 }
252
253
254 bool InsetCharStyle::getStatus(LCursor & cur, FuncRequest const & cmd,
255         FuncStatus & status) const
256 {
257         switch (cmd.action) {
258                 // paragraph breaks not allowed in charstyle insets
259                 case LFUN_BREAK_PARAGRAPH:
260                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
261                 case LFUN_BREAK_PARAGRAPH_SKIP:
262                         status.enabled(false);
263                         return true;
264
265                 default:
266                         return InsetCollapsable::getStatus(cur, cmd, status);
267                 }
268 }
269
270
271 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
272                      OutputParams const & runparams) const
273 {
274         if (!undefined()) {
275                 os << "\\" << params_.latexname;
276                 if (!params_.latexparam.empty())
277                         os << params_.latexparam;
278                 os << "{";
279         }
280         int i = InsetText::latex(buf, os, runparams);
281         if (!undefined())
282                 os << "}";
283         return i;
284 }
285
286
287 int InsetCharStyle::docbook(Buffer const & buf, ostream & os,
288                             OutputParams const & runparams) const
289 {
290         ParagraphList::const_iterator beg = paragraphs().begin();
291         ParagraphList::const_iterator par = paragraphs().begin();
292         ParagraphList::const_iterator end = paragraphs().end();
293
294         if (!undefined())
295                 sgml::openTag(os, params_.latexname,
296                         par->getID(buf, runparams) + params_.latexparam);
297
298         for (; par != end; ++par) {
299                 par->simpleDocBookOnePar(buf, os, runparams,
300                                          outerFont(std::distance(beg, par),
301                                                    paragraphs()));
302         }
303
304         if (!undefined())
305                 sgml::closeTag(os, params_.latexname);
306
307         return 0;
308 }
309
310
311 int InsetCharStyle::plaintext(Buffer const & buf, ostream & os,
312                               OutputParams const & runparams) const
313 {
314         return InsetText::plaintext(buf, os, runparams);
315 }
316
317
318 int InsetCharStyle::textString(Buffer const & buf, ostream & os,
319                        OutputParams const & op) const
320 {
321         return plaintext(buf, os, op);
322 }
323
324
325 void InsetCharStyle::validate(LaTeXFeatures & features) const
326 {
327         // Force inclusion of preamble snippet in layout file
328         features.require(params_.type);
329         InsetText::validate(features);
330 }
331
332
333 void InsetCharStyleParams::write(ostream & os) const
334 {
335         os << "CharStyle " << type << "\n";
336 }
337
338
339 void InsetCharStyleParams::read(LyXLex & lex)
340 {
341         if (lex.isOK()) {
342                 lex.next();
343                 string token = lex.getString();
344         }
345
346         if (lex.isOK()) {
347                 lex.next();
348                 type = lex.getString();
349         }
350 }