summaryrefslogtreecommitdiff
path: root/lib/text.c
blob: 3d8684bf42db067b30651719305b541aff4785b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <math.h>

#include <gdk/gdkkeysyms.h>

#include "propinternals.h"
#include "text.h"
#include "diarenderer.h"
#include "diagramdata.h"
#include "objchange.h"
#include "textline.h"
#include "attributes.h"
#include "object.h"

static int text_key_event(Focus *focus, 
			  guint keystate, guint keysym,
			  const gchar *str, int strlen,
			  ObjectChange **change);

typedef enum {
  TYPE_DELETE_BACKWARD,
  TYPE_DELETE_FORWARD,
  TYPE_INSERT_CHAR,
  TYPE_JOIN_ROW,
  TYPE_SPLIT_ROW,
  TYPE_DELETE_ALL
} TextChangeType;

struct TextObjectChange {
  ObjectChange obj_change;

  Text *text;
  TextChangeType type;
  gunichar ch;
  int pos;
  int row;
  gchar *str;

  /* the owning object ... */
  DiaObject *obj;
  /* ... and it's size position properties, which might change
   * as a side-effect of changing the text
   */
  GPtrArray *props;
};

#define CURSOR_HEIGHT_RATIO 20

/* *** Encapsulation functions for transferring to text_line *** */
gchar *
text_get_line(const Text *text, int line)
{
  return text_line_get_string(text->lines[line]);
}

/** Raw sets one line to a given text, not copying, not freeing.
 */
static void
text_set_line(Text *text, int line_no, gchar *line)
{
  text_line_set_string(text->lines[line_no], line);
}

/** Set the text of a line, freeing, copying and mallocing as required.
 * Updates strlen and row_width entries, but not max_width.
 */
static void
text_set_line_text(Text *text, int line_no, gchar *line)
{
  text_set_line(text, line_no, line);
}

/** Delete the line, freeing appropriately and moving stuff up.
 * This function circumvents the normal free/alloc cycle of 
 * text_set_line_text. */
static void
text_delete_line(Text *text, int line_no)
{
  int i;

  g_free(text->lines[line_no]);
  for (i = line_no; i < text->numlines - 1; i++) {
    text->lines[i] = text->lines[i+1];
  }
  text->numlines -= 1;
  text->lines = g_realloc(text->lines, sizeof(TextLine *)*text->numlines);
}

/** Insert a new (empty) line at line_no.
 * This function circumvents the normal free/alloc cycle of 
 * text_set_line_text. */
static void
text_insert_line(Text *text, int line_no)
{
  int i;
  text->numlines += 1;
  text->lines = g_realloc(text->lines, sizeof(char *)*text->numlines);

  for (i = text->numlines - 1; i > line_no; i--) {
    text->lines[i] = text->lines[i - 1];
  }
  text->lines[line_no] = text_line_new("", text->font, text->height);;
}

/** Get the in-diagram width of the given line.
 * @param text The text object;
 * @param line_no The index of the line in the text object, starting at 0.
 * @returns The width in cm of the indicated line.
 */
real
text_get_line_width(const Text *text, int line_no)
{
  return text_line_get_width(text->lines[line_no]);
}

/** Get the number of characters of the given line.
 * @param text The text object;
 * @param line_no The index of the line in the text object, starting at 0.
 * @returns The number of UTF-8 characters of the indicated line.
 */
int
text_get_line_strlen(const Text *text, int line_no)
{
  return g_utf8_strlen(text_line_get_string(text->lines[line_no]), -1);
}

real
text_get_max_width(Text *text)
{
  return text->max_width;
}

/** Get the *average* ascent of this Text object.
 * @param a Text object
 * @returns the average of the ascents of each line (height above baseline)
 */
real
text_get_ascent(Text *text)
{
  return text->ascent;
}

/** Get the *average* descent of this Text object.
 * @param a Text object
 * @returns the average of the descents of each line (height below baseline)
 */
real
text_get_descent(Text *text)
{
  return text->descent;
}

static ObjectChange *text_create_change(Text *text, TextChangeType type,
					gunichar ch, int pos, int row,
					DiaObject *obj);

static void
calc_width(Text *text)
{
  real width;
  int i;

  width = 0.0;
  for (i = 0; i < text->numlines; i++) {
    width = MAX(width, text_get_line_width(text, i));
  }
  
  text->max_width = width;
}

