1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/* This file is part of the KDE libraries
Copyright (c) 2001 Malte Starostik <malte@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <tqfontdatabase.h>
#include <tqlistbox.h>
#include <tqpainter.h>
#include <tqregexp.h>
#include <kcharsets.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <tdefontdialog.h>
#include "tdefontcombo.h"
#include "tdefontcombo.moc"
#include <ft2build.h>
#include <fontconfig/fontconfig.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xft/Xft.h>
struct TDEFontComboPrivate
{
TDEFontComboPrivate()
: bold(false),
italic(false),
underline(false),
strikeOut(false),
modified(false),
size(0),
lineSpacing(0)
{
}
bool bold : 1;
bool italic : 1;
bool underline : 1;
bool strikeOut : 1;
bool displayFonts : 1;
bool modified : 1;
int size;
int lineSpacing;
TQString defaultFamily;
};
class TDEFontListItem : public TQListBoxItem
{
public:
TDEFontListItem(const TQString &fontName, TDEFontCombo *combo);
virtual ~TDEFontListItem();
virtual int width(const TQListBox *) const;
virtual int height(const TQListBox *) const;
void updateFont();
protected:
virtual void paint(TQPainter *p);
private:
void createFont();
private:
TDEFontCombo *m_combo;
TQString m_fontName;
TQFont *m_font;
bool m_canPaintName;
};
TDEFontListItem::TDEFontListItem(const TQString &fontName, TDEFontCombo *combo)
: TQListBoxItem(combo->listBox()),
m_combo(combo),
m_fontName(fontName),
m_font(0),
m_canPaintName(true)
{
setText(fontName);
}
TDEFontListItem::~TDEFontListItem()
{
delete m_font;
}
int TDEFontListItem::width(const TQListBox *lb) const
{
if (m_font)
return TQFontMetrics(*m_font).width(text()) + 6;
return lb->fontMetrics().width(text()) + 6;
}
int TDEFontListItem::height(const TQListBox *lb) const
{
if (m_combo->d->displayFonts)
return m_combo->d->lineSpacing + 2;
TQFontMetrics fm(lb->fontMetrics());
return fm.lineSpacing() + 2;
}
void TDEFontListItem::paint(TQPainter *p)
{
if (m_combo->d->displayFonts)
{
if (!m_font)
createFont();
TQString t = m_fontName;
if (p->device() != m_combo)
{
if (m_canPaintName)
p->setFont(*m_font);
else
t = TQString::fromLatin1("(%1)").arg(m_fontName);
}
TQFontMetrics fm(p->fontMetrics());
p->drawText(3, (m_combo->d->lineSpacing + fm.ascent() + fm.leading() / 2) / 2, t);
}
else
{
TQFontMetrics fm(p->fontMetrics());
p->drawText(3, fm.ascent() + fm.leading() / 2, m_fontName);
}
}
void TDEFontListItem::updateFont()
{
if (!m_font)
return;
m_font->setBold(m_combo->d->bold);
m_font->setItalic(m_combo->d->italic);
m_font->setUnderline(m_combo->d->underline);
m_font->setStrikeOut(m_combo->d->strikeOut);
m_font->setPointSize(m_combo->d->size);
}
void TDEFontListItem::createFont()
{
if (m_font)
return;
m_font = new TQFont(m_fontName);
TQFontMetrics fm(*m_font);
for (unsigned int i = 0; i < m_fontName.length(); ++i)
if (!fm.inFont(m_fontName[i]))
{
m_canPaintName = false;
break;
}
updateFont();
}
TDEFontCombo::TDEFontCombo(TQWidget *parent, const char *name)
: KComboBox(true, parent, name)
{
init();
TQStringList families;
TDEFontChooser::getFontList(families, 0);
setFonts(families);
}
TDEFontCombo::TDEFontCombo(const TQStringList &fonts, TQWidget *parent, const char *name)
: KComboBox(true, parent, name)
{
init();
setFonts(fonts);
}
void TDEFontCombo::setFonts(const TQStringList &fonts)
{
clear();
for (TQStringList::ConstIterator it = fonts.begin(); it != fonts.end(); ++it)
new TDEFontListItem(*it, this);
}
/*
* Maintenance note: Keep in sync with TDEFontAction::setFont()
*/
void TDEFontCombo::setCurrentFont(const TQString &family)
{
TQString lowerName = family.lower();
int c = count();
for(int i = 0; i < c; i++)
{
if (text(i).lower() == lowerName)
{
setCurrentItem(i);
d->defaultFamily = text(i);
d->modified = false;
return;
}
}
int x = lowerName.find(" [");
if (x>-1)
{
lowerName = lowerName.left(x);
for(int i = 0; i < c; i++)
{
if (text(i).lower() == lowerName)
{
setCurrentItem(i);
d->defaultFamily = text(i);
d->modified = false;
return;
}
}
}
lowerName += " [";
for(int i = 0; i < c; i++)
{
if (text(i).lower().startsWith(lowerName))
{
setCurrentItem(i);
d->defaultFamily = text(i);
d->modified = false;
return;
}
}
// nothing matched yet, try a fontconfig reverse lookup and
// check again to solve an alias
FcPattern *pattern = NULL;
FcConfig *config = NULL;
FcResult result;
TQString realFamily;
TQRegExp regExp("[-:]");
pattern = FcNameParse( (unsigned char*) family.ascii() );
FcDefaultSubstitute(pattern);
FcConfigSubstitute (config, pattern, FcMatchPattern);
pattern = FcFontMatch(NULL, pattern, &result);
realFamily = (char*)FcNameUnparse(pattern);
realFamily.remove(realFamily.find(regExp), realFamily.length());
if ( !realFamily.isEmpty() && realFamily != family )
setCurrentFont( realFamily );
}
void TDEFontCombo::slotModified( int )
{
d->modified = 1;
}
TQString TDEFontCombo::currentFont() const
{
if (d->modified)
return currentText();
return d->defaultFamily;
}
void TDEFontCombo::setCurrentItem(int i)
{
d->modified = true;
TQComboBox::setCurrentItem(i);
}
void TDEFontCombo::init()
{
d = new TDEFontComboPrivate;
d->displayFonts = displayFonts();
setInsertionPolicy(NoInsertion);
setAutoCompletion(true);
setSize(12);
connect( this, TQ_SIGNAL(highlighted(int)), TQ_SLOT(slotModified(int)));
}
TDEFontCombo::~TDEFontCombo()
{
delete d;
}
void TDEFontCombo::setBold(bool bold)
{
if (d->bold == bold)
return;
d->bold = bold;
updateFonts();
}
bool TDEFontCombo::bold() const
{
return d->bold;
}
void TDEFontCombo::setItalic(bool italic)
{
if (d->italic == italic)
return;
d->italic = italic;
updateFonts();
}
bool TDEFontCombo::italic() const
{
return d->italic;
}
void TDEFontCombo::setUnderline(bool underline)
{
if (d->underline == underline)
return;
d->underline = underline;
updateFonts();
}
bool TDEFontCombo::underline() const
{
return d->underline;
}
void TDEFontCombo::setStrikeOut(bool strikeOut)
{
if (d->strikeOut == strikeOut)
return;
d->strikeOut = strikeOut;
updateFonts();
}
bool TDEFontCombo::strikeOut() const
{
return d->strikeOut;
}
void TDEFontCombo::setSize(int size)
{
if (d->size == size)
return;
d->size = size;
TQFont f;
f.setPointSize(size);
TQFontMetrics fm(f);
d->lineSpacing = fm.lineSpacing();
updateFonts();
}
int TDEFontCombo::size() const
{
return d->size;
}
void TDEFontCombo::updateFonts()
{
if (!d->displayFonts)
return;
for (unsigned int i = 0; i < listBox()->count(); ++i)
{
TDEFontListItem *item = static_cast<TDEFontListItem *>(listBox()->item(i));
item->updateFont();
}
}
bool TDEFontCombo::displayFonts()
{
TDEConfigGroupSaver saver(TDEGlobal::config(), "KDE");
return TDEGlobal::config()->readBoolEntry("DisplayFontItems", true);
}
void TDEFontCombo::virtual_hook( int id, void* data )
{ KComboBox::virtual_hook( id, data ); }
|