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