CL_Rect CL_ListViewLayoutIcons::get_icon_rect(const CL_Rect &cell_content_rect, CL_ListViewItem item, int offset_x)
{
	int icon_x = cell_content_rect.get_center().x - size_icon.width/2;
	int icon_y = cell_content_rect.get_center().y - size_icon.height/2;
	CL_Rect rect_icon(CL_Point(icon_x, icon_y), size_icon);
	return rect_icon;
}
void CL_ListViewIcon::draw(CL_GraphicContext &gc, const CL_Rect &rect, CL_ListViewDisplayMode mode, const CL_Colorf &color)
{
	CL_Sprite sp = get_sprite(mode);
	if (!sp.is_null())
	{
		float scale = 1.0;

		sp.set_color(color);
		//		if (mode == listview_mode_thumbnails && sp.get_frame_size(0) != rect.get_size())
		if (impl->scalable && (sp.get_frame_size(0) != rect.get_size()))
		{
			// Scale to max vertically or horizontally.
			float sx, sy;
			sx = rect.get_width()/float(sp.get_width());
			sy = rect.get_height()/(float)sp.get_height();
			if (sx <= 0 || sy <= 0)
			{
				return;
			}
			scale = cl_min(sx, sy);
			sp.set_scale(scale,scale);
		}

		CL_Rect R = rect;
		CL_Point offset = get_offset(mode);

		R.translate(offset);

		if (!impl->scalable && offset == CL_Point(0,0))
		{
			// center in cell rect.
			int center_offset_x = int((float)rect.get_center().x - (float)rect.left - scale*(float)sp.get_width()/2.0); 
			int center_offset_y = int((float)rect.get_center().y - (float)rect.top - scale*(float)sp.get_height()/2.0); 
			R.left += center_offset_x;
			R.top += center_offset_y;
		}

		if (!impl->scalable)
			sp.draw(gc, (float)R.left, (float)R.top);
		else
			sp.draw(gc, R);

		return;
	}

	CL_PixelBuffer pb = get_pixel_buffer(mode);
	if (!pb.is_null())
	{
		float scale = 1.0;
		float center_offset_x = 0, center_offset_y = 0;

//		if (mode == listview_mode_thumbnails && pb.get_size() != rect.get_size())
		if (impl->scalable && (pb.get_size() != rect.get_size()))
		{
			float sx = 1.0, sy = 1.0;

			// Scale to max vertically or horizontally.
			sx = rect.get_width()/float(pb.get_width());
			sy = rect.get_height()/(float)pb.get_height();
			if (sx <= 0 || sy <= 0)
			{
				return;
			}

			scale = cl_min(sx, sy);

			// center in the rect.
			center_offset_x = (float)rect.get_center().x - (float)rect.left - scale*(float)pb.get_width()/2.0f; 
			center_offset_y = (float)rect.get_center().y - (float)rect.top - scale*(float)pb.get_height()/2.0f; 
		}

		CL_Point offset = get_offset(mode);

		gc.draw_pixels(rect.left + center_offset_x + offset.x, rect.top + center_offset_y + offset.y, scale, scale, pb, pb.get_size(), color);
	}
}