1 // File: QtxMenuButton.cxx
2 // Author: Sergey TELKOV
4 #include "QtxMenuButton.h"
8 #include <qpopupmenu.h>
9 #include <qpointarray.h>
10 #include <qapplication.h>
12 class QtxMenuButton::PopupMenu : public QPopupMenu
15 PopupMenu( QtxMenuButton* mb ) : QPopupMenu( mb ), myMenuButton( mb ) {};
16 virtual ~PopupMenu() {};
18 virtual void setMinimumSize( int, int );
21 QtxMenuButton* myMenuButton;
24 void QtxMenuButton::PopupMenu::setMinimumSize( int w, int h )
26 if ( myMenuButton->isAlignWidth() &&
27 ( myMenuButton->position() == Top || myMenuButton->position() == Bottom ) )
28 w = QMAX( w, myMenuButton->width() );
30 QPopupMenu::setMinimumSize( w, h );
34 QtxMenuButton::QtxMenuButton( int pos, QWidget* parent, const char* name )
35 : QPushButton( parent, name ),
41 QtxMenuButton::QtxMenuButton( const QString& text, QWidget* parent, const char* name )
42 : QPushButton( parent, name ),
49 QtxMenuButton::QtxMenuButton( int pos, const QString& text, QWidget* parent, const char* name )
50 : QPushButton( parent, name ),
57 QtxMenuButton::QtxMenuButton( QWidget* parent, const char* name )
58 : QPushButton( parent, name ),
64 QtxMenuButton::~QtxMenuButton()
68 void QtxMenuButton::initialize()
73 setAutoDefault( false );
74 myPopup = new PopupMenu( this );
77 connect( myPopup, SIGNAL( activated( int ) ), this, SIGNAL( activated( int ) ) );
78 connect( this, SIGNAL( clicked() ), this, SLOT( onShowPopup() ) );
81 int QtxMenuButton::position() const
86 bool QtxMenuButton::isAlignWidth() const
91 bool QtxMenuButton::isArrowEnabled() const
96 void QtxMenuButton::setPosition( const int pos )
102 if ( myPopup->isVisible() )
106 void QtxMenuButton::setAlignWidth( const bool on )
115 void QtxMenuButton::setArrowEnabled( const bool on )
124 void QtxMenuButton::clear()
132 void QtxMenuButton::removeItem( int id )
139 int QtxMenuButton::insertSeparator( int id )
143 res = myPopup->insertSeparator( id );
147 int QtxMenuButton::insertItem( const QString& t, int id, int index )
151 resId = myPopup->insertItem( t, id, index );
159 int QtxMenuButton::insertItem( const QIconSet& is, const QString& t, int id, int index )
163 resId = myPopup->insertItem( is, t, id, index );
171 void QtxMenuButton::onShowPopup()
173 if ( !myPopup || !myPopup->count() )
179 QPoint p = mapToGlobal( QPoint( 0, 0 ) );
185 switch ( position() )
188 xoffset = -1 * ( myPopup->sizeHint().width() + margin );
191 xoffset = width() + margin;
194 yoffset = -1 * ( myPopup->sizeHint().height() + margin );
198 yoffset = height() + margin;
201 int dw = QApplication::desktop()->width();
202 int dh = QApplication::desktop()->height();
203 x = QMIN( QMAX( x + xoffset, 0 ), dw );
204 y = QMIN( QMAX( y + yoffset, 0 ), dh );
206 myPopup->exec( QPoint( x, y ) );
209 bool QtxMenuButton::event( QEvent* e )
211 if ( e->type() == QEvent::MouseButtonPress ||
212 e->type() == QEvent::MouseButtonDblClick ||
213 e->type() == QEvent::MouseButtonRelease )
219 return QPushButton::event( e );
222 QSize QtxMenuButton::sizeHint() const
224 QSize sz = QPushButton::sizeHint();
225 if ( ( position() == Top || position() == Bottom ) && myPopup && myAlign )
226 sz = QSize( QMAX( sz.width(), myPopup->sizeHint().width() ), sz.height() );
231 QSize QtxMenuButton::minimumSizeHint() const
233 QSize sz = QPushButton::minimumSizeHint();
234 if ( ( position() == Top || position() == Bottom ) && myPopup && myAlign )
235 sz = QSize( QMAX( sz.width(), myPopup->sizeHint().width() ), sz.height() );
240 void QtxMenuButton::resizeEvent( QResizeEvent* re )
243 QPushButton::resizeEvent( re );
245 if ( ( position() == Top || position() == Bottom ) && myPopup && myAlign )
246 myPopup->setMinimumWidth( re ? re->size().width() : width() );
249 QPopupMenu* QtxMenuButton::popup() const
254 void QtxMenuButton::drawButtonLabel( QPainter* p )
256 QPushButton::drawButtonLabel( p );
258 QStyle::SFlags flags = QStyle::Style_Default;
260 flags |= QStyle::Style_Enabled;
262 flags |= QStyle::Style_HasFocus;
267 QRect r = style().subRect( QStyle::SR_PushButtonContents, this );
270 if ( myArrow && myPopup && myPopup->count() )
276 QRect ar( 0, 0, w, h );
277 if ( position() == Left || position() == Top )
278 r.moveBy( ar.width() + 2 * margin, 0 );
280 ar.moveBy( r.width() - ar.width() - 2 * margin, 0 );
282 r.setWidth( r.width() - ar.width() - 2 * margin );
284 ar.moveBy( margin, ( height() - h ) / 2 );
286 QPointArray arrow( 3 );
287 switch ( position() )
290 arrow.putPoints( 0, 3, ar.left(), ar.top() + ar.height() / 2, ar.right(), ar.top(), ar.right(), ar.bottom() );
293 arrow.putPoints( 0, 3, ar.left(), ar.top(), ar.left(), ar.bottom(), ar.right(), ar.top() + ar.height() / 2 );
296 arrow.putPoints( 0, 3, ar.left(), ar.bottom(), ar.right(), ar.bottom(), ar.left() + ar.width() / 2, ar.top() );
300 arrow.putPoints( 0, 3, ar.left(), ar.top(), ar.right(), ar.top(), ar.left() + ar.width() / 2, ar.bottom() );
304 p->setPen( colorGroup().text() );
305 p->setBrush( colorGroup().text() );
306 p->drawPolygon( arrow, true );
309 // style().drawControl( QStyle::CE_PushButtonLabel, p, this, r, colorGroup(), flags );