]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsBitmap.C
Yet more dialog tweaking from Rob.
[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 "bmtable.h"
21 #include "debug.h"
22 #include "forms_gettext.h"
23 #include "gettext.h"
24 #include "support/LAssert.h"
25
26 #include XPM_H_LOCATION
27
28 #include <algorithm>
29 #include <iomanip>
30
31 using std::vector;
32 using std::endl;
33 using std::setw;
34 using std::max;
35
36 extern  "C" void C_FormBaseDeprecatedCancelCB(FL_OBJECT *, long);
37 extern  "C" void C_FormBaseDeprecatedInputCB(FL_OBJECT *, long);
38
39 FormMathsBitmap::FormMathsBitmap(LyXView & lv, Dialogs & d,
40                                  FormMathsPanel const & p, string const & t,
41                                  vector<string> const & l)
42         : FormMathsSub(lv, d, p, 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 FormMathsBitmap::~FormMathsBitmap()
52 {
53         if (!form())
54                 return;
55
56         if (form()->visible) fl_hide_form(form());
57         fl_free_form(form());
58 }
59
60
61 FL_FORM * FormMathsBitmap::form() const
62 {
63         return form_.get();
64 }
65
66
67 void FormMathsBitmap::build()
68 {
69         lyx::Assert(bitmaps_.size() > 0);
70
71         h_+= 50; // Allow room for a Close button
72
73         form_.reset(fl_bgn_form(FL_UP_BOX, w_, h_), fl_free_form);
74         form_->u_vdata = this;
75
76         fl_add_box(FL_UP_BOX, 0, 0, w_, h_, "");
77
78         y_ = 0;
79         for (vector<bm_ptr>::const_iterator it = bitmaps_.begin();
80              it < bitmaps_.end(); ++it) {
81                 FL_OBJECT * obj = it->get();
82
83                 fl_add_object(form_.get(), obj);
84                 bc().addReadOnly(obj);
85
86                 y_ = max(y_, obj->y + obj->h);
87         }
88
89         char const * const label = _("Close|^[");
90         x_ = (form_->w - 90) / 2;
91         y_ += 10;
92
93         FL_OBJECT * button_close =
94                 fl_add_button(FL_NORMAL_BUTTON, x_, y_, 90, 30, idex(_(label)));
95         fl_set_button_shortcut(button_close, scex(_(label)), 1);
96         fl_set_object_lsize(button_close, FL_NORMAL_SIZE);
97         fl_set_object_callback(button_close, C_FormBaseDeprecatedCancelCB, 0);
98
99         fl_end_form();
100
101         bc().setCancel(button_close);
102 }
103
104
105 void FormMathsBitmap::addBitmap(int nt, int nx, int ny, int bw, int bh,
106                                 unsigned char const * data, bool vert)
107 {
108         // Add a bitmap to a button panel: one bitmap per panel.
109         // nt is the number of buttons and nx, ny the nr. of buttons
110         // in x and y direction.
111         // bw, bh and data are the bitmap dimensions width, height and
112         // bit pattern; these come directly from an .xbm file included
113         // as source.
114         // vert indicates whether the next button panel within this
115         // window will be below (true, default) or next to this one.
116         //
117         // The scaling of the bitmap on top of the buttons will be
118         // correct if the nx, ny values are given correctly.
119         int wx = bw + ww_ / 2;
120         int wy = bh + ww_ / 2;
121         wx += (wx % nx);
122         wy += (wy % ny);
123         FL_OBJECT * obj = fl_create_bmtable(1, x_, y_, wx, wy, "");
124         fl_set_object_lcol(obj, FL_BLUE);
125         fl_set_object_boxtype(obj, FL_UP_BOX);
126         fl_set_bmtable_data(obj, nx, ny, bw, bh, data);
127         fl_set_bmtable_maxitems(obj, nt);
128         fl_set_object_callback(obj, C_FormBaseDeprecatedInputCB, 0);
129
130         if (vert) {
131                 y_ += wy + 8;
132                 h_ = max(y_, h_);
133                 w_ = max(x_ + wx + ww_, w_);
134         } else  {
135                 x_ += wx + 8;
136                 w_ = max(x_, w_);
137                 h_ = max(y_ + wy + ww_, h_);
138         }
139
140         bitmaps_.push_back(bm_ptr(obj, fl_free_object));
141 }
142
143
144 int FormMathsBitmap::GetIndex(FL_OBJECT * ob)
145 {
146         int k = 0;
147         for (vector<bm_ptr>::const_iterator it = bitmaps_.begin();
148              it < bitmaps_.end(); ++it) {
149                 if (it->get() == ob)
150                         return k + fl_get_bmtable(ob);
151                 else
152                         k += fl_get_bmtable_maxitems(it->get());
153         }
154         return -1;
155 }
156
157
158 void FormMathsBitmap::apply()
159 {
160         string::size_type const i = latex_chosen_.find(' ');
161         if (i != string::npos) {
162                 parent_.dispatchFunc(LFUN_MATH_MODE);
163                 parent_.insertSymbol(latex_chosen_.substr(0,i));
164                 parent_.insertSymbol(latex_chosen_.substr(i + 1), false);
165         } else
166                 parent_.insertSymbol(latex_chosen_);
167 }
168
169
170 bool FormMathsBitmap::input(FL_OBJECT * ob, long)
171 {
172         int const i = GetIndex(ob);
173
174         if (i < 0)
175                 return false;
176
177         latex_chosen_ = latex_[i];
178         apply();
179         return true;
180 }