]> git.lyx.org Git - lyx.git/blob - src/mathed/math_casesinset.C
Fix math cursor positioning bug
[lyx.git] / src / mathed / math_casesinset.C
1 /**
2  * \file math_casesinset.C
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 "math_casesinset.h"
14 #include "math_data.h"
15 #include "math_mathmlstream.h"
16 #include "math_support.h"
17 #include "FuncStatus.h"
18 #include "LaTeXFeatures.h"
19 #include "support/std_ostream.h"
20 #include "cursor.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "undo.h"
24
25 #include "support/lstrings.h"
26
27 #include <sstream>
28
29 using lyx::support::bformat;
30
31 using std::endl;
32 using std::max;
33 using std::min;
34 using std::swap;
35 using std::istringstream;
36 using std::string;
37
38 using std::auto_ptr;
39
40
41 MathCasesInset::MathCasesInset(row_type n)
42         : MathGridInset(2, n, 'c', "ll")
43 {}
44
45
46 auto_ptr<InsetBase> MathCasesInset::doClone() const
47 {
48         return auto_ptr<InsetBase>(new MathCasesInset(*this));
49 }
50
51
52 void MathCasesInset::metrics(MetricsInfo & mi, Dimension & dim) const
53 {
54         MathGridInset::metrics(mi);
55         dim_.wid += 8;
56         dim = dim_;
57 }
58
59
60 void MathCasesInset::draw(PainterInfo & pi, int x, int y) const
61 {
62         mathed_draw_deco(pi, x + 1, y - dim_.ascent(), 6, dim_.height(), "{");
63         MathGridInset::draw(pi, x + 8, y);
64         setPosCache(pi, x, y);
65 }
66
67
68 void MathCasesInset::doDispatch(LCursor & cur, FuncRequest & cmd)
69 {
70         //lyxerr << "*** MathCasesInset: request: " << cmd << endl;
71         switch (cmd.action) {
72         case LFUN_TABULAR_FEATURE: {
73                 recordUndo(cur);
74                 istringstream is(cmd.argument);
75                 string s;
76                 is >> s; 
77                 if (s == "add-vline-left" || s == "add-vline-right") {
78                         cur.undispatched();
79                         break;
80                 }
81         }
82         default:
83                 MathGridInset::doDispatch(cur, cmd);
84         }
85 }
86
87
88 bool MathCasesInset::getStatus(LCursor & cur, FuncRequest const & cmd,
89                 FuncStatus & flag) const
90 {
91         switch (cmd.action) {
92         case LFUN_TABULAR_FEATURE: {
93                 istringstream is(cmd.argument);
94                 string s;
95                 is >> s;
96                 if (s == "add-vline-left" || s == "add-vline-right") {
97                         flag.enabled(false);
98                         flag.message(bformat(
99                                 N_("No vertical grid lines in '%1$s'"), s));
100                         return true;
101                 }
102         }
103         default:
104                 return MathGridInset::getStatus(cur, cmd, flag);
105         }
106 }
107
108
109 void MathCasesInset::write(WriteStream & os) const
110 {
111         if (os.fragile())
112                 os << "\\protect";
113         os << "\\begin{cases}\n";
114         MathGridInset::write(os);
115         if (os.fragile())
116                 os << "\\protect";
117         os << "\\end{cases}";
118 }
119
120
121 void MathCasesInset::normalize(NormalStream & os) const
122 {
123         os << "[cases ";
124         MathGridInset::normalize(os);
125         os << ']';
126 }
127
128
129 void MathCasesInset::maple(MapleStream & os) const
130 {
131         os << "cases(";
132         MathGridInset::maple(os);
133         os << ')';
134 }
135
136
137 void MathCasesInset::infoize(std::ostream & os) const
138 {
139         os << "Cases ";
140 }
141
142
143 void MathCasesInset::validate(LaTeXFeatures & features) const
144 {
145         features.require("amsmath");
146         MathGridInset::validate(features);
147 }