]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsMatrix.C
bug 183
[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, a.leeming@ic.ac.uk
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG_
15 #pragma implementation
16 #endif
17
18 #include <algorithm>
19
20 #include "FormMathsMatrix.h"
21 #include "form_maths_matrix.h"
22 #include "Dialogs.h"
23 #include "LyXView.h"
24 #include "Lsstream.h"
25 #include "lyxfunc.h"
26 #include "support/LAssert.h"
27
28 #ifndef CXX_GLOBAL_CSTD
29 using std::strlen;
30 #endif
31
32
33 static char h_align_str[80] = "c";
34 static char v_align_c[] = "tcb";
35
36
37 extern "C" {
38         
39         static
40         int C_FormMathsMatrixAlignFilter(FL_OBJECT * ob, char const *,
41                                          char const * cur, int c)
42         {
43                 lyx::Assert(ob);
44                 FormMathsMatrix * pre =
45                         static_cast<FormMathsMatrix *>(ob->u_vdata);
46                 lyx::Assert(pre);
47                 return pre->AlignFilter(cur, c);
48         }
49         
50 }
51
52
53 FormMathsMatrix::FormMathsMatrix(LyXView * lv, Dialogs * d,
54                                FormMathsPanel const & p)
55         : FormMathsSub(lv, d, p, _("Maths Matrix"), false)
56 {}
57
58
59 FL_FORM * FormMathsMatrix::form() const
60 {
61         if (dialog_.get())
62                 return dialog_->form;
63         return 0;
64 }
65
66
67 void FormMathsMatrix::build()
68 {
69         dialog_.reset(build_maths_matrix());
70
71         fl_addto_choice(dialog_->choice_valign, _("Top | Center | Bottom"));
72         fl_set_choice(dialog_->choice_valign, 2);
73         fl_set_input(dialog_->input_halign, h_align_str);
74         dialog_->input_halign->u_vdata = this;
75         fl_set_input_filter(dialog_->input_halign,
76                             C_FormMathsMatrixAlignFilter);
77
78         bc().setOK(dialog_->button_ok);
79         bc().setApply(dialog_->button_apply);
80         bc().setCancel(dialog_->button_cancel);
81
82         bc().addReadOnly(dialog_->slider_rows);
83         bc().addReadOnly(dialog_->slider_columns);
84         bc().addReadOnly(dialog_->choice_valign);
85         bc().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 ost;
97         ost << nx << ' ' << ny << ' ' << c << ' ' << sh;
98  
99         lv_->getLyXFunc()->dispatch(LFUN_INSERT_MATRIX, ost.str().c_str());
100 }
101
102 bool FormMathsMatrix::input(FL_OBJECT * ob, long)
103 {
104         if (ob == dialog_->choice_valign ||
105             ob == dialog_->slider_rows) return true;
106
107         int const nx = int(fl_get_slider_value(dialog_->slider_columns)+0.5);
108         for (int i = 0; i < nx; ++i)
109                 h_align_str[i] = 'c';
110  
111         h_align_str[nx] = '\0';
112
113         fl_set_input(dialog_->input_halign, h_align_str);
114         fl_redraw_object(dialog_->input_halign);
115         return true;
116 }
117
118
119 int FormMathsMatrix::AlignFilter(char const * cur, int c)
120 {
121         size_t len = strlen(cur);
122         // Use the HP version of std::count because the other one is broken on
123         // some systems, (anon)  and the HP one might even not exist... (Lgb)
124         // Before "fixing" this again, please investige _why_ the standard
125         // count is not working. Run it my be as well. (Lgb)
126 #if 0
127         int counted = 0;
128         std::count(cur, cur+len, '|', counted);
129 #else
130         int counted = std::count(cur, cur + len, '|');
131 #endif
132         
133         int const n = int(fl_get_slider_value(dialog_->slider_columns) + 0.5) -
134                 int(len) + counted;
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 }