]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GMathDelim.C
Display accelerator (binding) labels in menus.
[lyx.git] / src / frontends / gtk / GMathDelim.C
1 /**
2  * \file GMathDelim.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlMath.h"
14 #include "GMathDelim.h"
15 #include "ghelpers.h"
16
17 #include "support/lstrings.h"
18
19 #include <libglademm.h>
20
21 #include <sstream>
22
23 #include "delim.xbm"
24 #include "delim0.xpm"
25
26 using std::string;
27
28 namespace lyx {
29 namespace frontend {
30
31 namespace
32 {
33
34 enum enumDelimType {LEFT, RIGHT, SINGLE};
35
36
37 int const delimType[] = {
38         //row 1
39         LEFT, RIGHT, LEFT, RIGHT, SINGLE, SINGLE, LEFT, RIGHT,LEFT, RIGHT,
40         SINGLE, SINGLE,
41         //row 2
42         LEFT, RIGHT, LEFT, RIGHT, SINGLE, SINGLE, LEFT, RIGHT, SINGLE, SINGLE,
43         SINGLE
44 };
45
46
47 int const delimRevert[] = {
48         1,0,3,2,4,5,7,6,9,8,10,11,
49         13,12,15,14,16,17,19,18,20,21,22
50 };
51
52
53 char const * delimValues[] = {
54         "(", ")", "lceil",  "rceil",  "uparrow",  "Uparrow",
55         "[", "]", "lfloor", "rfloor", "updownarrow", "Updownarrow",
56         "{", "}",  "/", "backslash",  "downarrow",  "Downarrow",
57         "langle",  "rangle", "|", "Vert", ".", 0
58 };
59
60 int const delimTblRows = 2;
61
62 int const delimTblCols = 12;
63
64 int const delimMax = 23;
65
66
67 GXpmBtnTbl::XbmData xbm =
68 {
69         delim_bits,
70         delim_width,
71         delim_height,
72         {0, 0, 0, 65535}
73 };
74
75
76 inline int index(int row, int col)
77 {
78         return row * delimTblCols + col;
79 }
80
81
82 inline int indexToRow(int index)
83 {
84         return index / delimTblCols;
85 }
86
87
88 inline int indexToCol(int index)
89 {
90         return index % delimTblCols;
91 }
92
93 }
94
95
96 GMathDelim::GMathDelim(Dialog & parent) :
97         GViewCB<ControlMath, GViewGladeB>(parent, _("Math Delimiters")),
98         delimTbl_(delimTblRows, delimTblCols, xbm)
99 {
100 }
101
102
103 void GMathDelim::doBuild()
104 {
105         string const gladeName = findGladeFile("mathDelim");
106         xml_ = Gnome::Glade::Xml::create(gladeName);
107         Gtk::Button * ok;
108         Gtk::Button * apply;
109         Gtk::Button * close;
110         Gtk::Box * box;
111         xml_->get_widget("Left", left_);
112         xml_->get_widget("Right", right_);
113         xml_->get_widget("Both", both_);
114         xml_->get_widget("OK", ok);
115         xml_->get_widget("Apply", apply);
116         xml_->get_widget("Close", close);
117         xml_->get_widget("Demo", demo_);
118         setOK(ok);
119         setApply(apply);
120         setCancel(close);
121         // Initialize demo button pixmap to "()" as found in images/delim0.xpm
122         setDemoPixmap();
123         leftSel_ = 0;
124         rightSel_ = 1;
125         xml_->get_widget("Box", box);
126         delimTbl_.signalClicked().connect(
127                 sigc::mem_fun(*this, &GMathDelim::onDelimTblClicked));
128         delimTbl_.show();
129         box->children().push_back(
130                 Gtk::Box_Helpers::Element(delimTbl_));
131         bcview().addReadOnly(&delimTbl_);
132         bcview().addReadOnly(left_);
133         bcview().addReadOnly(right_);
134         bcview().addReadOnly(both_);
135         bcview().addReadOnly(demo_);
136         left_->signal_clicked().connect(
137                 sigc::mem_fun(*this, &GMathDelim::onRadioClicked));
138         right_->signal_clicked().connect(
139                 sigc::mem_fun(*this, &GMathDelim::onRadioClicked));
140         both_->signal_clicked().connect(
141                 sigc::mem_fun(*this, &GMathDelim::onRadioClicked));
142 }
143
144
145 void GMathDelim::setDemoPixmap()
146 {
147         Gtk::Image * image;
148         pixmap_ = Gdk::Pixmap::create_from_xpm(demo_->get_colormap(),
149                                                mask_,
150                                                delim0);
151         image = Gtk::manage(new Gtk::Image(pixmap_, mask_));
152         image->show();
153         demo_->add(*image);
154         gcMask_ = Gdk::GC::create(mask_);
155 }
156
157
158 void GMathDelim::apply()
159 {
160         std::ostringstream os;
161         os << delimValues[leftSel_] << ' ' << delimValues[rightSel_];
162         controller().dispatchDelim(os.str());
163 }
164
165
166 void GMathDelim::update()
167 {
168         bc().valid();
169 }
170
171
172 void GMathDelim::onDelimTblClicked(int row, int col)
173 {
174         int const sel = index(row, col);
175         if (sel >= delimMax)
176                 return;
177         bool left = left_->get_active();
178         bool right = right_->get_active();
179         bool both = both_->get_active();
180         if (left)
181                 leftSel_ = sel;
182         else if (right)
183                 rightSel_ = sel;
184         else if (both)
185                 if (delimType[sel] == LEFT) {
186                         leftSel_ = sel;
187                         rightSel_ = delimRevert[sel];
188                 } else if (delimType[sel] == RIGHT) {
189                         rightSel_ = sel;
190                         leftSel_ = delimRevert[sel];
191                 } else {
192                         leftSel_ = rightSel_ = sel;
193                 }
194         updateDemoPixmap();
195 }
196
197
198 void GMathDelim::updateDemoPixmap()
199 {
200         int const delimWidth = delim_width / delimTblCols;
201         Glib::RefPtr<Gdk::Pixmap> pixmap;
202         Glib::RefPtr<Gdk::Bitmap> mask;
203         GXpmBtnTbl::GXpmBtn * btn =  delimTbl_.getBtn(indexToRow(leftSel_),
204                                                        indexToCol(leftSel_));
205         pixmap = btn->getPixmap();
206         mask = btn->getMask();
207         pixmap_->draw_drawable(left_->get_style()->get_black_gc(),
208                                pixmap,
209                                0, 0,
210                                0, 0);
211         mask_->draw_drawable(gcMask_,
212                              mask,
213                              0, 0,
214                              0, 0);
215         btn =  delimTbl_.getBtn(indexToRow(rightSel_),
216                                 indexToCol(rightSel_));
217         pixmap = btn->getPixmap();
218         mask = btn->getMask();
219         pixmap_->draw_drawable(left_->get_style()->get_black_gc(),
220                                pixmap,
221                                0, 0,
222                                delimWidth, 0);
223         mask_->draw_drawable(gcMask_,
224                              mask,
225                              0, 0,
226                              delimWidth, 0);
227         int x, y, width, height, depth;
228         demo_->get_window()->get_geometry(x, y, width, height, depth);
229         demo_->get_window()->invalidate_rect(
230                 Gdk::Rectangle(x, y, width, height), true);
231         bc().valid();
232 }
233
234
235 void GMathDelim::onRadioClicked()
236 {
237         bc().valid();
238 }
239
240 } // namespace frontend
241 } // namespace lyx