]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSymbols.cpp
98229454ffc961e85e1efcc4c87701ea0e2b15d6
[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 "Buffer.h"
16 #include "BufferView.h"
17
18 #include "GuiApplication.h"
19 #include "GuiView.h"
20
21 #include "qt_helpers.h"
22
23 #include "Encoding.h"
24 #include "support/gettext.h"
25
26 #include <QPixmap>
27 #include <QListWidgetItem>
28
29 // Set to zero if unicode symbols are preferred.
30 #define USE_PIXMAP 1
31
32 using namespace std;
33
34 namespace lyx {
35 namespace frontend {
36
37
38 namespace {
39
40 /// name of unicode block, start and end code point
41 struct UnicodeBlocks {
42         char const * name;
43         char_type start;
44         char_type end;
45 };
46
47
48 /// all unicode blocks with start and end code point
49 UnicodeBlocks unicode_blocks[] = {
50         { N_("Basic Latin"), 0x0000, 0x007f },
51         { N_("Latin-1 Supplement"), 0x0080, 0x00ff },
52         { N_("Latin Extended-A"), 0x0100, 0x017f },
53         { N_("Latin Extended-B"), 0x0180, 0x024f },
54         { N_("IPA Extensions"), 0x0250, 0x02af },
55         { N_("Spacing Modifier Letters"), 0x02b0, 0x02ff },
56         { N_("Combining Diacritical Marks"), 0x0300, 0x036f },
57         { N_("Greek"), 0x0370, 0x03ff },
58         { N_("Cyrillic"), 0x0400, 0x04ff },
59         { N_("Armenian"), 0x0530, 0x058f },
60         { N_("Hebrew"), 0x0590, 0x05ff },
61         { N_("Arabic"), 0x0600, 0x06ff },
62         { N_("Devanagari"), 0x0900, 0x097f },
63         { N_("Bengali"), 0x0980, 0x09ff },
64         { N_("Gurmukhi"), 0x0a00, 0x0a7f },
65         { N_("Gujarati"), 0x0a80, 0x0aff },
66         { N_("Oriya"), 0x0b00, 0x0b7f },
67         { N_("Tamil"), 0x0b80, 0x0bff },
68         { N_("Telugu"), 0x0c00, 0x0c7f },
69         { N_("Kannada"), 0x0c80, 0x0cff },
70         { N_("Malayalam"), 0x0d00, 0x0d7f },
71         { N_("Thai"), 0x0e00, 0x0e7f },
72         { N_("Lao"), 0x0e80, 0x0eff },
73         { N_("Tibetan"), 0x0f00, 0x0fbf },
74         { N_("Georgian"), 0x10a0, 0x10ff },
75         { N_("Hangul Jamo"), 0x1100, 0x11ff },
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_("CJK Symbols and Punctuation"), 0x3000, 0x303f },
96         { N_("Hiragana"), 0x3040, 0x309f },
97         { N_("Katakana"), 0x30a0, 0x30ff },
98         { N_("Bopomofo"), 0x3100, 0x312f },
99         { N_("Hangul Compatibility Jamo"), 0x3130, 0x318f },
100         { N_("Kanbun"), 0x3190, 0x319f },
101         { N_("Enclosed CJK Letters and Months"), 0x3200, 0x32ff },
102         { N_("CJK Compatibility"), 0x3300, 0x33ff },
103         { N_("CJK Unified Ideographs"), 0x4e00, 0x9fa5 },
104         { N_("Hangul Syllables"), 0xac00, 0xd7a3 },
105         { N_("High Surrogates"), 0xd800, 0xdb7f },
106         { N_("Private Use High Surrogates"), 0xdb80, 0xdbff },
107         { N_("Low Surrogates"), 0xdc00, 0xdfff },
108         { N_("Private Use Area"), 0xe000, 0xf8ff },
109         { N_("CJK Compatibility Ideographs"), 0xf900, 0xfaff },
110         { N_("Alphabetic Presentation Forms"), 0xfb00, 0xfb4f },
111         { N_("Arabic Presentation Forms-A"), 0xfb50, 0xfdff },
112         { N_("Combining Half Marks"), 0xfe20, 0xfe2f },
113         { N_("CJK Compatibility Forms"), 0xfe30, 0xfe4f },
114         { N_("Small Form Variants"), 0xfe50, 0xfe6f },
115         { N_("Arabic Presentation Forms-B"), 0xfe70, 0xfeff },
116         { N_("Halfwidth and Fullwidth Forms"), 0xff00, 0xffef },
117         { N_("Specials"), 0xfff0, 0xffff },
118         { N_("Linear B Syllabary"), 0x10000, 0x1007f },
119         { N_("Linear B Ideograms"), 0x10080, 0x100ff },
120         { N_("Aegean Numbers"), 0x10100, 0x1013f },
121         { N_("Ancient Greek Numbers"), 0x10140, 0x1018f },
122         { N_("Old Italic"), 0x10300, 0x1032f },
123         { N_("Gothic"), 0x10330, 0x1034f },
124         { N_("Ugaritic"), 0x10380, 0x1039f },
125         { N_("Old Persian"), 0x103a0, 0x103df },
126         { N_("Deseret"), 0x10400, 0x1044f },
127         { N_("Shavian"), 0x10450, 0x1047f },
128         { N_("Osmanya"), 0x10480, 0x104af },
129         { N_("Cypriot Syllabary"), 0x10800, 0x1083f },
130         { N_("Kharoshthi"), 0x10a00, 0x10a5f },
131         { N_("Byzantine Musical Symbols"), 0x1d000, 0x1d0ff },
132         { N_("Musical Symbols"), 0x1d100, 0x1d1ff },
133         { N_("Ancient Greek Musical Notation"), 0x1d200, 0x1d24f },
134         { N_("Tai Xuan Jing Symbols"), 0x1d300, 0x1d35f },
135         { N_("Mathematical Alphanumeric Symbols"), 0x1d400, 0x1d7ff },
136         { N_("CJK Unified Ideographs Extension B"), 0x20000, 0x2a6d6 },
137         { N_("CJK Compatibility Ideographs Supplement"), 0x2f800, 0x2fa1f },
138         { N_("Tags"), 0xe0000, 0xe007f },
139         { N_("Variation Selectors Supplement"), 0xe0100, 0xe01ef },
140         { N_("Supplementary Private Use Area-A"), 0xf0000, 0xe01ef },
141         { N_("Supplementary Private Use Area-B"), 0x100000, 0x10ffff }
142 };
143
144 const int no_blocks = sizeof(unicode_blocks) / sizeof(UnicodeBlocks);
145
146 } // namespace anon
147
148
149 GuiSymbols::GuiSymbols(GuiView & lv)
150         : DialogView(lv, "symbols", qt_("Symbols")), encoding_("ascii")
151 {
152         setupUi(this);
153
154         setFocusProxy(symbolsLW);
155
156         symbolsLW->setViewMode(QListView::IconMode);
157         symbolsLW->setUniformItemSizes(true);
158         // increase the display size of the symbols a bit
159         QFont font= symbolsLW->font();
160         int size = font.pointSize() + 3;
161         font.setPointSize(size);
162         symbolsLW->setFont(font);
163
164         okPB->setEnabled(!chosenLE->text().isEmpty() &&
165                 !bufferview()->buffer().isReadonly());
166         applyPB->setEnabled(!chosenLE->text().isEmpty() &&
167                 !bufferview()->buffer().isReadonly());
168 }
169
170
171 void GuiSymbols::updateView()
172 {
173         chosenLE->clear();
174         initialiseParams(bufferview()->cursor().getEncoding()->name());
175 }
176
177
178 void GuiSymbols::on_applyPB_clicked()
179 {
180         dispatchParams();
181 }
182
183
184 void GuiSymbols::on_okPB_clicked()
185 {
186         dispatchParams();
187         hide();
188 }
189
190
191 void GuiSymbols::on_closePB_clicked()
192 {
193         hide();
194 }
195
196
197 void GuiSymbols::on_symbolsLW_itemActivated(QListWidgetItem *)
198 {
199         on_okPB_clicked();
200 }
201
202
203 void GuiSymbols::on_chosenLE_textChanged(QString const & text)
204 {
205         okPB->setEnabled(!text.isEmpty() && !bufferview()->buffer().isReadonly());
206         applyPB->setEnabled(!text.isEmpty() && !bufferview()->buffer().isReadonly());
207 }
208
209
210 void GuiSymbols::on_chosenLE_returnPressed()
211 {
212         on_okPB_clicked();
213 }
214
215
216 void GuiSymbols::on_symbolsLW_itemClicked(QListWidgetItem * item)
217 {
218         QString const text = item->text();
219         if (text.isEmpty())
220                 return;
221         chosenLE->insert(text);
222         QString const category = getBlock(text.data()->unicode());
223         categoryCO->setCurrentIndex(categoryCO->findText(category));
224 }
225
226
227 void GuiSymbols::on_categoryCO_activated(QString const & text)
228 {
229         if (used_blocks.find(text) != used_blocks.end())
230                 symbolsLW->scrollToItem(used_blocks[text]);
231 }
232
233
234 void GuiSymbols::updateSymbolList()
235 {
236         symbolsLW->clear();
237         used_blocks.clear();
238         categoryCO->clear();
239
240         typedef set<char_type> SymbolsList;
241         Encoding enc = *(encodings.getFromLyXName(encoding_));
242         SymbolsList symbols = enc.getSymbolsList();
243
244         SymbolsList::const_iterator const end = symbols.end();
245         for (SymbolsList::const_iterator it = symbols.begin(); it != end; ++it) {
246                 char_type c = *it;
247                 // we do not want control or space characters
248                 if (QChar(c).category() == QChar::Other_Control ||
249                     QChar(c).category() == QChar::Separator_Space)
250                         continue;
251                 QListWidgetItem * lwi = new QListWidgetItem(QChar(c));
252                 lwi->setTextAlignment(Qt::AlignCenter);
253                 symbolsLW->addItem(lwi);
254                 QString block = getBlock(c);
255                 if (used_blocks.find(block) == used_blocks.end())
256                         used_blocks[block] = lwi;
257         }
258
259         // update category combo
260         for (UsedBlocks::iterator it = used_blocks.begin(); it != used_blocks.end(); ++it) {
261                 categoryCO->addItem(it->first);
262         }
263 }
264
265
266 QString const GuiSymbols::getBlock(char_type c) const
267 {
268         int i = 0;
269         while (c > unicode_blocks[i].end && i < no_blocks)
270                 ++i;
271         if (unicode_blocks[i].name)
272                 return toqstr(unicode_blocks[i].name);
273         return QString();
274 }
275
276
277
278 bool GuiSymbols::initialiseParams(string const & data)
279 {
280         if (data == encoding_)
281                 // everything up to date
282                 return true;
283         if (!data.empty())
284                 encoding_ = data;
285         updateSymbolList();
286         return true;
287 }
288
289
290 void GuiSymbols::dispatchParams()
291 {
292         dispatch(FuncRequest(LFUN_SELF_INSERT, fromqstr(chosenLE->text())));
293 }
294
295
296 Dialog * createGuiSymbols(GuiView & lv)
297 {
298         return new GuiSymbols(lv);
299 }
300
301
302 } // namespace frontend
303 } // namespace lyx
304
305 #include "GuiSymbols_moc.cpp"