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