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