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