//
//   File : libkvidockwidget.cpp
//   Creation date : Tue Jan 02 20001 14:34:12 CEST by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2000-2001 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.
//

#include "kvi_settings.h"

#include "kvi_app.h"
#include "kvi_module.h"
#include "kvi_locale.h"
#include "kvi_memmove.h"
#include "kvi_taskbar.h"
#include "kvi_window.h"
#include "kvi_dynamictooltip.h"
#include "kvi_iconmanager.h"
#include "kvi_internalcmd.h"
#include "kvi_console.h"
#include "kvi_ircconnection.h"
#include "kvi_ircconnectionuserinfo.h"
#include "kvi_imagelib.h"
#include "kvi_options.h"
#include "kvi_ircview.h"

#include "libkvidockwidget.h"
#include "kvi_doublebuffer.h"

#ifdef COMPILE_TDE_SUPPORT
	#include <twin.h>
	#include <tdepopupmenu.h>
#else
	#include <tqlabel.h>
#endif

#include "kvi_tal_popupmenu.h"


#include <tqpixmap.h>
#include <tqpainter.h>
#include <tqtimer.h>
#include <tqevent.h>
#include <tqregexp.h>

#include <stdlib.h>
#include <time.h>

#ifdef COMPILE_ON_WINDOWS
	#include <windows.h>

	#include <tqbitmap.h>
	#include <tqcursor.h>

	#define ID_DOCKWIDGET_TASKBAR_ICON 0xdeadbeef
	#define WM_KVIRC_NOTIFY_ICON_MESSAGE (WM_USER + 0xbeef)

	static UINT WM_KVIRC_TASKBAR_CREATED = 0;
#else
#ifndef Q_OS_MACX
	#include <X11/Xlib.h>

	//const int XFocusOut = FocusOut;
	const int XFocusIn = FocusIn;
	#undef FocusOut
	#undef FocusIn
	#undef KeyPress
	#undef KeyRelease

	//#warning "Later remove this stuff and use a wrapper for #include <X11/Xlib.h>"
	#ifdef Bool
		#undef Bool
	#endif
#endif
#endif

extern KVIRC_API KviPointerHashTable<TQString,KviWindow> * g_pGlobalWindowDict;
static KviPointerList<KviDockWidget> * g_pDockWidgetList = 0;

static TQPixmap * g_pDock1 = 0;
static TQPixmap * g_pDock2 = 0;
static TQPixmap * g_pDock3 = 0;

#ifdef COMPILE_ON_WINDOWS
	static HICON g_pCurrentIcon = 0;
	static TQPixmap * g_pCurrentPixmap = 0;
#endif


KviDockWidget::KviDockWidget(KviFrame * frm,const char * name)
: TQWidget(0,name)
{
	m_uConsoles = 0;
	m_uChannels = 0;
	m_uQueries  = 0;
	m_uOther    = 0;
	
	m_pFlashingTimer = new TQTimer(this,"flashing_timer");
	connect( m_pFlashingTimer, TQ_SIGNAL(timeout()), this, TQ_SLOT(flashingTimerShot()) );
	m_bFlashed=0;
	g_pDockWidgetList->append(this);
	m_pFrm = frm;
	m_pFrm->setDockExtension(this);

#ifdef COMPILE_ON_WINDOWS
	// kode54
	if(!WM_KVIRC_TASKBAR_CREATED)WM_KVIRC_TASKBAR_CREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));
	createTaskbarIcon();
#else //!COMPILE_ON_WINDOWS
	setMinimumSize(22,22);
	setBackgroundMode(X11ParentRelative);
	#ifdef COMPILE_TDE_SUPPORT
		KWin::setSystemTrayWindowFor(winId(),frm->winId());
	#endif
#endif //!COMPILE_ON_WINDOWS


	m_pTip = new KviDynamicToolTip(this,"dock_tooltip");
	connect(m_pTip,TQ_SIGNAL(tipRequest(KviDynamicToolTip *,const TQPoint &)),this,TQ_SLOT(tipRequest(KviDynamicToolTip *,const TQPoint &)));
	
	m_pAwayPopup = new KviTalPopupMenu(this);
	
#ifdef COMPILE_TDE_SUPPORT
	m_pContextPopup = new TDEPopupMenu(this);
	m_pContextPopup->insertTitle(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_KVIRC)),__tr2qs("KVIrc"));
