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