static void
calc_ascent_descent(Text *text)
{
  real sig_a = 0.0,sig_d = 0.0;
  gint i;
    
  for ( i = 0; i < text->numlines; i++) {
    sig_a += text_line_get_ascent(text->lines[i]);
    sig_d += text_line_get_descent(text->lines[i]);
  }
  
  text->ascent = sig_a / (real)text->numlines;
  text->descent = sig_d / (real)text->numlines;
}

static void
free_string(Text *text)
{
  int i;

  for (i=0;i<text->numlines;i++) {
    text_line_destroy(text->lines[i]);
  }

  g_free(text->lines);
  text->lines = NULL;
}

static void
set_string(Text *text, const char *string)
{
  int numlines, i;
  const char *s,*s2;
  char *fallback = NULL;

  if (string && !g_utf8_validate (string, -1, NULL)) {
    GError *error = NULL;
    s = fallback = g_locale_to_utf8 (string, -1, NULL, NULL, &error);
    if (!fallback) {
      g_warning ("Invalid string data, neither UTF-8 nor locale: %s", error->message);
      string = NULL;
    }
  } else {
    s = string;
  }
  numlines = 1;
  if (s != NULL) 
    while ( (s = g_utf8_strchr(s, -1, '\n')) != NULL ) {
      numlines++;
      if (*s) {
	s = g_utf8_next_char(s);
      }
    }
  text->numlines = numlines;
  text->lines = g_new0(TextLine *, numlines);
  for (i = 0; i < numlines; i++) {
    text->lines[i] = text_line_new("", text->font, text->height);
  }

  s = fallback ? fallback : string;
  if (s == NULL) {
    text_set_line_text(text, 0, "");
    return;
  }

  for (i = 0; i < numlines; i++) {
    gchar *string_line;
    s2 = g_utf8_strchr(s, -1, '\n');
    if (s2 == NULL) { /* No newline */
      s2 = s + strlen(s);
    }
    string_line = g_strndup(s, s2 - s);
    text_set_line_text(text, i, string_line);
    g_free(string_line);
    s = s2;
    if (*s) {
      s = g_utf8_next_char(s);
    }
  }

  if (text->cursor_row >= text->numlines) {
    text->cursor_row = text->numlines - 1;
  }
  
  if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row)) {
    text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
  }
  g_free (fallback);
}

void
text_set_string(Text *text, const char *string)
{
  if (text->lines != NULL)
    free_string(text);
  
  set_string(text, string);
}

Text *
new_text(const char *string, DiaFont *font, real height,
	 Point *pos, Color *color, Alignment align)
{
  Text *text;

  text = g_new(Text, 1);

  text->font = dia_font_ref(font);
  text->height = height;

  text->position = *pos;
  text->color = *color;
  text->alignment = align;

  text->cursor_pos = 0;
  text->cursor_row = 0;
  
  text->focus.obj = NULL;
  text->focus.has_focus = FALSE;
  text->focus.key_event = text_key_event;
  text->focus.text = text;
  
  set_string(text, string);

  calc_ascent_descent(text);

  return text;
}

/*!
 * Fallback function returning a default initialized text object.
 */
Text *
new_text_default(Point *pos, Color *color, Alignment align)
{
  Text *text;
  DiaFont *font;
  real font_height;

  attributes_get_default_font(&font, &font_height);
  text = new_text("", font, font_height, pos, color, align); 
  dia_font_unref(font);
  return text;
}

Text *
text_copy(Text *text)
{
  Text *copy;
  int i;

  copy = g_new(Text, 1);
  copy->numlines = text->numlines;
  copy->lines = g_new(TextLine *, text->numlines);
  
  copy->font = dia_font_copy(text->font);
  copy->height = text->height;
  copy->position = text->position;
  copy->color = text->color;
  copy->alignment = text->alignment;

  for (i=0;i<text->numlines;i++) {
    TextLine *text_line = text->lines[i];
    copy->lines[i] = text_line_new(text_line_get_string(text_line),
				   text_line_get_font(text_line),
				   text_line_get_height(text_line));
  }

  copy->cursor_pos = 0;
  copy->cursor_row = 0;
  copy->focus.obj = NULL;
  copy->focus.has_focus = FALSE;
  copy->focus.key_event = text_key_event;
  copy->focus.text = copy;
  
  copy->ascent = text->ascent;
  copy->descent = text->descent;
  copy->max_width = text->max_width;
  
  return copy;
}

void
text_destroy(Text *text)
{
  free_string(text);
  dia_font_unref(text->font);
  g_free(text);
}