#else
	m_pContextPopup = new KviTalPopupMenu(this);
	TQLabel * l = new TQLabel(__tr2qs("KVIrc"),m_pContextPopup);
	l->setFrameStyle(TQFrame::Raised | TQFrame::StyledPanel);
	m_pContextPopup->insertItem(l);
#endif
	m_pContextPopup->setCaption(__tr2qs("Context"));
	m_iAwayMenuId = m_pContextPopup->insertItem ( __tr2qs("Away"), m_pAwayPopup);
	m_pContextPopup->changeItem(m_iAwayMenuId,*(g_pIconManager->getSmallIcon(KVI_SMALLICON_AWAY)),__tr2qs("Away"));
	
	int id = m_pContextPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_OPTIONS)),__tr2qs("&Configure KVIrc..."),m_pFrm,TQ_SLOT(executeInternalCommand(int)));
	m_pContextPopup->setItemParameter(id,KVI_INTERNALCOMMAND_OPTIONS_DIALOG);
	id = m_pContextPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_KVIRC)),__tr2qs("&About KVIrc"),m_pFrm,TQ_SLOT(executeInternalCommand(int)));
	m_pContextPopup->setItemParameter(id,KVI_INTERNALCOMMAND_ABOUT_ABOUTKVIRC);
	m_pContextPopup->insertSeparator();
	m_iToggleFrame = m_pContextPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_RAW)),TQString(""),this,TQ_SLOT(toggleParentFrame()));
	m_pContextPopup->insertSeparator();
	id = m_pContextPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_DOCKWIDGET)),__tr2qs("Un&dock"),m_pFrm,TQ_SLOT(executeInternalCommand(int)));
	m_pContextPopup->setItemParameter(id,KVI_INTERNALCOMMAND_DOCKWIDGET_HIDE);
	id = m_pContextPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_QUITAPP)),__tr2qs("&Quit"),g_pApp,TQ_SLOT(quit()));
	m_pContextPopup->setAccel(__tr2qs("Ctrl+Q"),id);
	connect(m_pContextPopup,TQ_SIGNAL(aboutToShow()),this,TQ_SLOT(fillContextPopup()));
}


KviDockWidget::~KviDockWidget()
{
#ifdef COMPILE_ON_WINDOWS
	destroyTaskbarIcon();
#endif
	m_pFrm->setDockExtension(0);
	g_pDockWidgetList->removeRef(this);
}

void KviDockWidget::die()
{
	delete this;
}
void KviDockWidget::flashingTimerShot()
{
	m_bFlashed=!m_bFlashed;
#ifdef COMPILE_ON_WINDOWS
	updateTaskbarIcon();
#else
	update();
#endif
}

#ifdef COMPILE_ON_WINDOWS
void KviDockWidget::createTaskbarIcon()
{
	ICONINFO inf;
	g_pCurrentPixmap=new TQPixmap(*g_pDock1);
	inf.hbmColor = g_pCurrentPixmap->hbm();
	if(!g_pCurrentPixmap->mask())g_pCurrentPixmap->setMask(g_pCurrentPixmap->createHeuristicMask());
	inf.hbmMask = g_pCurrentPixmap->mask()->hbm();
	g_pCurrentIcon=CreateIconIndirect(&inf);

	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(nid);
	nid.hWnd = winId();
	nid.uID = ID_DOCKWIDGET_TASKBAR_ICON;
	nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
	nid.uCallbackMessage = WM_KVIRC_NOTIFY_ICON_MESSAGE;
	kvi_memmove(nid.szTip,"KVIrc",6);
	nid.hIcon  = g_pCurrentIcon;
	Shell_NotifyIcon(NIM_ADD,&nid);
}

void KviDockWidget::destroyTaskbarIcon()
{
	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(nid);
	nid.hWnd = winId();
	nid.uID = ID_DOCKWIDGET_TASKBAR_ICON;
	nid.uFlags = 0;
	Shell_NotifyIcon(NIM_DELETE,&nid);
}

