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