summaryrefslogtreecommitdiffstats
path: root/kdeui/klanguagebutton.cpp
blob: 7da8b85e583fa8b4f8da93fbf2ed80eb1bea2907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * klanguagebutton.cpp - Adds some methods for inserting languages.
 *
 * Copyright (c) 1999-2003 Hans Petter Bieker <bieker@kde.org>
 *
 * Requires the Qt widget libraries, available at no cost at
 * http://www.trolltech.com/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#define INCLUDE_MENUITEM_DEF
#include <tqpopupmenu.h>
#include <tqlayout.h>
#include <tqpushbutton.h>

#include "klanguagebutton.h"
#include "klanguagebutton.moc"

#include <kdebug.h>

static void checkInsertPos( TQPopupMenu *popup, const TQString & str,
                            int &index )
{
  if ( index == -1 )
    return;

  int a = 0;
  int b = popup->count();
  while ( a < b )
  {
    int w = ( a + b ) / 2;

    int id = popup->idAt( w );
    int j = str.localeAwareCompare( popup->text( id ) );

    if ( j > 0 )
      a = w + 1;
    else
      b = w;
  }

  index = a; // it doesn't really matter ... a == b here.

  Q_ASSERT( a == b );
}

static TQPopupMenu * checkInsertIndex( TQPopupMenu *popup,
                                      const TQStringList *tags, const TQString &submenu )
{
  int pos = tags->tqfindIndex( submenu );

  TQPopupMenu *pi = 0;
  if ( pos != -1 )
  {
    TQMenuItem *p = popup->tqfindItem( pos );
    pi = p ? p->popup() : 0;
  }
  if ( !pi )
    pi = popup;

  return pi;
}

class KLanguageButtonPrivate
{
public:
  TQPushButton * button;
  bool staticText;
};

KLanguageButton::KLanguageButton( TQWidget * parent, const char *name )
  : TQWidget( parent, name )
{
  init(name);
}

KLanguageButton::KLanguageButton( const TQString & text, TQWidget * parent, const char *name )
  : TQWidget( parent, name )
{
  init(name);

  setText(text);
}

void KLanguageButton::setText(const TQString & text)
{
  d->staticText = true;
  d->button->setText(text);
  d->button->setIconSet(TQIconSet()); // remove the icon
}

void KLanguageButton::init(const char * name)
{
  m_current = 0;
  m_ids = new TQStringList;
  m_popup = 0;
  m_oldPopup = 0;
  d = new KLanguageButtonPrivate;

  d->staticText = false;

  TQHBoxLayout *layout = new TQHBoxLayout(this, 0, 0);
  layout->setAutoAdd(true);
  d->button = new TQPushButton( this, name ); // HPB don't touch this!!

  clear();
}

KLanguageButton::~KLanguageButton()
{
  delete m_ids;

  delete d->button;
  delete d;
}


void KLanguageButton::insertLanguage( const TQString& path, const TQString& name,
                        const TQString&, const TQString &submenu, int index )
{
  TQString output = name + TQString::tqfromLatin1( " (" ) + path +
                   TQString::tqfromLatin1( ")" );
#if 0
  // Nooooo ! Country != language
  TQPixmap flag( locate( "locale", sub + path +
                TQString::tqfromLatin1( "/flag.png" ) ) );
#endif
  insertItem( output, path, submenu, index );
}

void KLanguageButton::insertItem( const TQIconSet& icon, const TQString &text,
                                  const TQString & id, const TQString &submenu, int index )
{
  TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
  checkInsertPos( pi, text, index );
  pi->insertItem( icon, text, count(), index );
  m_ids->append( id );
}

void KLanguageButton::insertItem( const TQString &text, const TQString & id,
                                  const TQString &submenu, int index )
{
  insertItem( TQIconSet(), text, id, submenu, index );
}

void KLanguageButton::insertSeparator( const TQString &submenu, int index )
{
  TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
  pi->insertSeparator( index );
  m_ids->append( TQString::null );
}

void KLanguageButton::insertSubmenu( const TQIconSet & icon,
                                     const TQString &text, const TQString &id,
                                     const TQString &submenu, int index )
{
  TQPopupMenu *pi = checkInsertIndex( m_popup, m_ids, submenu );
  TQPopupMenu *p = new TQPopupMenu( pi );
  checkInsertPos( pi, text, index );
  pi->insertItem( icon, text, p, count(), index );
  m_ids->append( id );
  connect( p, TQT_SIGNAL( activated( int ) ),
           TQT_SLOT( slotActivated( int ) ) );
  connect( p, TQT_SIGNAL( highlighted( int ) ), this,
           TQT_SLOT( slotHighlighted( int ) ) );
}

void KLanguageButton::insertSubmenu( const TQString &text, const TQString &id,
                                     const TQString &submenu, int index )
{
  insertSubmenu(TQIconSet(), text, id, submenu, index);
}

void KLanguageButton::slotActivated( int index )
{
  //kdDebug() << "slotActivated" << index << endl;

  setCurrentItem( index );

  // Forward event from popup menu as if it was emitted from this widget:
  TQString id = *m_ids->tqat( index );
  emit activated( id );
}

void KLanguageButton::slotHighlighted( int index )
{
  //kdDebug() << "slotHighlighted" << index << endl;

  TQString id = *m_ids->tqat( index );
  emit ( highlighted(id) );
}

int KLanguageButton::count() const
{
  return m_ids->count();
}

void KLanguageButton::clear()
{
  m_ids->clear();

  delete m_oldPopup;
  m_oldPopup = m_popup;
  m_popup = new TQPopupMenu( this );

  d->button->setPopup( m_popup );

  connect( m_popup, TQT_SIGNAL( activated( int ) ),
           TQT_SLOT( slotActivated( int ) ) );
  connect( m_popup, TQT_SIGNAL( highlighted( int ) ),
           TQT_SLOT( slotHighlighted( int ) ) );

  if ( !d->staticText )
  {
    d->button->setText( TQString::null );
    d->button->setIconSet( TQIconSet() );
  }
}

bool KLanguageButton::contains( const TQString & id ) const
{
  return m_ids->tqcontains( id ) > 0;
}

TQString KLanguageButton::current() const
{
  return *m_ids->tqat( currentItem() );
}


TQString KLanguageButton::id( int i ) const
{
  if ( i < 0 || i >= count() )
  {
    kdDebug() << "KLanguageButton::tag(), unknown tag " << i << endl;
    return TQString::null;
  }
  return *m_ids->tqat( i );
}


int KLanguageButton::currentItem() const
{
  return m_current;
}

void KLanguageButton::setCurrentItem( int i )
{
  if ( i < 0 || i >= count() )
    return;
  m_current = i;

  if ( !d->staticText )
  {
    d->button->setText( m_popup->text( m_current ) );
    TQIconSet *icon = m_popup->iconSet( m_current );
    if ( icon )
      d->button->setIconSet( *icon );
    else
      d->button->setIconSet( TQIconSet() );
  }
}

void KLanguageButton::setCurrentItem( const TQString & id )
{
  int i = m_ids->tqfindIndex( id );
  if ( id.isNull() )
    i = 0;
  if ( i != -1 )
    setCurrentItem( i );
}