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