24 #include <tqapplication.h>
25 #include <tqcombobox.h>
30 #include <tdeconfig.h>
31 #include <knotifyclient.h>
32 #include <tdeglobalsettings.h>
34 #include "tdecompletionbox.h"
36 class TDECompletionBox::TDECompletionBoxPrivate
48 :
TDEListBox( parent, name, (WFlags)WType_Popup ), d(new TDECompletionBoxPrivate)
52 d->tabHandling =
true;
53 d->down_workaround =
false;
55 d->emitSelected =
true;
59 setFrameStyle( TQFrame::Box | TQFrame::Plain );
62 setFocusProxy( parent );
64 setFocusPolicy( TQ_NoFocus );
66 setVScrollBarMode( Auto );
67 setHScrollBarMode( AlwaysOff );
73 connect(
this, TQT_SIGNAL( currentChanged( TQListBoxItem * )),
74 TQT_SLOT( slotCurrentChanged() ));
75 connect(
this, TQT_SIGNAL( clicked( TQListBoxItem * )),
76 TQT_SLOT( slotItemClicked( TQListBoxItem * )) );
89 const TQListBoxItem* currItem = firstItem();
92 list.append(currItem->text());
93 currItem = currItem->next();
110 int type = e->type();
112 if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(d->m_parent) ) {
114 if ( type == TQEvent::KeyPress ) {
115 TQKeyEvent *ev = TQT_TQKEYEVENT( e );
116 switch ( ev->key() ) {
118 if ( d->tabHandling && (ev->state() == Qt::NoButton ||
119 (ev->state() & ShiftButton)) ) {
126 if ( d->tabHandling && (ev->state() == Qt::NoButton) ) {
139 if ( selectedItem() ||
140 mapToGlobal( TQPoint( 0, 0 ) ).y() >
141 d->m_parent->mapToGlobal( TQPoint( 0, 0 ) ).y() )
161 if ( ev->state() & ShiftButton ) {
168 if ( ev->state() & ControlButton )
175 if ( ev->state() & ControlButton )
185 else if ( type == TQEvent::AccelOverride ) {
188 TQKeyEvent *ev = TQT_TQKEYEVENT( e );
189 switch ( ev->key() ) {
202 if ( ev->state() == Qt::NoButton ||
203 (ev->state() & ShiftButton))
211 if ( ev->state() & ControlButton )
223 else if ( type == TQEvent::FocusOut || type == TQEvent::Resize ||
224 type == TQEvent::Close || type == TQEvent::Hide ||
225 type == TQEvent::Move ) {
232 else if ( type == TQEvent::MouseButtonPress ) {
233 TQMouseEvent *ev = TQT_TQMOUSEEVENT( e );
234 if ( !rect().contains( ev->pos() ))
237 if ( !d->emitSelected && currentItem() && !::tqqt_cast<TQScrollBar*>(o) )
239 emit highlighted( currentText() );
246 return TDEListBox::eventFilter( o, e );
255 ensureCurrentVisible();
256 bool block = signalsBlocked();
257 blockSignals(
true );
259 blockSignals( block );
263 else if ( size().height() != sizeHint().height() )
270 int currentGeom = height();
271 TQPoint currentPos = pos();
273 resize( geom.size() );
275 int x = currentPos.x(), y = currentPos.y();
277 if ( !isVisible() ) {
280 TQPoint orig = d->m_parent->mapToGlobal( TQPoint(0, d->m_parent->height()) );
281 x = orig.x() + geom.x();
282 y = orig.y() + geom.y();
284 if ( x + width() > screenSize.right() )
285 x = screenSize.right() - width();
286 if (y + height() > screenSize.bottom() ) {
287 y = y - height() - d->m_parent->height();
294 y += (currentGeom-height());
302 d->upwardBox =
false;
305 tqApp->installEventFilter(
this );
320 tqApp->sendPostedEvents();
327 tqApp->removeEventFilter(
this );
328 d->cancelText = TQString::null;
335 int ih = itemHeight();
336 int h = TQMIN( 15 * ih, (
int) count() * ih ) + 2*frameWidth();
338 int w = (d->m_parent) ? d->m_parent->width() : TDEListBox::minimumSizeHint().width();
339 w = TQMAX( TDEListBox::minimumSizeHint().width(), w );
344 const TQObject* combo;
345 if ( d->m_parent && (combo = d->m_parent->parent() ) &&
346 combo->inherits(TQCOMBOBOX_OBJECT_NAME_STRING) )
348 const TQComboBox* cb =
static_cast<const TQComboBox*
>(TQT_TQWIDGET_CONST(combo));
351 w = TQMAX( w, cb->width() );
353 TQPoint parentCorner = d->m_parent->mapToGlobal(TQPoint(0, 0));
354 TQPoint comboCorner = cb->mapToGlobal(TQPoint(0, 0));
357 x += comboCorner.x() - parentCorner.x();
360 y += cb->height() - d->m_parent->height() +
361 comboCorner.y() - parentCorner.y();
364 TQRect styleAdj = style().querySubControlMetrics(TQStyle::CC_ComboBox,
365 cb, TQStyle::SC_ComboBoxListBoxPopup,
366 TQStyleOption(x, y, w, h));
369 if (!styleAdj.isNull())
373 return TQRect(x, y, w, h);
376 TQSize TDECompletionBox::sizeHint()
const
383 int i = currentItem();
385 if ( i == 0 && d->down_workaround ) {
386 d->down_workaround =
false;
388 setSelected( 0,
true );
389 emit highlighted( currentText() );
392 else if ( i < (
int) count() - 1 )
393 setCurrentItem( i + 1 );
398 if ( currentItem() > 0 )
399 setCurrentItem( currentItem() - 1 );
404 int i = currentItem() + numItemsVisible();
405 i = i > (int)count() - 1 ? (int)count() - 1 : i;
411 int i = currentItem() - numItemsVisible();
423 setCurrentItem( count() -1 );
428 d->tabHandling = enable;
433 return d->tabHandling;
438 d->cancelText = text;
443 return d->cancelText;
446 void TDECompletionBox::canceled()
448 if ( !d->cancelText.isNull() )
454 class TDECompletionBoxItem :
public TQListBoxItem
458 bool reuse(
const TQString& newText )
460 if ( text() == newText )
470 bool block = signalsBlocked();
471 blockSignals(
true );
472 insertStringList(
items, index );
473 blockSignals( block );
474 d->down_workaround =
true;
479 bool block = signalsBlocked();
480 blockSignals(
true );
482 TQListBoxItem* item = firstItem();
484 insertStringList(
items );
492 TQStringList::ConstIterator it =
items.constBegin();
493 const TQStringList::ConstIterator itEnd =
items.constEnd();
495 for ( ; it != itEnd; ++it) {
497 const bool changed = ((TDECompletionBoxItem*)item)->reuse( *it );
498 dirty = dirty || changed;
504 insertItem(
new TQListBoxText( *it ) );
513 TQListBoxItem* tmp = item;
514 while ( (item = tmp ) ) {
520 triggerUpdate(
false );
523 if ( isVisible() && size().height() != sizeHint().height() )
526 blockSignals( block );
527 d->down_workaround =
true;
530 void TDECompletionBox::slotCurrentChanged()
532 d->down_workaround =
false;
535 void TDECompletionBox::slotItemClicked( TQListBoxItem *item )
539 if ( d->down_workaround ) {
540 d->down_workaround =
false;
541 emit highlighted( item->text() );
551 d->emitSelected = state;
556 return d->emitSelected;
559 void TDECompletionBox::virtual_hook(
int id,
void* data )
560 { TDEListBox::virtual_hook(
id, data ); }
562 #include "tdecompletionbox.moc"