void KviDockWidget::updateTaskbarIcon()
{
	ICONINFO inf;
	TQPixmap* pix = m_bFlashed ? g_pIconManager->getSmallIcon(KVI_SMALLICON_MESSAGE) : g_pCurrentPixmap;
	inf.hbmColor = pix->hbm();
	if(!pix->mask())pix->setMask(pix->createHeuristicMask());
	inf.hbmMask = pix->mask()->hbm();
	HICON hIcon=CreateIconIndirect(&inf);

	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(nid);
	nid.hWnd = winId();
	nid.uID = ID_DOCKWIDGET_TASKBAR_ICON;
	nid.uFlags = NIF_ICON;
	nid.hIcon = hIcon;
	Shell_NotifyIcon(NIM_MODIFY,&nid);
	DestroyIcon(g_pCurrentIcon);
	g_pCurrentIcon=hIcon;
}

bool KviDockWidget::winEvent(MSG * m)
{
	if(m->message == WM_KVIRC_NOTIFY_ICON_MESSAGE)
	{
		if(m->wParam == ID_DOCKWIDGET_TASKBAR_ICON)
		{
			switch(m->lParam)
			{
				case WM_MOUSEMOVE:
					tipRequest(m_pTip,TQCursor::pos());
					break;
				case WM_LBUTTONDOWN:
					toggleParentFrame();
					break;
				case WM_RBUTTONDOWN:
					m_pContextPopup->popup(TQCursor::pos());
					break;
			}
			return true;
		}
	} else if(m->message == WM_KVIRC_TASKBAR_CREATED)
	{
		// kode54 : when the window shell dies and is restarted
		// it sends the "TaskbarCreated" message to all the toplevel windows
		// We recreate the taskbar icon then
		createTaskbarIcon();
		refresh();
	}
	return false;
}

#endif

#define NIDLEMSGS 18

static const char * idlemsgs[NIDLEMSGS]=
{
	__tr("Nothing is happening...") ,
	__tr("Just idling...") ,
	__tr("Dum de dum de dum...") ,
	__tr("Hey man... do something!") ,
	__tr("Umpf!") ,
	__tr("Silence speaking") ,
	__tr("Are ya here?") ,
	__tr("The world has stopped?") ,
	__tr("Everything is all right") ,
	__tr("idle()") ,
	__tr("It's so cold here...") ,
	__tr("Do not disturb... watching TV") ,
	__tr("Just vegetating") ,
	__tr("Hey... are ya sure that your network is up?") ,
	__tr("Seems like the world has stopped spinning") ,
	__tr("This silence is freaking me out!") ,
	__tr("Mieeeeeowww!") ,
	__tr("idle idle idle idle!")
};

#ifdef COMPILE_TDE_SUPPORT
	extern Time tqt_x_time;
#endif

void KviDockWidget::enterEvent( TQEvent* )
{
#ifdef COMPILE_TDE_SUPPORT
	if(!g_pApp->focusWidget())
	{
		XEvent ev;
		kvi_memset(&ev, 0, sizeof(ev));
		ev.xfocus.display = tqt_xdisplay();
		ev.xfocus.type = XFocusIn;
		ev.xfocus.window = winId();
		ev.xfocus.mode = NotifyNormal;
		ev.xfocus.detail = NotifyAncestor;
		Time time = tqt_x_time;
		tqt_x_time = 1;
		g_pApp->x11ProcessEvent( &ev );
		tqt_x_time = time;
	}
#endif
}

void KviDockWidget::tipRequest(KviDynamicToolTip *tip,const TQPoint &pnt)
{
	TQString tmp;

	KviTaskBarBase * t = m_pFrm->taskBar();

	TQString line;

	for(KviTaskBarItem * b = t->firstItem();b;b = t->nextItem())
	{
	
		if(b->kviWindow()->view())
		{
			if(b->kviWindow()->view()->haveUnreadedMessages())
			{
				line = b->kviWindow()->lastMessageText();
				if(!line.isEmpty())
				{
					line.replace(TQChar('&'),"&amp;");
					line.replace(TQChar('<'),"&lt;");
					line.replace(TQChar('>'),"&gt;");
					tmp += "<b>";
					tmp += b->kviWindow()->plainTextCaption();
					tmp += "</b><br>";
					tmp += line;
					tmp += "<br><br>\n";
				}
			}
		}
	}


	srand(time(0));

	// We use the bad way to generate random numbers :)))))

	if(tmp.isEmpty())tmp = __tr2qs_no_xgettext(idlemsgs[(int)(rand() % NIDLEMSGS)]);

#ifdef COMPILE_ON_WINDOWS

#else
	m_pTip->tip(rect(),tmp);
#endif
}

