]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsBitmap.C
remove defaults stuff, let Qt handle no toolbar
[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 XPM_H_LOCATION
26
27 #include <algorithm>
28
29 extern  "C" void C_FormBaseCancelCB(FL_OBJECT *, long);
30 extern  "C" void C_FormBaseInputCB(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 FormCB<ControlMathSub, FormDB<FD_maths_bitmap> > base_class;
44
45
46 FormMathsBitmap::FormMathsBitmap(string const & t, vector<string> const & l)
47         : base_class(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(int nt, int nx, int ny, int bw, int bh,
57                                 unsigned char const * data, bool vert)
58 {
59         bitmaps_.push_back(BitmapStore(nt, nx, ny, bw, bh, data, vert));
60
61         int wx = bw + ww_ / 2;
62         int wy = bh + ww_ / 2;
63         wx += (wx % nx);
64         wy += (wy % ny);
65
66         if (vert) {
67                 y_ += wy + 8;
68                 h_ = max(y_, h_);
69                 w_ = max(x_ + wx + ww_, w_);
70         } else  {
71                 x_ += wx + 8;
72                 w_ = max(x_, w_);
73                 h_ = max(y_ + wy + ww_, h_);
74         }
75 }
76
77
78 void FormMathsBitmap::build()
79 {
80         lyx::Assert(bitmaps_.size() > 0);
81
82         h_+= 42; // Allow room for a Close button
83
84         FD_maths_bitmap * fdui = new FD_maths_bitmap;
85
86         fdui->form = fl_bgn_form(FL_UP_BOX, w_, h_);
87         fdui->form->u_vdata = this;
88
89         fl_add_box(FL_UP_BOX, 0, 0, w_, h_, "");
90
91         x_ = y_ = ww_;
92         y_ += 8;
93
94         int y_close = 0;
95         for (vector<BitmapStore>::const_iterator it = bitmaps_.begin();
96              it < bitmaps_.end(); ++it) {
97                 FL_OBJECT * obj = buildBitmap(*it);
98
99                 bcview().addReadOnly(obj);
100                 y_close = max(y_close, obj->y + obj->h);
101         }
102         bitmaps_.clear();
103
104         x_ = (fdui->form->w - 90) / 2;
105         y_ = y_close + 10;
106
107         char const * const label = _("Close|^[");
108         fdui->button_close = fl_add_button(FL_NORMAL_BUTTON, x_, y_, 90, 30,
109                                            idex(label).c_str());
110         fl_set_button_shortcut(fdui->button_close, scex(label).c_str(), 1);
111         fl_set_object_lsize(fdui->button_close, FL_NORMAL_SIZE);
112         fl_set_object_callback(fdui->button_close, C_FormBaseCancelCB, 0);
113
114         fl_end_form();
115
116         fdui->form->fdui = fdui;
117
118         dialog_.reset(fdui);
119         bcview().setCancel(dialog_->button_close);
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_FormBaseInputCB, 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().insertSymbol(latex_chosen_.substr(0,i));
184                 controller().insertSymbol(latex_chosen_.substr(i + 1), false);
185         } else
186                 controller().insertSymbol(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 }