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