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