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