#include "kbiconlist.h"
#include <iostream.h>

// OLE
// #ifndef BIGHEIGHT
// #define BIGHEIGHT	80
// #endif
// OLE

#include <qbitmap.h>
// OLE  resized mask_bits from 32x32 to 80x60
#define mask_width 80
#define mask_height 60
static unsigned char mask_bits[] = {
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
   0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
// OLE

KBigIconList::KBigIconList(bool showMiniImages, QWidget *parent, const char *name, WFlags f)
	: KFileListBase(parent,name,f)
{
	setTableFlags(Tbl_vScrollBar | Tbl_smoothScrolling);
	setLineWidth(1);
	setFrameStyle(QFrame::Panel | QFrame::Sunken);
// OLE
	ShowMiniImages=showMiniImages;
	if (ShowMiniImages)
	  {
	  setCellHeight(HEIGHT_WITH_MINI_IMAGES);
	  setCellWidth(WIDTH_WITH_MINI_IMAGES);
	  }
	else
	  {
	  setCellHeight(HEIGHT_WITHOUT);
          setCellWidth(WIDTH_WITHOUT);
	  }
// OLE
	Mask.resize(mask_width,mask_height);
	Mask.fill(backgroundColor());
	QBitmap	bm(mask_width,mask_height,mask_bits);
	Mask.setMask(bm);
}

// OLE
void KBigIconList::paintCell(QPainter *p, int row, int col)
{
	int		index = row*numCols()+col;
	FileInfo	*item = index < (int)(FileList->count()) ? FileList->at(index) : 0;
	
	if (item) {
	        QPixmap *icon = item->icon();

// MG
		if (ShowMiniImages) {
			emit requestMiniImage(item);
			if (!item->miniImage()->isNull()) icon = item->miniImage();
		}
// MG

		// Icon or MiniImage ?
/*      		icon = ((ShowMiniImages &&  item->fetchMiniImage())
		    ?
		    (icon = item->miniImage())
		    :
       		    (icon = item->icon()));*/

		if (Selected->findRef(item) != -1) {
			p->setBrush(darkBlue);

			p->drawRect(BORDER_DIST, cellHeight()-TEXT_BOX_ABOVE_BOTTOM_BORDER,
				   cellWidth()-(BORDER_DIST<<1), TEXT_BOX_HEIGHT);

			p->setBrush(NoBrush);

			p->drawWinFocusRect(BORDER_DIST, cellHeight()-TEXT_BOX_ABOVE_BOTTOM_BORDER,
				   cellWidth()-(BORDER_DIST<<1), TEXT_BOX_HEIGHT);

			p->setPen(colorGroup().base());
			p->setRasterOp(NotROP);
		}
		QFontMetrics	fm(font());
	 	int	type = 0;
		if (fm.width(item->fileName().data()) > cellWidth()-(BORDER_DIST<<1))
			type = AlignLeft | AlignVCenter;
		else
			type = AlignCenter;

		p->drawText(BORDER_DIST,cellHeight()-TEXT_BOX_ABOVE_BOTTOM_BORDER,
				cellWidth()-(BORDER_DIST<<1), TEXT_BOX_HEIGHT,
				type,item->fileName().data());
		if (Selected->findRef(item) != -1) p->setRasterOp(CopyROP);

		int icx, icy, ich, icw;

		icx = QMAX((cellWidth()-icon->width())>>1, BORDER_DIST);
	        icy = QMAX(cellHeight()-icon->height()-ICON_ABOVE_BOTTOM_BORDER, BORDER_DIST);
                ich = QMIN(icon->height(),cellHeight()-BORDER_DIST-TEXT_BOX_ABOVE_BOTTOM_BORDER);
		icw = QMIN(icon->width(), cellWidth()-(BORDER_DIST<<1));

		p->drawPixmap(icx, icy, *icon,0,0,icw,ich);

		if (item->isSymLink() && LinkArrow) {
		           p->drawPixmap(icx,
			               cellHeight()-(ICON_ABOVE_BOTTOM_BORDER)-(LINK_ARROW_HEIGHT),
				      *LinkArrow);
                }		
		
		if (item->isCutted()) p->drawPixmap(icx,icy,Mask,0,0,icw, ich);
		
		p->setPen(colorGroup().text());
	}

}
// OLE

void KBigIconList::resizeEvent(QResizeEvent *)
{
	setNumCols(QMAX(viewWidth()/cellWidth(),1));
	if (FileList) setNumRows(FileList->count()/numCols()+1);
	else setNumRows(0);
	QTableView::resizeEvent(0);
}

void KBigIconList::getTablePosition(int index, int& row, int& col)
{
	row = index / numCols();
	col = index - row*numCols();
}

bool KBigIconList::getItemCell(int row, int col, FileInfo **item, QPoint p)
{
	bool	selected = FALSE;
	*item = 0;
	int	index = row*numCols()+col;
	if (FileList == 0 || index >= (int)(FileList->count())) return selected;
	*item = FileList->at(index);
// OLE
	QPixmap *icon;

	// Icon or MiniImage ?
        icon = ((ShowMiniImages && (!(*item)->miniImage()->isNull()))
	      ?
	      (icon = (*item)->miniImage())
	      :
	      ((*item)->icon()));
	
	int     icx = QMAX(((cellWidth()-icon->width())>>1), BORDER_DIST),
	        icy = QMAX((cellHeight()-icon->height()-ICON_ABOVE_BOTTOM_BORDER), BORDER_DIST),
                ich = QMIN(icon->height(),cellHeight()-BORDER_DIST-TEXT_BOX_ABOVE_BOTTOM_BORDER),
		icw = QMIN(icon->width(), cellWidth()-(BORDER_DIST<<1));

	QRect	ic = QRect(col*cellWidth()+lineWidth()+icx-xOffset(),
			 row*cellHeight()+lineWidth()+icy-yOffset(), 	
			icw, 			
			ich),		
	        te = QRect(col*cellWidth()+lineWidth()+BORDER_DIST-xOffset(), 			
			(row+1)*cellHeight()+lineWidth()-TEXT_BOX_ABOVE_BOTTOM_BORDER-yOffset(), 		
   			cellWidth()-(BORDER_DIST<<1),
			TEXT_BOX_HEIGHT);

//	QRect	ic = QRect(col*cellWidth()+lineWidth()+24-xOffset(),row*cellHeight()+lineWidth()+12-yOffset(),32,32),
//		te =QRect(col*cellWidth()+lineWidth()+4-xOffset(),row*cellHeight()+lineWidth()+48-yOffset(),cellWidth()-4,20);
// OLE
	if (ic.contains(p) || te.contains(p))
		selected = TRUE;
	return selected;
}

void KBigIconList::updateListSize()
{
	if (FileList && FileList->count() > 0)
		setNumRows(FileList->count()/numCols()+1);
	else setNumRows(0);
}

QRect KBigIconList::getItemTextRect(FileInfo *item)
{
	int	row,col;
	getTablePosition(item->index(),row,col);
// OLE
	QRect	r( col   *cellWidth()+ lineWidth()+BORDER_DIST-xOffset(),
		  (row+1)*cellHeight()+lineWidth()-TEXT_BOX_ABOVE_BOTTOM_BORDER-yOffset(),
		  cellWidth()-(BORDER_DIST<<1),
		  TEXT_BOX_HEIGHT);
//	QRect	r(col*cellWidth()+lineWidth()+4-xOffset(),row*cellHeight()+lineWidth()+48-yOffset(),
//		     cellWidth()-4,20);

// OLE
	return r;
}
