summaryrefslogtreecommitdiffstats
path: root/src/kvirc/ui/kvi_irctoolbar.cpp
blob: 821f3ab8beb4d1d025b7d54ccb994a6785ba4350 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//=============================================================================
//
//   File : kvi_irctoolbar.cpp
//   Creation date : Thu Oct 29 2000 14:13:13 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2000-2004 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) 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.
//
//=============================================================================

#define __KVIRC__
#include "kvi_irctoolbar.h"
#include "kvi_console.h"
#include "kvi_frame.h"
#include "kvi_locale.h"
#include "kvi_iconmanager.h"
#include "kvi_settings.h"
#include "kvi_options.h"
#include "kvi_frame.h"
#include "kvi_app.h"
#include "kvi_dynamictooltip.h"
#include "kvi_ircurl.h"
#include "kvi_internalcmd.h"
#include "kvi_pointerlist.h"
#include "kvi_ircconnection.h"
#include "kvi_ircconnectionuserinfo.h"
#include "kvi_irccontext.h"
#include "kvi_lagmeter.h"

#include <tqstyle.h>
#include <tqpainter.h>
#include "kvi_tal_popupmenu.h"
#include <tqlayout.h>


#ifdef COMPILE_PSEUDO_TRANSPARENCY
	extern TQPixmap * g_pShadedChildGlobalDesktopBackground;
#endif

static TQPixmap                                  * g_pIccMemBuffer               = 0;
static KviPointerList<KviToolBarGraphicalApplet>    * g_pToolBarGraphicalAppletList = 0;

KviToolBarGraphicalApplet::KviToolBarGraphicalApplet(TQWidget * par,const char * name)
: TQToolButton(par,name)
{
	if(!g_pToolBarGraphicalAppletList)
	{
		g_pToolBarGraphicalAppletList = new KviPointerList<KviToolBarGraphicalApplet>();
		g_pToolBarGraphicalAppletList->setAutoDelete(false);
		g_pIccMemBuffer = new TQPixmap(1,1);
	}
	
	g_pToolBarGraphicalAppletList->append(this);
	setBackgroundMode(TQWidget::NoBackground);

	setMouseTracking(true);
	m_bResizeMode = false;

	m_sizeHint = TQSize(180,32);
	m_bSizeLoaded = false;
}

unsigned int KviToolBarGraphicalApplet::loadAppletWidth()
{
	if(KVI_OPTION_UINT(KviOption_uintIrcContextAppletWidth) < 32)
		KVI_OPTION_UINT(KviOption_uintIrcContextAppletWidth) = 32;
	return KVI_OPTION_UINT(KviOption_uintIrcContextAppletWidth);
}

void KviToolBarGraphicalApplet::saveAppletWidth(unsigned int uWidth)
{
	KVI_OPTION_UINT(KviOption_uintIrcContextAppletWidth) = uWidth;
}

void KviToolBarGraphicalApplet::setupSizeHint()
{
	m_sizeHint = TQSize(loadAppletWidth(),22);
	m_bSizeLoaded = true;
}

TQSize KviToolBarGraphicalApplet::sizeHint() const
{
	// forget constness :(
	KviToolBarGraphicalApplet * that = (KviToolBarGraphicalApplet *)this;
	if(!m_bSizeLoaded)that->setupSizeHint();
	return m_sizeHint;
}

/*
toolbar.define(default)
{
	applet(thisandthat);
	applet(thisandthat);
	applet(thisandthat);
	applet(thisandthat);
}
*/


void KviToolBarGraphicalApplet::mouseMoveEvent(TQMouseEvent * e)
{
	if(e->state() & TQt::LeftButton)
	{
		if(m_bResizeMode)
		{
			int w = e->pos().x();
			if(w < 32)w = 32;
			if(w > 480)w = 480;
			m_sizeHint = TQSize(w,22);
			resize(w,height());
			g_pApp->postEvent(parentWidget(),new TQEvent(TQEvent::LayoutHint));
		}
	} else {
		if(e->pos().x() > width() - 4)
			setCursor(TQt::sizeHorCursor);
		else
			setCursor(TQt::arrowCursor);
	}
}

void KviToolBarGraphicalApplet::mousePressEvent(TQMouseEvent * e)
{
	if(e->button() & TQt::LeftButton)
	{
		m_bResizeMode = (e->pos().x() > (width() - 4));
	}
}

void KviToolBarGraphicalApplet::mouseReleaseEvent(TQMouseEvent * e)
{
	m_bResizeMode = false;
}


KviToolBarGraphicalApplet::~KviToolBarGraphicalApplet()
{
	saveAppletWidth(m_sizeHint.width());
	g_pToolBarGraphicalAppletList->removeRef(this);
	if(g_pToolBarGraphicalAppletList->isEmpty())
	{
		delete g_pToolBarGraphicalAppletList;
		g_pToolBarGraphicalAppletList = 0;
		delete g_pIccMemBuffer;
		g_pIccMemBuffer = 0;
	} else {
		// resize the mem buffer to match the maximum width / height of the applets
		resizeMemBuffer();
	}
}