void
text_set_height(Text *text, real height)
{
  int i;
  text->height = height;
  for (i = 0; i < text->numlines; i++) {
    text_line_set_height(text->lines[i], height);
  }
  calc_width(text);
  calc_ascent_descent(text);
}

real
text_get_height(const Text *text)
{
  return text->height;
}

void
text_set_font(Text *text, DiaFont *font)
{
  DiaFont *old_font = text->font;
  int i;

  text->font = dia_font_ref(font);
  dia_font_unref(old_font);

  for (i = 0; i < text->numlines; i++) {
    text_line_set_font(text->lines[i], font);
  }
  
  calc_width(text);
  calc_ascent_descent(text);
}

void
text_set_position(Text *text, Point *pos)
{
  text->position = *pos;
}

void
text_set_color(Text *text, Color *col)
{
  text->color = *col;
}

void
text_set_alignment(Text *text, Alignment align)
{
  text->alignment = align;
}

void
text_calc_boundingbox(Text *text, Rectangle *box)
{
  calc_width(text);
  calc_ascent_descent(text);

  if (box == NULL) return; /* For those who just want the text info
			      updated */
  box->left = text->position.x;
  switch (text->alignment) {
  case ALIGN_LEFT:
    break;
  case ALIGN_CENTER:
    box->left -= text->max_width / 2.0;
    break;
  case ALIGN_RIGHT:
    box->left -= text->max_width;
    break;
  }

  box->right = box->left + text->max_width;
  
  box->top = text->position.y - text->ascent;
#if 0
  box->bottom = box->top + text->height*text->numlines + text->descent;
#else
  /* why should we add one descent? isn't ascent+descent~=height? */
  box->bottom = box->top + (text->ascent+text->descent+text->height*(text->numlines-1));
#endif
  if (text->focus.has_focus) {
    real height = text->ascent + text->descent;
    if (text->cursor_pos == 0) {
      /* Half the cursor width */
      box->left -= height/(CURSOR_HEIGHT_RATIO*2);
    } else {
      /* Half the cursor width. Assume that
	 if it isn't at position zero, it might be 
	 at the last position possible. */
      box->right += height/(CURSOR_HEIGHT_RATIO*2);
    }
   
    /* Account for the size of the cursor top and bottom */
    box->top -= height/(CURSOR_HEIGHT_RATIO*2);
    box->bottom += height/CURSOR_HEIGHT_RATIO;
  }
}

char *
text_get_string_copy(const Text *text)
{
  int num,i;
  char *str;
  
  num = 0;
  for (i=0;i<text->numlines;i++) {
    /* This is for allocation, so it should not use g_utf8_strlen() */
    num += strlen(text_get_line(text, i))+1; 
  }

  str = g_malloc(num);

  *str = 0;
  
  for (i=0;i<text->numlines;i++) {
    strcat(str, text_get_line(text, i));
    if (i != (text->numlines-1)) {
      strcat(str, "\n");
    }
  }
  
  return str;
}

real
text_distance_from(Text *text, Point *point)
{
  real dx, dy;
  real topy, bottomy;
  real left, right;
  int line;
  
  topy = text->position.y - text->ascent;
  bottomy = text->position.y + text->descent + text->height*(text->numlines-1);
  if (point->y <= topy) {
    dy = topy - point->y;
    line = 0;
  } else if (point->y >= bottomy) {
    dy = point->y - bottomy;
    line = text->numlines - 1;
  } else {
    dy = 0.0;
    line = (int) floor( (point->y - topy) / text->height );
    if (line >= text->numlines)
      line = text->numlines - 1;
  }

  left = text->position.x;
  switch (text->alignment) {
  case ALIGN_LEFT:
    break;
  case ALIGN_CENTER:
    left -= text_get_line_width(text, line) / 2.0;
    break;
  case ALIGN_RIGHT:
    left -= text_get_line_width(text, line);
    break;
  }
  right = left + text_get_line_width(text, line);

  if (point->x <= left) {
    dx = left - point->x;
  } else if (point->x >= right) {
    dx = point->x - right;
  } else {
    dx = 0.0;
  }

  return dx + dy;
}

