]> git.lyx.org Git - lyx.git/blob - src/MetricsInfo.cpp
Clear background behind sublables when necessary.
[lyx.git] / src / MetricsInfo.cpp
1 /**
2  * \file MetricsInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "ColorSet.h"
15 #include "LyXRC.h"
16 #include "MetricsInfo.h"
17
18 #include "insets/Inset.h"
19
20 #include "mathed/MathSupport.h"
21
22 #include "frontends/Painter.h"
23
24 #include "support/docstring.h"
25 #include "support/lassert.h"
26 #include "support/RefChanger.h"
27
28 using namespace std;
29
30
31 namespace lyx {
32
33 /////////////////////////////////////////////////////////////////////////
34 //
35 // MetricsBase
36 //
37 /////////////////////////////////////////////////////////////////////////
38
39 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
40         : bv(b), font(move(f)), style(LM_ST_TEXT), fontname("mathnormal"),
41           textwidth(w), solid_line_thickness_(1), solid_line_offset_(1),
42           dotted_line_thickness_(1)
43 {
44         if (lyxrc.zoom >= 200) {
45                 // derive the line thickness from zoom factor
46                 // the zoom is given in percent
47                 // (increase thickness at 250%, 450% etc.)
48                 solid_line_thickness_ = (lyxrc.zoom + 150) / 200;
49                 // adjust line_offset_ too
50                 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
51         }
52         if (lyxrc.zoom >= 100) {
53                 // derive the line thickness from zoom factor
54                 // the zoom is given in percent
55                 // (increase thickness at 150%, 250% etc.)
56                 dotted_line_thickness_ = (lyxrc.zoom + 50) / 100;
57         }
58 }
59
60
61 Changer MetricsBase::changeFontSet(string const & name, bool cond)
62 {
63         RefChanger<MetricsBase> rc = make_save(*this);
64         if (!cond)
65                 rc->keep();
66         else {
67                 ColorCode oldcolor = font.color();
68                 string const oldname = fontname;
69                 fontname = name;
70                 if (isMathFont(name) || isMathFont(oldname))
71                         font = sane_font;
72                 augmentFont(font, name);
73                 font.setSize(rc->old.font.size());
74                 if (name != "lyxtex"
75                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
76                         || (isMathFont(oldname) && oldcolor != Color_math)))
77                         font.setColor(oldcolor);
78         }
79         return move(rc);
80 }
81
82
83 /////////////////////////////////////////////////////////////////////////
84 //
85 // MetricsInfo
86 //
87 /////////////////////////////////////////////////////////////////////////
88
89 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
90                          MacroContext const & mc)
91         : base(bv, font, textwidth), macrocontext(mc)
92 {}
93
94
95 /////////////////////////////////////////////////////////////////////////
96 //
97 // PainterInfo
98 //
99 /////////////////////////////////////////////////////////////////////////
100
101 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
102         : pain(painter), ltr_pos(false), change_(), selected(false),
103         do_spellcheck(true), full_repaint(true), background_color(Color_background)
104 {
105         base.bv = bv;
106 }
107
108
109 void PainterInfo::draw(int x, int y, char_type c)
110 {
111         pain.text(x, y, c, base.font);
112 }
113
114
115 void PainterInfo::draw(int x, int y, docstring const & str)
116 {
117         pain.text(x, y, str, base.font);
118 }
119
120
121 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
122 {
123         ColorCode const color_bg = inset->backgroundColor(*this);
124
125         if (selected && sel)
126                 // This inset is in a selection
127                 return Color_selection;
128         else {
129                 if (color_bg != Color_none)
130                         // This inset has its own color
131                         return color_bg;
132                 else {
133                         if (background_color == Color_none)
134                                 // This inset has no own color and does not inherit a color
135                                 return Color_background;
136                         else
137                                 // This inset has no own color, but inherits a color
138                                 return background_color;
139                 }
140         }
141 }
142
143
144 Color PainterInfo::textColor(Color const & color) const
145 {
146         if (change_.changed()) 
147                 return change_.color();
148         if (selected)
149                 return Color_selectiontext;
150         return color;
151 }
152
153
154 Changer MetricsBase::changeScript(bool cond)
155 {
156         switch (style) {
157         case LM_ST_DISPLAY:
158         case LM_ST_TEXT:
159                 return changeStyle(LM_ST_SCRIPT, cond);
160         case LM_ST_SCRIPT:
161         case LM_ST_SCRIPTSCRIPT:
162                 return changeStyle(LM_ST_SCRIPTSCRIPT, cond);
163         }
164         //remove Warning
165         LASSERT(false, return Changer());
166 }
167
168
169 Changer MetricsBase::changeFrac(bool cond)
170 {
171         switch (style) {
172         case LM_ST_DISPLAY:
173                 return changeStyle(LM_ST_TEXT, cond);
174         case LM_ST_TEXT:
175                 return changeStyle(LM_ST_SCRIPT, cond);
176         case LM_ST_SCRIPT:
177         case LM_ST_SCRIPTSCRIPT:
178                 return changeStyle(LM_ST_SCRIPTSCRIPT, cond);
179         }
180         //remove Warning
181         return Changer();
182 }
183
184
185 Changer MetricsBase::changeStyle(Styles new_style, bool cond)
186 {
187         static const int diff[4][4] =
188                 { { 0, 0, -3, -5 },
189                   { 0, 0, -3, -5 },
190                   { 3, 3,  0, -2 },
191                   { 5, 5,  2,  0 } };
192         int t = diff[style][new_style];
193         RefChanger<MetricsBase> rc = make_save(*this);
194         if (!cond)
195                 rc->keep();
196         else {
197                 if (t > 0)
198                         while (t--)
199                                 font.incSize();
200                 else
201                         while (t++)
202                                 font.decSize();
203                 style = new_style;
204         }
205         return move(rc);
206 }
207
208
209 } // namespace lyx