summaryrefslogtreecommitdiffstats
path: root/client/crystalbutton.cpp
blob: 77f6e06bf5d15c71c790f5b17a214ac40594b981 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/***************************************************************************
 *   Copyright (C) 2006 by Sascha Hlusiak                                  *
 *   [email protected]                                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/


#include <tqimage.h>
#include <tqtooltip.h>
#include <tqpainter.h>

#include "crystalclient.h"
#include "crystalbutton.h"
#include "buttonimage.h"
#include "imageholder.h"


CrystalButton::CrystalButton(CrystalClient *parent, const char *name,
				const TQString& tip, ButtonType type,
				ButtonImage *vimage)
: TQButton(parent->widget(), name), client_(parent), type_(type), image(vimage), lastmouse_(0)
{
	setBackgroundMode(NoBackground);
	resetSize(false);
	setCursor(arrowCursor);
	
	hover=first=last=false;
	animation=0.0;
	TQToolTip::add(this, tip);
	connect ( &animation_timer,TQT_SIGNAL(timeout()),this,TQT_SLOT(animate()));
}

CrystalButton::~CrystalButton()
{
}

void CrystalButton::resetSize(bool FullSize)
{
	if (FullSize || (image && image->drawMode==1))
	{
		setFixedSize(buttonSizeH(),factory->titlesize);
	}else setFixedSize(buttonSizeH(),buttonSizeV());
}

void CrystalButton::setBitmap(ButtonImage *newimage)
{
	image=newimage;
	repaint(false);
}

TQSize CrystalButton::sizeHint() const
{
	return TQSize(buttonSizeH(),buttonSizeV());
}

int CrystalButton::buttonSizeH() const
{
	int w=image?image->image_width:DEFAULT_IMAGE_SIZE;
	int h=image?image->image_height:DEFAULT_IMAGE_SIZE;
	int vS=image?image->vSpace:2;
	int hS=image?image->hSpace:2;
	return (factory->titlesize-1-vS>=h)?
		w+hS*2:
		(int)(((float)buttonSizeV()/(float)h)*(float)w)+hS;
}

int CrystalButton::buttonSizeV() const
{
	int h=image?image->image_height:DEFAULT_IMAGE_SIZE;
	int vS=image?image->vSpace:2;
	return (factory->titlesize-1-vS>h)?h:factory->titlesize-1-vS;
}

void CrystalButton::enterEvent(TQEvent *e)
{
	hover=true;
	if (factory->hovereffect)repaint(false);
	if (factory->animateHover)animation_timer.start(60);
	TQButton::enterEvent(e);
}

void CrystalButton::leaveEvent(TQEvent *e)
{
	hover=false;
	if (factory->hovereffect)repaint(false);
	if (factory->animateHover)animation_timer.start(80);
	TQButton::leaveEvent(e);
}

void CrystalButton::mousePressEvent(TQMouseEvent* e)
{
	lastmouse_ = e->button();
	int button;
	switch(e->button())
	{
	case Qt::LeftButton:
		button=Qt::LeftButton;
		break;
	case Qt::RightButton:
		if ((type_ == ButtonMax) || (type_ == ButtonMin) || (type_ == ButtonMenu) || (type_ == ButtonClose))
			button=Qt::LeftButton; else button=Qt::NoButton;
		break;
	case Qt::MidButton:
		if ((type_ == ButtonMax) || (type_ == ButtonMin))
			button=Qt::LeftButton; else button=Qt::NoButton;
		break;

	default:button=Qt::NoButton;
		break;
	}
	TQMouseEvent me(e->type(), e->pos(), e->globalPos(),button, e->state());
	TQButton::mousePressEvent(&me);
}

void CrystalButton::mouseReleaseEvent(TQMouseEvent* e)
{
	lastmouse_ = e->button();
	int button;
	switch(e->button())
	{
	case Qt::LeftButton:
		button=Qt::LeftButton;
		break;
	case Qt::RightButton:
		if ((type_ == ButtonMax) || (type_ == ButtonMin) || (type_ == ButtonMenu) || (type_==ButtonClose))
			button=Qt::LeftButton; else button=Qt::NoButton;
		break;
	case Qt::MidButton:
		if ((type_ == ButtonMax) || (type_ == ButtonMin))
			button=Qt::LeftButton; else button=Qt::NoButton;
		break;
	
	default:button=Qt::NoButton;
		break;
	}
	TQMouseEvent me(e->type(), e->pos(), e->globalPos(), button, e->state());
	TQButton::mouseReleaseEvent(&me);
}

void CrystalButton::drawButton(TQPainter *painter)
{
	if (!CrystalFactory::initialized()) return;

	TQColorGroup group;
	float dx, dy;
	int dm=0;

	TQPixmap pufferPixmap;
	pufferPixmap.resize(width(), height());
	TQPainter pufferPainter(&pufferPixmap);
	
	CrystalFactory *f=((CrystalFactory*)client_->factory());
	TQPixmap *background;
	if (f->transparency)background=f->image_holder->image(client_->isActive());
		else background=NULL;
	WND_CONFIG *wndcfg=client_->isActive()?&f->active:&f->inactive;

	if (background && !background->isNull())
	{
		TQRect r=rect();
		TQPoint p=mapToGlobal(TQPoint(0,0));
		r.moveBy(p.x(),p.y());
	
		pufferPainter.drawPixmap(TQPoint(0,0),*background,r);
	}else{
		group = client_->options()->colorGroup(KDecoration::ColorTitleBar, client_->isActive());
		pufferPainter.fillRect(rect(), group.background());
	}

	if (!wndcfg->overlay.isNull())
	{
		pufferPainter.drawTiledPixmap(rect(),wndcfg->overlay,TQPoint(x(),y()));
	}

	dm=0;
	if (image && (image->drawMode==1))dm=1;
	if (wndcfg->outlineMode)
	{
		// outline the frame
		pufferPainter.setPen(wndcfg->frameColor);

		if (wndcfg->outlineMode==2)pufferPainter.setPen(wndcfg->frameColor.dark(150));
		if (wndcfg->outlineMode==3)pufferPainter.setPen(wndcfg->frameColor.light(150));
		// top
		if ((client_->FullMax && client_->isShade() && (dm==0)) ||
			((dm==1)&&(!client_->FullMax || client_->isShade()))) pufferPainter.drawLine(0,0,width(),0);
		// left
		if (first && client_->FullMax && client_->isShade())pufferPainter.drawLine(0,0,0,height());

		if (wndcfg->outlineMode==2)pufferPainter.setPen(wndcfg->frameColor.light(150));
		if (wndcfg->outlineMode==3)pufferPainter.setPen(wndcfg->frameColor.dark(150));
		// bottom
		if (client_->isShade() && ((dm==1)||(client_->FullMax)))pufferPainter.drawLine(0,height()-1,width(),height()-1);
		
		// right
		if (last && client_->FullMax && client_->isShade())pufferPainter.drawLine(width()-1,0,width()-1,height());
	}
	if (wndcfg->inlineMode && (client_->FullMax||dm==1) && !client_->isShade())
	{
		// inline the frame
		if (wndcfg->inlineMode==1)pufferPainter.setPen(wndcfg->inlineColor);
		if (wndcfg->inlineMode==2)pufferPainter.setPen(wndcfg->inlineColor.dark(150));
		if (wndcfg->inlineMode==3)pufferPainter.setPen(wndcfg->inlineColor.light(150));
		// buttons just need to draw the bottom line
		pufferPainter.drawLine(0,height()-1,width(),height()-1);
	}


	if (type_ == ButtonMenu && (!::factory->menuImage || image==NULL || (image!=NULL && !image->initialized()))) {
		// we paint the mini icon (which is 16 pixels high)
		dx = float(width() - 16) / 2.0;
		dy = float(height() - 16) / 2.0;

		if (dx<1 || dy<=1)
		{
			int m=(rect().width()-2<rect().height())?rect().width()-2:rect().height();
			TQRect r((rect().width()-m)/2,(rect().height()-m)/2,m,m);
// 			if (isDown()) { r.moveBy(1,1); }
        		pufferPainter.drawPixmap(r, client_->icon().pixmap(TQIconSet::Small,
                                                           TQIconSet::Normal));
		}else{
//         	if (isDown()) { dx++; dy++; }
			pufferPainter.drawPixmap((int)dx, (int)dy, client_->icon().pixmap(TQIconSet::Small,
                                                           TQIconSet::Normal));
		}
	} else if (image && image->initialized()) {
		// otherwise we paint the deco
		dx = float(width() - image->image_width) / 2.0;
		dy = float(height() - image->image_height) / 2.0;
		
		TQImage *img=image->normal;

		if (::factory->hovereffect)
		{
			if (hover)
			{
				img=image->hovered; 
			}
			if (::factory->animateHover)
			{
				img=image->getAnimated(animation);
			}
		}
		if (isDown())
		{
			img=image->pressed;
		}
	
		if (img)
		if (dx<image->hSpace/2 || dy<0)
		{	// Deco size is smaller than image, we need to stretch it
			int w,h;

			if (rect().width()-image->hSpace<rect().height())
			{
				w=rect().width()-image->hSpace;
				h=(int)((float)w*(float)image->image_height/(float)image->image_width);
			}else{
				h=rect().height();
				w=(int)((float)h*(float)image->image_width/(float)image->image_height);
			}

			TQRect r((rect().width()-w)/2,(rect().height()-h)/2,w,h);

			pufferPainter.drawImage(r,*img);
			if (type_ == ButtonMenu) drawMenuImage(&pufferPainter, r);
		}else{
			// Otherwise we just paint it
			if (image->drawMode==1)dy=0;
			pufferPainter.drawImage(TQPoint((int)dx,(int)dy),*img);

			if (type_ == ButtonMenu) drawMenuImage(&pufferPainter, 
				TQRect((int)dx,(int)dy,image->image_width,image->image_height));
	    }
	}
	
	pufferPainter.end();
	painter->drawPixmap(0,0, pufferPixmap);
}

void CrystalButton::drawMenuImage(TQPainter* painter, TQRect r)
{
	if (type_ != ButtonMenu) return;
	// we paint the mini icon (which is 16 pixels high)
	r.setTop(r.top()+1);
	r.setBottom(r.bottom()-1);
	float dx = float(r.width() - 16) / 2.0;
	float dy = float(r.height() - 16) / 2.0;
		
	if (dx<1 || dy<=1)
	{
		int m=(r.width()-2<r.height())?r.width()-2:r.height();
		TQRect r2(r.left()+(r.width()-m)/2,r.top()+(r.height()-m)/2,m,m);
		painter->drawPixmap(r2, client_->icon().pixmap(TQIconSet::Small,
								TQIconSet::Normal));
	}else{
		painter->drawPixmap(r.left()+(int)dx, r.top()+(int)dy, client_->icon().pixmap(TQIconSet::Small,
									TQIconSet::Normal));
	}
}

void CrystalButton::animate()
{
	if (hover)
	{
		animation+=0.25;
		if (animation>1.0)
		{
			animation=1.0;
			animation_timer.stop();
		}
	}else{
		animation-=0.15;
		if (animation<0.0)
		{
			animation=0.0;
			animation_timer.stop();
		}
	}
	repaint(false);
}

#include "crystalbutton.moc"