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