]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QMathDialog.C
Spaces menu for math dialog
[lyx.git] / src / frontends / qt2 / QMathDialog.C
1 /**
2  * \file QMathDialog.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #include "support/filetools.h"
12 #include "gettext.h"
13 #include "debug.h"
14  
15 #include "QMathDialog.h"
16 #include "QMath.h"
17
18 #include "ControlMath.h"
19 #include "iconpalette.h"
20  
21 #include <qapplication.h>
22 #include <qwidgetstack.h>
23 #include <qcombobox.h>
24 #include <qpushbutton.h>
25 #include <qlistbox.h>
26 #include <qpixmap.h>
27 #include <qscrollview.h>
28 #include <qlayout.h>
29 #include <qpopupmenu.h>
30 #include <qcursor.h>
31  
32 using std::min;
33 using std::max;
34 using std::endl;
35  
36 class QScrollViewSingle : public QScrollView {
37 public:
38         QScrollViewSingle(QWidget * p)
39                 : QScrollView(p), w_(0) {
40                 setResizePolicy(Manual);
41                 setHScrollBarMode(AlwaysOff);
42                 setVScrollBarMode(AlwaysOn);
43                 setBackgroundMode(PaletteBackground);
44                 viewport()->setBackgroundMode(PaletteBackground);
45         }
46  
47         void setChild(QWidget * w) {
48                 w_ = w; 
49                 setMinimumWidth(verticalScrollBar()->width() + w_->width() + 4);
50                 addChild(w_);
51         }
52
53 protected:
54         virtual void resizeEvent(QResizeEvent * e) {
55                 QScrollView::resizeEvent(e);
56                 if (!w_)
57                         return;
58  
59                 w_->resize(viewport()->width(), w_->height());
60                 // force the resize to get accurate scrollbars
61                 qApp->processEvents();
62                 resizeContents(w_->width(), w_->height());
63         }
64
65 private:
66         QWidget * w_; 
67 };
68  
69 namespace { 
70         char const ** panels[] = {
71                 latex_bop, latex_varsz, latex_brel, latex_greek, latex_arrow,
72                 latex_dots, latex_deco, latex_misc, latex_ams_ops,
73                 latex_ams_rel, latex_ams_nrel, latex_ams_arrows,
74                 latex_ams_misc
75         };
76         int const nr_panels = sizeof(panels)/sizeof(panels[0]);
77
78 bool panel_initialised[nr_panels];
79 }
80  
81  
82 QMathDialog::QMathDialog(QMath * form)
83         : QMathDialogBase(0, 0, false, 0),
84         form_(form)
85 {
86         connect(symbolsCO, SIGNAL(activated(int)), symbolsWS, SLOT(raiseWidget(int))); 
87
88         for (int i = 0; *function_names[i]; ++i) {
89                 functionsLB->insertItem(function_names[i]);
90         }
91  
92         for (int i = 0; i < nr_panels; ++i) { 
93                 QScrollViewSingle * view = new QScrollViewSingle(symbolsWS);
94                 symbolsWS->addWidget(view, i);
95         }
96
97         // aboutToShow() only fires when != 0 in Qt 2 !
98         symbolsWS->raiseWidget(0);
99         addPanel(0);
100         panel_initialised[0] = true;
101  
102         connect(symbolsWS, SIGNAL(aboutToShow(int)), this, SLOT(showingPanel(int)));
103  
104         space_menu_ = new QPopupMenu(spacePB);
105         space_menu_->insertItem(_("Thin space   \\,"), 1); 
106         space_menu_->insertItem(_("Medium space \\:"), 2); 
107         space_menu_->insertItem(_("Thick space  \\;"), 3); 
108         space_menu_->insertItem(_("Quadratin space      \\quad"), 4); 
109         space_menu_->insertItem(_("Double quadratin space       \\qquad"), 5); 
110         space_menu_->insertItem(_("Negative space       \\!"), 6);
111         connect(space_menu_, SIGNAL(activated(int)), this, SLOT(insertSpace(int)));
112 }
113
114
115 void QMathDialog::showingPanel(int num)
116 {
117         if (panel_initialised[num])
118                 return;
119
120         addPanel(num);
121
122         // Qt needs to catch up. Dunno why. 
123         qApp->processEvents();
124  
125         panel_initialised[num] = true;
126 }
127
128  
129 IconPalette * QMathDialog::makePanel(QWidget * parent, char const ** entries)
130 {
131         IconPalette * p = new IconPalette(parent);
132         for (int i = 0; *entries[i]; ++i) {
133                 string xpm_name = LibFileSearch("images/math/", entries[i], "xpm");
134                 p->add(QPixmap(xpm_name.c_str()), entries[i], string("\\") + entries[i]);
135         }
136         connect(p, SIGNAL(button_clicked(string)), this, SLOT(symbol_clicked(string)));
137  
138         return p;
139 }
140
141  
142 void QMathDialog::addPanel(int num)
143 {
144         QScrollViewSingle * view = static_cast<QScrollViewSingle*>(symbolsWS->visibleWidget());
145         IconPalette * p = makePanel(view->viewport(), panels[num]);
146         view->setChild(p);
147 }
148
149  
150 void QMathDialog::insertSpace(int id)
151 {
152         string str;
153         switch (id) {
154                 case 1: str = ","; break;
155                 case 2: str = ":"; break;
156                 case 3: str = ";"; break;
157                 case 4: str = "quad"; break;
158                 case 5: str = "qquad"; break;
159                 case 6: str = "!"; break;
160         }
161         form_->insert_symbol(str);
162 }
163
164  
165 void QMathDialog::symbol_clicked(string str)
166 {
167         form_->insert_symbol(str);
168 }
169
170  
171 void QMathDialog::fracClicked()
172 {
173         form_->insert_symbol("frac");
174 }
175  
176
177 void QMathDialog::sqrtClicked()
178 {
179         form_->insert_symbol("sqrt");
180 }
181
182  
183 void QMathDialog::delimiterClicked()
184 {
185 }
186
187  
188 void QMathDialog::expandClicked()
189 {
190         int const id = symbolsWS->id(symbolsWS->visibleWidget());
191         IconPalette * p = makePanel(0, panels[id]);
192         string s = "LyX: "; 
193         s += symbolsCO->text(id).latin1();
194         p->setCaption(s.c_str());
195         p->resize(40 * 5, p->height());
196         p->show();
197         p->setMaximumSize(p->width(), p->height());
198 }
199  
200  
201 void QMathDialog::functionSelected(const QString & str)
202 {
203         form_->insert_symbol(str.latin1()); 
204 }
205
206  
207 void QMathDialog::matrixClicked()
208 {
209 }
210
211  
212 void QMathDialog::spaceClicked()
213 {
214         space_menu_->exec(QCursor::pos());
215 }
216
217  
218 void QMathDialog::styleClicked()
219 {
220 }
221
222  
223 void QMathDialog::subscriptClicked()
224 {
225         form_->subscript();
226 }
227
228  
229 void QMathDialog::superscriptClicked()
230 {
231         form_->superscript();
232 }