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