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