void
text_draw(Text *text, DiaRenderer *renderer)
{
  DIA_RENDERER_GET_CLASS(renderer)->draw_text(renderer, text);

  if ((renderer->is_interactive) && (text->focus.has_focus)) {
    real curs_x, curs_y;
    real str_width_first;
    real str_width_whole;
    Point p1, p2;
    real height = text->ascent+text->descent;
    curs_y = text->position.y - text->ascent + text->cursor_row*text->height; 

    DIA_RENDERER_GET_CLASS(renderer)->set_font(renderer, text->font, text->height);

    str_width_first =
      DIA_RENDERER_GET_CLASS(renderer)->get_text_width(renderer,
						       text_get_line(text, text->cursor_row),
						       text->cursor_pos);
    str_width_whole =
      DIA_RENDERER_GET_CLASS(renderer)->get_text_width(renderer,
						       text_get_line(text, text->cursor_row),
						       text_get_line_strlen(text, text->cursor_row));
    curs_x = text->position.x + str_width_first;

    switch (text->alignment) {
    case ALIGN_LEFT:
      break;
    case ALIGN_CENTER:
      curs_x -= str_width_whole / 2.0;
      break;
    case ALIGN_RIGHT:
      curs_x -= str_width_whole;
      break;
    }

    p1.x = curs_x;
    p1.y = curs_y;
    p2.x = curs_x;
    p2.y = curs_y + height;
    
    DIA_RENDERER_GET_CLASS(renderer)->set_linestyle(renderer, LINESTYLE_SOLID, 0.0);
    DIA_RENDERER_GET_CLASS(renderer)->set_linewidth(renderer, height/CURSOR_HEIGHT_RATIO);
    DIA_RENDERER_GET_CLASS(renderer)->draw_line(renderer, &p1, &p2, &color_black);
  }
}

void
text_grab_focus(Text *text, DiaObject *object)
{
  text->focus.obj = object;
  request_focus(&text->focus);
}

void
text_set_cursor_at_end( Text* text )
{
  text->cursor_row = text->numlines - 1 ;
  text->cursor_pos = text_get_line_strlen(text, text->cursor_row) ;
}

typedef enum {
  WORD_START = 1,
  WORD_END
} CursorMovement;

static void
text_move_cursor(Text *text, CursorMovement mv)
{
  gchar *str = text_line_get_string(text->lines[text->cursor_row]);
  gchar *p = str;
  int curmax = text_get_line_strlen(text, text->cursor_row);
  if (text->cursor_pos > 0 && text->cursor_pos <= curmax) {
    int i;
    for (i = 0; i < text->cursor_pos; ++i)
      p = g_utf8_next_char (p);
  } 
  if (WORD_START == mv && text->cursor_pos < 1) {
    if (text->cursor_row) {
      text->cursor_row--;
      text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
    }
    return;
  } else if (WORD_END == mv && text->cursor_pos == curmax) {
    if (text->cursor_row < text->numlines - 1) {
      text->cursor_row++;
      text->cursor_pos = 0;
    }
    return;
  }
  while (!g_unichar_isalnum (g_utf8_get_char (p))) {
    p = (WORD_START == mv ? g_utf8_find_prev_char (str, p) : g_utf8_next_char (p));
    if (p) text->cursor_pos += (WORD_START == mv ? -1 : 1);
    if (!p || !*p)
      return;
    if (!text->cursor_pos || text->cursor_pos == curmax)
      return;
  }
  while (g_unichar_isalnum (g_utf8_get_char (p))) {
    p = (WORD_START == mv ? g_utf8_find_prev_char (str, p) : g_utf8_next_char (p));
    if (p) text->cursor_pos += (WORD_START == mv ? -1 : 1);
    if (!p || !*p)
      return;
    if (!text->cursor_pos || text->cursor_pos == curmax)
      return;
  }
}

/* The renderer is only used to determine where the click is, so is not
 * required when no point is given. */
void
text_set_cursor(Text *text, Point *clicked_point,
		DiaRenderer *renderer)
{
  real str_width_whole;
  real str_width_first;
  real top;
  real start_x;
  int row;
  int i;

  if (clicked_point != NULL) {
    top = text->position.y - text->ascent;
  
    row = (int)floor((clicked_point->y - top) / text->height);

    if (row < 0)
      row = 0;

    if (row >= text->numlines)
      row = text->numlines - 1;
    
    text->cursor_row = row;
    text->cursor_pos = 0;

    if (!renderer->is_interactive) {
      g_warning("Internal error: Select gives non interactive renderer!\n"
		"val: %d\n", renderer->is_interactive);
      return;
    }


    DIA_RENDERER_GET_CLASS(renderer)->set_font(renderer, text->font, text->height);
    str_width_whole =
      DIA_RENDERER_GET_CLASS(renderer)->get_text_width(renderer,
						       text_get_line(text, row),
						       text_get_line_strlen(text, row));
    start_x = text->position.x;
    switch (text->alignment) {
    case ALIGN_LEFT:
      break;
    case ALIGN_CENTER:
      start_x -= str_width_whole / 2.0;
      break;
    case ALIGN_RIGHT:
      start_x -= str_width_whole;
      break;
    }

    /* Do an ugly linear search for the cursor index:
       TODO: Change to binary search */
    {
      real min_dist = G_MAXDOUBLE;
      for (i=0;i<=text_get_line_strlen(text, row);i++) {
	real dist;
	str_width_first =
	  DIA_RENDERER_GET_CLASS(renderer)->get_text_width(renderer, text_get_line(text, row), i);
	dist = fabs(clicked_point->x - (start_x + str_width_first));
	if (dist < min_dist) {
	  min_dist = dist;
	  text->cursor_pos = i;
	} else {
	  return;
	}
      }
    }
    text->cursor_pos = text_get_line_strlen(text, row);
  } else {
    /* No clicked point, leave cursor where it is */
  }
}

static void
text_join_lines(Text *text, int first_line)
{
  gchar *combined_line;
  int len1;
  
  len1 = text_get_line_strlen(text, first_line);

  combined_line = g_strconcat(text_get_line(text, first_line), 
			      text_get_line(text, first_line + 1), NULL);
  text_delete_line(text, first_line);
  text_set_line_text(text, first_line, combined_line);
  g_free(combined_line);

  text->max_width = MAX(text->max_width, text_get_line_width(text, first_line));

  text->cursor_row = first_line;
  text->cursor_pos = len1;
}

static void
text_delete_forward(Text *text)
{
  int row;
  int i;
  real width;
  gchar *line;
  gchar *utf8_before, *utf8_after;
  gchar *str1, *str;
  
  row = text->cursor_row;
  
  if (text->cursor_pos >= text_get_line_strlen(text, row)) {
    if (row + 1 < text->numlines)
      text_join_lines(text, row);
    return;
  }

  line = text_get_line(text, row);
  utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos));
  utf8_after = g_utf8_offset_to_pointer(utf8_before, 1);
  str1 = g_strndup(line, utf8_before - line);
  str = g_strconcat(str1, utf8_after, NULL);
  text_set_line_text(text, row, str);
  g_free(str1);
  g_free(str);

  if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
    text->cursor_pos = text_get_line_strlen(text, text->cursor_row);

  width = 0.0;
  for (i = 0; i < text->numlines; i++) {
    width = MAX(width, text_get_line_width(text, i));
  }
  text->max_width = width;
}

static void
text_delete_backward(Text *text)
{
  int row;
  int i;
  real width;
  gchar *line;
  gchar *utf8_before, *utf8_after;
  gchar *str1, *str;
  
  row = text->cursor_row;
  
  if (text->cursor_pos <= 0) {
    if (row > 0)
      text_join_lines(text, row-1);
    return;
  }

  line = text_get_line(text, row);
  utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos - 1));
  utf8_after = g_utf8_offset_to_pointer(utf8_before, 1);
  str1 = g_strndup(line, utf8_before - line);
  str = g_strconcat(str1, utf8_after, NULL);
  text_set_line_text(text, row, str);
  g_free(str);
  g_free(str1);

  text->cursor_pos --;
  if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
    text->cursor_pos = text_get_line_strlen(text, text->cursor_row);

  width = 0.0;
  for (i = 0; i < text->numlines; i++) {
    width = MAX(width, text_get_line_width(text, i));
  }
  text->max_width = width;
}

static void
text_split_line(Text *text)
{
  int i;
  char *line;
  real width;
  gchar *utf8_before;
  gchar *str1, *str2;
  
  /* Split the lines at cursor_pos */
  line = text_get_line(text, text->cursor_row);
  text_insert_line(text, text->cursor_row);
  utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos));
  str1 = g_strndup(line, utf8_before - line);
  str2 = g_strdup(utf8_before); /* Must copy before dealloc */
  text_set_line_text(text, text->cursor_row, str1);
  text_set_line_text(text, text->cursor_row + 1, str2);
  g_free(str2);
  g_free(str1);

  text->cursor_row ++;
  text->cursor_pos = 0;

  width = 0.0;
  for (i=0;i<text->numlines;i++) {
    width = MAX(width, text_get_line_width(text, i));
  }
  text->max_width = width;
}

