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( TQWidget::NoFocus );
66 setVScrollBarMode( Auto );
67 setHScrollBarMode( AlwaysOff );
73 connect(
this, TQ_SIGNAL( currentChanged( TQListBoxItem * )),
74 TQ_SLOT( slotCurrentChanged() ));
75 connect(
this, TQ_SIGNAL( clicked( TQListBoxItem * )),
76 TQ_SLOT( slotItemClicked( TQListBoxItem * )) );
89 const TQListBoxItem* currItem = firstItem();
92 list.append(currItem->text());
93 currItem = currItem->next();
110 int type = e->type();
112 if ( o == d->m_parent ) {
114 if ( type == TQEvent::KeyPress ) {
115 TQKeyEvent *ev =
static_cast<TQKeyEvent*
>( e );
116 switch ( ev->key() ) {
118 if ( d->tabHandling && (ev->state() == TQt::NoButton ||
119 (ev->state() & ShiftButton)) ) {
126 if ( d->tabHandling && (ev->state() == TQt::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 =
static_cast<TQKeyEvent*
>( e );
189 switch ( ev->key() ) {
202 if ( ev->state() == TQt::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 =
static_cast<TQMouseEvent*
>( e );
234 if ( !rect().contains( ev->pos() ))
237 if ( !d->emitSelected && currentItem() && !::tqt_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") )
348 const TQComboBox* cb =
static_cast<const TQComboBox*
>(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"
virtual void hide()
Re-implemented for internal reasons.
void userCancelled(const TQString &)
Emitted whenever the user chooses to ignore the available selections and close the this box.
TQString cancelledText() const
~TDECompletionBox()
Destroys the box.
TDECompletionBox(TQWidget *parent, const char *name=0)
Constructs a TDECompletionBox.
void activated(const TQString &)
Emitted when an item was selected, contains the text of the selected item.
virtual void slotActivated(TQListBoxItem *)
Called when an item was activated.
void down()
Moves the selection one line down or select the first item if nothing is selected yet.
void setItems(const TQStringList &items)
Clears the box and inserts items.
virtual bool eventFilter(TQObject *, TQEvent *)
Reimplemented from TDEListBox to get events from the viewport (to hide this widget on mouse-click,...
void pageDown()
Moves the selection one page down.
void setActivateOnSelect(bool state)
Set whether or not the selected signal should be emitted when an item is selected.
void up()
Moves the selection one line up or select the first item if nothing is selected yet.
void setCancelledText(const TQString &txt)
Sets the text to be emitted if the user chooses not to pick from the available matches.
void home()
Moves the selection up to the first item.
virtual void popup()
Adjusts the size of the box to fit the width of the parent given in the constructor and pops it up at...
TQRect calculateGeometry() const
This calculates the size of the dropdown and the relative position of the top left corner with respec...
void insertItems(const TQStringList &items, int index=-1)
Inserts items into the box.
void pageUp()
Moves the selection one page up.
void setTabHandling(bool enable)
Makes this widget (when visible) capture Tab-key events to traverse the items in the dropdown list.
bool activateOnSelect() const
virtual void show()
Re-implemented for internal reasons.
void end()
Moves the selection down to the last item.
bool isTabHandling() const
TQStringList items() const
Returns a list of all items currently in the box.
void sizeAndPosition()
This properly sizes and positions the listbox.
static TQRect desktopGeometry(const TQPoint &point)
A variant of TQListBox that honors KDE's system-wide settings.
void doubleClicked(TQListBoxItem *item, const TQPoint &pos)
This signal gets emitted whenever the user double clicks into the listbox.