]> git.lyx.org Git - lyx.git/blob - src/frontends/Painter.cpp
* gcc does not like missing characters in keywords
[lyx.git] / src / frontends / Painter.cpp
1 /**
2  * \file Painter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "frontends/Painter.h"
15
16 #include "frontends/FontMetrics.h"
17
18 #include "FontInfo.h"
19
20 using lyx::docstring;
21
22 using std::max;
23 using std::string;
24
25 namespace lyx {
26 namespace frontend {
27
28 void Painter::button(int x, int y, int w, int h, bool mouseHover)
29 {
30         if (mouseHover)
31                 fillRectangle(x, y, w, h, Color_buttonhoverbg);
32         else
33                 fillRectangle(x, y, w, h, Color_buttonbg);
34         buttonFrame(x, y, w, h);
35 }
36
37
38 void Painter::buttonFrame(int x, int y, int w, int h)
39 {
40         line(x, y, x, y + h - 1, Color_buttonframe);
41         line(x - 1 + w, y, x - 1 + w, y + h - 1, Color_buttonframe);
42         line(x, y - 1, x - 1 + w, y - 1, Color_buttonframe);
43         line(x, y + h - 1, x - 1 + w, y + h - 1, Color_buttonframe);
44 }
45
46
47 void Painter::rectText(int x, int y,
48         docstring const & str,
49         FontInfo const & font,
50         ColorCode back,
51         ColorCode frame)
52 {
53         int width;
54         int ascent;
55         int descent;
56
57         FontMetrics const & fm = theFontMetrics(font);
58         fm.rectText(str, width, ascent, descent);
59
60         if (back != Color_none)
61                 fillRectangle(x + 1, y - ascent + 1, width - 1,
62                               ascent + descent - 1, back);
63
64         if (frame != Color_none)
65                 rectangle(x, y - ascent, width, ascent + descent, frame);
66
67         text(x + 3, y, str, font);
68 }
69
70
71 void Painter::buttonText(int x, int y, docstring const & str,
72         FontInfo const & font, bool mouseHover)
73 {
74         int width;
75         int ascent;
76         int descent;
77
78         FontMetrics const & fm = theFontMetrics(font);
79         fm.buttonText(str, width, ascent, descent);
80
81         button(x + 1, y - ascent, width - 2, descent + ascent, mouseHover);
82         text(x + 4, y - 1, str, font);
83 }
84
85
86 int Painter::preeditText(int x, int y, char_type c,
87         FontInfo const & font, preedit_style style)
88 {
89         FontInfo temp_font = font;
90         FontMetrics const & fm = theFontMetrics(font);
91         int ascent = fm.maxAscent();
92         int descent = fm.maxDescent();
93         int height = ascent + descent;
94         int width = fm.width(c);
95
96         switch (style) {
97                 case preedit_default:
98                         // default unselecting mode.
99                         fillRectangle(x, y - height + 1, width, height, Color_background);
100                         dashedUnderline(font, x, y - descent + 1, width);
101                         break;
102                 case preedit_selecting:
103                         // We are in selecting mode: white text on black background.
104                         fillRectangle(x, y - height + 1, width, height, Color_black);
105                         temp_font.setColor(Color_white);
106                         break;
107                 case preedit_cursor:
108                         // The character comes with a cursor.
109                         fillRectangle(x, y - height + 1, width, height, Color_background);
110                         underline(font, x, y - descent + 1, width);
111                         break;
112         }
113         text(x, y - descent + 1, c, temp_font);
114
115         return width;
116 }
117
118
119 void Painter::underline(FontInfo const & f, int x, int y, int width)
120 {
121         FontMetrics const & fm = theFontMetrics(f);
122
123         int const below = max(fm.maxDescent() / 2, 2);
124         int const height = max((fm.maxDescent() / 4) - 1, 1);
125
126         if (height < 2)
127                 line(x, y + below, x + width, y + below, f.color());
128         else
129                 fillRectangle(x, y + below, width, below + height, f.color());
130 }
131
132
133 void Painter::dashedUnderline(FontInfo const & f, int x, int y, int width)
134 {
135         FontMetrics const & fm = theFontMetrics(f);
136
137         int const below = max(fm.maxDescent() / 2, 2);
138         int height = max((fm.maxDescent() / 4) - 1, 1);
139
140         if (height >= 2)
141                 height += below;
142
143         for (int n = 0; n < height; ++n)
144                 line(x, y + below + n, x + width, y + below + n, f.color(), line_onoffdash);
145 }
146
147 } // namespace frontend
148 } // namespace lyx