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