20 #include <tqfontdatabase.h>
21 #include <tqlistbox.h>
22 #include <tqpainter.h>
25 #include <kcharsets.h>
28 #include <kfontdialog.h>
30 #include "kfontcombo.h"
31 #include "kfontcombo.moc"
34 #include <fontconfig/fontconfig.h>
36 #include <X11/Xatom.h>
37 #include <X11/Intrinsic.h>
38 #include <X11/StringDefs.h>
39 #include <X11/Shell.h>
41 #include <X11/Xft/Xft.h>
44 struct KFontComboPrivate
61 bool displayFonts : 1;
65 TQString defaultFamily;
68 class KFontListItem :
public TQListBoxItem
71 KFontListItem(
const TQString &fontName,
KFontCombo *combo);
72 virtual ~KFontListItem();
74 virtual int width(
const TQListBox *)
const;
75 virtual int height(
const TQListBox *)
const;
80 virtual void paint(TQPainter *p);
92 KFontListItem::KFontListItem(
const TQString &fontName,
KFontCombo *combo)
93 : TQListBoxItem(combo->listBox()),
102 KFontListItem::~KFontListItem()
107 int KFontListItem::width(
const TQListBox *lb)
const
110 return TQFontMetrics(*m_font).width(text()) + 6;
111 return lb->fontMetrics().width(text()) + 6;
114 int KFontListItem::height(
const TQListBox *lb)
const
116 if (m_combo->d->displayFonts)
117 return m_combo->d->lineSpacing + 2;
118 TQFontMetrics fm(lb->fontMetrics());
119 return fm.lineSpacing() + 2;
122 void KFontListItem::paint(TQPainter *p)
124 if (m_combo->d->displayFonts)
129 TQString t = m_fontName;
130 if (p->device() != m_combo)
135 t = TQString::fromLatin1(
"(%1)").arg(m_fontName);
137 TQFontMetrics fm(p->fontMetrics());
138 p->drawText(3, (m_combo->d->lineSpacing + fm.ascent() + fm.leading() / 2) / 2, t);
142 TQFontMetrics fm(p->fontMetrics());
143 p->drawText(3, fm.ascent() + fm.leading() / 2, m_fontName);
147 void KFontListItem::updateFont()
152 m_font->setBold(m_combo->d->bold);
153 m_font->setItalic(m_combo->d->italic);
154 m_font->setUnderline(m_combo->d->underline);
155 m_font->setStrikeOut(m_combo->d->strikeOut);
156 m_font->setPointSize(m_combo->d->size);
159 void KFontListItem::createFont()
164 m_font =
new TQFont(m_fontName);
165 TQFontMetrics fm(*m_font);
166 for (
unsigned int i = 0; i < m_fontName.length(); ++i)
167 if (!fm.inFont(m_fontName[i]))
169 m_canPaintName =
false;
179 TQStringList families;
194 for (TQStringList::ConstIterator it = fonts.begin(); it != fonts.end(); ++it)
195 new KFontListItem(*it,
this);
203 TQString lowerName = family.lower();
205 for(
int i = 0; i < c; i++)
207 if (text(i).lower() == lowerName)
210 d->defaultFamily = text(i);
215 int x = lowerName.find(
" [");
218 lowerName = lowerName.left(x);
219 for(
int i = 0; i < c; i++)
221 if (text(i).lower() == lowerName)
224 d->defaultFamily = text(i);
232 for(
int i = 0; i < c; i++)
234 if (text(i).lower().startsWith(lowerName))
237 d->defaultFamily = text(i);
245 FcPattern *pattern = NULL;
246 FcConfig *config = NULL;
249 TQRegExp regExp(
"[-:]");
250 pattern = FcNameParse( (
unsigned char*) family.ascii() );
251 FcDefaultSubstitute(pattern);
252 FcConfigSubstitute (config, pattern, FcMatchPattern);
253 pattern = FcFontMatch(NULL, pattern, &result);
254 realFamily = (
char*)FcNameUnparse(pattern);
255 realFamily.remove(realFamily.find(regExp), realFamily.length());
257 if ( !realFamily.isEmpty() && realFamily != family )
269 return currentText();
270 return d->defaultFamily;
273 void KFontCombo::setCurrentItem(
int i)
276 TQComboBox::setCurrentItem(i);
279 void KFontCombo::init()
281 d =
new KFontComboPrivate;
283 setInsertionPolicy(NoInsertion);
286 connect(
this, TQT_SIGNAL(highlighted(
int)), TQT_SLOT(
slotModified(
int)));
309 if (d->italic == italic)
322 if (d->underline == underline)
335 if (d->strikeOut == strikeOut)
352 f.setPointSize(size);
354 d->lineSpacing = fm.lineSpacing();
365 if (!d->displayFonts)
368 for (
unsigned int i = 0; i < listBox()->count(); ++i)
370 KFontListItem *item =
static_cast<KFontListItem *
>(listBox()->item(i));
381 void KFontCombo::virtual_hook(
int id,
void* data )
382 { KComboBox::virtual_hook(
id, data ); }
void setFonts(const TQStringList &fonts)
Sets the font list.
void setUnderline(bool underline)
Sets the listed fonts to underlined or not underlined.
void setCurrentFont(const TQString &family)
Sets the currently selected font.
virtual ~KFontCombo()
Destructor.
void setSize(int size)
Sets the listed fonts' size.
bool strikeOut() const
Returns the current strike out status.
static void getFontList(TQStringList &list, uint fontListCriteria)
Creates a list of font strings.
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
A combobox that lists the available fonts.
virtual void setAutoCompletion(bool autocomplete)
Re-implemented from TQComboBox.
void setBold(bool bold)
Sets the listed fonts to bold or normal.
TQString currentFont() const
KFontCombo(TQWidget *parent, const char *name=0)
Constructor.
bool bold() const
Returns the current bold status.
void updateFonts()
Updated the combo's listBox() to reflect changes made to the fonts' attributed.
void setStrikeOut(bool strikeOut)
Sets the listed fonts to striked out or not.
bool underline() const
Returns the current underline status.
static KConfig * config()
void setItalic(bool italic)
Sets the listed fonts to italic or regular.
bool italic() const
Returns the current italic status.
int size() const
Returns the current font size.
static bool displayFonts()
Returns the user's setting of whether the items should be painted in the respective fonts or not...