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