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

tdeui

klanguagebutton.cpp
00001 /*
00002  * klanguagebutton.cpp - Adds some methods for inserting languages.
00003  *
00004  * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
00005  *
00006  * Requires the Qt widget libraries, available at no cost at
00007  * http://www.trolltech.com/
00008  *
00009  *  This program is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022  */
00023 
00024 #define INCLUDE_MENUITEM_DEF
00025 #include <tqpopupmenu.h>
00026 #include <tqlayout.h>
00027 #include <tqpushbutton.h>
00028 
00029 #include "klanguagebutton.h"
00030 #include "klanguagebutton.moc"
00031 
00032 #include <kdebug.h>
00033 
00034 static void checkInsertPos( TQPopupMenu *popup, const TQString & str,
00035                             int &index )
00036 {
00037   if ( index == -1 )
00038     return;
00039 
00040   int a = 0;
00041   int b = popup->count();
00042   while ( a < b )
00043   {
00044     int w = ( a + b ) / 2;
00045 
00046     int id = popup->idAt( w );
00047     int j = str.localeAwareCompare( popup->text( id ) );
00048 
00049     if ( j > 0 )
00050       a = w + 1;
00051     else
00052       b = w;
00053   }
00054 
00055   index = a; // it doesn't really matter ... a == b here.
00056 
00057   Q_ASSERT( a == b );
00058 }
00059 
00060 static TQPopupMenu * checkInsertIndex( TQPopupMenu *popup,
00061                                       const TQStringList *tags, const TQString &submenu )
00062 {
00063   int pos = tags->findIndex( submenu );
00064 
00065   TQPopupMenu *pi = 0;
00066   if ( pos != -1 )
00067   {
00068     TQMenuItem *p = popup->findItem( pos );
00069     pi = p ? p->popup() : 0;
00070   }
00071   if ( !pi )
00072     pi = popup;
00073 
00074   return pi;
00075 }
00076 
00077 class KLanguageButtonPrivate
00078 {
00079 public:
00080   TQPushButton * button;
00081   bool staticText;
00082 };
00083 
00084 KLanguageButton::KLanguageButton( TQWidget * parent, const char *name )
00085   : TQWidget( parent, name )
00086 {
00087   init(name);
00088 }
00089 
00090 KLanguageButton::KLanguageButton( const TQString & text, TQWidget * parent, const char *name )
00091   : TQWidget( parent, name )
00092 {
00093   init(name);
00094 
00095   setText(text);
00096 }
00097 
00098 void KLanguageButton::setText(const TQString & text)
00099 {
00100   d->staticText = true;
00101   d->button->setText(text);
00102   d->button->setIconSet(TQIconSet()); // remove the icon
00103 }
00104 
00105 void KLanguageButton::init(const char * name)
00106 {
00107   m_current = 0;
00108   m_ids = new TQStringList;
00109   m_popup = 0;
00110   m_oldPopup = 0;
00111   d = new KLanguageButtonPrivate;
00112 
00113   d->staticText = false;
00114 
00115   TQHBoxLayout *layout = new TQHBoxLayout(this, 0, 0);
00116   layout->setAutoAdd(true);
00117   d->button = new TQPushButton( this, name ); // HPB don't touch this!!
00118 
00119   clear();
00120 }
00121 
00122 KLanguageButton::~KLanguageButton()
00123 {
00124   delete m_ids;
00125 
00126   delete d->button;
00127   delete d;
00128 }
00129 
00130 
00131 void KLanguageButton::insertLanguage( const TQString& path, const TQString& name,
00132                         const TQString&, const TQString &submenu, int index )
00133 {
00134   TQString output = name + TQString::fromLatin1( " (" ) + path +
00135                    TQString::fromLatin1( ")" );
00136 #if 0
00137   // Nooooo ! Country != language
00138   TQPixmap flag( locate( "locale", sub + path +
00139                 TQString::fromLatin1( "/flag.png" ) ) );
00140 #endif
00141   insertItem( output, path, submenu, index );
00142 }
00143 
00144 void KLanguageButton::insertItem( const TQIconSet& icon, const TQString &text,
00145                                   const TQString & id, const TQString &submenu, int index )
00146 {
00147   TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
00148   checkInsertPos( pi, text, index );
00149   pi->insertItem( icon, text, count(), index );
00150   m_ids->append( id );
00151 }
00152 
00153 void KLanguageButton::insertItem( const TQString &text, const TQString & id,
00154                                   const TQString &submenu, int index )
00155 {
00156   insertItem( TQIconSet(), text, id, submenu, index );
00157 }
00158 
00159 void KLanguageButton::insertSeparator( const TQString &submenu, int index )
00160 {
00161   TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
00162   pi->insertSeparator( index );
00163   m_ids->append( TQString::null );
00164 }
00165 
00166 void KLanguageButton::insertSubmenu( const TQIconSet & icon,
00167                                      const TQString &text, const TQString &id,
00168                                      const TQString &submenu, int index )
00169 {
00170   TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
00171   TQPopupMenu *p = new TQPopupMenu( pi );
00172   checkInsertPos( pi, text, index );
00173   pi->insertItem( icon, text, p, count(), index );
00174   m_ids->append( id );
00175   connect( p, TQT_SIGNAL( activated( int ) ),
00176            TQT_SLOT( slotActivated( int ) ) );
00177   connect( p, TQT_SIGNAL( highlighted( int ) ), this,
00178            TQT_SLOT( slotHighlighted( int ) ) );
00179 }
00180 
00181 void KLanguageButton::insertSubmenu( const TQString &text, const TQString &id,
00182                                      const TQString &submenu, int index )
00183 {
00184   insertSubmenu(TQIconSet(), text, id, submenu, index);
00185 }
00186 
00187 void KLanguageButton::slotActivated( int index )
00188 {
00189   //kdDebug() << "slotActivated" << index << endl;
00190 
00191   setCurrentItem( index );
00192 
00193   // Forward event from popup menu as if it was emitted from this widget:
00194   TQString id = *m_ids->at( index );
00195   emit activated( id );
00196 }
00197 
00198 void KLanguageButton::slotHighlighted( int index )
00199 {
00200   //kdDebug() << "slotHighlighted" << index << endl;
00201 
00202   TQString id = *m_ids->at( index );
00203   emit ( highlighted(id) );
00204 }
00205 
00206 int KLanguageButton::count() const
00207 {
00208   return m_ids->count();
00209 }
00210 
00211 void KLanguageButton::clear()
00212 {
00213   m_ids->clear();
00214 
00215   delete m_oldPopup;
00216   m_oldPopup = m_popup;
00217   m_popup = new TQPopupMenu( this );
00218 
00219   d->button->setPopup( m_popup );
00220 
00221   connect( m_popup, TQT_SIGNAL( activated( int ) ),
00222            TQT_SLOT( slotActivated( int ) ) );
00223   connect( m_popup, TQT_SIGNAL( highlighted( int ) ),
00224            TQT_SLOT( slotHighlighted( int ) ) );
00225 
00226   if ( !d->staticText )
00227   {
00228     d->button->setText( TQString::null );
00229     d->button->setIconSet( TQIconSet() );
00230   }
00231 }
00232 
00233 bool KLanguageButton::contains( const TQString & id ) const
00234 {
00235   return m_ids->contains( id ) > 0;
00236 }
00237 
00238 TQString KLanguageButton::current() const
00239 {
00240   return *m_ids->at( currentItem() );
00241 }
00242 
00243 
00244 TQString KLanguageButton::id( int i ) const
00245 {
00246   if ( i < 0 || i >= count() )
00247   {
00248     kdDebug() << "KLanguageButton::tag(), unknown tag " << i << endl;
00249     return TQString::null;
00250   }
00251   return *m_ids->at( i );
00252 }
00253 
00254 
00255 int KLanguageButton::currentItem() const
00256 {
00257   return m_current;
00258 }
00259 
00260 void KLanguageButton::setCurrentItem( int i )
00261 {
00262   if ( i < 0 || i >= count() )
00263     return;
00264   m_current = i;
00265 
00266   if ( !d->staticText )
00267   {
00268     d->button->setText( m_popup->text( m_current ) );
00269     TQIconSet *icon = m_popup->iconSet( m_current );
00270     if ( icon )
00271       d->button->setIconSet( *icon );
00272     else
00273       d->button->setIconSet( TQIconSet() );
00274   }
00275 }
00276 
00277 void KLanguageButton::setCurrentItem( const TQString & id )
00278 {
00279   int i = m_ids->findIndex( id );
00280   if ( id.isNull() )
00281     i = 0;
00282   if ( i != -1 )
00283     setCurrentItem( i );
00284 }

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.7.6.1
This website is maintained by Timothy Pearson.