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