more lint includes
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_math.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
50 #include "BKE_context.h"
51 #include "BKE_curve.h"
52 #include "BKE_depsgraph.h"
53 #include "BKE_font.h"
54 #include "BKE_global.h"
55 #include "BKE_library.h"
56 #include "BKE_main.h"
57 #include "BKE_object.h"
58 #include "BKE_report.h"
59 #include "BKE_utildefines.h"
60
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63
64 #include "WM_api.h"
65 #include "WM_types.h"
66
67 #include "ED_object.h"
68 #include "ED_screen.h"
69 #include "ED_util.h"
70
71 #include "UI_interface.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 *path;
385         int retval;
386         
387         path= RNA_string_get_alloc(op->ptr, "path", NULL, 0);
388         retval= paste_file(C, op->reports, path);
389         MEM_freeN(path);
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, "path"))
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, FILE_OPENFILE);
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         float rot[3] = {0.f, 0.f, 0.f};
479         
480         obedit= add_object(scene, OB_FONT);
481         base= scene->basact;
482
483         
484         ED_object_base_init_transform(C, base, NULL, rot); /* seems to assume view align ? TODO - look into this, could be an operator option */
485         where_is_object(scene, obedit);
486
487         obedit->loc[0] += offset[0];
488         obedit->loc[1] += offset[1];
489         obedit->loc[2] += offset[2];
490
491         cu= obedit->data;
492         cu->vfont= get_builtin_font();
493         cu->vfont->id.us++;
494
495         for(tmp=firstline, a=0; cu->len<MAXTEXT && a<totline; tmp=tmp->next, a++)
496                 nchars += strlen(tmp->line) + 1;
497
498         if(cu->str) MEM_freeN(cu->str);
499         if(cu->strinfo) MEM_freeN(cu->strinfo); 
500
501         cu->str= MEM_callocN(nchars+4, "str");
502         cu->strinfo= MEM_callocN((nchars+4)*sizeof(CharInfo), "strinfo");
503
504         cu->str[0]= '\0';
505         cu->len= 0;
506         cu->pos= 0;
507         
508         for(tmp=firstline, a=0; cu->len<MAXTEXT && a<totline; tmp=tmp->next, a++) {
509                 strcat(cu->str, tmp->line);
510                 cu->len+= strlen(tmp->line);
511
512                 if(tmp->next) {
513                         strcat(cu->str, "\n");
514                         cu->len++;
515                 }
516
517                 cu->pos= cu->len;
518         }
519
520         WM_event_add_notifier(C, NC_OBJECT|NA_ADDED, obedit);
521 }
522
523 void ED_text_to_object(bContext *C, Text *text, int split_lines)
524 {
525         RegionView3D *rv3d= CTX_wm_region_view3d(C);
526         TextLine *line;
527         float offset[3];
528         int linenum= 0;
529
530         if(!text || !text->lines.first) return;
531
532         if(split_lines) {
533                 for(line=text->lines.first; line; line=line->next) {
534                         /* skip lines with no text, but still make space for them */
535                         if(line->line[0] == '\0') {
536                                 linenum++;
537                                 continue;
538                         }
539         
540                         /* do the translation */
541                         offset[0] = 0;
542                         offset[1] = -linenum;
543                         offset[2] = 0;
544         
545                         if(rv3d)
546                                 mul_mat3_m4_v3(rv3d->viewinv, offset);
547
548                         txt_add_object(C, line, 1, offset);
549
550                         linenum++;
551                 }
552         }
553         else {
554                 offset[0]= 0.0f;
555                 offset[1]= 0.0f;
556                 offset[2]= 0.0f;
557
558                 txt_add_object(C, text->lines.first, BLI_countlist(&text->lines), offset);
559         }
560 }
561
562 /********************** utilities ***************************/
563
564 static short next_word(Curve *cu)
565 {
566         short s;
567         for(s=cu->pos; (cu->str[s]) && (cu->str[s]!=' ') && (cu->str[s]!='\n') &&
568                                         (cu->str[s]!=1) && (cu->str[s]!='\r'); s++);
569         if(cu->str[s]) return(s+1); else return(s);
570 }
571
572 static short prev_word(Curve *cu)
573 {
574         short s;
575         
576         if(cu->pos==0) return(0);
577         for(s=cu->pos-2; (cu->str[s]) && (cu->str[s]!=' ') && (cu->str[s]!='\n') &&
578                                         (cu->str[s]!=1) && (cu->str[s]!='\r'); s--);
579         if(cu->str[s]) return(s+1); else return(s);
580 }
581
582 static int kill_selection(Object *obedit, int ins)      /* 1 == new character */
583 {
584         Curve *cu= obedit->data;
585         EditFont *ef= cu->editfont;
586         int selend, selstart, direction;
587         int offset = 0;
588         int getfrom;
589
590         direction = BKE_font_getselection(obedit, &selstart, &selend);
591         if(direction) {
592                 int size;
593                 if(ins) offset = 1;
594                 if(cu->pos >= selstart) cu->pos = selstart+offset;
595                 if((direction == -1) && ins) {
596                         selstart++;
597                         selend++;
598                 }
599                 getfrom = selend+offset;
600                 if(ins==0) getfrom++;
601                 size = (cu->len * sizeof(wchar_t)) - (selstart * sizeof(wchar_t)) + (offset*sizeof(wchar_t));
602                 memmove(ef->textbuf+selstart, ef->textbuf+getfrom, size);
603                 memmove(ef->textbufinfo+selstart, ef->textbufinfo+getfrom, ((cu->len-selstart)+offset)*sizeof(CharInfo));
604                 cu->len -= (selend-selstart)+offset;
605                 cu->selstart = cu->selend = 0;
606         }
607
608         return(direction);
609 }
610
611 /******************* set style operator ********************/
612
613 static EnumPropertyItem style_items[]= {
614         {CU_BOLD, "BOLD", 0, "Bold", ""},
615         {CU_ITALIC, "ITALIC", 0, "Italic", ""},
616         {CU_UNDERLINE, "UNDERLINE", 0, "Underline", ""},
617         {0, NULL, 0, NULL, NULL}};
618
619 static int set_style(bContext *C, int style, int clear)
620 {
621         Object *obedit= CTX_data_edit_object(C);
622         Curve *cu= obedit->data;
623         EditFont *ef= cu->editfont;
624         int i, selstart, selend;
625
626         if(!BKE_font_getselection(obedit, &selstart, &selend))
627                 return OPERATOR_CANCELLED;
628
629         for(i=selstart; i<=selend; i++) {
630                 if(clear)
631                         ef->textbufinfo[i].flag &= ~style;
632                 else
633                         ef->textbufinfo[i].flag |= style;
634         }
635
636         DAG_id_flush_update(obedit->data, OB_RECALC_DATA);
637         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
638
639         return OPERATOR_FINISHED;
640 }
641
642 static int set_style_exec(bContext *C, wmOperator *op)
643 {
644         int style, clear;
645
646         style= RNA_enum_get(op->ptr, "style");
647         clear= RNA_enum_get(op->ptr, "clear");
648
649         return set_style(C, style, clear);
650 }
651
652 void FONT_OT_style_set(wmOperatorType *ot)
653 {
654         /* identifiers */
655         ot->name= "Set Style";
656         ot->description= "Set font style";
657         ot->idname= "FONT_OT_style_set";
658         
659         /* api callbacks */
660         ot->exec= set_style_exec;
661         ot->poll= ED_operator_editfont;
662         
663         /* flags */
664         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
665
666         /* properties */
667         RNA_def_enum(ot->srna, "style", style_items, CU_BOLD, "Style", "Style to set selection to.");
668         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "Clear style rather than setting it.");
669 }
670
671 /******************* toggle style operator ********************/
672
673 static int toggle_style_exec(bContext *C, wmOperator *op)
674 {
675         Object *obedit= CTX_data_edit_object(C);
676         Curve *cu= obedit->data;
677         int style, clear, selstart, selend;
678
679         if(!BKE_font_getselection(obedit, &selstart, &selend))
680                 return OPERATOR_CANCELLED;
681         
682         style= RNA_enum_get(op->ptr, "style");
683
684         cu->curinfo.flag ^= style;
685         clear= (cu->curinfo.flag & style) == 0;
686
687         return set_style(C, style, clear);
688 }
689
690 void FONT_OT_style_toggle(wmOperatorType *ot)
691 {
692         /* identifiers */
693         ot->name= "Toggle Style";
694         ot->description= "Toggle font style";
695         ot->idname= "FONT_OT_style_toggle";
696         
697         /* api callbacks */
698         ot->exec= toggle_style_exec;
699         ot->poll= ED_operator_editfont;
700         
701         /* flags */
702         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
703
704         /* properties */
705         RNA_def_enum(ot->srna, "style", style_items, CU_BOLD, "Style", "Style to set selection to.");
706 }
707
708 /******************* copy text operator ********************/
709
710 static void copy_selection(Object *obedit)
711 {
712         int selstart, selend;
713         
714         if(BKE_font_getselection(obedit, &selstart, &selend)) {
715                 Curve *cu= obedit->data;
716                 EditFont *ef= cu->editfont;
717                 
718                 memcpy(ef->copybuf, ef->textbuf+selstart, ((selend-selstart)+1)*sizeof(wchar_t));
719                 ef->copybuf[(selend-selstart)+1]=0;
720                 memcpy(ef->copybufinfo, ef->textbufinfo+selstart, ((selend-selstart)+1)*sizeof(CharInfo));      
721         }
722 }
723
724 static int copy_text_exec(bContext *C, wmOperator *op)
725 {
726         Object *obedit= CTX_data_edit_object(C);
727
728         copy_selection(obedit);
729
730         return OPERATOR_FINISHED;
731 }
732
733 void FONT_OT_text_copy(wmOperatorType *ot)
734 {
735         /* identifiers */
736         ot->name= "Copy Text";
737         ot->description= "Copy selected text to clipboard";
738         ot->idname= "FONT_OT_text_copy";
739         
740         /* api callbacks */
741         ot->exec= copy_text_exec;
742         ot->poll= ED_operator_editfont;
743 }
744
745 /******************* cut text operator ********************/
746
747 static int cut_text_exec(bContext *C, wmOperator *op)
748 {
749         Scene *scene= CTX_data_scene(C);
750         Object *obedit= CTX_data_edit_object(C);
751         int selstart, selend;
752
753         if(!BKE_font_getselection(obedit, &selstart, &selend))
754                 return OPERATOR_CANCELLED;
755
756         copy_selection(obedit);
757         kill_selection(obedit, 0);
758
759         text_update_edited(C, scene, obedit, 1, 0);
760
761         return OPERATOR_FINISHED;
762 }
763
764 void FONT_OT_text_cut(wmOperatorType *ot)
765 {
766         /* identifiers */
767         ot->name= "Cut Text";
768         ot->description= "Cut selected text to clipboard";
769         ot->idname= "FONT_OT_text_cut";
770         
771         /* api callbacks */
772         ot->exec= cut_text_exec;
773         ot->poll= ED_operator_editfont;
774
775         /* flags */
776         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
777 }
778
779 /******************* paste text operator ********************/
780
781 static int paste_selection(Object *obedit, ReportList *reports)
782 {
783         Curve *cu= obedit->data;
784         EditFont *ef= cu->editfont;
785         int len= wcslen(ef->copybuf);
786
787         // Verify that the copy buffer => [copy buffer len] + cu->len < MAXTEXT
788         if(cu->len + len <= MAXTEXT) {
789                 if(len) {       
790                         int size = (cu->len * sizeof(wchar_t)) - (cu->pos*sizeof(wchar_t)) + sizeof(wchar_t);
791                         memmove(ef->textbuf+cu->pos+len, ef->textbuf+cu->pos, size);
792                         memcpy(ef->textbuf+cu->pos, ef->copybuf, len * sizeof(wchar_t));
793                 
794                         memmove(ef->textbufinfo+cu->pos+len, ef->textbufinfo+cu->pos, (cu->len-cu->pos+1)*sizeof(CharInfo));
795                         memcpy(ef->textbufinfo+cu->pos, ef->copybufinfo, len*sizeof(CharInfo)); 
796                 
797                         cu->len += len;
798                         cu->pos += len;
799
800                         return 1;
801                 }
802         }
803         else
804                 BKE_report(reports, RPT_WARNING, "Text too long.");
805         
806         return 0;
807 }
808
809 static int paste_text_exec(bContext *C, wmOperator *op)
810 {
811         Scene *scene= CTX_data_scene(C);
812         Object *obedit= CTX_data_edit_object(C);
813
814         if(!paste_selection(obedit, op->reports))
815                 return OPERATOR_CANCELLED;
816
817         text_update_edited(C, scene, obedit, 1, 0);
818
819         return OPERATOR_FINISHED;
820 }
821
822 void FONT_OT_text_paste(wmOperatorType *ot)
823 {
824         /* identifiers */
825         ot->name= "Paste Text";
826         ot->description= "Paste text from clipboard";
827         ot->idname= "FONT_OT_text_paste";
828         
829         /* api callbacks */
830         ot->exec= paste_text_exec;
831         ot->poll= ED_operator_editfont;
832
833         /* flags */
834         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
835 }
836
837 /************************ move operator ************************/
838
839 static EnumPropertyItem move_type_items[]= {
840         {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
841         {LINE_END, "LINE_END", 0, "Line End", ""},
842         {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
843         {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
844         {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
845         {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
846         {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""},
847         {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""},
848         {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""},
849         {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""},
850         {0, NULL, 0, NULL, NULL}};
851
852 static int move_cursor(bContext *C, int type, int select)
853 {
854         Scene *scene= CTX_data_scene(C);
855         Object *obedit= CTX_data_edit_object(C);
856         Curve *cu= obedit->data;
857         EditFont *ef= cu->editfont;
858         int cursmove= 0;
859
860         switch(type) {
861                 case LINE_BEGIN:
862                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
863                         while(cu->pos>0) {
864                                 if(ef->textbuf[cu->pos-1]=='\n') break;
865                                 if(ef->textbufinfo[cu->pos-1].flag & CU_WRAP ) break;                           
866                                 cu->pos--;
867                         }               
868                         cursmove=FO_CURS;
869                         break;
870                         
871                 case LINE_END:
872                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;                
873                         while(cu->pos<cu->len) {
874                                 if(ef->textbuf[cu->pos]==0) break;
875                                 if(ef->textbuf[cu->pos]=='\n') break;
876                                 if(ef->textbufinfo[cu->pos].flag & CU_WRAP ) break;
877                                 cu->pos++;
878                         }
879                         cursmove=FO_CURS;
880                         break;
881
882                 case PREV_WORD:
883                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
884                         cu->pos= prev_word(cu);
885                         cursmove= FO_CURS;
886                         break;
887
888                 case NEXT_WORD:
889                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
890                         cu->pos= next_word(cu);
891                         cursmove= FO_CURS;                              
892                         break;
893
894                 case PREV_CHAR:
895                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
896                         cu->pos--;
897                         cursmove=FO_CURS;
898                         break;
899
900                 case NEXT_CHAR: 
901                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
902                         cu->pos++;
903                         cursmove= FO_CURS;                              
904
905                         break;
906
907                 case PREV_LINE:
908                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
909                         cursmove=FO_CURSUP;
910                         break;
911                         
912                 case NEXT_LINE:
913                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
914                         cursmove= FO_CURSDOWN;
915                         break;
916
917                 case PREV_PAGE:
918                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
919                         cursmove=FO_PAGEUP;
920                         break;
921
922                 case NEXT_PAGE:
923                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
924                         cursmove=FO_PAGEDOWN;
925                         break;
926         }
927                 
928         if(!cursmove)
929                 return OPERATOR_CANCELLED;
930
931         if(select == 0) {
932                 if(cu->selstart) {
933                         cu->selstart = cu->selend = 0;
934                         update_string(cu);
935                         BKE_text_to_curve(scene, obedit, FO_SELCHANGE);
936                 }
937         }
938
939         if(cu->pos>cu->len) cu->pos= cu->len;
940         else if(cu->pos>=MAXTEXT) cu->pos= MAXTEXT;
941         else if(cu->pos<0) cu->pos= 0;
942
943         text_update_edited(C, scene, obedit, select, cursmove);
944
945         if(select)
946                 cu->selend = cu->pos;
947
948         return OPERATOR_FINISHED;
949 }
950
951 static int move_exec(bContext *C, wmOperator *op)
952 {
953         int type= RNA_enum_get(op->ptr, "type");
954
955         return move_cursor(C, type, 0);
956 }
957
958 void FONT_OT_move(wmOperatorType *ot)
959 {
960         /* identifiers */
961         ot->name= "Move Cursor";
962         ot->description= "Move cursor to position type";
963         ot->idname= "FONT_OT_move";
964         
965         /* api callbacks */
966         ot->exec= move_exec;
967         ot->poll= ED_operator_editfont;
968
969         /* flags */
970         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
971
972         /* properties */
973         RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
974 }
975
976 /******************* move select operator ********************/
977
978 static int move_select_exec(bContext *C, wmOperator *op)
979 {
980         int type= RNA_enum_get(op->ptr, "type");
981
982         return move_cursor(C, type, 1);
983 }
984
985 void FONT_OT_move_select(wmOperatorType *ot)
986 {
987         /* identifiers */
988         ot->name= "Move Select";
989         ot->description= "Make selection from current cursor position to new cursor position type";
990         ot->idname= "FONT_OT_move_select";
991         
992         /* api callbacks */
993         ot->exec= move_select_exec;
994         ot->poll= ED_operator_editfont;
995
996         /* flags */
997         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
998
999         /* properties */
1000         RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to, to make a selection.");
1001 }
1002
1003 /************************* change spacing **********************/
1004
1005 static int change_spacing_exec(bContext *C, wmOperator *op)
1006 {
1007         Scene *scene= CTX_data_scene(C);
1008         Object *obedit= CTX_data_edit_object(C);
1009         Curve *cu= obedit->data;
1010         EditFont *ef= cu->editfont;
1011         int kern, delta= RNA_int_get(op->ptr, "delta");
1012
1013         kern = ef->textbufinfo[cu->pos-1].kern;
1014         kern += delta;
1015         CLAMP(kern, -20, 20);
1016
1017         if(ef->textbufinfo[cu->pos-1].kern == kern)
1018                 return OPERATOR_CANCELLED;
1019
1020         ef->textbufinfo[cu->pos-1].kern = kern;
1021
1022         text_update_edited(C, scene, obedit, 1, 0);
1023
1024         return OPERATOR_FINISHED;
1025 }
1026
1027 void FONT_OT_change_spacing(wmOperatorType *ot)
1028 {
1029         /* identifiers */
1030         ot->name= "Change Spacing";
1031         ot->description= "Change font spacing";
1032         ot->idname= "FONT_OT_change_spacing";
1033         
1034         /* api callbacks */
1035         ot->exec= change_spacing_exec;
1036         ot->poll= ED_operator_editfont;
1037
1038         /* flags */
1039         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1040
1041         /* properties */
1042         RNA_def_int(ot->srna, "delta", 1, -20, 20, "Delta", "Amount to decrease or increasing character spacing with.", -20, 20);
1043 }
1044
1045 /************************* change character **********************/
1046
1047 static int change_character_exec(bContext *C, wmOperator *op)
1048 {
1049         Scene *scene= CTX_data_scene(C);
1050         Object *obedit= CTX_data_edit_object(C);
1051         Curve *cu= obedit->data;
1052         EditFont *ef= cu->editfont;
1053         int character, delta= RNA_int_get(op->ptr, "delta");
1054
1055         if(cu->pos <= 0)
1056                 return OPERATOR_CANCELLED;
1057
1058         character= ef->textbuf[cu->pos - 1];
1059         character += delta;
1060         CLAMP(character, 0, 255);
1061
1062         if(character == ef->textbuf[cu->pos - 1])
1063                 return OPERATOR_CANCELLED;
1064
1065         ef->textbuf[cu->pos - 1]= character;
1066
1067         text_update_edited(C, scene, obedit, 1, 0);
1068
1069         return OPERATOR_FINISHED;
1070 }
1071
1072 void FONT_OT_change_character(wmOperatorType *ot)
1073 {
1074         /* identifiers */
1075         ot->name= "Change Character";
1076         ot->description= "Change font character code";
1077         ot->idname= "FONT_OT_change_character";
1078         
1079         /* api callbacks */
1080         ot->exec= change_character_exec;
1081         ot->poll= ED_operator_editfont;
1082
1083         /* flags */
1084         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1085
1086         /* properties */
1087         RNA_def_int(ot->srna, "delta", 1, -255, 255, "Delta", "Number to increase or decrease character code with.", -255, 255);
1088 }
1089
1090 /******************* line break operator ********************/
1091
1092 static int line_break_exec(bContext *C, wmOperator *op)
1093 {
1094         Scene *scene= CTX_data_scene(C);
1095         Object *obedit= CTX_data_edit_object(C);
1096         Curve *cu= obedit->data;
1097         EditFont *ef= cu->editfont;
1098         int ctrl= RNA_enum_get(op->ptr, "ctrl");
1099
1100         if(ctrl) {
1101                 insert_into_textbuf(obedit, 1);
1102                 if(ef->textbuf[cu->pos]!='\n')
1103                         insert_into_textbuf(obedit, '\n');
1104         }
1105         else
1106                 insert_into_textbuf(obedit, '\n');
1107
1108         cu->selstart = cu->selend = 0;
1109
1110         text_update_edited(C, scene, obedit, 1, 0);
1111
1112         return OPERATOR_FINISHED;
1113 }
1114
1115 void FONT_OT_line_break(wmOperatorType *ot)
1116 {
1117         /* identifiers */
1118         ot->name= "Line Break";
1119         ot->description= "Insert line break at cursor position";
1120         ot->idname= "FONT_OT_line_break";
1121         
1122         /* api callbacks */
1123         ot->exec= line_break_exec;
1124         ot->poll= ED_operator_editfont;
1125
1126         /* flags */
1127         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1128
1129         /* properties */
1130         RNA_def_boolean(ot->srna, "ctrl", 0, "Ctrl", ""); // XXX what is this?
1131 }
1132
1133 /******************* delete operator **********************/
1134
1135 static EnumPropertyItem delete_type_items[]= {
1136         {DEL_ALL, "ALL", 0, "All", ""},
1137         {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
1138         {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
1139         {DEL_SELECTION, "SELECTION", 0, "Selection", ""},
1140         {DEL_NEXT_SEL, "NEXT_OR_SELECTION", 0, "Next or Selection", ""},
1141         {DEL_PREV_SEL, "PREVIOUS_OR_SELECTION", 0, "Previous or Selection", ""},
1142         {0, NULL, 0, NULL, NULL}};
1143
1144 static int delete_exec(bContext *C, wmOperator *op)
1145 {
1146         Scene *scene= CTX_data_scene(C);
1147         Object *obedit= CTX_data_edit_object(C);
1148         Curve *cu= obedit->data;
1149         EditFont *ef= cu->editfont;
1150         int x, selstart, selend, type= RNA_enum_get(op->ptr, "type");
1151
1152         if(cu->len == 0)
1153                 return OPERATOR_CANCELLED;
1154
1155         if(BKE_font_getselection(obedit, &selstart, &selend)) {
1156                 if(type == DEL_NEXT_SEL) type= DEL_SELECTION;
1157                 else if(type == DEL_PREV_SEL) type= DEL_SELECTION;
1158         }
1159         else {
1160                 if(type == DEL_NEXT_SEL) type= DEL_NEXT_CHAR;
1161                 else if(type == DEL_PREV_SEL) type= DEL_PREV_CHAR;
1162         }
1163
1164         switch(type) {
1165                 case DEL_ALL:
1166                         cu->len = cu->pos = 0;
1167                         ef->textbuf[0]= 0;
1168                         break;
1169                 case DEL_SELECTION:
1170                         if(!kill_selection(obedit, 0))
1171                                 return OPERATOR_CANCELLED;
1172                         break;
1173                 case DEL_PREV_CHAR:
1174                         if(cu->pos<=0)
1175                                 return OPERATOR_CANCELLED;
1176
1177                         for(x=cu->pos;x<=cu->len;x++)
1178                                 ef->textbuf[x-1]= ef->textbuf[x];
1179                         for(x=cu->pos;x<=cu->len;x++)
1180                                 ef->textbufinfo[x-1]= ef->textbufinfo[x];                                       
1181
1182                         cu->pos--;
1183                         ef->textbuf[--cu->len]='\0';
1184                         break;
1185                 case DEL_NEXT_CHAR:
1186                         if(cu->pos>=cu->len)
1187                                 return OPERATOR_CANCELLED;
1188
1189                         for(x=cu->pos;x<cu->len;x++)
1190                                 ef->textbuf[x]= ef->textbuf[x+1];
1191                         for(x=cu->pos;x<cu->len;x++)
1192                                 ef->textbufinfo[x]= ef->textbufinfo[x+1];                                       
1193
1194                         ef->textbuf[--cu->len]='\0';
1195                         break;
1196                 default:
1197                         return OPERATOR_CANCELLED;
1198         }
1199
1200         text_update_edited(C, scene, obedit, 1, 0);
1201
1202         return OPERATOR_FINISHED;
1203 }
1204
1205 void FONT_OT_delete(wmOperatorType *ot)
1206 {
1207         /* identifiers */
1208         ot->name= "Delete";
1209         ot->description= "Delete text by cursor position";
1210         ot->idname= "FONT_OT_delete";
1211         
1212         /* api callbacks */
1213         ot->exec= delete_exec;
1214         ot->poll= ED_operator_editfont;
1215
1216         /* flags */
1217         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1218
1219         /* properties */
1220         RNA_def_enum(ot->srna, "type", delete_type_items, DEL_ALL, "Type", "Which part of the text to delete.");
1221 }
1222
1223 /*********************** insert text operator *************************/
1224
1225 static int insert_text_exec(bContext *C, wmOperator *op)
1226 {
1227         Scene *scene= CTX_data_scene(C);
1228         Object *obedit= CTX_data_edit_object(C);
1229         char *inserted_utf8;
1230         wchar_t *inserted_text, first;
1231         int len;
1232
1233         if(!RNA_property_is_set(op->ptr, "text"))
1234                 return OPERATOR_CANCELLED;
1235         
1236         inserted_utf8= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
1237         len= strlen(inserted_utf8);
1238
1239         inserted_text= MEM_callocN(sizeof(wchar_t)*(len+1), "FONT_insert_text");
1240         utf8towchar(inserted_text, inserted_utf8);
1241         first= inserted_text[0];
1242
1243         MEM_freeN(inserted_text);
1244         MEM_freeN(inserted_utf8);
1245
1246         if(!first)
1247                 return OPERATOR_CANCELLED;
1248
1249         insert_into_textbuf(obedit, first);
1250         kill_selection(obedit, 1);
1251         text_update_edited(C, scene, obedit, 1, 0);
1252
1253         return OPERATOR_FINISHED;
1254 }
1255
1256 static int insert_text_invoke(bContext *C, wmOperator *op, wmEvent *evt)
1257 {
1258         Scene *scene= CTX_data_scene(C);
1259         Object *obedit= CTX_data_edit_object(C);
1260         Curve *cu= obedit->data;
1261         EditFont *ef= cu->editfont;
1262         static int accentcode= 0;
1263         uintptr_t ascii = evt->ascii;
1264         int alt= evt->alt, shift= evt->shift, ctrl= evt->ctrl;
1265         int event= evt->type, val= evt->val;
1266         wchar_t inserted_text[2]= {0};
1267
1268         if(RNA_property_is_set(op->ptr, "text"))
1269                 return insert_text_exec(C, op);
1270         
1271         /* tab should exit editmode, but we allow it to be typed using modifier keys */
1272         if(event==TABKEY) {
1273                 if((alt||ctrl||shift) == 0)
1274                         return OPERATOR_PASS_THROUGH;
1275                 else
1276                         ascii= 9;
1277         }
1278         else if(event==BACKSPACEKEY)
1279                 ascii= 0;
1280
1281         if(val && ascii) {
1282                 /* handle case like TAB (== 9) */
1283                 if((ascii > 31 && ascii < 254 && ascii != 127) || (ascii==13) || (ascii==10) || (ascii==8)) {
1284                         if(accentcode) {
1285                                 if(cu->pos>0) {
1286                                         inserted_text[0]= findaccent(ef->textbuf[cu->pos-1], ascii);
1287                                         ef->textbuf[cu->pos-1]= inserted_text[0];
1288                                 }
1289                                 accentcode= 0;
1290                         }
1291                         else if(cu->len<MAXTEXT-1) {
1292                                 if(alt) {
1293                                         /* might become obsolete, apple has default values for this, other OS's too? */
1294                                         if(ascii=='t') ascii= 137;
1295                                         else if(ascii=='c') ascii= 169;
1296                                         else if(ascii=='f') ascii= 164;
1297                                         else if(ascii=='g') ascii= 176;
1298                                         else if(ascii=='l') ascii= 163;
1299                                         else if(ascii=='r') ascii= 174;
1300                                         else if(ascii=='s') ascii= 223;
1301                                         else if(ascii=='y') ascii= 165;
1302                                         else if(ascii=='.') ascii= 138;
1303                                         else if(ascii=='1') ascii= 185;
1304                                         else if(ascii=='2') ascii= 178;
1305                                         else if(ascii=='3') ascii= 179;
1306                                         else if(ascii=='%') ascii= 139;
1307                                         else if(ascii=='?') ascii= 191;
1308                                         else if(ascii=='!') ascii= 161;
1309                                         else if(ascii=='x') ascii= 215;
1310                                         else if(ascii=='>') ascii= 187;
1311                                         else if(ascii=='<') ascii= 171;
1312                                 }
1313
1314                                 inserted_text[0]= ascii;
1315                                 insert_into_textbuf(obedit, ascii);
1316                         }
1317                         
1318                         kill_selection(obedit, 1);
1319                         text_update_edited(C, scene, obedit, 1, 0);
1320                 }
1321                 else {
1322                         inserted_text[0]= ascii;
1323                         insert_into_textbuf(obedit, ascii);
1324                         text_update_edited(C, scene, obedit, 1, 0);
1325                 }
1326         }
1327         else if(val && event == BACKSPACEKEY) {
1328                 if(alt && cu->len!=0 && cu->pos>0)
1329                         accentcode= 1;
1330
1331                 return OPERATOR_PASS_THROUGH;
1332         }
1333         else
1334                 return OPERATOR_PASS_THROUGH;
1335
1336         if(inserted_text[0]) {
1337                 /* store as utf8 in RNA string */
1338                 char inserted_utf8[8] = {0};
1339
1340                 wcs2utf8s(inserted_utf8, inserted_text);
1341                 RNA_string_set(op->ptr, "text", inserted_utf8);
1342         }
1343
1344         return OPERATOR_FINISHED;
1345 }
1346
1347 void FONT_OT_text_insert(wmOperatorType *ot)
1348 {
1349         /* identifiers */
1350         ot->name= "Insert Text";
1351         ot->description= "Insert text at cursor position";
1352         ot->idname= "FONT_OT_text_insert";
1353         
1354         /* api callbacks */
1355         ot->exec= insert_text_exec;
1356         ot->invoke= insert_text_invoke;
1357         ot->poll= ED_operator_editfont;
1358         
1359         /* flags */
1360         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1361
1362         /* properties */
1363         RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
1364 }
1365
1366 /***************** editmode enter/exit ********************/
1367
1368 void make_editText(Object *obedit)
1369 {
1370         Curve *cu= obedit->data;
1371         EditFont *ef= cu->editfont;
1372         
1373         if(ef==NULL) {
1374                 ef= cu->editfont= MEM_callocN(sizeof(EditFont), "editfont");
1375         
1376                 ef->textbuf= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "texteditbuf");
1377                 ef->textbufinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "texteditbufinfo");
1378                 ef->copybuf= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "texteditcopybuf");
1379                 ef->copybufinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "texteditcopybufinfo");      
1380                 ef->oldstr= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "oldstrbuf");
1381                 ef->oldstrinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "oldstrbuf");
1382         }
1383         
1384         // Convert the original text to wchar_t
1385         utf8towchar(ef->textbuf, cu->str);
1386         wcscpy(ef->oldstr, ef->textbuf);
1387                 
1388         cu->len= wcslen(ef->textbuf);
1389         
1390         memcpy(ef->textbufinfo, cu->strinfo, (cu->len)*sizeof(CharInfo));
1391         memcpy(ef->oldstrinfo, cu->strinfo, (cu->len)*sizeof(CharInfo));
1392
1393         if(cu->pos>cu->len) cu->pos= cu->len;
1394
1395         if(cu->pos)
1396                 cu->curinfo = ef->textbufinfo[cu->pos-1];
1397         else
1398                 cu->curinfo = ef->textbufinfo[0];
1399         
1400         // Convert to UTF-8
1401         update_string(cu);
1402 }
1403
1404 void load_editText(Object *obedit)
1405 {
1406         Curve *cu= obedit->data;
1407         EditFont *ef= cu->editfont;
1408         
1409         MEM_freeN(ef->oldstr);
1410         ef->oldstr= NULL;
1411         MEM_freeN(ef->oldstrinfo);
1412         ef->oldstrinfo= NULL;
1413         
1414         update_string(cu);
1415         
1416         if(cu->strinfo)
1417                 MEM_freeN(cu->strinfo);
1418         cu->strinfo= MEM_callocN((cu->len+4)*sizeof(CharInfo), "texteditinfo");
1419         memcpy(cu->strinfo, ef->textbufinfo, (cu->len)*sizeof(CharInfo));
1420
1421         cu->len= strlen(cu->str);
1422         
1423         /* this memory system is weak... */
1424         
1425         if(cu->selboxes) {
1426                 MEM_freeN(cu->selboxes);
1427                 cu->selboxes= NULL;
1428         }
1429 }
1430
1431 void free_editText(Object *obedit)
1432 {
1433         BKE_free_editfont((Curve *)obedit->data);
1434 }
1435
1436 /********************** set case operator *********************/
1437
1438 static EnumPropertyItem case_items[]= {
1439         {CASE_LOWER, "LOWER", 0, "Lower", ""},
1440         {CASE_UPPER, "UPPER", 0, "Upper", ""},
1441         {0, NULL, 0, NULL, NULL}};
1442
1443 static int set_case(bContext *C, int ccase)
1444 {
1445         Scene *scene= CTX_data_scene(C);
1446         Object *obedit= CTX_data_edit_object(C);
1447         Curve *cu= obedit->data;
1448         EditFont *ef= cu->editfont;
1449         wchar_t *str;
1450         int len;
1451         
1452         len= wcslen(ef->textbuf);
1453         str= ef->textbuf;
1454         while(len) {
1455                 if(*str>='a' && *str<='z')
1456                         *str-= 32;
1457                 len--;
1458                 str++;
1459         }
1460         
1461         if(ccase == CASE_LOWER) {
1462                 len= wcslen(ef->textbuf);
1463                 str= ef->textbuf;
1464                 while(len) {
1465                         if(*str>='A' && *str<='Z') {
1466                                 *str+= 32;
1467                         }
1468                         len--;
1469                         str++;
1470                 }
1471         }
1472
1473         text_update_edited(C, scene, obedit, 1, 0);
1474
1475         return OPERATOR_FINISHED;
1476 }
1477
1478 static int set_case_exec(bContext *C, wmOperator *op)
1479 {
1480         return set_case(C, RNA_enum_get(op->ptr, "case"));
1481 }
1482
1483 void FONT_OT_case_set(wmOperatorType *ot)
1484 {
1485         /* identifiers */
1486         ot->name= "Set Case";
1487         ot->description= "Set font case";
1488         ot->idname= "FONT_OT_case_set";
1489         
1490         /* api callbacks */
1491         ot->exec= set_case_exec;
1492         ot->poll= ED_operator_editfont;
1493
1494         /* flags */
1495         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1496
1497         /* properties */
1498         RNA_def_enum(ot->srna, "case", case_items, CASE_LOWER, "Case", "Lower or upper case.");
1499 }
1500
1501 /********************** toggle case operator *********************/
1502
1503 static int toggle_case_exec(bContext *C, wmOperator *op)
1504 {
1505         Object *obedit= CTX_data_edit_object(C);
1506         Curve *cu= obedit->data;
1507         EditFont *ef= cu->editfont;
1508         wchar_t *str;
1509         int len, ccase= CASE_UPPER;
1510         
1511         len= wcslen(ef->textbuf);
1512         str= ef->textbuf;
1513         while(len) {
1514                 if(*str>='a' && *str<='z') {
1515                         ccase= CASE_LOWER;
1516                         break;
1517                 }
1518
1519                 len--;
1520                 str++;
1521         }
1522         
1523         return set_case(C, ccase);
1524 }
1525
1526 void FONT_OT_case_toggle(wmOperatorType *ot)
1527 {
1528         /* identifiers */
1529         ot->name= "Toggle Case";
1530         ot->description= "Toggle font case";
1531         ot->idname= "FONT_OT_case_toggle";
1532         
1533         /* api callbacks */
1534         ot->exec= toggle_case_exec;
1535         ot->poll= ED_operator_editfont;
1536
1537         /* flags */
1538         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1539 }
1540
1541 /* **************** Open Font ************** */
1542
1543 static void open_init(bContext *C, wmOperator *op)
1544 {
1545         PropertyPointerRNA *pprop;
1546         
1547         op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
1548         uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
1549 }
1550
1551 static int open_cancel(bContext *C, wmOperator *op)
1552 {
1553         MEM_freeN(op->customdata);
1554         op->customdata= NULL;
1555         return OPERATOR_CANCELLED;
1556 }
1557
1558 static int open_exec(bContext *C, wmOperator *op)
1559 {
1560         Object *ob = CTX_data_active_object(C);
1561         Curve *cu;
1562         VFont *font;
1563         PropertyPointerRNA *pprop;
1564         PointerRNA idptr;
1565         char str[FILE_MAX];
1566         
1567         RNA_string_get(op->ptr, "path", str);
1568
1569         font = load_vfont(str);
1570         
1571         if(!font) {
1572                 if(op->customdata) MEM_freeN(op->customdata);
1573                 return OPERATOR_CANCELLED;
1574         }
1575         
1576         if(!op->customdata)
1577                 open_init(C, op);
1578         
1579         /* hook into UI */
1580         pprop= op->customdata;
1581         
1582         if(pprop->prop) {
1583                 /* when creating new ID blocks, use is already 1, but RNA
1584                  * pointer se also increases user, so this compensates it */
1585                 font->id.us--;
1586                 
1587                 RNA_id_pointer_create(&font->id, &idptr);
1588                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
1589                 RNA_property_update(C, &pprop->ptr, pprop->prop);
1590         } else if(ob && ob->type == OB_FONT) {
1591                 cu = ob->data;
1592                 id_us_min(&cu->vfont->id);
1593                 cu->vfont = font;
1594         }
1595         
1596         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1597         WM_event_add_notifier(C, NC_GEOM|ND_DATA|NA_EDITED, ob->data);
1598         
1599         MEM_freeN(op->customdata);
1600         
1601         return OPERATOR_FINISHED;
1602 }
1603
1604 static int open_invoke(bContext *C, wmOperator *op, wmEvent *event)
1605 {
1606         Object *ob = CTX_data_active_object(C);
1607         Curve *cu;
1608         VFont *font=NULL;
1609         char *path;
1610         if (ob && ob->type == OB_FONT) {
1611                 cu = ob->data;
1612                 font = cu->vfont;
1613         }
1614         path = (font && font->name)? font->name: U.fontdir;
1615          
1616         if(RNA_property_is_set(op->ptr, "path"))
1617                 return open_exec(C, op);
1618         
1619         open_init(C, op);
1620         
1621         RNA_string_set(op->ptr, "path", path);
1622         WM_event_add_fileselect(C, op); 
1623
1624         return OPERATOR_RUNNING_MODAL;
1625 }
1626
1627 void FONT_OT_open(wmOperatorType *ot)
1628 {
1629         /* identifiers */
1630         ot->name= "Open";
1631         ot->idname= "FONT_OT_open";
1632         
1633         /* api callbacks */
1634         ot->exec= open_exec;
1635         ot->invoke= open_invoke;
1636         ot->cancel= open_cancel;
1637         
1638         /* flags */
1639         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1640         
1641         /* properties */
1642         WM_operator_properties_filesel(ot, FOLDERFILE|FTFONTFILE, FILE_SPECIAL, FILE_OPENFILE);
1643 }
1644
1645 /******************* delete operator *********************/
1646 static int font_unlink_poll(bContext *C)
1647 {
1648         Object *ob = CTX_data_active_object(C);
1649         Curve *cu;
1650         
1651         if (!ED_operator_object_active_editable(C) ) return 0;
1652         if (ob->type != OB_FONT) return 0;
1653         
1654         cu = ob->data;
1655         if (cu && strcmp(cu->vfont->name, "<builtin>")==0) return 0;
1656         return 1;
1657 }
1658
1659 static int font_unlink_exec(bContext *C, wmOperator *op)
1660 {
1661         Object *ob = CTX_data_active_object(C);
1662         Curve *cu;
1663         VFont *font, *builtin_font;
1664                 
1665         cu = ob->data;
1666         font = cu->vfont;
1667         
1668         if (!font) {
1669                 BKE_report(op->reports, RPT_ERROR, "No font datablock available to unlink.");
1670                 return OPERATOR_CANCELLED;
1671         }
1672         
1673         if (strcmp(font->name, "<builtin>")==0) {
1674                 BKE_report(op->reports, RPT_WARNING, "Can't unlink the default builtin font.");
1675                 return OPERATOR_FINISHED;
1676         }
1677
1678         /* revert back to builtin font */
1679         builtin_font = get_builtin_font();
1680
1681         cu->vfont = builtin_font;
1682         id_us_plus(&cu->vfont->id);
1683         id_us_min(&font->id);
1684         
1685         DAG_id_flush_update(ob->data, OB_RECALC_DATA);
1686         WM_event_add_notifier(C, NC_GEOM|ND_DATA|NA_EDITED, ob->data);
1687         
1688         return OPERATOR_FINISHED;
1689 }
1690
1691 void FONT_OT_unlink(wmOperatorType *ot)
1692 {
1693         /* identifiers */
1694         ot->name= "Unlink";
1695         ot->idname= "FONT_OT_unlink";
1696         ot->description= "Unlink active font data block";
1697         
1698         /* api callbacks */
1699         ot->exec= font_unlink_exec;
1700         ot->poll= font_unlink_poll;
1701 }
1702
1703
1704 /* **************** undo for font object ************** */
1705
1706 static void undoFont_to_editFont(void *strv, void *ecu)
1707 {
1708         Curve *cu= (Curve *)ecu;
1709         EditFont *ef= cu->editfont;
1710         char *str= strv;
1711
1712         cu->pos= *((short *)str);
1713         cu->len= *((short *)(str+2));
1714
1715         memcpy(ef->textbuf, str+4, (cu->len+1)*sizeof(wchar_t));
1716         memcpy(ef->textbufinfo, str+4 + (cu->len+1)*sizeof(wchar_t), cu->len*sizeof(CharInfo));
1717         
1718         cu->selstart = cu->selend = 0;
1719         
1720         update_string(cu);
1721 }
1722
1723 static void *editFont_to_undoFont(void *ecu)
1724 {
1725         Curve *cu= (Curve *)ecu;
1726         EditFont *ef= cu->editfont;
1727         char *str;
1728         
1729         // The undo buffer includes [MAXTEXT+6]=actual string and [MAXTEXT+4]*sizeof(CharInfo)=charinfo
1730         str= MEM_callocN((MAXTEXT+6)*sizeof(wchar_t) + (MAXTEXT+4)*sizeof(CharInfo), "string undo");
1731
1732         // Copy the string and string information
1733         memcpy(str+4, ef->textbuf, (cu->len+1)*sizeof(wchar_t));
1734         memcpy(str+4 + (cu->len+1)*sizeof(wchar_t), ef->textbufinfo, cu->len*sizeof(CharInfo));
1735
1736         *((short *)str)= cu->pos;
1737         *((short *)(str+2))= cu->len;   
1738         
1739         return str;
1740 }
1741
1742 static void free_undoFont(void *strv)
1743 {
1744         MEM_freeN(strv);
1745 }
1746
1747 static void *get_undoFont(bContext *C)
1748 {
1749         Object *obedit= CTX_data_edit_object(C);
1750         if(obedit && obedit->type==OB_FONT) {
1751                 return obedit->data;
1752         }
1753         return NULL;
1754 }
1755
1756 /* and this is all the undo system needs to know */
1757 void undo_push_font(bContext *C, char *name)
1758 {
1759         undo_editmode_push(C, name, get_undoFont, free_undoFont, undoFont_to_editFont, editFont_to_undoFont, NULL);
1760 }