void
JXTabGroup::Draw
	(
	JXWindowPainter&	p,
	const JRect&		rect
	)
{
	const JRect ap = GetAperture();

	p.SetFont(itsFontName, itsFontSize, itsFontStyle);
	const JSize lineHeight = p.GetLineHeight();
	const JSize tabHeight  = 2*(kBorderWidth + kTextMargin) + lineHeight;

	JIndex selIndex;
	JRect selRect;
	const JBoolean hasSelection = itsCardFile->GetCurrentCardIndex(&selIndex);

	itsTabRects->RemoveAll();
	itsCanScrollUpFlag   = JI2B(itsFirstDrawIndex > 1);
	itsCanScrollDownFlag = kJFalse;

	const JCoordinate scrollArrowWidth = 2*(kArrowWidth + kBorderWidth);

	const JSize count = itsTitles->GetElementCount();
	itsLastDrawIndex  = JMax(count, itsFirstDrawIndex);

	const JColormap* cmap = p.GetColormap();
	if (itsEdge == kTop)
		{
		JRect r(ap.top + kSelMargin,             ap.left + kSelMargin,
				ap.top + kSelMargin + tabHeight, ap.left + kSelMargin);

		for (JIndex i=itsFirstDrawIndex; i<=count; i++)
			{
			const JString* title = itsTitles->NthElement(i);
			const JBoolean isSel = JI2B(hasSelection && i == selIndex);
			const TabInfo info   = itsTabInfoList->GetElement(i);

			r.right += 2*kBorderWidth + info.preMargin +info.postMargin + p.GetStringWidth(*title);
			if (info.closable)
				{
				r.right += kCloseMarginWidth + itsCloseImage->GetWidth();
				}
			JPoint titlePt(r.left + kBorderWidth + info.preMargin,
						   r.top  + kBorderWidth + kTextMargin);
			if (isSel)
				{
//				titlePt.y -= kSelMargin;
				r.top     -= kSelMargin;
				r.Expand(kSelMargin, 0);

				selRect = r;
				}

			if (isSel)
				{
				p.SetPenColor(cmap->GetGrayColor(kSelGrayPercentage));
				p.SetFilling(kJTrue);
				p.JPainter::Rect(r);
				p.SetFilling(kJFalse);
				}
			else
				{
				DrawTabBorder(p, r, kJFalse);
				}
			p.JPainter::String(titlePt, *title);

			itsTabRects->AppendElement(r);
			if (isSel)
				{
				r.top += kSelMargin;
				r.Shrink(kSelMargin, 0);
				}

			r.Shrink(kBorderWidth, kBorderWidth);
			DrawTab(i, p, r, itsEdge);
			DrawCloseButton(i, p, r);
			r.Expand(kBorderWidth, kBorderWidth);

			if (r.right >= ap.right - scrollArrowWidth)
				{
				if (itsFirstDrawIndex == 1 && i == count && r.right <= ap.right)
					{
					break;
					}
				itsCanScrollDownFlag = JI2B( itsFirstDrawIndex < count );
				itsLastDrawIndex     = i;
				if (r.right > ap.right - scrollArrowWidth && i > itsFirstDrawIndex)
					{
					itsLastDrawIndex--;
					}
				break;
				}

			r.left = r.right;
			}
		}

	else if (itsEdge == kLeft)
		{
		JRect r(ap.bottom - kSelMargin, ap.left + kSelMargin,
				ap.bottom - kSelMargin, ap.left + kSelMargin + tabHeight);

		for (JIndex i=itsFirstDrawIndex; i<=count; i++)
			{
			const JString* title = itsTitles->NthElement(i);
			const JBoolean isSel = JI2B(hasSelection && i == selIndex);
			const TabInfo info   = itsTabInfoList->GetElement(i);

			r.top -= 2*kBorderWidth + info.preMargin + info.postMargin + p.GetStringWidth(*title);
			if (info.closable)
				{
				r.top -= kCloseMarginWidth + itsCloseImage->GetWidth();
				}
			JPoint titlePt(r.left   + kBorderWidth + kTextMargin,
						   r.bottom - kBorderWidth - info.preMargin);
			if (isSel)
				{
//				titlePt.x -= kSelMargin;
				r.left    -= kSelMargin;
				r.Expand(0, kSelMargin);

				selRect = r;
				}

			if (isSel)
				{
				p.SetPenColor(cmap->GetGrayColor(kSelGrayPercentage));
				p.SetFilling(kJTrue);
				p.JPainter::Rect(r);
				p.SetFilling(kJFalse);
				}
			else
				{
				DrawTabBorder(p, r, kJFalse);
				}
			p.JPainter::String(90, titlePt, *title);

			itsTabRects->AppendElement(r);
			if (isSel)
				{
				r.left += kSelMargin;
				r.Shrink(0, kSelMargin);
				}

			r.Shrink(kBorderWidth, kBorderWidth);
			DrawTab(i, p, r, itsEdge);
			DrawCloseButton(i, p, r);
			r.Expand(kBorderWidth, kBorderWidth);

			if (r.top <= ap.top + scrollArrowWidth)
				{
				if (itsFirstDrawIndex == 1 && i == count && r.top >= ap.top)
					{
					break;
					}
				itsCanScrollDownFlag = JI2B( itsFirstDrawIndex < count );
				itsLastDrawIndex     = i;
				if (r.top < ap.top + scrollArrowWidth && i > itsFirstDrawIndex)
					{
					itsLastDrawIndex--;
					}
				break;
				}

			r.bottom = r.top;
			}
		}

	else if (itsEdge == kBottom)
		{
		JRect r(ap.bottom - kSelMargin - tabHeight, ap.left + kSelMargin,
				ap.bottom - kSelMargin,             ap.left + kSelMargin);

		for (JIndex i=itsFirstDrawIndex; i<=count; i++)
			{
			const JString* title = itsTitles->NthElement(i);
			const JBoolean isSel = JI2B(hasSelection && i == selIndex);
			const TabInfo info   = itsTabInfoList->GetElement(i);

			r.right += 2*kBorderWidth + info.preMargin + info.postMargin + p.GetStringWidth(*title);
			if (info.closable)
				{
				r.right += kCloseMarginWidth + itsCloseImage->GetWidth();
				}
			JPoint titlePt(r.left + kBorderWidth + info.preMargin,
						   r.top  + kBorderWidth + kTextMargin);
			if (isSel)
				{
//				titlePt.y += kSelMargin;
				r.bottom  += kSelMargin;
				r.Expand(kSelMargin, 0);

				selRect = r;
				}

			if (isSel)
				{
				p.SetPenColor(cmap->GetGrayColor(kSelGrayPercentage));
				p.SetFilling(kJTrue);
				p.JPainter::Rect(r);
				p.SetFilling(kJFalse);
				}
			else
				{
				DrawTabBorder(p, r, kJFalse);
				}
			p.JPainter::String(titlePt, *title);

			itsTabRects->AppendElement(r);
			if (isSel)
				{
				r.bottom -= kSelMargin;
				r.Shrink(kSelMargin, 0);
				}

			r.Shrink(kBorderWidth, kBorderWidth);
			DrawTab(i, p, r, itsEdge);
			DrawCloseButton(i, p, r);
			r.Expand(kBorderWidth, kBorderWidth);

			if (r.right >= ap.right - scrollArrowWidth)
				{
				if (itsFirstDrawIndex == 1 && i == count && r.right <= ap.right)
					{
					break;
					}
				itsCanScrollDownFlag = JI2B( itsFirstDrawIndex < count );
				itsLastDrawIndex     = i;
				if (r.right > ap.right - scrollArrowWidth && i > itsFirstDrawIndex)
					{
					itsLastDrawIndex--;
					}
				break;
				}

			r.left = r.right;
			}
		}

	else if (itsEdge == kRight)
		{
		JRect r(ap.top + kSelMargin, ap.right - kSelMargin - tabHeight,
				ap.top + kSelMargin, ap.right - kSelMargin);

		for (JIndex i=itsFirstDrawIndex; i<=count; i++)
			{
			const JString* title = itsTitles->NthElement(i);
			const JBoolean isSel = JI2B(hasSelection && i == selIndex);
			const TabInfo info   = itsTabInfoList->GetElement(i);

			r.bottom += 2*kBorderWidth + info.preMargin + info.postMargin + p.GetStringWidth(*title);
			if (info.closable)
				{
				r.bottom += kCloseMarginWidth + itsCloseImage->GetWidth();
				}
			JPoint titlePt(r.right - kBorderWidth - kTextMargin,
						   r.top   + kBorderWidth + info.preMargin);
			if (isSel)
				{
//				titlePt.x += kSelMargin;
				r.right   += kSelMargin;
				r.Expand(0, kSelMargin);

				selRect = r;
				}

			if (isSel)
				{
				p.SetPenColor(cmap->GetGrayColor(kSelGrayPercentage));
				p.SetFilling(kJTrue);
				p.JPainter::Rect(r);
				p.SetFilling(kJFalse);
				}
			else
				{
				DrawTabBorder(p, r, kJFalse);
				}
			p.JPainter::String(-90, titlePt, *title);

			itsTabRects->AppendElement(r);
			if (isSel)
				{
				r.right -= kSelMargin;
				r.Shrink(0, kSelMargin);
				}

			r.Shrink(kBorderWidth, kBorderWidth);
			DrawTab(i, p, r, itsEdge);
			DrawCloseButton(i, p, r);
			r.Expand(kBorderWidth, kBorderWidth);

			if (r.bottom >= ap.bottom - scrollArrowWidth)
				{
				if (itsFirstDrawIndex == 1 && i == count && r.bottom <= ap.bottom)
					{
					break;
					}
				itsCanScrollDownFlag = JI2B( itsFirstDrawIndex < count );
				itsLastDrawIndex     = i;
				if (r.bottom > ap.bottom - scrollArrowWidth && i > itsFirstDrawIndex)
					{
					itsLastDrawIndex--;
					}
				break;
				}

			r.top = r.bottom;
			}
		}

	JRect r = itsCardFile->GetFrame();
	r.Expand(kBorderWidth, kBorderWidth);
	JXDrawUpFrame(p, r, kBorderWidth);

	if (!selRect.IsEmpty())
		{
		DrawTabBorder(p, selRect, kJTrue);
		}

	DrawScrollButtons(p, lineHeight);
}