+
+ if( !vg_ui.focused_control_hit ){
+ ui_defocus_all();
+ }
+
+ if( vg_ui.wants_mouse ){
+ SDL_SetWindowGrab( vg.window, SDL_FALSE );
+ SDL_SetRelativeMouseMode( SDL_FALSE );
+ }
+ else{
+ SDL_SetWindowGrab( vg.window, SDL_TRUE );
+ SDL_SetRelativeMouseMode( SDL_TRUE );
+ }
+
+ SDL_SetCursor( vg_ui.cursor_map[ vg_ui.cursor ] );
+ SDL_ShowCursor(1);
+}
+
+/*
+ * checkbox
+ * -----------------------------------------------------------------------------
+ */
+
+static int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data ){
+ ui_rect rect, label, box;
+ ui_standard_widget( inout_panel, rect, 1 );
+
+ ui_split( rect, k_ui_axis_v, -rect[3], 0, label, box );
+ ui_text( label, str_label, k_ui_scale, k_ui_align_middle_left, 0 );
+
+ int changed = ui_colourbutton( box, k_ui_bg )==1;
+ if( changed )
+ *data = (*data) ^ 0x1;
+
+ if( *data ){
+ ui_rect_pad( box, (ui_px[2]){4,4} );
+ ui_fill( box, ui_colour( k_ui_orange ) );
+ }
+
+ return changed;
+}
+
+/*
+ * Dropdown / Enum
+ * -----------------------------------------------------------------------------
+ */
+
+/*
+ * unfortunately no return value since we only find out that event in the
+ * postrender step.
+ */
+static void ui_enum( ui_rect inout_panel, const char *str_label,
+ struct ui_enum_opt *options, u32 len, i32 *value ){
+ ui_rect rect, label, box;
+ ui_standard_widget( inout_panel, rect, 1 );
+ ui_label( rect, str_label, k_ui_scale, 0, box );
+
+ const char *display = "OUT OF RANGE";
+ int valid = 0;
+ for( u32 i=0; i<len; i ++ ){
+ if( *value == options[i].value ){
+ display = options[i].alias;
+ valid = 1;
+ break;
+ }
+ }
+
+ if( ui_button_text( box, display, k_ui_scale ) == 1 ){
+ vg_ui.focused_control_type = k_ui_control_enum;
+ vg_ui.ptr_enum = value;
+ vg_ui._enum.option_count = len;
+ vg_ui._enum.options = options;
+ rect_copy( box, vg_ui._enum.rect );
+ }
+
+ if( !valid )
+ ui_outline( box, 1, ui_colour(k_ui_red), 0 );
+}
+
+static void ui_enum_post(void){
+ ui_rect drawer;
+ rect_copy( vg_ui._enum.rect, drawer );
+ drawer[3] *= vg_ui._enum.option_count;
+
+ int close = 0;
+ int clickany= ui_click_up(UI_MOUSE_LEFT|UI_MOUSE_RIGHT|UI_MOUSE_MIDDLE),
+ hover = ui_inside_rect( drawer, vg_ui.mouse );
+
+ if( clickany && !hover ){
+ return;
+ }
+
+ /* HACK */
+ vg_ui.focused_control_type = k_ui_control_none;
+ i32 *value = vg_ui.ptr_enum;
+
+ for( u32 i=0; i<vg_ui._enum.option_count; i++ ){
+ ui_rect button;
+ ui_split( drawer, k_ui_axis_h, vg_ui._enum.rect[3], 0, button,drawer );
+
+ enum ui_scheme_colour colour = k_ui_bg+3;
+ if( vg_ui._enum.options[i].value == *value )
+ colour = k_ui_orange;
+
+ if( ui_colourbutton_text( button, vg_ui._enum.options[i].alias,
+ k_ui_scale, colour ) == 1 ){
+ *value = vg_ui._enum.options[i].value;
+ close = 1;
+ }
+ }
+
+ /* HACK */
+ vg_ui.focused_control_type = k_ui_control_enum;
+
+ if( !close )
+ vg_ui.focused_control_hit = 1;
+}
+
+/*
+ * Textbox chaos
+ * -----------------------------------------------------------------------------
+ */
+
+static void _ui_textbox_make_selection( int *start, int *end ){
+ *start = VG_MIN( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
+ *end = VG_MAX( vg_ui.textbox.cursor_pos, vg_ui.textbox.cursor_user );
+}
+
+static void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
+ int dir, int snap_together ){
+ *cursor0 = VG_MAX( 0, vg_ui.textbox.cursor_user + dir );
+ *cursor0 =
+ VG_MIN(
+ VG_MIN( vg_ui.textbox.len-1, strlen( vg_ui.textbuf )),
+ *cursor0 );
+
+ if( snap_together )
+ *cursor1 = *cursor0;
+}
+
+static int _ui_textbox_makeroom( int datastart, int length ){
+ int move_to = VG_MIN( datastart+length, vg_ui.textbox.len-1 );
+ int move_amount = strlen( vg_ui.textbuf )-datastart;
+ int move_end = VG_MIN( move_to+move_amount, vg_ui.textbox.len-1 );
+ move_amount = move_end-move_to;
+
+ if( move_amount )
+ memmove( &vg_ui.textbuf[ move_to ],
+ &vg_ui.textbuf[ datastart ],
+ move_end-move_to );
+
+ vg_ui.textbuf[ move_end ] = '\0';
+
+ return VG_MIN( length, vg_ui.textbox.len-datastart-1 );
+}
+
+static int _ui_textbox_delete_char( int direction ){
+ int start, end;
+ _ui_textbox_make_selection( &start, &end );
+
+ /* There is no selection */
+ if( !(end-start) ){
+ if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_ui.textbuf ) );
+ else if( direction == -1 ) start = VG_MAX( start-1, 0 );
+ }
+
+ /* Still no selction, no need to do anything */
+ if( !(end-start) )
+ return start;
+
+ /* Copy the end->terminator to start */
+ int remaining_length = strlen( vg_ui.textbuf )+1-end;
+ memmove( &vg_ui.textbuf[ start ],
+ &vg_ui.textbuf[ end ],
+ remaining_length );
+ return start;
+}
+
+static void _ui_textbox_to_clipboard(void){
+ int start, end;
+ _ui_textbox_make_selection( &start, &end );
+ char buffer[512];
+
+ if( end-start ){
+ memcpy( buffer, &vg_ui.textbuf[ start ], end-start );
+ buffer[ end-start ] = 0x00;
+ SDL_SetClipboardText( buffer );
+ }
+}
+
+static void ui_start_modal( const char *message, u32 options );
+static void _ui_textbox_clipboard_paste(void){
+ if( !SDL_HasClipboardText() )
+ return;
+
+ char *text = SDL_GetClipboardText();
+
+ if( !text )
+ return;
+
+ int datastart = _ui_textbox_delete_char( 0 );
+ int length = strlen( text );
+
+ if( (vg_ui.textbox.len - strlen(vg_ui.textbuf)) < length ){
+ ui_start_modal( "Clipboard content exceeds buffer size.", UI_MODAL_BAD );
+ return;
+ }
+
+ int cpylength = _ui_textbox_makeroom( datastart, length );
+
+ memcpy( vg_ui.textbuf + datastart, text, cpylength);
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
+ &vg_ui.textbox.cursor_pos, cpylength, 1 );
+ SDL_free( text );
+}
+
+static void _ui_textbox_put_char( char c ){
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
+ if( (vg_ui.textbox.len - strlen(vg_ui.textbuf)) <= 1 ) return;
+
+ if( _ui_textbox_makeroom( vg_ui.textbox.cursor_user, 1 ) )
+ vg_ui.textbuf[ vg_ui.textbox.cursor_user ] = c;
+
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
+ &vg_ui.textbox.cursor_pos, 1, 1 );
+}
+
+/* Receed secondary cursor */
+static void _ui_textbox_left_select(void){
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, -1, 0 );
+}
+
+/* Match and receed both cursors */
+static void _ui_textbox_left(void){
+ int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
+
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
+ &vg_ui.textbox.cursor_pos, -cursor_diff, 1 );
+}
+
+static void _ui_textbox_up(void){
+ if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
+ int line_begin = vg_ui.textbox.cursor_user;
+
+ while( line_begin ){
+ if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
+ break;
+ }
+
+ line_begin --;
+ }
+
+ if( line_begin ){
+ int line_above_begin = line_begin-1;
+
+ while( line_above_begin ){
+ if( vg_ui.textbuf[ line_above_begin-1 ] == '\n' ){
+ break;
+ }
+
+ line_above_begin --;
+ }
+
+ int offset = vg_ui.textbox.cursor_user - line_begin,
+ line_length_above = line_begin - line_above_begin -1;
+
+ offset = VG_MIN( line_length_above, offset );
+
+ vg_ui.textbox.cursor_user = line_above_begin+offset;
+ vg_ui.textbox.cursor_pos = line_above_begin+offset;
+ }
+ else{
+ vg_ui.textbox.cursor_user = line_begin;
+ vg_ui.textbox.cursor_pos = line_begin;
+ }
+ }
+ else{
+ if( vg_ui.textbox.callbacks.up ){
+ vg_ui.textbox.callbacks.up( vg_ui.textbuf, vg_ui.textbox.len );
+ }
+ }
+}
+
+static void _ui_textbox_down(void){
+ if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
+ int line_begin = vg_ui.textbox.cursor_user;
+
+ while( line_begin ){
+ if( vg_ui.textbuf[ line_begin-1 ] == '\n' ){
+ break;
+ }
+
+ line_begin --;
+ }
+
+ int line_below_begin = vg_ui.textbox.cursor_user;
+
+ while(1){
+ if( vg_ui.textbuf[ line_below_begin ] == '\0' ){
+ vg_ui.textbox.cursor_user = line_below_begin;
+ vg_ui.textbox.cursor_pos = line_below_begin;
+ return;
+ }
+
+ if( vg_ui.textbuf[ line_below_begin ] == '\n' ){
+ line_below_begin ++;
+ break;
+ }
+
+ line_below_begin ++;
+ }
+
+ int line_below_end = line_below_begin;
+ while(1){
+ if( vg_ui.textbuf[ line_below_end ] == '\0' ||
+ vg_ui.textbuf[ line_below_end ] == '\n' ){
+ line_below_end ++;
+ break;
+ }
+ line_below_end ++;
+ }
+
+ int offset = vg_ui.textbox.cursor_user - line_begin,
+ line_length_below = line_below_end - line_below_begin -1;
+
+ offset = VG_MIN( line_length_below, offset );
+
+ vg_ui.textbox.cursor_user = line_below_begin+offset;
+ vg_ui.textbox.cursor_pos = line_below_begin+offset;
+ }
+ else{
+ if( vg_ui.textbox.callbacks.down ){
+ vg_ui.textbox.callbacks.down( vg_ui.textbuf, vg_ui.textbox.len );
+ }
+ }
+}
+
+static void _ui_textbox_right_select(void){
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 1, 0 );
+}
+
+static void _ui_textbox_right(void){
+ int cursor_diff = vg_ui.textbox.cursor_pos - vg_ui.textbox.cursor_user? 0: 1;
+
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user,
+ &vg_ui.textbox.cursor_pos, +cursor_diff, 1 );
+}
+
+static void _ui_textbox_backspace(void){
+ if( vg_ui.focused_control_type == k_ui_control_textbox ){
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char( -1 );
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
+
+ if( vg_ui.textbox.callbacks.change ){
+ vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
+ }
+ }
+}
+
+static void _ui_textbox_delete(void){
+ if( vg_ui.focused_control_type == k_ui_control_textbox ){
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char( 1 );
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
+
+ if( vg_ui.textbox.callbacks.change ){
+ vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
+ }
+ }
+}
+
+static void _ui_textbox_home_select(void){
+ i32 start = vg_ui.textbox.cursor_user;
+
+ if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
+ while( start ){
+ if( vg_ui.textbuf[start-1] == '\n' )
+ break;
+ else
+ start --;
+ }
+ }
+ else
+ start = 0;
+
+ vg_ui.textbox.cursor_user = start;
+}
+
+static void _ui_textbox_home(void){
+ _ui_textbox_home_select();
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
+}
+
+static void _ui_textbox_end_select(void){
+ i32 end = vg_ui.textbox.cursor_user;
+
+ if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE ){
+ while( vg_ui.textbuf[end] ){
+ if( vg_ui.textbuf[end] == '\n' )
+ break;
+ else
+ end ++;
+ }
+ }
+ else
+ end = VG_MIN( vg_ui.textbox.len-1, strlen(vg_ui.textbuf) );
+
+ vg_ui.textbox.cursor_user = end;
+}
+
+static void _ui_textbox_end(void){
+ _ui_textbox_end_select();
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
+}
+
+static void _ui_textbox_select_all(void){
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_user, NULL, 10000, 0);
+ _ui_textbox_move_cursor( &vg_ui.textbox.cursor_pos, NULL, -10000, 0);
+}
+
+static void _ui_textbox_cut(void){
+ _ui_textbox_to_clipboard();
+ vg_ui.textbox.cursor_user = _ui_textbox_delete_char(0);
+ vg_ui.textbox.cursor_pos = vg_ui.textbox.cursor_user;
+}
+
+static void _ui_textbox_enter(void){
+ if( vg_ui.focused_control_type == k_ui_control_textbox ){
+ if( vg_ui.textbox.callbacks.enter )
+ vg_ui.textbox.callbacks.enter( vg_ui.textbuf, vg_ui.textbox.len );
+
+ if( vg_ui.focused_control_type != k_ui_control_textbox ) return;
+
+ if( vg_ui.textbox.flags & UI_TEXTBOX_MULTILINE )
+ _ui_textbox_put_char( '\n' );
+ else{
+ if( !(vg_ui.textbox.flags & UI_TEXTBOX_AUTOFOCUS ) )
+ ui_defocus_all();
+ }
+ }
+}
+
+/*
+ * based on a visual character coordinate relative to the anchor of the textbox,
+ * this works out the linear place in the buffer that coordinate maps to
+ *
+ * input coordinates go in co[0], co[1], and the result index is in co[2]
+ */
+static void _ui_textbox_calc_index_from_grid( int co[3], int wrap_length ){
+ int i[3] = {0,0,0};
+
+ char c;
+ while( (c = vg_ui.textbuf[i[2]]) ){
+ if( i[1]==co[1] && i[0]>=co[0] ) break;
+
+ if( i[0] >= wrap_length ){
+ i[1] ++;
+ i[0] = 0;
+ }
+
+ if( c >= 32 && c <= 126 ){
+ i[0] ++;
+ i[2] ++;
+ }
+ else if( c == '\n' ){
+ i[1] ++;
+
+ if( i[1] > co[1] ) break;
+
+ i[2] ++;
+ i[0] = 0;
+ }
+ else i[2] ++;
+ }
+
+ co[0] = i[0];
+ co[1] = i[1];
+ co[2] = i[2];
+}
+
+/*
+ * based on the index specied in co[2], work out the visual character
+ * coordinates and store them in co[0], co[1]
+ */
+static void _ui_textbox_index_calc_coords( int co[3], int wrap_length ){
+ co[0] = 0;
+ co[1] = 0;
+
+ char c;
+ int i=0;
+
+ while( (c = vg_ui.textbuf[i ++]) ){
+ if( i > co[2] ) break;
+ if( co[0] >= wrap_length ){
+ co[1] ++;
+ co[0] = 0;
+ }
+ if( c >= 32 && c <= 126 ) co[0] ++;
+ else if( c == '\n' ){
+ co[1] ++;
+ co[0] = 0;
+ }
+ }
+}
+
+/*
+ * calculate the number of characters remaining until either:
+ * - the wrap_length limit is hit
+ * - end of the line/string
+ *
+ * index must be fully populated with visual X/Y, and linear index
+ */
+static int _ui_textbox_run_remaining( int index[3], int wrap_length ){
+ int i=0, printed_chars=0;
+ char c;
+ while( (c = vg_ui.textbuf[index[2] + (i ++)]) ){
+ if( index[0]+i >= wrap_length ) break;
+ if( c >= 32 && c <= 126 ) printed_chars ++;
+ else if( c == '\n' ) break;
+ }
+
+ return printed_chars+1;
+}
+
+static int ui_textbox( ui_rect inout_panel, const char *label,
+ char *buf, u32 len, u32 lines, u32 flags,
+ struct ui_textbox_callbacks *callbacks ){
+ assert( lines > 0 );
+ assert( buf );
+
+ if( lines > 1 ) flags |= UI_TEXTBOX_MULTILINE;
+
+ ui_rect rect;
+ ui_standard_widget( inout_panel, rect, lines );
+
+ if( label )
+ ui_label( rect, label, 1, 0, rect );
+
+ int clickup= ui_click_up(UI_MOUSE_LEFT),
+ clickdown = ui_click_down(UI_MOUSE_LEFT),
+ click = ui_clicking(UI_MOUSE_LEFT) | clickup,
+ target = ui_inside_rect( rect, vg_ui.mouse_click ) && click,
+ hover = ui_inside_rect( rect, vg_ui.mouse );
+
+ /* allow instant transitions from textbox->textbox */
+ if( (vg_ui.focused_control_type != k_ui_control_none) &&
+ (vg_ui.focused_control_type != k_ui_control_textbox) ){
+ clickup = 0;
+ clickdown = 0;
+ click = 0;
+ target = 0;
+ hover = 0;
+ flags &= ~UI_TEXTBOX_AUTOFOCUS;
+ }
+
+ u32 col_base = ui_colour( k_ui_bg ),
+ col_highlight = ui_colour( k_ui_fg ),
+ col_cursor = (0x00ffffff & ui_colour(k_ui_fg))|0x7f000000;
+
+ ui_px border = -1;
+
+ ui_rect text_rect;
+ rect_copy( rect, text_rect );
+
+ if( flags & UI_TEXTBOX_MULTILINE ) text_rect[3] = rect[3]-16;
+ else text_rect[3] = vg_ui.font->line_height;
+
+ text_rect[2] -= 16;
+ ui_rect_center( rect, text_rect );
+
+ ui_px wrap_length = 1024;
+
+ if( flags & UI_TEXTBOX_WRAP )
+ wrap_length = text_rect[2] / vg_ui.font->spacing;
+
+ if( hover ){
+ vg_ui.cursor = k_ui_cursor_ibeam;
+ }
+
+ if( vg_ui.focused_control_id == buf ){
+ ui_fill( rect, col_base );
+ ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
+
+ if( !(flags & UI_TEXTBOX_AUTOFOCUS) && ((clickup||clickdown) && !target)){
+ ui_defocus_all();
+ }
+ else{
+ vg_ui.focused_control_hit = 1;
+ if( click && target ){
+ int p0[3] ={
+ (vg_ui.mouse_click[0] - text_rect[0]) / vg_ui.font->spacing,
+ (vg_ui.mouse_click[1] - text_rect[1]) / vg_ui.font->line_height,
+ -1
+ },
+ p1[3] = {
+ (vg_ui.mouse[0] - text_rect[0]) / vg_ui.font->spacing,
+ (vg_ui.mouse[1] - text_rect[1]) / vg_ui.font->line_height,
+ -1
+ };
+
+ if( flags & UI_TEXTBOX_MULTILINE ){
+ _ui_textbox_calc_index_from_grid( p0, wrap_length );
+ _ui_textbox_calc_index_from_grid( p1, wrap_length );
+
+ vg_ui.textbox.cursor_pos = p0[2];
+ vg_ui.textbox.cursor_user = p1[2];
+ }
+ else{
+ int max = strlen( buf );
+ vg_ui.textbox.cursor_pos = VG_MAX( 0, VG_MIN( max, p0[0] )),
+ vg_ui.textbox.cursor_user = VG_MAX( 0, VG_MIN( max, p1[0] ));
+ }
+ }
+
+ ui_outline( rect, -2, vg_ui.scheme[ k_ui_orange ], 0 );
+
+ ui_rect cursor;
+
+ int c0 = vg_ui.textbox.cursor_pos,
+ c1 = vg_ui.textbox.cursor_user,
+ start = VG_MIN( c0, c1 ),
+ end = VG_MAX( c0, c1 ),
+ chars = end-start;
+
+ if( flags & (UI_TEXTBOX_WRAP|UI_TEXTBOX_MULTILINE) ){
+ int pos[3], remaining = chars;
+
+ pos[2] = start;
+ _ui_textbox_index_calc_coords( pos, wrap_length );
+
+ if( start==end ){
+ cursor[0] = text_rect[0] + pos[0]*vg_ui.font->spacing-1;
+ cursor[1] = text_rect[1] + pos[1]*14;
+ cursor[2] = 2;
+ cursor[3] = 13;
+ ui_fill( cursor, col_cursor );
+ rect_copy( cursor, vg_ui.click_fader_end );
+ }
+ else{
+ while( remaining ){
+ int run = _ui_textbox_run_remaining( pos, wrap_length );
+ run = VG_MIN( run, remaining );
+
+ cursor[0] = text_rect[0] + pos[0]*vg_ui.font->spacing-1;
+ cursor[1] = text_rect[1] + pos[1]*14;
+ cursor[2] = (float)(run)*(float)vg_ui.font->spacing;
+ cursor[3] = 13;
+
+ ui_fill( cursor, col_cursor );
+
+ remaining -= run;
+ pos[0] = 0;
+ pos[1] ++;
+ pos[2] += run;
+ }
+ rect_copy( cursor, vg_ui.click_fader_end );
+ }
+ }
+ else{
+ cursor[0] = text_rect[0] + start*vg_ui.font->spacing-1;
+ cursor[1] = text_rect[1];
+ cursor[3] = 13;
+
+ if( start==end ){
+ cursor[2] = 2;
+ }
+ else{
+ cursor[2] = (float)(chars)*(float)vg_ui.font->spacing;
+ }
+
+ if( (vg_ui.click_fade_opacity<=0.0f) &&
+ ui_clip( rect, cursor, cursor ) ){
+ ui_fill( cursor, col_cursor );
+ }
+
+ rect_copy( cursor, vg_ui.click_fader_end );
+ }
+ }
+
+ return 0;
+ }
+
+ if( click || (flags & UI_TEXTBOX_AUTOFOCUS) ){
+ if( (target && hover) || (flags & UI_TEXTBOX_AUTOFOCUS) ){
+ ui_defocus_all();
+
+ ui_fill( rect, col_highlight );
+ vg_ui.ignore_input_frames = 2;
+ rect_copy( rect, vg_ui.click_fader );
+ rect_copy( rect, vg_ui.click_fader_end );
+
+ vg_ui.click_fade_opacity = 1.0f;
+ vg_ui.textbuf = buf;
+ vg_ui.focused_control_hit = 1;
+ vg_ui.focused_control_type = k_ui_control_textbox;
+ vg_ui.textbox.len = len;
+ vg_ui.textbox.flags = flags;
+ vg_ui.textbox.cursor_pos = 0;
+ vg_ui.textbox.cursor_user = 0;
+
+ if( callbacks ){
+ vg_ui.textbox.callbacks = *callbacks;
+ }
+ else{
+ vg_ui.textbox.callbacks.change = NULL;
+ vg_ui.textbox.callbacks.down = NULL;
+ vg_ui.textbox.callbacks.up = NULL;
+ vg_ui.textbox.callbacks.enter = NULL;
+ }
+
+ SDL_StartTextInput();
+ }
+ }
+
+ ui_fill( rect, col_base );
+
+ if( hover ){
+ ui_outline( rect, -1, col_highlight, 0 );
+ }
+
+ ui_ntext( text_rect, buf, wrap_length, 1, k_ui_align_left, 0 );
+ return 0;
+}
+
+/*
+ * Tabs
+ * -----------------------------------------------------------------------------
+ */
+
+static void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel,
+ const char **titles, u32 count, i32 *page ){
+ ui_rect bar;
+ ui_standard_widget( inout_panel, bar, 1 );
+
+ i32 cur_page = *page;
+
+ f32 width = (f32)inout_panel[2] / (f32)count;
+
+ ui_px h = (inout_panel[1] + inout_panel[3]) - (bar[1]+bar[3]);
+ inout_panel[1] = bar[1]+bar[3];
+ inout_panel[3] = h;
+
+ ui_fill( inout_panel, ui_colour( k_ui_bg+2 ) );
+ ui_outline( inout_panel, 1, ui_colour( k_ui_bg+5 ), 0 );
+
+ rect_copy( inout_panel, out_content_panel );
+ ui_rect_pad( out_content_panel, (ui_px[2]){ k_ui_padding, k_ui_padding } );
+
+ /* place buttons */
+ for( i32 i=0; i<count; i++ ){
+ ui_rect button = {
+ bar[0] + ((f32)i*width),
+ bar[1],
+ width,
+ bar[3]-1
+ };
+
+ enum ui_scheme_colour colour = k_ui_bg+4;
+ if( i == cur_page ){
+ colour = k_ui_bg+2;
+ ui_outline( button, 1, ui_colour( k_ui_bg+5 ),
+ UI_TOP|UI_LEFT|UI_RIGHT );
+ button[3] ++;
+ }
+
+ if( ui_colourbutton_text( button, titles[i], 1, colour ) == 1 )
+ *page = i;
+ }
+}
+
+/*
+ * Modal UI
+ * -----------------------------------------------------------------------------
+ */
+static void ui_start_modal( const char *message, u32 options ){
+ vg_ui.focused_control_type = k_ui_control_modal;
+ vg_ui.modal.message = message;
+ vg_ui.modal.callbacks.close = NULL;
+ vg_ui.modal.options = options;
+
+ u32 type = options & UI_MODAL_TYPE_BITS;
+ if( type == UI_MODAL_OK ) vg_info( message );
+ else if( type == UI_MODAL_WARN ) vg_warn( message );
+ else if( type == UI_MODAL_GOOD ) vg_success( message );
+ else if( type == UI_MODAL_BAD ) vg_error( message );
+}
+
+/*
+ * Input handling
+ * -----------------------------------------------------------------------------
+ */
+
+/*
+ * Handles binds
+ */
+static void _ui_proc_key( SDL_Keysym ev ){
+ if( vg_ui.focused_control_type != k_ui_control_textbox ){
+ return;
+ }
+
+ struct textbox_mapping{
+ u16 mod;
+ SDL_Keycode key;
+
+ void (*handler)(void);
+ }
+ mappings[] =
+ {
+ { 0, SDLK_LEFT, _ui_textbox_left },
+ { KMOD_SHIFT, SDLK_LEFT, _ui_textbox_left_select },
+ { 0, SDLK_RIGHT, _ui_textbox_right },
+ { KMOD_SHIFT, SDLK_RIGHT, _ui_textbox_right_select },
+ { 0, SDLK_DOWN, _ui_textbox_down },
+ { 0, SDLK_UP, _ui_textbox_up },
+ { 0, SDLK_BACKSPACE, _ui_textbox_backspace },
+ { KMOD_SHIFT, SDLK_BACKSPACE, _ui_textbox_backspace },
+ { KMOD_CTRL, SDLK_BACKSPACE, _ui_textbox_backspace },
+ { 0, SDLK_DELETE, _ui_textbox_delete },
+ { 0, SDLK_HOME, _ui_textbox_home },
+ { KMOD_SHIFT, SDLK_HOME, _ui_textbox_home_select },
+ { 0, SDLK_END, _ui_textbox_end },
+ { KMOD_SHIFT, SDLK_END, _ui_textbox_end_select },
+ { KMOD_CTRL, SDLK_a, _ui_textbox_select_all },
+ { KMOD_CTRL, SDLK_c, _ui_textbox_to_clipboard },
+ { KMOD_CTRL, SDLK_x, _ui_textbox_cut },
+ { KMOD_CTRL, SDLK_v, _ui_textbox_clipboard_paste },
+ { 0, SDLK_RETURN, _ui_textbox_enter },
+ { 0, SDLK_ESCAPE, ui_defocus_all },
+ };
+
+ SDL_Keymod mod = 0;
+
+ if( ev.mod & KMOD_SHIFT )
+ mod |= KMOD_SHIFT;
+
+ if( ev.mod & KMOD_CTRL )
+ mod |= KMOD_CTRL;
+
+ if( ev.mod & KMOD_ALT )
+ mod |= KMOD_ALT;
+
+ for( int i=0; i<vg_list_size( mappings ); i++ ){
+ struct textbox_mapping *mapping = &mappings[i];
+
+ if( mapping->key == ev.sym ){
+ if( mapping->mod == 0 ){
+ if( mod == 0 ){
+ mapping->handler();
+ return;
+ }
+ }
+ else if( (mod & mapping->mod) == mapping->mod ){
+ mapping->handler();
+ return;
+ }
+ }
+ }
+}
+
+/*
+ * Callback for text entry mode
+ */
+static void ui_proc_utf8( const char *text ){
+ if( vg_ui.focused_control_type == k_ui_control_textbox ){
+ const char *ptr = text;
+
+ while( *ptr ){
+ if( *ptr != '`' ) _ui_textbox_put_char( *ptr );
+ ptr ++;
+ }
+
+ if( vg_ui.textbox.callbacks.change ){
+ vg_ui.textbox.callbacks.change( vg_ui.textbuf, vg_ui.textbox.len );
+ }
+ }
+}
+
+/*
+ * Development utils
+ * -----------------------------------------------------------------------------
+ */
+
+static void ui_dev_colourview(void){
+ ui_rect window = {vg.window_x-256,0,256,vg.window_y}, swatch;
+
+ const char *names[vg_list_size(vg_ui.scheme)] = {
+ [k_ui_bg] = "k_ui_bg", "k_ui_bg+1", "k_ui_bg+2", "k_ui_bg+3",
+ "k_ui_bg+4", "k_ui_bg+5", "k_ui_bg+6", "k_ui_bg+7",
+
+ [k_ui_fg] = "k_ui_fg", "k_ui_fg+1", "k_ui_fg+2", "k_ui_fg+3",
+ "k_ui_fg+4", "k_ui_fg+5", "k_ui_fg+6", "k_ui_fg+7",
+
+ [k_ui_red] = "k_ui_red", "k_ui_orange", "k_ui_yellow", "k_ui_green",
+ "k_ui_aqua", "k_ui_blue", "k_ui_purple", "k_ui_gray",
+ "k_ui_red+8","k_ui_orange+8","k_ui_yellow+8","k_ui_green+8",
+ "k_ui_aqua+8","k_ui_blue+8","k_ui_purple+8","k_ui_gray+8" };
+
+ ui_rect col[2];
+ ui_split_ratio( window, k_ui_axis_v, 0.5f, 0, col[0], col[1] );
+
+ for( int i=0; i<vg_list_size(vg_ui.scheme); i++ ){
+ int which = (i/8)%2;
+
+ ui_split( col[which], k_ui_axis_h, 24, 0, swatch, col[which] );
+ ui_fill( swatch, ui_colour(i) );
+
+ if( names[i] )
+ ui_text(swatch, names[i], 1, k_ui_align_middle_left, ui_colourcont(i));
+ }