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
14 #pragma implementation
17 #include "support/filetools.h"
21 #include "ControlMath.h"
23 #include "QMathDialog.h"
26 #include "iconpalette.h"
27 #include "QDelimiterDialog.h"
28 #include "QMathMatrixDialog.h"
30 #include <qapplication.h>
31 #include <qwidgetstack.h>
32 #include <qcombobox.h>
33 #include <qpushbutton.h>
36 #include <qscrollview.h>
38 #include <qpopupmenu.h>
46 class QScrollViewSingle : public QScrollView {
48 QScrollViewSingle(QWidget * p)
49 : QScrollView(p), w_(0) {
50 setResizePolicy(Manual);
51 setHScrollBarMode(AlwaysOff);
52 setVScrollBarMode(AlwaysOn);
53 setBackgroundMode(PaletteBackground);
54 viewport()->setBackgroundMode(PaletteBackground);
57 void setChild(QWidget * w) {
59 setMinimumWidth(verticalScrollBar()->width() + w_->width() + 4);
63 virtual void resizeEvent(QResizeEvent * e) {
64 QScrollView::resizeEvent(e);
68 w_->resize(viewport()->width(), w_->height());
69 // force the resize to get accurate scrollbars
70 qApp->processEvents();
71 resizeContents(w_->width(), w_->height());
79 char const ** panels[] = {
80 latex_bop, latex_varsz, latex_brel, latex_greek, latex_arrow,
81 latex_dots, latex_deco, latex_misc, latex_ams_ops,
82 latex_ams_rel, latex_ams_nrel, latex_ams_arrows,
86 int const nr_panels = sizeof(panels)/sizeof(panels[0]);
88 bool panel_initialised[nr_panels];
93 QMathDialog::QMathDialog(QMath * form)
94 : QMathDialogBase(0, 0, false, 0),
97 // enlarge the symbols ComboBox (no scrollbar)
98 symbolsCO->setSizeLimit(13);
100 connect(symbolsCO, SIGNAL(activated(int)), symbolsWS, SLOT(raiseWidget(int)));
102 for (int i = 0; *function_names[i]; ++i) {
103 functionsLB->insertItem(function_names[i]);
106 for (int i = 0; i < nr_panels; ++i) {
107 QScrollViewSingle * view = new QScrollViewSingle(symbolsWS);
108 symbolsWS->addWidget(view, i);
111 // aboutToShow() only fires when != 0 in Qt 2 !
112 symbolsWS->raiseWidget(0);
114 panel_initialised[0] = true;
116 connect(symbolsWS, SIGNAL(aboutToShow(int)), this, SLOT(showingPanel(int)));
118 QPopupMenu * m = new QPopupMenu(spacePB);
119 m->setCaption(_("LyX: Insert space"));
120 m->insertTearOffHandle();
121 m->insertItem(_("Thin space \\,"), 1);
122 m->insertItem(_("Medium space \\:"), 2);
123 m->insertItem(_("Thick space \\;"), 3);
124 m->insertItem(_("Quadratin space \\quad"), 4);
125 m->insertItem(_("Double quadratin space \\qquad"), 5);
126 m->insertItem(_("Negative space \\!"), 6);
127 connect(m, SIGNAL(activated(int)), this, SLOT(insertSpace(int)));
128 spacePB->setPopup(m);
130 m = new QPopupMenu(sqrtPB);
131 m->setCaption(_("LyX: Insert root"));
132 m->insertTearOffHandle();
133 m->insertItem(_("Square root \\sqrt"), 1);
134 m->insertItem(_("Cube root \\root"), 2);
135 m->insertItem(_("Other root \\root"), 3);
136 connect(m, SIGNAL(activated(int)), this, SLOT(insertRoot(int)));
139 m = new QPopupMenu(stylePB);
140 m->setCaption(_("LyX: Set math style"));
141 m->insertTearOffHandle();
142 m->insertItem(_("Display style \\displaystyle"), 1);
143 m->insertItem(_("Normal text style \\textstyle"), 2);
144 m->insertItem(_("Script (small) style \\scriptstyle"), 3);
145 m->insertItem(_("Scriptscript (smaller) style \\scriptscriptstyle"), 4);
146 connect(m, SIGNAL(activated(int)), this, SLOT(insertStyle(int)));
147 stylePB->setPopup(m);
149 m = new QPopupMenu(fontPB);
150 m->setCaption(_("LyX: Set math font"));
151 m->insertTearOffHandle();
152 m->insertItem(_("Roman \\mathrm"), 1);
153 m->insertItem(_("Bold \\mathbf"), 2);
154 m->insertItem(_("San serif \\mathsf"), 3);
155 m->insertItem(_("Italic \\mathit"), 4);
156 m->insertItem(_("Typewriter \\mathtt"), 5);
157 m->insertItem(_("Blackboard \\mathbb"), 6);
158 m->insertItem(_("Fraktur \\mathfrak"), 7);
159 m->insertItem(_("Calligraphic \\mathcal"), 8);
160 m->insertItem(_("Normal text mode \\textrm"), 9);
161 connect(m, SIGNAL(activated(int)), this, SLOT(insertFont(int)));
166 void QMathDialog::showingPanel(int num)
168 if (panel_initialised[num])
173 // Qt needs to catch up. Dunno why.
174 qApp->processEvents();
176 panel_initialised[num] = true;
180 IconPalette * QMathDialog::makePanel(QWidget * parent, char const ** entries)
182 IconPalette * p = new IconPalette(parent);
183 for (int i = 0; *entries[i]; ++i) {
184 p->add(QPixmap(find_xpm(entries[i]).c_str()), entries[i], string("\\") + entries[i]);
186 connect(p, SIGNAL(button_clicked(string const &)), this, SLOT(symbol_clicked(string const &)));
192 void QMathDialog::addPanel(int num)
194 QScrollViewSingle * view = static_cast<QScrollViewSingle*>(symbolsWS->visibleWidget());
195 IconPalette * p = makePanel(view->viewport(), panels[num]);
200 void QMathDialog::symbol_clicked(string const & str)
206 void QMathDialog::fracClicked()
208 form_->insert("frac");
212 void QMathDialog::delimiterClicked()
215 QDelimiterDialog * d = new QDelimiterDialog(form_);
220 void QMathDialog::expandClicked()
222 int const id = symbolsWS->id(symbolsWS->visibleWidget());
223 IconPalette * p = makePanel(0, panels[id]);
225 s += symbolsCO->text(id).latin1();
226 p->setCaption(s.c_str());
227 p->resize(40 * 5, p->height());
229 p->setMaximumSize(p->width(), p->height());
233 void QMathDialog::functionSelected(const QString & str)
235 form_->insert(str.latin1());
239 void QMathDialog::matrixClicked()
242 QMathMatrixDialog * d = new QMathMatrixDialog(form_);
247 void QMathDialog::equationClicked()
249 form_->toggleDisplay();
253 void QMathDialog::subscriptClicked()
259 void QMathDialog::superscriptClicked()
261 form_->superscript();
265 void QMathDialog::insertSpace(int id)
269 case 1: str = ","; break;
270 case 2: str = ":"; break;
271 case 3: str = ";"; break;
272 case 4: str = "quad"; break;
273 case 5: str = "qquad"; break;
274 case 6: str = "!"; break;
281 void QMathDialog::insertRoot(int id)
285 form_->insert("sqrt");
288 form_->insertCubeRoot();
291 form_->insert("root");
297 void QMathDialog::insertStyle(int id)
301 case 1: str = "displaystyle"; break;
302 case 2: str = "textstyle"; break;
303 case 3: str = "scriptstyle"; break;
304 case 4: str = "scriptscriptstyle"; break;
311 void QMathDialog::insertFont(int id)
315 case 1: str = "mathrm"; break;
316 case 2: str = "mathbf"; break;
317 case 3: str = "mathsf"; break;
318 case 4: str = "mathit"; break;
319 case 5: str = "mathtt"; break;
320 case 6: str = "mathbb"; break;
321 case 7: str = "mathfrak"; break;
322 case 8: str = "mathcal"; break;
323 case 9: str = "textrm"; break;