=bmesh= merge from trunk at r36529
[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         Curve *cu= obedit->data;
263         EditFont *ef= cu->editfont;
264         cu->curinfo = ef->textbufinfo[cu->pos?cu->pos-1:0];
265         
266         if(obedit->totcol>0)
267                 obedit->actcol= ef->textbufinfo[cu->pos?cu->pos-1:0].mat_nr;
268
269         if(mode == FO_EDIT)
270                 update_string(cu);
271
272         BKE_text_to_curve(scene, obedit, mode);
273
274         if(recalc)
275                 DAG_id_tag_update(obedit->data, 0);
276         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
277 }
278
279 /********************** insert lorem operator *********************/
280
281 static int insert_lorem_exec(bContext *C, wmOperator *UNUSED(op))
282 {
283         Object *obedit= CTX_data_edit_object(C);
284         const char *p, *p2;
285         int i;
286         static const char *lastlorem;
287         
288         if(lastlorem)
289                 p= lastlorem;
290         else
291                 p= ED_lorem;
292         
293         i= rand()/(RAND_MAX/6)+4;       
294                 
295         for(p2=p; *p2 && i; p2++) {
296                 insert_into_textbuf(obedit, *p2);
297
298                 if(*p2=='.')
299                         i--;
300         }
301
302         lastlorem = p2+1;
303         if(strlen(lastlorem)<5)
304                 lastlorem = ED_lorem;
305         
306         insert_into_textbuf(obedit, '\n');
307         insert_into_textbuf(obedit, '\n');      
308
309         DAG_id_tag_update(obedit->data, 0);
310         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
311
312         return OPERATOR_FINISHED;
313 }
314
315 void FONT_OT_insert_lorem(wmOperatorType *ot)
316 {
317         /* identifiers */
318         ot->name= "Insert Lorem";
319         ot->description= "Insert placeholder text";
320         ot->idname= "FONT_OT_insert_lorem";
321         
322         /* api callbacks */
323         ot->exec= insert_lorem_exec;
324         ot->poll= ED_operator_editfont;
325         
326         /* flags */
327         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
328 }
329
330 /******************* paste file operator ********************/
331
332 /* note this handles both ascii and utf8 unicode, previously
333  * there were 3 functions that did effectively the same thing. */
334
335 static int paste_file(bContext *C, ReportList *reports, const char *filename)
336 {
337         Scene *scene= CTX_data_scene(C);
338         Object *obedit= CTX_data_edit_object(C);
339         Curve *cu= obedit->data;
340         EditFont *ef= cu->editfont;
341         FILE *fp;
342         int filelen;
343         char *strp;
344
345         fp= fopen(filename, "r");
346
347         if(!fp) {
348                 if(reports)
349                         BKE_reportf(reports, RPT_ERROR, "Failed to open file %s.", filename);
350                 return OPERATOR_CANCELLED;
351         }
352
353         fseek(fp, 0L, SEEK_END);
354         filelen = ftell(fp);
355         fseek(fp, 0L, SEEK_SET);
356
357         strp= MEM_callocN(filelen+4, "tempstr");
358
359         // fread() instead of read(), because windows read() converts text
360         // to DOS \r\n linebreaks, causing double linebreaks in the 3d text
361         filelen = fread(strp, 1, filelen, fp);
362         fclose(fp);
363         strp[filelen]= 0;
364
365         if(cu->len+filelen<MAXTEXT) {
366                 int tmplen;
367                 wchar_t *mem = MEM_callocN((sizeof(wchar_t)*filelen)+(4*sizeof(wchar_t)), "temporary");
368                 tmplen = utf8towchar(mem, strp);
369                 wcscat(ef->textbuf, mem);
370                 MEM_freeN(mem);
371                 cu->len += tmplen;
372                 cu->pos= cu->len;
373         }
374         MEM_freeN(strp);
375
376         text_update_edited(C, scene, obedit, 1, FO_EDIT);
377
378         return OPERATOR_FINISHED;
379 }
380
381 static int paste_file_exec(bContext *C, wmOperator *op)
382 {
383         char *path;
384         int retval;
385         
386         path= RNA_string_get_alloc(op->ptr, "filepath", NULL, 0);
387         retval= paste_file(C, op->reports, path);
388         MEM_freeN(path);
389
390         return retval;
391 }
392
393 static int paste_file_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
394 {
395         if(RNA_property_is_set(op->ptr, "filepath"))
396                 return paste_file_exec(C, op);
397
398         WM_event_add_fileselect(C, op); 
399
400         return OPERATOR_RUNNING_MODAL;
401 }
402
403 void FONT_OT_file_paste(wmOperatorType *ot)
404 {
405         /* identifiers */
406         ot->name= "Paste File";
407         ot->description= "Paste contents from file";
408         ot->idname= "FONT_OT_file_paste";
409         
410         /* api callbacks */
411         ot->exec= paste_file_exec;
412         ot->invoke= paste_file_invoke;
413         ot->poll= ED_operator_editfont;
414         
415         /* flags */
416         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
417
418         /* properties */
419         WM_operator_properties_filesel(ot, FOLDERFILE|TEXTFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH);
420 }
421
422 /******************* paste buffer operator ********************/
423
424 static int paste_buffer_exec(bContext *C, wmOperator *UNUSED(op))
425 {
426         const char *filename;
427
428 #ifdef WIN32
429         filename= "C:\\windows\\temp\\cutbuf.txt";
430
431 //      The following is more likely to work on all Win32 installations.
432 //      suggested by Douglas Toltzman. Needs windows include files...
433 /*
434         char tempFileName[MAX_PATH];
435         DWORD pathlen;
436         static const char cutbufname[]="cutbuf.txt";
437
438         if((pathlen=GetTempPath(sizeof(tempFileName),tempFileName)) > 0 &&
439                 pathlen + sizeof(cutbufname) <= sizeof(tempFileName))
440         {
441                 strcat(tempFileName,cutbufname);
442                 filename= tempFilename;
443         }
444 */
445 #else
446         filename= "/tmp/.cutbuffer";
447 #endif
448
449         return paste_file(C, NULL, filename);
450 }
451
452 void FONT_OT_buffer_paste(wmOperatorType *ot)
453 {
454         /* identifiers */
455         ot->name= "Paste Buffer";
456         ot->description= "Paste text from OS buffer";
457         ot->idname= "FONT_OT_buffer_paste";
458         
459         /* api callbacks */
460         ot->exec= paste_buffer_exec;
461         ot->poll= ED_operator_editfont;
462         
463         /* flags */
464         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
465 }
466
467 /******************* text to object operator ********************/
468
469 static void txt_add_object(bContext *C, TextLine *firstline, int totline, float offset[3])
470 {
471         Scene *scene= CTX_data_scene(C);
472         Curve *cu;
473         Object *obedit;
474         Base *base;
475         struct TextLine *tmp;
476         int nchars = 0, a;
477         float rot[3] = {0.f, 0.f, 0.f};
478         
479         obedit= add_object(scene, OB_FONT);
480         base= scene->basact;
481
482         
483         ED_object_base_init_transform(C, base, NULL, rot); /* seems to assume view align ? TODO - look into this, could be an operator option */
484         where_is_object(scene, obedit);
485
486         obedit->loc[0] += offset[0];
487         obedit->loc[1] += offset[1];
488         obedit->loc[2] += offset[2];
489
490         cu= obedit->data;
491         cu->vfont= get_builtin_font();
492         cu->vfont->id.us++;
493
494         for(tmp=firstline, a=0; cu->len<MAXTEXT && a<totline; tmp=tmp->next, a++)
495                 nchars += strlen(tmp->line) + 1;
496
497         if(cu->str) MEM_freeN(cu->str);
498         if(cu->strinfo) MEM_freeN(cu->strinfo); 
499
500         cu->str= MEM_callocN(nchars+4, "str");
501         cu->strinfo= MEM_callocN((nchars+4)*sizeof(CharInfo), "strinfo");
502
503         cu->str[0]= '\0';
504         cu->len= 0;
505         cu->pos= 0;
506         
507         for(tmp=firstline, a=0; cu->len<MAXTEXT && a<totline; tmp=tmp->next, a++) {
508                 strcat(cu->str, tmp->line);
509                 cu->len+= strlen(tmp->line);
510
511                 if(tmp->next) {
512                         strcat(cu->str, "\n");
513                         cu->len++;
514                 }
515
516                 cu->pos= cu->len;
517         }
518
519         WM_event_add_notifier(C, NC_OBJECT|NA_ADDED, obedit);
520 }
521
522 void ED_text_to_object(bContext *C, Text *text, int split_lines)
523 {
524         RegionView3D *rv3d= CTX_wm_region_view3d(C);
525         TextLine *line;
526         float offset[3];
527         int linenum= 0;
528
529         if(!text || !text->lines.first) return;
530
531         if(split_lines) {
532                 for(line=text->lines.first; line; line=line->next) {
533                         /* skip lines with no text, but still make space for them */
534                         if(line->line[0] == '\0') {
535                                 linenum++;
536                                 continue;
537                         }
538         
539                         /* do the translation */
540                         offset[0] = 0;
541                         offset[1] = -linenum;
542                         offset[2] = 0;
543         
544                         if(rv3d)
545                                 mul_mat3_m4_v3(rv3d->viewinv, offset);
546
547                         txt_add_object(C, line, 1, offset);
548
549                         linenum++;
550                 }
551         }
552         else {
553                 offset[0]= 0.0f;
554                 offset[1]= 0.0f;
555                 offset[2]= 0.0f;
556
557                 txt_add_object(C, text->lines.first, BLI_countlist(&text->lines), offset);
558         }
559 }
560
561 /********************** utilities ***************************/
562
563 static short next_word(Curve *cu)
564 {
565         short s;
566         for(s=cu->pos; (cu->str[s]) && (cu->str[s]!=' ') && (cu->str[s]!='\n') &&
567                                         (cu->str[s]!=1) && (cu->str[s]!='\r'); s++);
568         if(cu->str[s]) return(s+1); else return(s);
569 }
570
571 static short prev_word(Curve *cu)
572 {
573         short s;
574         
575         if(cu->pos==0) return(0);
576         for(s=cu->pos-2; (cu->str[s]) && (cu->str[s]!=' ') && (cu->str[s]!='\n') &&
577                                         (cu->str[s]!=1) && (cu->str[s]!='\r'); s--);
578         if(cu->str[s]) return(s+1); else return(s);
579 }
580
581 static int kill_selection(Object *obedit, int ins)      /* 1 == new character */
582 {
583         Curve *cu= obedit->data;
584         EditFont *ef= cu->editfont;
585         int selend, selstart, direction;
586         int offset = 0;
587         int getfrom;
588
589         direction = BKE_font_getselection(obedit, &selstart, &selend);
590         if(direction) {
591                 int size;
592                 if(ins) offset = 1;
593                 if(cu->pos >= selstart) cu->pos = selstart+offset;
594                 if((direction == -1) && ins) {
595                         selstart++;
596                         selend++;
597                 }
598                 getfrom = selend+offset;
599                 if(ins==0) getfrom++;
600                 size = (cu->len * sizeof(wchar_t)) - (selstart * sizeof(wchar_t)) + (offset*sizeof(wchar_t));
601                 memmove(ef->textbuf+selstart, ef->textbuf+getfrom, size);
602                 memmove(ef->textbufinfo+selstart, ef->textbufinfo+getfrom, ((cu->len-selstart)+offset)*sizeof(CharInfo));
603                 cu->len -= (selend-selstart)+offset;
604                 cu->selstart = cu->selend = 0;
605         }
606
607         return(direction);
608 }
609
610 /******************* set style operator ********************/
611
612 static EnumPropertyItem style_items[]= {
613         {CU_CHINFO_BOLD, "BOLD", 0, "Bold", ""},
614         {CU_CHINFO_ITALIC, "ITALIC", 0, "Italic", ""},
615         {CU_CHINFO_UNDERLINE, "UNDERLINE", 0, "Underline", ""},
616         {CU_CHINFO_SMALLCAPS, "SMALL_CAPS", 0, "Small Caps", ""},
617         {0, NULL, 0, NULL, NULL}};
618
619 static int set_style(bContext *C, const int style, const 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_tag_update(obedit->data, 0);
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         const int style= RNA_enum_get(op->ptr, "style");
645         const int clear= RNA_boolean_get(op->ptr, "clear");
646
647         return set_style(C, style, clear);
648 }
649
650 void FONT_OT_style_set(wmOperatorType *ot)
651 {
652         /* identifiers */
653         ot->name= "Set Style";
654         ot->description= "Set font style";
655         ot->idname= "FONT_OT_style_set";
656         
657         /* api callbacks */
658         ot->exec= set_style_exec;
659         ot->poll= ED_operator_editfont;
660         
661         /* flags */
662         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
663
664         /* properties */
665         RNA_def_enum(ot->srna, "style", style_items, CU_CHINFO_BOLD, "Style", "Style to set selection to.");
666         RNA_def_boolean(ot->srna, "clear", 0, "Clear", "Clear style rather than setting it.");
667 }
668
669 /******************* toggle style operator ********************/
670
671 static int toggle_style_exec(bContext *C, wmOperator *op)
672 {
673         Object *obedit= CTX_data_edit_object(C);
674         Curve *cu= obedit->data;
675         int style, clear, selstart, selend;
676
677         if(!BKE_font_getselection(obedit, &selstart, &selend))
678                 return OPERATOR_CANCELLED;
679         
680         style= RNA_enum_get(op->ptr, "style");
681
682         cu->curinfo.flag ^= style;
683         clear= (cu->curinfo.flag & style) == 0;
684
685         return set_style(C, style, clear);
686 }
687
688 void FONT_OT_style_toggle(wmOperatorType *ot)
689 {
690         /* identifiers */
691         ot->name= "Toggle Style";
692         ot->description= "Toggle font style";
693         ot->idname= "FONT_OT_style_toggle";
694         
695         /* api callbacks */
696         ot->exec= toggle_style_exec;
697         ot->poll= ED_operator_editfont;
698         
699         /* flags */
700         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
701
702         /* properties */
703         RNA_def_enum(ot->srna, "style", style_items, CU_CHINFO_BOLD, "Style", "Style to set selection to.");
704 }
705
706 /******************* copy text operator ********************/
707
708 static void copy_selection(Object *obedit)
709 {
710         int selstart, selend;
711         
712         if(BKE_font_getselection(obedit, &selstart, &selend)) {
713                 Curve *cu= obedit->data;
714                 EditFont *ef= cu->editfont;
715                 
716                 memcpy(ef->copybuf, ef->textbuf+selstart, ((selend-selstart)+1)*sizeof(wchar_t));
717                 ef->copybuf[(selend-selstart)+1]=0;
718                 memcpy(ef->copybufinfo, ef->textbufinfo+selstart, ((selend-selstart)+1)*sizeof(CharInfo));      
719         }
720 }
721
722 static int copy_text_exec(bContext *C, wmOperator *UNUSED(op))
723 {
724         Object *obedit= CTX_data_edit_object(C);
725
726         copy_selection(obedit);
727
728         return OPERATOR_FINISHED;
729 }
730
731 void FONT_OT_text_copy(wmOperatorType *ot)
732 {
733         /* identifiers */
734         ot->name= "Copy Text";
735         ot->description= "Copy selected text to clipboard";
736         ot->idname= "FONT_OT_text_copy";
737         
738         /* api callbacks */
739         ot->exec= copy_text_exec;
740         ot->poll= ED_operator_editfont;
741 }
742
743 /******************* cut text operator ********************/
744
745 static int cut_text_exec(bContext *C, wmOperator *UNUSED(op))
746 {
747         Scene *scene= CTX_data_scene(C);
748         Object *obedit= CTX_data_edit_object(C);
749         int selstart, selend;
750
751         if(!BKE_font_getselection(obedit, &selstart, &selend))
752                 return OPERATOR_CANCELLED;
753
754         copy_selection(obedit);
755         kill_selection(obedit, 0);
756
757         text_update_edited(C, scene, obedit, 1, FO_EDIT);
758
759         return OPERATOR_FINISHED;
760 }
761
762 void FONT_OT_text_cut(wmOperatorType *ot)
763 {
764         /* identifiers */
765         ot->name= "Cut Text";
766         ot->description= "Cut selected text to clipboard";
767         ot->idname= "FONT_OT_text_cut";
768         
769         /* api callbacks */
770         ot->exec= cut_text_exec;
771         ot->poll= ED_operator_editfont;
772
773         /* flags */
774         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
775 }
776
777 /******************* paste text operator ********************/
778
779 static int paste_selection(Object *obedit, ReportList *reports)
780 {
781         Curve *cu= obedit->data;
782         EditFont *ef= cu->editfont;
783         int len= wcslen(ef->copybuf);
784
785         // Verify that the copy buffer => [copy buffer len] + cu->len < MAXTEXT
786         if(cu->len + len <= MAXTEXT) {
787                 if(len) {       
788                         int size = (cu->len * sizeof(wchar_t)) - (cu->pos*sizeof(wchar_t)) + sizeof(wchar_t);
789                         memmove(ef->textbuf+cu->pos+len, ef->textbuf+cu->pos, size);
790                         memcpy(ef->textbuf+cu->pos, ef->copybuf, len * sizeof(wchar_t));
791                 
792                         memmove(ef->textbufinfo+cu->pos+len, ef->textbufinfo+cu->pos, (cu->len-cu->pos+1)*sizeof(CharInfo));
793                         memcpy(ef->textbufinfo+cu->pos, ef->copybufinfo, len*sizeof(CharInfo)); 
794                 
795                         cu->len += len;
796                         cu->pos += len;
797
798                         return 1;
799                 }
800         }
801         else
802                 BKE_report(reports, RPT_WARNING, "Text too long.");
803         
804         return 0;
805 }
806
807 static int paste_text_exec(bContext *C, wmOperator *op)
808 {
809         Scene *scene= CTX_data_scene(C);
810         Object *obedit= CTX_data_edit_object(C);
811
812         if(!paste_selection(obedit, op->reports))
813                 return OPERATOR_CANCELLED;
814
815         text_update_edited(C, scene, obedit, 1, FO_EDIT);
816
817         return OPERATOR_FINISHED;
818 }
819
820 void FONT_OT_text_paste(wmOperatorType *ot)
821 {
822         /* identifiers */
823         ot->name= "Paste Text";
824         ot->description= "Paste text from clipboard";
825         ot->idname= "FONT_OT_text_paste";
826         
827         /* api callbacks */
828         ot->exec= paste_text_exec;
829         ot->poll= ED_operator_editfont;
830
831         /* flags */
832         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
833 }
834
835 /************************ move operator ************************/
836
837 static EnumPropertyItem move_type_items[]= {
838         {LINE_BEGIN, "LINE_BEGIN", 0, "Line Begin", ""},
839         {LINE_END, "LINE_END", 0, "Line End", ""},
840         {PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
841         {NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
842         {PREV_WORD, "PREVIOUS_WORD", 0, "Previous Word", ""},
843         {NEXT_WORD, "NEXT_WORD", 0, "Next Word", ""},
844         {PREV_LINE, "PREVIOUS_LINE", 0, "Previous Line", ""},
845         {NEXT_LINE, "NEXT_LINE", 0, "Next Line", ""},
846         {PREV_PAGE, "PREVIOUS_PAGE", 0, "Previous Page", ""},
847         {NEXT_PAGE, "NEXT_PAGE", 0, "Next Page", ""},
848         {0, NULL, 0, NULL, NULL}};
849
850 static int move_cursor(bContext *C, int type, int select)
851 {
852         Scene *scene= CTX_data_scene(C);
853         Object *obedit= CTX_data_edit_object(C);
854         Curve *cu= obedit->data;
855         EditFont *ef= cu->editfont;
856         int cursmove= -1;
857
858         switch(type) {
859                 case LINE_BEGIN:
860                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
861                         while(cu->pos>0) {
862                                 if(ef->textbuf[cu->pos-1]=='\n') break;
863                                 if(ef->textbufinfo[cu->pos-1].flag & CU_CHINFO_WRAP) break;                             
864                                 cu->pos--;
865                         }               
866                         cursmove=FO_CURS;
867                         break;
868                         
869                 case LINE_END:
870                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;                
871                         while(cu->pos<cu->len) {
872                                 if(ef->textbuf[cu->pos]==0) break;
873                                 if(ef->textbuf[cu->pos]=='\n') break;
874                                 if(ef->textbufinfo[cu->pos].flag & CU_CHINFO_WRAP ) break;
875                                 cu->pos++;
876                         }
877                         cursmove=FO_CURS;
878                         break;
879
880                 case PREV_WORD:
881                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
882                         cu->pos= prev_word(cu);
883                         cursmove= FO_CURS;
884                         break;
885
886                 case NEXT_WORD:
887                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
888                         cu->pos= next_word(cu);
889                         cursmove= FO_CURS;                              
890                         break;
891
892                 case PREV_CHAR:
893                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
894                         cu->pos--;
895                         cursmove=FO_CURS;
896                         break;
897
898                 case NEXT_CHAR: 
899                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
900                         cu->pos++;
901                         cursmove= FO_CURS;                              
902
903                         break;
904
905                 case PREV_LINE:
906                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
907                         cursmove=FO_CURSUP;
908                         break;
909                         
910                 case NEXT_LINE:
911                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
912                         cursmove= FO_CURSDOWN;
913                         break;
914
915                 case PREV_PAGE:
916                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
917                         cursmove=FO_PAGEUP;
918                         break;
919
920                 case NEXT_PAGE:
921                         if((select) && (cu->selstart==0)) cu->selstart = cu->selend = cu->pos+1;
922                         cursmove=FO_PAGEDOWN;
923                         break;
924         }
925                 
926         if(cursmove == -1)
927                 return OPERATOR_CANCELLED;
928
929         if(select == 0) {
930                 if(cu->selstart) {
931                         cu->selstart = cu->selend = 0;
932                         update_string(cu);
933                         BKE_text_to_curve(scene, obedit, FO_SELCHANGE);
934                 }
935         }
936
937         if(cu->pos>cu->len) cu->pos= cu->len;
938         else if(cu->pos>=MAXTEXT) cu->pos= MAXTEXT;
939         else if(cu->pos<0) cu->pos= 0;
940
941         text_update_edited(C, scene, obedit, select, cursmove);
942
943         if(select)
944                 cu->selend = cu->pos;
945
946         return OPERATOR_FINISHED;
947 }
948
949 static int move_exec(bContext *C, wmOperator *op)
950 {
951         int type= RNA_enum_get(op->ptr, "type");
952
953         return move_cursor(C, type, 0);
954 }
955
956 void FONT_OT_move(wmOperatorType *ot)
957 {
958         /* identifiers */
959         ot->name= "Move Cursor";
960         ot->description= "Move cursor to position type";
961         ot->idname= "FONT_OT_move";
962         
963         /* api callbacks */
964         ot->exec= move_exec;
965         ot->poll= ED_operator_editfont;
966
967         /* flags */
968         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
969
970         /* properties */
971         RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to.");
972 }
973
974 /******************* move select operator ********************/
975
976 static int move_select_exec(bContext *C, wmOperator *op)
977 {
978         int type= RNA_enum_get(op->ptr, "type");
979
980         return move_cursor(C, type, 1);
981 }
982
983 void FONT_OT_move_select(wmOperatorType *ot)
984 {
985         /* identifiers */
986         ot->name= "Move Select";
987         ot->description= "Make selection from current cursor position to new cursor position type";
988         ot->idname= "FONT_OT_move_select";
989         
990         /* api callbacks */
991         ot->exec= move_select_exec;
992         ot->poll= ED_operator_editfont;
993
994         /* flags */
995         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
996
997         /* properties */
998         RNA_def_enum(ot->srna, "type", move_type_items, LINE_BEGIN, "Type", "Where to move cursor to, to make a selection.");
999 }
1000
1001 /************************* change spacing **********************/
1002
1003 static int change_spacing_exec(bContext *C, wmOperator *op)
1004 {
1005         Scene *scene= CTX_data_scene(C);
1006         Object *obedit= CTX_data_edit_object(C);
1007         Curve *cu= obedit->data;
1008         EditFont *ef= cu->editfont;
1009         int kern, delta= RNA_int_get(op->ptr, "delta");
1010
1011         kern = ef->textbufinfo[cu->pos-1].kern;
1012         kern += delta;
1013         CLAMP(kern, -20, 20);
1014
1015         if(ef->textbufinfo[cu->pos-1].kern == kern)
1016                 return OPERATOR_CANCELLED;
1017
1018         ef->textbufinfo[cu->pos-1].kern = kern;
1019
1020         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1021
1022         return OPERATOR_FINISHED;
1023 }
1024
1025 void FONT_OT_change_spacing(wmOperatorType *ot)
1026 {
1027         /* identifiers */
1028         ot->name= "Change Spacing";
1029         ot->description= "Change font spacing";
1030         ot->idname= "FONT_OT_change_spacing";
1031         
1032         /* api callbacks */
1033         ot->exec= change_spacing_exec;
1034         ot->poll= ED_operator_editfont;
1035
1036         /* flags */
1037         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1038
1039         /* properties */
1040         RNA_def_int(ot->srna, "delta", 1, -20, 20, "Delta", "Amount to decrease or increasing character spacing with.", -20, 20);
1041 }
1042
1043 /************************* change character **********************/
1044
1045 static int change_character_exec(bContext *C, wmOperator *op)
1046 {
1047         Scene *scene= CTX_data_scene(C);
1048         Object *obedit= CTX_data_edit_object(C);
1049         Curve *cu= obedit->data;
1050         EditFont *ef= cu->editfont;
1051         int character, delta= RNA_int_get(op->ptr, "delta");
1052
1053         if(cu->pos <= 0)
1054                 return OPERATOR_CANCELLED;
1055
1056         character= ef->textbuf[cu->pos - 1];
1057         character += delta;
1058         CLAMP(character, 0, 255);
1059
1060         if(character == ef->textbuf[cu->pos - 1])
1061                 return OPERATOR_CANCELLED;
1062
1063         ef->textbuf[cu->pos - 1]= character;
1064
1065         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1066
1067         return OPERATOR_FINISHED;
1068 }
1069
1070 void FONT_OT_change_character(wmOperatorType *ot)
1071 {
1072         /* identifiers */
1073         ot->name= "Change Character";
1074         ot->description= "Change font character code";
1075         ot->idname= "FONT_OT_change_character";
1076         
1077         /* api callbacks */
1078         ot->exec= change_character_exec;
1079         ot->poll= ED_operator_editfont;
1080
1081         /* flags */
1082         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1083
1084         /* properties */
1085         RNA_def_int(ot->srna, "delta", 1, -255, 255, "Delta", "Number to increase or decrease character code with.", -255, 255);
1086 }
1087
1088 /******************* line break operator ********************/
1089
1090 static int line_break_exec(bContext *C, wmOperator *op)
1091 {
1092         Scene *scene= CTX_data_scene(C);
1093         Object *obedit= CTX_data_edit_object(C);
1094         Curve *cu= obedit->data;
1095         EditFont *ef= cu->editfont;
1096         const int ctrl= RNA_boolean_get(op->ptr, "ctrl");
1097
1098         if(ctrl) {
1099                 insert_into_textbuf(obedit, 1);
1100                 if(ef->textbuf[cu->pos]!='\n')
1101                         insert_into_textbuf(obedit, '\n');
1102         }
1103         else
1104                 insert_into_textbuf(obedit, '\n');
1105
1106         cu->selstart = cu->selend = 0;
1107
1108         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1109
1110         return OPERATOR_FINISHED;
1111 }
1112
1113 void FONT_OT_line_break(wmOperatorType *ot)
1114 {
1115         /* identifiers */
1116         ot->name= "Line Break";
1117         ot->description= "Insert line break at cursor position";
1118         ot->idname= "FONT_OT_line_break";
1119         
1120         /* api callbacks */
1121         ot->exec= line_break_exec;
1122         ot->poll= ED_operator_editfont;
1123
1124         /* flags */
1125         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1126
1127         /* properties */
1128         RNA_def_boolean(ot->srna, "ctrl", 0, "Ctrl", ""); // XXX what is this?
1129 }
1130
1131 /******************* delete operator **********************/
1132
1133 static EnumPropertyItem delete_type_items[]= {
1134         {DEL_ALL, "ALL", 0, "All", ""},
1135         {DEL_NEXT_CHAR, "NEXT_CHARACTER", 0, "Next Character", ""},
1136         {DEL_PREV_CHAR, "PREVIOUS_CHARACTER", 0, "Previous Character", ""},
1137         {DEL_SELECTION, "SELECTION", 0, "Selection", ""},
1138         {DEL_NEXT_SEL, "NEXT_OR_SELECTION", 0, "Next or Selection", ""},
1139         {DEL_PREV_SEL, "PREVIOUS_OR_SELECTION", 0, "Previous or Selection", ""},
1140         {0, NULL, 0, NULL, NULL}};
1141
1142 static int delete_exec(bContext *C, wmOperator *op)
1143 {
1144         Scene *scene= CTX_data_scene(C);
1145         Object *obedit= CTX_data_edit_object(C);
1146         Curve *cu= obedit->data;
1147         EditFont *ef= cu->editfont;
1148         int x, selstart, selend, type= RNA_enum_get(op->ptr, "type");
1149
1150         if(cu->len == 0)
1151                 return OPERATOR_CANCELLED;
1152
1153         if(BKE_font_getselection(obedit, &selstart, &selend)) {
1154                 if(type == DEL_NEXT_SEL) type= DEL_SELECTION;
1155                 else if(type == DEL_PREV_SEL) type= DEL_SELECTION;
1156         }
1157         else {
1158                 if(type == DEL_NEXT_SEL) type= DEL_NEXT_CHAR;
1159                 else if(type == DEL_PREV_SEL) type= DEL_PREV_CHAR;
1160         }
1161
1162         switch(type) {
1163                 case DEL_ALL:
1164                         cu->len = cu->pos = 0;
1165                         ef->textbuf[0]= 0;
1166                         break;
1167                 case DEL_SELECTION:
1168                         if(!kill_selection(obedit, 0))
1169                                 return OPERATOR_CANCELLED;
1170                         break;
1171                 case DEL_PREV_CHAR:
1172                         if(cu->pos<=0)
1173                                 return OPERATOR_CANCELLED;
1174
1175                         for(x=cu->pos;x<=cu->len;x++)
1176                                 ef->textbuf[x-1]= ef->textbuf[x];
1177                         for(x=cu->pos;x<=cu->len;x++)
1178                                 ef->textbufinfo[x-1]= ef->textbufinfo[x];                                       
1179
1180                         cu->pos--;
1181                         ef->textbuf[--cu->len]='\0';
1182                         break;
1183                 case DEL_NEXT_CHAR:
1184                         if(cu->pos>=cu->len)
1185                                 return OPERATOR_CANCELLED;
1186
1187                         for(x=cu->pos;x<cu->len;x++)
1188                                 ef->textbuf[x]= ef->textbuf[x+1];
1189                         for(x=cu->pos;x<cu->len;x++)
1190                                 ef->textbufinfo[x]= ef->textbufinfo[x+1];                                       
1191
1192                         ef->textbuf[--cu->len]='\0';
1193                         break;
1194                 default:
1195                         return OPERATOR_CANCELLED;
1196         }
1197
1198         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1199
1200         return OPERATOR_FINISHED;
1201 }
1202
1203 void FONT_OT_delete(wmOperatorType *ot)
1204 {
1205         /* identifiers */
1206         ot->name= "Delete";
1207         ot->description= "Delete text by cursor position";
1208         ot->idname= "FONT_OT_delete";
1209         
1210         /* api callbacks */
1211         ot->exec= delete_exec;
1212         ot->poll= ED_operator_editfont;
1213
1214         /* flags */
1215         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1216
1217         /* properties */
1218         RNA_def_enum(ot->srna, "type", delete_type_items, DEL_ALL, "Type", "Which part of the text to delete.");
1219 }
1220
1221 /*********************** insert text operator *************************/
1222
1223 static int insert_text_exec(bContext *C, wmOperator *op)
1224 {
1225         Scene *scene= CTX_data_scene(C);
1226         Object *obedit= CTX_data_edit_object(C);
1227         char *inserted_utf8;
1228         wchar_t *inserted_text;
1229         int a, len;
1230
1231         if(!RNA_property_is_set(op->ptr, "text"))
1232                 return OPERATOR_CANCELLED;
1233         
1234         inserted_utf8= RNA_string_get_alloc(op->ptr, "text", NULL, 0);
1235         len= strlen(inserted_utf8);
1236
1237         inserted_text= MEM_callocN(sizeof(wchar_t)*(len+1), "FONT_insert_text");
1238         utf8towchar(inserted_text, inserted_utf8);
1239
1240         for(a=0; a<len; a++)
1241                 insert_into_textbuf(obedit, inserted_text[a]);
1242
1243         MEM_freeN(inserted_text);
1244         MEM_freeN(inserted_utf8);
1245
1246         kill_selection(obedit, 1);
1247         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1248
1249         return OPERATOR_FINISHED;
1250 }
1251
1252 static int insert_text_invoke(bContext *C, wmOperator *op, wmEvent *evt)
1253 {
1254         Scene *scene= CTX_data_scene(C);
1255         Object *obedit= CTX_data_edit_object(C);
1256         Curve *cu= obedit->data;
1257         EditFont *ef= cu->editfont;
1258         static int accentcode= 0;
1259         uintptr_t ascii = evt->ascii;
1260         int alt= evt->alt, shift= evt->shift, ctrl= evt->ctrl;
1261         int event= evt->type, val= evt->val;
1262         wchar_t inserted_text[2]= {0};
1263
1264         if(RNA_property_is_set(op->ptr, "text"))
1265                 return insert_text_exec(C, op);
1266
1267         if(RNA_property_is_set(op->ptr, "accent")) {
1268                 if(cu->len!=0 && cu->pos>0)
1269                         accentcode= 1;
1270                 return OPERATOR_FINISHED;
1271         }
1272         
1273         /* tab should exit editmode, but we allow it to be typed using modifier keys */
1274         if(event==TABKEY) {
1275                 if((alt||ctrl||shift) == 0)
1276                         return OPERATOR_PASS_THROUGH;
1277                 else
1278                         ascii= 9;
1279         }
1280         else if(event==BACKSPACEKEY)
1281                 ascii= 0;
1282
1283         if(val && ascii) {
1284                 /* handle case like TAB (== 9) */
1285                 if((ascii > 31 && ascii < 254 && ascii != 127) || (ascii==13) || (ascii==10) || (ascii==8)) {
1286                         if(accentcode) {
1287                                 if(cu->pos>0) {
1288                                         inserted_text[0]= findaccent(ef->textbuf[cu->pos-1], ascii);
1289                                         ef->textbuf[cu->pos-1]= inserted_text[0];
1290                                 }
1291                                 accentcode= 0;
1292                         }
1293                         else if(cu->len<MAXTEXT-1) {
1294                                 if(alt) {
1295                                         /* might become obsolete, apple has default values for this, other OS's too? */
1296                                         if(ascii=='t') ascii= 137;
1297                                         else if(ascii=='c') ascii= 169;
1298                                         else if(ascii=='f') ascii= 164;
1299                                         else if(ascii=='g') ascii= 176;
1300                                         else if(ascii=='l') ascii= 163;
1301                                         else if(ascii=='r') ascii= 174;
1302                                         else if(ascii=='s') ascii= 223;
1303                                         else if(ascii=='y') ascii= 165;
1304                                         else if(ascii=='.') ascii= 138;
1305                                         else if(ascii=='1') ascii= 185;
1306                                         else if(ascii=='2') ascii= 178;
1307                                         else if(ascii=='3') ascii= 179;
1308                                         else if(ascii=='%') ascii= 139;
1309                                         else if(ascii=='?') ascii= 191;
1310                                         else if(ascii=='!') ascii= 161;
1311                                         else if(ascii=='x') ascii= 215;
1312                                         else if(ascii=='>') ascii= 187;
1313                                         else if(ascii=='<') ascii= 171;
1314                                 }
1315
1316                                 inserted_text[0]= ascii;
1317                                 insert_into_textbuf(obedit, ascii);
1318                         }
1319                         
1320                         kill_selection(obedit, 1);
1321                         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1322                 }
1323                 else {
1324                         inserted_text[0]= ascii;
1325                         insert_into_textbuf(obedit, ascii);
1326                         text_update_edited(C, scene, obedit, 1, FO_EDIT);
1327                 }
1328         }
1329         else if(val && event == BACKSPACEKEY) {
1330                 if(alt && cu->len!=0 && cu->pos>0)
1331                         accentcode= 1;
1332
1333                 return OPERATOR_PASS_THROUGH;
1334         }
1335         else
1336                 return OPERATOR_PASS_THROUGH;
1337
1338         if(inserted_text[0]) {
1339                 /* store as utf8 in RNA string */
1340                 char inserted_utf8[8] = {0};
1341
1342                 wcs2utf8s(inserted_utf8, inserted_text);
1343                 RNA_string_set(op->ptr, "text", inserted_utf8);
1344         }
1345
1346         /* reset property? */
1347         accentcode= 0;
1348         
1349         return OPERATOR_FINISHED;
1350 }
1351
1352 void FONT_OT_text_insert(wmOperatorType *ot)
1353 {
1354         /* identifiers */
1355         ot->name= "Insert Text";
1356         ot->description= "Insert text at cursor position";
1357         ot->idname= "FONT_OT_text_insert";
1358         
1359         /* api callbacks */
1360         ot->exec= insert_text_exec;
1361         ot->invoke= insert_text_invoke;
1362         ot->poll= ED_operator_editfont;
1363         
1364         /* flags */
1365         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1366
1367         /* properties */
1368         RNA_def_string(ot->srna, "text", "", 0, "Text", "Text to insert at the cursor position.");
1369         RNA_def_boolean(ot->srna, "accent", 0, "Accent mode", "Next typed character will strike through previous, for special character input.");
1370 }
1371
1372
1373 /*********************** textbox add operator *************************/
1374 static int textbox_add_exec(bContext *C, wmOperator *UNUSED(op))
1375 {
1376         Object *obedit= CTX_data_active_object(C);
1377         Curve *cu= obedit->data;
1378         int i;
1379         
1380         if (cu->totbox < 256) {
1381                 for (i = cu->totbox; i>cu->actbox; i--) cu->tb[i]= cu->tb[i-1];
1382                 cu->tb[cu->actbox]= cu->tb[cu->actbox-1];
1383                 cu->actbox++;
1384                 cu->totbox++;
1385         }
1386         
1387         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
1388         return OPERATOR_FINISHED;
1389 }
1390
1391 void FONT_OT_textbox_add(wmOperatorType *ot)
1392 {
1393         /* identifiers */
1394         ot->name= "Add Textbox";
1395         ot->description= "Add a new text box";
1396         ot->idname= "FONT_OT_textbox_add";
1397         
1398         /* api callbacks */
1399         ot->exec= textbox_add_exec;
1400         ot->poll= ED_operator_object_active_editable_font;
1401
1402         /* flags */
1403         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1404         
1405         
1406 }
1407
1408
1409
1410 /*********************** textbox remove operator *************************/
1411
1412
1413
1414 static int textbox_remove_exec(bContext *C, wmOperator *op)
1415 {
1416         Object *obedit= CTX_data_active_object(C);
1417         Curve *cu= obedit->data;
1418         int i;
1419         int index = RNA_int_get(op->ptr, "index");
1420         
1421         
1422         if (cu->totbox > 1) {
1423                 for (i = index; i < cu->totbox; i++) cu->tb[i]= cu->tb[i+1];
1424                 cu->totbox--;
1425                 if (cu->actbox >= index)
1426                         cu->actbox--;
1427         }
1428         
1429         WM_event_add_notifier(C, NC_GEOM|ND_DATA, obedit->data);
1430         
1431         return OPERATOR_FINISHED;
1432 }
1433
1434 void FONT_OT_textbox_remove(wmOperatorType *ot)
1435 {
1436         /* identifiers */
1437         ot->name= "Remove Textbox";
1438         ot->description= "Remove the textbox";
1439         ot->idname= "FONT_OT_textbox_remove";
1440         
1441         /* api callbacks */
1442         ot->exec= textbox_remove_exec;
1443         ot->poll= ED_operator_object_active_editable_font;
1444         
1445         /* flags */
1446         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1447         
1448         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "The current text box.", 0, INT_MAX);
1449         
1450         
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         VFont *font;
1650         PropertyPointerRNA *pprop;
1651         PointerRNA idptr;
1652         char str[FILE_MAX];
1653         RNA_string_get(op->ptr, "filepath", str);
1654
1655         font = load_vfont(str);
1656
1657         if(!font) {
1658                 if(op->customdata) MEM_freeN(op->customdata);
1659                 return OPERATOR_CANCELLED;
1660         }
1661
1662         if(!op->customdata)
1663                 font_ui_template_init(C, op);
1664         
1665         /* hook into UI */
1666         pprop= op->customdata;
1667
1668         if(pprop->prop) {
1669                 /* when creating new ID blocks, use is already 1, but RNA
1670                  * pointer se also increases user, so this compensates it */
1671                 font->id.us--;
1672         
1673                 RNA_id_pointer_create(&font->id, &idptr);
1674                 RNA_property_pointer_set(&pprop->ptr, pprop->prop, idptr);
1675                 RNA_property_update(C, &pprop->ptr, pprop->prop);
1676         }
1677
1678         MEM_freeN(op->customdata);
1679
1680         return OPERATOR_FINISHED;
1681 }
1682
1683 static int open_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1684 {
1685         VFont *font=NULL;
1686         char *path;
1687
1688         PointerRNA idptr;
1689         PropertyPointerRNA *pprop;
1690
1691         font_ui_template_init(C, op);
1692
1693         /* hook into UI */
1694         pprop= op->customdata;
1695
1696         if(pprop->prop) {
1697                 idptr= RNA_property_pointer_get((PointerRNA *)pprop, pprop->prop);
1698                 font= idptr.id.data;
1699         }
1700
1701         path = (font && strcmp(font->name, FO_BUILTIN_NAME) != 0)? font->name: U.fontdir;
1702         
1703         if(!RNA_property_is_set(op->ptr, "relative_path"))
1704                 RNA_boolean_set(op->ptr, "relative_path", U.flag & USER_RELPATHS);
1705                 
1706         if(RNA_property_is_set(op->ptr, "filepath"))
1707                 return open_exec(C, op);
1708
1709         RNA_string_set(op->ptr, "filepath", path);
1710         WM_event_add_fileselect(C, op); 
1711
1712         return OPERATOR_RUNNING_MODAL;
1713 }
1714
1715 void FONT_OT_open(wmOperatorType *ot)
1716 {
1717         /* identifiers */
1718         ot->name= "Open Font";
1719         ot->idname= "FONT_OT_open";
1720         
1721         /* api callbacks */
1722         ot->exec= open_exec;
1723         ot->invoke= open_invoke;
1724         ot->cancel= open_cancel;
1725         
1726         /* flags */
1727         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1728         
1729         /* properties */
1730         WM_operator_properties_filesel(ot, FOLDERFILE|FTFONTFILE, FILE_SPECIAL, FILE_OPENFILE, WM_FILESEL_FILEPATH|WM_FILESEL_RELPATH);
1731 }
1732
1733 /******************* delete operator *********************/
1734
1735 static int font_unlink_exec(bContext *C, wmOperator *op)
1736 {
1737         VFont *builtin_font;
1738
1739         PointerRNA idptr;
1740         PropertyPointerRNA pprop;
1741
1742         uiIDContextProperty(C, &pprop.ptr, &pprop.prop);
1743         
1744         if(pprop.prop==NULL) {
1745                 BKE_report(op->reports, RPT_ERROR, "Incorrect context for running font unlink");
1746                 return OPERATOR_CANCELLED;
1747         }
1748
1749         builtin_font = get_builtin_font();
1750
1751         RNA_id_pointer_create(&builtin_font->id, &idptr);
1752         RNA_property_pointer_set(&pprop.ptr, pprop.prop, idptr);
1753         RNA_property_update(C, &pprop.ptr, pprop.prop);
1754
1755         return OPERATOR_FINISHED;
1756 }
1757
1758 void FONT_OT_unlink(wmOperatorType *ot)
1759 {
1760         /* identifiers */
1761         ot->name= "Unlink";
1762         ot->idname= "FONT_OT_unlink";
1763         ot->description= "Unlink active font data block";
1764         
1765         /* api callbacks */
1766         ot->exec= font_unlink_exec;
1767 }
1768
1769
1770 /* **************** undo for font object ************** */
1771
1772 static void undoFont_to_editFont(void *strv, void *ecu, void *UNUSED(obdata))
1773 {
1774         Curve *cu= (Curve *)ecu;
1775         EditFont *ef= cu->editfont;
1776         char *str= strv;
1777
1778         cu->pos= *((short *)str);
1779         cu->len= *((short *)(str+2));
1780
1781         memcpy(ef->textbuf, str+4, (cu->len+1)*sizeof(wchar_t));
1782         memcpy(ef->textbufinfo, str+4 + (cu->len+1)*sizeof(wchar_t), cu->len*sizeof(CharInfo));
1783         
1784         cu->selstart = cu->selend = 0;
1785         
1786         update_string(cu);
1787 }
1788
1789 static void *editFont_to_undoFont(void *ecu, void *UNUSED(obdata))
1790 {
1791         Curve *cu= (Curve *)ecu;
1792         EditFont *ef= cu->editfont;
1793         char *str;
1794         
1795         // The undo buffer includes [MAXTEXT+6]=actual string and [MAXTEXT+4]*sizeof(CharInfo)=charinfo
1796         str= MEM_callocN((MAXTEXT+6)*sizeof(wchar_t) + (MAXTEXT+4)*sizeof(CharInfo), "string undo");
1797
1798         // Copy the string and string information
1799         memcpy(str+4, ef->textbuf, (cu->len+1)*sizeof(wchar_t));
1800         memcpy(str+4 + (cu->len+1)*sizeof(wchar_t), ef->textbufinfo, cu->len*sizeof(CharInfo));
1801
1802         *((short *)str)= cu->pos;
1803         *((short *)(str+2))= cu->len;   
1804         
1805         return str;
1806 }
1807
1808 static void free_undoFont(void *strv)
1809 {
1810         MEM_freeN(strv);
1811 }
1812
1813 static void *get_undoFont(bContext *C)
1814 {
1815         Object *obedit= CTX_data_edit_object(C);
1816         if(obedit && obedit->type==OB_FONT) {
1817                 return obedit->data;
1818         }
1819         return NULL;
1820 }
1821
1822 /* and this is all the undo system needs to know */
1823 void undo_push_font(bContext *C, const char *name)
1824 {
1825         undo_editmode_push(C, name, get_undoFont, free_undoFont, undoFont_to_editFont, editFont_to_undoFont, NULL);
1826 }