static void
text_insert_char(Text *text, gunichar c)
{
  gchar ch[7];
  int unilen;
  int row;
  gchar *line, *str;
  gchar *utf8_before;
  gchar *str1;

  /* Make a string of the the char */
  unilen = g_unichar_to_utf8 (c, ch);
  ch[unilen] = 0;
  
  row = text->cursor_row;

  /* Copy the before and after parts with the new char in between */
  line = text_get_line(text, row);
  utf8_before = g_utf8_offset_to_pointer(line, (glong)(text->cursor_pos));
  str1 = g_strndup(line, utf8_before - line);
  str = g_strconcat(str1, ch, utf8_before, NULL);
  text_set_line_text(text, row, str);
  g_free(str);
  g_free(str1);

  text->cursor_pos++;
  text->max_width = MAX(text->max_width, text_get_line_width(text, row));
}

gboolean
text_delete_key_handler(Focus *focus, ObjectChange ** change)
{
  Text *text;
  int row, i;
  const char *utf;
  gunichar c;

  text = focus->text;
  row = text->cursor_row;
  if (text->cursor_pos >= text_get_line_strlen(text, row)) {
    if (row+1 < text->numlines) {
      *change = text_create_change(text, TYPE_JOIN_ROW, 'Q',
				   text->cursor_pos, row, focus->obj);
    } else {
      return FALSE;
    }
  } else {
    utf = text_get_line(text, row);
    for (i = 0; i < text->cursor_pos; i++)
      utf = g_utf8_next_char (utf);
    c = g_utf8_get_char (utf);
    *change = text_create_change (text, TYPE_DELETE_FORWARD, c,
				  text->cursor_pos, text->cursor_row, focus->obj);
  }
  text_delete_forward(text);
  return TRUE;;
}

static int
text_key_event(Focus *focus, 
	       guint keystate, guint keyval, 
	       const gchar *str, int strlen,
               ObjectChange **change)
{
  Text *text;
  int return_val = FALSE;
  int row, i;
  const char *utf;
  gunichar c;

  *change = NULL;
  
  text = focus->text;

  switch(keyval) {
      case GDK_Up:
      case GDK_KP_Up:
        text->cursor_row--;
        if (text->cursor_row<0)
          text->cursor_row = 0;

        if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
          text->cursor_pos = text_get_line_strlen(text, text->cursor_row);

        break;
      case GDK_Down:
      case GDK_KP_Down:
        text->cursor_row++;
        if (text->cursor_row >= text->numlines)
          text->cursor_row = text->numlines - 1;

        if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
          text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
    
        break;
      case GDK_Left:
      case GDK_KP_Left:
        if (keystate & GDK_CONTROL_MASK)
	  text_move_cursor(text, WORD_START);
	else
          text->cursor_pos--;
        if (text->cursor_pos<0)
          text->cursor_pos = 0;
        break;
      case GDK_Right:
      case GDK_KP_Right:
        if (keystate & GDK_CONTROL_MASK)
	  text_move_cursor(text, WORD_END);
	else
          text->cursor_pos++;
        if (text->cursor_pos > text_get_line_strlen(text, text->cursor_row))
          text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
        break;
      case GDK_Home:
      case GDK_KP_Home:
        text->cursor_pos = 0;
        break;
      case GDK_End:
      case GDK_KP_End:
        text->cursor_pos = text_get_line_strlen(text, text->cursor_row);
        break;
      case GDK_Delete:
      case GDK_KP_Delete:
        return_val = text_delete_key_handler(focus, change);
        break;
      case GDK_BackSpace:
        return_val = TRUE;
        row = text->cursor_row;
        if (text->cursor_pos <= 0) {
          if (row > 0) {
            *change = text_create_change(text, TYPE_JOIN_ROW, 'Q',
                                         text_get_line_strlen(text, row-1), row-1,
					 focus->obj);
          } else {
            return_val = FALSE;
            break;
          }
        } else {
          utf = text_get_line(text, row);
          for (i = 0; i < (text->cursor_pos - 1); i++)
            utf = g_utf8_next_char (utf);
          c = g_utf8_get_char (utf);
          *change = text_create_change (text, TYPE_DELETE_BACKWARD, c,
                                        text->cursor_pos - 1,
                                        text->cursor_row,
					focus->obj);
        }
        text_delete_backward(text);
        break;
      case GDK_Return:
      case GDK_KP_Enter:
        return_val = TRUE;
        *change = text_create_change(text, TYPE_SPLIT_ROW, 'Q',
                                     text->cursor_pos, text->cursor_row,
				     focus->obj);
        text_split_line(text);
        break;
      case GDK_Shift_L:
      case GDK_Shift_R:
      case GDK_Control_L:
      case GDK_Control_R:
      case GDK_Alt_L:
      case GDK_Alt_R:
      case GDK_Meta_L:
      case GDK_Meta_R:
        return_val = FALSE; /* no text change for modifiers */
        break;
      default:
        if (str || (strlen>0)) {
          if (str && *str == '\r')
            break; /* avoid putting junk into our string */
          return_val = TRUE;
	  *change = change_list_create();
          for (utf = str; utf && *utf && strlen > 0 ;
	       utf = g_utf8_next_char (utf), strlen--) {
	    ObjectChange *step;
            c = g_utf8_get_char (utf);
            
            step = text_create_change (text, TYPE_INSERT_CHAR, c,
                                       text->cursor_pos, text->cursor_row,
				       focus->obj);
	    change_list_add (*change, step);
            text_insert_char (text, c);
          }
        }
        break;
  }  
  
  return return_val;
}

