kdeui Library API Documentation

klistbox.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 Reginald Stadlbauer <reggie@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 #include <qtimer.h>
00019 
00020 #include <kglobalsettings.h>
00021 #include <kcursor.h>
00022 #include <kapplication.h>
00023 #include <kipc.h>
00024 #include <kdebug.h>
00025 
00026 #include "klistbox.h"
00027 
00028 #ifdef Q_WS_X11
00029 #include <X11/Xlib.h>
00030 #endif
00031 
00032 KListBox::KListBox( QWidget *parent, const char *name, WFlags f )
00033     : QListBox( parent, name, f )
00034 {
00035     connect( this, SIGNAL( onViewport() ),
00036          this, SLOT( slotOnViewport() ) );
00037     connect( this, SIGNAL( onItem( QListBoxItem * ) ),
00038          this, SLOT( slotOnItem( QListBoxItem * ) ) );
00039     slotSettingsChanged(KApplication::SETTINGS_MOUSE);
00040     if (kapp)
00041     {
00042         connect( kapp, SIGNAL( settingsChanged(int) ), SLOT( slotSettingsChanged(int) ) );
00043         kapp->addKipcEventMask( KIPC::SettingsChanged );
00044     }
00045 
00046     m_pCurrentItem = 0L;
00047 
00048     m_pAutoSelect = new QTimer( this );
00049     connect( m_pAutoSelect, SIGNAL( timeout() ),
00050              this, SLOT( slotAutoSelect() ) );
00051 }
00052 
00053 void KListBox::slotOnItem( QListBoxItem *item )
00054 {
00055     if ( item && m_bChangeCursorOverItem && m_bUseSingle )
00056         viewport()->setCursor( KCursor().handCursor() );
00057 
00058     if ( item && (m_autoSelectDelay > -1) && m_bUseSingle ) {
00059       m_pAutoSelect->start( m_autoSelectDelay, true );
00060       m_pCurrentItem = item;
00061     }
00062 }
00063 
00064 void KListBox::slotOnViewport()
00065 {
00066     if ( m_bChangeCursorOverItem )
00067         viewport()->unsetCursor();
00068 
00069     m_pAutoSelect->stop();
00070     m_pCurrentItem = 0L;
00071 }
00072 
00073 
00074 void KListBox::slotSettingsChanged(int category)
00075 {
00076     if (category != KApplication::SETTINGS_MOUSE)
00077         return;
00078     m_bUseSingle = KGlobalSettings::singleClick();
00079 
00080     disconnect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *,
00081                           const QPoint & ) ),
00082         this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
00083                             const QPoint & ) ) );
00084 //         disconnect( this, SIGNAL( doubleClicked( QListBoxItem *, 
00085 //                       const QPoint & ) ),
00086 //          this, SLOT( slotExecute( QListBoxItem *, 
00087 //                       const QPoint & ) ) );
00088 
00089     if( m_bUseSingle )
00090     {
00091       connect( this, SIGNAL( mouseButtonClicked( int, QListBoxItem *, 
00092                          const QPoint & ) ),
00093            this, SLOT( slotMouseButtonClicked( int, QListBoxItem *,
00094                            const QPoint & ) ) );
00095     }
00096     else
00097     {
00098 //         connect( this, SIGNAL( doubleClicked( QListBoxItem *, 
00099 //                        const QPoint & ) ),
00100 //                  this, SLOT( slotExecute( QListBoxItem *, 
00101 //                    const QPoint & ) ) );
00102     }
00103 
00104     m_bChangeCursorOverItem = KGlobalSettings::changeCursorOverIcon();
00105     m_autoSelectDelay = KGlobalSettings::autoSelectDelay();
00106 
00107     if( !m_bUseSingle || !m_bChangeCursorOverItem )
00108         viewport()->unsetCursor();
00109 }
00110 
00111 void KListBox::slotAutoSelect()
00112 {
00113   // check that the item still exists
00114   if( index( m_pCurrentItem ) == -1 )
00115     return;
00116 
00117   //Give this widget the keyboard focus.
00118   if( !hasFocus() )
00119     setFocus();
00120 
00121 #ifdef Q_WS_X11 //FIXME
00122   Window root;
00123   Window child;
00124   int root_x, root_y, win_x, win_y;
00125   uint keybstate;
00126   XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00127          &root_x, &root_y, &win_x, &win_y, &keybstate );
00128 #endif
00129 
00130   QListBoxItem* previousItem = item( currentItem() ); 
00131   setCurrentItem( m_pCurrentItem );
00132 
00133   if( m_pCurrentItem ) {
00134 #ifndef Q_WS_QWS //FIXME
00135     //Shift pressed?
00136     if( (keybstate & ShiftMask) ) {
00137 #endif
00138       bool block = signalsBlocked();
00139       blockSignals( true );
00140 
00141 #ifndef Q_WS_QWS //FIXME
00142       //No Ctrl? Then clear before!
00143       if( !(keybstate & ControlMask) )  
00144     clearSelection(); 
00145 #endif
00146 
00147       bool select = !m_pCurrentItem->isSelected();
00148       bool update = viewport()->isUpdatesEnabled();
00149       viewport()->setUpdatesEnabled( false );
00150 
00151       bool down = index( previousItem ) < index( m_pCurrentItem );
00152       QListBoxItem* it = down ? previousItem : m_pCurrentItem;
00153       for (;it ; it = it->next() ) {
00154     if ( down && it == m_pCurrentItem ) {
00155       setSelected( m_pCurrentItem, select );
00156       break;
00157     }
00158     if ( !down && it == previousItem ) {
00159       setSelected( previousItem, select );
00160       break;
00161     }
00162     setSelected( it, select );
00163       }
00164       
00165       blockSignals( block );
00166       viewport()->setUpdatesEnabled( update );
00167       triggerUpdate( false );
00168 
00169       emit selectionChanged();
00170 
00171       if( selectionMode() == QListBox::Single )
00172     emit selectionChanged( m_pCurrentItem );
00173     }
00174 #ifndef Q_WS_QWS //FIXME
00175     else if( (keybstate & ControlMask) )
00176       setSelected( m_pCurrentItem, !m_pCurrentItem->isSelected() );
00177 #endif
00178     else {
00179       bool block = signalsBlocked();
00180       blockSignals( true );
00181 
00182       if( !m_pCurrentItem->isSelected() )
00183     clearSelection(); 
00184 
00185       blockSignals( block );
00186 
00187       setSelected( m_pCurrentItem, true );
00188     }
00189 #ifndef Q_WS_QWS //FIXME
00190   }
00191   else
00192     kdDebug() << "Thatīs not supposed to happen!!!!" << endl;
00193 #endif
00194 }
00195 
00196 void KListBox::emitExecute( QListBoxItem *item, const QPoint &pos )
00197 {
00198 #ifdef Q_WS_X11 //FIXME
00199   Window root;
00200   Window child;
00201   int root_x, root_y, win_x, win_y;
00202   uint keybstate;
00203   XQueryPointer( qt_xdisplay(), qt_xrootwin(), &root, &child,
00204          &root_x, &root_y, &win_x, &win_y, &keybstate );
00205 #endif
00206     
00207   m_pAutoSelect->stop();
00208   
00209 #ifndef Q_WS_QWS //FIXME
00210   //Donīt emit executed if in SC mode and Shift or Ctrl are pressed
00211   if( !( m_bUseSingle && ((keybstate & ShiftMask) || (keybstate & ControlMask)) ) ) {
00212 #endif
00213     emit executed( item );
00214     emit executed( item, pos );
00215 #ifndef Q_WS_QWS //FIXME
00216   }
00217 #endif
00218 }
00219 
00220 //
00221 // 2000-16-01 Espen Sand
00222 // This widget is used in dialogs. It should ignore
00223 // F1 (and combinations) and Escape since these are used
00224 // to start help or close the dialog. This functionality
00225 // should be done in QListView but it is not (at least now)
00226 //
00227 void KListBox::keyPressEvent(QKeyEvent *e)
00228 {
00229   if( e->key() == Key_Escape )
00230   {
00231     e->ignore();
00232   }
00233   else if( e->key() == Key_F1 )
00234   {
00235     e->ignore();
00236   }
00237   else
00238   {
00239     QListBox::keyPressEvent(e);
00240   }
00241 }
00242 
00243 void KListBox::focusOutEvent( QFocusEvent *fe )
00244 {
00245   m_pAutoSelect->stop();
00246 
00247   QListBox::focusOutEvent( fe );
00248 }
00249 
00250 void KListBox::leaveEvent( QEvent *e ) 
00251 {
00252   m_pAutoSelect->stop();
00253 
00254   QListBox::leaveEvent( e );
00255 }
00256 
00257 void KListBox::contentsMousePressEvent( QMouseEvent *e )
00258 {
00259   if( (selectionMode() == Extended) && (e->state() & ShiftButton) && !(e->state() & ControlButton) ) {
00260     bool block = signalsBlocked();
00261     blockSignals( true );
00262 
00263     clearSelection();
00264 
00265     blockSignals( block );
00266   }
00267 
00268   QListBox::contentsMousePressEvent( e );
00269 }
00270 
00271 void KListBox::contentsMouseDoubleClickEvent ( QMouseEvent * e )
00272 {
00273   QListBox::contentsMouseDoubleClickEvent( e );
00274 
00275   QListBoxItem* item = itemAt( e->pos() );
00276 
00277   if( item ) {
00278     emit doubleClicked( item, e->globalPos() );
00279 
00280     if( (e->button() == LeftButton) && !m_bUseSingle )
00281       emitExecute( item, e->globalPos() );
00282   }
00283 }
00284 
00285 void KListBox::slotMouseButtonClicked( int btn, QListBoxItem *item, const QPoint &pos )
00286 {
00287   if( (btn == LeftButton) && item )
00288     emitExecute( item, pos );
00289 }
00290 
00291 void KListBox::virtual_hook( int, void* )
00292 { /*BASE::virtual_hook( id, data );*/ }
00293 
00294 #include "klistbox.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:04 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001