minor changes to test editing
authorCampbell Barton <ideasman42@gmail.com>
Thu, 20 Oct 2011 07:12:14 +0000 (07:12 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 20 Oct 2011 07:12:14 +0000 (07:12 +0000)
- use BLI_strncpy_utf8 for utf8 buttons when pasting.
- reuse code for ui_textedit_type_ascii / ui_textedit_type_utf8.
- use memmove rather then for() loops in string editing.
- merge jump/all arguments in interface_handlers.c into one enum arg.

source/blender/editors/interface/interface.c
source/blender/editors/interface/interface_anim.c
source/blender/editors/interface/interface_handlers.c
source/blender/editors/interface/interface_intern.h

index ac1fab365f0e78a5690b919065b7a7bce4d47de1..16edd0a1a9e55b9ea34b214e005493ad7f47581e 100644 (file)
@@ -1747,7 +1747,9 @@ int ui_set_but_string(bContext *C, uiBut *but, const char *str)
        }
        else if(but->type == TEX) {
                /* string */
-               BLI_strncpy(but->poin, str, but->hardmax);
+               if(ui_is_but_utf8(but)) BLI_strncpy_utf8(but->poin, str, but->hardmax);
+               else                    BLI_strncpy(but->poin, str, but->hardmax);
+
                return 1;
        }
        else if(but->type == SEARCH_MENU) {
index 6c661ba014e7063ae997dfc690c78a6aa16f2d60..545d60bfcb9a65445e1f4bef1877a7990840af18 100644 (file)
@@ -115,7 +115,7 @@ int ui_but_anim_expression_set(uiBut *but, const char *str)
                driver= fcu->driver;
                
                if(driver && driver->type == DRIVER_TYPE_PYTHON) {
-                       BLI_strncpy(driver->expression, str, sizeof(driver->expression));
+                       BLI_strncpy_utf8(driver->expression, str, sizeof(driver->expression));
                        driver->flag |= DRIVER_FLAG_RECOMPILE;
                        WM_event_add_notifier(but->block->evil_C, NC_ANIMATION|ND_KEYFRAME, NULL);
                        return 1;
@@ -164,7 +164,7 @@ int ui_but_anim_expression_create(uiBut *but, const char *str)
                        
                        /* set the expression */
                        // TODO: need some way of identifying variables used
-                       BLI_strncpy(driver->expression, str, sizeof(driver->expression));
+                       BLI_strncpy_utf8(driver->expression, str, sizeof(driver->expression));
                        
                        /* FIXME: for now, assume that 
                         *      - for expressions, users are likely to be using "frame" -> current frame" as a variable
index 85103b4224448790d1305d05fe37ce554d98d5da..1f30f8a5109c6a2a5c2ada4269317c36a1240af2 100644 (file)
@@ -107,6 +107,12 @@ typedef enum uiHandleButtonState {
        BUTTON_STATE_EXIT
 } uiHandleButtonState;
 
+typedef enum uiButtonJumpType {
+       BUTTON_EDIT_JUMP_NONE,
+       BUTTON_EDIT_JUMP_DELIM,
+       BUTTON_EDIT_JUMP_ALL
+} uiButtonJumpType;
+
 typedef struct uiHandleButtonData {
        wmWindowManager *wm;
        wmWindow *window;
@@ -260,7 +266,7 @@ static int ui_is_a_warp_but(uiBut *but)
 }
 
 /* file selectors are exempt from utf-8 checks */
-static int ui_is_utf8_but(uiBut *but)
+int ui_is_but_utf8(uiBut *but)
 {
        if (but->rnaprop) {
                const int subtype= RNA_property_subtype(but->rnaprop);
@@ -1163,7 +1169,10 @@ static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data,
                }
                else {
                        button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
-                       BLI_strncpy(active_data->str, buf, active_data->maxlen);
+
+                       if(ui_is_but_utf8(but)) BLI_strncpy_utf8(active_data->str, buf, active_data->maxlen);
+                       else                    BLI_strncpy(active_data->str, buf, active_data->maxlen);
+
                        if(but->type == SEARCH_MENU) {
                                /* else uiSearchboxData.active member is not updated [#26856] */
                                ui_searchbox_update(C, data->searchbox, but, 1);
@@ -1282,18 +1291,18 @@ static int ui_textedit_step_prev_utf8(const char *str, size_t UNUSED(maxlen), sh
 
 static void ui_textedit_step_utf8(const char *str, size_t maxlen,
                                   short *pos, const char direction,
-                                  const short do_jump, const short do_all)
+                                  uiButtonJumpType jump)
 {
        const short pos_prev= *pos;
 
        if(direction) { /* right*/
-               if(do_jump) {
+               if(jump != BUTTON_EDIT_JUMP_NONE) {
                        /* jump between special characters (/,\,_,-, etc.),
                         * look at function test_special_char() for complete
                         * list of special character, ctr -> */
                        while((*pos) < maxlen) {
                                if (ui_textedit_step_next_utf8(str, maxlen, pos)) {
-                                       if(!do_all && test_special_char(str[(*pos)])) break;
+                                       if((jump != BUTTON_EDIT_JUMP_ALL) && test_special_char(str[(*pos)])) break;
                                }
                                else {
                                        break; /* unlikely but just incase */
@@ -1305,7 +1314,7 @@ static void ui_textedit_step_utf8(const char *str, size_t maxlen,
                }
        }
        else { /* left */
-               if(do_jump) {
+               if(jump != BUTTON_EDIT_JUMP_NONE) {
                        /* left only: compensate for index/change in direction */
                        ui_textedit_step_prev_utf8(str, maxlen, pos);
 
@@ -1314,7 +1323,7 @@ static void ui_textedit_step_utf8(const char *str, size_t maxlen,
                         * list of special character, ctr -> */
                        while ((*pos) > 0) {
                                if (ui_textedit_step_prev_utf8(str, maxlen, pos)) {
-                                       if(!do_all && test_special_char(str[(*pos)])) break;
+                                       if((jump != BUTTON_EDIT_JUMP_ALL) && test_special_char(str[(*pos)])) break;
                                }
                                else {
                                        break;
@@ -1432,24 +1441,23 @@ static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data,
 static int ui_textedit_type_utf8(uiBut *but, uiHandleButtonData *data, const char utf8_buf[6])
 {
        char *str;
-       int len, x, changed= 0;
-       size_t step= BLI_strnlen(utf8_buf, sizeof(utf8_buf));
+       int len, changed= 0;
 
        str= data->str;
        len= strlen(str);
 
        if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
+               int step= BLI_strnlen(utf8_buf, sizeof(utf8_buf));
+
                /* type over the current selection */
-               if ((but->selend - but->selsta) > 0)
+               if ((but->selend - but->selsta) > 0) {
                        changed= ui_textedit_delete_selection(but, data);
+                       len= strlen(str);
+               }
 
-               len= strlen(str);
-               if(len+step < data->maxlen) {
-                       for(x= data->maxlen; x>but->pos; x--)
-                               str[x]= str[x-step];
+               if(len + step < data->maxlen) {
+                       memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
                        memcpy(&str[but->pos], utf8_buf, step * sizeof(char));
-                       str[len+step]= '\0';
-
                        but->pos += step;
                        changed= 1;
                }
@@ -1460,33 +1468,11 @@ static int ui_textedit_type_utf8(uiBut *but, uiHandleButtonData *data, const cha
 
 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
 {
-       char *str;
-       int len, x, changed= 0;
-
-       str= data->str;
-       len= strlen(str);
-
-       if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
-               /* type over the current selection */
-               if ((but->selend - but->selsta) > 0)
-                       changed= ui_textedit_delete_selection(but, data);
-
-               len= strlen(str);
-               if(len+1 < data->maxlen) {
-                       for(x= data->maxlen; x>but->pos; x--)
-                               str[x]= str[x-1];
-                       str[but->pos]= ascii;
-                       str[len+1]= '\0';
-
-                       but->pos++; 
-                       changed= 1;
-               }
-       }
-
-       return changed;
+       char utf8_buf[6]= {ascii, '\0'};
+       return ui_textedit_type_utf8(but, data, utf8_buf);
 }
 
-static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction, int select, int jump, int jump_all)
+static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction, int select, uiButtonJumpType jump)
 {
        const char *str= data->str;
        const int len= strlen(str);
@@ -1495,7 +1481,7 @@ static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction
 
        /* special case, quit selection and set cursor */
        if (has_sel && !select) {
-               if (jump_all) {
+               if (jump == BUTTON_EDIT_JUMP_ALL) {
                        but->selsta = but->selend= but->pos = direction ? len : 0;
                }
                else {
@@ -1509,7 +1495,7 @@ static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction
                data->selextend = 0;
        }
        else {
-               ui_textedit_step_utf8(str, len, &but->pos, direction, jump, jump_all);
+               ui_textedit_step_utf8(str, len, &but->pos, direction, jump);
 
                if(select) {
                        /* existing selection */
@@ -1558,35 +1544,33 @@ static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction
        }
 }
 
-static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, const int all, const int jump)
+static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, uiButtonJumpType jump)
 {
        char *str= data->str;
        const int len= strlen(str);
 
-       int x, changed= 0;
+       int changed= 0;
 
-       if(all) {
+       if(jump == BUTTON_EDIT_JUMP_ALL) {
                if(len) changed=1;
-               str[0]= 0;
+               str[0]= '\0';
                but->pos= 0;
        }
        else if(direction) { /* delete */
                if ((but->selend - but->selsta) > 0) {
                        changed= ui_textedit_delete_selection(but, data);
                }
-               else if(but->pos>=0 && but->pos<len) {
+               else if (but->pos>=0 && but->pos<len) {
                        short pos= but->pos;
                        int step;
-                       ui_textedit_step_utf8(str, len, &pos, direction, jump, all);
+                       ui_textedit_step_utf8(str, len, &pos, direction, jump);
                        step= pos - but->pos;
-                       for(x=but->pos; x<len; x++)
-                               str[x]= str[x+step];
-                       str[len-step]='\0';
+                       memmove(&str[but->pos], &str[but->pos + step], (len + 1) - but->pos);
                        changed= 1;
                }
        }
        else { /* backspace */
-               if(len!=0) {
+               if (len != 0) {
                        if ((but->selend - but->selsta) > 0) {
                                changed= ui_textedit_delete_selection(but, data);
                        }
@@ -1594,13 +1578,9 @@ static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int directio
                                short pos= but->pos;
                                int step;
 
-                               ui_textedit_step_utf8(str, len, &pos, direction, jump, all);
+                               ui_textedit_step_utf8(str, len, &pos, direction, jump);
                                step= but->pos - pos;
-
-                               for(x=but->pos; x<len; x++)
-                                       str[x-step]= str[x];
-                               str[len-step]='\0';
-
+                               memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
                                but->pos -= step;
                                changed= 1;
                        }
@@ -1710,19 +1690,9 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
        ui_get_but_string(but, data->str, data->maxlen);
 
        if(ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
-               /* XXX: we dont have utf editing yet so for numbers its best to strip out utf chars 
-                * this is so the deg' synbol isnt included in number editing fields: bug 22274 */
-               int i;
-               for(i=0; data->str[i]; i++) {
-                       if(!isascii(data->str[i])) {
-                               /* no stripping actually: just convert to alt name */
-                               ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
-                               break;
-                       }
-               }
+               ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
        }
-       
-       
+
        data->origstr= BLI_strdup(data->str);
        data->selextend= 0;
        data->selstartx= 0;
@@ -1747,7 +1717,7 @@ static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
 {
        if(but) {
-               if(ui_is_utf8_but(but)) {
+               if(ui_is_but_utf8(but)) {
                        int strip= BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
                        /* not a file?, strip non utf-8 chars */
                        if(strip) {
@@ -1899,11 +1869,11 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                }
                                break;
                        case RIGHTARROWKEY:
-                               ui_textedit_move(but, data, 1, event->shift, event->ctrl, FALSE);
+                               ui_textedit_move(but, data, 1, event->shift, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
                                retval= WM_UI_HANDLER_BREAK;
                                break;
                        case LEFTARROWKEY:
-                               ui_textedit_move(but, data, 0, event->shift, event->ctrl, FALSE);
+                               ui_textedit_move(but, data, 0, event->shift, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
                                retval= WM_UI_HANDLER_BREAK;
                                break;
                        case DOWNARROWKEY:
@@ -1913,7 +1883,7 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                }
                                /* pass on purposedly */
                        case ENDKEY:
-                               ui_textedit_move(but, data, 1, event->shift, TRUE, TRUE);
+                               ui_textedit_move(but, data, 1, event->shift, BUTTON_EDIT_JUMP_ALL);
                                retval= WM_UI_HANDLER_BREAK;
                                break;
                        case UPARROWKEY:
@@ -1923,7 +1893,7 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                }
                                /* pass on purposedly */
                        case HOMEKEY:
-                               ui_textedit_move(but, data, 0, event->shift, TRUE, TRUE);
+                               ui_textedit_move(but, data, 0, event->shift, BUTTON_EDIT_JUMP_ALL);
                                retval= WM_UI_HANDLER_BREAK;
                                break;
                        case PADENTER:
@@ -1932,12 +1902,12 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                retval= WM_UI_HANDLER_BREAK;
                                break;
                        case DELKEY:
-                               changed= ui_textedit_delete(but, data, 1, 0, event->ctrl);
+                               changed= ui_textedit_delete(but, data, 1, event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE);
                                retval= WM_UI_HANDLER_BREAK;
                                break;
 
                        case BACKSPACEKEY:
-                               changed= ui_textedit_delete(but, data, 0, event->shift, event->ctrl);
+                               changed= ui_textedit_delete(but, data, 0, event->shift ? BUTTON_EDIT_JUMP_ALL :  (event->ctrl ? BUTTON_EDIT_JUMP_DELIM : BUTTON_EDIT_JUMP_NONE));
                                retval= WM_UI_HANDLER_BREAK;
                                break;
                                
@@ -1969,9 +1939,10 @@ static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandle
                                if(event->type == PADPERIOD && ascii == ',')
                                        ascii = '.';
 
-                       if(event->utf8_buf[0]) {
+                       if(event->utf8_buf[0] || 1) {
                                /* keep this printf until utf8 is well tested */
                                printf("%s: utf8 char '%s'\n", __func__, event->utf8_buf);
+                               // strcpy(event->utf8_buf, "12345");
                                changed= ui_textedit_type_utf8(but, data, event->utf8_buf);
                        }
                        else {
index c5843397c12b4485ec623a3c53cabc86a035c122..b7a2227f98ace5f337141c085bd142304a572b15 100644 (file)
@@ -373,6 +373,7 @@ extern void ui_check_but(uiBut *but);
 extern int  ui_is_but_float(uiBut *but);
 extern int  ui_is_but_unit(uiBut *but);
 extern int  ui_is_but_rna_valid(uiBut *but);
+extern int  ui_is_but_utf8(uiBut *but);
 
 extern void ui_bounds_block(uiBlock *block);
 extern void ui_block_translate(uiBlock *block, int x, int y);