// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %rdi, %r15
movq 0xa52a(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x33c0
movq %rax, 0x30(%rsp)
movq 0xa511(%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 0xa4dd(%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, 0x28(%rsp)
callq 0x3670
leaq 0x40(%r15), %rsi
movswl 0x1c(%r15), %r8d
movswl 0x1e(%r15), %r9d
movq %rax, 0x20(%rsp)
movq %rax, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x32c0
movq 0x50(%r15), %r13
testq %r13, %r13
leaq 0x5fac(%rip), %rax      # 0xa6d2
movq %r13, %rbp
cmoveq %rax, %rbp
movq 0x58(%r15), %rbx
testq %rbx, %rbx
cmoveq %rax, %rbx
movq %rbp, %rdi
callq 0x3250
movq %rax, %r12
movq 0x28(%r15), %rax
movl 0x4(%rax), %ecx
addl (%rax), %ecx
movl %ecx, 0x14(%rsp)
movslq %ecx, %rax
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
cmpl $0x14, %eax
movl $0x2, %r14d
cmovgel %ecx, %r14d
testb $0x8, 0x2(%r15)
jne 0x4794
movl $0x0, 0x4(%rsp)
movl %r12d, %ebp
movq $0x0, 0x8(%rsp)
jmp 0x485f
movl %r14d, 0x10(%rsp)
movq %rbx, %rdi
callq 0x3250
leal (%rax,%r12), %r13d
movswl 0x20(%r15), %ecx
leal (%rcx,%rax), %edx
cmpl %r12d, %ecx
cmovgel %r13d, %edx
movl %edx, 0x8(%rsp)
addl %r12d, %eax
addl $0xa, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3500
movq %rax, %r14
leaq 0x5b9d(%rip), %rsi      # 0xa372
movq %rax, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x32d0
movq %r13, 0x18(%rsp)
testl %r13d, %r13d
jle 0x480e
callq 0x3290
movq (%rax), %rcx
movl 0x18(%rsp), %eax
movsbq -0x1(%r14,%rax), %rdx
testb $0x20, 0x1(%rcx,%rdx,2)
je 0x480e
movb $0x2e, -0x1(%r14,%rax)
movq 0xa39b(%rip), %rdi      # 0xebb0
movq 0x28(%r15), %rsi
leaq 0x3c(%rsp), %r12
movq %r14, %rdx
movl 0x8(%rsp), %ecx
movq %r12, %r8
callq 0x3520
movzwl (%r12), %eax
movl %eax, 0x4(%rsp)
leaq 0x5b35(%rip), %rsi      # 0xa372
movq %r14, %rdi
movq %rbx, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x32d0
movq %r14, %r13
movq %r14, 0x8(%rsp)
movl 0x10(%rsp), %r14d
movq 0x18(%rsp), %rbp
movq 0xa34a(%rip), %rdi      # 0xebb0
movq 0x28(%r15), %rsi
leaq 0x3c(%rsp), %rbx
movq %r13, %rdx
movl %ebp, %ecx
movq %rbx, %r8
callq 0x3520
movzwl (%rbx), %eax
movq (%r15), %rsi
movq 0x28(%r15), %rdx
btl $0x11, %esi
movq 0x20(%rsp), %r12
movl $0x0, %ecx
jae 0x489d
movswl 0x1c(%r15), %ecx
subl %eax, %ecx
movl (%rdx), %r8d
btl $0x12, %esi
jae 0x48be
movswl 0x1c(%r15), %esi
subl %eax, %esi
movl %esi, %eax
shrl $0x1f, %eax
addl %esi, %eax
sarl %eax
xorl %ecx, %ecx
cmpl $-0x1, %esi
cmovgel %eax, %ecx
leaq 0x30(%r15), %rbx
subq $0x8, %rsp
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %r9
pushq %rbp
callq 0x3170
addq $0x10, %rsp
testb $0x8, 0x2(%r15)
je 0x4900
movl 0x14(%rsp), %r9d
addl $-0x4, %r9d
movq %r12, %rdi
movq %rbx, %rsi
movl 0x4(%rsp), %edx
movl $0x2, %ecx
movl %r14d, %r8d
callq 0x32c0
movq 0xa2a9(%rip), %rdi      # 0xebb0
movq 0x8(%r15), %rdx
movswl 0x1c(%r15), %eax
movswl 0x1e(%r15), %r10d
xorl %r11d, %r11d
movq 0x28(%rsp), %r14
movq %r14, %rsi
movq 0x30(%rsp), %rbx
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
pushq %r11
pushq %r11
pushq %r10
pushq %rax
callq 0x3240
addq $0x20, %rsp
movq 0xa26b(%rip), %rdi      # 0xebb0
movq %rbx, %rsi
callq 0x32f0
movq %r12, %rdi
callq 0x3230
movq 0xa254(%rip), %rdi      # 0xebb0
movq %r14, %rsi
callq 0x3600
movq 0x8(%rsp), %rdi
callq 0x3220
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq