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