+
+ SDL_SetCursor( vg_ui.cursor_map[ vg_ui.cursor ] );
+ SDL_ShowCursor(1);
+}
+
+
+
+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.0f, col[0], col[1] );
+
+ for( int i=0; i<vg_list_size(vg_ui.scheme); i++ ){
+ int which = (i/8)%2;
+
+ ui_split_px( 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));
+ }
+}
+
+
+
+
+
+/*
+ * text box interface
+ */
+static void _ui_textbox_make_selection( int *start, int *end )
+{
+ *start = VG_MIN( vg_ui.cursor_pos, vg_ui.cursor_user );
+ *end = VG_MAX( vg_ui.cursor_pos, vg_ui.cursor_user );
+}
+
+static void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
+ int dir, int snap_together )
+{
+ *cursor0 = VG_MAX( 0, vg_ui.cursor_user + dir );
+ *cursor0 =
+ VG_MIN(
+ VG_MIN( vg_ui.textbuf_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.textbuf_len-1 );
+ int move_amount = strlen( vg_ui.textbuf )-datastart;
+ int move_end =
+ VG_MIN( move_to+move_amount, vg_ui.textbuf_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.textbuf_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_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 );
+ int cpylength = _ui_textbox_makeroom( datastart, length );
+
+ memcpy( vg_ui.textbuf + datastart, text, cpylength);
+ _ui_textbox_move_cursor( &vg_ui.cursor_user,
+ &vg_ui.cursor_pos, cpylength, 1 );
+ SDL_free( text );
+}
+
+static void _ui_textbox_put_char( char c )
+{
+ vg_ui.cursor_user = _ui_textbox_delete_char(0);
+
+ if( _ui_textbox_makeroom( vg_ui.cursor_user, 1 ) )
+ vg_ui.textbuf[ vg_ui.cursor_user ] = c;
+
+ _ui_textbox_move_cursor( &vg_ui.cursor_user, &vg_ui.cursor_pos, 1, 1 );
+}
+
+/* Receed secondary cursor */
+static void _ui_textbox_left_select(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, -1, 0 );
+}
+
+/* Match and receed both cursors */
+static void _ui_textbox_left(void)
+{
+ int cursor_diff = vg_ui.cursor_pos - vg_ui.cursor_user? 0: 1;
+
+ _ui_textbox_move_cursor( &vg_ui.cursor_user,
+ &vg_ui.cursor_pos, -cursor_diff, 1 );
+}
+
+static void _ui_textbox_right_select(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, 1, 0 );
+}
+
+static void _ui_textbox_right(void)
+{
+ int cursor_diff = vg_ui.cursor_pos - vg_ui.cursor_user? 0: 1;
+
+ _ui_textbox_move_cursor( &vg_ui.cursor_user,
+ &vg_ui.cursor_pos, +cursor_diff, 1 );
+}
+
+static void _ui_textbox_backspace(void)
+{
+ vg_ui.cursor_user = _ui_textbox_delete_char( -1 );
+ vg_ui.cursor_pos = vg_ui.cursor_user;
+}
+
+static void _ui_textbox_delete(void)
+{
+ vg_ui.cursor_user = _ui_textbox_delete_char( 1 );
+ vg_ui.cursor_pos = vg_ui.cursor_user;
+}
+
+static void _ui_textbox_home_select(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, -10000, 0 );
+}
+
+static void _ui_textbox_home(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user,
+ &vg_ui.cursor_pos, -10000, 1 );
+}
+
+static void _ui_textbox_end_select(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, 10000, 0 );
+}
+
+static void _ui_textbox_end(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user,
+ &vg_ui.cursor_pos,
+ vg_ui.textbuf_len-1, 1 );
+}
+
+static void _ui_textbox_select_all(void)
+{
+ _ui_textbox_move_cursor( &vg_ui.cursor_user, NULL, 10000, 0);
+ _ui_textbox_move_cursor( &vg_ui.cursor_pos, NULL, -10000, 0);
+}
+
+static void _ui_textbox_cut(void)
+{
+ _ui_textbox_to_clipboard();
+ vg_ui.cursor_user = _ui_textbox_delete_char(0);
+ vg_ui.cursor_pos = vg_ui.cursor_user;
+}
+
+static void _ui_textbox_enter(void)
+{
+ if( !strlen( vg_ui.textbuf ) )
+ return;
+}
+
+/*
+ * Handles binds
+ */
+static void _ui_proc_key( SDL_Keysym ev )
+{
+ 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 },
+#if 0
+ { 0, SDLK_DOWN, _ui_textbox_down },
+ { 0, SDLK_UP, _ui_textbox_up },
+#endif
+ { 0, 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 },
+#if 0
+ { KMOD_CTRL, SDLK_n, _ui_textbox_suggest_next },
+ { KMOD_CTRL, SDLK_p, _ui_textbox_suggest_prev }
+#endif
+ { 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
+ */
+VG_STATIC void ui_proc_utf8( const char *text )
+{
+ const char *ptr = text;
+
+ while( *ptr ){
+ if( *ptr != '`' ) _ui_textbox_put_char( *ptr );
+ ptr ++;
+ }