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