//int KviDockWidget::message(int,void *)
//{
//	tqDebug("Message");
//	update();
//	return 0;
//}

void KviDockWidget::mousePressEvent(TQMouseEvent *e)
{
	if(e->button() & TQt::LeftButton)toggleParentFrame();
	else if(e->button() & TQt::RightButton)
		m_pContextPopup->popup(mapToGlobal(e->pos()));
}

void KviDockWidget::doAway(int id)
{
	if(id<0)
	{
		KviPointerHashTableIterator<TQString,KviWindow> it(*g_pGlobalWindowDict);
		while(KviWindow * wnd = it.current())
		{
			if(wnd->type()==KVI_WINDOW_TYPE_CONSOLE)
			{
				KviConsole* pConsole=(KviConsole*)wnd;
				if(pConsole->isConnected())
					if(id==-2)
						pConsole->connection()->sendFmtData("AWAY");
					else
						pConsole->connection()->sendFmtData("AWAY :%s",
							pConsole->connection()->encodeText(KVI_OPTION_STRING(KviOption_stringAwayMessage)).data()
							);
			}
 			++it;
		}
	} else {
		KviConsole* pConsole=g_pApp->findConsole((unsigned int)id);
		if(pConsole)
			if(pConsole->isConnected())
				if(pConsole->connection()->userInfo()->isAway())
					pConsole->connection()->sendFmtData("AWAY");
				else
					pConsole->connection()->sendFmtData("AWAY :%s",
						pConsole->connection()->encodeText(KVI_OPTION_STRING(KviOption_stringAwayMessage)).data()
						);
	}
}

void KviDockWidget::fillContextPopup()
{
	m_pContextPopup->changeItem(m_iToggleFrame,m_pFrm->isVisible() ? __tr2qs("Hide Window") : __tr2qs("Show Window"));
	if(g_pApp->topmostConnectedConsole())
	{
		m_pContextPopup->setItemVisible(m_iAwayMenuId,true);
		m_pAwayPopup->clear();
		
		int iAllAway=m_pAwayPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_CONSOLE)),__tr2qs("Away on all"),this,TQ_SLOT(doAway(int)));
		m_pAwayPopup->setItemParameter(iAllAway,-1);
		
		int iAllUnaway=m_pAwayPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_CONSOLE)),__tr2qs("Back on all"),this,TQ_SLOT(doAway(int)));
		m_pAwayPopup->setItemParameter(iAllUnaway,-2);
		
		int iSeparator=m_pAwayPopup->insertSeparator();
		
		KviPointerHashTableIterator<TQString,KviWindow> it(*g_pGlobalWindowDict);
		bool bAllAway=1;
		bool bAllUnaway=1;
		int iNetCount=0;
		while(KviWindow * wnd = it.current())
		{
			if(wnd->type()==KVI_WINDOW_TYPE_CONSOLE)
			{
				KviConsole* pConsole=(KviConsole*)wnd;
				if(pConsole->isConnected())
				{
					int id;
					if(pConsole->connection()->userInfo()->isAway())
					{
						id=m_pAwayPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_CONSOLE)),__tr2qs("Back on %1").arg(pConsole->currentNetworkName()),this,TQ_SLOT(doAway(int)));
						bAllUnaway=0;
					} else {
						id=m_pAwayPopup->insertItem(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_CONSOLE)),__tr2qs("Away on %1").arg(pConsole->currentNetworkName()),this,TQ_SLOT(doAway(int)));
						bAllAway=0;
					}
					m_pAwayPopup->setItemParameter(id,pConsole->ircContextId());
					iNetCount++;
				}
			}
 			++it;
		}
		if(iNetCount==1)
		{
			m_pAwayPopup->setItemVisible(iAllAway,false);
			m_pAwayPopup->setItemVisible(iAllUnaway,false);
			m_pAwayPopup->setItemVisible(iSeparator,false);
		} else {
			m_pAwayPopup->setItemVisible(iAllAway,!bAllAway);
			m_pAwayPopup->setItemVisible(iAllUnaway,!bAllUnaway);
		}
	} else {
		m_pContextPopup->setItemVisible(m_iAwayMenuId,false);
	}
}