void KviToolBarGraphicalApplet::resizeMemBuffer()
{
	int uMaxW = 0;
	int uMaxH = 0;
	for(KviToolBarGraphicalApplet * a = g_pToolBarGraphicalAppletList->first();a;a = g_pToolBarGraphicalAppletList->next())
	{
		if(uMaxW < a->width())uMaxW = a->width();
		if(uMaxH < a->height())uMaxH = a->height();
	}
	g_pIccMemBuffer->resize(uMaxW,uMaxH);
}

void KviToolBarGraphicalApplet::paintEvent(TQPaintEvent *e)
{
	if(!isVisible())return;

	TQPainter pa(g_pIccMemBuffer);

#ifdef COMPILE_PSEUDO_TRANSPARENCY
	if(g_pShadedChildGlobalDesktopBackground)
	{
		TQPoint pnt = mapToGlobal(TQPoint(0,0));
		pa.drawTiledPixmap(e->rect().left(),e->rect().top(),e->rect().width(),e->rect().height(),*g_pShadedChildGlobalDesktopBackground,pnt.x(),pnt.y());
	} else {
#endif
		if(KVI_OPTION_PIXMAP(KviOption_pixmapIrcToolBarAppletBackground).pixmap())
		{
			TQPoint pnt = mapToGlobal(TQPoint(0,0));
			pa.drawTiledPixmap(e->rect().left(),e->rect().top(),e->rect().width(),e->rect().height(),*(KVI_OPTION_PIXMAP(KviOption_pixmapIrcToolBarAppletBackground).pixmap()),pnt.x(),pnt.y());
		} else {
			pa.fillRect(e->rect().left(),e->rect().top(),e->rect().width(),e->rect().height(),KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletBackground));
		}
#ifdef COMPILE_PSEUDO_TRANSPARENCY
	}
#endif

	drawContents(&pa);

	//Need to draw the sunken rect around the view now...
	pa.setPen(colorGroup().dark());
	pa.drawLine(0,0,width(),0);
	pa.drawLine(0,0,0,width());
	pa.setPen(colorGroup().light());
	pa.drawLine(1,height() - 1,width() - 1,height() - 1);
	pa.drawLine(width() - 1,1,width() - 1,height());

	bitBlt(this,e->rect().left(),e->rect().top(),g_pIccMemBuffer,e->rect().left(),e->rect().top(),e->rect().width(),e->rect().height(),TQt::CopyROP);
}

void KviToolBarGraphicalApplet::drawContents(TQPainter *)
{
	// nothing here
}

void KviToolBarGraphicalApplet::resizeEvent(TQResizeEvent *e)
{
	unsigned int uBufferW = g_pIccMemBuffer->width();
	unsigned int uBufferH = g_pIccMemBuffer->height();
	unsigned int uW = width();
	unsigned int uH = height();

	if((uBufferW != uW) || (uBufferH != uH))
	{
		if((uBufferW < uW) && (uBufferH < uH))g_pIccMemBuffer->resize(uW,uH);
		else resizeMemBuffer();
	}
}


//////////////////////////////////////////////////////////////////////////////
//
// KviIrcContextDisplay
//
//      Main applet of all irc contexts
//      Displays the server connection status, server name
//      nickname, user mode and the graphical indication of the context
//
//////////////////////////////////////////////////////////////////////////////

KviIrcContextDisplay::KviIrcContextDisplay(TQWidget * par,const char * name)
: KviToolBarGraphicalApplet(par,name)
{
	KviDynamicToolTip * tip = new KviDynamicToolTip(this);
	connect(tip,TQT_SIGNAL(tipRequest(KviDynamicToolTip *,const TQPoint &)),this,TQT_SLOT(tipRequest(KviDynamicToolTip *,const TQPoint &)));
}


KviIrcContextDisplay::~KviIrcContextDisplay()
{
}

