// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq 0xf6aa(%rip), %rdi      # 0x13ba0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x33c0
movq %rax, -0x20(%rbp)
movq 0xf68b(%rip), %rdi      # 0x13ba0
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x8(%rbp), %rax
movswl 0x1c(%rax), %edx
movq -0x8(%rbp), %rax
movswl 0x1e(%rax), %ecx
movq 0xf66c(%rip), %rax      # 0x13ba0
movq 0xe8(%rax), %rax
movq 0xf65e(%rip), %r8       # 0x13ba0
movslq 0xe0(%r8), %r8
shlq $0x7, %r8
addq %r8, %rax
movl 0x38(%rax), %r8d
callq 0x3640
movq %rax, -0x28(%rbp)
movq 0xf63c(%rip), %rdi      # 0x13ba0
movq -0x28(%rbp), %rsi
movq 0xf631(%rip), %rax      # 0x13ba0
movq 0xe8(%rax), %rax
movq 0xf623(%rip), %rcx      # 0x13ba0
movslq 0xe0(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x40(%rax), %rdx
movq 0xf60a(%rip), %rax      # 0x13ba0
movq 0xe8(%rax), %rax
movq 0xf5fc(%rip), %rcx      # 0x13ba0
movslq 0xe0(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x50(%rax), %rcx
callq 0x3660
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x40, %rsi
movq -0x8(%rbp), %rax
movswl 0x1c(%rax), %r8d
movq -0x8(%rbp), %rax
movswl 0x1e(%rax), %r9d
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x32c0
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x50(%rax)
je 0x460b
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x80(%rbp)
jmp 0x4618
leaq 0xaff4(%rip), %rax      # 0xf606
movq %rax, -0x80(%rbp)
jmp 0x4618
movq -0x80(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
cmpq $0x0, 0x58(%rax)
je 0x463c
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x88(%rbp)
jmp 0x464c
leaq 0xafc3(%rip), %rax      # 0xf606
movq %rax, -0x88(%rbp)
jmp 0x464c
movq -0x88(%rbp), %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x40(%rbp), %rdi
callq 0x3250
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
addl 0x4(%rcx), %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x5c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl $0x2, %eax
cmpl %ecx, %eax
jle 0x46ba
movl $0x2, %eax
movl %eax, -0x8c(%rbp)
jmp 0x46cb
movl -0x5c(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
andq $0x80000, %rax          # imm = 0x80000
cmpq $0x0, %rax
je 0x47ef
movq -0x48(%rbp), %rdi
callq 0x3250
movl %eax, -0x6c(%rbp)
movl -0x54(%rbp), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x8(%rbp), %rax
movswl 0x20(%rax), %eax
addl -0x6c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jge 0x4723
movq -0x8(%rbp), %rax
movswl 0x20(%rax), %eax
addl -0x6c(%rbp), %eax
movl %eax, -0x90(%rbp)
jmp 0x472c
movl -0x58(%rbp), %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x64(%rbp)
movl -0x58(%rbp), %eax
addl $0xa, %eax
movslq %eax, %rdi
movl $0x1, %esi
callq 0x3500
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq 0xab3b(%rip), %rsi      # 0xf2a2
movb $0x0, %al
callq 0x32d0
cmpl $0x0, -0x58(%rbp)
jle 0x47af
callq 0x3290
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movl -0x58(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
movsbl (%rcx,%rdx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x47af
movq -0x38(%rbp), %rax
movl -0x58(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movb $0x2e, (%rax,%rcx)
movq 0xf3ea(%rip), %rdi      # 0x13ba0
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x38(%rbp), %rdx
movl -0x64(%rbp), %ecx
leaq -0x14(%rbp), %r8
callq 0x3520
movzwl -0x14(%rbp), %eax
movl %eax, -0x60(%rbp)
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq 0xaaba(%rip), %rsi      # 0xf2a2
movb $0x0, %al
callq 0x32d0
movq 0xf3aa(%rip), %rdi      # 0x13ba0
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x38(%rbp), %rdx
movl -0x58(%rbp), %ecx
leaq -0x14(%rbp), %r8
callq 0x3520
movzwl -0x14(%rbp), %eax
movl %eax, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl (%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
andq $0x20000, %rax          # imm = 0x20000
cmpq $0x0, %rax
je 0x484a
movq -0x8(%rbp), %rax
movswl 0x1c(%rax), %eax
subl -0x70(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
andq $0x40000, %rax          # imm = 0x40000
cmpq $0x0, %rax
je 0x48a4
movq -0x8(%rbp), %rax
movswl 0x1c(%rax), %eax
subl -0x70(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jle 0x4882
xorl %eax, %eax
movl %eax, -0x94(%rbp)
jmp 0x489b
movq -0x8(%rbp), %rax
movswl 0x1c(%rax), %eax
subl -0x70(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x74(%rbp)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x30, %rsi
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rdx
movl -0x74(%rbp), %ecx
movl -0x78(%rbp), %r8d
movq -0x38(%rbp), %r9
movl -0x58(%rbp), %eax
movl %eax, (%rsp)
callq 0x3170
movq -0x8(%rbp), %rax
movq (%rax), %rax
andq $0x80000, %rax          # imm = 0x80000
cmpq $0x0, %rax
je 0x4906
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x30, %rsi
movl -0x60(%rbp), %edx
movl -0x68(%rbp), %r8d
movl -0x5c(%rbp), %r9d
subl $0x4, %r9d
movl $0x2, %ecx
callq 0x32c0
movq 0xf293(%rip), %rdi      # 0x13ba0
movq -0x28(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x20(%rbp), %rcx
movq -0x8(%rbp), %rax
movswl 0x1c(%rax), %r10d
movq -0x8(%rbp), %rax
movswl 0x1e(%rax), %eax
xorl %r9d, %r9d
movl %r9d, %r8d
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
callq 0x3240
movq 0xf248(%rip), %rdi      # 0x13ba0
movq -0x20(%rbp), %rsi
callq 0x32f0
movq -0x30(%rbp), %rdi
callq 0x3230
movq 0xf22f(%rip), %rdi      # 0x13ba0
movq -0x28(%rbp), %rsi
callq 0x3600
movq -0x50(%rbp), %rdi
callq 0x3220
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)