void KviDockWidget::toggleParentFrame()
{
	TQWidget *top_widget = m_pFrm->topLevelWidget();

	if(m_pFrm->isVisible()) {
		m_pFrm->hide();
	} else {
		top_widget->show();
		top_widget->raise();
		top_widget->setActiveWindow();
		if(g_pActiveWindow) g_pActiveWindow->setFocus();
		if(m_pFrm->isMinimized())
			m_pFrm->showNormal();
		else
			m_pFrm->show();
	}
}

void KviDockWidget::refresh()
{
	grabActivityInfo();

#ifdef COMPILE_ON_WINDOWS
	// how to copy transparency????
	delete g_pCurrentPixmap;
	g_pCurrentPixmap=new TQPixmap(16,16);
	bitBlt(g_pCurrentPixmap,0,0,m_uOther ? ((m_uOther == 2) ? g_pDock3 : g_pDock2) : g_pDock1,0,0,8,8,TQt::ClearROP);
	bitBlt(g_pCurrentPixmap,0,8,m_uConsoles ? ((m_uConsoles == 2) ? g_pDock3 : g_pDock2) : g_pDock1,0,8,8,8,TQt::ClearROP);
	bitBlt(g_pCurrentPixmap,8,0,m_uQueries ? ((m_uQueries == 2) ? g_pDock3 : g_pDock2) : g_pDock1,8,0,8,8,TQt::ClearROP);
	bitBlt(g_pCurrentPixmap,8,8,m_uChannels ? ((m_uChannels == 2) ? g_pDock3 : g_pDock2) : g_pDock1,8,8,8,8,TQt::ClearROP);
#endif
	
	if( (m_uChannels == 2) || (m_uQueries == 2) )
	{
		if(!m_pFlashingTimer->isActive() && KVI_OPTION_BOOL(KviOption_boolEnableTrayIconFlashing) ) m_pFlashingTimer->start(1000);
	} else {
		if(m_pFlashingTimer->isActive()) m_pFlashingTimer->stop();
		m_bFlashed=false;
	}
	
#ifdef COMPILE_ON_WINDOWS
	updateTaskbarIcon();
#else
	update();
#endif
}

void KviDockWidget::grabActivityInfo()
{
	KviTaskBarBase * t = m_pFrm->taskBar();
	
	if(KVI_OPTION_BOOL(KviOption_boolUseLevelBasedTrayNotification))
	{
		if(KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)>5) KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)=5;
		if(KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)>5) KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)=5;
		
		if(KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)<1) KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)=1;
		if(KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)<1) KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)=1;
		
		if(KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)<KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage))
			KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)=KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage);
	}

	m_uConsoles = 0;
	m_uChannels = 0;
	m_uQueries  = 0;
	m_uOther    = 0;

	for(KviTaskBarItem * b = t->firstItem();b;b = t->nextItem())
	{
		if(KVI_OPTION_BOOL(KviOption_boolUseLevelBasedTrayNotification))
		{
			unsigned int uLevel = static_cast<unsigned int>(b->highlightLevel());
			switch(b->kviWindow()->type())
			{
				case KVI_WINDOW_TYPE_CONSOLE:
					if(m_uConsoles < uLevel) m_uConsoles = uLevel;
				break;
				case KVI_WINDOW_TYPE_CHANNEL:
					if(m_uChannels < uLevel) m_uChannels = uLevel;
				break;
				case KVI_WINDOW_TYPE_QUERY:
					if(m_uQueries < uLevel) m_uQueries = uLevel;
				break;
				default:
					if(m_uOther < uLevel) m_uOther = uLevel;
				break;
			}
		} else {
			int uLevel=0;
			if(b->kviWindow()->view())
			{
				if(b->kviWindow()->view()->haveUnreadedHighlightedMessages())
				{
					uLevel=2;
				} else if(b->kviWindow()->view()->haveUnreadedMessages())
				{
					uLevel=1;
				}
				if(uLevel>0)
				switch(b->kviWindow()->type())
				{
					case KVI_WINDOW_TYPE_CONSOLE:
						if(m_uConsoles < uLevel) m_uConsoles = uLevel;
					break;
					case KVI_WINDOW_TYPE_CHANNEL:
						if(m_uChannels < uLevel) m_uChannels = uLevel;
					break;
					case KVI_WINDOW_TYPE_QUERY:
						if(m_uQueries < uLevel) m_uQueries = uLevel;
					break;
					default:
						if(m_uOther < uLevel) m_uOther = uLevel;
					break;
				}
			}
		}
	}
	
	if(KVI_OPTION_BOOL(KviOption_boolUseLevelBasedTrayNotification))
	{
		if(m_uConsoles >= KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)) m_uConsoles=2;
		else if(m_uConsoles >= KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)) m_uConsoles=1;
		else m_uConsoles=0;
		
		if(m_uChannels >= KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)) m_uChannels=2;
		else if(m_uChannels >= KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)) m_uChannels=1;
		else m_uChannels=0;
		
		if(m_uQueries >= KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)) m_uQueries=2;
		else if(m_uQueries >= KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)) m_uQueries=1;
		else m_uQueries=0;
		
		if(m_uOther >= KVI_OPTION_UINT(KviOption_uintMinTrayHighLevelMessage)) m_uOther=2;
		else if(m_uOther >= KVI_OPTION_UINT(KviOption_uintMinTrayLowLevelMessage)) m_uOther=1;
		else m_uOther=0;
	}
}

