• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

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

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.1
This website is maintained by Timothy Pearson.