void KviIrcContextDisplay::tipRequest(KviDynamicToolTip * tip,const TQPoint &)
{
	TQString txt;

	KviConsole * c = g_pActiveWindow->console();

	static TQString b = "<b>";
	static TQString nb = "</b>";
	static TQString br = "<br>";

	if(c)
	{
		KviIrcConnection * ic = c->connection();

		txt = b;

		if(!ic)
		{
			txt += __tr2qs("No connection");
			txt += nb;
			txt += br;
		} else {
			KviStr nickAndMode = ic->userInfo()->nickName();
			if(!(ic->userInfo()->userMode().isEmpty()))nickAndMode.append(KviStr::Format," (+%s)",ic->userInfo()->userMode().utf8().data());

			txt += ic->currentServerName();
			txt += nb;
			txt += br;
			txt += nickAndMode.ptr();
			txt += br;
		}

		TQString szNum;
		szNum.setNum(c->ircContextId());

		TQString szIrcContext = TQChar('(');
		szIrcContext += __tr2qs("IRC Context");
		szIrcContext += TQChar(' ');
		szIrcContext += szNum;
		szIrcContext += TQChar(')');
		txt += szIrcContext;

		if(ic && ic->lagMeter() && (KVI_OPTION_BOOL(KviOption_boolShowLagOnContextDisplay)))
		{
			txt += br;
			int lll;
			if((lll = ic->lagMeter()->lag()) > 0)
			{
				int llls = lll / 1000;
				int llld = (lll % 1000) / 100;
				int lllc = (lll % 100) / 10;
				KviTQString::appendFormatted(txt,__tr2qs("Lag: %d.%d%d"),llls,llld,lllc);
			} else {
				txt += __tr2qs("Lag: ?.??");
			}
		}
	} else {
		txt = b;
		txt += __tr2qs("No IRC context");
		txt += nb;
	}

	tip->tip(rect(),txt);
}


/*
TQSize KviIrcContextDisplay::sizeHint() const
{
	return TQSize(160,22);
}
*/

#define KVI_APPLETIRCCONTEXTINDICATORWIDTH 12

void KviIrcContextDisplay::drawContents(TQPainter * p)
{
	// The context indicator
	KviWindow * wnd = g_pActiveWindow;
	KviConsole * c = wnd ? wnd->console() : 0;

	if(c)
	{
		TQString serv,nick;
        TQString tmp;
		if(!c->connection())
		{
			serv = __tr2qs("Not connected");
		} else {
			if(c->isConnected())
			{
				KviIrcConnection * ic = c->connection();
				nick = ic->currentNickName();
				if(!ic->userInfo()->userMode().isEmpty())
				{
					static TQString spp(" (+");
					nick += spp;
					nick += ic->userInfo()->userMode();
					if(ic->userInfo()->isAway())
					{
						nick += TQChar(' ');
						nick += __tr2qs("away");
					}
					nick += TQChar(')');
				} else {
					if(ic->userInfo()->isAway())
					{
						static TQString ugly(" (");
						nick += ugly;
						nick += __tr2qs("away");
						nick += TQChar(')');
					}
				}
				serv = ic->currentServerName();
				if(ic->lagMeter() && (KVI_OPTION_BOOL(KviOption_boolShowLagOnContextDisplay)))
				{
					nick += " ";
					int lll;
					if((lll = ic->lagMeter()->lag()) > 0)
					{
						int llls = lll / 1000;
						int llld = (lll % 1000) / 100;
						int lllc = (lll % 100) / 10;
						KviTQString::appendFormatted(nick,__tr2qs("Lag: %d.%d%d"),llls,llld,lllc);
					} else {
						nick += __tr2qs("Lag: ?.??");
					}
				}
			} else {
				serv = __tr2qs("In progress...");
			}
		}

		p->setPen(KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundHighContrastActive1));
	
		p->setClipRect(KVI_APPLETIRCCONTEXTINDICATORWIDTH + 2,2,width() - (KVI_APPLETIRCCONTEXTINDICATORWIDTH + 4),height() - 4);

		if(height() < 30)
		{
			static TQString xxx(" [");
			serv += xxx;
			serv += nick;
			serv += TQChar(']');
			p->drawText(KVI_APPLETIRCCONTEXTINDICATORWIDTH + 4,16,serv,serv.length());
		} else {
			p->drawText(KVI_APPLETIRCCONTEXTINDICATORWIDTH + 4,16,serv,serv.length());
			p->drawText(KVI_APPLETIRCCONTEXTINDICATORWIDTH + 4,30,nick,nick.length());
		}
	
		p->setClipping(false);

		TQColor base = colorGroup().background();
		TQColor cntx = KVI_OPTION_ICCOLOR(c->ircContextId() % KVI_NUM_ICCOLOR_OPTIONS);
		base.setRgb((base.red() + cntx.red()) >> 1,(base.green() + cntx.green()) >> 1,
			(base.blue() + cntx.blue()) >> 1);
	
		p->fillRect(2,2, KVI_APPLETIRCCONTEXTINDICATORWIDTH - 2,height() - 4,base);
	}

	p->setPen(KVI_OPTION_COLOR(KviOption_colorIrcToolBarAppletForegroundMidContrast));
	p->drawLine(1,1,width() - 1,1);
	p->drawLine(1,1,1,height() - 1);
	p->drawLine(2,height() - 2,width() - 1,height() - 2);
	p->drawLine(width() - 2,1,width() - 2,height());
	p->drawLine(KVI_APPLETIRCCONTEXTINDICATORWIDTH,2,KVI_APPLETIRCCONTEXTINDICATORWIDTH,height() - 2);

}


#ifdef Bool
	#undef Bool
#endif

#include "kvi_irctoolbar.moc"