]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathAMSArray.cpp
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / mathed / InsetMathAMSArray.cpp
1 /**
2  * \file InsetMathAMSArray.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 "InsetMathAMSArray.h"
14
15 #include "LaTeXFeatures.h"
16 #include "MathData.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
19 #include "MetricsInfo.h"
20
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "support/gettext.h"
24
25 #include "support/lstrings.h"
26
27 #include <sstream>
28 #include <ostream>
29
30
31 using namespace std;
32 using namespace lyx::support;
33
34 namespace lyx {
35
36
37 InsetMathAMSArray::InsetMathAMSArray(Buffer * buf, docstring const & name,
38                 int m, int n)
39         : InsetMathGrid(buf, m, n), name_(name)
40 {}
41
42
43 InsetMathAMSArray::InsetMathAMSArray(Buffer * buf, docstring const & name)
44         : InsetMathGrid(buf, 1, 1), name_(name)
45 {}
46
47
48 Inset * InsetMathAMSArray::clone() const
49 {
50         return new InsetMathAMSArray(*this);
51 }
52
53
54 char const * InsetMathAMSArray::name_left() const
55 {
56         if (name_ == "bmatrix")
57                 return "[";
58         if (name_ == "Bmatrix")
59                 return "{";
60         if (name_ == "vmatrix")
61                 return "|";
62         if (name_ == "Vmatrix")
63                 return "Vert";
64         if (name_ == "pmatrix")
65                 return "(";
66         return ".";
67 }
68
69
70 char const * InsetMathAMSArray::name_right() const
71 {
72         if (name_ == "bmatrix")
73                 return "]";
74         if (name_ == "Bmatrix")
75                 return "}";
76         if (name_ == "vmatrix")
77                 return "|";
78         if (name_ == "Vmatrix")
79                 return "Vert";
80         if (name_ == "pmatrix")
81                 return ")";
82         return ".";
83 }
84
85
86 void InsetMathAMSArray::metrics(MetricsInfo & mi, Dimension & dim) const
87 {
88         ArrayChanger dummy(mi.base);
89         InsetMathGrid::metrics(mi, dim);
90         dim.wid += 14;
91 }
92
93
94 void InsetMathAMSArray::draw(PainterInfo & pi, int x, int y) const
95 {
96         Dimension const dim = dimension(*pi.base.bv);
97         int const yy = y - dim.ascent();
98         // Drawing the deco after an ArrayChanger does not work
99         mathed_draw_deco(pi, x + 1, yy, 5, dim.height(), from_ascii(name_left()));
100         mathed_draw_deco(pi, x + dim.width() - 8, yy, 5, dim.height(), from_ascii(name_right()));
101         ArrayChanger dummy(pi.base);
102         InsetMathGrid::drawWithMargin(pi, x, y, 6, 8);
103 }
104
105
106 bool InsetMathAMSArray::getStatus(Cursor & cur, FuncRequest const & cmd,
107                 FuncStatus & flag) const
108 {
109         switch (cmd.action()) {
110         case LFUN_INSET_MODIFY: {
111                 istringstream is(to_utf8(cmd.argument()));
112                 string s;
113                 is >> s;
114                 if (s != "tabular")
115                         break;
116                 is >> s;
117                 if (s == "add-vline-left" || s == "add-vline-right") {
118                         flag.message(bformat(
119                                 from_utf8(N_("Can't add vertical grid lines in '%1$s'")),
120                                 name_));
121                         flag.setEnabled(false);
122                         return true;
123                 }
124                 break;
125         }
126         default:
127                 break;
128         }
129         return InsetMathGrid::getStatus(cur, cmd, flag);
130 }
131
132
133 void InsetMathAMSArray::write(WriteStream & os) const
134 {
135         MathEnsurer ensurer(os);
136         os << "\\begin{" << name_ << '}';
137         InsetMathGrid::write(os);
138         os << "\\end{" << name_ << '}';
139 }
140
141
142 void InsetMathAMSArray::infoize(odocstream & os) const
143 {
144         docstring name = name_;
145         name[0] = support::uppercase(name[0]);
146         os << name << ' ';
147 }
148
149
150 void InsetMathAMSArray::normalize(NormalStream & os) const
151 {
152         os << '[' << name_ << ' ';
153         InsetMathGrid::normalize(os);
154         os << ']';
155 }
156
157
158 void InsetMathAMSArray::validate(LaTeXFeatures & features) const
159 {
160         if (name_ == "CD")
161                 // amscd is independent of amsmath although it is part of
162                 // the amsmath bundle
163                 features.require("amscd");
164         else
165                 features.require("amsmath");
166         InsetMathGrid::validate(features);
167 }
168
169
170 } // namespace lyx