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