00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifdef KDE_USE_FINAL
00027 #undef Always
00028 #include <qdockwindow.h>
00029 #endif
00030 #include "ktoolbar.h"
00031 #include "kmainwindow.h"
00032 
00033 #include <string.h>
00034 
00035 #include <qpainter.h>
00036 #include <qtooltip.h>
00037 #include <qdrawutil.h>
00038 #include <qstring.h>
00039 #include <qrect.h>
00040 #include <qobjectlist.h>
00041 #include <qtimer.h>
00042 #include <qstyle.h>
00043 
00044 #include <config.h>
00045 
00046 #include "klineedit.h"
00047 #include "kseparator.h"
00048 #include <klocale.h>
00049 #include <kapplication.h>
00050 #include <kaction.h>
00051 #include <kstdaction.h>
00052 #include <kglobal.h>
00053 #include <kconfig.h>
00054 #include <kiconloader.h>
00055 #include <kcombobox.h>
00056 #include <kpopupmenu.h>
00057 #include <kanimwidget.h>
00058 
00059 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00060 #include <kipc.h> 
00061 #endif
00062 
00063 #include <kwin.h>
00064 #include <kdebug.h>
00065 #include <qlayout.h>
00066 
00067 #include "ktoolbarbutton.h"
00068 
00069 enum {
00070     CONTEXT_TOP = 0,
00071     CONTEXT_LEFT = 1,
00072     CONTEXT_RIGHT = 2,
00073     CONTEXT_BOTTOM = 3,
00074     CONTEXT_FLOAT = 4,
00075     CONTEXT_FLAT = 5,
00076     CONTEXT_ICONS = 6,
00077     CONTEXT_TEXT = 7,
00078     CONTEXT_TEXTRIGHT = 8,
00079     CONTEXT_TEXTUNDER = 9,
00080     CONTEXT_ICONSIZES = 50 
00081 };
00082 
00083 class KToolBarPrivate
00084 {
00085 public:
00086     KToolBarPrivate() {
00087         m_iconSize     = 0;
00088         m_iconText     = KToolBar::IconOnly;
00089         m_highlight    = true;
00090         m_transparent  = true;
00091         m_honorStyle   = false;
00092 
00093         m_enableContext  = true;
00094 
00095         m_xmlguiClient   = 0;
00096         m_configurePlugged = false;
00097 
00098         oldPos = Qt::DockUnmanaged;
00099 
00100         modified = m_isHorizontal = positioned = false;
00101 
00102         IconSizeDefault = 0;
00103         IconTextDefault = "IconOnly";
00104 
00105         NewLineDefault = false;
00106         OffsetDefault = 0;
00107         PositionDefault = "Top";
00108     HiddenDefault = false;
00109         idleButtons.setAutoDelete(true);
00110     }
00111 
00112     int m_iconSize;
00113     KToolBar::IconText m_iconText;
00114     bool m_highlight : 1;
00115     bool m_transparent : 1;
00116     bool m_honorStyle : 1;
00117     bool m_isHorizontal : 1;
00118     bool m_enableContext : 1;
00119     bool m_configurePlugged : 1;
00120     bool modified : 1;
00121     bool positioned : 1;
00122 
00123     QWidget *m_parent;
00124 
00125     QMainWindow::ToolBarDock oldPos;
00126 
00127     KXMLGUIClient *m_xmlguiClient;
00128 
00129     struct ToolBarInfo
00130     {
00131         ToolBarInfo() : index( -1 ), offset( -1 ), newline( false ), dock( Qt::DockTop ) {}
00132         ToolBarInfo( Qt::Dock d, int i, bool n, int o ) : index( i ), offset( o ), newline( n ), dock( d ) {}
00133         int index, offset;
00134         bool newline;
00135         Qt::Dock dock;
00136     };
00137 
00138     ToolBarInfo toolBarInfo;
00139     QValueList<int> iconSizes;
00140     QTimer repaintTimer;
00141 
00142   
00143   bool HiddenDefault;
00144   int IconSizeDefault;
00145   QString IconTextDefault;
00146   bool NewLineDefault;
00147   int OffsetDefault;
00148   QString PositionDefault;
00149 
00150    QPtrList<QWidget> idleButtons;
00151 };
00152 
00153 KToolBarSeparator::KToolBarSeparator(Orientation o , bool l, QToolBar *parent,
00154                                      const char* name )
00155     :QFrame( parent, name ), line( l )
00156 {
00157     connect( parent, SIGNAL(orientationChanged(Orientation)),
00158              this, SLOT(setOrientation(Orientation)) );
00159     setOrientation( o );
00160     setBackgroundMode( parent->backgroundMode() );
00161     setBackgroundOrigin( ParentOrigin );
00162 }
00163 
00164 void KToolBarSeparator::setOrientation( Orientation o )
00165 {
00166     orient = o;
00167     setFrameStyle( NoFrame );
00168 }
00169 
00170 void KToolBarSeparator::drawContents( QPainter* p )
00171 {
00172     if ( line ) {
00173         QStyle::SFlags flags = QStyle::Style_Default;
00174 
00175         if ( orientation() == Horizontal )
00176             flags = flags | QStyle::Style_Horizontal;
00177 
00178         style().drawPrimitive(QStyle::PE_DockWindowSeparator, p,
00179                               contentsRect(), colorGroup(), flags);
00180     } else {
00181         QFrame::drawContents(p);
00182     }
00183 }
00184 
00185 void KToolBarSeparator::styleChange( QStyle& )
00186 {
00187     setOrientation( orient );
00188 }
00189 
00190 QSize KToolBarSeparator::sizeHint() const
00191 {
00192     int dim = style().pixelMetric( QStyle::PM_DockWindowSeparatorExtent, this );
00193     return orientation() == Vertical ? QSize( 0, dim ) : QSize( dim, 0 );
00194 }
00195 
00196 QSizePolicy KToolBarSeparator::sizePolicy() const
00197 {
00198     return QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
00199 }
00200 
00201 KToolBar::KToolBar( QWidget *parent, const char *name, bool honorStyle, bool readConfig )
00202     : QToolBar( QString::fromLatin1( name ),
00203       dynamic_cast<QMainWindow*>(parent),
00204       parent, false,
00205       name ? name : "mainToolBar")
00206 {
00207     init( readConfig, honorStyle );
00208 }
00209 
00210 KToolBar::KToolBar( QMainWindow *parentWindow, QMainWindow::ToolBarDock dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
00211     : QToolBar( QString::fromLatin1( name ),
00212       parentWindow, dock, newLine,
00213       name ? name : "mainToolBar")
00214 {
00215     init( readConfig, honorStyle );
00216 }
00217 
00218 KToolBar::KToolBar( QMainWindow *parentWindow, QWidget *dock, bool newLine, const char *name, bool honorStyle, bool readConfig )
00219     : QToolBar( QString::fromLatin1( name ),
00220       parentWindow, dock, newLine,
00221       name ? name : "mainToolBar")
00222 {
00223     init( readConfig, honorStyle );
00224 }
00225 
00226 KToolBar::~KToolBar()
00227 {
00228     emit toolbarDestroyed();
00229     delete d;
00230 }
00231 
00232 void KToolBar::init( bool readConfig, bool honorStyle )
00233 {
00234     d = new KToolBarPrivate;
00235     
00236     d->IconSizeDefault = iconSize();
00237     setFullSize( true );
00238     d->m_honorStyle = honorStyle;
00239     context = 0;
00240     layoutTimer = new QTimer( this );
00241     connect( layoutTimer, SIGNAL( timeout() ),
00242              this, SLOT( rebuildLayout() ) );
00243     connect( &(d->repaintTimer), SIGNAL( timeout() ),
00244              this, SLOT( slotRepaint() ) );
00245 
00246     if ( kapp ) { 
00247         connect(kapp, SIGNAL(toolbarAppearanceChanged(int)), this, SLOT(slotAppearanceChanged()));
00248         
00249 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00250         kapp->addKipcEventMask(KIPC::IconChanged);
00251 #endif
00252         connect(kapp, SIGNAL(iconChanged(int)), this, SLOT(slotIconChanged(int)));
00253     }
00254 
00255     
00256     if ( readConfig )
00257         slotReadConfig();
00258 
00259     if ( mainWindow() )
00260         connect( mainWindow(), SIGNAL( toolBarPositionChanged( QToolBar * ) ),
00261                  this, SLOT( toolBarPosChanged( QToolBar * ) ) );
00262 
00263     
00264     connect( this, SIGNAL(placeChanged(QDockWindow::Place)), SLOT(rebuildLayout()) );
00265 }
00266 
00267 int KToolBar::insertButton(const QString& icon, int id, bool enabled,
00268                             const QString& text, int index, KInstance *_instance )
00269 {
00270     KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text, _instance );
00271 
00272     insertWidgetInternal( button, index, id );
00273     button->setEnabled( enabled );
00274     doConnections( button );
00275     return index;
00276 }
00277 
00278 
00279 int KToolBar::insertButton(const QString& icon, int id, const char *signal,
00280                             const QObject *receiver, const char *slot,
00281                             bool enabled, const QString& text, int index, KInstance *_instance )
00282 {
00283     KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text, _instance);
00284     insertWidgetInternal( button, index, id );
00285     button->setEnabled( enabled );
00286     connect( button, signal, receiver, slot );
00287     doConnections( button );
00288     return index;
00289 }
00290 
00291 
00292 int KToolBar::insertButton(const QPixmap& pixmap, int id, bool enabled,
00293                             const QString& text, int index )
00294 {
00295     KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text);
00296     insertWidgetInternal( button, index, id );
00297     button->setEnabled( enabled );
00298     doConnections( button );
00299     return index;
00300 }
00301 
00302 
00303 int KToolBar::insertButton(const QPixmap& pixmap, int id, const char *signal,
00304                             const QObject *receiver, const char *slot,
00305                             bool enabled, const QString& text,
00306                             int index )
00307 {
00308     KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text);
00309     insertWidgetInternal( button, index, id );
00310     button->setEnabled( enabled );
00311     connect( button, signal, receiver, slot );
00312     doConnections( button );
00313     return index;
00314 }
00315 
00316 
00317 int KToolBar::insertButton(const QString& icon, int id, QPopupMenu *popup,
00318                             bool enabled, const QString &text, int index )
00319 {
00320     KToolBarButton *button = new KToolBarButton( icon, id, this, 0, text );
00321     insertWidgetInternal( button, index, id );
00322     button->setEnabled( enabled );
00323     button->setPopup( popup );
00324     doConnections( button );
00325     return index;
00326 }
00327 
00328 
00329 int KToolBar::insertButton(const QPixmap& pixmap, int id, QPopupMenu *popup,
00330                             bool enabled, const QString &text, int index )
00331 {
00332     KToolBarButton *button = new KToolBarButton( pixmap, id, this, 0, text );
00333     insertWidgetInternal( button, index, id );
00334     button->setEnabled( enabled );
00335     button->setPopup( popup );
00336     doConnections( button );
00337     return index;
00338 }
00339 
00340 
00341 int KToolBar::insertLined (const QString& text, int id,
00342                             const char *signal,
00343                             const QObject *receiver, const char *slot,
00344                             bool enabled ,
00345                             const QString& toolTipText,
00346                             int size, int index )
00347 {
00348     KLineEdit *lined = new KLineEdit ( this, 0 );
00349     if ( !toolTipText.isEmpty() )
00350         QToolTip::add( lined, toolTipText );
00351     if ( size > 0 )
00352         lined->setMinimumWidth( size );
00353     insertWidgetInternal( lined, index, id );
00354     connect( lined, signal, receiver, slot );
00355     lined->setText(text);
00356     lined->setEnabled( enabled );
00357     return index;
00358 }
00359 
00360 int KToolBar::insertCombo (const QStringList &list, int id, bool writable,
00361                             const char *signal, const QObject *receiver,
00362                             const char *slot, bool enabled,
00363                             const QString& tooltiptext,
00364                             int size, int index,
00365                             QComboBox::Policy policy )
00366 {
00367     KComboBox *combo = new KComboBox ( writable, this );
00368 
00369     insertWidgetInternal( combo, index, id );
00370     combo->insertStringList (list);
00371     combo->setInsertionPolicy(policy);
00372     combo->setEnabled( enabled );
00373     if ( !tooltiptext.isEmpty() )
00374         QToolTip::add( combo, tooltiptext );
00375     if ( size > 0 )
00376         combo->setMinimumWidth( size );
00377     if (!tooltiptext.isNull())
00378         QToolTip::add( combo, tooltiptext );
00379 
00380     if ( signal && receiver && slot )
00381         connect ( combo, signal, receiver, slot );
00382     return index;
00383 }
00384 
00385 
00386 int KToolBar::insertCombo (const QString& text, int id, bool writable,
00387                             const char *signal, QObject *receiver,
00388                             const char *slot, bool enabled,
00389                             const QString& tooltiptext,
00390                             int size, int index,
00391                             QComboBox::Policy policy )
00392 {
00393     KComboBox *combo = new KComboBox ( writable, this );
00394     insertWidgetInternal( combo, index, id );
00395     combo->insertItem (text);
00396     combo->setInsertionPolicy(policy);
00397     combo->setEnabled( enabled );
00398     if ( !tooltiptext.isEmpty() )
00399         QToolTip::add( combo, tooltiptext );
00400     if ( size > 0 )
00401         combo->setMinimumWidth( size );
00402     if (!tooltiptext.isNull())
00403         QToolTip::add( combo, tooltiptext );
00404     connect (combo, signal, receiver, slot);
00405     return index;
00406 }
00407 
00408 int KToolBar::insertSeparator(int index, int id)
00409 {
00410     QWidget *w = new KToolBarSeparator( orientation(), false, this, "tool bar separator" );
00411     insertWidgetInternal( w, index, id );
00412     return index;
00413 }
00414 
00415 int KToolBar::insertLineSeparator(int index, int id)
00416 {
00417     QWidget *w = new KToolBarSeparator( orientation(), true, this, "tool bar separator" );
00418     insertWidgetInternal( w, index, id );
00419     return index;
00420 }
00421 
00422 
00423 int KToolBar::insertWidget(int id, int , QWidget *widget, int index)
00424 {
00425     removeWidgetInternal( widget ); 
00426     insertWidgetInternal( widget, index, id );
00427     return index;
00428 }
00429 
00430 int KToolBar::insertAnimatedWidget(int id, QObject *receiver, const char *slot,
00431                                     const QString& icons, int index )
00432 {
00433     KAnimWidget *anim = new KAnimWidget( icons, d->m_iconSize, this );
00434     insertWidgetInternal( anim, index, id );
00435 
00436     if ( receiver )
00437         connect( anim, SIGNAL(clicked()), receiver, slot);
00438 
00439     return index;
00440 }
00441 
00442 KAnimWidget *KToolBar::animatedWidget( int id )
00443 {
00444     Id2WidgetMap::Iterator it = id2widget.find( id );
00445     if ( it == id2widget.end() )
00446         return 0;
00447     KAnimWidget *aw = dynamic_cast<KAnimWidget *>(*it);
00448     if ( aw )
00449         return aw;
00450     QObjectList *l = queryList( "KAnimWidget" );
00451     if ( !l || !l->first() ) {
00452         delete l;
00453         return 0;
00454     }
00455 
00456     for ( QObject *o = l->first(); o; o = l->next() ) {
00457         KAnimWidget *aw = dynamic_cast<KAnimWidget *>(o);
00458         if ( aw )
00459         {
00460             delete l;
00461             return aw;
00462         }
00463     }
00464 
00465     delete l;
00466     return 0;
00467 }
00468 
00469 
00470 void KToolBar::addConnection (int id, const char *signal,
00471                                const QObject *receiver, const char *slot)
00472 {
00473     Id2WidgetMap::Iterator it = id2widget.find( id );
00474     if ( it == id2widget.end() )
00475         return;
00476     if ( (*it) )
00477         connect( (*it), signal, receiver, slot );
00478 }
00479 
00480 void KToolBar::setItemEnabled( int id, bool enabled )
00481 {
00482     Id2WidgetMap::Iterator it = id2widget.find( id );
00483     if ( it == id2widget.end() )
00484         return;
00485     if ( (*it) )
00486         (*it)->setEnabled( enabled );
00487 }
00488 
00489 
00490 void KToolBar::setButtonPixmap( int id, const QPixmap& _pixmap )
00491 {
00492     Id2WidgetMap::Iterator it = id2widget.find( id );
00493     if ( it == id2widget.end() )
00494         return;
00495     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00496     if ( button )
00497         button->setPixmap( _pixmap );
00498 }
00499 
00500 
00501 void KToolBar::setButtonIcon( int id, const QString& _icon )
00502 {
00503     Id2WidgetMap::Iterator it = id2widget.find( id );
00504     if ( it == id2widget.end() )
00505         return;
00506     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00507     if ( button )
00508         button->setIcon( _icon );
00509 }
00510 
00511 void KToolBar::setButtonIconSet( int id, const QIconSet& iconset )
00512 {
00513     Id2WidgetMap::Iterator it = id2widget.find( id );
00514     if ( it == id2widget.end() )
00515         return;
00516     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00517     if ( button )
00518         button->setIconSet( iconset );
00519 }
00520 
00521 
00522 void KToolBar::setDelayedPopup (int id , QPopupMenu *_popup, bool toggle )
00523 {
00524     Id2WidgetMap::Iterator it = id2widget.find( id );
00525     if ( it == id2widget.end() )
00526         return;
00527     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00528     if ( button )
00529         button->setDelayedPopup( _popup, toggle );
00530 }
00531 
00532 
00533 void KToolBar::setAutoRepeat (int id, bool flag)
00534 {
00535     Id2WidgetMap::Iterator it = id2widget.find( id );
00536     if ( it == id2widget.end() )
00537         return;
00538     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00539     if ( button )
00540         button->setAutoRepeat( flag );
00541 }
00542 
00543 
00544 void KToolBar::setToggle (int id, bool flag )
00545 {
00546     Id2WidgetMap::Iterator it = id2widget.find( id );
00547     if ( it == id2widget.end() )
00548         return;
00549     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00550     if ( button )
00551         button->setToggle( flag );
00552 }
00553 
00554 
00555 void KToolBar::toggleButton (int id)
00556 {
00557     Id2WidgetMap::Iterator it = id2widget.find( id );
00558     if ( it == id2widget.end() )
00559         return;
00560     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00561     if ( button )
00562         button->toggle();
00563 }
00564 
00565 
00566 void KToolBar::setButton (int id, bool flag)
00567 {
00568     Id2WidgetMap::Iterator it = id2widget.find( id );
00569     if ( it == id2widget.end() )
00570         return;
00571     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00572     if ( button )
00573         button->on( flag );
00574 }
00575 
00576 
00577 bool KToolBar::isButtonOn (int id) const
00578 {
00579     Id2WidgetMap::ConstIterator it = id2widget.find( id );
00580     if ( it == id2widget.end() )
00581         return false;
00582     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
00583     return button ? button->isOn() : false;
00584 }
00585 
00586 
00587 void KToolBar::setLinedText (int id, const QString& text)
00588 {
00589     Id2WidgetMap::Iterator it = id2widget.find( id );
00590     if ( it == id2widget.end() )
00591         return;
00592     QLineEdit * lineEdit = dynamic_cast<QLineEdit *>( *it );
00593     if ( lineEdit )
00594         lineEdit->setText( text );
00595 }
00596 
00597 
00598 QString KToolBar::getLinedText (int id) const
00599 {
00600     Id2WidgetMap::ConstIterator it = id2widget.find( id );
00601     if ( it == id2widget.end() )
00602         return QString::null;
00603     QLineEdit * lineEdit = dynamic_cast<QLineEdit *>( *it );
00604     return lineEdit ? lineEdit->text() : QString::null;
00605 }
00606 
00607 
00608 void KToolBar::insertComboItem (int id, const QString& text, int index)
00609 {
00610     Id2WidgetMap::Iterator it = id2widget.find( id );
00611     if ( it == id2widget.end() )
00612         return;
00613     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00614     if (comboBox)
00615         comboBox->insertItem( text, index );
00616 }
00617 
00618 void KToolBar::insertComboList (int id, const QStringList &list, int index)
00619 {
00620     Id2WidgetMap::Iterator it = id2widget.find( id );
00621     if ( it == id2widget.end() )
00622         return;
00623     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00624     if (comboBox)
00625         comboBox->insertStringList( list, index );
00626 }
00627 
00628 
00629 void KToolBar::removeComboItem (int id, int index)
00630 {
00631     Id2WidgetMap::Iterator it = id2widget.find( id );
00632     if ( it == id2widget.end() )
00633         return;
00634     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00635     if (comboBox)
00636         comboBox->removeItem( index );
00637 }
00638 
00639 
00640 void KToolBar::setCurrentComboItem (int id, int index)
00641 {
00642     Id2WidgetMap::Iterator it = id2widget.find( id );
00643     if ( it == id2widget.end() )
00644         return;
00645     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00646     if (comboBox)
00647         comboBox->setCurrentItem( index );
00648 }
00649 
00650 
00651 void KToolBar::changeComboItem  (int id, const QString& text, int index)
00652 {
00653     Id2WidgetMap::Iterator it = id2widget.find( id );
00654     if ( it == id2widget.end() )
00655         return;
00656     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00657     if (comboBox)
00658         comboBox->changeItem( text, index );
00659 }
00660 
00661 
00662 void KToolBar::clearCombo (int id)
00663 {
00664     Id2WidgetMap::Iterator it = id2widget.find( id );
00665     if ( it == id2widget.end() )
00666         return;
00667     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00668     if (comboBox)
00669         comboBox->clear();
00670 }
00671 
00672 
00673 QString KToolBar::getComboItem (int id, int index) const
00674 {
00675     Id2WidgetMap::ConstIterator it = id2widget.find( id );
00676     if ( it == id2widget.end() )
00677         return QString::null;
00678     QComboBox * comboBox = dynamic_cast<QComboBox *>( *it );
00679     return comboBox ? comboBox->text( index ) : QString::null;
00680 }
00681 
00682 
00683 KComboBox * KToolBar::getCombo(int id)
00684 {
00685     Id2WidgetMap::Iterator it = id2widget.find( id );
00686     if ( it == id2widget.end() )
00687         return 0;
00688     return dynamic_cast<KComboBox *>( *it );
00689 }
00690 
00691 
00692 KLineEdit * KToolBar::getLined (int id)
00693 {
00694     Id2WidgetMap::Iterator it = id2widget.find( id );
00695     if ( it == id2widget.end() )
00696         return 0;
00697     return dynamic_cast<KLineEdit *>( *it );
00698 }
00699 
00700 
00701 KToolBarButton * KToolBar::getButton (int id)
00702 {
00703     Id2WidgetMap::Iterator it = id2widget.find( id );
00704     if ( it == id2widget.end() )
00705         return 0;
00706     return dynamic_cast<KToolBarButton *>( *it );
00707 }
00708 
00709 
00710 void KToolBar::alignItemRight (int id, bool right )
00711 {
00712     Id2WidgetMap::Iterator it = id2widget.find( id );
00713     if ( it == id2widget.end() )
00714         return;
00715     if ( rightAligned && !right && (*it) == rightAligned )
00716         rightAligned = 0;
00717     if ( (*it) && right )
00718         rightAligned = (*it);
00719 }
00720 
00721 
00722 QWidget *KToolBar::getWidget (int id)
00723 {
00724     Id2WidgetMap::Iterator it = id2widget.find( id );
00725     return ( it == id2widget.end() ) ? 0 : (*it);
00726 }
00727 
00728 
00729 void KToolBar::setItemAutoSized (int id, bool yes )
00730 {
00731     QWidget *w = getWidget(id);
00732     if ( w && yes )
00733         setStretchableWidget( w );
00734 }
00735 
00736 
00737 void KToolBar::clear ()
00738 {
00739     QToolBar::clear();
00740     widget2id.clear();
00741     id2widget.clear();
00742 }
00743 
00744 
00745 void KToolBar::removeItem(int id)
00746 {
00747     Id2WidgetMap::Iterator it = id2widget.find( id );
00748     if ( it == id2widget.end() )
00749     {
00750         kdDebug(220) << name() << " KToolBar::removeItem item " << id << " not found" << endl;
00751         return;
00752     }
00753     QWidget * w = (*it);
00754     id2widget.remove( id );
00755     widget2id.remove( w );
00756     widgets.removeRef( w );
00757     delete w;
00758 }
00759 
00760 
00761 void KToolBar::removeItemDelayed(int id)
00762 {
00763     Id2WidgetMap::Iterator it = id2widget.find( id );
00764     if ( it == id2widget.end() )
00765     {
00766         kdDebug(220) << name() << " KToolBar::removeItem item " << id << " not found" << endl;
00767         return;
00768     }
00769     QWidget * w = (*it);
00770     id2widget.remove( id );
00771     widget2id.remove( w );
00772     widgets.removeRef( w );
00773 
00774     w->blockSignals(true);
00775     d->idleButtons.append(w);
00776     layoutTimer->start( 50, true );
00777 }
00778 
00779 
00780 void KToolBar::hideItem (int id)
00781 {
00782     QWidget *w = getWidget(id);
00783     if ( w )
00784         w->hide();
00785 }
00786 
00787 
00788 void KToolBar::showItem (int id)
00789 {
00790     QWidget *w = getWidget(id);
00791     if ( w )
00792         w->show();
00793 }
00794 
00795 
00796 int KToolBar::itemIndex (int id)
00797 {
00798     QWidget *w = getWidget(id);
00799     return w ? widgets.findRef(w) : -1;
00800 }
00801 
00802 int KToolBar::idAt (int index)
00803 {
00804     QWidget *w = widgets.at(index);
00805     return widget2id[w];
00806 }
00807 
00808 void KToolBar::setFullSize(bool flag )
00809 {
00810     setHorizontalStretchable( flag );
00811     setVerticalStretchable( flag );
00812 }
00813 
00814 
00815 bool KToolBar::fullSize() const
00816 {
00817     return isHorizontalStretchable() || isVerticalStretchable();
00818 }
00819 
00820 
00821 void KToolBar::enableMoving(bool flag )
00822 {
00823     setMovingEnabled(flag);
00824 }
00825 
00826 
00827 void KToolBar::setBarPos (BarPosition bpos)
00828 {
00829     if ( !mainWindow() )
00830         return;
00831     mainWindow()->moveDockWindow( this, (Dock)bpos );
00832     
00833 }
00834 
00835 
00836 KToolBar::BarPosition KToolBar::barPos() const
00837 {
00838     if ( !this->mainWindow() )
00839         return KToolBar::Top;
00840     Dock dock;
00841     int dm1, dm2;
00842     bool dm3;
00843     this->mainWindow()->getLocation( (QToolBar*)this, dock, dm1, dm3, dm2 );
00844     if ( dock == DockUnmanaged ) {
00845         return (KToolBar::BarPosition)DockTop;
00846     }
00847     return (BarPosition)dock;
00848 }
00849 
00850 
00851 bool KToolBar::enable(BarStatus stat)
00852 {
00853     bool mystat = isVisible();
00854 
00855     if ( (stat == Toggle && mystat) || stat == Hide )
00856         hide();
00857     else
00858         show();
00859 
00860     return isVisible() == mystat;
00861 }
00862 
00863 
00864 void KToolBar::setMaxHeight ( int h )
00865 {
00866     setMaximumHeight( h );
00867 }
00868 
00869 int KToolBar::maxHeight()
00870 {
00871     return maximumHeight();
00872 }
00873 
00874 
00875 void KToolBar::setMaxWidth (int dw)
00876 {
00877     setMaximumWidth( dw );
00878 }
00879 
00880 
00881 int KToolBar::maxWidth()
00882 {
00883     return maximumWidth();
00884 }
00885 
00886 
00887 void KToolBar::setTitle (const QString& _title)
00888 {
00889     setLabel( _title );
00890 }
00891 
00892 
00893 void KToolBar::enableFloating (bool )
00894 {
00895 }
00896 
00897 
00898 void KToolBar::setIconText(IconText it)
00899 {
00900     setIconText( it, true );
00901 }
00902 
00903 
00904 void KToolBar::setIconText(IconText icontext, bool update)
00905 {
00906     bool doUpdate=false;
00907 
00908     if (icontext != d->m_iconText) {
00909         d->m_iconText = icontext;
00910         doUpdate=true;
00911         
00912     }
00913     else {
00914         
00915     }
00916 
00917     if (update == false)
00918         return;
00919 
00920     if (doUpdate)
00921         emit modechange(); 
00922 
00923     
00924     QMainWindow *mw = mainWindow();
00925     if ( mw ) {
00926         mw->setUpdatesEnabled( false );
00927         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00928         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00929         mw->setUpdatesEnabled( true );
00930     }
00931 }
00932 
00933 
00934 KToolBar::IconText KToolBar::iconText() const
00935 {
00936     return d->m_iconText;
00937 }
00938 
00939 
00940 void KToolBar::setIconSize(int size)
00941 {
00942     setIconSize( size, true );
00943 }
00944 
00945 void KToolBar::setIconSize(int size, bool update)
00946 {
00947     bool doUpdate=false;
00948 
00949     if ( size != d->m_iconSize ) {
00950             d->m_iconSize = size;
00951             doUpdate=true;
00952     }
00953 
00954     if (update == false)
00955         return;
00956 
00957     if (doUpdate)
00958         emit modechange(); 
00959 
00960     
00961     if ( mainWindow() ) {
00962         QMainWindow *mw = mainWindow();
00963         mw->setUpdatesEnabled( false );
00964         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00965         mw->setToolBarsMovable( !mw->toolBarsMovable() );
00966         mw->setUpdatesEnabled( true );
00967     }
00968 }
00969 
00970 
00971 int KToolBar::iconSize() const
00972 {
00973     if ( !d->m_iconSize ) 
00974     {
00975          if (!::qstrcmp(QObject::name(), "mainToolBar"))
00976              return KGlobal::iconLoader()->currentSize(KIcon::MainToolbar);
00977          else
00978              return KGlobal::iconLoader()->currentSize(KIcon::Toolbar);
00979     }
00980     return d->m_iconSize;
00981 }
00982 
00983 
00984 void KToolBar::setEnableContextMenu(bool enable )
00985 {
00986     d->m_enableContext = enable;
00987 }
00988 
00989 
00990 bool KToolBar::contextMenuEnabled() const
00991 {
00992     return d->m_enableContext;
00993 }
00994 
00995 
00996 void KToolBar::setItemNoStyle(int id, bool no_style )
00997 {
00998     Id2WidgetMap::Iterator it = id2widget.find( id );
00999     if ( it == id2widget.end() )
01000         return;
01001     KToolBarButton * button = dynamic_cast<KToolBarButton *>( *it );
01002     if (button)
01003         button->setNoStyle( no_style );
01004 }
01005 
01006 
01007 void KToolBar::setFlat (bool flag)
01008 {
01009     if ( !mainWindow() )
01010         return;
01011     if ( flag )
01012         mainWindow()->moveDockWindow( this, DockMinimized );
01013     else
01014         mainWindow()->moveDockWindow( this, DockTop );
01015     
01016     KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
01017     if ( kmw )
01018         kmw->setSettingsDirty();
01019 }
01020 
01021 
01022 int KToolBar::count() const
01023 {
01024     return id2widget.count();
01025 }
01026 
01027 
01028 void KToolBar::saveState()
01029 {
01030     
01031     if ( d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty() ) {
01032         
01033         
01034         QDomElement elem = d->m_xmlguiClient->domDocument().documentElement().toElement();
01035         elem = elem.firstChild().toElement();
01036         QString barname(!::qstrcmp(name(), "unnamed") ? "mainToolBar" : name());
01037         QDomElement current;
01038         
01039         d->modified = false;
01040         for( ; !elem.isNull(); elem = elem.nextSibling().toElement() ) {
01041             current = elem;
01042 
01043             if ( current.tagName().lower() != "toolbar" )
01044                 continue;
01045 
01046             QString curname(current.attribute( "name" ));
01047 
01048             if ( curname == barname ) {
01049                 saveState( current );
01050                 break;
01051             }
01052         }
01053         
01054         if ( !d->modified )
01055             return;
01056 
01057         
01058         QString local_xml(KXMLGUIFactory::readConfigFile(d->m_xmlguiClient->xmlFile(), true, d->m_xmlguiClient->instance()));
01059         QDomDocument local;
01060         local.setContent(local_xml);
01061 
01062         
01063         bool just_append = true;
01064         elem = local.documentElement().toElement();
01065         KXMLGUIFactory::removeDOMComments( elem );
01066         elem = elem.firstChild().toElement();
01067         for( ; !elem.isNull(); elem = elem.nextSibling().toElement() ) {
01068             if ( elem.tagName().lower() != "toolbar" )
01069                 continue;
01070 
01071             QString curname(elem.attribute( "name" ));
01072 
01073             if ( curname == barname ) {
01074                 just_append = false;
01075                 local.documentElement().replaceChild( current, elem );
01076                 break;
01077             }
01078         }
01079 
01080         if (just_append)
01081             local.documentElement().appendChild( current );
01082 
01083         KXMLGUIFactory::saveConfigFile(local, d->m_xmlguiClient->localXMLFile(), d->m_xmlguiClient->instance() );
01084 
01085         return;
01086     }
01087 
01088     
01089     KConfig *config = KGlobal::config();
01090     saveSettings(config, QString::null);
01091     config->sync();
01092 }
01093 
01094 QString KToolBar::settingsGroup() const
01095 {
01096     QString configGroup;
01097     if (!::qstrcmp(name(), "unnamed") || !::qstrcmp(name(), "mainToolBar"))
01098         configGroup = "Toolbar style";
01099     else
01100         configGroup = QString(name()) + " Toolbar style";
01101     if ( this->mainWindow() )
01102     {
01103         configGroup.prepend(" ");
01104         configGroup.prepend( this->mainWindow()->name() );
01105     }
01106     return configGroup;
01107 }
01108 
01109 void KToolBar::saveSettings(KConfig *config, const QString &_configGroup)
01110 {
01111     QString configGroup = _configGroup;
01112     if (configGroup.isEmpty())
01113         configGroup = settingsGroup();
01114     
01115 
01116     QString position, icontext;
01117     int index;
01118     getAttributes( position, icontext, index );
01119 
01120     
01121 
01122     KConfigGroupSaver saver(config, configGroup);
01123 
01124     if(!config->hasDefault("Position") && position == d->PositionDefault )
01125       config->revertToDefault("Position");
01126     else
01127       config->writeEntry("Position", position);
01128 
01129     
01130 
01131     if(!config->hasDefault("IconText") && icontext == d->IconTextDefault )
01132     {
01133       
01134       config->revertToDefault("IconText");
01135     }
01136     else
01137     {
01138       
01139       config->writeEntry("IconText", icontext);
01140     }
01141 
01142     if(!config->hasDefault("IconSize") && iconSize() == d->IconSizeDefault )
01143       config->revertToDefault("IconSize");
01144     else
01145       config->writeEntry("IconSize", iconSize());
01146 
01147     if(!config->hasDefault("Hidden") && isHidden() == d->HiddenDefault )
01148       config->revertToDefault("Hidden");
01149     else
01150       config->writeEntry("Hidden", isHidden());
01151 
01152     
01153     
01154     
01155     
01156     
01157     
01158     
01159     
01160     
01161     
01162     config->writeEntry("Index", index);
01163 
01164     if(!config->hasDefault("Offset") && offset() == d->OffsetDefault )
01165       config->revertToDefault("Offset");
01166     else
01167       config->writeEntry("Offset", offset());
01168 
01169     if(!config->hasDefault("NewLine") && newLine() == d->NewLineDefault )
01170       config->revertToDefault("NewLine");
01171     else
01172       config->writeEntry("NewLine", newLine());
01173 }
01174 
01175 
01176 void KToolBar::setXMLGUIClient( KXMLGUIClient *client )
01177 {
01178     d->m_xmlguiClient = client;
01179 }
01180 
01181 void KToolBar::setText( const QString & txt )
01182 {
01183     setLabel( txt + " (" + kapp->caption() + ") " );
01184 }
01185 
01186 
01187 QString KToolBar::text() const
01188 {
01189     return label();
01190 }
01191 
01192 
01193 void KToolBar::doConnections( KToolBarButton *button )
01194 {
01195     connect(button, SIGNAL(clicked(int)), this, SIGNAL( clicked( int ) ) );
01196     connect(button, SIGNAL(doubleClicked(int)), this, SIGNAL( doubleClicked( int ) ) );
01197     connect(button, SIGNAL(released(int)), this, SIGNAL( released( int ) ) );
01198     connect(button, SIGNAL(pressed(int)), this, SIGNAL( pressed( int ) ) );
01199     connect(button, SIGNAL(toggled(int)), this, SIGNAL( toggled( int ) ) );
01200     connect(button, SIGNAL(highlighted(int, bool)), this, SIGNAL( highlighted( int, bool ) ) );
01201 }
01202 
01203 void KToolBar::mousePressEvent ( QMouseEvent *m )
01204 {
01205     if ( !mainWindow() )
01206         return;
01207     QMainWindow *mw = mainWindow();
01208     if ( mw->toolBarsMovable() && d->m_enableContext ) {
01209         if ( m->button() == RightButton ) {
01210             int i = contextMenu()->exec( m->globalPos(), 0 );
01211             switch ( i ) {
01212             case -1:
01213                 return; 
01214             case CONTEXT_LEFT:
01215                 mw->moveDockWindow( this, DockLeft );
01216                 break;
01217             case CONTEXT_RIGHT:
01218                 mw->moveDockWindow( this, DockRight );
01219                 break;
01220             case CONTEXT_TOP:
01221                 mw->moveDockWindow( this, DockTop );
01222                 break;
01223             case CONTEXT_BOTTOM:
01224                 mw->moveDockWindow( this, DockBottom );
01225                 break;
01226             case CONTEXT_FLOAT:
01227                 break;
01228             case CONTEXT_FLAT:
01229                 mw->moveDockWindow( this, DockMinimized );
01230                 break;
01231             case CONTEXT_ICONS:
01232                 setIconText( IconOnly );
01233                 break;
01234             case CONTEXT_TEXTRIGHT:
01235                 setIconText( IconTextRight );
01236                 break;
01237             case CONTEXT_TEXT:
01238                 setIconText( TextOnly );
01239                 break;
01240             case CONTEXT_TEXTUNDER:
01241                 setIconText( IconTextBottom );
01242                 break;
01243             default:
01244                 if ( i >= CONTEXT_ICONSIZES )
01245                     setIconSize( i - CONTEXT_ICONSIZES );
01246                 else
01247                     return; 
01248             }
01249             KMainWindow *kmw = dynamic_cast<KMainWindow *>(mw);
01250             if ( kmw )
01251                 kmw->setSettingsDirty();
01252         }
01253     }
01254 }
01255 
01256 
01257 void KToolBar::rebuildLayout()
01258 {
01259     for(QWidget *w=d->idleButtons.first(); w; w=d->idleButtons.next())
01260        w->blockSignals(false);
01261     d->idleButtons.clear();
01262 
01263     layoutTimer->stop();
01264     QApplication::sendPostedEvents( this, QEvent::ChildInserted );
01265     QBoxLayout *l = boxLayout();
01266 
01267     
01268     QLayoutIterator it = l->iterator();
01269     while ( it.current() )
01270         it.deleteCurrent();
01271 
01272     for ( QWidget *w = widgets.first(); w; w = widgets.next() ) {
01273         if ( w == rightAligned )
01274             continue;
01275         KToolBarSeparator *ktbs = dynamic_cast<KToolBarSeparator *>(w);
01276         if ( ktbs && !ktbs->showLine() ) {
01277             l->addSpacing( orientation() == Vertical ? w->sizeHint().height() : w->sizeHint().width() );
01278             w->hide();
01279             continue;
01280         }
01281         if ( dynamic_cast<QPopupMenu *>(w) ) 
01282             continue;
01283         l->addWidget( w );
01284         w->show();
01285         if ((orientation() == Horizontal) && dynamic_cast<QLineEdit *>(w)) 
01286             l->addSpacing(2); 
01287     }
01288     if ( rightAligned ) {
01289         l->addStretch();
01290         l->addWidget( rightAligned );
01291         rightAligned->show();
01292     }
01293 
01294     if ( fullSize() ) {
01295         if ( !rightAligned )
01296             l->addStretch();
01297         if ( stretchableWidget )
01298             l->setStretchFactor( stretchableWidget, 10 );
01299     }
01300     l->invalidate();
01301     QApplication::postEvent( this, new QEvent( QEvent::LayoutHint ) );
01302 }
01303 
01304 void KToolBar::childEvent( QChildEvent *e )
01305 {
01306     if ( e->child()->isWidgetType() ) {
01307         QWidget * w = (QWidget*)e->child();
01308         if ( e->type() == QEvent::ChildInserted ) {
01309             if ( !dynamic_cast<QPopupMenu *>(e->child()) && 
01310                  ::qstrcmp( "qt_dockwidget_internal", e->child()->name() ) != 0 ) {
01311 
01312                 
01313                 
01314                 if ( !widget2id.contains( w ) )
01315                 {
01316                     int dummy = -1;
01317                     insertWidgetInternal( w, dummy, -1 );
01318                 }
01319             }
01320         } else {
01321             removeWidgetInternal( w );
01322         }
01323         if ( isVisibleTo( 0 ) )
01324         {
01325             layoutTimer->start( 50, true );
01326             QBoxLayout *l = boxLayout();
01327 
01328             
01329             
01330             QLayoutIterator it = l->iterator();
01331             while ( it.current() )
01332                it.deleteCurrent();
01333         }
01334     }
01335     QToolBar::childEvent( e );
01336 }
01337 
01338 void KToolBar::insertWidgetInternal( QWidget *w, int &index, int id )
01339 {
01340     
01341     
01342 
01343     connect( w, SIGNAL( destroyed() ),
01344              this, SLOT( widgetDestroyed() ) );
01345     if ( index == -1 || index > (int)widgets.count() ) {
01346         index = (int)widgets.count();
01347         widgets.append( w );
01348     }
01349     else
01350         widgets.insert( index, w );
01351     if ( id == -1 )
01352         id = id2widget.count();
01353     id2widget.insert( id, w );
01354     widget2id.insert( w, id );
01355 }
01356 
01357 void KToolBar::showEvent( QShowEvent *e )
01358 {
01359     QToolBar::showEvent( e );
01360     rebuildLayout();
01361 }
01362 
01363 void KToolBar::setStretchableWidget( QWidget *w )
01364 {
01365     QToolBar::setStretchableWidget( w );
01366     stretchableWidget = w;
01367 }
01368 
01369 QSizePolicy KToolBar::sizePolicy() const
01370 {
01371     if ( orientation() == Horizontal )
01372         return QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
01373     else
01374         return QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Expanding );
01375 }
01376 
01377 QSize KToolBar::sizeHint() const
01378 {
01379     QSize minSize(0,0);
01380     KToolBar *ncThis = const_cast<KToolBar *>(this);
01381 
01382     ncThis->polish();
01383 
01384     int margin = static_cast<QWidget*>(ncThis)->layout()->margin() + frameWidth();
01385     switch( barPos() )
01386     {
01387      case KToolBar::Top:
01388      case KToolBar::Bottom:
01389        for ( QWidget *w = ncThis->widgets.first(); w; w = ncThis->widgets.next() )
01390        {
01391           QSize sh = w->sizeHint();
01392           if ( w->sizePolicy().horData() == QSizePolicy::Ignored )
01393              sh.setWidth( 1 );
01394           if ( w->sizePolicy().verData() == QSizePolicy::Ignored )
01395              sh.setHeight( 1 );
01396           sh = sh.boundedTo( w->maximumSize() )
01397                  .expandedTo( w->minimumSize() ).expandedTo( QSize(1, 1) );
01398 
01399           minSize = minSize.expandedTo(QSize(0, sh.height()));
01400           minSize += QSize(sh.width()+1, 0);
01401           if (dynamic_cast<QLineEdit *>(w)) 
01402              minSize += QSize(2, 0); 
01403        }
01404 
01405        minSize += QSize(QApplication::style().pixelMetric( QStyle::PM_DockWindowHandleExtent ), 0);
01406        minSize += QSize(margin*2, margin*2);
01407        break;
01408 
01409      case KToolBar::Left:
01410      case KToolBar::Right:
01411        for ( QWidget *w = ncThis->widgets.first(); w; w = ncThis->widgets.next() )
01412        {
01413           QSize sh = w->sizeHint();
01414           if ( w->sizePolicy().horData() == QSizePolicy::Ignored )
01415              sh.setWidth( 1 );
01416           if ( w->sizePolicy().verData() == QSizePolicy::Ignored )
01417              sh.setHeight( 1 );
01418           sh = sh.boundedTo( w->maximumSize() )
01419                  .expandedTo( w->minimumSize() ).expandedTo( QSize(1, 1) );
01420 
01421           minSize = minSize.expandedTo(QSize(sh.width(), 0));
01422           minSize += QSize(0, sh.height()+1);
01423        }
01424        minSize += QSize(0, QApplication::style().pixelMetric( QStyle::PM_DockWindowHandleExtent ));
01425        minSize += QSize(margin*2, margin*2);
01426        break;
01427 
01428      default:
01429        minSize = QToolBar::sizeHint();
01430        break;
01431     }
01432     return minSize;
01433 }
01434 
01435 QSize KToolBar::minimumSize() const
01436 {
01437     return minimumSizeHint();
01438 }
01439 
01440 QSize KToolBar::minimumSizeHint() const
01441 {
01442     return sizeHint();
01443 }
01444 
01445 bool KToolBar::highlight() const
01446 {
01447     return d->m_highlight;
01448 }
01449 
01450 void KToolBar::hide()
01451 {
01452     QToolBar::hide();
01453 }
01454 
01455 void KToolBar::show()
01456 {
01457     QToolBar::show();
01458 }
01459 
01460 void KToolBar::resizeEvent( QResizeEvent *e )
01461 {
01462     bool b = isUpdatesEnabled();
01463     setUpdatesEnabled( false );
01464     QToolBar::resizeEvent( e );
01465     if (b)
01466        d->repaintTimer.start( 100, true );
01467 }
01468 
01469 void KToolBar::slotIconChanged(int group)
01470 {
01471     if ((group != KIcon::Toolbar) && (group != KIcon::MainToolbar))
01472         return;
01473     if ((group == KIcon::MainToolbar) != !::qstrcmp(name(), "mainToolBar"))
01474         return;
01475 
01476     emit modechange();
01477     if (isVisible())
01478         updateGeometry();
01479 }
01480 
01481 void KToolBar::slotReadConfig()
01482 {
01483     
01484     
01485     
01486     
01487     applyAppearanceSettings(KGlobal::config(), QString::null );
01488 }
01489 
01490 void KToolBar::slotAppearanceChanged()
01491 {
01492     
01493     applyAppearanceSettings(KGlobal::config(), QString::null, true  );
01494 
01495     
01496     KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
01497     if ( kmw )
01498         kmw->setSettingsDirty();
01499 }
01500 
01501 
01502 bool KToolBar::highlightSetting()
01503 {
01504     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01505     KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01506     return KGlobal::config()->readBoolEntry(QString::fromLatin1("Highlighting"),true);
01507 }
01508 
01509 
01510 bool KToolBar::transparentSetting()
01511 {
01512     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01513     KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01514     return KGlobal::config()->readBoolEntry(QString::fromLatin1("TransparentMoving"),true);
01515 }
01516 
01517 
01518 KToolBar::IconText KToolBar::iconTextSetting()
01519 {
01520     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01521     KConfigGroupSaver saver(KGlobal::config(), grpToolbar);
01522     QString icontext = KGlobal::config()->readEntry(QString::fromLatin1("IconText"),QString::fromLatin1("IconOnly"));
01523     if ( icontext == "IconTextRight" )
01524         return IconTextRight;
01525     else if ( icontext == "IconTextBottom" )
01526         return IconTextBottom;
01527     else if ( icontext == "TextOnly" )
01528         return TextOnly;
01529     else
01530         return IconOnly;
01531 }
01532 
01533 void KToolBar::applyAppearanceSettings(KConfig *config, const QString &_configGroup, bool forceGlobal)
01534 {
01535     QString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
01536     
01537 
01538     
01539     
01540     
01541     
01542     
01543     bool xmlgui = d->m_xmlguiClient && !d->m_xmlguiClient->xmlFile().isEmpty();
01544 
01545     KConfig *gconfig = KGlobal::config();
01546 
01547     static const QString &attrIconText  = KGlobal::staticQString("IconText");
01548     static const QString &attrHighlight = KGlobal::staticQString("Highlighting");
01549     static const QString &attrTrans     = KGlobal::staticQString("TransparentMoving");
01550     static const QString &attrIconSize  = KGlobal::staticQString("IconSize");
01551 
01552     
01553     
01554     
01555     bool highlight;
01556     int transparent;
01557     bool applyIconText = !xmlgui; 
01558     bool applyIconSize = !xmlgui;
01559 
01560     int iconSize = d->IconSizeDefault;
01561     QString iconText = d->IconTextDefault;
01562 
01563     
01564     QString grpToolbar(QString::fromLatin1("Toolbar style"));
01565     { 
01566         KConfigGroupSaver saver(gconfig, grpToolbar);
01567 
01568         
01569         highlight   = gconfig->readBoolEntry(attrHighlight, true);
01570         transparent = gconfig->readBoolEntry(attrTrans, true);
01571 
01572         
01573         
01574         if (d->m_honorStyle)
01575             d->IconTextDefault = gconfig->readEntry(attrIconText, d->IconTextDefault);
01576         else
01577             d->IconTextDefault = "IconOnly";
01578 
01579         
01580         d->IconSizeDefault = gconfig->readNumEntry(attrIconSize, d->IconSizeDefault);
01581 
01582         iconSize = d->IconSizeDefault;
01583         iconText = d->IconTextDefault;
01584     
01585         if ( !forceGlobal && config->hasGroup(configGroup) )
01586         {
01587             config->setGroup(configGroup);
01588 
01589             
01590             highlight   = config->readBoolEntry(attrHighlight, highlight);
01591             transparent = config->readBoolEntry(attrTrans, transparent);
01592 
01593             
01594             if ( config->hasKey( attrIconText ) ) {
01595                 iconText = config->readEntry(attrIconText);
01596                 applyIconText = true;
01597                 
01598             }
01599 
01600             
01601             if ( config->hasKey( attrIconSize ) ) {
01602                 iconSize = config->readNumEntry(attrIconSize);
01603                 applyIconSize = true;
01604             }
01605         }
01606 
01607         
01608     } 
01609 
01610     bool doUpdate = false;
01611 
01612     IconText icon_text;
01613     if ( iconText == "IconTextRight" )
01614         icon_text = IconTextRight;
01615     else if ( iconText == "IconTextBottom" )
01616         icon_text = IconTextBottom;
01617     else if ( iconText == "TextOnly" )
01618         icon_text = TextOnly;
01619     else
01620         icon_text = IconOnly;
01621 
01622     
01623     if (icon_text != d->m_iconText && applyIconText) {
01624         
01625         setIconText(icon_text, false);
01626         doUpdate = true;
01627     }
01628 
01629     
01630     if (iconSize != d->m_iconSize && applyIconSize) {
01631         setIconSize(iconSize, false);
01632         doUpdate = true;
01633     }
01634 
01635     QMainWindow *mw = mainWindow();
01636 
01637     
01638     if ( highlight != d->m_highlight ) {
01639         d->m_highlight = highlight;
01640         doUpdate = true;
01641     }
01642 
01643     
01644     if ( mw && transparent != (!mw->opaqueMoving()) ) {
01645         mw->setOpaqueMoving( !transparent );
01646     }
01647 
01648     if (doUpdate)
01649         emit modechange(); 
01650     if (isVisible ())
01651         updateGeometry();
01652 }
01653 
01654 void KToolBar::applySettings(KConfig *config, const QString &_configGroup)
01655 {
01656     
01657 
01658     QString configGroup = _configGroup.isEmpty() ? settingsGroup() : _configGroup;
01659 
01660     
01661 
01662 
01663 
01664 
01665 
01666 
01667 
01668 
01669 
01670 
01671 
01672 
01673 
01674     
01675     applyAppearanceSettings( config, configGroup );
01676 
01677     
01678     if ( config->hasGroup(configGroup) )
01679     {
01680         KConfigGroupSaver cgs(config, configGroup);
01681 
01682         static const QString &attrPosition  = KGlobal::staticQString("Position");
01683         static const QString &attrIndex  = KGlobal::staticQString("Index");
01684         static const QString &attrOffset  = KGlobal::staticQString("Offset");
01685         static const QString &attrNewLine  = KGlobal::staticQString("NewLine");
01686         static const QString &attrHidden  = KGlobal::staticQString("Hidden");
01687 
01688         QString position = config->readEntry(attrPosition, d->PositionDefault);
01689         int index = config->readNumEntry(attrIndex, -1);
01690         int offset = config->readNumEntry(attrOffset, d->OffsetDefault);
01691         bool newLine = config->readBoolEntry(attrNewLine, d->NewLineDefault);
01692         bool hidden = config->readBoolEntry(attrHidden, d->HiddenDefault);
01693 
01694         Dock pos(DockTop);
01695         if ( position == "Top" )
01696             pos = DockTop;
01697         else if ( position == "Bottom" )
01698             pos = DockBottom;
01699         else if ( position == "Left" )
01700             pos = DockLeft;
01701         else if ( position == "Right" )
01702             pos = DockRight;
01703         else if ( position == "Floating" )
01704             pos = DockTornOff;
01705         else if ( position == "Flat" )
01706             pos = DockMinimized;
01707 
01708         
01709         if (hidden)
01710             hide();
01711         else
01712             show();
01713 
01714         if ( mainWindow() )
01715         {
01716             
01717             d->toolBarInfo = KToolBarPrivate::ToolBarInfo( pos, index, newLine, offset );
01718             positionYourself( true );
01719         }
01720         if (isVisible ())
01721             updateGeometry();
01722     }
01723 }
01724 
01725 bool KToolBar::event( QEvent *e )
01726 {
01727     if ( (e->type() == QEvent::LayoutHint) && isUpdatesEnabled() )
01728        d->repaintTimer.start( 100, true );
01729 
01730     if (e->type() == QEvent::ChildInserted )
01731     {
01732        
01733        
01734        
01735        childEvent((QChildEvent *)e);
01736        return true;
01737     }
01738 
01739     return QToolBar::event( e );
01740 }
01741 
01742 void KToolBar::slotRepaint()
01743 {
01744     setUpdatesEnabled( false );
01745     
01746     
01747     
01748     QResizeEvent ev(size(), size());
01749     resizeEvent(&ev);
01750     QApplication::sendPostedEvents( this, QEvent::LayoutHint );
01751     setUpdatesEnabled( true );
01752     repaint( true );
01753 }
01754 
01755 void KToolBar::toolBarPosChanged( QToolBar *tb )
01756 {
01757     if ( tb != this )
01758         return;
01759     if ( d->oldPos == DockMinimized )
01760         rebuildLayout();
01761     d->oldPos = (QMainWindow::ToolBarDock)barPos();
01762     KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
01763     if ( kmw )
01764         kmw->setSettingsDirty();
01765 }
01766 
01767 void KToolBar::loadState( const QDomElement &element )
01768 {
01769     
01770     QMainWindow *mw = mainWindow();
01771 
01772     if ( !mw )
01773         return;
01774 
01775     {
01776         QCString text = element.namedItem( "text" ).toElement().text().utf8();
01777         if ( text.isEmpty() )
01778             text = element.namedItem( "Text" ).toElement().text().utf8();
01779         if ( !text.isEmpty() )
01780             setText( i18n( text ) );
01781     }
01782 
01783     {
01784         QCString attrFullWidth = element.attribute( "fullWidth" ).lower().latin1();
01785         if ( !attrFullWidth.isEmpty() )
01786             setFullSize( attrFullWidth == "true" );
01787     }
01788 
01789     Dock dock = DockTop;
01790     {
01791         QCString attrPosition = element.attribute( "position" ).lower().latin1();
01792         
01793         if ( !attrPosition.isEmpty() ) {
01794             if ( attrPosition == "top" )
01795                 dock = DockTop;
01796             else if ( attrPosition == "left" )
01797                 dock = DockLeft;
01798             else if ( attrPosition == "right" )
01799                 dock = DockRight;
01800             else if ( attrPosition == "bottom" )
01801                 dock = DockBottom;
01802             else if ( attrPosition == "floating" )
01803                 dock = DockTornOff;
01804             else if ( attrPosition == "flat" )
01805                 dock = DockMinimized;
01806         }
01807     }
01808 
01809     {
01810         QCString attrIconText = element.attribute( "iconText" ).lower().latin1();
01811         if ( !attrIconText.isEmpty() ) {
01812             
01813             if ( attrIconText == "icontextright" )
01814                 setIconText( KToolBar::IconTextRight );
01815             else if ( attrIconText == "textonly" )
01816                 setIconText( KToolBar::TextOnly );
01817             else if ( attrIconText == "icontextbottom" )
01818                 setIconText( KToolBar::IconTextBottom );
01819             else if ( attrIconText == "icononly" )
01820                 setIconText( KToolBar::IconOnly );
01821         } else
01822     {
01823         
01824             
01825             setIconText( iconTextSetting() );
01826     }
01827     }
01828 
01829     {
01830         QString attrIconSize = element.attribute( "iconSize" ).lower();
01831         if ( !attrIconSize.isEmpty() )
01832             d->IconSizeDefault = attrIconSize.toInt();
01833         setIconSize( d->IconSizeDefault );
01834     }
01835 
01836     int index = -1; 
01837     
01838     {
01839         QString attrIndex = element.attribute( "index" ).lower();
01840         if ( !attrIndex.isEmpty() )
01841             index = attrIndex.toInt();
01842     }
01843 
01844     {
01845         QString attrOffset = element.attribute( "offset" ).lower();
01846         if ( !attrOffset.isEmpty() )
01847             d->OffsetDefault = attrOffset.toInt();
01848     }
01849 
01850     {
01851         QString attrNewLine = element.attribute( "newline" ).lower();
01852         if ( !attrNewLine.isEmpty() )
01853             d->NewLineDefault = attrNewLine == "true";
01854     }
01855 
01856     {
01857         QString attrHidden = element.attribute( "hidden" ).lower();
01858         if ( !attrHidden.isEmpty() )
01859             d->HiddenDefault = attrHidden  == "true";
01860     }
01861 
01862     d->toolBarInfo = KToolBarPrivate::ToolBarInfo( dock, index, d->NewLineDefault, d->OffsetDefault );
01863     mw->addDockWindow( this, dock, d->NewLineDefault );
01864     mw->moveDockWindow( this, dock, d->NewLineDefault, index, d->OffsetDefault );
01865 
01866     
01867     d->m_highlight = highlightSetting();
01868 
01869     
01870     
01871     if ( transparentSetting() != !mw->opaqueMoving() )
01872         mw->setOpaqueMoving( !transparentSetting() );
01873 
01874     if ( d->HiddenDefault )
01875         hide();
01876     else
01877         show();
01878 
01879     getAttributes( d->PositionDefault, d->IconTextDefault, index );
01880     
01881 }
01882 
01883 int KToolBar::dockWindowIndex()
01884 {
01885     int index = 0;
01886     Q_ASSERT( mainWindow() );
01887     if ( mainWindow() ) {
01888         QMainWindow::ToolBarDock dock;
01889         bool newLine;
01890         int offset;
01891         mainWindow()->getLocation( this, dock, index, newLine, offset );
01892     }
01893     return index;
01894 }
01895 
01896 void KToolBar::getAttributes( QString &position, QString &icontext, int &index )
01897 {
01898     
01899     switch ( barPos() ) {
01900     case KToolBar::Flat:
01901         position = "Flat";
01902         break;
01903     case KToolBar::Bottom:
01904         position = "Bottom";
01905         break;
01906     case KToolBar::Left:
01907         position = "Left";
01908         break;
01909     case KToolBar::Right:
01910         position = "Right";
01911         break;
01912     case KToolBar::Floating:
01913         position = "Floating";
01914         break;
01915     case KToolBar::Top:
01916     default:
01917         position = "Top";
01918         break;
01919     }
01920 
01921     index = dockWindowIndex();
01922 
01923     switch (d->m_iconText) {
01924     case KToolBar::IconTextRight:
01925         icontext = "IconTextRight";
01926         break;
01927     case KToolBar::IconTextBottom:
01928         icontext = "IconTextBottom";
01929         break;
01930     case KToolBar::TextOnly:
01931         icontext = "TextOnly";
01932         break;
01933     case KToolBar::IconOnly:
01934     default:
01935         icontext = "IconOnly";
01936         break;
01937     }
01938     
01939 }
01940 
01941 void KToolBar::saveState( QDomElement ¤t )
01942 {
01943     Q_ASSERT( !current.isNull() );
01944     QString position, icontext;
01945     int index = -1;
01946     getAttributes( position, icontext, index );
01947 
01948     current.setAttribute( "noMerge", "1" );
01949     current.setAttribute( "position", position );
01950     current.setAttribute( "iconText", icontext );
01951     current.setAttribute( "index", index );
01952     current.setAttribute( "offset", offset() );
01953     current.setAttribute( "newline", newLine() );
01954     if ( isHidden() )
01955         current.setAttribute( "hidden", "true" );
01956     d->modified = true;
01957     
01958 }
01959 
01960 
01961 void KToolBar::positionYourself( bool force )
01962 {
01963     if (force)
01964         d->positioned = false;
01965 
01966     if ( d->positioned || !mainWindow() )
01967     {
01968         
01969         return;
01970     }
01971     
01972     
01973     bool doHide = isHidden();
01974     
01975     mainWindow()->moveDockWindow( this, d->toolBarInfo.dock,
01976                                   d->toolBarInfo.newline,
01977                                   d->toolBarInfo.index,
01978                                   d->toolBarInfo.offset );
01979 
01980     
01981     if ( doHide )
01982         hide();
01983     
01984     d->positioned = true;
01985 }
01986 
01987 KPopupMenu *KToolBar::contextMenu()
01988 {
01989   if ( context )
01990     return context;
01991 
01992   
01993   
01994   context = new KPopupMenu( this, "qt_dockwidget_internal" );
01995   context->insertTitle(i18n("Toolbar Menu"));
01996 
01997   KPopupMenu *orient = new KPopupMenu( context, "orient" );
01998   orient->insertItem( i18n("toolbar position string","Top"),  CONTEXT_TOP );
01999   orient->insertItem( i18n("toolbar position string","Left"), CONTEXT_LEFT );
02000   orient->insertItem( i18n("toolbar position string","Right"), CONTEXT_RIGHT );
02001   orient->insertItem( i18n("toolbar position string","Bottom"), CONTEXT_BOTTOM );
02002   orient->insertSeparator(-1);
02003   
02004   orient->insertItem( i18n("min toolbar", "Flat"), CONTEXT_FLAT );
02005 
02006   KPopupMenu *mode = new KPopupMenu( context, "mode" );
02007   mode->insertItem( i18n("Icons Only"), CONTEXT_ICONS );
02008   mode->insertItem( i18n("Text Only"), CONTEXT_TEXT );
02009   mode->insertItem( i18n("Text Alongside Icons"), CONTEXT_TEXTRIGHT );
02010   mode->insertItem( i18n("Text Under Icons"), CONTEXT_TEXTUNDER );
02011 
02012   KPopupMenu *size = new KPopupMenu( context, "size" );
02013   size->insertItem( i18n("Default"), CONTEXT_ICONSIZES );
02014   
02015   KIconTheme *theme = KGlobal::instance()->iconLoader()->theme();
02016   QValueList<int> avSizes;
02017   if (!::qstrcmp(QObject::name(), "mainToolBar"))
02018       avSizes = theme->querySizes( KIcon::MainToolbar);
02019   else
02020       avSizes = theme->querySizes( KIcon::Toolbar);
02021 
02022   d->iconSizes = avSizes;
02023 
02024   QValueList<int>::Iterator it;
02025   for (it=avSizes.begin(); it!=avSizes.end(); it++) {
02026       QString text;
02027       if ( *it < 19 )
02028           text = i18n("Small (%1x%2)").arg(*it).arg(*it);
02029       else if (*it < 25)
02030           text = i18n("Medium (%1x%2)").arg(*it).arg(*it);
02031       else
02032           text = i18n("Large (%1x%2)").arg(*it).arg(*it);
02033       
02034       size->insertItem( text, CONTEXT_ICONSIZES + *it );
02035   }
02036 
02037   context->insertItem( i18n("Orientation"), orient );
02038   orient->setItemChecked(CONTEXT_TOP, true);
02039   context->insertItem( i18n("Text Position"), mode );
02040   context->setItemChecked(CONTEXT_ICONS, true);
02041   context->insertItem( i18n("Icon Size"), size );
02042 
02043   KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
02044   if ( kmw )
02045   {
02046       if ( kmw->toolBarMenuAction() && kmw->hasMenuBar() )
02047           kmw->toolBarMenuAction()->plug(context);
02048   }
02049 
02050   connect( context, SIGNAL( aboutToShow() ), this, SLOT( slotContextAboutToShow() ) );
02051   connect( context, SIGNAL( aboutToHide() ), this, SLOT( slotContextAboutToHide() ) );
02052   return context;
02053 }
02054 
02055 void KToolBar::slotContextAboutToShow()
02056 {
02057   if (!d->m_configurePlugged)
02058   {
02059     
02060     KXMLGUIClient *xmlGuiClient = d->m_xmlguiClient;
02061 
02062     KMainWindow *kmw = dynamic_cast<KMainWindow *>(mainWindow());
02063     if ( !xmlGuiClient && kmw )
02064       xmlGuiClient = kmw;
02065     if ( xmlGuiClient )
02066     {
02067         KAction *configureAction = xmlGuiClient->actionCollection()->action(KStdAction::stdName(KStdAction::ConfigureToolbars));
02068         if ( configureAction )
02069         {
02070           configureAction->plug(context);
02071           d->m_configurePlugged = true;
02072         }
02073     }
02074   }
02075 
02076   for(int i = CONTEXT_ICONS; i <= CONTEXT_TEXTUNDER; ++i)
02077     context->setItemChecked(i, false);
02078 
02079   switch( d->m_iconText )
02080   {
02081         case IconOnly:
02082         default:
02083             context->setItemChecked(CONTEXT_ICONS, true);
02084             break;
02085         case IconTextRight:
02086             context->setItemChecked(CONTEXT_TEXTRIGHT, true);
02087             break;
02088         case TextOnly:
02089             context->setItemChecked(CONTEXT_TEXT, true);
02090             break;
02091         case IconTextBottom:
02092             context->setItemChecked(CONTEXT_TEXTUNDER, true);
02093             break;
02094   }
02095 
02096   QValueList<int>::ConstIterator iIt = d->iconSizes.begin();
02097   QValueList<int>::ConstIterator iEnd = d->iconSizes.end();
02098   for (; iIt != iEnd; ++iIt )
02099       context->setItemChecked( CONTEXT_ICONSIZES + *iIt, false );
02100 
02101   context->setItemChecked( CONTEXT_ICONSIZES, false );
02102 
02103   context->setItemChecked( CONTEXT_ICONSIZES + d->m_iconSize, true );
02104 
02105   for ( int i = CONTEXT_TOP; i <= CONTEXT_FLAT; ++i )
02106       context->setItemChecked( i, false );
02107 
02108   switch ( barPos() )
02109   {
02110   case KToolBar::Flat:
02111       context->setItemChecked( CONTEXT_FLAT, true );
02112       break;
02113   case KToolBar::Bottom:
02114       context->setItemChecked( CONTEXT_BOTTOM, true );
02115       break;
02116   case KToolBar::Left:
02117       context->setItemChecked( CONTEXT_LEFT, true );
02118       break;
02119   case KToolBar::Right:
02120       context->setItemChecked( CONTEXT_RIGHT, true );
02121       break;
02122   case KToolBar::Floating:
02123       context->setItemChecked( CONTEXT_FLOAT, true );
02124       break;
02125   case KToolBar::Top:
02126       context->setItemChecked( CONTEXT_TOP, true );
02127       break;
02128   default: break;
02129   }
02130 }
02131 
02132 void KToolBar::slotContextAboutToHide()
02133 {
02134   QPtrListIterator<QWidget> it( widgets );
02135   QWidget *wdg;
02136   while ( ( wdg = it.current() ) != 0 ) {
02137     if ( wdg->inherits( "QToolButton" ) )
02138       static_cast<QToolButton*>( wdg )->setDown( false );
02139     ++it;
02140   }
02141 }
02142 
02143 void KToolBar::widgetDestroyed()
02144 {
02145     removeWidgetInternal( (QWidget*)sender() );
02146 }
02147 
02148 void KToolBar::removeWidgetInternal( QWidget * w )
02149 {
02150     widgets.removeRef( w );
02151     QMap< QWidget*, int >::Iterator it = widget2id.find( w );
02152     if ( it == widget2id.end() )
02153         return;
02154     id2widget.remove( *it );
02155     widget2id.remove( it );
02156 }
02157 
02158 void KToolBar::virtual_hook( int, void* )
02159 {  }
02160 
02161 #include "ktoolbar.moc"
02162