27 #include "tdetoolbarbutton.h"
28 #include "tdetoolbar.h"
33 #include <tqdrawutil.h>
34 #include <tqtooltip.h>
36 #include <tqpopupmenu.h>
38 #include <tqpainter.h>
41 #include <tdeapplication.h>
43 #include <tdeglobal.h>
44 #include <tdeglobalsettings.h>
45 #include <kiconeffect.h>
46 #include <kiconloader.h>
49 #include <tdemainwindow.h>
51 template class TQIntDict<TDEToolBarButton>;
53 class TDEToolBarButtonPrivate
56 TDEToolBarButtonPrivate();
57 ~TDEToolBarButtonPrivate();
71 TDEToolBar::IconText m_iconText;
75 TQPoint m_mousePressPos;
80 TDEToolBarButtonPrivate::TDEToolBarButtonPrivate()
85 m_isSeparator =
false;
91 m_iconName = TQString::null;
92 m_iconText = TDEToolBar::IconOnly;
99 TDEToolBarButtonPrivate::~TDEToolBarButtonPrivate()
106 : TQToolButton( _parent , _name)
108 d =
new TDEToolBarButtonPrivate();
112 d->m_isSeparator =
true;
116 TQWidget *_parent,
const char *_name,
118 : TQToolButton( _parent, _name ), d( 0 )
120 d =
new TDEToolBarButtonPrivate();
123 TQToolButton::setTextLabel(_txt);
124 d->m_instance = _instance;
126 d->m_parent =
dynamic_cast<TDEToolBar*
>(_parent);
128 connect(d->m_parent, TQ_SIGNAL( modechange() ),
132 setFocusPolicy( TQWidget::NoFocus );
135 connect(
this, TQ_SIGNAL(
clicked() ),
136 this, TQ_SLOT( slotClicked() ) );
137 connect(
this, TQ_SIGNAL( pressed() ),
138 this, TQ_SLOT( slotPressed() ) );
139 connect(
this, TQ_SIGNAL( released() ),
140 this, TQ_SLOT( slotReleased() ) );
141 installEventFilter(
this);
143 d->m_iconName = _icon;
150 TQWidget *_parent,
const char *name,
152 : TQToolButton( _parent, name ), d( 0 )
154 d =
new TDEToolBarButtonPrivate();
157 TQToolButton::setTextLabel(txt);
159 d->m_parent =
dynamic_cast<TDEToolBar*
>(_parent);
161 connect(d->m_parent, TQ_SIGNAL( modechange() ),
165 setFocusPolicy( TQWidget::NoFocus );
168 connect(
this, TQ_SIGNAL(
clicked() ),
169 this, TQ_SLOT( slotClicked() ));
170 connect(
this, TQ_SIGNAL( pressed() ),
171 this, TQ_SLOT( slotPressed() ));
172 connect(
this, TQ_SIGNAL( released() ),
173 this, TQ_SLOT( slotReleased() ));
174 installEventFilter(
this);
192 d->m_highlight = d->m_parent->highlight();
193 d->m_iconText = d->m_parent->iconText();
195 d->m_iconSize = d->m_parent->iconSize();
197 if (!d->m_iconName.isNull())
201 int pix_width = d->m_iconSize;
202 if ( d->m_iconSize == 0 ) {
203 if (d->m_parent && !strcmp(d->m_parent->name(),
"mainToolBar"))
208 int pix_height = pix_width;
213 TQToolTip::remove(
this);
214 if (d->m_iconText != TDEToolBar::IconOnly)
220 TQFontMetrics fm(tmp_font);
222 text_height = fm.lineSpacing();
223 text_width = fm.width(textLabel());
229 TQToolTip::add(
this, textLabel());
232 switch (d->m_iconText)
234 case TDEToolBar::IconOnly:
235 mysize = TQSize(pix_width, pix_height);
238 case TDEToolBar::IconTextRight:
239 mysize = TQSize(pix_width + text_width + 4, pix_height);
242 case TDEToolBar::TextOnly:
243 mysize = TQSize(text_width + 4, text_height);
246 case TDEToolBar::IconTextBottom:
247 mysize = TQSize((text_width + 4 > pix_width) ? text_width + 4 : pix_width, pix_height + text_height);
254 mysize = style().sizeFromContents(TQStyle::CT_ToolButton,
this, mysize).
255 expandedTo(TQApplication::globalStrut());
258 if (mysize.height() > mysize.width())
259 mysize.setWidth(mysize.height());
265 void TDEToolBarButton::setTextLabel(
const TQString& text,
bool tipToo)
271 if (txt.endsWith(TQString::fromLatin1(
"...")))
272 txt.truncate(txt.length() - 3);
274 TQToolButton::setTextLabel(txt, tipToo);
280 setTextLabel(text,
true);
286 d->m_iconName = icon;
288 d->m_iconSize = d->m_parent->iconSize();
290 if (d->m_parent && !strcmp(d->m_parent->name(),
"mainToolBar"))
291 TQToolButton::setIconSet( d->m_instance->iconLoader()->loadIconSet(
294 TQToolButton::setIconSet( d->m_instance->iconLoader()->loadIconSet(
300 TQToolButton::setIconSet( iconset );
308 TQToolButton::setPixmap( pixmap );
311 TQIconSet set = iconSet();
312 set.setPixmap( pixmap, TQIconSet::Automatic, TQIconSet::Active );
313 TQToolButton::setIconSet( set );
318 TQIconSet set = iconSet();
319 set.setPixmap( pixmap, TQIconSet::Automatic, TQIconSet::Normal );
320 TQToolButton::setIconSet( set );
325 TQIconSet set = iconSet();
326 set.setPixmap( pixmap, TQIconSet::Automatic, TQIconSet::Disabled );
327 TQToolButton::setIconSet( set );
332 TQIconSet set = iconSet();
334 if (d->m_parent && !strcmp(d->m_parent->name(),
"mainToolBar"))
340 set.setPixmap( pm, TQIconSet::Automatic, TQIconSet::Normal );
341 TQToolButton::setIconSet( set );
346 TQIconSet set = iconSet();
348 if (d->m_parent && !strcmp(d->m_parent->name(),
"mainToolBar"))
354 set.setPixmap( pm, TQIconSet::Automatic, TQIconSet::Disabled );
355 TQToolButton::setIconSet( set );
362 return TQToolButton::popup();
367 TQToolButton::setPopup(p);
368 TQToolButton::setPopupDelay(-1);
374 TQToolButton::setPopup(p);
375 TQToolButton::setPopupDelay(TQApplication::startDragTime());
378 void TDEToolBarButton::leaveEvent(TQEvent *)
380 if( d->m_isRaised || d->m_isActive )
382 d->m_isRaised =
false;
383 d->m_isActive =
false;
387 emit highlighted(d->m_id,
false);
390 void TDEToolBarButton::enterEvent(TQEvent *)
396 d->m_isActive =
true;
397 if (!isToggleButton())
398 d->m_isRaised =
true;
402 d->m_isRaised =
false;
403 d->m_isActive =
false;
408 emit highlighted(d->m_id,
true);
411 bool TDEToolBarButton::eventFilter(TQObject *o, TQEvent *ev)
418 if (TQToolButton::popup())
420 if (ev->type() == TQEvent::MouseButtonPress)
422 TQMouseEvent* mev =
static_cast<TQMouseEvent*
>(ev);
423 d->m_mousePressPos = mev->pos();
425 else if (ev->type() == TQEvent::MouseMove)
427 TQMouseEvent* mev =
static_cast<TQMouseEvent*
>(ev);
428 if ((mev->pos() - d->m_mousePressPos).manhattanLength()
438 (ev->type() == TQEvent::MouseButtonPress ||
439 ev->type() == TQEvent::MouseButtonRelease ||
440 ev->type() == TQEvent::MouseButtonDblClick) && isOn())
444 if (ev->type() == TQEvent::MouseButtonDblClick)
446 emit doubleClicked(d->m_id);
451 return TQToolButton::eventFilter(o, ev);
456 d->m_buttonDown =
true;
458 if ( e->button() == TQt::MidButton )
461 TQMouseEvent ev( TQEvent::MouseButtonPress, e->pos(), e->globalPos(), TQt::LeftButton, e->state() );
462 TQToolButton::mousePressEvent(&ev);
465 TQToolButton::mousePressEvent(e);
470 TQt::ButtonState state = TQt::ButtonState(e->button() | (e->state() & KeyButtonMask));
471 if ( e->button() == TQt::MidButton )
473 TQMouseEvent ev( TQEvent::MouseButtonRelease, e->pos(), e->globalPos(), TQt::LeftButton, e->state() );
474 TQToolButton::mouseReleaseEvent(&ev);
477 TQToolButton::mouseReleaseEvent(e);
479 if ( !d->m_buttonDown )
481 d->m_buttonDown =
false;
483 if ( hitButton( e->pos() ) )
487 void TDEToolBarButton::drawButton( TQPainter *_painter )
489 TQStyle::SFlags flags = TQStyle::Style_Default;
490 TQStyle::SCFlags active = TQStyle::SC_None;
493 flags |= TQStyle::Style_Down;
494 active |= TQStyle::SC_ToolButton;
496 if (isEnabled()) flags |= TQStyle::Style_Enabled;
497 if (isOn()) flags |= TQStyle::Style_On;
498 if (isEnabled() && hasMouse()) flags |= TQStyle::Style_Raised;
499 if (hasFocus()) flags |= TQStyle::Style_HasFocus;
502 style().drawComplexControl(TQStyle::CC_ToolButton, _painter,
this, rect(),
503 colorGroup(), flags, TQStyle::SC_ToolButton, active, TQStyleOption());
507 TQFontMetrics fm(tmp_font);
511 if (d->m_iconText == TDEToolBar::IconOnly)
513 TQPixmap pixmap = iconSet().pixmap( TQIconSet::Automatic,
514 isEnabled() ? (d->m_isActive ? TQIconSet::Active : TQIconSet::Normal) :
516 isOn() ? TQIconSet::On : TQIconSet::Off );
517 if( !pixmap.isNull())
519 dx = ( width() - pixmap.width() ) / 2;
520 dy = ( height() - pixmap.height() ) / 2;
521 if ( isDown() && style().styleHint(TQStyle::SH_GUIStyle) == WindowsStyle )
526 _painter->drawPixmap( dx, dy, pixmap );
529 else if (d->m_iconText == TDEToolBar::IconTextRight)
531 TQPixmap pixmap = iconSet().pixmap( TQIconSet::Automatic,
532 isEnabled() ? (d->m_isActive ? TQIconSet::Active : TQIconSet::Normal) :
534 isOn() ? TQIconSet::On : TQIconSet::Off );
535 if( !pixmap.isNull())
538 dy = ( height() - pixmap.height() ) / 2;
539 if ( isDown() && style().styleHint(TQStyle::SH_GUIStyle) == WindowsStyle )
544 _painter->drawPixmap( dx, dy, pixmap );
547 if (!textLabel().isNull())
549 textFlags = AlignVCenter|AlignLeft;
550 if (!pixmap.isNull())
551 dx = 4 + pixmap.width() + 2;
555 if ( isDown() && style().styleHint(TQStyle::SH_GUIStyle) == WindowsStyle )
560 textRect = TQRect(dx, dy, width()-dx, height());
563 else if (d->m_iconText == TDEToolBar::TextOnly)
565 if (!textLabel().isNull())
567 textFlags = AlignVCenter|AlignLeft;
568 dx = (width() - fm.width(textLabel())) / 2;
569 dy = (height() - fm.lineSpacing()) / 2;
570 if ( isDown() && style().styleHint(TQStyle::SH_GUIStyle) == WindowsStyle )
575 textRect = TQRect( dx, dy, fm.width(textLabel()), fm.lineSpacing() );
578 else if (d->m_iconText == TDEToolBar::IconTextBottom)
580 TQPixmap pixmap = iconSet().pixmap( TQIconSet::Automatic,
581 isEnabled() ? (d->m_isActive ? TQIconSet::Active : TQIconSet::Normal) :
583 isOn() ? TQIconSet::On : TQIconSet::Off );
584 if( !pixmap.isNull())
586 dx = (width() - pixmap.width()) / 2;
587 dy = (height() - fm.lineSpacing() - pixmap.height()) / 2;
588 if ( isDown() && style().styleHint(TQStyle::SH_GUIStyle) == WindowsStyle )
593 _painter->drawPixmap( dx, dy, pixmap );
596 if (!textLabel().isNull())
598 textFlags = AlignBottom|AlignHCenter;
599 dx = (width() - fm.width(textLabel())) / 2;
600 dy = height() - fm.lineSpacing() - 4;
602 if ( isDown() && style().styleHint(TQStyle::SH_GUIStyle) == WindowsStyle )
607 textRect = TQRect( dx, dy, fm.width(textLabel()), fm.lineSpacing() );
612 if (!textLabel().isNull() && !textRect.isNull())
616 _painter->setPen(palette().disabled().dark());
617 else if(d->m_isRaised)
620 _painter->setPen( colorGroup().buttonText() );
621 _painter->drawText(textRect, textFlags, textLabel());
624 if (TQToolButton::popup())
626 TQStyle::SFlags arrowFlags = TQStyle::Style_Default;
628 if (isDown()) arrowFlags |= TQStyle::Style_Down;
629 if (isEnabled()) arrowFlags |= TQStyle::Style_Enabled;
631 style().drawPrimitive(TQStyle::PE_ArrowDown, _painter,
632 TQRect(width()-7, height()-7, 7, 7), colorGroup(),
633 arrowFlags, TQStyleOption() );
637 void TDEToolBarButton::paletteChange(
const TQPalette &)
639 if(!d->m_isSeparator)
646 bool TDEToolBarButton::event(TQEvent *e)
648 if (e->type() == TQEvent::ParentFontChange || e->type() == TQEvent::ApplicationFontChange)
651 if (d->m_iconText != TDEToolBar::IconOnly)
656 return TQToolButton::event(e);
660 void TDEToolBarButton::showMenu()
666 void TDEToolBarButton::slotDelayTimeout()
672 void TDEToolBarButton::slotClicked()
677 if ( d->m_parent && !d->m_parent->rect().contains( geometry().center() ) ) {
679 if ( ( state & TQt::MouseButtonMask ) == TQt::NoButton )
680 state = ButtonState( TQt::LeftButton | state );
685 void TDEToolBarButton::slotPressed()
687 emit pressed( d->m_id );
690 void TDEToolBarButton::slotReleased()
692 emit released( d->m_id );
695 void TDEToolBarButton::slotToggled()
697 emit toggled( d->m_id );
702 d->m_noStyle = no_style;
705 d->m_iconText = TDEToolBar::IconTextRight;
722 leaveEvent((TQEvent *) 0);
735 setToggleButton(flag);
737 connect(
this, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotToggled()));
739 disconnect(
this, TQ_SIGNAL(toggled(
bool)),
this, TQ_SLOT(slotToggled()));
742 TQSize TDEToolBarButton::sizeHint()
const
747 TQSize TDEToolBarButton::minimumSizeHint()
const
752 TQSize TDEToolBarButton::minimumSize()
const
759 return d->m_isRaised;
764 return d->m_isActive;
769 return static_cast<int>( d->m_iconText );
778 TDEToolBarButtonList::TDEToolBarButtonList()
780 setAutoDelete(
false);
783 void TDEToolBarButton::virtual_hook(
int,
void* )
786 #include "tdetoolbarbutton.moc"
static ButtonState keyboardMouseState()
static TQColor toolBarHighlightColor()
static TQFont toolBarFont()
static int dndEventDelay()
static TDEInstance * instance()