Commit: 38bbd9c778d6733f21732df1f51dd2dc5236fa44
Author: Campbell Barton
Date:   Mon Jan 6 00:36:09 2014 +1100
https://developer.blender.org/rB38bbd9c778d6733f21732df1f51dd2dc5236fa44

Text3d: paste additions
- Add paste from system clipboard which behaves like paste from file.
- Paste from file now replaces the selection rather then just adding to the end.
- Move paste operations into the 'Edit' menu.
- Added generic paste functions: font_paste_wchar, font_paste_utf8.
- Fix paste max length check not taking the selection length into account.

===================================================================

M       release/scripts/startup/bl_ui/space_view3d.py
M       source/blender/blenkernel/BKE_font.h
M       source/blender/editors/curve/curve_intern.h
M       source/blender/editors/curve/curve_ops.c
M       source/blender/editors/curve/editfont.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/space_view3d.py 
b/release/scripts/startup/bl_ui/space_view3d.py
index e0f5342..1fed424 100644
--- a/release/scripts/startup/bl_ui/space_view3d.py
+++ b/release/scripts/startup/bl_ui/space_view3d.py
@@ -728,6 +728,11 @@ class VIEW3D_MT_select_edit_text(Menu):
 
         layout.separator()
 
+        layout.operator("font.text_paste_from_file")
+        layout.operator("font.text_paste_from_clipboard")
+
+        layout.separator()
+
         layout.operator("font.select_all")
 
 
@@ -2435,10 +2440,6 @@ class VIEW3D_MT_edit_font(Menu):
     def draw(self, context):
         layout = self.layout
 
-        layout.operator("font.file_paste")
-
-        layout.separator()
-
         layout.menu("VIEW3D_MT_edit_text_chars")
 
         layout.separator()
