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