// File: /workspace/github_c_makefile_star2_O3/enzo1982[P]smooth/classes/gui/widgets/multi/menu/menubar.cpp
// Function: smooth::GUI::Menubar::Paint(int)
S::Int S::GUI::Menubar::Paint(Int message)
{
	if (!IsRegistered()) return Error();
	if (!IsVisible())    return Success();

#ifdef __APPLE__
	if (orientation == OR_FREE)
	{
		if (menubarCocoa != NIL) menubarCocoa->Paint(message);

		return Success();
	}
#endif

	Window		*window	   = GetContainerWindow();
	Surface		*surface   = GetDrawSurface();
	Rect		 menubar   = Rect(GetRealPosition(), GetRealSize() - Size(1, 1));

#ifdef __WIN32__
	static Bool	 flatStyle = Backends::BackendWin32::IsWindowsVersionAtLeast(VER_PLATFORM_WIN32_NT, 6, 2);
#else
	static Bool	 flatStyle = True;
#endif

	if (orientation == OR_TOP || orientation == OR_BOTTOM)
	{
#ifdef __APPLE__
		Int	 leftWidth	= 0;
		Int	 rightWidth	= 0;

		for (Int i = 0; i < Length(); i++)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if	(entry->GetOrientation() == OR_TOP    || entry->GetOrientation() == OR_LEFT)  leftWidth	 += entry->GetWidth() + 2;
			else if (entry->GetOrientation() == OR_BOTTOM || entry->GetOrientation() == OR_RIGHT) rightWidth += entry->GetWidth() + 2;
		}

		Int	 nextXPosLeft	= (GetSize().cx - leftWidth - rightWidth + 2) / 2;
		Int	 nextXPosRight	= nextXPosLeft + leftWidth + rightWidth - 2;
#else
		Int	 nextXPosLeft	= 7 + (window->GetIcon() != NIL ? 17 : 0);
		Int	 nextXPosRight	= GetSize().cx - 1;

		if (flatStyle) nextXPosLeft = 1;
#endif
		Int	 highestEntry	= 0;

		for (Int i = 0; i < Length(); i++)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if (entry->GetOrientation() == OR_TOP) entry->SetOrientation(OR_LEFT);

			if (entry->GetOrientation() == OR_LEFT)
			{
				entry->SetPosition(Point(nextXPosLeft, 2));

				nextXPosLeft += entry->GetWidth() + 2;

				if (highestEntry < entry->GetHeight()) highestEntry = entry->GetHeight();
			}
		}

		for (Int i = Length() - 1; i >= 0; i--)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if (entry->GetOrientation() == OR_BOTTOM) entry->SetOrientation(OR_RIGHT);

			if (entry->GetOrientation() == OR_RIGHT)
			{
				entry->SetPosition(Point(nextXPosRight - entry->GetWidth(), 2));

				nextXPosRight -= entry->GetWidth() + 2;

				if (highestEntry < entry->GetHeight()) highestEntry = entry->GetHeight();
			}
		}

		SetHeight(highestEntry + 2);

		for (Int i = 0; i < Length(); i++)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if (entry->GetText() == NIL && entry->GetBitmap() == NIL) entry->SetHeight(GetHeight() - 3);
		}
	}
	else if (orientation == OR_LEFT || orientation == OR_RIGHT)
	{
		Int	 nextYPos	= 1;
		Int	 widestEntry	= 0;

		for (Int i = 0; i < Length(); i++)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if (entry->GetOrientation() == OR_LEFT) entry->SetOrientation(OR_TOP);

			if (entry->GetOrientation() == OR_TOP)
			{
				entry->SetPosition(Point(1, nextYPos));

				nextYPos += entry->GetHeight() + 2;

				if (widestEntry < entry->GetWidth()) widestEntry = entry->GetWidth();
			}
		}

		for (Int i = 0; i < Length(); i++)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if (entry->GetOrientation() == OR_RIGHT) entry->SetOrientation(OR_BOTTOM);

			if (entry->GetOrientation() == OR_BOTTOM)
			{
				entry->SetPosition(Point(1, nextYPos));

				nextYPos += entry->GetHeight() + 2;

				if (widestEntry < entry->GetWidth()) widestEntry = entry->GetWidth();
			}
		}

		SetWidth(widestEntry + 3);

		for (Int i = 0; i < Length(); i++)
		{
			MenuEntry	*entry = GetNthEntry(i);

			if (entry->GetText() == NIL && entry->GetBitmap() == NIL) entry->SetWidth(GetWidth() - 3);
		}
	}

	/* Draw handles of non-flat style menubars.
	 */
	switch (message)
	{
		case SP_PAINT:
			if (flatStyle) break;

			if (orientation == OR_TOP || orientation == OR_BOTTOM)
			{
				if (orientation == OR_BOTTOM) { menubar.top--; menubar.bottom--; }

				Point	 p1 = Point(menubar.left + 1, menubar.top + 2);
				Point	 p2 = Point(p1.x, menubar.bottom);

				if (window->GetIcon() != NIL && orientation == OR_TOP)
				{
					p1.x += Math::Round(18 * surface->GetSurfaceDPI() / 96.0) - 1;
					p2.x += Math::Round(18 * surface->GetSurfaceDPI() / 96.0) - 1;
				}

				surface->Bar(p1, p2, OR_VERT);

				p1.x += 2;
				p2.x += 2;

				surface->Bar(p1, p2, OR_VERT);
			}

			break;
	}

	return Widget::Paint(message);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %ebx