void KviDockWidget::paintEvent(TQPaintEvent * event)
{
	if(m_bFlashed)
	{
		erase();
		bitBlt(this,4,4,g_pIconManager->getSmallIcon(KVI_SMALLICON_MESSAGE),0,0,16,16);
	} else {
		bitBlt(this,0,0,m_uOther ? ((m_uOther == 2) ? g_pDock3 : g_pDock2) : g_pDock1,0,0,12,12,TQt::CopyROP,false);
		bitBlt(this,0,12,m_uConsoles ? ((m_uConsoles == 2) ? g_pDock3 : g_pDock2) : g_pDock1,0,12,12,12,TQt::CopyROP,false);
		bitBlt(this,12,0,m_uQueries ? ((m_uQueries == 2) ? g_pDock3 : g_pDock2) : g_pDock1,12,0,12,12,TQt::CopyROP,false);
		bitBlt(this,12,12,m_uChannels ? ((m_uChannels == 2) ? g_pDock3 : g_pDock2) : g_pDock1,12,12,12,12,TQt::CopyROP,false);
	}
}


static KviDockWidget * dockwidget_find(KviFrame *f)
{
	if(!g_pDockWidgetList)return 0;
	for(KviDockWidget * w = g_pDockWidgetList->first();w;w = g_pDockWidgetList->next())
	{
		if(w->frame() == f)return w;
	}
	return 0;
}

/*
	@doc: dockwidget.show
	@type:
		command
	@title:
		dockwidget.show
	@short:
		Shows the dock widget for the current frame window
	@keyterms:
		dock widget , system tray
	@syntax:
		dockwidget.show
	@description:
		Shows the dock widget for the current frame window.[br]
		The dock widget is a small widget that docks in the TDE panel.[br]
		It shows a small icon of the earth and eventually displays four squares
		that cover this icon: the bottom left square appears when there is some new
		text in any console window, the square becomes red if the text is highlighted.[br]
		The bottom right square appears when there is some new text in any channel window,
		and it becomes red when the text is highlighted.[br] The upper right square refers to
		query windows and the upper left one to any other kind of window (dcc , links...).[br]
		If you move the mouse over the dock widget a tooltip will show you the last lines
		of the "new" text in all these windows.[br]
		This is useful when you keep the main KVIrc window minimized and you're working on something else:
		if the dock widget shows nothing but the earth icon , nothing is happening in the main KVIrc window.
		If the dock widget shows one or more white (or red) squares , you can move the mouse over
		and check what's happened exactly and eventually bring up the main KVIrc window by clicking on the widget.[br]
		[big]tecnical details[/big]
		The dock widget is currently working in KDE compilation mode only:
		it relies on the KWin implementation of the Window Manager interaction protocol.
	@seealso:
		[cmd]dockwidget.hide[/cmd]
*/

static bool dockwidget_kvs_cmd_show(KviKvsModuleCommandCall * c)
{
	if(!(dockwidget_find(c->window()->frame())))
	{
		KviDockWidget * w = new KviDockWidget(c->window()->frame(),"dock_widget");
#ifndef COMPILE_ON_WINDOWS
		w->show();
#else
		w->hide();
#endif
	}
	return true;
}

