void GroupListEmoticons::CreateToolTips()
{
	int top = 0;

	for(int i = 0; i < num_groups; i++)
	{
		Group &group = groups[i];
		
		if (group.name[0] != '\0')
		{
			RECT rc = CalcRect(group.name, groupFont);
			top += HeightWithBorders(rc);
		}

		int index = 0;
		for (int j = group.start; j <= group.end; j++)
		{
			Emoticon *e = ssd->module->emoticons[j];
			if (e->IgnoreFor(ssd->module))
				continue;

			RECT rc = GetEmoticonRect(group, index);
			rc.top += top;
			rc.bottom += top;
			CreateEmoticonToolTip(e, rc);
			index++;
		}

		top += group.max_height * group.lines + (group.lines + 1) * BORDER;
	}
}
int GroupListEmoticons::GetIndex(Group &group, int line, int col)
{
	int desired = line * group.cols + col;
	int index = 0;
	for (int j = group.start; j <= group.end; j++)
	{	
		Emoticon *e = ssd->module->emoticons[j];
		if (e->IgnoreFor(ssd->module))
			continue;

		if (index == desired)
			return j;

		index++;
	}
	return -1;
}
void GroupListEmoticons::GetMaxEmoticonSize(Group &group)
{
	group.max_height = 4;
	group.max_width = 4;

	for(int i = group.start; i <= group.end; i++)
	{
		Emoticon *e = ssd->module->emoticons[i];
		if (e->IgnoreFor(ssd->module))
			continue;

		int height, width;
		GetEmoticonSize(e, width, height);

		group.max_height = max(group.max_height, height);
		group.max_width = max(group.max_width, width);
	}
}
int GroupListEmoticons::CountGroups()
{
	int groups = 1;
	char *current_group = ssd->module->emoticons[0]->group;
	for(int i = 1; i < ssd->module->emoticons.getCount(); i++)
	{
		Emoticon *e = ssd->module->emoticons[i];
		if (e->IgnoreFor(ssd->module))
			continue;

		if (stricmp(e->group, current_group) != 0)
		{
			current_group = e->group;
			groups++;
		}
	}
	return groups;
}
void GroupListEmoticons::Load()
{
	selection = -1;

	// Load fonts
	groupFont = mir_font_get(_T("Emoticons"), _T("Group Name"), &groupColor);
	emoticonFont = mir_font_get(_T("Emoticons"), _T("Emoticons Text"), &emoticonColor);
	groupBkgColor = mir_color_get(_T("Emoticons"), _T("Group Background"));
	ssd->background = mir_color_get(_T("Emoticons"), _T("Emoticons Background"));

	num_groups = CountGroups();
	groups = (Group *) malloc(num_groups * sizeof(Group));

	char *current_group = ssd->module->emoticons[0]->group;

	int current_id = -1;
	int i;
	for(i = 0; i < ssd->module->emoticons.getCount(); i++)
	{
		Emoticon *e = ssd->module->emoticons[i];
		if (e->IgnoreFor(ssd->module))
			continue;

		if (stricmp(e->group, current_group) != 0 || i == 0)
		{
			if (i != 0)
				groups[current_id].end = i - 1;

			current_group = e->group;
			current_id++;

			SetGroupName(groups[current_id], current_group);
			groups[current_id].start = i;
			groups[current_id].count = 1;
		}
		else 
		{
			groups[current_id].count++;
		}
	}
	groups[current_id].end = i - 1;

	// First calc the width
	window.width = 0;
	for(i = 0; i < num_groups; i++)
	{
		Group &group = groups[i];
		GetMaxEmoticonSize(group);
		group.cols = GetNumOfCols(group.count);

		if (group.name[0] != _T('\0'))
		{
			RECT rc = CalcRect(group.name, groupFont);
			window.width = max(window.width, rc.right - rc.left + 2 * BORDER + 1);
		}

		window.width = max(window.width, group.max_width * group.cols + (group.cols + 1) * BORDER);
	}

	// Now calc the height
	window.height = 0;
	for(i = 0; i < num_groups; i++)
	{
		Group &group = groups[i];
		group.top = window.height;

		int w = window.width - BORDER;

		// Recalc the num of cols
		group.lines = GetNumOfLines(group.count, group.cols);
		
		int new_cols = w / (group.max_width + BORDER);
		int new_lines = GetNumOfLines(group.count, new_cols);
		if (new_lines < group.lines)
		{
			group.cols = new_cols;
			group.lines = new_lines;
		}


		// If there is space left, put it into the emoticons
		group.max_width += (w - group.cols * (group.max_width + BORDER)) / group.cols;

		if (group.name[0] != '\0')
		{
			RECT rc = CalcRect(group.name, groupFont);
			window.height += HeightWithBorders(rc);
		}

		window.height += group.max_height * group.lines + (group.lines + 1) * BORDER;
	}
}
void GroupListEmoticons::Draw(HDC hdc)
{
	EraseBackground(hdc);

	RECT client_rc;
	GetClientRect(hwnd, &client_rc);

	COLORREF old_text_color = SetTextColor(hdc, emoticonColor);

	int top = 0;

	for(int i = 0; i < num_groups; i++)
	{
		Group &group = groups[i];
		
		if (group.name[0] != '\0')
		{
			RECT rc = CalcRect(group.name, groupFont);
			rc.top += top + BORDER;
			rc.bottom += top + BORDER;

			int width = rc.right - rc.left;
			rc.left = client_rc.left + (client_rc.right - client_rc.left - width) / 2;
			rc.right = rc.left + width;

			RECT title_rc = client_rc;
			title_rc.top = rc.top - BORDER;
			title_rc.bottom = rc.bottom + BORDER + 1;
			HBRUSH hB = CreateSolidBrush(groupBkgColor);
			FillRect(hdc, &title_rc, hB);
			DeleteObject(hB);

			SetTextColor(hdc, groupColor);

			DrawEmoticonText(hdc, group.name, rc, groupFont);

			SetTextColor(hdc, emoticonColor);

			top += HeightWithBorders(rc);
		}

		int index = 0;
		for (int j = group.start; j <= group.end; j++)
		{	
			Emoticon *e = ssd->module->emoticons[j];
			if (e->IgnoreFor(ssd->module))
				continue;

			RECT rc = GetEmoticonRect(group, index);
			rc.top += top;
			rc.bottom += top;

			DrawEmoticon(hdc, j, rc);

			index++;
		}

		top += group.max_height * group.lines + (group.lines + 1) * BORDER;
	}

	SetTextColor(hdc, old_text_color);
}