]> git.lyx.org Git - features.git/blob - src/MetricsInfo.cpp
3353a7d3e65f617facd8496630ecb481c3aac7f7
[features.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()
40         : bv(0), font(), style(LM_ST_TEXT), fontname("mathnormal"), textwidth(0),
41           solid_line_thickness_(1), solid_line_offset_(1), dotted_line_thickness_(1)
42 {
43         if (lyxrc.zoom >= 200) {
44                 // derive the line thickness from zoom factor
45                 // the zoom is given in percent
46                 // (increase thickness at 250%, 450% etc.)
47                 solid_line_thickness_ = (lyxrc.zoom + 50) / 200;
48                 // adjust line_offset_ too
49                 solid_line_offset_ = 1 + solid_line_thickness_ / 2;
50         }
51         if (lyxrc.zoom >= 100) {
52                 // derive the line thickness from zoom factor
53                 // the zoom is given in percent
54                 // (increase thickness at 150%, 250% etc.)
55                 dotted_line_thickness_ = (lyxrc.zoom + 50) / 100;
56         }
57 }
58
59
60 MetricsBase::MetricsBase(BufferView * b, FontInfo f, int w)
61         : MetricsBase()
62 {
63         bv = b;
64         font = f;
65         textwidth = w;
66 }
67
68
69 Changer MetricsBase::changeFontSet(docstring const & name, bool cond)
70 {
71         RefChanger<MetricsBase> rc = make_save(*this);
72         if (!cond)
73                 rc->keep();
74         else {
75                 ColorCode oldcolor = font.color();
76                 docstring const oldname = from_ascii(fontname);
77                 fontname = to_utf8(name);
78                 font = sane_font;
79                 augmentFont(font, name);
80                 font.setSize(rc->old.font.size());
81                 if (name != "lyxtex"
82                     && ((isTextFont(oldname) && oldcolor != Color_foreground)
83                         || (isMathFont(oldname) && oldcolor != Color_math)))
84                         font.setColor(oldcolor);
85         }
86         return move(rc);
87 }
88
89
90 Changer MetricsBase::changeFontSet(char const * name, bool cond)
91 {
92         return changeFontSet(from_ascii(name), cond);
93 }
94
95
96 /////////////////////////////////////////////////////////////////////////
97 //
98 // MetricsInfo
99 //
100 /////////////////////////////////////////////////////////////////////////
101
102 MetricsInfo::MetricsInfo(BufferView * bv, FontInfo font, int textwidth,
103                          MacroContext const & mc)
104         : base(bv, font, textwidth), macrocontext(mc)
105 {}
106
107
108 /////////////////////////////////////////////////////////////////////////
109 //
110 // PainterInfo
111 //
112 /////////////////////////////////////////////////////////////////////////
113
114 PainterInfo::PainterInfo(BufferView * bv, lyx::frontend::Painter & painter)
115         : pain(painter), ltr_pos(false), change_(), selected(false),
116         do_spellcheck(true), full_repaint(true), background_color(Color_background)
117 {
118         base.bv = bv;
119 }
120
121
122 void PainterInfo::draw(int x, int y, char_type c)
123 {
124         pain.text(x, y, c, base.font);
125 }
126
127
128 void PainterInfo::draw(int x, int y, docstring const & str)
129 {
130         pain.text(x, y, str, base.font);
131 }
132
133
134 ColorCode PainterInfo::backgroundColor(Inset const * inset, bool sel) const
135 {
136         ColorCode const color_bg = inset->backgroundColor(*this);
137
138         if (selected && sel)
139                 // This inset is in a selection
140                 return Color_selection;
141         else {
142                 if (color_bg != Color_none)
143                         // This inset has its own color
144                         return color_bg;
145                 else {
146                         if (background_color == Color_none)
147                                 // This inset has no own color and does not inherit a color
148                                 return Color_background;
149                         else
150                                 // This inset has no own color, but inherits a color
151                                 return background_color;
152                 }
153         }
154 }
155
156
157 Color PainterInfo::textColor(Color const & color) const
158 {
159         if (change_.changed()) 
160                 return change_.color();
161         if (selected)
162                 return Color_selectiontext;
163         return color;
164 }
165
166
167 Changer MetricsBase::changeScript(bool cond)
168 {
169         switch (style) {
170         case LM_ST_DISPLAY:
171         case LM_ST_TEXT:
172                 return changeStyle(LM_ST_SCRIPT, cond);
173         case LM_ST_SCRIPT:
174         case LM_ST_SCRIPTSCRIPT:
175                 return changeStyle(LM_ST_SCRIPTSCRIPT, cond);
176         }
177         //remove Warning
178         LASSERT(false, return Changer());
179 }
180
181
182 Changer MetricsBase::changeFrac(bool cond)
183 {
184         switch (style) {
185         case LM_ST_DISPLAY:
186                 return changeStyle(LM_ST_TEXT, cond);
187         case LM_ST_TEXT:
188                 return changeStyle(LM_ST_SCRIPT, cond);
189         case LM_ST_SCRIPT:
190         case LM_ST_SCRIPTSCRIPT:
191                 return changeStyle(LM_ST_SCRIPTSCRIPT, cond);
192         }
193         //remove Warning
194         return Changer();
195 }
196
197
198 Changer MetricsBase::changeStyle(Styles new_style, bool cond)
199 {
200         static const int diff[4][4] =
201                 { { 0, 0, -3, -5 },
202                   { 0, 0, -3, -5 },
203                   { 3, 3,  0, -2 },
204                   { 5, 5,  2,  0 } };
205         int t = diff[style][new_style];
206         RefChanger<MetricsBase> rc = make_save(*this);
207         if (!cond)
208                 rc->keep();
209         else {
210                 if (t > 0)
211                         while (t--)
212                                 font.incSize();
213                 else
214                         while (t++)
215                                 font.decSize();
216                 style = new_style;
217         }
218         return move(rc);
219 }
220
221
222 } // namespace lyx