// File: /workspace/github_c_makefile_star2_O2/seanpringle[P]xoat/./textbox.c
// Function: textbox_draw
void textbox_draw(Textbox *tb)
{
	XGlyphInfo extents;

	GC context    = XCreateGC(display, tb->window, 0, 0);
	Pixmap canvas = XCreatePixmap(display, tb->window, tb->w, tb->h, DefaultDepth(display, DefaultScreen(display)));
	XftDraw *draw = XftDrawCreate(display, canvas, DefaultVisual(display, DefaultScreen(display)), DefaultColormap(display, DefaultScreen(display)));

	// clear canvas
	XftDrawRect(draw, &tb->color_bg, 0, 0, tb->w, tb->h);

	char *line  = tb->text,
		*text   = tb->text ? tb->text: "",
		*prompt = tb->prompt ? tb->prompt: "",
		*eline  = NULL;

	int text_len    = strlen(text);
	int length      = text_len;
	int line_height = tb->font->ascent + tb->font->descent;

	int cursor_x      = 0;
	int cursor_offset = 0;
	int cursor_width  = MAX(2, line_height/10);

	if (tb->flags & TB_EDITABLE)
	{
		int prompt_len = strlen(prompt);
		length = text_len + prompt_len;
		cursor_offset = MIN(tb->cursor + prompt_len, length);

		eline = calloc(length+10, sizeof(char)); line = eline;
		sprintf(line, "%s%s", prompt, text);

		// replace trailing space so XftTextExtents8 includes its width
		if (length > 0 && isspace(line[length-1])) line[length-1] = '.';

		// calc cursor position
		XftTextExtentsUtf8(display, tb->font, (unsigned char*)line, cursor_offset, &extents);
		cursor_x = extents.width;

		// restore correct text string
		sprintf(line, "%s%s", prompt, text);
	}

	// calc full input text width
	XftTextExtentsUtf8(display, tb->font, (unsigned char*)line, length, &extents);
	int line_width = extents.width;

	int x = 0, y = tb->font->ascent;
	if (tb->flags & TB_RIGHT)  x = tb->w - line_width;
	if (tb->flags & TB_CENTER) x = MAX(0, (tb->w - line_width) / 2);

	// draw the text, including any prompt in edit mode
	XftDrawStringUtf8(draw, &tb->color_fg, tb->font, x, y, (unsigned char*)line, length);

	// draw the cursor
	if (tb->flags & TB_EDITABLE)
		XftDrawRect(draw, &tb->color_fg, cursor_x, 2, cursor_width, line_height-4);

	// flip canvas to window
	XCopyArea(display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0);

	XFreeGC(display, context);
	XftDrawDestroy(draw);
	XFreePixmap(display, canvas);

	free(eline);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %r15
movq 0xad15(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x33c0
movq %rax, 0x40(%rsp)
movq 0xacfc(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rsi
movswl 0x1c(%r15), %edx
movswl 0x1e(%r15), %ecx
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %r8
shlq $0x7, %r8
movl 0x38(%rax,%r8), %r8d
callq 0x3650
movq %rax, %rsi
movq 0xacc8(%rip), %rdi      # 0xebb0
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %rcx
shlq $0x7, %rcx
movq 0x40(%rax,%rcx), %rdx
movq 0x50(%rax,%rcx), %rcx
movq %rsi, 0x38(%rsp)
callq 0x3670
leaq 0x40(%r15), %rsi
movswl 0x1c(%r15), %r8d
movswl 0x1e(%r15), %r9d
movq %rax, 0x30(%rsp)
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x32c0
movq 0x50(%r15), %rbx
testq %rbx, %rbx
leaq 0x67cf(%rip), %rax      # 0xa70a
movq %rbx, %rbp
cmoveq %rax, %rbp
movq 0x58(%r15), %r14
testq %r14, %r14
cmoveq %rax, %r14
movq %rbp, %rdi
callq 0x3250
movq 0x28(%r15), %rsi
movl 0x4(%rsi), %edi
addl (%rsi), %edi
movq %rax, %r12
pushq $0xa
popq %rcx
movl %edi, %eax
cltd
idivl %ecx
movl %edi, 0x2c(%rsp)
cmpl $0x14, %edi
pushq $0x2
popq %rcx
cmovll %ecx, %eax
movl %eax, 0x28(%rsp)
testb $0x8, 0x2(%r15)
jne 0x3f94
movl $0x0, 0x20(%rsp)
movl %r12d, %ebp
xorl %r13d, %r13d
jmp 0x404e
movq %r14, %rdi
callq 0x3250
leal (%rax,%r12), %ebx
movswl 0x20(%r15), %ecx
leal (%rcx,%rax), %edx
cmpl %r12d, %ecx
cmovgel %ebx, %edx
movl %edx, 0x20(%rsp)
addl %r12d, %eax
addl $0xa, %eax
movslq %eax, %rdi
pushq $0x1
popq %rsi
callq 0x3500
movq %rax, %r13
leaq 0x63dd(%rip), %rsi      # 0xa3aa
movq %rax, %rdi
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x32d0
movl %ebx, 0x24(%rsp)
testl %ebx, %ebx
jle 0x4004
callq 0x3290
movq (%rax), %rcx
movl 0x24(%rsp), %eax
movsbq -0x1(%r13,%rax), %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
je 0x4004
movb $0x2e, -0x1(%r13,%rax)
movq 0xaba5(%rip), %rdi      # 0xebb0
movq 0x28(%r15), %rsi
leaq 0x4c(%rsp), %r12
movq %r13, %rdx
movl 0x20(%rsp), %ecx
movq %r12, %r8
callq 0x3520
movzwl (%r12), %eax
movl %eax, 0x20(%rsp)
leaq 0x6377(%rip), %rsi      # 0xa3aa
movq %r13, %rdi
movq %r14, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x32d0
movq 0x28(%r15), %rsi
movq %r13, %rbx
movl 0x24(%rsp), %ebp
movq 0xab5b(%rip), %rdi      # 0xebb0
leaq 0x4c(%rsp), %r14
movq %rbx, %rdx
movl %ebp, %ecx
movq %r14, %r8
callq 0x3520
movzwl (%r14), %eax
movq (%r15), %rdx
movq 0x28(%r15), %r9
btl $0x11, %edx
movq 0x30(%rsp), %r12
movl $0x0, %ecx
jae 0x4089
movswl 0x1c(%r15), %ecx
subl %eax, %ecx
movl (%r9), %r8d
btl $0x12, %edx
jae 0x40a9
movswl 0x1c(%r15), %esi
subl %eax, %esi
movl %esi, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
xorl %ecx, %ecx
cmpl $-0x1, %esi
cmovgel %eax, %ecx
leaq 0x30(%r15), %r14
movl %ebp, (%rsp)
movq %r12, %rdi
movq %r14, %rsi
movq %r9, %rdx
movq %rbx, %r9
callq 0x3170
testb $0x8, 0x2(%r15)
je 0x40e8
movl 0x2c(%rsp), %r9d
addl $-0x4, %r9d
pushq $0x2
popq %rcx
movq %r12, %rdi
movq %r14, %rsi
movl 0x20(%rsp), %edx
movl 0x28(%rsp), %r8d
callq 0x32c0
movq 0xaac1(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rdx
movswl 0x1c(%r15), %eax
movswl 0x1e(%r15), %ecx
andl $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
movl %ecx, 0x8(%rsp)
movl %eax, (%rsp)
movq 0x38(%rsp), %r14
movq %r14, %rsi
movq 0x40(%rsp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3240
movq 0xaa80(%rip), %rdi      # 0xebb0
movq %rbx, %rsi
callq 0x32f0
movq %r12, %rdi
callq 0x3230
movq 0xaa69(%rip), %rdi      # 0xebb0
movq %r14, %rsi
callq 0x3600
movq %r13, %rdi
callq 0x3220
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq