]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormMathsMatrix.C
change "support/std_sstream.h" to <sstream>
[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/lyxalgo.h" // lyx::count
25
26 #include "lyx_forms.h"
27
28 #include <sstream>
29
30
31 using std::ostringstream;
32
33 #ifndef CXX_GLOBAL_CSTD
34 using std::strlen;
35 #endif
36
37 namespace lyx {
38 namespace frontend {
39
40 namespace {
41
42 char h_align_str[80] = "c";
43 char v_align_c[] = "tcb";
44
45
46 extern "C"
47 int C_FormMathsMatrixAlignFilter(FL_OBJECT * ob, char const *,
48                                  char const * cur, int c)
49 {
50         BOOST_ASSERT(ob);
51         FormMathsMatrix * pre =
52                 static_cast<FormMathsMatrix *>(ob->u_vdata);
53         BOOST_ASSERT(pre);
54         return pre->AlignFilter(cur, c);
55 }
56
57 } // namespace anon
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(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(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 }
142
143 } // namespace frontend
144 } // namespace lyx