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

tdeui

  • tdeui
klanguagebutton.cpp
1 /*
2  * klanguagebutton.cpp - Adds some methods for inserting languages.
3  *
4  * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
5  *
6  * Requires the Qt widget libraries, available at no cost at
7  * http://www.trolltech.com/
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #define INCLUDE_MENUITEM_DEF
25 #include <tqpopupmenu.h>
26 #include <tqlayout.h>
27 #include <tqpushbutton.h>
28 
29 #include "klanguagebutton.h"
30 #include "klanguagebutton.moc"
31 
32 #include <kdebug.h>
33 
34 static void checkInsertPos( TQPopupMenu *popup, const TQString & str,
35  int &index )
36 {
37  if ( index == -1 )
38  return;
39 
40  int a = 0;
41  int b = popup->count();
42  while ( a < b )
43  {
44  int w = ( a + b ) / 2;
45 
46  int id = popup->idAt( w );
47  int j = str.localeAwareCompare( popup->text( id ) );
48 
49  if ( j > 0 )
50  a = w + 1;
51  else
52  b = w;
53  }
54 
55  index = a; // it doesn't really matter ... a == b here.
56 
57  Q_ASSERT( a == b );
58 }
59 
60 static TQPopupMenu * checkInsertIndex( TQPopupMenu *popup,
61  const TQStringList *tags, const TQString &submenu )
62 {
63  int pos = tags->findIndex( submenu );
64 
65  TQPopupMenu *pi = 0;
66  if ( pos != -1 )
67  {
68  TQMenuItem *p = popup->findItem( pos );
69  pi = p ? p->popup() : 0;
70  }
71  if ( !pi )
72  pi = popup;
73 
74  return pi;
75 }
76 
77 class KLanguageButtonPrivate
78 {
79 public:
80  TQPushButton * button;
81  bool staticText;
82 };
83 
84 KLanguageButton::KLanguageButton( TQWidget * parent, const char *name )
85  : TQWidget( parent, name )
86 {
87  init(name);
88 }
89 
90 KLanguageButton::KLanguageButton( const TQString & text, TQWidget * parent, const char *name )
91  : TQWidget( parent, name )
92 {
93  init(name);
94 
95  setText(text);
96 }
97 
98 void KLanguageButton::setText(const TQString & text)
99 {
100  d->staticText = true;
101  d->button->setText(text);
102  d->button->setIconSet(TQIconSet()); // remove the icon
103 }
104 
105 void KLanguageButton::init(const char * name)
106 {
107  m_current = 0;
108  m_ids = new TQStringList;
109  m_popup = 0;
110  m_oldPopup = 0;
111  d = new KLanguageButtonPrivate;
112 
113  d->staticText = false;
114 
115  TQHBoxLayout *layout = new TQHBoxLayout(this, 0, 0);
116  layout->setAutoAdd(true);
117  d->button = new TQPushButton( this, name ); // HPB don't touch this!!
118 
119  clear();
120 }
121 
122 KLanguageButton::~KLanguageButton()
123 {
124  delete m_ids;
125 
126  delete d->button;
127  delete d;
128 }
129 
130 
131 void KLanguageButton::insertLanguage( const TQString& path, const TQString& name,
132  const TQString&, const TQString &submenu, int index )
133 {
134  TQString output = name + TQString::fromLatin1( " (" ) + path +
135  TQString::fromLatin1( ")" );
136 #if 0
137  // Nooooo ! Country != language
138  TQPixmap flag( locate( "locale", sub + path +
139  TQString::fromLatin1( "/flag.png" ) ) );
140 #endif
141  insertItem( output, path, submenu, index );
142 }
143 
144 void KLanguageButton::insertItem( const TQIconSet& icon, const TQString &text,
145  const TQString & id, const TQString &submenu, int index )
146 {
147  TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
148  checkInsertPos( pi, text, index );
149  pi->insertItem( icon, text, count(), index );
150  m_ids->append( id );
151 }
152 
153 void KLanguageButton::insertItem( const TQString &text, const TQString & id,
154  const TQString &submenu, int index )
155 {
156  insertItem( TQIconSet(), text, id, submenu, index );
157 }
158 
159 void KLanguageButton::insertSeparator( const TQString &submenu, int index )
160 {
161  TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
162  pi->insertSeparator( index );
163  m_ids->append( TQString::null );
164 }
165 
166 void KLanguageButton::insertSubmenu( const TQIconSet & icon,
167  const TQString &text, const TQString &id,
168  const TQString &submenu, int index )
169 {
170  TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
171  TQPopupMenu *p = new TQPopupMenu( pi );
172  checkInsertPos( pi, text, index );
173  pi->insertItem( icon, text, p, count(), index );
174  m_ids->append( id );
175  connect( p, TQ_SIGNAL( activated( int ) ),
176  TQ_SLOT( slotActivated( int ) ) );
177  connect( p, TQ_SIGNAL( highlighted( int ) ), this,
178  TQ_SLOT( slotHighlighted( int ) ) );
179 }
180 
181 void KLanguageButton::insertSubmenu( const TQString &text, const TQString &id,
182  const TQString &submenu, int index )
183 {
184  insertSubmenu(TQIconSet(), text, id, submenu, index);
185 }
186 
187 void KLanguageButton::slotActivated( int index )
188 {
189  //kdDebug() << "slotActivated" << index << endl;
190 
191  setCurrentItem( index );
192 
193  // Forward event from popup menu as if it was emitted from this widget:
194  TQString id = *m_ids->at( index );
195  emit activated( id );
196 }
197 
198 void KLanguageButton::slotHighlighted( int index )
199 {
200  //kdDebug() << "slotHighlighted" << index << endl;
201 
202  TQString id = *m_ids->at( index );
203  emit ( highlighted(id) );
204 }
205 
206 int KLanguageButton::count() const
207 {
208  return m_ids->count();
209 }
210 
211 void KLanguageButton::clear()
212 {
213  m_ids->clear();
214 
215  delete m_oldPopup;
216  m_oldPopup = m_popup;
217  m_popup = new TQPopupMenu( this );
218 
219  d->button->setPopup( m_popup );
220 
221  connect( m_popup, TQ_SIGNAL( activated( int ) ),
222  TQ_SLOT( slotActivated( int ) ) );
223  connect( m_popup, TQ_SIGNAL( highlighted( int ) ),
224  TQ_SLOT( slotHighlighted( int ) ) );
225 
226  if ( !d->staticText )
227  {
228  d->button->setText( TQString::null );
229  d->button->setIconSet( TQIconSet() );
230  }
231 }
232 
233 bool KLanguageButton::contains( const TQString & id ) const
234 {
235  return m_ids->contains( id ) > 0;
236 }
237 
238 TQString KLanguageButton::current() const
239 {
240  return *m_ids->at( currentItem() );
241 }
242 
243 
244 TQString KLanguageButton::id( int i ) const
245 {
246  if ( i < 0 || i >= count() )
247  {
248  kdDebug() << "KLanguageButton::tag(), unknown tag " << i << endl;
249  return TQString::null;
250  }
251  return *m_ids->at( i );
252 }
253 
254 
255 int KLanguageButton::currentItem() const
256 {
257  return m_current;
258 }
259 
260 void KLanguageButton::setCurrentItem( int i )
261 {
262  if ( i < 0 || i >= count() )
263  return;
264  m_current = i;
265 
266  if ( !d->staticText )
267  {
268  d->button->setText( m_popup->text( m_current ) );
269  TQIconSet *icon = m_popup->iconSet( m_current );
270  if ( icon )
271  d->button->setIconSet( *icon );
272  else
273  d->button->setIconSet( TQIconSet() );
274  }
275 }
276 
277 void KLanguageButton::setCurrentItem( const TQString & id )
278 {
279  int i = m_ids->findIndex( id );
280  if ( id.isNull() )
281  i = 0;
282  if ( i != -1 )
283  setCurrentItem( i );
284 }
KLanguageButton::contains
bool contains(const TQString &id) const
Returns TRUE if the combobox contains id.
Definition: klanguagebutton.cpp:233
KLanguageButton::~KLanguageButton
virtual ~KLanguageButton()
Deconstructor.
Definition: klanguagebutton.cpp:122
KLanguageButton::clear
void clear()
Removes all combobox items.
Definition: klanguagebutton.cpp:211
KLanguageButton::setText
void setText(const TQString &text)
Changes the current text item of the combobox, and makes the text static.
Definition: klanguagebutton.cpp:98
KLanguageButton::current
TQString current() const
Returns the id of the combobox's current item.
Definition: klanguagebutton.cpp:238
KLanguageButton::insertSubmenu
void insertSubmenu(const TQIconSet &icon, const TQString &text, const TQString &id, const TQString &submenu=TQString::null, int index=-1)
Inserts a submenu into the combo box.
Definition: klanguagebutton.cpp:166
KLanguageButton::KLanguageButton
KLanguageButton(TQWidget *parent=0, const char *name=0)
Constructs a combobox widget with parent parent called name.
Definition: klanguagebutton.cpp:84
KLanguageButton::insertSeparator
void insertSeparator(const TQString &submenu=TQString::null, int index=-1)
Inserts a seperator item into the combo box.
Definition: klanguagebutton.cpp:159
KLanguageButton::setCurrentItem
void setCurrentItem(const TQString &id)
Sets id as current item.
Definition: klanguagebutton.cpp:277
KLanguageButton::activated
void activated(const TQString &id)
This signal is emitted when a new item is activated.
KLanguageButton::insertItem
void insertItem(const TQIconSet &icon, const TQString &text, const TQString &id, const TQString &submenu=TQString::null, int index=-1)
Inserts an item into the combo box.
Definition: klanguagebutton.cpp:144
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
locate
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())

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.