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 "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> // schroder
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> // schroder
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 //         disconnect( this, SIGNAL( doubleClicked( QListBoxItem *, 
00093 //                       const QPoint & ) ),
00094 //          this, SLOT( slotExecute( QListBoxItem *, 
00095 //                       const QPoint & ) ) );
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 //         connect( this, SIGNAL( doubleClicked( QListBoxItem *, 
00107 //                        const QPoint & ) ),
00108 //                  this, SLOT( slotExecute( QListBoxItem *, 
00109 //                    const QPoint & ) ) );
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   // check that the item still exists
00122   if( index( m_pCurrentItem ) == -1 )
00123     return;
00124 
00125   //Give this widget the keyboard focus.
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     //Shift pressed?
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       //No Ctrl? Then clear before!
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   //Donīt emit executed if in SC mode and Shift or Ctrl are pressed
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 // 2000-16-01 Espen Sand
00230 // This widget is used in dialogs. It should ignore
00231 // F1 (and combinations) and Escape since these are used
00232 // to start help or close the dialog. This functionality
00233 // should be done in QListView but it is not (at least now)
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 { /*BASE::virtual_hook( id, data );*/ }
00301 
00302 #include "klistbox.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:43:52 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003