00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "config.h"
00019
00020 #include <qtimer.h>
00021
00022 #include <kglobalsettings.h>
00023 #include <kcursor.h>
00024 #include <kapplication.h>
00025
00026 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00027 #include <kipc.h>
00028 #endif
00029
00030 #include <kdebug.h>
00031
00032 #include "klistbox.h"
00033
00034 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00035 #include <X11/Xlib.h>
00036 #endif
00037
00038 KListBox::KListBox( QWidget *parent, const char *name, WFlags f )
00039 : QListBox( parent, name, f )
00040 {
00041 connect( this, SIGNAL( onViewport() ),
00042 this, SLOT( slotOnViewport() ) );
00043 connect( this, SIGNAL( onItem( QListBoxItem * ) ),
00044 this, SLOT( slotOnItem( QListBoxItem * ) ) );
00045 slotSettingsChanged(KApplication::SETTINGS_MOUSE);
00046 if (kapp)
00047 {
00048 connect( kapp, SIGNAL( settingsChanged(int) ), SLOT( slotSettingsChanged(int) ) );
00049 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00050 kapp->addKipcEventMask( KIPC::SettingsChanged );
00051 #endif
00052 }
00053
00054 m_pCurrentItem = 0L;
00055
00056 m_pAutoSelect = new QTimer( this );
00057 connect( m_pAutoSelect, SIGNAL( timeout() ),
00058 this, SLOT( slotAutoSelect() ) );
00059 }
00060
00061 void KListBox::slotOnItem( QListBoxItem *item )
00062 {
00063 if ( item && m_bChangeCursorOverItem && m_bUseSingle )
00064 viewport()->setCursor( KCursor().handCursor() );
00065
00066 if ( item && (m_autoSelectDelay > -1) && m_bUseSingle ) {
00067 m_pAutoSelect->start( m_autoSelectDelay, true );
00068 m_pCurrentItem = item;
00069 }
00070 }
00071
00072 void KListBox::slotOnViewport()
00073 {
00074 if ( m_bChangeCursorOverItem )
00075 viewport()->unsetCursor();
00076
00077 m_pAutoSelect->stop();
00078 m_pCurrentItem = 0L;
00079 }
00080
00081
00082 void KListBox::slotSettingsChanged(int category)
00083 {
00084 if (category != KApplication::SETTINGS_MOUSE)
00085 return;
00086 m_bUseSingle = KGlobalSettings::singleClick();
00087
00088 disconnect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *,
00089 const QPoint & ) ),
00090 this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
00091 const QPoint & ) ) );
00092
00093
00094
00095
00096
00097 if( m_bUseSingle )
00098 {
00099 connect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *,
00100 const QPoint & ) ),
00101 this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
00102 const QPoint & ) ) );
00103 }
00104 else
00105 {
00106
00107
00108
00109
00110 }
00111
00112 m_bChangeCursorOverItem = KGlobalSettings::changeCursorOverIcon();
00113 m_autoSelectDelay = KGlobalSettings::autoSelectDelay();
00114
00115 if( !m_bUseSingle || !m_bChangeCursorOverItem )
00116 viewport()->unsetCursor();
00117 }
00118
00119 void KListBox::slotAutoSelect()
00120 {
00121
00122 if( index( m_pCurrentItem ) == -1 )
00123 return;
00124
00125
00126 if( !hasFocus() )
00127 setFocus();
00128
00129 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00130 Window root;
00131 Window child;
00132 int root_x, root_y, win_x, win_y;
00133 uint keybstate;
00134 XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00135 &root_x, &root_y, &win_x, &win_y, &keybstate );
00136 #endif
00137
00138 QListBoxItem* previousItem = item( currentItem() );
00139 setCurrentItem( m_pCurrentItem );
00140
00141 if( m_pCurrentItem ) {
00142 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00143
00144 if( (keybstate & ShiftMask) ) {
00145 #endif
00146 bool block = signalsBlocked();
00147 blockSignals( true );
00148
00149 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00150
00151 if( !(keybstate & ControlMask) )
00152 clearSelection();
00153 #endif
00154
00155 bool select = !m_pCurrentItem->isSelected();
00156 bool update = viewport()->isUpdatesEnabled();
00157 viewport()->setUpdatesEnabled( false );
00158
00159 bool down = index( previousItem ) < index( m_pCurrentItem );
00160 QListBoxItem* it = down ? previousItem : m_pCurrentItem;
00161 for (;it ; it = it->next() ) {
00162 if ( down && it == m_pCurrentItem ) {
00163 setSelected( m_pCurrentItem, select );
00164 break;
00165 }
00166 if ( !down && it == previousItem ) {
00167 setSelected( previousItem, select );
00168 break;
00169 }
00170 setSelected( it, select );
00171 }
00172
00173 blockSignals( block );
00174 viewport()->setUpdatesEnabled( update );
00175 triggerUpdate( false );
00176
00177 emit selectionChanged();
00178
00179 if( selectionMode() == QListBox::Single )
00180 emit selectionChanged( m_pCurrentItem );
00181 }
00182 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00183 else if( (keybstate & ControlMask) )
00184 setSelected( m_pCurrentItem, !m_pCurrentItem->isSelected() );
00185 #endif
00186 else {
00187 bool block = signalsBlocked();
00188 blockSignals( true );
00189
00190 if( !m_pCurrentItem->isSelected() )
00191 clearSelection();
00192
00193 blockSignals( block );
00194
00195 setSelected( m_pCurrentItem, true );
00196 }
00197 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00198 }
00199 else
00200 kdDebug() << "Thatīs not supposed to happen!!!!" << endl;
00201 #endif
00202 }
00203
00204 void KListBox::emitExecute( QListBoxItem *item, const QPoint &pos )
00205 {
00206 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00207 Window root;
00208 Window child;
00209 int root_x, root_y, win_x, win_y;
00210 uint keybstate;
00211 XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00212 &root_x, &root_y, &win_x, &win_y, &keybstate );
00213 #endif
00214
00215 m_pAutoSelect->stop();
00216
00217 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00218
00219 if( !( m_bUseSingle && ((keybstate & ShiftMask) || (keybstate & ControlMask)) ) ) {
00220 #endif
00221 emit executed( item );
00222 emit executed( item, pos );
00223 #if defined Q_WS_X11 && ! defined K_WS_QTONLY //FIXME
00224 }
00225 #endif
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235 void KListBox::keyPressEvent(QKeyEvent *e)
00236 {
00237 if( e->key() == Key_Escape )
00238 {
00239 e->ignore();
00240 }
00241 else if( e->key() == Key_F1 )
00242 {
00243 e->ignore();
00244 }
00245 else
00246 {
00247 QListBox::keyPressEvent(e);
00248 }
00249 }
00250
00251 void KListBox::focusOutEvent( QFocusEvent *fe )
00252 {
00253 m_pAutoSelect->stop();
00254
00255 QListBox::focusOutEvent( fe );
00256 }
00257
00258 void KListBox::leaveEvent( QEvent *e )
00259 {
00260 m_pAutoSelect->stop();
00261
00262 QListBox::leaveEvent( e );
00263 }
00264
00265 void KListBox::contentsMousePressEvent( QMouseEvent *e )
00266 {
00267 if( (selectionMode() == Extended) && (e->state() & ShiftButton) && !(e->state() & ControlButton) ) {
00268 bool block = signalsBlocked();
00269 blockSignals( true );
00270
00271 clearSelection();
00272
00273 blockSignals( block );
00274 }
00275
00276 QListBox::contentsMousePressEvent( e );
00277 }
00278
00279 void KListBox::contentsMouseDoubleClickEvent ( QMouseEvent * e )
00280 {
00281 QListBox::contentsMouseDoubleClickEvent( e );
00282
00283 QListBoxItem* item = itemAt( e->pos() );
00284
00285 if( item ) {
00286 emit doubleClicked( item, e->globalPos() );
00287
00288 if( (e->button() == LeftButton) && !m_bUseSingle )
00289 emitExecute( item, e->globalPos() );
00290 }
00291 }
00292
00293 void KListBox::slotMouseButtonClicked( int btn, QListBoxItem *item, const QPoint &pos )
00294 {
00295 if( (btn == LeftButton) && item )
00296 emitExecute( item, pos );
00297 }
00298
00299 void KListBox::virtual_hook( int, void* )
00300 { }
00301
00302 #include "klistbox.moc"