/*
	@doc: dockwidget.hide
	@type:
		command
	@title:
		dockwidget.hide
	@short:
		Hides the dock widget for the current frame window
	@syntax:
		dockwidget.hide
	@description:
		Hides the dock widget for the current frame window
	@seealso:
		[cmd]dockwidget.show[/cmd]
*/

static bool dockwidget_kvs_cmd_hide(KviKvsModuleCommandCall * c)
{
	KviDockWidget * w= dockwidget_find(c->window()->frame());
	if(w)delete w;
	// show the parent frame.. otherwise there will be no way to get it back
	if(!c->window()->frame()->isVisible())
	{
		c->window()->frame()->show();
	}
	return true;
}

/*
	@doc: dockwidget.hidewindow
	@type:
		command
	@title:
		dockwidget.hidewindow
	@short:
		Returns the state of the dock widget
	@syntax:
		dockwidget.hidewindow
	@description:
		Hides the window, assotiated with dockwidget
	@seealso:
		[cmd]dockwidget.show[/cmd], [cmd]dockwidget.hide[/cmd]
*/

static bool dockwidget_kvs_cmd_hidewindow(KviKvsModuleCommandCall * c)
{
	KviDockWidget * w= dockwidget_find(c->window()->frame());
	if(w)
	{
		w->setPrevWindowState(c->window()->frame()->windowState());
		c->window()->frame()->hide();
	}
	return true;
}

/*
	@doc: dockwidget.isVisible
	@type:
		function
	@title:
		$dockwidget.isVisible
	@short:
		Returns the state of the dock widget
	@syntax:
		$reguser.isVisible()
	@description:
		Returns 1 if the dock widget is actually visible , 0 otherwise.
	@seealso:
		[cmd]dockwidget.show[/cmd]
*/

static bool dockwidget_kvs_fnc_isvisible(KviKvsModuleFunctionCall * c)
{
	c->returnValue()->setBoolean(dockwidget_find(c->window()->frame()));
	return true;
}



// =======================================
// init routine
// =======================================
static bool dockwidget_module_init(KviModule * m)
{
	KviStr buffer;
#ifdef COMPILE_ON_WINDOWS
	g_pApp->findImage(buffer,"kvi_dock_win32.png");
	KviImageLibrary l1(buffer.ptr(),16,16);
#else
	g_pApp->findImage(buffer,"kvi_dock.png");
	KviImageLibrary l1(buffer.ptr(),22,22);
#endif
	g_pDock1 = new TQPixmap(l1.getImage(0));
	g_pDock2 = new TQPixmap(l1.getImage(1));
	g_pDock3 = new TQPixmap(l1.getImage(2));

#ifdef COMPILE_ON_WINDOWS
	
#endif


	g_pDockWidgetList = new KviPointerList<KviDockWidget>;
	g_pDockWidgetList->setAutoDelete(false);


	KVSM_REGISTER_SIMPLE_COMMAND(m,"hide",dockwidget_kvs_cmd_hide);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"hidewindow",dockwidget_kvs_cmd_hidewindow);
	KVSM_REGISTER_SIMPLE_COMMAND(m,"show",dockwidget_kvs_cmd_show);
	KVSM_REGISTER_FUNCTION(m,"isVisible",dockwidget_kvs_fnc_isvisible);

	return true;
}

static bool dockwidget_module_cleanup(KviModule *m)
{
	while(g_pDockWidgetList->first())delete g_pDockWidgetList->first();
	delete g_pDockWidgetList;
	g_pDockWidgetList = 0;

#ifdef COMPILE_ON_WINDOWS
	DestroyIcon(g_pCurrentIcon);
#endif

	delete g_pDock1;
	g_pDock1 = 0;
	delete g_pDock2;
	g_pDock2 = 0;
	delete g_pDock3;
	g_pDock3 = 0;

	return true;
}

static bool dockwidget_module_can_unload(KviModule *)
{
	return g_pDockWidgetList->isEmpty();
}

// =======================================
// plugin definition structure
// =======================================
KVIRC_MODULE(
	"KVIrc dock widget implementation",
	"1.0.0",
	"Szymon Stefanek <pragma at kvirc dot net>" ,
	"exports the /dockwidget.* interface\n",
	dockwidget_module_init ,
	dockwidget_module_can_unload,
	0,
	dockwidget_module_cleanup
)

#include "libkvidockwidget_qt3.moc"

