2.5 filebrowser
[blender.git] / source / blender / editors / curve / editfont.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <wchar.h>
32
33 #ifndef WIN32 
34 #include <unistd.h>
35 #else
36 #include <io.h>
37 #endif
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLI_blenlib.h"
42 #include "BLI_arithb.h"
43
44 #include "DNA_curve_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_vfont_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_text_types.h"
49 #include "DNA_view3d_types.h"
50 #include "DNA_userdef_types.h"
51
52 #include "BKE_context.h"
53 #include "BKE_curve.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_font.h"
56 #include "BKE_global.h"
57 #include "BKE_main.h"
58 #include "BKE_object.h"
59 #include "BKE_report.h"
60 #include "BKE_utildefines.h"
61
62 #include "RNA_access.h"
63 #include "RNA_define.h"
64
65 #include "WM_api.h"
66 #include "WM_types.h"
67
68 #include "ED_curve.h"
69 #include "ED_object.h"
70 #include "ED_screen.h"
71 #include "ED_util.h"
72
73 #include "curve_intern.h"
74
75 #define MAXTEXT 32766
76
77 /************************* utilities ******************************/
78
79 static char findaccent(char char1, unsigned int code)
80 {
81         char new= 0;
82         
83         if(char1=='a') {
84                 if(code=='`') new= 224;
85                 else if(code==39) new= 225;
86                 else if(code=='^') new= 226;
87                 else if(code=='~') new= 227;
88                 else if(code=='"') new= 228;
89                 else if(code=='o') new= 229;
90                 else if(code=='e') new= 230;
91                 else if(code=='-') new= 170;
92         }
93         else if(char1=='c') {
94                 if(code==',') new= 231;
95                 if(code=='|') new= 162;
96         }
97         else if(char1=='e') {
98                 if(code=='`') new= 232;
99                 else if(code==39) new= 233;
100                 else if(code=='^') new= 234;
101                 else if(code=='"') new= 235;
102         }
103         else if(char1=='i') {
104                 if(code=='`') new= 236;
105                 else if(code==39) new= 237;
106                 else if(code=='^') new= 238;
107                 else if(code=='"') new= 239;
108         }
109         else if(char1=='n') {
110                 if(code=='~') new= 241;
111         }
112         else if(char1=='o') {
113                 if(code=='`') new= 242;
114                 else if(code==39) new= 243;
115                 else if(code=='^') new= 244;
116                 else if(code=='~') new= 245;
117                 else if(code=='"') new= 246;
118                 else if(code=='/') new= 248;
119                 else if(code=='-') new= 186;
120                 else if(code=='e') new= 143;
121         }
122         else if(char1=='s') {
123                 if(code=='s') new= 167;
124         }
125         else if(char1=='u') {
126                 if(code=='`') new= 249;
127                 else if(code==39) new= 250;
128                 else if(code=='^') new= 251;
129                 else if(code=='"') new= 252;
130         }
131         else if(char1=='y') {
132                 if(code==39) new= 253;
133                 else if(code=='"') new= 255;
134         }
135         else if(char1=='A') {
136                 if(code=='`') new= 192;
137                 else if(code==39) new= 193;
138                 else if(code=='^') new= 194;
139                 else if(code=='~') new= 195;
140                 else if(code=='"') new= 196;
141                 else if(code=='o') new= 197;
142                 else if(code=='e') new= 198;
143         }
144         else if(char1=='C') {
145                 if(code==',') new= 199;
146         }
147         else if(char1=='E') {
148                 if(code=='`') new= 200;
149                 else if(code==39) new= 201;
150                 else if(code=='^') new= 202;
151                 else if(code=='"') new= 203;
152         }
153         else if(char1=='I') {
154                 if(code=='`') new= 204;
155                 else if(code==39) new= 205;
156                 else if(code=='^') new= 206;
157                 else if(code=='"') new= 207;
158         }
159         else if(char1=='N') {
160                 if(code=='~') new= 209;
161         }
162         else if(char1=='O') {
163                 if(code=='`') new= 210;
164                 else if(code==39) new= 211;
165                 else if(code=='^') new= 212;
166                 else if(code=='~') new= 213;
167                 else if(code=='"') new= 214;
168                 else if(code=='/') new= 216;
169                 else if(code=='e') new= 141;
170         }
171         else if(char1=='U') {
172                 if(code=='`') new= 217;
173                 else if(code==39) new= 218;
174                 else if(code=='^') new= 219;
175                 else if(code=='"') new= 220;
176         }
177         else if(char1=='Y') {
178                 if(code==39) new= 221;
179         }
180         else if(char1=='1') {
181                 if(code=='4') new= 188;
182                 if(code=='2') new= 189;
183         }
184         else if(char1=='3') {
185                 if(code=='4') new= 190;
186         }
187         else if(char1==':') {
188                 if(code=='-') new= 247;
189         }
190         else if(char1=='-') {
191                 if(code==':') new= 247;
192                 if(code=='|') new= 135;
193                 if(code=='+') new= 177;
194         }
195         else if(char1=='|') {
196                 if(code=='-') new= 135;
197                 if(code=='=') new= 136;
198         }
199         else if(char1=='=') {
200                 if(code=='|') new= 136;
201         }
202         else if(char1=='+') {
203                 if(code=='-') new= 177;
204         }
205         
206         if(new) return new;
207         else return char1;
208 }
209
210
211 void update_string(Curve *cu)
212 {
213         EditFont *ef= cu->editfont;
214         int len;
215
216         // Free the old curve string    
217         MEM_freeN(cu->str);
218
219         // Calculate the actual string length in UTF-8 variable characters
220         len = wcsleninu8(ef->textbuf);
221
222         // Alloc memory for UTF-8 variable char length string
223         cu->str = MEM_callocN(len + sizeof(wchar_t), "str");
224
225         // Copy the wchar to UTF-8
226         wcs2utf8s(cu->str, ef->textbuf);
227 }
228
229 static int insert_into_textbuf(Object *obedit, uintptr_t c)
230 {
231         Curve *cu= obedit->data;
232         
233         if(cu->len<MAXTEXT-1) {
234                 EditFont *ef= cu->editfont;
235                 int x;
236
237                 for(x= cu->len; x>cu->pos; x--) ef->textbuf[x]= ef->textbuf[x-1];
238                 for(x= cu->len; x>cu->pos; x--) ef->textbufinfo[x]= ef->textbufinfo[x-1];               
239                 ef->textbuf[cu->pos]= c;
240                 ef->textbufinfo[cu->pos] = cu->curinfo;
241                 ef->textbufinfo[cu->pos].kern = 0;
242                 if(obedit->actcol>0)
243                         ef->textbufinfo[cu->pos].mat_nr = obedit->actcol;
244                 else
245                         ef->textbufinfo[cu->pos].mat_nr = 0;
246                                         
247                 cu->pos++;
248                 cu->len++;
249                 ef->textbuf[cu->len]='\0';
250
251                 update_string(cu);
252
253                 return 1;
254         }
255         else
256                 return 0;
257 }
258
259 static void text_update_edited(bContext *C, Scene *scene, Object *obedit, int recalc, int mode)
260 {
261         Curve *cu= obedit->data;
262         EditFont *ef= cu->editfont;
263
264         if(cu->pos)
265                 cu->curinfo = ef->textbufinfo[cu->pos-1];
266         else
267                 cu->curinfo = ef->textbufinfo[0];
268         
269         if(obedit->totcol>0)
270                 obedit->actcol= ef->textbufinfo[cu->pos-1].mat_nr;
271
272         update_string(cu);
273         BKE_text_to_curve(scene, obedit, mode);
274
275         if(recalc)
276                 DAG_id_flush_update(obedit->data, OB_RECALC_DATA);
277         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
278 }
279
280 /********************** insert lorem operator *********************/
281
282 static int insert_lorem_exec(bContext *C, wmOperator *op)
283 {
284         Object *obedit= CTX_data_edit_object(C);
285         char *p, *p2;
286         int i;
287         static char *lastlorem;
288         
289         if(lastlorem)
290                 p= lastlorem;
291         else
292                 p= ED_lorem;
293         
294         i= rand()/(RAND_MAX/6)+4;       
295                 
296         for(p2=p; *p2 && i; p2++) {
297                 insert_into_textbuf(obedit, *p2);
298
299                 if(*p2=='.')
300                         i--;
301         }
302
303         lastlorem = p2+1;
304         if(strlen(lastlorem)<5)
305                 lastlorem = ED_lorem;
306         
307         insert_into_textbuf(obedit, '\n');
308         insert_into_textbuf(obedit, '\n');      
309
310         DAG_id_flush_update(obedit->data, OB_RECALC_DATA);
311         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
312
313         return OPERATOR_FINISHED;
314 }
315
316 void FONT_OT_insert_lorem(wmOperatorType *ot)
317 {
318         /* identifiers */
319         ot->name= "Insert Lorem";
320     ot->description= "Insert placeholder text.";
321         ot->idname= "FONT_OT_insert_lorem";
322         
323         /* api callbacks */
324         ot->exec= insert_lorem_exec;
325         ot->poll= ED_operator_editfont;
326         
327         /* flags */
328         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
329 }
330
331 /******************* paste file operator ********************/
332
333 /* note this handles both ascii and utf8 unicode, previously
334  * there were 3 functions that did effectively the same thing. */
335
336 static int paste_file(bContext *C, ReportList *reports, char *filename)
337 {
338         Scene *scene= CTX_data_scene(C);
339         Object *obedit= CTX_data_edit_object(C);
340         Curve *cu= obedit->data;
341         EditFont *ef= cu->editfont;
342         FILE *fp;
343         int filelen;
344         char *strp;
345
346         fp= fopen(filename, "r");
347
348         if(!fp) {
349                 if(reports)
350                         BKE_reportf(reports, RPT_ERROR, "Failed to open file %s.", filename);
351                 return OPERATOR_CANCELLED;
352         }
353
354         fseek(fp, 0L, SEEK_END);
355         filelen = ftell(fp);
356         fseek(fp, 0L, SEEK_SET);
357
358         strp= MEM_callocN(filelen+4, "tempstr");
359
360         // fread() instead of read(), because windows read() converts text
361         // to DOS \r\n linebreaks, causing double linebreaks in the 3d text
362         filelen = fread(strp, 1, filelen, fp);
363         fclose(fp);
364         strp[filelen]= 0;
365
366         if(cu->len+filelen<MAXTEXT) {
367                 int tmplen;
368                 wchar_t *mem = MEM_callocN((sizeof(wchar_t)*filelen)+(4*sizeof(wchar_t)), "temporary");
369                 tmplen = utf8towchar(mem, strp);
370                 wcscat(ef->textbuf, mem);
371                 MEM_freeN(mem);
372                 cu->len += tmplen;
373                 cu->pos= cu->len;
374         }
375         MEM_freeN(strp);
376
377         text_update_edited(C, scene, obedit, 1, 0);
378
379         return OPERATOR_FINISHED;
380 }
381
382 static int paste_file_exec(bContext *C, wmOperator *op)
383 {
384         char *filename;
385         int retval;
386         
387         filename= RNA_string_get_alloc(op->ptr, "filename", NULL, 0);
388         retval= paste_file(C, op->reports, filename);
389         MEM_freeN(filename);
390
391         return retval;
392 }
393
394 static int paste_file_invoke(bContext *C, wmOperator *op, wmEvent *event)
395 {
396         if(RNA_property_is_set(op->ptr, "filename"))
397                 return paste_file_exec(C, op);
398
399         WM_event_add_fileselect(C, op); 
400
401         return OPERATOR_RUNNING_MODAL;
402 }
403
404 void FONT_OT_file_paste(wmOperatorType *ot)
405 {
406         /* identifiers */
407         ot->name= "Paste File";
408     ot->description= "Paste contents from file.";
409         ot->idname= "FONT_OT_file_paste";
410         
411         /* api callbacks */
412         ot->exec= paste_file_exec;
413         ot->invoke= paste_file_invoke;
414         ot->poll= ED_operator_editfont;
415         
416         /* flags */
417         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
418
419         /* properties */
420         WM_operator_properties_filesel(ot, FOLDERFILE|TEXTFILE, FILE_SPECIAL);
421 }
422
423 /******************* paste buffer operator ********************/
424
425 static int paste_buffer_exec(bContext *C, wmOperator *op)
426 {
427         char *filename;
428
429 #ifdef WIN32
430         filename= "C:\\windows\\temp\\cutbuf.txt";
431
432 //      The following is more likely to work on all Win32 installations.
433 //      suggested by Douglas Toltzman. Needs windows include files...
434 /*
435         char tempFileName[MAX_PATH];
436         DWORD pathlen;
437         static const char cutbufname[]="cutbuf.txt";
438
439         if((pathlen=GetTempPath(sizeof(tempFileName),tempFileName)) > 0 &&
440                 pathlen + sizeof(cutbufname) <= sizeof(tempFileName))
441         {
442                 strcat(tempFileName,cutbufname);
443                 filename= tempFilename;
444         }
445 */
446 #else
447         filename= "/tmp/.cutbuffer";
448 #endif
449
450         return paste_file(C, NULL, filename);
451 }
452
453 void FONT_OT_buffer_paste(wmOperatorType *ot)
454 {
455         /* identifiers */
456         ot->name= "Paste Buffer";
457     ot->description= "Paste text from OS buffer.";
458         ot->idname= "FONT_OT_buffer_paste";
459         
460         /* api callbacks */
461         ot->exec= paste_buffer_exec;
462         ot->poll= ED_operator_editfont;
463         
464         /* flags */
465         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
466 }
467
468 /******************* text to object operator ********************/
469
470 static void txt_add_object(bContext *C, TextLine *firstline, int totline, float offset[3])
471 {
472         Scene *scene= CTX_data_scene(C);
473         Curve *cu;
474         Object *obedit;
475         Base *base;
476         struct TextLine *tmp;
477         int nchars = 0, a;
478
479         obedit= add_object(scene, OB_FONT);
480         base= scene->basact;
481
482         ED_object_base_init_from_view(C, base);
483         where_is_object(scene, obedit);
484
485         obedit->loc[0] += offset[0];
486         obedit->loc[1] += offset[1];
487         obedit->loc[2] += offset[2];
488
489         cu= obedit->data;
490         cu->vfont= get_builtin_font();
491         cu->vfont->id.us++;
492
493         for(tmp=firstline, a=0; cu->len<MAXTEXT && a<totline; tmp=tmp->next, a++)
494                 nchars += strlen(tmp->line) + 1;
495
496         if(cu->str) MEM_freeN(cu->str);
497         if(cu->strinfo) MEM_freeN(cu->strinfo); 
498
499         cu->str= MEM_callocN(nchars+4, "str");
500         cu->strinfo= MEM_callocN((nchars+4)*sizeof(CharInfo), "strinfo");
501
502         cu->str[0]= '\0';
503         cu->len= 0;
504         cu->pos= 0;
505         
506         for(tmp=firstline, a=0; cu->len<MAXTEXT && a<totline; tmp=tmp->next, a++) {
507                 strcat(cu->str, tmp->line);
508                 cu->len+= strlen(tmp->line);
509
510                 if(tmp->next) {
511                         strcat(cu->str, "\n");
512                         cu->len++;
513                 }
514
515                 cu->pos= cu->len;
516         }
517
518         WM_event_add_notifier(C, NC_OBJECT|NA_ADDED, obedit);
519 }
520
521 void ED_text_to_object(bContext *C, Text *text, int split_lines)
522 {
523         RegionView3D *rv3d= CTX_wm_region_view3d(C);
524         TextLine *line;
525         float offset[3];
526         int linenum= 0;
527
528         if(!text || !text->lines.first) return;
529
530         if(split_lines) {
531                 for(line=text->lines.first; line; line=line->next) {
532                         /* skip lines with no text, but still make space for them */
533                         if(line->line[0] == '\0') {
534                                 linenum++;
535                                 continue;
536                         }
537         
538                         /* do the translation */
539                         offset[0] = 0;
540                         offset[1] = -linenum;
541                         offset[2] = 0;
542         
543                         if(rv3d)
544                                 Mat4Mul3Vecfl(rv3d->viewinv, offset);
545
546                         txt_add_object(C, line, 1, offset);
547
548                         linenum++;
549                 }
550         }
551         else {
552                 offset[0]= 0.0f;
553                 offset[1]= 0.0f;
554                 offset[2]= 0.0f;
555
556                 txt_add_object(C, text->lines.first, BLI_countlist(&text->lines), offset);
557         }
558 }
559
560 /********************** utilities ***************************/
561
562 static short next_word(Curve *cu)
563 {
564         short s;
565         for(s=cu->pos; (cu->str[s]) && (cu->str[s]!=' ') && (cu->str[s]!='\n') &&
566                         (cu->str[s]!=1) && (cu->str[s]!='\r'); s++);
567         if(cu->str[s]) return(s+1); else return(s);
568 }
569
570 static short prev_word(Curve *cu)
571 {
572         short s;
573         
574         if(cu->pos==0) return(0);
575         for(s=cu->pos-2; (cu->str[s]) && (cu->str[s]!=' ') && (cu->str[s]!='\n') &&
576                         (cu->str[s]!=1) && (cu->str[s]!='\r'); s--);
577         if(cu->str[s]) return(s+1); else return(s);
578 }
579
580 static int kill_selection(Object *obedit, int ins)      /* 1 == new character */
581 {
582         Curve *cu= obedit->data;
583         EditFont *ef= cu->editfont;
584         int selend, selstart, direction;
585         int offset = 0;
586         int getfrom;
587
588         direction = BKE_font_getselection(obedit, &selstart, &selend);
589         if(direction) {
590                 int size;
591                 if(ins) offset = 1;
592                 if(cu->pos >= selstart) cu->pos = selstart+offset;
593                 if((direction == -1) && ins) {
594                         selstart++;
595                         selend++;
596                 }
597                 getfrom = selend+offset;
598                 if(ins==0) getfrom++;
599                 size = (cu->len * sizeof(wchar_t)) - (selstart * sizeof(wchar_t)) + (offset*sizeof(wchar_t));
600                 memmove(ef->textbuf+selstart, ef->textbuf+getfrom, size);
601                 memmove(ef->textbufinfo+selstart, ef->textbufinfo+getfrom, ((cu->len-selstart)+offset)*sizeof(CharInfo));
602                 cu->len -= (selend-selstart)+offset;
603                 cu->selstart = cu->selend = 0;
604         }
605
606         return(direction);
607 }
608
609 /******************* set style operator ********************/
610
611 static EnumPropertyItem style_items[]= {
612         {CU_BOLD, "BOLD", 0, "Bold", ""},
613         {CU_ITALIC, "ITALIC", 0, "Italic", ""},
614         {CU_UNDERLINE, "UNDERLINE", 0, "Underline", ""},
615         {0, NULL, 0, NULL, NULL}};
616
617 static int set_style(bContext *C, int style, int clear)
618 {
619         Object *obedit= CTX_data_edit_object(C);
620         Curve *cu= obedit->data;
621         EditFont *ef= cu->editfont;
622         int i, selstart, selend;
623
624         if(!BKE_font_getselection(obedit, &selstart, &selend))
625                 return OPERATOR_CANCELLED;
626
627         for(i=selstart; i<=selend; i++) {
628                 if(clear)
629                         ef->textbufinfo[i].flag &= ~style;
630                 else
631                         ef->textbufinfo[i].flag |= style;
632         }
633
634         DAG_id_flush_update(obedit->data, OB_RECALC_DATA);
635         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
636
637         return OPERATOR_FINISHED;
638 }
639
640 static int set_style_exec(bContext *C, wmOperator *op)
641 {
642         int style, clear;
643
644         style= RNA_enum_get(op->ptr, "style");
645         clear= RNA_enum_get(op->ptr, "clear");
646
647         return set_style(C, style, clear);
648 }
649
650 void FONT_OT_style_set(wmOperatorType *ot)
651 {
652         /* identifiers */
653         ot->name= "Set Style";
654     ot->description= "Set font style.";
655         ot->idname= "FONT_OT_style_set";
656         
657         /* api callbacks */
658         ot->exec= set_style_exec;
659         ot->poll= ED_operator_editfont;
660         
661         /* flags */
662         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
663
664         /* properties */
665         RNA_def_enum(ot->srna, "style", style_items, CU_BOLD, "Style", "Style to set selection to.");
666         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "Clear style rather than setting it.");
667 }
668
669 /******************* toggle style operator ********************/
670
671 static int toggle_style_exec(bContext *C, wmOperator *op)
672 {
673         Object *obedit= CTX_data_edit_object(C);
674         Curve *cu= obedit->data;
675         int style, clear, selstart, selend;
676
677         if(!BKE_font_getselection(obedit, &selstart, &selend))
678                 return OPERATOR_CANCELLED;
679         
680         style= RNA_enum_get(op->ptr, "style");
681
682         cu->curinfo.flag ^= style;
683         clear= (cu->curinfo.flag & style) == 0;
684
685         return set_style(C, style, clear);
686 }
687
688 void FONT_OT_style_toggle(wmOperatorType *ot)
689 {
690         /* identifiers */
691         ot->name= "Toggle Style";
692     ot->description= "Toggle font style.";
693         ot->idname= "FONT_OT_style_toggle";
694         
695         /* api callbacks */
696         ot->exec= toggle_style_exec;
697         ot->poll= ED_operator_editfont;
698         
699         /* flags */
700         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
701
702         /* properties */
703         RNA_def_enum(ot->srna, "style", style_items, CU_BOLD, "Style", "Style to set selection to.");
704 }
705
706 /******************* copy text operator ********************/
707
708 static void copy_selection(Object *obedit)
709 {
710         int selstart, selend;
711         
712         if(BKE_font_getselection(obedit, &selstart, &selend)) {
713                 Curve *cu= obedit->data;
714                 EditFont *ef= cu->editfont;
715                 
716                 memcpy(ef->copybuf, ef->textbuf+selstart, ((selend-selstart)+1)*sizeof(wchar_t));
717                 ef->copybuf[(selend-selstart)+1]=0;
718                 memcpy(ef->copybufinfo, ef->textbufinfo+selstart, ((selend-selstart)+1)*sizeof(CharInfo));      
719         }
720 }
721
722 static int copy_text_exec(bContext *C, wmOperator *op)
723 {
724         Object *obedit= CTX_data_edit_object(C);
725
726         copy_selection(obedit);
727
728         return OPERATOR_FINISHED;
729 }
730
731 void FONT_OT_text_copy(wmOperatorType *ot)
732 {
733         /* identifiers */
734         ot->name= "Copy Text";
735     ot->description= "Copy selected text to clipboard.";
736         ot->idname= "FONT_OT_text_copy";
737         
738         /* api callbacks */
739         ot->exec= copy_text_exec;
740         ot->poll= ED_operator_editfont;
741 }
742
743 /******************* cut text operator ********************/
744
745 static int cut_text_exec(bContext *C, wmOperator *op)
746 {
747         Scene *scene= CTX_data_scene(C);
748         Object *obedit= CTX_data_edit_object(C);
749         int selstart, selend;
750
751         if(!BKE_font_getselection(obedit, &selstart, &selend))
752                 return OPERATOR_CANCELLED;
753
754         copy_selection(obedit);
755         kill_selection(obedit, 0);
756
757         text_update_edited(C, scene, obedit, 1, 0);
758
759         return OPERATOR_FINISHED;
760 }
761
762 void FONT_OT_text_cut(wmOperatorType *ot)
763 {
764         /* identifiers */
765         ot->name= "Cut Text";
766     ot->description= "Cut selected text to clipboard.";
767         ot->idname= "FONT_OT_text_cut";
768         
769         /* api callbacks */
770         ot->exec= cut_text_exec;
771         ot->poll= ED_operator_editfont;
772
773         /* flags */
774         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
775 }
776
777 /******************* paste text operator ********************/
778
779 static int paste_selection(Object *obedit, ReportList *reports)
780 {
781         Curve *cu= obedit->data;
782         EditFont *ef= cu->editfont;
783         int len= wcslen(ef->copybuf);
784
785         // Verify that the copy buffer => [copy buffer len] + cu->len < MAXTEXT
786         if(cu->len + len <= MAXTEXT) {
787                 if(len) {       
788                         int size = (cu->len * sizeof(wchar_t)) - (cu->pos*sizeof(wchar_t)) + sizeof(wchar_t);
789                         memmove(ef->textbuf+cu->pos+len, ef->textbuf+cu->pos, size);
790                         memcpy(ef->textbuf+cu->pos, ef->copybuf, len * sizeof(wchar_t));
791                 
792                         memmove(ef->textbufinfo+cu->pos+len, ef->textbufinfo+cu->pos, (cu->len-cu->pos+1)*sizeof(CharInfo));
793                         memcpy(ef->textbufinfo+cu->pos, ef->copybufinfo, len*sizeof(CharInfo)); 
794                 
795                         cu->len += len;
796                         cu->pos += len;
797
798                         return 1;
799                 }
800         }
801         else
802                 BKE_report(reports, RPT_WARNING, "Text too long.");
803         
804         return 0;
805 }
806
807 static int paste_text_exec(bContext *C, wmOperator *op)
808 {
809         Scene *scene= CTX_data_scene(C);
810         Object *obedit= CTX_data_edit_object(C);
811
812         if(!paste_selection(obedit, op->reports))
813                 return OPERATOR_CANCELLED;
814
815         text_update_edited(C, scene, obedit, 1, 0);
816
817         return OPERATOR_FINISHED;
818 }
819
820 void FONT_OT_text_paste(wmOperatorType *ot)
821 {
822         /* identifiers */
823         ot->name= "Paste Text";
824     ot->description= "Paste text from clipboard.";
825         ot->idname= "FONT_OT_text_paste";
826         
827         /* api callbacks */
828         ot->exec= paste_text_exec;
829         ot->poll= ED_operator_editfont;
830
831         /* flags */
832         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
833 }
834
835 /************************ move operator ************************/
836
837 static EnumPropertyItem move_type_items[]= {
838         {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
839         {LINE_END, "LINE_END", 0, "Line End", ""},
840         {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
841         {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
842         {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
843         {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
844         {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""},
845         {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""},
846         {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""},
847         {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""},
848         {0, NULL, 0, NULL, NULL}};
849
850 static int move_cursor(bContext *C, int type, int select)
851 {
852         Scene *scene= CTX_data_scene(C);
853         Object *obedit= CTX_data_edit_object(C);
854         Curve *cu= obedit->data;
855         EditFont *ef= cu->editfont;
856         int cursmove= 0;
857
858         switch(type) {
859                 case LINE_BEGIN:
860                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
861                         while(cu->pos>0) {
862                                 if(ef->textbuf[cu->pos-1]=='\n') break;
863                                 if(ef->textbufinfo[cu->pos-1].flag & CU_WRAP ) break;                           
864                                 cu->pos--;
865                         }               
866                         cursmove=FO_CURS;
867                         break;
868                         
869                 case LINE_END:
870                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;                
871                         while(cu->pos<cu->len) {
872                                 if(ef->textbuf[cu->pos]==0) break;
873                                 if(ef->textbuf[cu->pos]=='\n') break;
874                                 if(ef->textbufinfo[cu->pos].flag & CU_WRAP ) break;
875                                 cu->pos++;
876                         }
877                         cursmove=FO_CURS;
878                         break;
879
880                 case PREV_WORD:
881                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
882                         cu->pos= prev_word(cu);
883                         cursmove= FO_CURS;
884                         break;
885
886                 case NEXT_WORD:
887                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
888                         cu->pos= next_word(cu);
889                         cursmove= FO_CURS;                              
890                         break;
891
892                 case PREV_CHAR:
893                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
894                         cu->pos--;
895                         cursmove=FO_CURS;
896                         break;
897
898                 case NEXT_CHAR: 
899                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
900                         cu->pos++;
901                         cursmove= FO_CURS;                              
902
903                         break;
904
905                 case PREV_LINE:
906                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
907                         cursmove=FO_CURSUP;
908                         break;
909                         
910                 case NEXT_LINE:
911                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
912                         cursmove= FO_CURSDOWN;
913                         break;
914
915                 case PREV_PAGE:
916                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
917                         cursmove=FO_PAGEUP;
918                         break;
919
920                 case NEXT_PAGE:
921                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
922                         cursmove=FO_PAGEDOWN;
923                         break;
924         }
925                 
926         if(!cursmove)
927                 return OPERATOR_CANCELLED;
928
929         if(select == 0) {
930                 if(cu->selstart) {
931                         cu->selstart = cu->selend = 0;
932                         update_string(cu);
933                         BKE_text_to_curve(scene, obedit, FO_SELCHANGE);
934                 }
935         }
936
937         if(cu->pos>cu->len) cu->pos= cu->len;
938         else if(cu->pos>=MAXTEXT) cu->pos= MAXTEXT;
939         else if(cu->pos<0) cu->pos= 0;
940
941         text_update_edited(C, scene, obedit, select, cursmove);
942
943         if(select)
944                 cu->selend = cu->pos;
945
946         return OPERATOR_FINISHED;
947 }
948
949 static int move_exec(bContext *C, wmOperator *op)
950 {
951         int type= RNA_enum_get(op->ptr, "type");
952
953         return move_cursor(C, type, 0);
954 }
955
956 void FONT_OT_move(wmOperatorType *ot)
957 {
958         /* identifiers */
959         ot->name= "Move Cursor";
960     ot->description= "Move cursor to position type.";
961         ot->idname= "FONT_OT_move";
962         
963         /* api callbacks */
964         ot->exec= move_exec;
965         ot->poll= ED_operator_editfont;
966
967         /* flags */
968         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
969
970         /* properties */
971         RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
972 }
973
974 /******************* move select operator ********************/
975
976 static int move_select_exec(bContext *C, wmOperator *op)
977 {
978         int type= RNA_enum_get(op->ptr, "type");
979
980         return move_cursor(C, type, 1);
981 }
982
983 void FONT_OT_move_select(wmOperatorType *ot)
984 {
985         /* identifiers */
986         ot->name= "Move Select";
987     ot->description= "Make selection from current cursor position to new cursor position type.";
988         ot->idname= "FONT_OT_move_select";
989         
990         /* api callbacks */
991         ot->exec= move_select_exec;
992         ot->poll= ED_operator_editfont;
993
994         /* flags */
995         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
996
997         /* properties */
998         RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to, to make a selection.");
999 }
1000
1001 /************************* change spacing **********************/
1002
1003 static int change_spacing_exec(bContext *C, wmOperator *op)
1004 {
1005         Scene *scene= CTX_data_scene(C);
1006         Object *obedit= CTX_data_edit_object(C);
1007         Curve *cu= obedit->data;
1008         EditFont *ef= cu->editfont;
1009         int kern, delta= RNA_int_get(op->ptr, "delta");
1010
1011         kern = ef->textbufinfo[cu->pos-1].kern;
1012         kern += delta;
1013         CLAMP(kern, -20, 20);
1014
1015         if(ef->textbufinfo[cu->pos-1].kern == kern)
1016                 return OPERATOR_CANCELLED;
1017
1018         ef->textbufinfo[cu->pos-1].kern = kern;
1019
1020         text_update_edited(C, scene, obedit, 1, 0);
1021
1022         return OPERATOR_FINISHED;
1023 }
1024
1025 void FONT_OT_change_spacing(wmOperatorType *ot)
1026 {
1027         /* identifiers */
1028         ot->name= "Change Spacing";
1029     ot->description= "Change font spacing.";
1030         ot->idname= "FONT_OT_change_spacing";
1031         
1032         /* api callbacks */
1033         ot->exec= change_spacing_exec;
1034         ot->poll= ED_operator_editfont;
1035
1036         /* flags */
1037         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1038
1039         /* properties */
1040         RNA_def_int(ot->srna, "delta", 1, -20, 20, "Delta", "Amount to decrease or increasing character spacing with.", -20, 20);
1041 }
1042
1043 /************************* change character **********************/
1044
1045 static int change_character_exec(bContext *C, wmOperator *op)
1046 {
1047         Scene *scene= CTX_data_scene(C);
1048         Object *obedit= CTX_data_edit_object(C);
1049         Curve *cu= obedit->data;
1050         EditFont *ef= cu->editfont;
1051         int character, delta= RNA_int_get(op->ptr, "delta");
1052
1053         if(cu->pos <= 0)
1054                 return OPERATOR_CANCELLED;
1055
1056         character= ef->textbuf[cu->pos - 1];
1057         character += delta;
1058         CLAMP(character, 0, 255);
1059
1060         if(character == ef->textbuf[cu->pos - 1])
1061                 return OPERATOR_CANCELLED;
1062
1063         ef->textbuf[cu->pos - 1]= character;
1064
1065         text_update_edited(C, scene, obedit, 1, 0);
1066
1067         return OPERATOR_FINISHED;
1068 }
1069
1070 void FONT_OT_change_character(wmOperatorType *ot)
1071 {
1072         /* identifiers */
1073         ot->name= "Change Character";
1074     ot->description= "Change font character code.";
1075         ot->idname= "FONT_OT_change_character";
1076         
1077         /* api callbacks */
1078         ot->exec= change_character_exec;
1079         ot->poll= ED_operator_editfont;
1080
1081         /* flags */
1082         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1083
1084         /* properties */
1085         RNA_def_int(ot->srna, "delta", 1, -255, 255, "Delta", "Number to increase or decrease character code with.", -255, 255);
1086 }
1087
1088 /******************* line break operator ********************/
1089
1090 static int line_break_exec(bContext *C, wmOperator *op)
1091 {
1092         Scene *scene= CTX_data_scene(C);
1093         Object *obedit= CTX_data_edit_object(C);
1094         Curve *cu= obedit->data;
1095         EditFont *ef= cu->editfont;
1096         int ctrl= RNA_enum_get(op->ptr, "ctrl");
1097
1098         if(ctrl) {
1099                 insert_into_textbuf(obedit, 1);
1100                 if(ef->textbuf[cu->pos]!='\n')
1101                         insert_into_textbuf(obedit, '\n');
1102         }
1103         else
1104                 insert_into_textbuf(obedit, '\n');
1105
1106         cu->selstart = cu->selend = 0;
1107
1108         text_update_edited(C, scene, obedit, 1, 0);
1109
1110         return OPERATOR_FINISHED;
1111 }
1112
1113 void FONT_OT_line_break(wmOperatorType *ot)
1114 {
1115         /* identifiers */
1116         ot->name= "Line Break";
1117     ot->description= "Insert line break at cursor position.";
1118         ot->idname= "FONT_OT_line_break";
1119         
1120         /* api callbacks */
1121         ot->exec= line_break_exec;
1122         ot->poll= ED_operator_editfont;
1123
1124         /* flags */
1125         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1126
1127         /* properties */
1128         RNA_def_boolean(ot->srna, "ctrl", 0, "Ctrl", ""); // XXX what is this?
1129 }
1130
1131 /******************* delete operator **********************/
1132
1133 static EnumPropertyItem delete_type_items[]= {
1134         {DEL_ALL, "ALL", 0, "All", ""},
1135         {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
1136         {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
1137         {DEL_SELECTION, "SELECTION", 0, "Selection", ""},
1138         {DEL_NEXT_SEL, "NEXT_OR_SELECTION", 0, "Next or Selection", ""},
1139         {DEL_PREV_SEL, "PREVIOUS_OR_SELECTION", 0, "Previous or Selection", ""},
1140         {0, NULL, 0, NULL, NULL}};
1141
1142 static int delete_exec(bContext *C, wmOperator *op)
1143 {
1144         Scene *scene= CTX_data_scene(C);
1145         Object *obedit= CTX_data_edit_object(C);
1146         Curve *cu= obedit->data;
1147         EditFont *ef= cu->editfont;
1148         int x, selstart, selend, type= RNA_enum_get(op->ptr, "type");
1149
1150         if(cu->len == 0)
1151                 return OPERATOR_CANCELLED;
1152
1153         if(BKE_font_getselection(obedit, &selstart, &selend)) {
1154                 if(type == DEL_NEXT_SEL) type= DEL_SELECTION;
1155                 else if(type == DEL_PREV_SEL) type= DEL_SELECTION;
1156         }
1157         else {
1158                 if(type == DEL_NEXT_SEL) type= DEL_NEXT_CHAR;
1159                 else if(type == DEL_PREV_SEL) type= DEL_PREV_CHAR;
1160         }
1161
1162         switch(type) {
1163                 case DEL_ALL:
1164                         cu->len = cu->pos = 0;
1165                         ef->textbuf[0]= 0;
1166                         break;
1167                 case DEL_SELECTION:
1168                         if(!kill_selection(obedit, 0))
1169                                 return OPERATOR_CANCELLED;
1170                         break;
1171                 case DEL_PREV_CHAR:
1172                         if(cu->pos<=0)
1173                                 return OPERATOR_CANCELLED;
1174
1175                         for(x=cu->pos;x<=cu->len;x++)
1176                                 ef->textbuf[x-1]= ef->textbuf[x];
1177                         for(x=cu->pos;x<=cu->len;x++)
1178                                 ef->textbufinfo[x-1]= ef->textbufinfo[x];                                       
1179
1180                         cu->pos--;
1181                         ef->textbuf[--cu->len]='\0';
1182                         break;
1183                 case DEL_NEXT_CHAR:
1184                         if(cu->pos>=cu->len)
1185                                 return OPERATOR_CANCELLED;
1186
1187                         for(x=cu->pos;x<cu->len;x++)
1188                                 ef->textbuf[x]= ef->textbuf[x+1];
1189                         for(x=cu->pos;x<cu->len;x++)
1190                                 ef->textbufinfo[x]= ef->textbufinfo[x+1];                                       
1191
1192                         ef->textbuf[--cu->len]='\0';
1193                         break;
1194                 default:
1195                         return OPERATOR_CANCELLED;
1196         }
1197
1198         text_update_edited(C, scene, obedit, 1, 0);
1199
1200         return OPERATOR_FINISHED;
1201 }
1202
1203 void FONT_OT_delete(wmOperatorType *ot)
1204 {
1205         /* identifiers */
1206         ot->name= "Delete";
1207     ot->description= "Delete text by cursor position.";
1208         ot->idname= "FONT_OT_delete";
1209         
1210         /* api callbacks */
1211         ot->exec= delete_exec;
1212         ot->poll= ED_operator_editfont;
1213
1214         /* flags */
1215         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1216
1217         /* properties */
1218         RNA_def_enum(ot->srna, "type", delete_type_items, DEL_ALL, "Type", "Which part of the text to delete.");
1219 }
1220
1221 /*********************** insert text operator *************************/
1222
1223 static int insert_text_exec(bContext *C, wmOperator *op)
1224 {
1225         Scene *scene= CTX_data_scene(C);
1226         Object *obedit= CTX_data_edit_object(C);
1227         char *inserted_utf8;
1228         wchar_t *inserted_text, first;
1229         int len;
1230
1231         if(!RNA_property_is_set(op->ptr, "text"))
1232                 return OPERATOR_CANCELLED;
1233         
1234         inserted_utf8= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
1235         len= strlen(inserted_utf8);
1236
1237         inserted_text= MEM_callocN(sizeof(wchar_t)*(len+1), "FONT_insert_text");
1238         utf8towchar(inserted_text, inserted_utf8);
1239         first= inserted_text[0];
1240
1241         MEM_freeN(inserted_text);
1242         MEM_freeN(inserted_utf8);
1243
1244         if(!first)
1245                 return OPERATOR_CANCELLED;
1246
1247         insert_into_textbuf(obedit, first);
1248         kill_selection(obedit, 1);
1249         text_update_edited(C, scene, obedit, 1, 0);
1250
1251         return OPERATOR_FINISHED;
1252 }
1253
1254 static int insert_text_invoke(bContext *C, wmOperator *op, wmEvent *evt)
1255 {
1256         Scene *scene= CTX_data_scene(C);
1257         Object *obedit= CTX_data_edit_object(C);
1258         Curve *cu= obedit->data;
1259         EditFont *ef= cu->editfont;
1260         static int accentcode= 0;
1261         uintptr_t ascii = evt->ascii;
1262         int alt= evt->alt, shift= evt->shift, ctrl= evt->ctrl;
1263         int event= evt->type, val= evt->val;
1264         wchar_t inserted_text[2]= {0};
1265
1266         if(RNA_property_is_set(op->ptr, "text"))
1267                 return insert_text_exec(C, op);
1268         
1269         /* tab should exit editmode, but we allow it to be typed using modifier keys */
1270         if(event==TABKEY) {
1271                 if((alt||ctrl||shift) == 0)
1272                         return OPERATOR_PASS_THROUGH;
1273                 else
1274                         ascii= 9;
1275         }
1276         else if(event==BACKSPACEKEY)
1277                 ascii= 0;
1278
1279         if(val && ascii) {
1280                 /* handle case like TAB (== 9) */
1281                 if((ascii > 31 && ascii < 254 && ascii != 127) || (ascii==13) || (ascii==10) || (ascii==8)) {
1282                         if(accentcode) {
1283                                 if(cu->pos>0) {
1284                                         inserted_text[0]= findaccent(ef->textbuf[cu->pos-1], ascii);
1285                                         ef->textbuf[cu->pos-1]= inserted_text[0];
1286                                 }
1287                                 accentcode= 0;
1288                         }
1289                         else if(cu->len<MAXTEXT-1) {
1290                                 if(alt) {
1291                                         /* might become obsolete, apple has default values for this, other OS's too? */
1292                                         if(ascii=='t') ascii= 137;
1293                                         else if(ascii=='c') ascii= 169;
1294                                         else if(ascii=='f') ascii= 164;
1295                                         else if(ascii=='g') ascii= 176;
1296                                         else if(ascii=='l') ascii= 163;
1297                                         else if(ascii=='r') ascii= 174;
1298                                         else if(ascii=='s') ascii= 223;
1299                                         else if(ascii=='y') ascii= 165;
1300                                         else if(ascii=='.') ascii= 138;
1301                                         else if(ascii=='1') ascii= 185;
1302                                         else if(ascii=='2') ascii= 178;
1303                                         else if(ascii=='3') ascii= 179;
1304                                         else if(ascii=='%') ascii= 139;
1305                                         else if(ascii=='?') ascii= 191;
1306                                         else if(ascii=='!') ascii= 161;
1307                                         else if(ascii=='x') ascii= 215;
1308                                         else if(ascii=='>') ascii= 187;
1309                                         else if(ascii=='<') ascii= 171;
1310                                 }
1311
1312                                 inserted_text[0]= ascii;
1313                                 insert_into_textbuf(obedit, ascii);
1314                         }
1315                         
1316                         kill_selection(obedit, 1);
1317                         text_update_edited(C, scene, obedit, 1, 0);
1318                 }
1319                 else {
1320                         inserted_text[0]= ascii;
1321                         insert_into_textbuf(obedit, ascii);
1322                         text_update_edited(C, scene, obedit, 1, 0);
1323                 }
1324         }
1325         else if(val && event == BACKSPACEKEY) {
1326                 if(alt && cu->len!=0 && cu->pos>0)
1327                         accentcode= 1;
1328
1329                 return OPERATOR_PASS_THROUGH;
1330         }
1331         else
1332                 return OPERATOR_PASS_THROUGH;
1333
1334         if(inserted_text[0]) {
1335                 /* store as utf8 in RNA string */
1336                 char inserted_utf8[8] = {0};
1337
1338                 wcs2utf8s(inserted_utf8, inserted_text);
1339                 RNA_string_set(op->ptr, "text", inserted_utf8);
1340         }
1341
1342         return OPERATOR_FINISHED;
1343 }
1344
1345 void FONT_OT_text_insert(wmOperatorType *ot)
1346 {
1347         /* identifiers */
1348         ot->name= "Insert Text";
1349     ot->description= "Insert text at cursor position.";
1350         ot->idname= "FONT_OT_text_insert";
1351         
1352         /* api callbacks */
1353         ot->exec= insert_text_exec;
1354         ot->invoke= insert_text_invoke;
1355         ot->poll= ED_operator_editfont;
1356         
1357         /* flags */
1358         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1359
1360         /* properties */
1361         RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
1362 }
1363
1364 /***************** editmode enter/exit ********************/
1365
1366 void make_editText(Object *obedit)
1367 {
1368         Curve *cu= obedit->data;
1369         EditFont *ef= cu->editfont;
1370         
1371         if(ef==NULL) {
1372                 ef= cu->editfont= MEM_callocN(sizeof(EditFont), "editfont");
1373         
1374                 ef->textbuf= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "texteditbuf");
1375                 ef->textbufinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "texteditbufinfo");
1376                 ef->copybuf= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "texteditcopybuf");
1377                 ef->copybufinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "texteditcopybufinfo");      
1378                 ef->oldstr= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "oldstrbuf");
1379                 ef->oldstrinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "oldstrbuf");
1380         }
1381         
1382         // Convert the original text to wchar_t
1383         utf8towchar(ef->textbuf, cu->str);
1384         wcscpy(ef->oldstr, ef->textbuf);
1385                 
1386         cu->len= wcslen(ef->textbuf);
1387         
1388         memcpy(ef->textbufinfo, cu->strinfo, (cu->len)*sizeof(CharInfo));
1389         memcpy(ef->oldstrinfo, cu->strinfo, (cu->len)*sizeof(CharInfo));
1390
1391         if(cu->pos>cu->len) cu->pos= cu->len;
1392
1393         if(cu->pos)
1394                 cu->curinfo = ef->textbufinfo[cu->pos-1];
1395         else
1396                 cu->curinfo = ef->textbufinfo[0];
1397         
1398         // Convert to UTF-8
1399         update_string(cu);
1400 }
1401
1402 void load_editText(Object *obedit)
1403 {
1404         Curve *cu= obedit->data;
1405         EditFont *ef= cu->editfont;
1406         
1407         MEM_freeN(ef->oldstr);
1408         ef->oldstr= NULL;
1409         MEM_freeN(ef->oldstrinfo);
1410         ef->oldstrinfo= NULL;
1411         
1412         update_string(cu);
1413         
1414         if(cu->strinfo)
1415                 MEM_freeN(cu->strinfo);
1416         cu->strinfo= MEM_callocN((cu->len+4)*sizeof(CharInfo), "texteditinfo");
1417         memcpy(cu->strinfo, ef->textbufinfo, (cu->len)*sizeof(CharInfo));
1418
1419         cu->len= strlen(cu->str);
1420         
1421         /* this memory system is weak... */
1422         
1423         if(cu->selboxes) {
1424                 MEM_freeN(cu->selboxes);
1425                 cu->selboxes= NULL;
1426         }
1427 }
1428
1429 void free_editText(Object *obedit)
1430 {
1431         BKE_free_editfont((Curve *)obedit->data);
1432 }
1433
1434 /********************** set case operator *********************/
1435
1436 static EnumPropertyItem case_items[]= {
1437         {CASE_LOWER, "LOWER", 0, "Lower", ""},
1438         {CASE_UPPER, "UPPER", 0, "Upper", ""},
1439         {0, NULL, 0, NULL, NULL}};
1440
1441 static int set_case(bContext *C, int ccase)
1442 {
1443         Scene *scene= CTX_data_scene(C);
1444         Object *obedit= CTX_data_edit_object(C);
1445         Curve *cu= obedit->data;
1446         EditFont *ef= cu->editfont;
1447         wchar_t *str;
1448         int len;
1449         
1450         len= wcslen(ef->textbuf);
1451         str= ef->textbuf;
1452         while(len) {
1453                 if(*str>='a' && *str<='z')
1454                         *str-= 32;
1455                 len--;
1456                 str++;
1457         }
1458         
1459         if(ccase == CASE_LOWER) {
1460                 len= wcslen(ef->textbuf);
1461                 str= ef->textbuf;
1462                 while(len) {
1463                         if(*str>='A' && *str<='Z') {
1464                                 *str+= 32;
1465                         }
1466                         len--;
1467                         str++;
1468                 }
1469         }
1470
1471         text_update_edited(C, scene, obedit, 1, 0);
1472
1473         return OPERATOR_FINISHED;
1474 }
1475
1476 static int set_case_exec(bContext *C, wmOperator *op)
1477 {
1478         return set_case(C, RNA_enum_get(op->ptr, "case"));
1479 }
1480
1481 void FONT_OT_case_set(wmOperatorType *ot)
1482 {
1483         /* identifiers */
1484         ot->name= "Set Case";
1485     ot->description= "Set font case.";
1486         ot->idname= "FONT_OT_case_set";
1487         
1488         /* api callbacks */
1489         ot->exec= set_case_exec;
1490         ot->poll= ED_operator_editfont;
1491
1492         /* flags */
1493         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1494
1495         /* properties */
1496         RNA_def_enum(ot->srna, "case", case_items, CASE_LOWER, "Case", "Lower or upper case.");
1497 }
1498
1499 /********************** toggle case operator *********************/
1500
1501 static int toggle_case_exec(bContext *C, wmOperator *op)
1502 {
1503         Object *obedit= CTX_data_edit_object(C);
1504         Curve *cu= obedit->data;
1505         EditFont *ef= cu->editfont;
1506         wchar_t *str;
1507         int len, ccase= CASE_UPPER;
1508         
1509         len= wcslen(ef->textbuf);
1510         str= ef->textbuf;
1511         while(len) {
1512                 if(*str>='a' && *str<='z') {
1513                         ccase= CASE_LOWER;
1514                         break;
1515                 }
1516
1517                 len--;
1518                 str++;
1519         }
1520         
1521         return set_case(C, ccase);
1522 }
1523
1524 void FONT_OT_case_toggle(wmOperatorType *ot)
1525 {
1526         /* identifiers */
1527         ot->name= "Toggle Case";
1528     ot->description= "Toggle font case.";
1529         ot->idname= "FONT_OT_case_toggle";
1530         
1531         /* api callbacks */
1532         ot->exec= toggle_case_exec;
1533         ot->poll= ED_operator_editfont;
1534
1535         /* flags */
1536         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1537 }
1538
1539 /* **************** undo for font object ************** */
1540
1541 static void undoFont_to_editFont(void *strv, void *ecu)
1542 {
1543         Curve *cu= (Curve *)ecu;
1544         EditFont *ef= cu->editfont;
1545         char *str= strv;
1546
1547         cu->pos= *((short *)str);
1548         cu->len= *((short *)(str+2));
1549
1550         memcpy(ef->textbuf, str+4, (cu->len+1)*sizeof(wchar_t));
1551         memcpy(ef->textbufinfo, str+4 + (cu->len+1)*sizeof(wchar_t), cu->len*sizeof(CharInfo));
1552         
1553         cu->selstart = cu->selend = 0;
1554         
1555         update_string(cu);
1556 }
1557
1558 static void *editFont_to_undoFont(void *ecu)
1559 {
1560         Curve *cu= (Curve *)ecu;
1561         EditFont *ef= cu->editfont;
1562         char *str;
1563         
1564         // The undo buffer includes [MAXTEXT+6]=actual string and [MAXTEXT+4]*sizeof(CharInfo)=charinfo
1565         str= MEM_callocN((MAXTEXT+6)*sizeof(wchar_t) + (MAXTEXT+4)*sizeof(CharInfo), "string undo");
1566
1567         // Copy the string and string information
1568         memcpy(str+4, ef->textbuf, (cu->len+1)*sizeof(wchar_t));
1569         memcpy(str+4 + (cu->len+1)*sizeof(wchar_t), ef->textbufinfo, cu->len*sizeof(CharInfo));
1570
1571         *((short *)str)= cu->pos;
1572         *((short *)(str+2))= cu->len;   
1573         
1574         return str;
1575 }
1576
1577 static void free_undoFont(void *strv)
1578 {
1579         MEM_freeN(strv);
1580 }
1581
1582 static void *get_undoFont(bContext *C)
1583 {
1584         Object *obedit= CTX_data_edit_object(C);
1585         if(obedit && obedit->type==OB_FONT) {
1586                 return obedit->data;
1587         }
1588         return NULL;
1589 }
1590
1591 /* and this is all the undo system needs to know */
1592 void undo_push_font(bContext *C, char *name)
1593 {
1594         undo_editmode_push(C, name, get_undoFont, free_undoFont, undoFont_to_editFont, editFont_to_undoFont, NULL);
1595 }
1596