]> git.lyx.org Git - lyx.git/blob - src/frontends/Painter.cpp
cosmetics (whitespace and no-op functions)
[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 "Color.h"
19 #include "Font.h"
20
21 using lyx::docstring;
22
23 using std::max;
24 using std::string;
25
26 namespace lyx {
27 namespace frontend {
28
29 void Painter::button(int x, int y, int w, int h, bool mouseHover)
30 {
31         if (mouseHover)
32                 fillRectangle(x, y, w, h, Color::buttonhoverbg);
33         else
34                 fillRectangle(x, y, w, h, Color::buttonbg);
35         buttonFrame(x, y, w, h);
36 }
37
38
39 void Painter::buttonFrame(int x, int y, int w, int h)
40 {
41         line(x, y, x, y + h - 1, Color::buttonframe);
42         line(x - 1 + w, y, x - 1 + w, y + h - 1, Color::buttonframe);
43         line(x, y - 1, x - 1 + w, y - 1, Color::buttonframe);
44         line(x, y + h - 1, x - 1 + w, y + h - 1, Color::buttonframe);
45 }
46
47
48 void Painter::rectText(int x, int y,
49         docstring const & str,
50         Font const & font,
51         Color_color back,
52         Color_color frame)
53 {
54         int width;
55         int ascent;
56         int descent;
57
58         FontMetrics const & fm = theFontMetrics(font);
59         fm.rectText(str, width, ascent, descent);
60
61         if (back != Color::none)
62                 fillRectangle(x + 1, y - ascent + 1, width - 1,
63                               ascent + descent - 1, back);
64
65         if (frame != Color::none)
66                 rectangle(x, y - ascent, width, ascent + descent, frame);
67
68         text(x + 3, y, str, font);
69 }
70
71
72 void Painter::buttonText(int x, int y, docstring const & str,
73         Font const & font, bool mouseHover)
74 {
75         int width;
76         int ascent;
77         int descent;
78
79         FontMetrics const & fm = theFontMetrics(font);
80         fm.buttonText(str, width, ascent, descent);
81
82         button(x, y - ascent, width, descent + ascent, mouseHover);
83         text(x + 3, y - 1, str, font);
84 }
85
86
87 int Painter::preeditText(int x, int y, char_type c,
88         Font const & font, preedit_style style)
89 {
90         Font temp_font = font;
91         FontMetrics const & fm = theFontMetrics(font);
92         int ascent = fm.maxAscent();
93         int descent = fm.maxDescent();
94         int height = ascent + descent;
95         int width = fm.width(c);
96
97         switch (style) {
98                 case preedit_default:
99                         // default unselecting mode.
100                         fillRectangle(x, y - height + 1, width, height, Color::background);
101                         dashedUnderline(font, x, y - descent + 1, width);
102                         break;
103                 case preedit_selecting:
104                         // We are in selecting mode: white text on black background.
105                         fillRectangle(x, y - height + 1, width, height, Color::black);
106                         temp_font.setColor(Color::white);
107                         break;
108                 case preedit_cursor:
109                         // The character comes with a cursor.
110                         fillRectangle(x, y - height + 1, width, height, Color::background);
111                         underline(font, x, y - descent + 1, width);
112                         break;
113         }
114         text(x, y - descent + 1, c, temp_font);
115
116         return width;
117 }
118
119
120 void Painter::underline(Font const & f, int x, int y, int width)
121 {
122         FontMetrics const & fm = theFontMetrics(f);
123
124         int const below = max(fm.maxDescent() / 2, 2);
125         int const height = max((fm.maxDescent() / 4) - 1, 1);
126
127         if (height < 2)
128                 line(x, y + below, x + width, y + below, f.color());
129         else
130                 fillRectangle(x, y + below, width, below + height, f.color());
131 }
132
133
134 void Painter::dashedUnderline(Font const & f, int x, int y, int width)
135 {
136         FontMetrics const & fm = theFontMetrics(f);
137
138         int const below = max(fm.maxDescent() / 2, 2);
139         int height = max((fm.maxDescent() / 4) - 1, 1);
140
141         if (height >= 2)
142                 height += below;
143
144         for (int n = 0; n < height; ++n)
145                 line(x, y + below + n, x + width, y + below + n, f.color(), line_onoffdash);
146 }
147
148 } // namespace frontend
149 } // namespace lyx