movq %rdi, %r14
movq (%rdi), %rax
callq *0x88(%rax)
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
testb %cl, %cl
je 0x88b15
movq (%r14), %rax
movq %r14, %rdi
callq *0x90(%rax)
movl %eax, %ecx
xorl %eax, %eax
testb %cl, %cl
je 0x88b15
movq %r14, %rdi
callq 0x559e0
movq %rax, %r15
movq (%r14), %rax
movq %r14, %rdi
callq *0x40(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x48(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x50(%rax)
movl 0xd0(%r14), %eax
addl $-0x4, %eax
rolw $0xe, %ax
cmpw $0x2, %ax
jb 0x886ff
movzwl %ax, %eax
cmpl $0x3, %eax
je 0x88634
cmpl $0x7, %eax
jne 0x88b0b
movl %ebx, 0xc(%rsp)
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
testl %eax, %eax
jle 0x8897b
movl $0x1, %r13d
xorl %ebp, %ebp
leaq 0x4(%rsp), %r15
xorl %ebx, %ebx
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
callq *0x180(%rax)
movq %rax, %r12
movq (%rax), %rax
movq %r12, %rdi
callq *0x128(%rax)
cmpl $0x10, %eax
jne 0x8868f
movq (%r12), %rax
movq %r12, %rdi
movl $0x4, %esi
callq *0x120(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x128(%rax)
cmpl $0x4, %eax
jne 0x886e4
movl $0x1, 0x4(%rsp)
movl %r13d, 0x8(%rsp)
movq %r12, %rdx
addq $0xb0, %rdx
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x150(%rax)
movl 0xb0(%r12), %eax
movl 0xb4(%r12), %ecx
addl %ecx, %r13d
addl $0x2, %r13d
cmpl %eax, %ebx
cmovlel %eax, %ebx
incl %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
cmpl %eax, %ebp
jl 0x8865b
jmp 0x88983
movl %ebx, 0xc(%rsp)
addq $0x5d0, %r15            # imm = 0x5D0
xorl %ebx, %ebx
movq %r15, %rdi
xorl %esi, %esi
callq 0x563f0
movl 0xb0(%r14), %eax
movq %rax, 0x10(%rsp)
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
testl %eax, %eax
jle 0x887e4
xorl %ebx, %ebx
movl $0x1, %r13d
leaq 0x4(%rsp), %r15
xorl %ebp, %ebp
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
callq *0x180(%rax)
movq %rax, %r12
movq (%rax), %rax
movq %r12, %rdi
callq *0x128(%rax)
cmpl $0x4, %eax
jne 0x88779
movq (%r12), %rax
movq %r12, %rdi
movl $0x10, %esi
callq *0x120(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x128(%rax)
cmpl $0x10, %eax
jne 0x887ce
movl %r13d, 0x4(%rsp)
movl $0x2, 0x8(%rsp)
movq %r12, %rdx
addq $0xb0, %rdx
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x150(%rax)
movl 0xb0(%r12), %eax
movl 0xb4(%r12), %ecx
addl %eax, %r13d
addl $0x2, %r13d
cmpl %ecx, %ebx
cmovlel %ecx, %ebx
incl %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
cmpl %eax, %ebp
jl 0x88745
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
testl %eax, %eax
jle 0x888a8
movl %eax, %r15d
movq 0x10(%rsp), %rbp
decl %ebp
incl %r15d
leaq 0x4(%rsp), %r12
leal -0x2(%r15), %esi
movq (%r14), %rax
movq %r14, %rdi
callq *0x180(%rax)
movq %rax, %r13
movq (%rax), %rax
movq %r13, %rdi
callq *0x128(%rax)
cmpl $0x8, %eax
jne 0x88840
movq (%r13), %rax
movq %r13, %rdi
movl $0x20, %esi
callq *0x120(%rax)
movq (%r13), %rax
movq %r13, %rdi
callq *0x128(%rax)
cmpl $0x20, %eax
jne 0x8889b
movq %r13, %rdx
movl %ebp, %eax
subl 0xb0(%r13), %eax
addq $0xb0, %rdx
movl %eax, 0x4(%rsp)
movl $0x2, 0x8(%rsp)
movq (%r13), %rax
movq %r13, %rdi
movq %r12, %rsi
callq *0x150(%rax)
movl 0xb0(%r13), %eax
movl 0xb4(%r13), %ecx
negl %eax
addl %eax, %ebp
addl $-0x2, %ebp
cmpl %ecx, %ebx
cmovlel %ecx, %ebx
decl %r15d
cmpl $0x1, %r15d
ja 0x8880a
addl $0x2, %ebx
leaq 0xa8(%r14), %rsi
movl 0xb0(%r14), %eax
leaq 0x4(%rsp), %rdx
movl %eax, (%rdx)
movl %ebx, 0x4(%rdx)
movq (%r14), %rax
movq %r14, %rdi
callq *0x150(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
testl %eax, %eax
movl 0xc(%rsp), %ebx
jle 0x88b0b
xorl %ebp, %ebp
leaq 0x4(%rsp), %r15
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
callq *0x180(%rax)
movq %rax, %r12
movq (%rax), %rax
movq %r12, %rdi
callq *0xa8(%rax)
movq %rax, %rdi
xorl %esi, %esi
callq 0x55260
testb %al, %al
je 0x88960
leaq 0x5c0(%r12), %rdi
xorl %esi, %esi
callq 0x57e30
testb %al, %al
je 0x88960
movl 0xb4(%r14), %eax
addl $-0x3, %eax
movq %r12, %rsi
addq $0xa8, %rsi
movl 0xb0(%r12), %ecx
movl %ecx, 0x4(%rsp)
movl %eax, 0x8(%rsp)
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rdx
callq *0x150(%rax)
incl %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
cmpl %eax, %ebp
jl 0x888ee
jmp 0x88b0b
xorl %ebx, %ebx
movl $0x1, %r13d
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
testl %eax, %eax
jle 0x88a3d
xorl %ebp, %ebp
leaq 0x4(%rsp), %r15
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
callq *0x180(%rax)
movq %rax, %r12
movq (%rax), %rax
movq %r12, %rdi
callq *0x128(%rax)
cmpl $0x20, %eax
jne 0x889d2
movq (%r12), %rax
movq %r12, %rdi
movl $0x8, %esi
callq *0x120(%rax)
movq (%r12), %rax
movq %r12, %rdi
callq *0x128(%rax)
cmpl $0x8, %eax
jne 0x88a27
movl $0x1, 0x4(%rsp)
movl %r13d, 0x8(%rsp)
movq %r12, %rdx
addq $0xb0, %rdx
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x150(%rax)
movl 0xb0(%r12), %eax
movl 0xb4(%r12), %ecx
addl %ecx, %r13d
addl $0x2, %r13d
cmpl %eax, %ebx
cmovlel %eax, %ebx
incl %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
cmpl %eax, %ebp
jl 0x8899e
addl $0x3, %ebx
leaq 0xa8(%r14), %rsi
movl 0xb4(%r14), %eax
leaq 0x4(%rsp), %rdx
movl %ebx, (%rdx)
movl %eax, 0x4(%rdx)
movq (%r14), %rax
movq %r14, %rdi
callq *0x150(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
testl %eax, %eax
movl 0xc(%rsp), %ebx
jle 0x88b0b
xorl %ebp, %ebp
leaq 0x4(%rsp), %r15
movq (%r14), %rax
movq %r14, %rdi
movl %ebp, %esi
callq *0x180(%rax)
movq %rax, %r12
movq (%rax), %rax
movq %r12, %rdi
callq *0xa8(%rax)
movq %rax, %rdi
xorl %esi, %esi
callq 0x55260
testb %al, %al
je 0x88af5
leaq 0x5c0(%r12), %rdi
xorl %esi, %esi
callq 0x57e30
testb %al, %al
je 0x88af5
movl 0xb0(%r14), %eax
addl $-0x3, %eax
movq %r12, %rsi
addq $0xa8, %rsi
movl 0xb4(%r12), %ecx
movl %eax, 0x4(%rsp)
movl %ecx, 0x8(%rsp)
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rdx
callq *0x150(%rax)
incl %ebp
movq (%r14), %rax
movq %r14, %rdi
callq *0x178(%rax)
cmpl %eax, %ebp
jl 0x88a83
movq %r14, %rdi
movl %ebx, %esi
callq 0x567d0
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq