]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormMathsBitmap.C
move more support functions into namespace lyx, small other changes
[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 #include <algorithm>
13 #include <iomanip>
14  
15 #include XPM_H_LOCATION
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "FormMathsBitmap.h"
22
23 #include "Dialogs.h"
24 #include "LyXView.h"
25 #include "bmtable.h"
26 #include "debug.h"
27 #include "lyx_gui_misc.h" // scex, idex
28 #include "gettext.h"
29
30 using std::vector;
31 using std::endl;
32 using std::setw;
33 using std::max;
34
35 extern  "C" void C_FormBaseDeprecatedCancelCB(FL_OBJECT *, long);
36 extern  "C" void C_FormBaseDeprecatedInputCB(FL_OBJECT *, long);
37
38 FormMathsBitmap::FormMathsBitmap(LyXView * lv, Dialogs * d,   
39                                  FormMathsPanel const & p,
40                                  vector<string> const & l)
41         : FormMathsSub(lv, d, p, _("Maths Bitmaps")),
42           latex_(l), ww_(0), x_(0), y_(0), w_(0), h_(0)
43 {
44         ww_ = 2 * FL_abs(FL_BOUND_WIDTH);
45         x_ = y_ = ww_;
46         y_ += 8;
47 }
48
49
50 FL_FORM * FormMathsBitmap::form() const
51 {
52         return form_;
53 }
54
55
56 void FormMathsBitmap::build()
57 {
58         lyx::Assert(bitmaps_.size() > 0);
59
60         h_+= 50; // Allow room for a Close button
61
62         form_ = fl_bgn_form(FL_UP_BOX, w_, h_);
63         form_->u_vdata = this;
64
65         FL_OBJECT * obj = fl_add_box(FL_UP_BOX, 0, 0, w_, h_, "");
66
67         y_ = 0;
68         for (vector<bm_ptr>::const_iterator it = bitmaps_.begin();
69              it < bitmaps_.end(); ++it) {
70                 FL_OBJECT * obj = it->get();
71
72                 fl_add_object(form_, obj);
73                 bc().addReadOnly(obj);
74
75                 y_ = max(y_, obj->y + obj->h);
76         }
77  
78         char const * const label = N_("Close|^[");
79
80         x_ = (form_->w - 90) / 2;
81         y_ += 10;
82                 
83         FL_OBJECT * button_cancel = obj = 
84                 fl_add_button(FL_NORMAL_BUTTON, x_, y_, 90, 30, idex(_(label)));
85         fl_set_button_shortcut(obj, scex(_(label)), 1);
86
87         fl_set_object_lsize(obj, FL_NORMAL_SIZE);
88         fl_set_object_callback(obj, C_FormBaseDeprecatedCancelCB, 0);
89
90         fl_end_form();
91
92         bc().setCancel(button_cancel);
93 }
94
95
96 void FormMathsBitmap::addBitmap(int nt, int nx, int ny, int bw, int bh,
97                                 unsigned char const * data, bool vert)
98 {
99         int wx = bw + ww_ / 2;
100         int wy = bh + ww_ / 2;
101         wx += (wx % nx);
102         wy += (wy % ny);
103
104         FL_OBJECT * obj = fl_create_bmtable(1, x_, y_, wx, wy, "");
105         fl_set_object_lcol(obj, FL_BLUE);
106         fl_set_object_boxtype(obj, FL_UP_BOX);
107         fl_set_bmtable_data(obj, nx, ny, bw, bh, data);
108         fl_set_bmtable_maxitems(obj, nt);
109         fl_set_object_callback(obj, C_FormBaseDeprecatedInputCB, 0);
110
111         if (vert) {
112                 y_ += wy + 8;
113                 h_ = max(y_, h_);
114                 w_ = max(x_ + wx + ww_, w_);
115         } else  {
116                 x_ += wx + 8;
117                 w_ = max(x_, w_);
118                 h_ = max(y_ + wy + ww_, h_);
119         }
120
121         bitmaps_.push_back(bm_ptr(obj));
122 }
123
124
125 int FormMathsBitmap::GetIndex(FL_OBJECT * ob)
126 {
127         int k = 0;
128         for (vector<bm_ptr>::const_iterator it = bitmaps_.begin();
129              it < bitmaps_.end(); ++it) {
130                 if (it->get() == ob)
131                         return k + fl_get_bmtable(ob);
132                 else
133                         k += fl_get_bmtable_maxitems(it->get());
134         }
135         return -1;
136 }
137
138
139 void FormMathsBitmap::apply()
140 {
141         parent_.insertSymbol(latex_chosen_);
142 }
143
144
145 bool FormMathsBitmap::input(FL_OBJECT * ob, long)
146 {
147         int const i = GetIndex(ob);
148
149         if (i < 0) 
150                 return false;
151
152         latex_chosen_ = latex_[i];
153         apply();
154         return true;
155 }