]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSymbols.cpp
ab745e8473e52d7965f6584b18927cd8563845bb
[lyx.git] / src / frontends / qt4 / GuiSymbols.cpp
1 /**
2  * \file GuiSymbols.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiSymbols.h"
14
15 #include "GuiApplication.h"
16 #include "GuiView.h"
17 #include "qt_helpers.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Encoding.h"
23
24 #include "support/gettext.h"
25
26 #include <QChar>
27 #include <QPixmap>
28 #include <QListWidgetItem>
29 #include <QString>
30
31 using namespace std;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 namespace {
38
39 /// name of unicode block, start and end code point
40 struct UnicodeBlocks {
41         char const * name;
42         char_type start;
43         char_type end;
44 };
45
46
47 /// all unicode blocks with start and end code point
48 UnicodeBlocks unicode_blocks[] = {
49         { N_("Basic Latin"), 0x0000, 0x007f },
50         { N_("Latin-1 Supplement"), 0x0080, 0x00ff },
51         { N_("Latin Extended-A"), 0x0100, 0x017f },
52         { N_("Latin Extended-B"), 0x0180, 0x024f },
53         { N_("IPA Extensions"), 0x0250, 0x02af },
54         { N_("Spacing Modifier Letters"), 0x02b0, 0x02ff },
55         { N_("Combining Diacritical Marks"), 0x0300, 0x036f },
56         { N_("Greek"), 0x0370, 0x03ff },
57         { N_("Cyrillic"), 0x0400, 0x04ff },
58         { N_("Armenian"), 0x0530, 0x058f },
59         { N_("Hebrew"), 0x0590, 0x05ff },
60         { N_("Arabic"), 0x0600, 0x06ff },
61         { N_("Devanagari"), 0x0900, 0x097f },
62         { N_("Bengali"), 0x0980, 0x09ff },
63         { N_("Gurmukhi"), 0x0a00, 0x0a7f },
64         { N_("Gujarati"), 0x0a80, 0x0aff },
65         { N_("Oriya"), 0x0b00, 0x0b7f },
66         { N_("Tamil"), 0x0b80, 0x0bff },
67         { N_("Telugu"), 0x0c00, 0x0c7f },
68         { N_("Kannada"), 0x0c80, 0x0cff },
69         { N_("Malayalam"), 0x0d00, 0x0d7f },
70         { N_("Thai"), 0x0e00, 0x0e7f },
71         { N_("Lao"), 0x0e80, 0x0eff },
72         { N_("Tibetan"), 0x0f00, 0x0fbf },
73         { N_("Georgian"), 0x10a0, 0x10ff },
74         { N_("Hangul Jamo"), 0x1100, 0x11ff },
75         { N_("Phonetic Extensions"), 0x1d00, 0x1d7f },
76         { N_("Latin Extended Additional"), 0x1e00, 0x1eff },
77         { N_("Greek Extended"), 0x1f00, 0x1fff },
78         { N_("General Punctuation"), 0x2000, 0x206f },
79         { N_("Superscripts and Subscripts"), 0x2070, 0x209f },
80         { N_("Currency Symbols"), 0x20a0, 0x20cf },
81         { N_("Combining Diacritical Marks for Symbols"), 0x20d0, 0x20ff },
82         { N_("Letterlike Symbols"), 0x2100, 0x214f },
83         { N_("Number Forms"), 0x2150, 0x218f },
84         { N_("Arrows"), 0x2190, 0x21ff },
85         { N_("Mathematical Operators"), 0x2200, 0x22ff },
86         { N_("Miscellaneous Technical"), 0x2300, 0x23ff },
87         { N_("Control Pictures"), 0x2400, 0x243f },
88         { N_("Optical Character Recognition"), 0x2440, 0x245f },
89         { N_("Enclosed Alphanumerics"), 0x2460, 0x24ff },
90         { N_("Box Drawing"), 0x2500, 0x257f },
91         { N_("Block Elements"), 0x2580, 0x259f },
92         { N_("Geometric Shapes"), 0x25a0, 0x25ff },
93         { N_("Miscellaneous Symbols"), 0x2600, 0x26ff },
94         { N_("Dingbats"), 0x2700, 0x27bf },
95         { N_("Miscellaneous Mathematical Symbols-A"), 0x27c0, 0x27ef },
96         { N_("CJK Symbols and Punctuation"), 0x3000, 0x303f },
97         { N_("Hiragana"), 0x3040, 0x309f },
98         { N_("Katakana"), 0x30a0, 0x30ff },
99         { N_("Bopomofo"), 0x3100, 0x312f },
100         { N_("Hangul Compatibility Jamo"), 0x3130, 0x318f },
101         { N_("Kanbun"), 0x3190, 0x319f },
102         { N_("Enclosed CJK Letters and Months"), 0x3200, 0x32ff },
103         { N_("CJK Compatibility"), 0x3300, 0x33ff },
104         { N_("CJK Unified Ideographs"), 0x4e00, 0x9fa5 },
105         { N_("Hangul Syllables"), 0xac00, 0xd7a3 },
106         { N_("High Surrogates"), 0xd800, 0xdb7f },
107         { N_("Private Use High Surrogates"), 0xdb80, 0xdbff },
108         { N_("Low Surrogates"), 0xdc00, 0xdfff },
109         { N_("Private Use Area"), 0xe000, 0xf8ff },
110         { N_("CJK Compatibility Ideographs"), 0xf900, 0xfaff },
111         { N_("Alphabetic Presentation Forms"), 0xfb00, 0xfb4f },
112         { N_("Arabic Presentation Forms-A"), 0xfb50, 0xfdff },
113         { N_("Combining Half Marks"), 0xfe20, 0xfe2f },
114         { N_("CJK Compatibility Forms"), 0xfe30, 0xfe4f },
115         { N_("Small Form Variants"), 0xfe50, 0xfe6f },
116         { N_("Arabic Presentation Forms-B"), 0xfe70, 0xfeff },
117         { N_("Halfwidth and Fullwidth Forms"), 0xff00, 0xffef },
118         { N_("Specials"), 0xfff0, 0xffff },
119         { N_("Linear B Syllabary"), 0x10000, 0x1007f },
120         { N_("Linear B Ideograms"), 0x10080, 0x100ff },
121         { N_("Aegean Numbers"), 0x10100, 0x1013f },
122         { N_("Ancient Greek Numbers"), 0x10140, 0x1018f },
123         { N_("Old Italic"), 0x10300, 0x1032f },
124         { N_("Gothic"), 0x10330, 0x1034f },
125         { N_("Ugaritic"), 0x10380, 0x1039f },
126         { N_("Old Persian"), 0x103a0, 0x103df },
127         { N_("Deseret"), 0x10400, 0x1044f },
128         { N_("Shavian"), 0x10450, 0x1047f },
129         { N_("Osmanya"), 0x10480, 0x104af },
130         { N_("Cypriot Syllabary"), 0x10800, 0x1083f },
131         { N_("Kharoshthi"), 0x10a00, 0x10a5f },
132         { N_("Byzantine Musical Symbols"), 0x1d000, 0x1d0ff },
133         { N_("Musical Symbols"), 0x1d100, 0x1d1ff },
134         { N_("Ancient Greek Musical Notation"), 0x1d200, 0x1d24f },
135         { N_("Tai Xuan Jing Symbols"), 0x1d300, 0x1d35f },
136         { N_("Mathematical Alphanumeric Symbols"), 0x1d400, 0x1d7ff },
137         { N_("CJK Unified Ideographs Extension B"), 0x20000, 0x2a6d6 },
138         { N_("CJK Compatibility Ideographs Supplement"), 0x2f800, 0x2fa1f },
139         { N_("Tags"), 0xe0000, 0xe007f },
140         { N_("Variation Selectors Supplement"), 0xe0100, 0xe01ef },
141         { N_("Supplementary Private Use Area-A"), 0xf0000, 0xe01ef },
142         { N_("Supplementary Private Use Area-B"), 0x100000, 0x10ffff }
143 };
144
145 const int no_blocks = sizeof(unicode_blocks) / sizeof(UnicodeBlocks);
146
147
148 QString getCodePoint(char_type c)
149 {
150         QString cp = QString::number(c, 16);
151         while (cp.size() < 4)
152                 cp.prepend('0');
153         cp.prepend("0x");
154         return cp;
155 }
156
157 } // namespace anon
158
159
160 GuiSymbols::GuiSymbols(GuiView & lv)
161         : DialogView(lv, "symbols", qt_("Symbols")), encoding_("ascii")
162 {
163         setupUi(this);
164
165         setFocusProxy(symbolsLW);
166
167         symbolsLW->setViewMode(QListView::IconMode);
168         symbolsLW->setUniformItemSizes(true);
169         // increase the display size of the symbols a bit
170         QFont font= symbolsLW->font();
171         int size = font.pointSize() + 3;
172         font.setPointSize(size);
173         symbolsLW->setFont(font);
174 }
175
176
177 void GuiSymbols::updateView()
178 {
179         chosenLE->clear();
180
181         string new_encoding = bufferview()->cursor().getEncoding()->name();
182         if (buffer().params().inputenc != "auto" &&
183             buffer().params().inputenc != "default")
184                 new_encoding = buffer().params().encoding().name();
185         if (new_encoding == encoding_)
186                 // everything up to date
187                 return;
188         if (!new_encoding.empty())
189                 encoding_ = new_encoding;
190         bool const utf8 = toqstr(encoding_).startsWith("utf8");
191         if (utf8)
192                 categoryFilterCB->setChecked(false);
193         categoryFilterCB->setEnabled(!utf8);
194         updateSymbolList();
195 }
196
197
198 void GuiSymbols::enableView(bool enable)
199 {
200         chosenLE->setEnabled(enable);
201         okPB->setEnabled(enable);
202         applyPB->setEnabled(enable);
203 }
204
205
206 void GuiSymbols::on_applyPB_clicked()
207 {
208         dispatchParams();
209 }
210
211
212 void GuiSymbols::on_okPB_clicked()
213 {
214         dispatchParams();
215         hide();
216 }
217
218
219 void GuiSymbols::on_closePB_clicked()
220 {
221         hide();
222 }
223
224
225 void GuiSymbols::on_symbolsLW_itemActivated(QListWidgetItem *)
226 {
227         on_okPB_clicked();
228 }
229
230
231 void GuiSymbols::on_chosenLE_textChanged(QString const & text)
232 {
233         bool const empty_sel = text.isEmpty();
234         okPB->setEnabled(!empty_sel);
235         applyPB->setEnabled(!empty_sel);
236 }
237
238
239 void GuiSymbols::on_chosenLE_returnPressed()
240 {
241         on_okPB_clicked();
242 }
243
244
245 void GuiSymbols::on_symbolsLW_itemClicked(QListWidgetItem * item)
246 {
247         QString const text = item->text();
248         if (text.isEmpty())
249                 return;
250         if (chosenLE->isEnabled())
251                 chosenLE->insert(text);
252         if (categoryFilterCB->isChecked()) {
253                 QString const category = getBlock(text.data()->unicode());
254                 categoryCO->setCurrentIndex(categoryCO->findText(category));
255         }
256 }
257
258
259 void GuiSymbols::on_categoryCO_activated(QString const & text)
260 {
261         if (!categoryFilterCB->isChecked())
262                 updateSymbolList(false);
263         else if (used_blocks.find(text) != used_blocks.end())
264                 symbolsLW->scrollToItem(used_blocks[text],
265                         QAbstractItemView::PositionAtTop);
266 }
267
268
269 void GuiSymbols::on_categoryFilterCB_toggled(bool on)
270 {
271         updateSymbolList(on);
272         if (on) {
273                 QString const category = categoryCO->currentText();
274                 if (used_blocks.find(category) != used_blocks.end())
275                         symbolsLW->scrollToItem(used_blocks[category],
276                                 QAbstractItemView::PositionAtTop);
277         }
278 }
279
280
281 void GuiSymbols::updateSymbolList(bool update_combo)
282 {
283         QString category = categoryCO->currentText();
284         bool const nocategory = category.isEmpty();
285         char_type range_start = 0x0000;
286         char_type range_end = 0x110000;
287         symbolsLW->clear();
288         if (update_combo) {
289                 used_blocks.clear();
290                 categoryCO->clear();
291         }
292         bool const show_all = categoryFilterCB->isChecked();
293
294         if (symbols_.empty() || update_combo) {
295                 Encoding enc = *(encodings.getFromLyXName(encoding_));
296                 symbols_ = enc.getSymbolsList();
297         }
298
299         if (!show_all) {
300                 for (int i = 0 ; i < no_blocks; ++i)
301                         if (unicode_blocks[i].name == fromqstr(category)) {
302                                 range_start = unicode_blocks[i].start;
303                                 range_end = unicode_blocks[i].end;
304                                 break;
305                         }
306         }
307
308         SymbolsList::const_iterator const end = symbols_.end();
309         for (SymbolsList::const_iterator it = symbols_.begin(); it != end; ++it) {
310                 char_type c = *it;
311                 if (!update_combo && !show_all && (c <= range_start || c >= range_end))
312                         continue;
313 #if QT_VERSION >= 0x040300
314                 QChar::Category const cat = QChar::category(uint(c));
315 #else
316                 QChar const qc = uint(c);
317                 QChar::Category const cat = qc.category();
318 #endif
319                 // we do not want control or space characters
320                 if (cat == QChar::Other_Control || cat == QChar::Separator_Space)
321                         continue;
322                 QListWidgetItem * lwi = new QListWidgetItem(toqstr(c));
323                 if (show_all || c >= range_start && c <= range_end) {
324                         lwi->setTextAlignment(Qt::AlignCenter);
325                         lwi->setToolTip(
326                                 qt_("Character: ") + toqstr(c) + "\n" +
327                                 qt_("Code Point: ") + getCodePoint(c)
328                                 );
329                         symbolsLW->addItem(lwi);
330                 }
331                 if (update_combo) {
332                         QString block = getBlock(c);
333                         if (category.isEmpty())
334                                 category = block;
335                         if (used_blocks.find(block) == used_blocks.end())
336                                 used_blocks[block] = lwi;
337                 }
338         }
339
340         if (update_combo) {
341                 // update category combo
342                 for (UsedBlocks::iterator it = used_blocks.begin();
343                      it != used_blocks.end(); ++it) {
344                         categoryCO->addItem(it->first);
345                 }
346         }
347
348         int old = categoryCO->findText(category);
349         if (old != -1)
350                 categoryCO->setCurrentIndex(old);
351         // update again in case the combo has not yet been filled
352         // on first cycle (at dialog initialization)
353         if (nocategory && !category.isEmpty())
354                 updateSymbolList();
355 }
356
357
358 QString const GuiSymbols::getBlock(char_type c) const
359 {
360         int i = 0;
361         while (c > unicode_blocks[i].end && i < no_blocks)
362                 ++i;
363         if (unicode_blocks[i].name)
364                 return toqstr(unicode_blocks[i].name);
365         return QString();
366 }
367
368
369 void GuiSymbols::dispatchParams()
370 {
371         dispatch(FuncRequest(getLfun(), fromqstr(chosenLE->text())));
372 }
373
374
375 Dialog * createGuiSymbols(GuiView & lv)
376 {
377         return new GuiSymbols(lv);
378 }
379
380
381 } // namespace frontend
382 } // namespace lyx
383
384 #include "GuiSymbols_moc.cpp"