]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsBitmap.C
Change glob() API to accept a dir parameter.
[lyx.git] / src / frontends / xforms / FormMathsBitmap.C
1 /**
2  * \file FormMathsBitmap.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 John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "FormMathsBitmap.h"
16
17 #include "bmtable.h"
18 #include "forms_gettext.h"
19 #include "xformsBC.h"
20
21 #include "ControlMath.h"
22
23
24 using std::max;
25 using std::vector;
26 using std::string;
27
28 namespace lyx {
29 namespace frontend {
30
31 extern  "C" void C_FormDialogView_CancelCB(FL_OBJECT *, long);
32 extern  "C" void C_FormDialogView_InputCB(FL_OBJECT *, long);
33
34 FD_maths_bitmap::~FD_maths_bitmap()
35 {
36         if (form->visible) fl_hide_form(form);
37         fl_free_form(form);
38 }
39
40
41 typedef FormController<ControlMath, FormView<FD_maths_bitmap> > base_class;
42
43
44 FormMathsBitmap::FormMathsBitmap(Dialog & parent, string const & t, vector<string> const & l)
45         : base_class(parent, t, false),
46           latex_(l), ww_(0), x_(0), y_(0), w_(0), h_(0)
47 {
48         ww_ = 2 * FL_abs(FL_BOUND_WIDTH);
49         x_ = y_ = ww_;
50         y_ += 8;
51 }
52
53
54 void FormMathsBitmap::addBitmap(BitmapStore const & bm)
55 {
56         bitmaps_.push_back(bm);
57
58         int wx = bm.bw + ww_ / 2;
59         int wy = bm.bh + ww_ / 2;
60         wx += (wx % bm.nx);
61         wy += (wy % bm.ny);
62
63         if (bm.vert) {
64                 y_ += wy + 8;
65                 h_ = max(y_, h_);
66                 w_ = max(x_ + wx + ww_, w_);
67         } else  {
68                 x_ += wx + 8;
69                 w_ = max(x_, w_);
70                 h_ = max(y_ + wy + ww_, h_);
71         }
72 }
73
74
75 void FormMathsBitmap::build()
76 {
77         BOOST_ASSERT(bitmaps_.size() > 0);
78
79         h_+= 42; // Allow room for a Close button
80
81         FD_maths_bitmap * fdui = new FD_maths_bitmap;
82
83         fdui->form = fl_bgn_form(FL_UP_BOX, w_, h_);
84         fdui->form->u_vdata = this;
85
86         fl_add_box(FL_UP_BOX, 0, 0, w_, h_, "");
87
88         x_ = y_ = ww_;
89         y_ += 8;
90
91         int y_close = 0;
92         for (vector<BitmapStore>::const_iterator it = bitmaps_.begin();
93              it < bitmaps_.end(); ++it) {
94                 FL_OBJECT * obj = buildBitmap(*it);
95
96                 bcview().addReadOnly(obj);
97                 y_close = max(y_close, obj->y + obj->h);
98         }
99         bitmaps_.clear();
100
101         x_ = (fdui->form->w - 90) / 2;
102         y_ = y_close + 10;
103
104         string const label = _("Close|^[");
105         fdui->button_close = fl_add_button(FL_NORMAL_BUTTON, x_, y_, 90, 30,
106                                            idex(label).c_str());
107         fl_set_button_shortcut(fdui->button_close, scex(label).c_str(), 1);
108         fl_set_object_lsize(fdui->button_close, FL_NORMAL_SIZE);
109         fl_set_object_callback(fdui->button_close, C_FormDialogView_CancelCB, 0);
110
111         fl_end_form();
112
113         fdui->form->fdui = fdui;
114
115         dialog_.reset(fdui);
116 }
117
118
119
120 FL_OBJECT * FormMathsBitmap::buildBitmap(BitmapStore const & bmstore)
121 {
122         // Add a bitmap to a button panel: one bitmap per panel.
123         // nt is the number of buttons and nx, ny the nr. of buttons
124         // in x and y direction.
125         // bw, bh and data are the bitmap dimensions width, height and
126         // bit pattern; these come directly from an .xbm file included
127         // as source.
128         // vert indicates whether the next button panel within this
129         // window will be below (true, default) or next to this one.
130         //
131         // The scaling of the bitmap on top of the buttons will be
132         // correct if the nx, ny values are given correctly.
133         int wx = bmstore.bw + ww_ / 2;
134         int wy = bmstore.bh + ww_ / 2;
135         wx += (wx % bmstore.nx);
136         wy += (wy % bmstore.ny);
137
138         FL_OBJECT * obj = fl_add_bmtable(1, x_, y_, wx, wy, "");
139         fl_set_object_lcol(obj, FL_BLUE);
140         fl_set_object_boxtype(obj, FL_UP_BOX);
141         fl_set_bmtable_data(obj, bmstore.nx, bmstore.ny, bmstore.bw, bmstore.bh,
142                             bmstore.data);
143         fl_set_bmtable_maxitems(obj, bmstore.nt);
144         fl_set_object_callback(obj, C_FormDialogView_InputCB, 0);
145
146         if (bmstore.vert) {
147                 y_ += wy + 8;
148         } else  {
149                 x_ += wx + 8;
150         }
151
152         return obj;
153 }
154
155
156 int FormMathsBitmap::GetIndex(FL_OBJECT * ob_in)
157 {
158         int k = 0;
159
160         for (FL_OBJECT * ob = form()->first; ob; ob = ob->next) {
161                 if (ob->objclass != FL_BMTABLE)
162                         continue;
163
164                 if (ob == ob_in)
165                         return k + fl_get_bmtable(ob);
166                 else
167                         k += fl_get_bmtable_maxitems(ob);
168         }
169
170         return -1;
171 }
172
173
174 void FormMathsBitmap::apply()
175 {
176         string::size_type const i = latex_chosen_.find(' ');
177         if (i != string::npos) {
178                 controller().dispatchFunc(LFUN_MATH_MODE);
179                 controller().dispatchInsert(latex_chosen_.substr(0,i));
180                 controller().dispatchInsert('\\' + latex_chosen_.substr(i + 1));
181         } else
182                 controller().dispatchInsert(latex_chosen_);
183 }
184
185
186 ButtonPolicy::SMInput FormMathsBitmap::input(FL_OBJECT * ob, long)
187 {
188         int const i = GetIndex(ob);
189
190         if (i < 0)
191                 return ButtonPolicy::SMI_INVALID;
192
193         latex_chosen_ = latex_[i];
194         apply();
195         return ButtonPolicy::SMI_VALID;
196 }
197
198 } // namespace frontend
199 } // namespace lyx