fix [#28846] Relative paths on linked scene fails
[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 /** \file blender/editors/curve/editfont.c
29  *  \ingroup edcurve
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <fcntl.h>
36 #include <wchar.h>
37
38 #ifndef WIN32 
39 #include <unistd.h>
40 #else
41 #include <io.h>
42 #endif
43
44 #include "MEM_guardedalloc.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_math.h"
48 #include "BLI_utildefines.h"
49
50 #include "DNA_curve_types.h"
51 #include "DNA_object_types.h"
52 #include "DNA_vfont_types.h"
53 #include "DNA_scene_types.h"
54 #include "DNA_text_types.h"
55
56 #include "BKE_context.h"
57 #include "BKE_curve.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_font.h"
60 #include "BKE_library.h"
61 #include "BKE_object.h"
62 #include "BKE_report.h"
63
64 #include "RNA_access.h"
65 #include "RNA_define.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "ED_curve.h"
71 #include "ED_object.h"
72 #include "ED_screen.h"
73 #include "ED_util.h"
74
75 #include "UI_interface.h"
76
77 #include "curve_intern.h"
78
79 #define MAXTEXT 32766
80
81 /************************* utilities ******************************/
82
83 static char findaccent(char char1, unsigned int code)
84 {
85         char new= 0;
86         
87         if(char1=='a') {
88                 if(code=='`') new= 224;
89                 else if(code==39) new= 225;
90                 else if(code=='^') new= 226;
91                 else if(code=='~') new= 227;
92                 else if(code=='"') new= 228;
93                 else if(code=='o') new= 229;
94                 else if(code=='e') new= 230;
95                 else if(code=='-') new= 170;
96         }
97         else if(char1=='c') {
98                 if(code==',') new= 231;
99                 if(code=='|') new= 162;
100         }
101         else if(char1=='e') {
102                 if(code=='`') new= 232;
103                 else if(code==39) new= 233;
104                 else if(code=='^') new= 234;
105                 else if(code=='"') new= 235;
106         }
107         else if(char1=='i') {
108                 if(code=='`') new= 236;
109                 else if(code==39) new= 237;
110                 else if(code=='^') new= 238;
111                 else if(code=='"') new= 239;
112         }
113         else if(char1=='n') {
114                 if(code=='~') new= 241;
115         }
116         else if(char1=='o') {
117                 if(code=='`') new= 242;
118                 else if(code==39) new= 243;
119                 else if(code=='^') new= 244;
120                 else if(code=='~') new= 245;
121                 else if(code=='"') new= 246;
122                 else if(code=='/') new= 248;
123                 else if(code=='-') new= 186;
124                 else if(code=='e') new= 143;
125         }
126         else if(char1=='s') {
127                 if(code=='s') new= 167;
128         }
129         else if(char1=='u') {
130                 if(code=='`') new= 249;
131                 else if(code==39) new= 250;
132                 else if(code=='^') new= 251;
133                 else if(code=='"') new= 252;
134         }
135         else if(char1=='y') {
136                 if(code==39) new= 253;
137                 else if(code=='"') new= 255;
138         }
139         else if(char1=='A') {
140                 if(code=='`') new= 192;
141                 else if(code==39) new= 193;
142                 else if(code=='^') new= 194;
143                 else if(code=='~') new= 195;
144                 else if(code=='"') new= 196;
145                 else if(code=='o') new= 197;
146                 else if(code=='e') new= 198;
147         }
148         else if(char1=='C') {
149                 if(code==',') new= 199;
150         }
151         else if(char1=='E') {
152                 if(code=='`') new= 200;
153                 else if(code==39) new= 201;
154                 else if(code=='^') new= 202;
155                 else if(code=='"') new= 203;
156         }
157         else if(char1=='I') {
158                 if(code=='`') new= 204;
159                 else if(code==39) new= 205;
160                 else if(code=='^') new= 206;
161                 else if(code=='"') new= 207;
162         }
163         else if(char1=='N') {
164                 if(code=='~') new= 209;
165         }
166         else if(char1=='O') {
167                 if(code=='`') new= 210;
168                 else if(code==39) new= 211;
169                 else if(code=='^') new= 212;
170                 else if(code=='~') new= 213;
171                 else if(code=='"') new= 214;
172                 else if(code=='/') new= 216;
173                 else if(code=='e') new= 141;
174         }
175         else if(char1=='U') {
176                 if(code=='`') new= 217;
177                 else if(code==39) new= 218;
178                 else if(code=='^') new= 219;
179                 else if(code=='"') new= 220;
180         }
181         else if(char1=='Y') {
182                 if(code==39) new= 221;
183         }
184         else if(char1=='1') {
185                 if(code=='4') new= 188;
186                 if(code=='2') new= 189;
187         }
188         else if(char1=='3') {
189                 if(code=='4') new= 190;
190         }
191         else if(char1==':') {
192                 if(code=='-') new= 247;
193         }
194         else if(char1=='-') {
195                 if(code==':') new= 247;
196                 if(code=='|') new= 135;
197                 if(code=='+') new= 177;
198         }
199         else if(char1=='|') {
200                 if(code=='-') new= 135;
201                 if(code=='=') new= 136;
202         }
203         else if(char1=='=') {
204                 if(code=='|') new= 136;
205         }
206         else if(char1=='+') {
207                 if(code=='-') new= 177;
208         }
209         
210         if(new) return new;
211         else return char1;
212 }
213
214
215 static void update_string(Curve *cu)
216 {
217         EditFont *ef= cu->editfont;
218         int len;
219
220         // Free the old curve string    
221         MEM_freeN(cu->str);
222
223         // Calculate the actual string length in UTF-8 variable characters
224         len = wcsleninu8(ef->textbuf);
225
226         // Alloc memory for UTF-8 variable char length string
227         cu->str = MEM_callocN(len + sizeof(wchar_t), "str");
228
229         // Copy the wchar to UTF-8
230         wcs2utf8s(cu->str, ef->textbuf);
231 }
232
233 static int insert_into_textbuf(Object *obedit, uintptr_t c)
234 {
235         Curve *cu= obedit->data;
236         
237         if(cu->len<MAXTEXT-1) {
238                 EditFont *ef= cu->editfont;
239                 int x;
240
241                 for(x= cu->len; x>cu->pos; x--) ef->textbuf[x]= ef->textbuf[x-1];
242                 for(x= cu->len; x>cu->pos; x--) ef->textbufinfo[x]= ef->textbufinfo[x-1];               
243                 ef->textbuf[cu->pos]= c;
244                 ef->textbufinfo[cu->pos] = cu->curinfo;
245                 ef->textbufinfo[cu->pos].kern = 0;
246                 ef->textbufinfo[cu->pos].mat_nr = obedit->actcol;
247                                         
248                 cu->pos++;
249                 cu->len++;
250                 ef->textbuf[cu->len]='\0';
251
252                 update_string(cu);
253
254                 return 1;
255         }
256         else
257                 return 0;
258 }
259
260 static void text_update_edited(bContext *C, Scene *scene, Object *obedit, int recalc, int mode)
261 {
262         struct Main *bmain= CTX_data_main(C);
263         Curve *cu= obedit->data;
264         EditFont *ef= cu->editfont;
265         cu->curinfo = ef->textbufinfo[cu->pos?cu->pos-1:0];
266         
267         if(obedit->totcol>0)
268                 obedit->actcol= ef->textbufinfo[cu->pos?cu->pos-1:0].mat_nr;
269
270         if(mode == FO_EDIT)
271                 update_string(cu);
272
273         BKE_text_to_curve(bmain, scene, obedit, mode);
274
275         if(recalc)
276                 DAG_id_tag_update(obedit->data, 0);
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 *UNUSED(op))
283 {
284         Object *obedit= CTX_data_edit_object(C);
285         const char *p, *p2;
286         int i;
287         static const 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_tag_update(obedit->data, 0);
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, const 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, FO_EDIT);
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, "filepath", 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 *UNUSED(event))
395 {
396         if(RNA_property_is_set(op->ptr, "filepath"))
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, WM_FILESEL_FILEPATH);
421 }
422
423 /******************* paste buffer operator ********************/
424
425 static int paste_buffer_exec(bContext *C, wmOperator *UNUSED(op))
426 {
427         const 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_CHINFO_BOLD, "BOLD", 0, "Bold", ""},
615         {CU_CHINFO_ITALIC, "ITALIC", 0, "Italic", ""},
616         {CU_CHINFO_UNDERLINE, "UNDERLINE", 0, "Underline", ""},
617         {CU_CHINFO_SMALLCAPS, "SMALL_CAPS", 0, "Small Caps", ""},
618         {0, NULL, 0, NULL, NULL}};
619
620 static int set_style(bContext *C, const int style, const int clear)
621 {
622         Object *obedit= CTX_data_edit_object(C);
623         Curve *cu= obedit->data;
624         EditFont *ef= cu->editfont;
625         int i, selstart, selend;
626
627         if(!BKE_font_getselection(obedit, &selstart, &selend))
628                 return OPERATOR_CANCELLED;
629
630         for(i=selstart; i<=selend; i++) {
631                 if(clear)
632                         ef->textbufinfo[i].flag &= ~style;
633                 else
634                         ef->textbufinfo[i].flag |= style;
635         }
636
637         DAG_id_tag_update(obedit->data, 0);
638         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
639
640         return OPERATOR_FINISHED;
641 }
642
643 static int set_style_exec(bContext *C, wmOperator *op)
644 {
645         const int style= RNA_enum_get(op->ptr, "style");
646         const int clear= RNA_boolean_get(op->ptr, "clear");
647
648         return set_style(C, style, clear);
649 }
650
651 void FONT_OT_style_set(wmOperatorType *ot)
652 {
653         /* identifiers */
654         ot->name= "Set Style";
655         ot->description= "Set font style";
656         ot->idname= "FONT_OT_style_set";
657         
658         /* api callbacks */
659         ot->exec= set_style_exec;
660         ot->poll= ED_operator_editfont;
661         
662         /* flags */
663         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
664
665         /* properties */
666         RNA_def_enum(ot->srna, "style", style_items, CU_CHINFO_BOLD, "Style", "Style to set selection to");
667         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "Clear style rather than setting it");
668 }
669
670 /******************* toggle style operator ********************/
671
672 static int toggle_style_exec(bContext *C, wmOperator *op)
673 {
674         Object *obedit= CTX_data_edit_object(C);
675         Curve *cu= obedit->data;
676         int style, clear, selstart, selend;
677
678         if(!BKE_font_getselection(obedit, &selstart, &selend))
679                 return OPERATOR_CANCELLED;
680         
681         style= RNA_enum_get(op->ptr, "style");
682
683         cu->curinfo.flag ^= style;
684         clear= (cu->curinfo.flag & style) == 0;
685
686         return set_style(C, style, clear);
687 }
688
689 void FONT_OT_style_toggle(wmOperatorType *ot)
690 {
691         /* identifiers */
692         ot->name= "Toggle Style";
693         ot->description= "Toggle font style";
694         ot->idname= "FONT_OT_style_toggle";
695         
696         /* api callbacks */
697         ot->exec= toggle_style_exec;
698         ot->poll= ED_operator_editfont;
699         
700         /* flags */
701         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
702
703         /* properties */
704         RNA_def_enum(ot->srna, "style", style_items, CU_CHINFO_BOLD, "Style", "Style to set selection to");
705 }
706
707 /******************* copy text operator ********************/
708
709 static void copy_selection(Object *obedit)
710 {
711         int selstart, selend;
712         
713         if(BKE_font_getselection(obedit, &selstart, &selend)) {
714                 Curve *cu= obedit->data;
715                 EditFont *ef= cu->editfont;
716                 
717                 memcpy(ef->copybuf, ef->textbuf+selstart, ((selend-selstart)+1)*sizeof(wchar_t));
718                 ef->copybuf[(selend-selstart)+1]=0;
719                 memcpy(ef->copybufinfo, ef->textbufinfo+selstart, ((selend-selstart)+1)*sizeof(CharInfo));      
720         }
721 }
722
723 static int copy_text_exec(bContext *C, wmOperator *UNUSED(op))
724 {
725         Object *obedit= CTX_data_edit_object(C);
726
727         copy_selection(obedit);
728
729         return OPERATOR_FINISHED;
730 }
731
732 void FONT_OT_text_copy(wmOperatorType *ot)
733 {
734         /* identifiers */
735         ot->name= "Copy Text";
736         ot->description= "Copy selected text to clipboard";
737         ot->idname= "FONT_OT_text_copy";
738         
739         /* api callbacks */
740         ot->exec= copy_text_exec;
741         ot->poll= ED_operator_editfont;
742 }
743
744 /******************* cut text operator ********************/
745
746 static int cut_text_exec(bContext *C, wmOperator *UNUSED(op))
747 {
748         Scene *scene= CTX_data_scene(C);
749         Object *obedit= CTX_data_edit_object(C);
750         int selstart, selend;
751
752         if(!BKE_font_getselection(obedit, &selstart, &selend))
753                 return OPERATOR_CANCELLED;
754
755         copy_selection(obedit);
756         kill_selection(obedit, 0);
757
758         text_update_edited(C, scene, obedit, 1, FO_EDIT);
759
760         return OPERATOR_FINISHED;
761 }
762
763 void FONT_OT_text_cut(wmOperatorType *ot)
764 {
765         /* identifiers */
766         ot->name= "Cut Text";
767         ot->description= "Cut selected text to clipboard";
768         ot->idname= "FONT_OT_text_cut";
769         
770         /* api callbacks */
771         ot->exec= cut_text_exec;
772         ot->poll= ED_operator_editfont;
773
774         /* flags */
775         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
776 }
777
778 /******************* paste text operator ********************/
779
780 static int paste_selection(Object *obedit, ReportList *reports)
781 {
782         Curve *cu= obedit->data;
783         EditFont *ef= cu->editfont;
784         int len= wcslen(ef->copybuf);
785
786         // Verify that the copy buffer => [copy buffer len] + cu->len < MAXTEXT
787         if(cu->len + len <= MAXTEXT) {
788                 if(len) {       
789                         int size = (cu->len * sizeof(wchar_t)) - (cu->pos*sizeof(wchar_t)) + sizeof(wchar_t);
790                         memmove(ef->textbuf+cu->pos+len, ef->textbuf+cu->pos, size);
791                         memcpy(ef->textbuf+cu->pos, ef->copybuf, len * sizeof(wchar_t));
792                 
793                         memmove(ef->textbufinfo+cu->pos+len, ef->textbufinfo+cu->pos, (cu->len-cu->pos+1)*sizeof(CharInfo));
794                         memcpy(ef->textbufinfo+cu->pos, ef->copybufinfo, len*sizeof(CharInfo)); 
795                 
796                         cu->len += len;
797                         cu->pos += len;
798
799                         return 1;
800                 }
801         }
802         else
803                 BKE_report(reports, RPT_WARNING, "Text too long");
804         
805         return 0;
806 }
807
808 static int paste_text_exec(bContext *C, wmOperator *op)
809 {
810         Scene *scene= CTX_data_scene(C);
811         Object *obedit= CTX_data_edit_object(C);
812
813         if(!paste_selection(obedit, op->reports))
814                 return OPERATOR_CANCELLED;
815
816         text_update_edited(C, scene, obedit, 1, FO_EDIT);
817
818         return OPERATOR_FINISHED;
819 }
820
821 void FONT_OT_text_paste(wmOperatorType *ot)
822 {
823         /* identifiers */
824         ot->name= "Paste Text";
825         ot->description= "Paste text from clipboard";
826         ot->idname= "FONT_OT_text_paste";
827         
828         /* api callbacks */
829         ot->exec= paste_text_exec;
830         ot->poll= ED_operator_editfont;
831
832         /* flags */
833         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
834 }
835
836 /************************ move operator ************************/
837
838 static EnumPropertyItem move_type_items[]= {
839         {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
840         {LINE_END, "LINE_END", 0, "Line End", ""},
841         {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
842         {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
843         {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
844         {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
845         {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""},
846         {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""},
847         {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""},
848         {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""},
849         {0, NULL, 0, NULL, NULL}};
850
851 static int move_cursor(bContext *C, int type, int select)
852 {
853         Scene *scene= CTX_data_scene(C);
854         Object *obedit= CTX_data_edit_object(C);
855         Curve *cu= obedit->data;
856         EditFont *ef= cu->editfont;
857         int cursmove= -1;
858
859         switch(type) {
860                 case LINE_BEGIN:
861                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
862                         while(cu->pos>0) {
863                                 if(ef->textbuf[cu->pos-1]=='\n') break;
864                                 if(ef->textbufinfo[cu->pos-1].flag & CU_CHINFO_WRAP) break;                             
865                                 cu->pos--;
866                         }               
867                         cursmove=FO_CURS;
868                         break;
869                         
870                 case LINE_END:
871                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;                
872                         while(cu->pos<cu->len) {
873                                 if(ef->textbuf[cu->pos]==0) break;
874                                 if(ef->textbuf[cu->pos]=='\n') break;
875                                 if(ef->textbufinfo[cu->pos].flag & CU_CHINFO_WRAP ) break;
876                                 cu->pos++;
877                         }
878                         cursmove=FO_CURS;
879                         break;
880
881                 case PREV_WORD:
882                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
883                         cu->pos= prev_word(cu);
884                         cursmove= FO_CURS;
885                         break;
886
887                 case NEXT_WORD:
888                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
889                         cu->pos= next_word(cu);
890                         cursmove= FO_CURS;                              
891                         break;
892
893                 case PREV_CHAR:
894                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
895                         cu->pos--;
896                         cursmove=FO_CURS;
897                         break;
898
899                 case NEXT_CHAR: 
900                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
901                         cu->pos++;
902                         cursmove= FO_CURS;                              
903
904                         break;
905
906                 case PREV_LINE:
907                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
908                         cursmove=FO_CURSUP;
909                         break;
910                         
911                 case NEXT_LINE:
912                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
913                         cursmove= FO_CURSDOWN;
914                         break;
915
916                 case PREV_PAGE:
917                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
918                         cursmove=FO_PAGEUP;
919                         break;
920
921                 case NEXT_PAGE:
922                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
923                         cursmove=FO_PAGEDOWN;
924                         break;
925         }
926                 
927         if(cursmove == -1)
928                 return OPERATOR_CANCELLED;
929
930         if(select == 0) {
931                 if(cu->selstart) {
932                         struct Main *bmain= CTX_data_main(C);
933                         cu->selstart = cu->selend = 0;
934                         update_string(cu);
935                         BKE_text_to_curve(bmain, 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, FO_EDIT);
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, FO_EDIT);
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         const int ctrl= RNA_boolean_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, FO_EDIT);
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, FO_EDIT);
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;
1231         int a, 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
1242         for(a=0; a<len; a++)
1243                 insert_into_textbuf(obedit, inserted_text[a]);
1244
1245         MEM_freeN(inserted_text);
1246         MEM_freeN(inserted_utf8);
1247
1248         kill_selection(obedit, 1);
1249         text_update_edited(C, scene, obedit, 1, FO_EDIT);
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         if(RNA_property_is_set(op->ptr, "accent")) {
1270                 if(cu->len!=0 && cu->pos>0)
1271                         accentcode= 1;
1272                 return OPERATOR_FINISHED;
1273         }
1274         
1275         /* tab should exit editmode, but we allow it to be typed using modifier keys */
1276         if(event==TABKEY) {
1277                 if((alt||ctrl||shift) == 0)
1278                         return OPERATOR_PASS_THROUGH;
1279                 else
1280                         ascii= 9;
1281         }
1282         else if(event==BACKSPACEKEY)
1283                 ascii= 0;
1284
1285         if(val && ascii) {
1286                 /* handle case like TAB (== 9) */
1287                 if((ascii > 31 && ascii < 254 && ascii != 127) || (ascii==13) || (ascii==10) || (ascii==8)) {
1288                         if(accentcode) {
1289                                 if(cu->pos>0) {
1290                                         inserted_text[0]= findaccent(ef->textbuf[cu->pos-1], ascii);
1291                                         ef->textbuf[cu->pos-1]= inserted_text[0];
1292                                 }
1293                                 accentcode= 0;
1294                         }
1295                         else if(cu->len<MAXTEXT-1) {
1296                                 if(alt) {
1297                                         /* might become obsolete, apple has default values for this, other OS's too? */
1298                                         if(ascii=='t') ascii= 137;
1299                                         else if(ascii=='c') ascii= 169;
1300                                         else if(ascii=='f') ascii= 164;
1301                                         else if(ascii=='g') ascii= 176;
1302                                         else if(ascii=='l') ascii= 163;
1303                                         else if(ascii=='r') ascii= 174;
1304                                         else if(ascii=='s') ascii= 223;
1305                                         else if(ascii=='y') ascii= 165;
1306                                         else if(ascii=='.') ascii= 138;
1307                                         else if(ascii=='1') ascii= 185;
1308                                         else if(ascii=='2') ascii= 178;
1309                                         else if(ascii=='3') ascii= 179;
1310                                         else if(ascii=='%') ascii= 139;
1311                                         else if(ascii=='?') ascii= 191;
1312                                         else if(ascii=='!') ascii= 161;
1313                                         else if(ascii=='x') ascii= 215;
1314                                         else if(ascii=='>') ascii= 187;
1315                                         else if(ascii=='<') ascii= 171;
1316                                 }
1317
1318                                 inserted_text[0]= ascii;
1319                                 insert_into_textbuf(obedit, ascii);
1320                         }
1321                         
1322                         kill_selection(obedit, 1);
1323                         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1324                 }
1325                 else {
1326                         inserted_text[0]= ascii;
1327                         insert_into_textbuf(obedit, ascii);
1328                         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1329                 }
1330         }
1331         else if(val && event == BACKSPACEKEY) {
1332                 if(alt && cu->len!=0 && cu->pos>0)
1333                         accentcode= 1;
1334
1335                 return OPERATOR_PASS_THROUGH;
1336         }
1337         else
1338                 return OPERATOR_PASS_THROUGH;
1339
1340         if(inserted_text[0]) {
1341                 /* store as utf8 in RNA string */
1342                 char inserted_utf8[8] = {0};
1343
1344                 wcs2utf8s(inserted_utf8, inserted_text);
1345                 RNA_string_set(op->ptr, "text", inserted_utf8);
1346         }
1347
1348         /* reset property? */
1349         accentcode= 0;
1350         
1351         return OPERATOR_FINISHED;
1352 }
1353
1354 void FONT_OT_text_insert(wmOperatorType *ot)
1355 {
1356         /* identifiers */
1357         ot->name= "Insert Text";
1358         ot->description= "Insert text at cursor position";
1359         ot->idname= "FONT_OT_text_insert";
1360         
1361         /* api callbacks */
1362         ot->exec= insert_text_exec;
1363         ot->invoke= insert_text_invoke;
1364         ot->poll= ED_operator_editfont;
1365         
1366         /* flags */
1367         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1368
1369         /* properties */
1370         RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position");
1371         RNA_def_boolean(ot->srna, "accent", 0, "Accent mode", "Next typed character will strike through previous, for special character input");
1372 }
1373
1374
1375 /*********************** textbox add operator *************************/
1376 static int textbox_add_exec(bContext *C, wmOperator *UNUSED(op))
1377 {
1378         Object *obedit= CTX_data_active_object(C);
1379         Curve *cu= obedit->data;
1380         int i;
1381         
1382         if (cu->totbox < 256) {
1383                 for (i = cu->totbox; i>cu->actbox; i--) cu->tb[i]= cu->tb[i-1];
1384                 cu->tb[cu->actbox]= cu->tb[cu->actbox-1];
1385                 cu->actbox++;
1386                 cu->totbox++;
1387         }
1388         
1389         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
1390         return OPERATOR_FINISHED;
1391 }
1392
1393 void FONT_OT_textbox_add(wmOperatorType *ot)
1394 {
1395         /* identifiers */
1396         ot->name= "Add Textbox";
1397         ot->description= "Add a new text box";
1398         ot->idname= "FONT_OT_textbox_add";
1399         
1400         /* api callbacks */
1401         ot->exec= textbox_add_exec;
1402         ot->poll= ED_operator_object_active_editable_font;
1403
1404         /* flags */
1405         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1406         
1407         
1408 }
1409
1410
1411
1412 /*********************** textbox remove operator *************************/
1413
1414
1415
1416 static int textbox_remove_exec(bContext *C, wmOperator *op)
1417 {
1418         Object *obedit= CTX_data_active_object(C);
1419         Curve *cu= obedit->data;
1420         int i;
1421         int index = RNA_int_get(op->ptr, "index");
1422         
1423         
1424         if (cu->totbox > 1) {
1425                 for (i = index; i < cu->totbox; i++) cu->tb[i]= cu->tb[i+1];
1426                 cu->totbox--;
1427                 if (cu->actbox >= index)
1428                         cu->actbox--;
1429         }
1430         
1431         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
1432         
1433         return OPERATOR_FINISHED;
1434 }
1435
1436 void FONT_OT_textbox_remove(wmOperatorType *ot)
1437 {
1438         /* identifiers */
1439         ot->name= "Remove Textbox";
1440         ot->description= "Remove the textbox";
1441         ot->idname= "FONT_OT_textbox_remove";
1442         
1443         /* api callbacks */
1444         ot->exec= textbox_remove_exec;
1445         ot->poll= ED_operator_object_active_editable_font;
1446         
1447         /* flags */
1448         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1449         
1450         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "The current text box", 0, INT_MAX);
1451 }
1452
1453
1454
1455 /***************** editmode enter/exit ********************/
1456
1457 void make_editText(Object *obedit)
1458 {
1459         Curve *cu= obedit->data;
1460         EditFont *ef= cu->editfont;
1461         
1462         if(ef==NULL) {
1463                 ef= cu->editfont= MEM_callocN(sizeof(EditFont), "editfont");
1464         
1465                 ef->textbuf= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "texteditbuf");
1466                 ef->textbufinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "texteditbufinfo");
1467                 ef->copybuf= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "texteditcopybuf");
1468                 ef->copybufinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "texteditcopybufinfo");      
1469                 ef->oldstr= MEM_callocN((MAXTEXT+4)*sizeof(wchar_t), "oldstrbuf");
1470                 ef->oldstrinfo= MEM_callocN((MAXTEXT+4)*sizeof(CharInfo), "oldstrbuf");
1471         }
1472         
1473         // Convert the original text to wchar_t
1474         utf8towchar(ef->textbuf, cu->str);
1475         wcscpy(ef->oldstr, ef->textbuf);
1476                 
1477         cu->len= wcslen(ef->textbuf);
1478         
1479         memcpy(ef->textbufinfo, cu->strinfo, (cu->len)*sizeof(CharInfo));
1480         memcpy(ef->oldstrinfo, cu->strinfo, (cu->len)*sizeof(CharInfo));
1481
1482         if(cu->pos>cu->len) cu->pos= cu->len;
1483
1484         if(cu->pos)
1485                 cu->curinfo = ef->textbufinfo[cu->pos-1];
1486         else
1487                 cu->curinfo = ef->textbufinfo[0];
1488         
1489         // Convert to UTF-8
1490         update_string(cu);
1491 }
1492
1493 void load_editText(Object *obedit)
1494 {
1495         Curve *cu= obedit->data;
1496         EditFont *ef= cu->editfont;
1497         
1498         MEM_freeN(ef->oldstr);
1499         ef->oldstr= NULL;
1500         MEM_freeN(ef->oldstrinfo);
1501         ef->oldstrinfo= NULL;
1502         
1503         update_string(cu);
1504         
1505         if(cu->strinfo)
1506                 MEM_freeN(cu->strinfo);
1507         cu->strinfo= MEM_callocN((cu->len+4)*sizeof(CharInfo), "texteditinfo");
1508         memcpy(cu->strinfo, ef->textbufinfo, (cu->len)*sizeof(CharInfo));
1509
1510         cu->len= strlen(cu->str);
1511         
1512         /* this memory system is weak... */
1513         
1514         if(cu->selboxes) {
1515                 MEM_freeN(cu->selboxes);
1516                 cu->selboxes= NULL;
1517         }
1518 }
1519
1520 void free_editText(Object *obedit)
1521 {
1522         BKE_free_editfont((Curve *)obedit->data);
1523 }
1524
1525 /********************** set case operator *********************/
1526
1527 static EnumPropertyItem case_items[]= {
1528         {CASE_LOWER, "LOWER", 0, "Lower", ""},
1529         {CASE_UPPER, "UPPER", 0, "Upper", ""},
1530         {0, NULL, 0, NULL, NULL}};
1531
1532 static int set_case(bContext *C, int ccase)
1533 {
1534         Scene *scene= CTX_data_scene(C);
1535         Object *obedit= CTX_data_edit_object(C);
1536         Curve *cu= obedit->data;
1537         EditFont *ef= cu->editfont;
1538         wchar_t *str;
1539         int len;
1540         
1541         len= wcslen(ef->textbuf);
1542         str= ef->textbuf;
1543         while(len) {
1544                 if(*str>='a' && *str<='z')
1545                         *str-= 32;
1546                 len--;
1547                 str++;
1548         }
1549         
1550         if(ccase == CASE_LOWER) {
1551                 len= wcslen(ef->textbuf);
1552                 str= ef->textbuf;
1553                 while(len) {
1554                         if(*str>='A' && *str<='Z') {
1555                                 *str+= 32;
1556                         }
1557                         len--;
1558                         str++;
1559                 }
1560         }
1561
1562         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1563
1564         return OPERATOR_FINISHED;
1565 }
1566
1567 static int set_case_exec(bContext *C, wmOperator *op)
1568 {
1569         return set_case(C, RNA_enum_get(op->ptr, "case"));
1570 }
1571
1572 void FONT_OT_case_set(wmOperatorType *ot)
1573 {
1574         /* identifiers */
1575         ot->name= "Set Case";
1576         ot->description= "Set font case";
1577         ot->idname= "FONT_OT_case_set";
1578         
1579         /* api callbacks */
1580         ot->exec= set_case_exec;
1581         ot->poll= ED_operator_editfont;
1582
1583         /* flags */
1584         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1585
1586         /* properties */
1587         RNA_def_enum(ot->srna, "case", case_items, CASE_LOWER, "Case", "Lower or upper case");
1588 }
1589
1590 /********************** toggle case operator *********************/
1591
1592 static int toggle_case_exec(bContext *C, wmOperator *UNUSED(op))
1593 {
1594         Object *obedit= CTX_data_edit_object(C);
1595         Curve *cu= obedit->data;
1596         EditFont *ef= cu->editfont;
1597         wchar_t *str;
1598         int len, ccase= CASE_UPPER;
1599         
1600         len= wcslen(ef->textbuf);
1601         str= ef->textbuf;
1602         while(len) {
1603                 if(*str>='a' && *str<='z') {
1604                         ccase= CASE_LOWER;
1605                         break;
1606                 }
1607
1608                 len--;
1609                 str++;
1610         }
1611         
1612         return set_case(C, ccase);
1613 }
1614
1615 void FONT_OT_case_toggle(wmOperatorType *ot)
1616 {
1617         /* identifiers */
1618         ot->name= "Toggle Case";
1619         ot->description= "Toggle font case";
1620         ot->idname= "FONT_OT_case_toggle";
1621         
1622         /* api callbacks */
1623         ot->exec= toggle_case_exec;
1624         ot->poll= ED_operator_editfont;
1625
1626         /* flags */
1627         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1628 }
1629
1630 /* **************** Open Font ************** */
1631
1632 static void font_ui_template_init(bContext *C, wmOperator *op)
1633 {
1634         PropertyPointerRNA *pprop;
1635         
1636         op->customdata= pprop= MEM_callocN(sizeof(PropertyPointerRNA), "OpenPropertyPointerRNA");
1637         uiIDContextProperty(C, &pprop->ptr, &pprop->prop);
1638 }
1639
1640 static int open_cancel(bContext *UNUSED(C), wmOperator *op)
1641 {
1642         MEM_freeN(op->customdata);
1643         op->customdata= NULL;
1644         return OPERATOR_CANCELLED;
1645 }
1646
1647 static int open_exec(bContext *C, wmOperator *op)
1648 {
1649         struct Main *bmain= CTX_data_main(C);
1650         VFont *font;
1651         PropertyPointerRNA *pprop;
1652         PointerRNA idptr;
1653         char filepath[FILE_MAX];
1654         RNA_string_get(op->ptr, "filepath", filepath);
1655
1656         font= load_vfont(bmain, filepath);
1657
1658         if(!font) {
1659                 if(op->customdata) MEM_freeN(op->customdata);
1660                 return OPERATOR_CANCELLED;
1661         }
1662
1663         if(!op->customdata)
1664                 font_ui_template_init(C, op);
1665         
1666         /* hook into UI */
1667         pprop= op->customdata;
1668
1669         if(pprop->prop) {
1670                 /* when creating new ID blocks, use is already 1, but RNA
1671                  * pointer se also increases user, so this compensates it */
1672                 font->id.us--;
1673         
1674                 RNA_id_pointer_create(&font->id, &idptr);
1675                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
1676                 RNA_property_update(C, &pprop->ptr, pprop->prop);
1677         }
1678
1679         MEM_freeN(op->customdata);
1680
1681         return OPERATOR_FINISHED;
1682 }
1683
1684 static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1685 {
1686         VFont *font=NULL;
1687         char *path;
1688
1689         PointerRNA idptr;
1690         PropertyPointerRNA *pprop;
1691
1692         font_ui_template_init(C, op);
1693
1694         /* hook into UI */
1695         pprop= op->customdata;
1696
1697         if(pprop->prop) {
1698                 idptr= RNA_property_pointer_get((PointerRNA *)pprop, pprop->prop);
1699                 font= idptr.id.data;
1700         }
1701
1702         path = (font && strcmp(font->name, FO_BUILTIN_NAME) != 0)? font->name: U.fontdir;
1703         
1704         if(!RNA_property_is_set(op->ptr, "relative_path"))
1705                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1706                 
1707         if(RNA_property_is_set(op->ptr, "filepath"))
1708                 return open_exec(C, op);
1709
1710         RNA_string_set(op->ptr, "filepath", path);
1711         WM_event_add_fileselect(C, op); 
1712
1713         return OPERATOR_RUNNING_MODAL;
1714 }
1715
1716 void FONT_OT_open(wmOperatorType *ot)
1717 {
1718         /* identifiers */
1719         ot->name= "Open Font";
1720         ot->idname= "FONT_OT_open";
1721         
1722         /* api callbacks */
1723         ot->exec= open_exec;
1724         ot->invoke= open_invoke;
1725         ot->cancel= open_cancel;
1726         
1727         /* flags */
1728         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1729         
1730         /* properties */
1731         WM_operator_properties_filesel(ot, FOLDERFILE|FTFONTFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1732 }
1733
1734 /******************* delete operator *********************/
1735
1736 static int font_unlink_exec(bContext *C, wmOperator *op)
1737 {
1738         VFont *builtin_font;
1739
1740         PointerRNA idptr;
1741         PropertyPointerRNA pprop;
1742
1743         uiIDContextProperty(C, &pprop.ptr, &pprop.prop);
1744         
1745         if(pprop.prop==NULL) {
1746                 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running font unlink");
1747                 return OPERATOR_CANCELLED;
1748         }
1749
1750         builtin_font = get_builtin_font();
1751
1752         RNA_id_pointer_create(&builtin_font->id, &idptr);
1753         RNA_property_pointer_set(&pprop.ptr, pprop.prop, idptr);
1754         RNA_property_update(C, &pprop.ptr, pprop.prop);
1755
1756         return OPERATOR_FINISHED;
1757 }
1758
1759 void FONT_OT_unlink(wmOperatorType *ot)
1760 {
1761         /* identifiers */
1762         ot->name= "Unlink";
1763         ot->idname= "FONT_OT_unlink";
1764         ot->description= "Unlink active font data block";
1765         
1766         /* api callbacks */
1767         ot->exec= font_unlink_exec;
1768 }
1769
1770
1771 /* **************** undo for font object ************** */
1772
1773 static void undoFont_to_editFont(void *strv, void *ecu)
1774 {
1775         Curve *cu= (Curve *)ecu;
1776         EditFont *ef= cu->editfont;
1777         char *str= strv;
1778
1779         cu->pos= *((short *)str);
1780         cu->len= *((short *)(str+2));
1781
1782         memcpy(ef->textbuf, str+4, (cu->len+1)*sizeof(wchar_t));
1783         memcpy(ef->textbufinfo, str+4 + (cu->len+1)*sizeof(wchar_t), cu->len*sizeof(CharInfo));
1784         
1785         cu->selstart = cu->selend = 0;
1786         
1787         update_string(cu);
1788 }
1789
1790 static void *editFont_to_undoFont(void *ecu)
1791 {
1792         Curve *cu= (Curve *)ecu;
1793         EditFont *ef= cu->editfont;
1794         char *str;
1795         
1796         // The undo buffer includes [MAXTEXT+6]=actual string and [MAXTEXT+4]*sizeof(CharInfo)=charinfo
1797         str= MEM_callocN((MAXTEXT+6)*sizeof(wchar_t) + (MAXTEXT+4)*sizeof(CharInfo), "string undo");
1798
1799         // Copy the string and string information
1800         memcpy(str+4, ef->textbuf, (cu->len+1)*sizeof(wchar_t));
1801         memcpy(str+4 + (cu->len+1)*sizeof(wchar_t), ef->textbufinfo, cu->len*sizeof(CharInfo));
1802
1803         *((short *)str)= cu->pos;
1804         *((short *)(str+2))= cu->len;   
1805         
1806         return str;
1807 }
1808
1809 static void free_undoFont(void *strv)
1810 {
1811         MEM_freeN(strv);
1812 }
1813
1814 static void *get_undoFont(bContext *C)
1815 {
1816         Object *obedit= CTX_data_edit_object(C);
1817         if(obedit && obedit->type==OB_FONT) {
1818                 return obedit->data;
1819         }
1820         return NULL;
1821 }
1822
1823 /* and this is all the undo system needs to know */
1824 void undo_push_font(bContext *C, const char *name)
1825 {
1826         undo_editmode_push(C, name, get_undoFont, free_undoFont, undoFont_to_editFont, editFont_to_undoFont, NULL);
1827 }