diff --git a/source/blender/blenkernel/BKE_font.h 
b/source/blender/blenkernel/BKE_font.h
index 199b194..9c6a28d 100644
--- a/source/blender/blenkernel/BKE_font.h
+++ b/source/blender/blenkernel/BKE_font.h
@@ -60,7 +60,7 @@ typedef struct SelBox {
 
 typedef struct EditFont {      
        wchar_t *copybuf;
-       wchar_t *copybufinfo;
+       struct CharInfo *copybufinfo;
        
        wchar_t *textbuf;
        struct CharInfo *textbufinfo;
diff --git a/source/blender/editors/curve/curve_intern.h 
b/source/blender/editors/curve/curve_intern.h
index 878e024..d54db77 100644
--- a/source/blender/editors/curve/curve_intern.h
+++ b/source/blender/editors/curve/curve_intern.h
@@ -61,7 +61,8 @@ void FONT_OT_select_all(struct wmOperatorType *ot);
 void FONT_OT_text_copy(struct wmOperatorType *ot);
 void FONT_OT_text_cut(struct wmOperatorType *ot);
 void FONT_OT_text_paste(struct wmOperatorType *ot);
-void FONT_OT_file_paste(struct wmOperatorType *ot);
+void FONT_OT_text_paste_from_file(struct wmOperatorType *ot);
+void FONT_OT_text_paste_from_clipboard(struct wmOperatorType *ot);
 
 void FONT_OT_move(struct wmOperatorType *ot);
 void FONT_OT_move_select(struct wmOperatorType *ot);
diff --git a/source/blender/editors/curve/curve_ops.c 
b/source/blender/editors/curve/curve_ops.c
index 6186440..4f95bce 100644
--- a/source/blender/editors/curve/curve_ops.c
+++ b/source/blender/editors/curve/curve_ops.c
@@ -70,7 +70,8 @@ void ED_operatortypes_curve(void)
        WM_operatortype_append(FONT_OT_text_copy);
        WM_operatortype_append(FONT_OT_text_cut);
        WM_operatortype_append(FONT_OT_text_paste);
-       WM_operatortype_append(FONT_OT_file_paste);
+       WM_operatortype_append(FONT_OT_text_paste_from_file);
+       WM_operatortype_append(FONT_OT_text_paste_from_clipboard);
 
        WM_operatortype_append(FONT_OT_move);
        WM_operatortype_append(FONT_OT_move_select);
diff --git a/source/blender/editors/curve/editfont.c 
b/source/blender/editors/curve/editfont.c
index 3118dd6..2263c69 100644
--- a/source/blender/editors/curve/editfont.c
+++ b/source/blender/editors/curve/editfont.c
@@ -78,6 +78,8 @@
 
 #define MAXTEXT 32766
 
+static int kill_selection(Object *obedit, int ins);
+
 /************************* utilities ******************************/
 
 static char findaccent(char char1, unsigned int code)
@@ -245,7 +247,7 @@ static int insert_into_textbuf(Object *obedit, uintptr_t c)
                return 0;
 }
 
-static void text_update_edited(bContext *C, Scene *scene, Object *obedit, int 
recalc, int mode)
+static void text_update_edited(bContext *C, Scene *scene, Object *obedit, 
const bool recalc, int mode)
 {
        struct Main *bmain = CTX_data_main(C);
        Curve *cu = obedit->data;
@@ -321,20 +323,80 @@ void FONT_OT_insert_lorem(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
-/******************* paste file operator ********************/
+/* -------------------------------------------------------------------- */
+/* Generic Paste Functions */
+
+/* text_update_edited(C, scene, obedit, 1, FO_EDIT); */
+static bool font_paste_wchar(Object *obedit, const wchar_t *str, const size_t 
str_len,
+                             /* optional */
+                             struct CharInfo *str_info)
+{
+       Curve *cu = obedit->data;
+       EditFont *ef = cu->editfont;
+       int selend, selstart;
+
+       if (BKE_vfont_select_get(obedit, &selstart, &selend) == 0) {
+               selstart = selend = 0;
+       }
+
+       /* Verify that the copy buffer => [copy buffer len] + ef->len < MAXTEXT 
*/
+       if ((ef->len + str_len) - (selend - selstart) <= MAXTEXT) {
+
+               kill_selection(obedit, 0);
+
+               if (str_len) {
+                       int size = (ef->len * sizeof(wchar_t)) - (ef->pos * 
sizeof(wchar_t)) + sizeof(wchar_t);
+                       memmove(ef->textbuf + ef->pos + str_len, ef->textbuf + 
ef->pos, size);
+                       memcpy(ef->textbuf + ef->pos, str, str_len * 
sizeof(wchar_t));
+
+                       memmove(ef->textbufinfo + ef->pos + str_len, 
ef->textbufinfo + ef->pos, (ef->len - ef->pos + 1) * sizeof(CharInfo));
+                       if (str_info) {
+                               memcpy(ef->textbufinfo + ef->pos, str_info, 
str_len * sizeof(CharInfo));
+                       }
+                       else {
+                               memset(ef->textbufinfo + ef->pos, '\0', str_len 
* sizeof(CharInfo));
+                       }
+
+                       ef->len += str_len;
+                       ef->pos += str_len;
+               }
+
+               return true;
+       }
+
+       return false;
+}
+
+static bool font_paste_utf8(bContext *C, const char *str, const size_t str_len)
+{
+       Object *obedit = CTX_data_edit_object(C);
+       bool retval;
+
+       int tmplen;
+
+       wchar_t *mem = MEM_mallocN((sizeof(wchar_t) * (str_len + 1)), __func__);
+
+       tmplen = BLI_strncpy_wchar_from_utf8(mem, str, str_len + 1);
+
+       retval = font_paste_wchar(obedit, mem, tmplen, NULL);
+
+       MEM_freeN(mem);
+
+       return retval;
+}
+
 
-/* note this handles both ascii and utf8 unicode, previously
- * there were 3 functions that did effectively the same thing. */
+/* -------------------------------------------------------------------- */
+/* Paste From File*/
 
-static int paste_file(bContext *C, ReportList *reports, const char *filename)
+static int paste_from_file(bContext *C, ReportList *reports, const char 
*filename)
 {
        Scene *scene = CTX_data_scene(C);
        Object *obedit = CTX_data_edit_object(C);
-       Curve *cu = obedit->data;
-       EditFont *ef = cu->editfont;
        FILE *fp;
-       int filelen;
        char *strp;
+       int filelen;
+       int retval;
 
        fp = BLI_fopen(filename, "r");
 
@@ -356,55 +418,52 @@ static int paste_file(bContext *C, ReportList *reports, 
const char *filename)
        fclose(fp);
        strp[filelen] = 0;
 
-       if (ef->len + filelen < MAXTEXT) {
-               int tmplen;
-               wchar_t *mem = MEM_mallocN((sizeof(wchar_t) * filelen) + (4 * 
sizeof(wchar_t)), "temporary");
-               tmplen = BLI_strncpy_wchar_from_utf8(mem, strp, filelen + 1);
-               wcscat(ef->textbuf, mem);
-               MEM_freeN(mem);
-               ef->len += tmplen;
+       if (font_paste_utf8(C, strp, filelen)) {
+               text_update_edited(C, scene, obedit, 1, FO_EDIT);
+               retval = OPERATOR_FINISHED;
+
+       }
+       else {
+               BKE_reportf(reports, RPT_ERROR, "File too long %s", filename);
+               retval = OPERATOR_CANCELLED;
        }
        MEM_freeN(strp);
 
-       ef->pos = ef->len;
-
-       text_update_edited(C, scene, obedit, 1, FO_EDIT);
-
-       return OPERATOR_FINISHED;
+       return retval;
 }
 
-static int paste_file_exec(bContext *C, wmOperator *op)
+static int paste_from_file_exec(bContext *C, wmOperator *op)
 {
        char *path;
        int retval;
        
        path = RNA_string_get_alloc(op->ptr, "filepath", NULL, 0);
-       retval = paste_file(C, op->reports, path);
+       retval = paste_from_file(C, op->reports, path);
        MEM_freeN(path);
 
        return retval;
 }
 
-static int paste_file_invoke(bContext *C, wmOperator *op, const wmEvent 
*UNUSED(event))
+static int paste_from_file_invoke(bContext *C, wmOperator *op, const wmEvent 
*UNUSED(event))
 {
        if (RNA_struct_property_is_set(op->ptr, "filepath"))
-               return paste_file_exec(C, op);
+               return paste_from_file_exec(C, op);
 
        WM_event_add_fileselect(C, op); 
 
        return OPERATOR_RUNNING_MODAL;
 }
 
-void FONT_OT_file_paste(wmOperatorType *ot)
+void FONT_OT_text_paste_from_file(wmOperatorType *ot)
 {
        /* identifiers */
        ot->name = "Paste File";
        ot->description = "Paste contents from file";
-       ot->idname = "FONT_OT_file_paste";
+       ot->idname = "FONT_OT_text_paste_from_file";
        
        /* api callbacks */
-       ot->exec = paste_file_exec;
-       ot->invoke = paste_file_invoke;
+       ot->exec = paste_from_file_exec;
+       ot->invoke = paste_from_file_invoke;
        ot->poll = ED_operator_editfont;
        
        /* flags */
@@ -415,6 +474,67 @@ void FONT_OT_file_paste(wmOperatorType *ot)
                                       WM_FILESEL_FILEPATH, 
FILE_DEFAULTDISPLAY);
 }
 
+
+/* -------------------------------------------------------------------- */
+/* Paste From Clipboard */
+
+static int paste_from_clipboard(bContext *C, ReportList *reports)
+{
+       Scene *scene = CTX_data_scene(C);
+       Object *obedit = CTX_data_edit_object(C);
+       char *strp;
+       int filelen;
+       int retval;
+
+       strp = WM_clipboard_text_get(false);
+       if (strp == NULL) {
+               BKE_report(reports, RPT_ERROR, "Clipboard empty");
+               return OPERATOR_CANCELLED;
+       }
+
+       filelen = strlen(strp);
+
+       if (font_paste_utf8(C, strp, filelen)) {
+               text_update_edited(C, scene, obedit, 1, FO_EDIT);
+               retval = OPERATOR_FINISHED;
+       }
+       else {
+               BKE_report(reports, RPT_ERROR, "Clipboard too long");
+               retval = OPERATOR_CANCELLED;
+       }
+       MEM_freeN(strp);
+
+       return retval;
+}
+
+static int paste_from_clipboard_exec(bContext *C, wmOperator *op)
+{
+       int retval;
+
+       retval = paste_from_clipboard(C, op->reports);
+
+       return retval;
+}
+
+void FONT_OT_text_paste_from_clipboard(wmOperatorType *ot)
+{
+       /* identifiers */
+       ot->name = "Paste Clipboard";
+       ot->description = "Paste contents from system clipboard";
+       ot->idname = "FONT_OT_text_paste_from_clipboard";
+
+       /* api callbacks */
+       ot->exec = paste_from_clipboard_exec;
+       ot->poll = ED_operator_editfont;
+
+       /* flags */
+       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+       /* properties */
+       WM_operator_properties_filesel(ot, FOLDERFILE | TEXTFILE, FILE_SPECIAL, 
FILE_OPENFILE,
+                                      WM_FILESEL_FILEPATH, 
FILE_DEFAULTDISPLAY);
+}
+
 /******************* text to object operator ********************/
 
 static void txt_add_object(bContext *C, TextLine *firstline, int totline, 
const float offset[3])
@@ -768,35 +888,19 @@ void FONT_OT_text_cut(wmOperatorType *ot)
 
 /******************* paste text operator ********************/
 
-static int paste_selection(Object *obedit, ReportList *reports)
+static bool paste_selection(Object *obedit, ReportList *reports)
 {
        Curve *cu = obedit->data;
        EditFont *ef = cu->editfont;
        int len = wcs

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to