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