int text_is_empty(const Text *text)
{
  int i;
  for (i = 0; i < text->numlines; i++) {
    if (text_get_line_strlen(text, i) != 0) {
      return FALSE;
    }
  }
  return TRUE;
}

int
text_delete_all(Text *text, ObjectChange **change, DiaObject *obj)
{
  if (!text_is_empty(text)) {
    *change = text_create_change(text, TYPE_DELETE_ALL,
				 0, text->cursor_pos, text->cursor_row,
				 obj);
    
    text_set_string(text, "");
    calc_ascent_descent(text);
    return TRUE;
  }
  return FALSE;
}

void
data_add_text(AttributeNode attr, Text *text, DiaContext *ctx)
{
  DataNode composite;
  char *str;

  composite = data_add_composite(attr, "text", ctx);

  str = text_get_string_copy(text);
  data_add_string(composite_add_attribute(composite, "string"),
		  str, ctx);
  g_free(str);
  data_add_font(composite_add_attribute(composite, "font"),
		text->font, ctx);
  data_add_real(composite_add_attribute(composite, "height"),
		text->height, ctx);
  data_add_point(composite_add_attribute(composite, "pos"),
		 &text->position, ctx);
  data_add_color(composite_add_attribute(composite, "color"),
		 &text->color, ctx);
  data_add_enum(composite_add_attribute(composite, "alignment"),
		text->alignment, ctx);
}


Text *
data_text(AttributeNode text_attr, DiaContext *ctx)
{
  char *string = NULL;
  DiaFont *font;
  real height;
  Point pos = {0.0, 0.0};
  Color col;
  Alignment align;
  AttributeNode attr;
  Text *text;

  attr = composite_find_attribute(text_attr, "string");
  if (attr != NULL)
    string = data_string(attribute_first_data(attr), ctx);

  height = 1.0;
  attr = composite_find_attribute(text_attr, "height");
  if (attr != NULL)
    height = data_real(attribute_first_data(attr), ctx);

  attr = composite_find_attribute(text_attr, "font");
  if (attr != NULL) {
    font = data_font(attribute_first_data(attr), ctx);
  } else {
    font = dia_font_new_from_style(DIA_FONT_SANS,1.0);
  }
  
  attr = composite_find_attribute(text_attr, "pos");
  if (attr != NULL)
    data_point(attribute_first_data(attr), &pos, ctx);

  col = color_black;
  attr = composite_find_attribute(text_attr, "color");
  if (attr != NULL)
    data_color(attribute_first_data(attr), &col, ctx);

  align = ALIGN_LEFT;
  attr = composite_find_attribute(text_attr, "alignment");
  if (attr != NULL)
    align = data_enum(attribute_first_data(attr), ctx);
  
  text = new_text(string ? string : "", font, height, &pos, &col, align);
  if (font) dia_font_unref(font);
  if (string) g_free(string);
  return text;
}

void
text_get_attributes(Text *text, TextAttributes *attr)
{    
  DiaFont *old_font;
  old_font = attr->font;
  attr->font = dia_font_ref(text->font);
  if (old_font != NULL) dia_font_unref(old_font);
  attr->height = text->height;
  attr->position = text->position;
  attr->color = text->color;
  attr->alignment = text->alignment;
}

void
text_set_attributes(Text *text, TextAttributes *attr)
{
  if (text->font != attr->font) {
    text_set_font(text, attr->font);
  }
  text_set_height(text, attr->height);
  text->position = attr->position;
  text->color = attr->color;
  text->alignment = attr->alignment;
}

