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 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
|
/*
Copyright (C) 1998-2002 Ulric Eriksson <ulric@siag.nu>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the Licence, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <sys/stat.h>
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <Mowitz/Mowitz.h>
#include <X11/xpm.h>
#include "MwRichtextP.h"
static void plugin_coords(Widget, XtPointer, int *, int *);
static int default_row_height(XtPointer, int);
static int default_adj_horiz(XtPointer, int);
static int default_style(XtPointer, int);
static MwRichchar *default_text(XtPointer, int);
static Boolean default_bop(XtPointer, int);
static float floatOne = 1.0;
#define offset(field) XtOffsetOf(MwRichtextRec, richtext.field)
static XtResource resources[] = {
{
XtNrichtextTopRow, /* name */
XtCRichtextTopRow, /* class */
XtRInt, /* type */
sizeof(int), /* size */
offset(top_row), /* offset */
XtRImmediate, /* default_type */
(XtPointer)0 /* default_addr */
}, {
XtNrichtextTopCol,
XtCRichtextTopCol,
XtRInt,
sizeof(int),
offset(top_col),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextSelectTopRow,
XtCRichtextSelectTopRow,
XtRInt,
sizeof(int),
offset(sel_top_row),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextSelectTopCol,
XtCRichtextSelectTopCol,
XtRInt,
sizeof(int),
offset(sel_top_col),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextSelectBottomRow,
XtCRichtextSelectBottomRow,
XtRInt,
sizeof(int),
offset(sel_bottom_row),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextSelectBottomCol,
XtCRichtextSelectBottomCol,
XtRInt,
sizeof(int),
offset(sel_bottom_col),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextPointRow,
XtCRichtextPointRow,
XtRInt,
sizeof(int),
offset(point_row),
XtRImmediate,
(XtPointer)1
}, {
XtNrichtextPointCol,
XtCRichtextPointCol,
XtRInt,
sizeof(int),
offset(point_col),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextRowHeight,
XtCRichtextRowHeight,
XtRPointer,
sizeof(XtPointer),
offset(row_height),
XtRImmediate,
(XtPointer)default_row_height
}, {
XtNrichtextAdjHoriz,
XtCRichtextAdjHoriz,
XtRPointer,
sizeof(XtPointer),
offset(adj_horiz),
XtRImmediate,
(XtPointer)default_adj_horiz
}, {
XtNrichtextStyle,
XtCRichtextStyle,
XtRPointer,
sizeof(XtPointer),
offset(style),
XtRImmediate,
(XtPointer)default_style
}, {
XtNrichtextBop,
XtCRichtextBop,
XtRPointer,
sizeof(XtPointer),
offset(bop),
XtRImmediate,
(XtPointer)default_bop
}, {
XtNrichtextText,
XtCRichtextText,
XtRPointer,
sizeof(XtPointer),
offset(text),
XtRImmediate,
(XtPointer)default_text
}, {
XtNrichtextData,
XtCRichtextData,
XtRPointer,
sizeof(XtPointer),
offset(data),
XtRImmediate,
(XtPointer) NULL
}, {
XtNrichtextRedisplay,
XtCRichtextRedisplay,
XtRBoolean,
sizeof(Boolean),
offset(redisplay),
XtRImmediate,
(XtPointer)False
}, {
XtNrichtextVisibleCursor,
XtCRichtextVisibleCursor,
XtRBoolean,
sizeof(Boolean),
offset(visible_cursor),
XtRImmediate,
(XtPointer)True
}, {
XtNrichtextPaperWidth,
XtCRichtextPaperWidth,
XtRInt,
sizeof(int),
offset(paper_width),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextLeftMargin,
XtCRichtextLeftMargin,
XtRInt,
sizeof(int),
offset(left_margin),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextRightMargin,
XtCRichtextRightMargin,
XtRInt,
sizeof(int),
offset(right_margin),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextPluginCoords,
XtCRichtextPluginCoords,
XtRPointer,
sizeof(XtPointer),
offset(plugin_coords),
XtRImmediate,
(XtPointer)plugin_coords
}, {
XtNzoom,
XtCZoom,
XtRFloat,
sizeof(float),
offset(zoom),
XtRFloat,
(XtPointer)&floatOne
}, {
XtNrichtextDelay,
XtCRichtextDelay,
XtRInt,
sizeof(int),
offset(delay),
XtRImmediate,
(XtPointer)0
}, {
XtNrichtextFormat,
XtCRichtextFormat,
XtRInt,
sizeof(int),
offset(format),
XtRImmediate,
(XtPointer)-1
}, {
XtNrichtextString,
XtCRichtextString,
XtRPointer,
sizeof(XtPointer),
offset(rc_string),
XtRImmediate,
(XtPointer)NULL
}, {
XtNrichtextRuler,
XtCRichtextRuler,
XtRWidget,
sizeof(Widget),
offset(ruler),
XtRImmediate,
(XtPointer)None,
}
};
#undef offset
/* methods */
static void DoLayout(MwRichtextWidget);
static void Resize(Widget);
static XtGeometryResult GeometryManager(Widget,
XtWidgetGeometry *, XtWidgetGeometry *);
static void ChangeManaged(Widget);
static void Redisplay(Widget, XEvent *, Region);
static void Initialize(Widget, Widget, ArgList, Cardinal *);
static void Realize(Widget, XtValueMask *, XSetWindowAttributes *);
static void Destroy(Widget);
static Boolean SetValues(Widget, Widget, Widget, ArgList, Cardinal *);
static int default_row_height(XtPointer p, int i)
{
MwRichtextWidget rtw = (MwRichtextWidget)p;
if (rtw->richtext.rc_string == NULL ||
rtw->richtext.rc_string[0].c == '\0')
return 20;
return MwRcStrheight(rtw->richtext.rc_string, -1);
}
static int default_adj_horiz(XtPointer p, int i)
{
return MW_HADJ_LEFT;
}
static int default_style(XtPointer p, int i)
{
return 0;
}
static MwRichchar *default_text(XtPointer p, int i)
{
MwRichtextWidget rw = (MwRichtextWidget)p;
if (i != 1) return NULL;
return rw->richtext.rc_string;
}
/* by default, do not display Beginning Of Paragraph marks */
static Boolean default_bop(XtPointer p, int i)
{
return False;
}
static void plugin_coords(Widget w, XtPointer p, int *x, int *y)
{
*x = *y = 0;
}
/* actions */
static void MoveForwardChar(Widget, XEvent *, String *, Cardinal *);
static void MoveBackwardChar(Widget, XEvent *, String *, Cardinal *);
static void MoveToLineStart(Widget, XEvent *, String *, Cardinal *);
static void MoveToLineEnd(Widget, XEvent *, String *, Cardinal *);
static void DeleteForwardChar(Widget, XEvent *, String *, Cardinal *);
static void DeleteBackwardChar(Widget, XEvent *, String *, Cardinal *);
static void InsertChar(Widget, XEvent *, String *, Cardinal *);
static void DeleteToEnd(Widget, XEvent *, String *, Cardinal *);
static void DeleteHighlighted(Widget, XEvent *, String *, Cardinal *);
static void SelectStart(Widget, XEvent *, String *, Cardinal *);
static void ExtendAdjust(Widget, XEvent *, String *, Cardinal *);
static void ExtendEnd(Widget, XEvent *, String *, Cardinal *);
static void InsertSelection(Widget, XEvent *, String *, Cardinal *);
static XtActionsRec actions[] =
{
{"forward-character", MoveForwardChar},
{"backward-character", MoveBackwardChar},
{"beginning-of-line", MoveToLineStart},
{"end-of-line", MoveToLineEnd},
{"delete-next-character", DeleteForwardChar},
{"delete-previous-character", DeleteBackwardChar},
{"insert-char", InsertChar},
{"delete-to-end", DeleteToEnd},
{"delete-highlighted", DeleteHighlighted},
{"select-start", SelectStart},
{"extend-adjust", ExtendAdjust},
{"extend-end", ExtendEnd},
{"insert-selection", InsertSelection},
};
/* translations */
static char translations[] =
"<Key>Home: beginning-of-line() \n"
":<Key>KP_Home: beginning-of-line() \n"
"Ctrl<Key>a: beginning-of-line() \n"
"<Key>End: end-of-line() \n"
":<Key>KP_End: end-of-line() \n"
"Ctrl<Key>e: end-of-line() \n"
"<Key>Right: forward-character() \n"
":<Key>KP_Right: forward-character() \n"
"Ctrl<Key>f: forward-character() \n"
"<Key>Left: backward-character() \n"
":<Key>KP_Left: backward-character() \n"
"Ctrl<Key>b: backward-character() \n"
"<Key>Delete: delete-next-character() \n"
":<Key>KP_Delete: delete-next-character() \n"
"Ctrl<Key>d: delete-next-character() \n"
"<Key>BackSpace: delete-previous-character() \n"
"Ctrl<Key>k: delete-to-end() \n"
"Ctrl<Key>w: delete-highlighted() \n"
"<Key>: insert-char()\n"
"<Btn1Down>: select-start() \n"
"<Btn1Motion>: extend-adjust() \n"
"<Btn1Up>: extend-end() \n"
"<Btn2Down>: insert-selection() \n"
;
MwRichtextClassRec mwRichtextClassRec = {
{ /* core fields */
/* superclass */ (WidgetClass) &compositeClassRec,
/* class_name */ "MwRichtext",
/* widget_size */ sizeof(MwRichtextRec),
/* class_initialize */ NULL,
/* class_part_initialize */ NULL,
/* class_inited */ FALSE,
/* initialize */ Initialize,
/* initialize_hook */ NULL,
/* realize */ Realize,
/* actions */ actions,
/* num_actions */ XtNumber(actions),
/* resources */ resources,
/* num_resources */ XtNumber(resources),
/* xrm_class */ NULLQUARK,
/* compress_motion */ TRUE,
/* compress_exposure */ TRUE,
/* compress_enterleave */ TRUE,
/* visible_interest */ FALSE,
/* destroy */ Destroy,
/* resize */ Resize,
/* expose */ Redisplay,
/* set_values */ SetValues,
/* set_values_hook */ NULL,
/* set_values_almost */ XtInheritSetValuesAlmost,
/* get_values_hook */ NULL,
/* accept_focus */ NULL,
/* version */ XtVersion,
/* callback_private */ NULL,
/* tm_table */ translations,
/* query_geometry */ XtInheritQueryGeometry,
/* display_accelerator */ XtInheritDisplayAccelerator,
/* extension */ NULL
},{
/* composite_class fields */
/* geometry_manager */ GeometryManager,
/* change_managed */ ChangeManaged,
/* insert_child */ XtInheritInsertChild,
/* delete_child */ XtInheritDeleteChild,
/* extension */ NULL
}, { /* richtext fields */
/* empty */ 0
}
};
WidgetClass mwRichtextWidgetClass = (WidgetClass)&mwRichtextClassRec;
static int ret_style(MwRichtextWidget rtw, int row)
{
if (rtw->richtext.style)
return (*rtw->richtext.style)(rtw->richtext.data, row);
return MW_STY_DEFAULT;
}
static MwRichchar *ret_text(MwRichtextWidget rtw, int row)
{
if (rtw->richtext.text)
return (*rtw->richtext.text)(rtw->richtext.data, row);
return NULL;
}
static void MoveForwardChar(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int i = rw->richtext.point_col;
MwRichchar *p = rw->richtext.rc_string;
if (p[i].c) i++;
rw->richtext.point_col = i;
Redisplay(w, NULL, None);
}
static void MoveBackwardChar(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int i = rw->richtext.point_col;
if (i) i--;
rw->richtext.point_col = i;
Redisplay(w, NULL, None);
}
static void MoveToLineStart(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
rw->richtext.point_col = 0;
Redisplay(w, NULL, None);
}
static void MoveToLineEnd(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
rw->richtext.point_col = MwRcStrlen(rw->richtext.rc_string);
Redisplay(w, NULL, None);
}
static void DeleteForwardChar(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int i = rw->richtext.point_col;
MwRichchar *p = rw->richtext.rc_string;
if (!p[i].c) return;
MwRcStrcpy(p+i, p+i+1);
Redisplay(w, NULL, None);
}
static void DeleteBackwardChar(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int i = rw->richtext.point_col;
MwRichchar *p = rw->richtext.rc_string;
if (!i) return;
i--;
MwRcStrcpy(p+i, p+i+1);
rw->richtext.point_col = i;
Redisplay(w, NULL, None);
}
static void InsertChar(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int count, bufsiz = 100;
unsigned char buf[120];
MwRichchar *rp;
KeySym keysym;
count = MwRichtextLookupString(w, event, buf, bufsiz, &keysym);
if (keysym >= ' ' && count == 1) {
buf[count] = '\0';
rp = MwRcMakerich(buf, rw->richtext.format);
MwRichtextInsertText(w, rp, count);
MwFree(rp);
rw->richtext.point_col += count;
Redisplay(w, NULL, None);
}
}
static void DeleteToEnd(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int i = rw->richtext.point_col;
MwRichchar *p = rw->richtext.rc_string;
p[i].c = '\0';
Redisplay(w, NULL, None);
}
static void DeleteHighlighted(Widget w, XEvent *event, String *params, Cardinal *n)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
MwRichchar *p = rw->richtext.rc_string;
if (rw->richtext.sel_top_col < 0) return;
MwRcStrcpy(p+rw->richtext.sel_top_col, p+rw->richtext.sel_bottom_col);
rw->richtext.point_col = rw->richtext.sel_top_col;
rw->richtext.sel_top_col = rw->richtext.sel_bottom_col = -1;
Redisplay(w, NULL, None);
}
static void SelectStart(Widget w, XEvent *event, String *params, Cardinal *n)
{
/*RichtextWidget rw = (RichtextWidget)w;*/
Redisplay(w, NULL, None);
}
static void ExtendAdjust(Widget w, XEvent *event, String *params, Cardinal *n)
{
/*RichtextWidget rw = (RichtextWidget)w;*/
Redisplay(w, NULL, None);
}
static void ExtendEnd(Widget w, XEvent *event, String *params, Cardinal *n)
{
/*RichtextWidget rw = (RichtextWidget)w;*/
Redisplay(w, NULL, None);
}
static void InsertSelection(Widget w, XEvent *event, String *params, Cardinal *n)
{
/*RichtextWidget rw = (RichtextWidget)w;*/
Redisplay(w, NULL, None);
}
static GC get_gc(Widget w, unsigned long fg, unsigned long bg, Font font)
{
unsigned long valuemask = 0;
XGCValues values;
GC gc = XCreateGC(XtDisplay(w), XtWindow(w), valuemask, &values);
XSetForeground(XtDisplay(w), gc, fg);
XSetBackground(XtDisplay(w), gc, bg);
if (font != -1)
XSetFont(XtDisplay(w), gc, font);
return gc;
}
static MwFmt fmt0 = {
"Helvetica", /* font family */
120, /* size in decipoints */
False, /* bold */
False, /* italic */
False, /* underline */
False, /* strikethrough */
"black", /* foreground */
"white", /* background */
0, /* borders */
MW_VADJ_CENTER, /* vertical adjust */
MW_HADJ_LEFT, /* horizontal adjust */
0}; /* style */
#define superclass (&coreClassRec)
static void Realize(Widget w, XtValueMask *valueMask,
XSetWindowAttributes *attributes)
{
MwRichtextWidget rtw = (MwRichtextWidget)w;
unsigned long fg, bg, blockbg;
XColor screen_color, exact_color;
Display *dpy = XtDisplay(w);
char *name, *class;
XIMStyles *styles;
int i;
if (rtw->richtext.format == -1)
rtw->richtext.format = MwEncodeFormat(~0, &fmt0);
(*superclass->core_class.realize)(w, valueMask, attributes);
fg = BlackPixelOfScreen(XtScreen(w));
bg = rtw->core.background_pixel;
XAllocNamedColor(dpy,
DefaultColormap(dpy, DefaultScreen(dpy)),
"grey", &screen_color, &exact_color);
blockbg = screen_color.pixel;
rtw->richtext.clear_gc = get_gc(w, bg, fg, -1);
rtw->richtext.cell_gc = get_gc(w, fg, blockbg,
-1/*get_font(0)*/);
rtw->richtext.block_gc = get_gc(w, blockbg, bg, -1);
rtw->richtext.cursor_gc = get_gc(w, fg^bg, 0, -1);
XSetFunction(dpy, rtw->richtext.cursor_gc, GXxor);
XSetLineAttributes(dpy, rtw->richtext.cursor_gc,
1, LineSolid, CapButt, JoinMiter);
#ifdef HAVE_XCREATEIC
/* Set up input methods */
XtGetApplicationNameAndClass(dpy, &name, &class);
rtw->richtext.xim = XOpenIM(dpy, XtDatabase(dpy), name, class);
XGetIMValues(rtw->richtext.xim,
XNQueryInputStyle, &styles,
(char *)0);
for (i = 0; i < styles->count_styles; i++) {
if (styles->supported_styles[i] ==
(XIMPreeditNothing | XIMStatusNothing)) break;
}
if (i == styles->count_styles) i = 0;
rtw->richtext.xic = XCreateIC(rtw->richtext.xim,
XNInputStyle, styles->supported_styles[i],
XNClientWindow, XtWindow(w),
(char *)0);
#endif
}
static void Initialize(Widget request, Widget new, ArgList args, Cardinal *n)
{
MwRichtextWidget rtw = (MwRichtextWidget)new;
rtw->richtext.timeout = None;
rtw->richtext.plain = NULL;
if (rtw->richtext.data == NULL)
rtw->richtext.data = (XtPointer)new;
if (rtw->core.width == 0) rtw->core.width = 200;
if (rtw->core.height == 0) rtw->core.height = 22;
}
static void Destroy(Widget w)
{
MwRichtextWidget rtw = (MwRichtextWidget)w;
XFreeGC(XtDisplay(w), rtw->richtext.clear_gc);
XFreeGC(XtDisplay(w), rtw->richtext.cell_gc);
XFreeGC(XtDisplay(w), rtw->richtext.cursor_gc);
XFreeGC(XtDisplay(w), rtw->richtext.block_gc);
#ifdef HAVE_XCREATEIC
XDestroyIC(rtw->richtext.xic);
XCloseIM(rtw->richtext.xim);
#endif
if (rtw->richtext.timeout)
XtRemoveTimeOut(rtw->richtext.timeout);
}
static Dimension row_height(MwRichtextWidget rtw, int row)
{
if (rtw->richtext.row_height)
return (*rtw->richtext.row_height)(rtw->richtext.data, row);
return 20;
}
/* ---
forget about x for now
*/
/* ---
Check if we are in the block.
*/
static int inblock(MwRichtextWidget rtw, int r, int c)
{
return ((r > rtw->richtext.sel_top_row ||
(r == rtw->richtext.sel_top_row &&
c >= rtw->richtext.sel_top_col)) &&
(r < rtw->richtext.sel_bottom_row ||
(r == rtw->richtext.sel_bottom_row &&
c <= rtw->richtext.sel_bottom_col)));
}
static int ret_hadj(MwRichtextWidget rtw, int row)
{
if (rtw->richtext.adj_horiz)
return (*rtw->richtext.adj_horiz)(rtw->richtext.data, row);
return MW_HADJ_LEFT;
}
static int segment_char2coords(MwRichtextWidget rtw, float *x,
MwRichchar *line, int length, int extra_space, int no_of_blanks,
int tabmode, int col)
{
int i;
float tw;
MwRichchar c;
tw = MwRcStrwidth(line, length);
switch (tabmode) {
case 'r':
*x -= tw;
break;
case 'c':
*x -= tw/2;
break;
default: /* left or full */
break;
}
for (i = 0; i < length; i++) {
if (i >= col) return 1;
c = line[i];
if (isspace(c.c)) c.c = ' ';
if (c.c == ' ' && extra_space > 0 && no_of_blanks > 0) {
float xx = extra_space/no_of_blanks;
*x += xx;
extra_space -= xx;
no_of_blanks--;
}
*x += MwRcWidth(c);
}
return 0;
}
/*
Function converts character coordinates to screen-ready pixel coordinates.
That means: zoomed + top added.
*/
void MwRichtextCharToCoords(MwRichtextWidget rtw,
int cell_row, int cell_col,
int *cell_x, int *cell_y)
{
int r;
MwTabstop mt;
int lm = rtw->richtext.left_margin;
int rm = rtw->richtext.right_margin;
int pw = rtw->richtext.paper_width;
float tw;
int nb, i = 0;
MwRichchar c;
MwRichchar *line = ret_text(rtw, cell_row);
int n;
int hadj;
int ss, nt;
float zoom = rtw->richtext.zoom;
float fx = 0;
*cell_y = 0;
for (r = 1; r < cell_row; r++)
*cell_y += row_height(rtw, r);
fx = lm;
hadj = ret_hadj(rtw, cell_row);
if (!line) goto Done;
if (hadj == MW_HADJ_CENTER) {
fx = lm+(pw-lm-rm)/2;
segment_char2coords(rtw, &fx, line, MwRcStrlen(line),
0, 0, 'c', cell_col);
} else if (hadj == MW_HADJ_RIGHT) {
fx = pw-rm;
segment_char2coords(rtw, &fx, line, MwRcStrlen(line),
0, 0, 'r', cell_col);
} else {
mt.x = 0;
mt.j = 'l';
ss = nt = 0;
while (line[nt].c && line[nt].c != '\t') nt++;
while (line[nt].c == '\t') {
n = segment_char2coords(rtw, &fx, line+ss, nt-ss,
0, 0, mt.j, cell_col-ss);
if (n) goto Done;
mt = MwTabstopNextStop(rtw->richtext.ruler, fx-lm);
fx = mt.x+lm;
ss = ++nt;
while (line[nt].c && line[nt].c != '\t') nt++;
}
tw = 0;
nb = 0;
if (hadj == MW_HADJ_FULL &&
!rtw->richtext.bop(rtw->richtext.data, cell_row+1)) {
for (i = 0; line[i].c; i++) {
c = line[i];
if (isspace(c.c)) nb++;
tw += MwRcWidth(c);
}
tw = pw-lm-rm-tw;
}
n = segment_char2coords(rtw, &fx, line+ss, nt-ss,
tw, nb, mt.j, cell_col-ss);
}
Done:
fx -= rtw->richtext.top_col;
*cell_y -= rtw->richtext.top_row;
fx *= zoom;
*cell_y *= zoom;
*cell_x = fx;
}
/*
Returns 1 if the position we're about to draw a character at is to the
right of the one we're looking for. The result is returned in col, which
is relative to the start of the segment.
*/
static int segment_coords2char(MwRichtextWidget rtw, float *x, int tx,
MwRichchar *line, int length, int extra_space, int no_of_blanks,
int tabmode, int *col)
{
float tw;
MwRichchar c;
*col = 0;
tw = MwRcStrwidth(line, length);
switch (tabmode) {
case 'r':
*x -= tw;
break;
case 'c':
*x -= tw/2;
break;
default: /* left or full */
break;
}
for (*col = 0; *col < length; (*col)++) {
if (*x > tx) {
return 1;
}
c = line[*col];
if (isspace(c.c)) c.c = ' ';
if (c.c == ' ' && extra_space > 0 && no_of_blanks > 0) {
float xx = extra_space/no_of_blanks;
*x += xx;
extra_space -= xx;
no_of_blanks--;
}
*x += MwRcWidth(c);
}
return 0;
}
/* ---
*/
void MwRichtextCoordsToChar(MwRichtextWidget rtw,
int *cur_row, int *cur_col,
int cur_x, int cur_y)
{
MwTabstop mt;
int lm = rtw->richtext.left_margin;
int rm = rtw->richtext.right_margin;
int pw = rtw->richtext.paper_width;
int tw, nb, i = 0;
MwRichchar c;
MwRichchar *line;
int n;
int hadj;
int ss, nt;
float cell_x = 0;
int cell_y = 0;
float zoom = rtw->richtext.zoom;
cur_x /= zoom;
cur_y /= zoom;
cur_x += rtw->richtext.top_col-2;
cur_y += rtw->richtext.top_row;
for (*cur_row = 1; cell_y < cur_y; *cur_row += 1)
cell_y += row_height(rtw, *cur_row);
if (*cur_row > 1) *cur_row -= 1;
*cur_col = 0;
line = ret_text(rtw, *cur_row);
cell_x = lm;
hadj = ret_hadj(rtw, *cur_row);
if (!line) return;
if (hadj == MW_HADJ_CENTER) {
cell_x = lm+(pw-lm-rm)/2;
n = segment_coords2char(rtw, &cell_x, cur_x,
line, MwRcStrlen(line),
0, 0, 'c', cur_col);
} else if (hadj == MW_HADJ_RIGHT) {
cell_x = pw-rm;
n = segment_coords2char(rtw, &cell_x, cur_x,
line, MwRcStrlen(line),
0, 0, 'r', cur_col);
} else {
mt.x = 0;
mt.j = 'l';
ss = nt = 0;
while (line[nt].c && line[nt].c != '\t') nt++;
while (line[nt].c == '\t') {
n = segment_coords2char(rtw, &cell_x, cur_x,
line+ss, nt-ss,
0, 0, mt.j, cur_col);
if (n) return;
mt = MwTabstopNextStop(rtw->richtext.ruler, cell_x-lm);
cell_x = mt.x+lm;
ss = ++nt;
while (line[nt].c && line[nt].c != '\t') nt++;
}
tw = 0;
nb = 0;
if (hadj == MW_HADJ_FULL &&
!rtw->richtext.bop(rtw->richtext.data, *cur_row+1)) {
for (i = 0; line[i].c; i++) {
c = line[i];
if (isspace(c.c)) nb++;
tw += MwRcWidth(c);
}
tw = pw-lm-rm-tw;
}
n = segment_coords2char(rtw, &cell_x, cur_x,
line+ss, nt-ss,
tw, nb, mt.j, cur_col);
}
}
static void update_ruler(Widget w)
{
MwRichtextWidget rtw = (MwRichtextWidget)w;
if (rtw->richtext.ruler) {
XtVaSetValues(rtw->richtext.ruler,
XtNtabstopTopCol, rtw->richtext.top_col,
XtNleftMargin, rtw->richtext.left_margin,
XtNrightMargin, rtw->richtext.right_margin,
XtNpaperWidth, rtw->richtext.paper_width,
(char *)0);
MwTabstopSetZoom(rtw->richtext.ruler, rtw->richtext.zoom);
}
}
/*
Change top_row and top_col so that point is visible.
*/
static Boolean move_top(MwRichtextWidget rtw)
{
Boolean pr_scr_flag = False;
int cur_x, cur_y, h;
int width, height;
float zoom = rtw->richtext.zoom;
int otc = rtw->richtext.top_col;
/* Figure out how big the window is */
width = rtw->core.width; /* unzoomed size */
height = rtw->core.height;
MwRichtextCharToCoords(rtw,
rtw->richtext.point_row, rtw->richtext.point_col,
&cur_x, &cur_y);
/* cur_x and cur_y are in zoomed pixels */
if (cur_x < 0) {
/* must move grid to the right, i.e. make top_col smaller */
rtw->richtext.top_col += cur_x;
pr_scr_flag = TRUE;
} else if (cur_x+1 > width) {
/* move grid to the left, i.e. make top_col larger */
rtw->richtext.top_col += cur_x+1-width;
pr_scr_flag = TRUE;
}
h = zoom*row_height(rtw, rtw->richtext.point_row)+4;
if (cur_y < 0) {
rtw->richtext.top_row += cur_y;
/* move grid down, i.e. make top_row smaller */
pr_scr_flag = TRUE;
} else if (cur_y+h > height) {
/* move grid up, i.e. make top_col larger */
rtw->richtext.top_row += cur_y+h-height;
pr_scr_flag = TRUE;
}
if (rtw->richtext.top_row < 0) rtw->richtext.top_row = 0;
if (rtw->richtext.top_col < 0) rtw->richtext.top_col = 0;
if (otc != rtw->richtext.top_col) update_ruler((Widget)rtw);
return pr_scr_flag;
}
static void toggle_cursor(MwRichtextWidget rtw)
{
int cur_x, cur_y;
int height = row_height(rtw, rtw->richtext.point_row);
float zoom = rtw->richtext.zoom;
MwRichtextCharToCoords(rtw,
rtw->richtext.point_row, rtw->richtext.point_col,
&cur_x, &cur_y);
XDrawLine(XtDisplay((Widget)rtw), XtWindow((Widget)rtw),
rtw->richtext.cursor_gc, cur_x, cur_y+zoom*height/4,
cur_x,
cur_y+zoom*5*height/4);
}
static void draw_segment(MwRichtextWidget rtw, Drawable cell_win,
float *x_base, int y_base, int yo, MwRichchar *line, int length,
int extra_space, int no_of_blanks, int tabmode, int row)
{
int of = -1;
MwFmt fmt;
XColor color;
int i;
Display *display = XtDisplay(rtw);
float zoom = rtw->richtext.zoom;
GC gc = rtw->richtext.cell_gc;
int tw;
MwRichchar c;
int tc = -rtw->richtext.top_col;
int height = row_height(rtw, row);
tw = MwRcStrwidth(line, length);
switch (tabmode) {
case 'r':
*x_base -= tw;
break;
case 'c':
*x_base -= tw/2;
break;
default: /* left or full */
break;
}
for (i = 0; i < length; i++) {
float width;
c = line[i];
if (isspace(c.c)) c.c = ' ';
width = MwRcWidth(c);
if (inblock(rtw, row, i)) {
XFillRectangle(display, cell_win,
rtw->richtext.block_gc,
zoom*(*x_base+tc),
y_base+zoom*(yo-3*height/4),
ceil(zoom*width),
zoom*(height));
}
if (c.fmt != of) {
of = c.fmt;
MwDecodeFormat(of, ~0, &fmt);
MwAllocNamedColor(display, fmt.fg, &color);
XSetForeground(display, gc, color.pixel);
}
MwRcStrdraw(cell_win, gc, 0, y_base, *x_base+tc, yo, &c, 1, zoom);
if (c.c == ' ' && extra_space > 0 && no_of_blanks > 0) {
float x = extra_space/no_of_blanks;
*x_base += x;
extra_space -= x;
no_of_blanks--;
}
if (fmt.uline) {
XDrawLine(display, cell_win, gc,
zoom*(*x_base+tc), y_base+zoom*(yo+1),
zoom*(*x_base+tc+width), y_base+zoom*(yo+1));
}
if (fmt.strike) {
XDrawLine(display, cell_win, gc,
zoom*(*x_base+tc), y_base+zoom*(yo-4),
zoom*(*x_base+tc+width), y_base+zoom*(yo-4));
}
*x_base += width;
}
}
/*
This does tabs like Word Perfect, which isn't perfect but better than nothing.
It goes like this:
0. Get the tab table: an array of tuples {justification, position}
where justification is one of center, left, right and position
is the horizontal position in unzoomed pixels. I'll simulate
the tabs for now, as pw currently has only left tabs with fixed distance.
1. Split the line into tab-separated segments. Most lines don't contain
any tabs, so there is only one segment.
2. If the line justification is center or right, tabs are ignored, i.e.
treated as blanks. I am not entirely happy with this part.
3. Otherwise start by drawing the first segment flush left. Each tab
skips to the first tab stop to the right of the last character.
4. Each tab skips to the first tab stop to the right of the last
character.
5. Start over at 3 by drawing the next segment. This time the segment
may be justified left, center or right.
In case we are doing "full" justification, the last segment
must be treated specially. The spaces must be laid out so that we get
flush left *and* right edges.
1. Calculate the total width of all the words, including the blanks.
Also count the blanks.
2. Calculate the extra space that needs to be laid out.
3. Draw the text as usual, but for each blank adjust the spacing. Add
extra_space/no_of_blanks, then subtract that amount
from extra_space and subtract 1 from no_of_blanks.
4. Draw the first word left-adjusted at the tab stop where we left off.
5. If there are more words, precede each with the amount of space from 3.
*/
/* y_base is in zoomed coordinates. yo is unzoomed */
static void draw_line(MwRichtextWidget rtw, Drawable cell_win,
int y_base, int yo, int row, int clr)
{
int height = row_height(rtw, row);
float x_base = 0;
Display *display = XtDisplay(rtw);
MwRichchar *line = ret_text(rtw, row);
float zoom = rtw->richtext.zoom;
int ss, nt, hadj;
MwTabstop mt;
int lm = rtw->richtext.left_margin;
int rm = rtw->richtext.right_margin;
int pw = rtw->richtext.paper_width;
float tw;
int nb, i = 0;
MwRichchar c;
if (clr) {
/* the 4 is from trial and error; should be from font metrics */
XClearArea(display, cell_win, 0, y_base+zoom*(yo+4),
rtw->core.width, zoom*height, FALSE);
}
if (!line) return;
x_base = lm;
if (ret_style(rtw, row) == MW_STY_EMBED) {
#if 0
char *p = (char *)MwRcMakeplain(line);
embed_draw(cell_win, x_base, y_base, p);
MwFree(p);
#else
fprintf(stderr, "No, no, no.\n"
"Nothing should be using this any more\n");
#endif
return; /* done */
}
hadj = ret_hadj(rtw, row);
if (hadj == MW_HADJ_CENTER) {
x_base = lm+(pw-lm-rm)/2;
draw_segment(rtw, cell_win, &x_base, y_base, yo+height,
line, MwRcStrlen(line), 0, 0, 'c', row);
} else if (hadj == MW_HADJ_RIGHT) {
x_base = pw-rm;
draw_segment(rtw, cell_win, &x_base, y_base, yo+height,
line, MwRcStrlen(line), 0, 0, 'r', row);
} else {
mt.x = 0;
mt.j = 'l';
ss = nt = 0;
while (line[nt].c && line[nt].c != '\t') nt++;
while (line[nt].c == '\t') {
draw_segment(rtw, cell_win, &x_base, y_base, yo+height,
line+ss, nt-ss, 0, 0, mt.j, row);
mt = MwTabstopNextStop(rtw->richtext.ruler, x_base-lm);
x_base = mt.x+lm;
ss = ++nt;
while (line[nt].c && line[nt].c != '\t') nt++;
}
tw = 0;
nb = 0;
if (hadj == MW_HADJ_FULL &&
!rtw->richtext.bop(rtw->richtext.data, row+1)) {
for (i = 0; line[i].c; i++) {
c = line[i];
if (isspace(c.c)) nb++;
tw += MwRcWidth(c);
}
tw = pw-lm-rm-tw;
}
draw_segment(rtw, cell_win, &x_base, y_base, yo+height,
line+ss, nt-ss, tw, nb, mt.j, row);
}
}
void MwRichtextDrawLine(Widget w, int row)
{
Window cell_win = XtWindow(w);
MwRichtextWidget rtw = (MwRichtextWidget)w;
int y_base = 0;
int r;
for (r = 1; r < row; r++)
y_base += row_height(rtw, r);
y_base -= rtw->richtext.top_row;
if (rtw->richtext.visible_cursor)
toggle_cursor(rtw);
draw_line(rtw, cell_win, 0, y_base, row, TRUE);
if (rtw->richtext.visible_cursor)
toggle_cursor(rtw);
}
void MwRichtextDraw(MwRichtextWidget rtw, Drawable d)
{
int i, c0, x_base, y_base, y, h;
int height;
float zoom = rtw->richtext.zoom;
MwRichtextCoordsToChar(rtw, &i, &c0, 0, 0); /* where to start */
if (i > 1) i--; /* just in case */
height = rtw->core.height; /* zoomed height */
MwRichtextCharToCoords(rtw, i, 0, &x_base, &y_base);
h = row_height(rtw, i);
y = 0;
while (y_base+zoom*y < height) {
draw_line(rtw, d, y_base, y, i, FALSE);
y += row_height(rtw, i);
i++;
}
}
/* ---
Draw onto a pixmap.
*/
Pixmap richtext_pixmap(MwRichtextWidget rtw)
{
int width, height, depth;
Pixmap scribble;
Widget w = (Widget)rtw;
width = rtw->core.width;
height = rtw->core.height;
depth = rtw->core.depth;
if (width > 2000 || height > 2000) return None;
scribble = XCreatePixmap(XtDisplay(w), XtWindow(w),
width, height, depth);
XFillRectangle(XtDisplay(w), scribble, rtw->richtext.clear_gc,
0, 0, width, height);
MwRichtextDraw(rtw, scribble);
return scribble;
}
static void do_redisplay(XtPointer client_data, XtIntervalId *id)
{
Widget w = (Widget)client_data;
Pixmap scribble;
GC gc;
unsigned long valuemask = 0;
XGCValues values;
MwRichtextWidget rtw = (MwRichtextWidget)w;
scribble = richtext_pixmap(rtw);
if (scribble == None) return;
gc = XCreateGC(XtDisplay(w), XtWindow(w),
valuemask, &values);
XCopyArea(XtDisplay(w), scribble, XtWindow(w),
gc, 0, 0, rtw->core.width, rtw->core.height, 0, 0);
XFreePixmap(XtDisplay(w), scribble);
XFreeGC(XtDisplay(w), gc);
if (rtw->richtext.visible_cursor) {
toggle_cursor(rtw);
}
/* update plugin positions */
DoLayout(rtw);
/* clear the timeout */
rtw->richtext.timeout = None;
update_ruler(w);
}
static void Redisplay(Widget w, XEvent *event, Region r)
{
MwRichtextWidget tw = (MwRichtextWidget)w;
if (tw->richtext.timeout) return; /* already set */
if (tw->richtext.delay) {
tw->richtext.timeout = XtAppAddTimeOut(
XtWidgetToApplicationContext(w),
tw->richtext.delay, do_redisplay, (XtPointer)w);
update_ruler(w);
} else {
do_redisplay((XtPointer)w, NULL);
}
}
static Boolean SetValues(Widget current, Widget request, Widget new,
ArgList args, Cardinal *nargs)
{
MwRichtextWidget currtw = (MwRichtextWidget)current;
MwRichtextWidget newrtw = (MwRichtextWidget)new;
Boolean do_redisplay = False;
do_redisplay = (currtw->richtext.sel_top_row != newrtw->richtext.sel_top_row
|| currtw->richtext.sel_top_col != newrtw->richtext.sel_top_col
|| currtw->richtext.sel_bottom_row != newrtw->richtext.sel_bottom_row
|| currtw->richtext.sel_bottom_col != newrtw->richtext.sel_bottom_col
|| currtw->richtext.top_row != newrtw->richtext.top_row
|| currtw->richtext.top_col != newrtw->richtext.top_col
|| currtw->richtext.zoom != newrtw->richtext.zoom);
if (newrtw->richtext.visible_cursor &&
(newrtw->richtext.point_row != currtw->richtext.point_row ||
newrtw->richtext.point_col != currtw->richtext.point_col ||
newrtw->richtext.redisplay))
do_redisplay |= move_top(newrtw);
if (newrtw->richtext.redisplay) {
do_redisplay = True;
newrtw->richtext.redisplay = False;
}
/* can't let Xt handle this because it will flicker */
if (do_redisplay) {
Redisplay((Widget)newrtw, NULL, None);
do_redisplay = False;
} else {
if (currtw->richtext.visible_cursor)
toggle_cursor(currtw);
if (newrtw->richtext.visible_cursor)
toggle_cursor(newrtw);
}
return do_redisplay;
}
/*
* Do a layout, actually assigning positions.
*
* This function uses a callback from the application to get the
* position of each plugin. In the case of PW, that callback uses
* richtext_char2coords. See pw/window.c.
*/
static void DoLayout(MwRichtextWidget sw)
{
int i;
int x, y, top_x = 0, top_y = 0;
if (sw->composite.num_children) {
top_x = sw->richtext.top_col;
top_y = sw->richtext.top_row;
}
for (i = 0; i < sw->composite.num_children; i++) {
(*sw->richtext.plugin_coords)(sw->composite.children[i],
sw->richtext.data, &x, &y);
XtMoveWidget(sw->composite.children[i], x, y);
}
}
/*
* Actually layout the table
*/
static void Resize(Widget w)
{
DoLayout((MwRichtextWidget)w);
}
/*
* Geometry Manager
*/
static XtGeometryResult GeometryManager(Widget w,
XtWidgetGeometry *request, XtWidgetGeometry *reply)
{
return XtGeometryYes;
}
static void ChangeManaged(Widget w)
{
DoLayout((MwRichtextWidget)w);
}
int MwRichtextLookupString(Widget w, XEvent *event, char *buf,
int bufsiz, KeySym *keysym)
{
#ifdef HAVE_XCREATEIC
Status status;
return XmbLookupString(((MwRichtextWidget)w)->richtext.xic,
(XKeyEvent *)event, (char *)buf, bufsiz,
keysym, &status);
#else
return XLookupString((XKeyEvent *)event, (char *)buf, bufsiz,
keysym, NULL);
#endif
}
void MwRichtextSetZoom(Widget w, float zoom)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
if (zoom < .1) zoom = .1;
if (zoom > 10) zoom = 10;
if (rw->richtext.zoom != zoom) {
rw->richtext.zoom = zoom;
Redisplay(w, NULL, None);
}
}
/*
Functions that allow this widget to replace TextField with a minimum of effort.
*/
void MwRichtextSetString(Widget w, char *p)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
MwFree(rw->richtext.rc_string);
rw->richtext.rc_string = MwRcMakerich((unsigned char *)p,
rw->richtext.format);
Redisplay(w, NULL, None);
}
char *MwRichtextGetString(Widget w)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
MwFree(rw->richtext.plain);
rw->richtext.plain = (char *)MwRcMakeplain(rw->richtext.rc_string);
return rw->richtext.plain;
}
void MwRichtextInsertText(Widget w, MwRichchar *buf, int count)
{
MwRichtextWidget rw = (MwRichtextWidget)w;
int i = rw->richtext.point_col;
MwRichchar *p = rw->richtext.rc_string;
int n = MwRcStrlen(p);
MwRichchar *q = MwMalloc((n+count+1)*sizeof *q);
MwRcStrncpy(q, p, i);
MwRcStrncpy(q+i, buf, count);
MwRcStrcpy(q+i+count, p+i);
rw->richtext.rc_string = q;
MwFree(p);
Redisplay(w, NULL, None);
}
|