]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsMatrix.C
0ed67fb302eb36a31d4e2c53c28f8e39612267a8
[lyx.git] / src / frontends / xforms / FormMathsMatrix.C
1 /**
2  * \file FormMathsMatrix.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Pablo De Napoli
8  * \author John Levon
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "FormMathsMatrix.h"
17 #include "forms/form_maths_matrix.h"
18 #include "ControlMath.h"
19
20 #include "xformsBC.h"
21
22 #include "controllers/ButtonController.h"
23
24 #include "support/LAssert.h"
25 #include "support/lyxalgo.h" // lyx::count
26
27 #include "lyx_forms.h"
28
29 #include "support/std_sstream.h"
30
31 using namespace lyx::support;
32
33 using std::ostringstream;
34
35 #ifndef CXX_GLOBAL_CSTD
36 using std::strlen;
37 #endif
38
39
40 static char h_align_str[80] = "c";
41 static char v_align_c[] = "tcb";
42
43
44 extern "C" {
45
46         static
47         int C_FormMathsMatrixAlignFilter(FL_OBJECT * ob, char const *,
48                                          char const * cur, int c)
49         {
50                 Assert(ob);
51                 FormMathsMatrix * pre =
52                         static_cast<FormMathsMatrix *>(ob->u_vdata);
53                 Assert(pre);
54                 return pre->AlignFilter(cur, c);
55         }
56
57 }
58
59
60 typedef FormController<ControlMath, FormView<FD_maths_matrix> > base_class;
61
62 FormMathsMatrix::FormMathsMatrix(Dialog & parent)
63         : base_class(parent, _("Math Matrix"), false)
64 {}
65
66
67 void FormMathsMatrix::build()
68 {
69         dialog_.reset(build_maths_matrix(this));
70
71         fl_addto_choice(dialog_->choice_valign,
72                         _("Top | Middle | Bottom").c_str());
73         fl_set_choice(dialog_->choice_valign, 2);
74         fl_set_input(dialog_->input_halign, h_align_str);
75         dialog_->input_halign->u_vdata = this;
76         fl_set_input_filter(dialog_->input_halign,
77                             C_FormMathsMatrixAlignFilter);
78         setPrehandler(dialog_->input_halign);
79
80         bcview().setOK(dialog_->button_ok);
81         bcview().setApply(dialog_->button_apply);
82         bcview().setCancel(dialog_->button_close);
83
84         bcview().addReadOnly(dialog_->slider_rows);
85         bcview().addReadOnly(dialog_->slider_columns);
86         bcview().addReadOnly(dialog_->choice_valign);
87         bcview().addReadOnly(dialog_->input_halign);
88 }
89
90
91 void FormMathsMatrix::apply()
92 {
93         char const c = v_align_c[fl_get_choice(dialog_->choice_valign) - 1];
94         char const * sh = fl_get_input(dialog_->input_halign);
95         int const nx = int(fl_get_slider_value(dialog_->slider_columns) + 0.5);
96         int const ny = int(fl_get_slider_value(dialog_->slider_rows) + 0.5);
97
98         ostringstream os;
99         os << nx << ' ' << ny << ' ' << c << ' ' << sh;
100         controller().dispatchMatrix(STRCONV(os.str()));
101 }
102
103
104 void FormMathsMatrix::update()
105 {
106         bc().valid();
107 }
108
109
110 ButtonPolicy::SMInput FormMathsMatrix::input(FL_OBJECT * ob, long)
111 {
112         if (ob == dialog_->choice_valign ||
113             ob == dialog_->slider_rows) return ButtonPolicy::SMI_VALID;
114
115         int const nx = int(fl_get_slider_value(dialog_->slider_columns)+0.5);
116         for (int i = 0; i < nx; ++i)
117                 h_align_str[i] = 'c';
118
119         h_align_str[nx] = '\0';
120
121         fl_set_input(dialog_->input_halign, h_align_str);
122         fl_redraw_object(dialog_->input_halign);
123         return ButtonPolicy::SMI_VALID;
124 }
125
126
127 int FormMathsMatrix::AlignFilter(char const * cur, int c)
128 {
129         size_t const len = strlen(cur);
130
131         int const n = int(fl_get_slider_value(dialog_->slider_columns) + 0.5)
132                 - int(len)
133                 + int(lyx::count(cur, cur + len, '|'));
134         if (n < 0)
135                 return FL_INVALID;
136
137         if (c == 'c' || c == 'l' || c == 'r' || c == '|')
138                 return FL_VALID;
139
140         return FL_INVALID;
141 }