static void
text_change_apply(struct TextObjectChange *change, DiaObject *obj)
{
  Text *text = change->text;

  /* remember previous position/size */
  if (change->obj->ops->get_props)
    change->obj->ops->get_props(change->obj, change->props);

  switch (change->type) {
  case TYPE_INSERT_CHAR:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_insert_char(text, change->ch);
    break;
  case TYPE_DELETE_BACKWARD:
    text->cursor_pos = change->pos+1;
    text->cursor_row = change->row;
    text_delete_backward(text);
    break;
  case TYPE_DELETE_FORWARD:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_delete_forward(text);
    break;
  case TYPE_SPLIT_ROW:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_split_line(text);
    break;
  case TYPE_JOIN_ROW:
    text_join_lines(text, change->row);
    break;
  case TYPE_DELETE_ALL:
    set_string(text, "");
    text->cursor_pos = 0;
    text->cursor_row = 0;
    break;
  }
}

static void
text_change_revert(struct TextObjectChange *change, DiaObject *obj)
{
  Text *text = change->text;
  switch (change->type) {
  case TYPE_INSERT_CHAR:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_delete_forward(text);
    break;
  case TYPE_DELETE_BACKWARD:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_insert_char(text, change->ch);
    break;
  case TYPE_DELETE_FORWARD:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_insert_char(text, change->ch);
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    break;
  case TYPE_SPLIT_ROW:
    text_join_lines(text, change->row);
    break;
  case TYPE_JOIN_ROW:
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    text_split_line(text);
    break;
  case TYPE_DELETE_ALL:
    set_string(text, change->str);
    text->cursor_pos = change->pos;
    text->cursor_row = change->row;
    break;
  }
  /* restore previous position/size */
  if (change->obj->ops->set_props)
    change->obj->ops->set_props(change->obj, change->props);
}

static void
text_change_free(struct TextObjectChange *change) 
{
  g_free(change->str);
  prop_list_free(change->props);
}

/* If some object does not properly resize when undoing
 * text changes consider adding some additional properties.
 *
 * The list can contain more properties than supported by
 * the specific object, the'll get ignored than.
 */
static PropDescription _prop_descs[] = {
  { "elem_corner", PROP_TYPE_POINT },
  { "elem_width", PROP_TYPE_REAL },
  { "elem_height", PROP_TYPE_REAL },
    PROP_DESC_END
};

static GPtrArray *
make_posision_and_size_prop_list (void)
{
  GPtrArray *props;

  props = prop_list_from_descs(_prop_descs,pdtpp_true);

  return props;
}

static ObjectChange *
text_create_change(Text *text, TextChangeType type,
		   gunichar ch, int pos, int row, DiaObject *obj)
{
  struct TextObjectChange *change;

  change = g_new0(struct TextObjectChange, 1);

  change->obj = obj;
  change->props = make_posision_and_size_prop_list ();
  /* remember previous position/size */
  if (change->obj->ops->get_props)
    change->obj->ops->get_props(change->obj, change->props);

  change->obj_change.apply = (ObjectChangeApplyFunc) text_change_apply;
  change->obj_change.revert = (ObjectChangeRevertFunc) text_change_revert;
  change->obj_change.free = (ObjectChangeFreeFunc) text_change_free;

  change->text = text;
  change->type = type;
  change->ch = ch;
  change->pos = pos;
  change->row = row;
  if (type == TYPE_DELETE_ALL)
    change->str = text_get_string_copy(text);
  else
    change->str = NULL;
  return (ObjectChange *)change;
}

gboolean 
apply_textattr_properties(GPtrArray *props,
                          Text *text, const gchar *textname,
                          TextAttributes *attrs)
{
  TextProperty *textprop = 
    (TextProperty *)find_prop_by_name_and_type(props,textname,PROP_TYPE_TEXT);

  if ((!textprop) || 
      ((textprop->common.experience & (PXP_LOADED|PXP_SFO))==0 )) {
    /* most likely we're called after the dialog box has been applied */
    text_set_attributes(text,attrs);
    return TRUE; 
  }
  return FALSE;
}

gboolean 
apply_textstr_properties(GPtrArray *props,
                         Text *text, const gchar *textname,
                         const gchar *str)
{
  TextProperty *textprop = 
    (TextProperty *)find_prop_by_name_and_type(props,textname,PROP_TYPE_TEXT);

  if ((!textprop) || 
      ((textprop->common.experience & (PXP_LOADED|PXP_SFO))==0 )) {
    /* most likely we're called after the dialog box has been applied */
    text_set_string(text,str);
    return TRUE; 
  }
  return FALSE;
}