summaryrefslogtreecommitdiffstats
path: root/src/MachBuntButton.cpp
blob: 82f1b144051290ba1eab35805225591a7a93a38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * $Id: MachBuntButton.cpp,v 1.34 2004/03/23 15:34:45 llunak Exp $
 *
 */

#include "MachBunt.h"  
#include <kpixmapeffect.h>
#include <kimageeffect.h>
#include <kiconeffect.h>
#include <kdrawutil.h>
#include <klocale.h>

#include <qlayout.h>
#include <qdrawutil.h>
#include <qtooltip.h>
#include <qdatetime.h>
#include <qbitmap.h>
#include <qimage.h>
#include <qcursor.h>

namespace SuSEMachBunt {

// the titlebar buttons
extern bool    pixmaps_created;

QPixmap *defaultPixmap;
QColor  *btnForeground;

MachBuntButton::MachBuntButton(MachBunt * c, const char *name,
                               int _button, QPixmap bgI, QPixmap bgA,
			       bool isMini,
                               const QString &tip)
    : QButton (c->widget(), name), position( ButtonMiddle ), client( c )
{
	setTipText(tip);
        setMouseTracking( true );
        setCursor( arrowCursor );

	// Eliminate background flicker
	setBackgroundMode( QWidget::NoBackground ); 
	
	menuBtn    = _button == BtnMenu;
	miniBtn    = isMini;
	button     = _button;
	state      = 0;
	pressed    = 0;
	bg[0]      = bgI;
	bg[1]      = bgA;

	// Use larger button for the menu, or mini-buttons for toolwindows.
	if ( isMini ) {
	    setFixedSize(12, 12);
	    resize(12, 12);
	} else{
	    setFixedSize(PIX_HEIGHT, PIX_HEIGHT);
	    resize(PIX_HEIGHT, PIX_HEIGHT);
	}
}

MachBuntButton::~MachBuntButton()
{
};

QSize MachBuntButton::sizeHint() const
{
   if ( miniBtn )
      return(QSize(12, 12));
   else
      return(QSize(PIX_HEIGHT, PIX_HEIGHT));
}


void MachBuntButton::reset(unsigned long changes)
{
	repaint(false);
	if (!menuBtn){
		emit ( shapeMe(0) );
		state=0;
		pressed = 0;
	}
}


void MachBuntButton::setBitmap(const unsigned char *bitmap)
{
    repaint( false );   
}


void MachBuntButton::setPixmap( const QPixmap &_p )
{
	QPixmap p;

	if ( _p.isNull() )
		p = *defaultPixmap;
	else
		p = _p;

	if ( miniBtn ){
		menuPixmap.convertFromImage( p.convertToImage().smoothScale(8,8) );
	}else
	    menuPixmap = p;


	for ( int m=0; m<2; m++ )
     for ( int M=0; M<3; M++ )
      for ( int a=0; a<2; a++ )
		if (menuBtn)
          menuButtonPixmap[button][m][M][a].resize(0,0);
		else
          buttonPixmap[button][m][M][a].resize(0,0);

    repaint( false );   
}

void MachBuntButton::enterEvent( QEvent *e )
{
	if (pressed){
		state = 2;
		emit ( shapeMe(this->x()) );
	}else
		state = 1;
	repaint();
	QButton::enterEvent(e);
}

void MachBuntButton::leaveEvent( QEvent *e )
{
	if (pressed)
		emit ( shapeMe(0) );
	state = 0;
	repaint();
	QButton::leaveEvent(e);
}

void MachBuntButton::setPosition( ButtonPos pos )
{
    position = pos;
}

bool MachBuntButton::resizePosition( QPoint pos )
{
    if( pos.y() < BUTTON_RESIZE_SIZE )
        return true;
    if( position == ButtonLeft && pos.x() < BUTTON_RESIZE_SIZE )
        return true;
    if( position == ButtonRight && pos.x() > width() - 1 - BUTTON_RESIZE_SIZE )
        return true;
    return false;
}

void MachBuntButton::mouseMoveEvent( QMouseEvent* e )
{
        if( resizePosition( e->pos()))
            {
            e->ignore();
            unsetCursor(); // use cursor set according to mousePosition()
            return;
            }
       if ( pressed == 1 )
	  emit( mousePressedMove(e) );
        e->ignore(); // so that dragging works, even when moving more inside the button
        setCursor( arrowCursor );
}

void MachBuntButton::mousePressEvent( QMouseEvent* e )
{
        if( resizePosition( e->pos()))
            {
            e->ignore();
            return;
            }
	if (!menuBtn){
		state = 2;
		pressed = 1;
		emit ( shapeMe(this->x()) );
	}

	last_button = e->button();
	QMouseEvent me ( e->type(), e->pos(), e->globalPos(),
					 LeftButton, e->state() );
	QButton::mousePressEvent( &me );
}

void MachBuntButton::mouseReleaseEvent( QMouseEvent* e )
{
	if (!menuBtn){
		emit ( shapeMe(0) );
		if (state==2)
			state=1;
		pressed = 0;
	}
        if( resizePosition( e->pos()))
            {
            e->ignore();
            return;
            }
	last_button = e->button();
	QMouseEvent me ( e->type(), e->pos(), e->globalPos(),
			 LeftButton, e->state() );
	QButton::mouseReleaseEvent( &me );
}

void MachBuntButton::drawButton(QPainter *p)
{
	bool a = client->isActive();
	QPixmap *P;
	if (menuBtn)
		P = &menuButtonPixmap[button][a][state][miniBtn];
	else
		P = &buttonPixmap[button][a][state][miniBtn];

	if ( P->isNull() )
        *P = create_buttonPixmap( miniBtn ? 12 : PIX_HEIGHT, miniBtn ? 12 : PIX_HEIGHT, menuPixmap, bg[a], a, state );

	p->drawPixmap( 0, 0, *P );
}

void MachBuntButton::setTipText(const QString &tip) {
  if(KDecoration::options()->showTooltips()) {
    QToolTip::remove(this );
    QToolTip::add(this, tip );
  }
}

}

// vim: ts=4