20 #include <tqfontdatabase.h>
21 #include <tqlistbox.h>
22 #include <tqpainter.h>
25 #include <kcharsets.h>
26 #include <tdeconfig.h>
27 #include <tdeglobal.h>
28 #include <tdefontdialog.h>
30 #include "tdefontcombo.h"
31 #include "tdefontcombo.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 TDEFontComboPrivate
61 bool displayFonts : 1;
65 TQString defaultFamily;
68 class TDEFontListItem :
public TQListBoxItem
71 TDEFontListItem(
const TQString &fontName,
TDEFontCombo *combo);
72 virtual ~TDEFontListItem();
74 virtual int width(
const TQListBox *)
const;
75 virtual int height(
const TQListBox *)
const;
80 virtual void paint(TQPainter *p);
92 TDEFontListItem::TDEFontListItem(
const TQString &fontName,
TDEFontCombo *combo)
93 : TQListBoxItem(combo->listBox()),
102 TDEFontListItem::~TDEFontListItem()
107 int TDEFontListItem::width(
const TQListBox *lb)
const
110 return TQFontMetrics(*m_font).width(text()) + 6;
111 return lb->fontMetrics().width(text()) + 6;
114 int TDEFontListItem::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 TDEFontListItem::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 TDEFontListItem::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 TDEFontListItem::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 TDEFontListItem(*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 TDEFontCombo::setCurrentItem(
int i)
276 TQComboBox::setCurrentItem(i);
279 void TDEFontCombo::init()
281 d =
new TDEFontComboPrivate;
283 setInsertionPolicy(NoInsertion);
286 connect(
this, TQ_SIGNAL(highlighted(
int)), TQ_SLOT(
slotModified(
int)));
352 f.setPointSize(
size);
354 d->lineSpacing = fm.lineSpacing();
365 if (!d->displayFonts)
368 for (
unsigned int i = 0; i < listBox()->count(); ++i)
370 TDEFontListItem *item =
static_cast<TDEFontListItem *
>(listBox()->item(i));
381 void TDEFontCombo::virtual_hook(
int id,
void* data )
382 { KComboBox::virtual_hook(
id, data ); }
virtual void setAutoCompletion(bool autocomplete)
Re-implemented from TQComboBox.
bool readBoolEntry(const TQString &pKey, bool bDefault=false) const
static void getFontList(TQStringList &list, uint fontListCriteria)
Creates a list of font strings.
A combobox that lists the available fonts.
void setStrikeOut(bool strikeOut)
Sets the listed fonts to striked out or not.
void setFonts(const TQStringList &fonts)
Sets the font list.
void updateFonts()
Updated the combo's listBox() to reflect changes made to the fonts' attributed.
bool bold() const
Returns the current bold status.
static bool displayFonts()
Returns the user's setting of whether the items should be painted in the respective fonts or not.
TDEFontCombo(TQWidget *parent, const char *name=0)
Constructor.
bool underline() const
Returns the current underline status.
virtual ~TDEFontCombo()
Destructor.
int size() const
Returns the current font size.
void setBold(bool bold)
Sets the listed fonts to bold or normal.
void setCurrentFont(const TQString &family)
Sets the currently selected font.
void setSize(int size)
Sets the listed fonts' size.
TQString currentFont() const
void setUnderline(bool underline)
Sets the listed fonts to underlined or not underlined.
bool italic() const
Returns the current italic status.
void setItalic(bool italic)
Sets the listed fonts to italic or regular.
bool strikeOut() const
Returns the current strike out status.
static TDEConfig * config()