2 * \file GuiDelimiter.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "GuiDelimiter.h"
15 #include "GuiApplication.h"
18 #include "qt_helpers.h"
23 #include <QListWidgetItem>
25 // Set to zero if unicode symbols are preferred.
30 static char const * latex_delimiters[] = {
31 "(", ")", "{", "}", "[", "]",
32 "lceil", "rceil", "lfloor", "rfloor", "langle", "rangle",
33 "uparrow", "updownarrow", "Uparrow", "Updownarrow", "downarrow", "Downarrow",
34 "|", "Vert", "/", "backslash", ""
38 static int const nr_latex_delimiters =
39 sizeof(latex_delimiters) / sizeof(char const *);
41 static QString const bigleft[] = {"", "bigl", "Bigl", "biggl", "Biggl"};
43 static QString const bigright[] = {"", "bigr", "Bigr", "biggr", "Biggr"};
45 static char const * const biggui[] = {
46 N_("big[[delimiter size]]"),
47 N_("Big[[delimiter size]]"),
48 N_("bigg[[delimiter size]]"),
49 N_("Bigg[[delimiter size]]"),
54 // FIXME: It might be better to fix the big delim LFUN to not require
55 // additional '\' prefix.
56 static QString fix_name(QString const & str, bool big)
60 if (!big || str == "(" || str == ")" || str == "[" || str == "]"
61 || str == "|" || str == "/")
72 GuiDelimiter::GuiDelimiter(LyXView & lv)
73 : GuiMath(lv, "mathdelimiter")
76 setViewTitle(_("Math Delimiter"));
77 setController(this, false);
79 connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
81 setFocusProxy(leftLW);
83 leftLW->setViewMode(QListView::IconMode);
84 rightLW->setViewMode(QListView::IconMode);
86 typedef std::map<char_type, QListWidgetItem *> ListItems;
88 // The last element is the empty one.
89 int const end = nr_latex_delimiters - 1;
90 for (int i = 0; i < end; ++i) {
91 string const delim = latex_delimiters[i];
92 MathSymbol const & ms = mathSymbol(delim);
93 QString symbol(ms.fontcode?
94 QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
95 QListWidgetItem * lwi = new QListWidgetItem(symbol);
96 lwi->setToolTip(toqstr(delim));
98 lyxfont.setFamily(ms.fontfamily);
99 QFont const & symbol_font = guiApp->guiFontLoader().get(lyxfont);
100 lwi->setFont(symbol_font);
101 list_items[ms.unicode] = lwi;
102 leftLW->addItem(lwi);
105 for (int i = 0; i != leftLW->count(); ++i) {
106 MathSymbol const & ms = mathSymbol(
107 fromqstr(leftLW->item(i)->toolTip()));
108 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
111 // The last element is the empty one.
112 leftLW->addItem(qt_("(None)"));
113 rightLW->addItem(qt_("(None)"));
115 sizeCO->addItem(qt_("Variable"));
117 for (int i = 0; *biggui[i]; ++i)
118 sizeCO->addItem(qt_(biggui[i]));
120 on_leftLW_currentRowChanged(0);
121 bc().setPolicy(ButtonPolicy::IgnorantPolicy);
125 char_type GuiDelimiter::doMatch(char_type const symbol)
127 string const & str = texName(symbol);
129 if (str == "(") match = ")";
130 else if (str == ")") match = "(";
131 else if (str == "[") match = "]";
132 else if (str == "]") match = "[";
133 else if (str == "{") match = "}";
134 else if (str == "}") match = "{";
135 else if (str == "l") match = "r";
136 else if (str == "rceil") match = "lceil";
137 else if (str == "lceil") match = "rceil";
138 else if (str == "rfloor") match = "lfloor";
139 else if (str == "lfloor") match = "rfloor";
140 else if (str == "rangle") match = "langle";
141 else if (str == "langle") match = "rangle";
142 else if (str == "backslash") match = "/";
143 else if (str == "/") match = "backslash";
146 return mathSymbol(match).unicode;
150 void GuiDelimiter::updateTeXCode(int size)
152 bool const bigsize = size != 0;
154 QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
155 QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
158 tex_code_ = left_str + ' ' + right_str;
160 tex_code_ = bigleft[size] + ' '
162 + bigright[size] + ' '
166 // Generate TeX-code for GUI display.
167 // FIXME: Instead of reconstructing the TeX code it would be nice to
168 // FIXME: retrieve the LateX code directly from mathed.
169 // In all cases, we want the '\' prefix if needed, so we pass 'true'
171 left_str = fix_name(leftLW->currentItem()->toolTip(), true);
172 right_str = fix_name(rightLW->currentItem()->toolTip(), true);
175 code_str = "\\left" + left_str + " \\right" + right_str;
177 // There should be nothing in the TeX-code when the delimiter is "None".
179 code_str = "\\" + bigleft[size] + left_str + ' ';
180 if (right_str != ".")
181 code_str += "\\" + bigright[size] + right_str;
184 texCodeL->setText(qt_("TeX Code: ") + code_str);
188 void GuiDelimiter::on_insertPB_clicked()
190 if (sizeCO->currentIndex() == 0)
191 dispatchDelim(fromqstr(tex_code_));
193 QString command = '"' + tex_code_ + '"';
194 command.replace(' ', "\" \"");
195 dispatchBigDelim(fromqstr(command));
200 void GuiDelimiter::on_sizeCO_activated(int index)
202 updateTeXCode(index);
206 void GuiDelimiter::on_leftLW_itemActivated(QListWidgetItem *)
208 on_insertPB_clicked();
213 void GuiDelimiter::on_rightLW_itemActivated(QListWidgetItem *)
215 on_insertPB_clicked();
220 void GuiDelimiter::on_leftLW_currentRowChanged(int item)
222 if (matchCB->isChecked())
223 rightLW->setCurrentRow(item);
225 updateTeXCode(sizeCO->currentIndex());
229 void GuiDelimiter::on_rightLW_currentRowChanged(int item)
231 if (matchCB->isChecked())
232 leftLW->setCurrentRow(item);
234 updateTeXCode(sizeCO->currentIndex());
238 void GuiDelimiter::on_matchCB_stateChanged(int state)
240 if (state == Qt::Checked)
241 on_leftLW_currentRowChanged(leftLW->currentRow());
243 updateTeXCode(sizeCO->currentIndex());
247 Dialog * createGuiDelimiter(LyXView & lv) { return new GuiDelimiter(lv); }
250 } // namespace frontend
253 #include "GuiDelimiter_moc.cpp"