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