+/*
+ * 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,