]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsMatrix.C
Really dull and boring header shit
[lyx.git] / src / frontends / xforms / FormMathsMatrix.C
1 /**
2  * \file FormMathsMatrix.C
3  * See the file COPYING.
4  *
5  * \author Alejandro Aguilar Sierra
6  * \author Pablo De Napoli
7  * \author John Levon
8  * \author Angus Leeming 
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "FormMathsMatrix.h"
20 #include "forms/form_maths_matrix.h"
21
22 #include "funcrequest.h"
23 #include "lyxfunc.h"
24
25 #include "frontends/LyXView.h"
26
27 #include "support/LAssert.h"
28 #include "support/lyxalgo.h" // lyx::count
29
30 #include "Lsstream.h"
31
32 #include FORMS_H_LOCATION
33
34 #include <algorithm>
35
36 #ifndef CXX_GLOBAL_CSTD
37 using std::strlen;
38 #endif
39
40
41 static char h_align_str[80] = "c";
42 static char v_align_c[] = "tcb";
43
44
45 extern "C" {
46
47         static
48         int C_FormMathsMatrixAlignFilter(FL_OBJECT * ob, char const *,
49                                          char const * cur, int c)
50         {
51                 lyx::Assert(ob);
52                 FormMathsMatrix * pre =
53                         static_cast<FormMathsMatrix *>(ob->u_vdata);
54                 lyx::Assert(pre);
55                 return pre->AlignFilter(cur, c);
56         }
57
58 }
59
60
61 FormMathsMatrix::FormMathsMatrix(LyXView & lv, Dialogs & d,
62                                  FormMathsPanel const & p)
63         : FormMathsSub(lv, d, p, _("Maths Matrix"), false)
64 {}
65
66
67 FL_FORM * FormMathsMatrix::form() const
68 {
69         if (dialog_.get())
70                 return dialog_->form;
71         return 0;
72 }
73
74
75 void FormMathsMatrix::build()
76 {
77         dialog_.reset(build_maths_matrix(this));
78
79         fl_addto_choice(dialog_->choice_valign, _("Top | Center | Bottom"));
80         fl_set_choice(dialog_->choice_valign, 2);
81         fl_set_input(dialog_->input_halign, h_align_str);
82         dialog_->input_halign->u_vdata = this;
83         fl_set_input_filter(dialog_->input_halign,
84                             C_FormMathsMatrixAlignFilter);
85         setPrehandler(dialog_->input_halign);
86
87         bc().setOK(dialog_->button_ok);
88         bc().setApply(dialog_->button_apply);
89         bc().setCancel(dialog_->button_close);
90
91         bc().addReadOnly(dialog_->slider_rows);
92         bc().addReadOnly(dialog_->slider_columns);
93         bc().addReadOnly(dialog_->choice_valign);
94         bc().addReadOnly(dialog_->input_halign);
95 }
96
97
98 void FormMathsMatrix::apply()
99 {
100         char const c = v_align_c[fl_get_choice(dialog_->choice_valign) - 1];
101         char const * sh = fl_get_input(dialog_->input_halign);
102         int const nx = int(fl_get_slider_value(dialog_->slider_columns) + 0.5);
103         int const ny = int(fl_get_slider_value(dialog_->slider_rows) + 0.5);
104
105         ostringstream os;
106         os << nx << ' ' << ny << ' ' << c << ' ' << sh;
107
108         lv_.dispatch(FuncRequest(LFUN_INSERT_MATRIX, os.str().c_str()));
109 }
110
111
112 bool FormMathsMatrix::input(FL_OBJECT * ob, long)
113 {
114         if (ob == dialog_->choice_valign ||
115             ob == dialog_->slider_rows) return true;
116
117         int const nx = int(fl_get_slider_value(dialog_->slider_columns)+0.5);
118         for (int i = 0; i < nx; ++i)
119                 h_align_str[i] = 'c';
120
121         h_align_str[nx] = '\0';
122
123         fl_set_input(dialog_->input_halign, h_align_str);
124         fl_redraw_object(dialog_->input_halign);
125         return true;
126 }
127
128
129 int FormMathsMatrix::AlignFilter(char const * cur, int c)
130 {
131         size_t const len = strlen(cur);
132
133         int const n = int(fl_get_slider_value(dialog_->slider_columns) + 0.5)
134                 - int(len)
135                 + int(lyx::count(cur, cur + len, '|'));
136         if (n < 0)
137                 return FL_INVALID;
138
139         if (c == 'c' || c == 'l' || c == 'r' || c == '|')
140                 return FL_VALID;
141
142         return FL_INVALID;
143 }