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
|
/*
* ttkLayout.c --
*
* Generic layout processing.
*
* Copyright (c) 2003 Joe English. Freely redistributable.
*
* layout.c,v 1.76 2007/12/02 04:34:30 jenglish Exp
*/
#include <string.h>
#include <tk.h>
#include "tkThemeInt.h"
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
/*------------------------------------------------------------------------
* +++ Ttk_Box and Ttk_Padding utilities:
*/
Ttk_Box Ttk_MakeBox(int x, int y, int width, int height)
{
Ttk_Box b;
b.x = x; b.y = y; b.width = width; b.height = height;
return b;
}
int Ttk_BoxContains(Ttk_Box box, int x, int y)
{
return box.x <= x && x < box.x + box.width
&& box.y <= y && y < box.y + box.height;
}
Tcl_Obj *Ttk_NewBoxObj(Ttk_Box box)
{
Tcl_Obj *result[4];
result[0] = Tcl_NewIntObj(box.x);
result[1] = Tcl_NewIntObj(box.y);
result[2] = Tcl_NewIntObj(box.width);
result[3] = Tcl_NewIntObj(box.height);
return Tcl_NewListObj(4, result);
}
/*
* packTop, packBottom, packLeft, packRight --
* Carve out a parcel of the specified height (resp width)
* from the specified cavity.
*
* Returns:
* The new parcel.
*
* Side effects:
* Adjust the cavity.
*/
static Ttk_Box packTop(Ttk_Box *cavity, int height)
{
Ttk_Box parcel;
height = MIN(height, cavity->height);
parcel = Ttk_MakeBox(cavity->x, cavity->y, cavity->width, height);
cavity->y += height;
cavity->height -= height;
return parcel;
}
static Ttk_Box packBottom(Ttk_Box *cavity, int height)
{
height = MIN(height, cavity->height);
cavity->height -= height;
return Ttk_MakeBox(
cavity->x, cavity->y + cavity->height,
cavity->width, height);
}
static Ttk_Box packLeft(Ttk_Box *cavity, int width)
{
Ttk_Box parcel;
width = MIN(width, cavity->width);
parcel = Ttk_MakeBox(cavity->x, cavity->y, width,cavity->height);
cavity->x += width;
cavity->width -= width;
return parcel;
}
static Ttk_Box packRight(Ttk_Box *cavity, int width)
{
width = MIN(width, cavity->width);
cavity->width -= width;
return Ttk_MakeBox(cavity->x + cavity->width,
cavity->y, width, cavity->height);
}
/*
* Ttk_PackBox --
* Carve out a parcel of the specified size on the specified side
* in the specified cavity.
*
* Returns:
* The new parcel.
*
* Side effects:
* Adjust the cavity.
*/
Ttk_Box Ttk_PackBox(Ttk_Box *cavity, int width, int height, Ttk_Side side)
{
switch (side) {
default:
case TTK_SIDE_TOP: return packTop(cavity, height);
case TTK_SIDE_BOTTOM: return packBottom(cavity, height);
case TTK_SIDE_LEFT: return packLeft(cavity, width);
case TTK_SIDE_RIGHT: return packRight(cavity, width);
}
}
/*
* Ttk_PadBox --
* Shrink a box by the specified padding amount.
*/
Ttk_Box Ttk_PadBox(Ttk_Box b, Ttk_Padding p)
{
b.x += p.left;
b.y += p.top;
b.width -= (p.left + p.right);
b.height -= (p.top + p.bottom);
if (b.width <= 0) b.width = 1;
if (b.height <= 0) b.height = 1;
return b;
}
/*
* Ttk_ExpandBox --
* Grow a box by the specified padding amount.
*/
Ttk_Box Ttk_ExpandBox(Ttk_Box b, Ttk_Padding p)
{
b.x -= p.left;
b.y -= p.top;
b.width += (p.left + p.right);
b.height += (p.top + p.bottom);
return b;
}
/*
* Ttk_StickBox --
* Place a box of size w * h in the specified parcel,
* according to the specified sticky bits.
*/
Ttk_Box Ttk_StickBox(Ttk_Box parcel, int width, int height, unsigned sticky)
{
int dx, dy;
if (width > parcel.width) width = parcel.width;
if (height > parcel.height) height = parcel.height;
dx = parcel.width - width;
dy = parcel.height - height;
/*
* X coordinate adjustment:
*/
switch (sticky & (TTK_STICK_W | TTK_STICK_E))
{
case TTK_STICK_W | TTK_STICK_E:
/* no-op -- use entire parcel width */
break;
case TTK_STICK_W:
parcel.width = width;
break;
case TTK_STICK_E:
parcel.x += dx;
parcel.width = width;
break;
default :
parcel.x += dx / 2;
parcel.width = width;
break;
}
/*
* Y coordinate adjustment:
*/
switch (sticky & (TTK_STICK_N | TTK_STICK_S))
{
case TTK_STICK_N | TTK_STICK_S:
/* use entire parcel height */
break;
case TTK_STICK_N:
parcel.height = height;
break;
case TTK_STICK_S:
parcel.y += dy;
parcel.height = height;
break;
default :
parcel.y += dy / 2;
parcel.height = height;
break;
}
return parcel;
}
/*
* AnchorToSticky --
* Convert a Tk_Anchor enum to a TTK_STICKY bitmask.
*/
static Ttk_Sticky AnchorToSticky(Tk_Anchor anchor)
{
switch (anchor)
{
case TK_ANCHOR_N: return TTK_STICK_N;
case TK_ANCHOR_NE: return TTK_STICK_N | TTK_STICK_E;
case TK_ANCHOR_E: return TTK_STICK_E;
case TK_ANCHOR_SE: return TTK_STICK_S | TTK_STICK_E;
case TK_ANCHOR_S: return TTK_STICK_S;
case TK_ANCHOR_SW: return TTK_STICK_S | TTK_STICK_W;
case TK_ANCHOR_W: return TTK_STICK_W;
case TK_ANCHOR_NW: return TTK_STICK_N | TTK_STICK_W;
default:
case TK_ANCHOR_CENTER: return 0;
}
}
/*
* Ttk_AnchorBox --
* Place a box of size w * h in the specified parcel,
* according to the specified anchor.
*/
Ttk_Box Ttk_AnchorBox(Ttk_Box parcel, int width, int height, Tk_Anchor anchor)
{
return Ttk_StickBox(parcel, width, height, AnchorToSticky(anchor));
}
/*
* Ttk_PlaceBox --
* Combine Ttk_PackBox() and Ttk_StickBox().
*/
Ttk_Box Ttk_PlaceBox(
Ttk_Box *cavity, int width, int height, Ttk_Side side, unsigned sticky)
{
return Ttk_StickBox(
Ttk_PackBox(cavity, width, height, side), width, height, sticky);
}
/*
* Ttk_PositionBox --
* Pack and stick a box according to PositionSpec flags.
*/
TTKAPI Ttk_Box
Ttk_PositionBox(Ttk_Box *cavity, int width, int height, Ttk_PositionSpec flags)
{
Ttk_Box parcel;
if (flags & TTK_EXPAND) parcel = *cavity;
else if (flags & TTK_PACK_TOP) parcel = packTop(cavity, height);
else if (flags & TTK_PACK_LEFT) parcel = packLeft(cavity, width);
else if (flags & TTK_PACK_BOTTOM) parcel = packBottom(cavity, height);
else if (flags & TTK_PACK_RIGHT) parcel = packRight(cavity, width);
else parcel = *cavity;
return Ttk_StickBox(parcel, width, height, flags);
}
/*
* TTKInitPadding --
* Common factor of Ttk_GetPaddingFromObj and Ttk_GetBorderFromObj.
* Initializes Ttk_Padding record, supplying default values
* for missing entries.
*/
static void TTKInitPadding(int padc, int pixels[4], Ttk_Padding *pad)
{
switch (padc)
{
case 0: pixels[0] = 0; /*FALLTHRU*/
case 1: pixels[1] = pixels[0]; /*FALLTHRU*/
case 2: pixels[2] = pixels[0]; /*FALLTHRU*/
case 3: pixels[3] = pixels[1]; /*FALLTHRU*/
}
pad->left = (short)pixels[0];
pad->top = (short)pixels[1];
pad->right = (short)pixels[2];
pad->bottom = (short)pixels[3];
}
/*
* Ttk_GetPaddingFromObj --
*
* Extract a padding specification from a Tcl_Obj * scaled
* to work with a particular Tk_Window.
*
* The string representation of a Ttk_Padding is a list
* of one to four Tk_Pixel specifications, corresponding
* to the left, top, right, and bottom padding.
*
* If the 'bottom' (fourth) element is missing, it defaults to 'top'.
* If the 'right' (third) element is missing, it defaults to 'left'.
* If the 'top' (second) element is missing, it defaults to 'left'.
*
* The internal representation is a Tcl_ListObj containing
* one to four Tk_PixelObj objects.
*
* Returns:
* TCL_OK or TCL_ERROR. In the latter case an error message is
* left in 'interp' and '*paddingPtr' is set to all-zeros.
* Otherwise, *paddingPtr is filled in with the padding specification.
*
*/
int Ttk_GetPaddingFromObj(
Tcl_Interp *interp,
Tk_Window tkwin,
Tcl_Obj *objPtr,
Ttk_Padding *pad)
{
Tcl_Obj **padv;
int i, padc, pixels[4];
if (TCL_OK != Tcl_ListObjGetElements(interp, objPtr, &padc, &padv)) {
goto error;
}
if (padc > 4) {
if (interp) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "Wrong #elements in padding spec", NULL);
}
goto error;
}
for (i=0; i < padc; ++i) {
if (Tk_GetPixelsFromObj(interp, tkwin, padv[i], &pixels[i]) != TCL_OK) {
goto error;
}
}
TTKInitPadding(padc, pixels, pad);
return TCL_OK;
error:
pad->left = pad->top = pad->right = pad->bottom = 0;
return TCL_ERROR;
}
/* Ttk_GetBorderFromObj --
* Same as Ttk_GetPaddingFromObj, except padding is a list of integers
* instead of Tk_Pixel specifications. Does not require a Tk_Window
* parameter.
*
*/
int Ttk_GetBorderFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, Ttk_Padding *pad)
{
Tcl_Obj **padv;
int i, padc, pixels[4];
if (TCL_OK != Tcl_ListObjGetElements(interp, objPtr, &padc, &padv)) {
goto error;
}
if (padc > 4) {
if (interp) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "Wrong #elements in border spec", NULL);
}
goto error;
}
for (i=0; i < padc; ++i) {
if (Tcl_GetIntFromObj(interp, padv[i], &pixels[i]) != TCL_OK) {
goto error;
}
}
TTKInitPadding(padc, pixels, pad);
return TCL_OK;
error:
pad->left = pad->top = pad->right = pad->bottom = 0;
return TCL_ERROR;
}
/*
* Ttk_MakePadding --
* Return an initialized Ttk_Padding structure.
*/
Ttk_Padding Ttk_MakePadding(short left, short top, short right, short bottom)
{
Ttk_Padding pad;
pad.left = left;
pad.top = top;
pad.right = right;
pad.bottom = bottom;
return pad;
}
/*
* Ttk_UniformPadding --
* Returns a uniform Ttk_Padding structure, with the same
* border width on all sides.
*/
Ttk_Padding Ttk_UniformPadding(short borderWidth)
{
Ttk_Padding pad;
pad.left = pad.top = pad.right = pad.bottom = borderWidth;
return pad;
}
/*
* Ttk_AddPadding --
* Combine two padding records.
*/
Ttk_Padding Ttk_AddPadding(Ttk_Padding p1, Ttk_Padding p2)
{
p1.left += p2.left;
p1.top += p2.top;
p1.right += p2.right;
p1.bottom += p2.bottom;
return p1;
}
/* Ttk_RelievePadding --
* Add an extra n pixels of padding according to specified relief.
* This may be used in element geometry procedures to simulate
* a "pressed-in" look for pushbuttons.
*/
Ttk_Padding Ttk_RelievePadding(Ttk_Padding padding, int relief, int n)
{
switch (relief)
{
case TK_RELIEF_RAISED:
padding.right += n;
padding.bottom += n;
break;
case TK_RELIEF_SUNKEN: /* shift */
padding.left += n;
padding.top += n;
break;
default:
{
int h1 = n/2, h2 = h1 + n % 2;
padding.left += h1;
padding.top += h1;
padding.right += h2;
padding.bottom += h2;
break;
}
}
return padding;
}
/*
* Ttk_GetStickyFromObj --
* Returns a stickiness specification from the specified Tcl_Obj*,
* consisting of any combination of n, s, e, and w.
*
* Returns: TCL_OK if objPtr holds a valid stickiness specification,
* otherwise TCL_ERROR. interp is used for error reporting if non-NULL.
*
*/
int Ttk_GetStickyFromObj(
Tcl_Interp *interp, Tcl_Obj *objPtr, Ttk_Sticky *result)
{
const char *string = Tcl_GetString(objPtr);
Ttk_Sticky sticky = 0;
char c;
while ((c = *string++) != '\0') {
switch (c) {
case 'w': case 'W': sticky |= TTK_STICK_W; break;
case 'e': case 'E': sticky |= TTK_STICK_E; break;
case 'n': case 'N': sticky |= TTK_STICK_N; break;
case 's': case 'S': sticky |= TTK_STICK_S; break;
default:
if (interp) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp,
"Bad -sticky specification ",
Tcl_GetString(objPtr),
NULL);
}
return TCL_ERROR;
}
}
*result = sticky;
return TCL_OK;
}
/* Ttk_NewStickyObj --
* Construct a new Tcl_Obj * containing a stickiness specification.
*/
Tcl_Obj *Ttk_NewStickyObj(Ttk_Sticky sticky)
{
char buf[5];
char *p = buf;
if (sticky & TTK_STICK_N) *p++ = 'n';
if (sticky & TTK_STICK_S) *p++ = 's';
if (sticky & TTK_STICK_W) *p++ = 'w';
if (sticky & TTK_STICK_E) *p++ = 'e';
*p = '\0';
return Tcl_NewStringObj(buf, p - buf);
}
/*------------------------------------------------------------------------
* +++ Layout nodes.
*/
struct Ttk_LayoutNode_
{
unsigned flags; /* Packing and sticky flags */
Ttk_ElementImpl element; /* Element implementation */
Ttk_State state; /* Current state */
Ttk_Box parcel; /* allocated parcel */
Ttk_LayoutNode *next, *child;
};
static Ttk_LayoutNode *Ttk_NewLayoutNode(unsigned flags, Ttk_ElementImpl element)
{
Ttk_LayoutNode *node = (Ttk_LayoutNode*)ckalloc(sizeof(Ttk_LayoutNode));
node->flags = flags;
node->element = element;
node->state = 0u;
node->next = node->child = 0;
/* parcel uninitialized */
return node;
}
static void Ttk_FreeLayoutNode(Ttk_LayoutNode *node)
{
while (node) {
Ttk_LayoutNode *next = node->next;
Ttk_FreeLayoutNode(node->child);
ckfree((ClientData)node);
node = next;
}
}
/*------------------------------------------------------------------------
* +++ Layout templates.
*/
struct Ttk_TemplateNode_ {
char *name;
unsigned flags;
struct Ttk_TemplateNode_ *next, *child;
};
static Ttk_TemplateNode *Ttk_NewTemplateNode(const char *name, unsigned flags)
{
Ttk_TemplateNode *op = (Ttk_TemplateNode*)ckalloc(sizeof(*op));
op->name = ckalloc(strlen(name) + 1); strcpy(op->name, name);
op->flags = flags;
op->next = op->child = 0;
return op;
}
void Ttk_FreeLayoutTemplate(Ttk_LayoutTemplate op)
{
while (op) {
Ttk_LayoutTemplate next = op->next;
Ttk_FreeLayoutTemplate(op->child);
ckfree(op->name);
ckfree((ClientData)op);
op = next;
}
}
/* InstantiateLayout --
* Create a layout tree from a template.
*/
static Ttk_LayoutNode *
Ttk_InstantiateLayout(Ttk_Theme theme, Ttk_TemplateNode *op)
{
Ttk_ElementImpl elementImpl = Ttk_GetElement(theme, op->name);
Ttk_LayoutNode *node = Ttk_NewLayoutNode(op->flags, elementImpl);
if (op->next) {
node->next = Ttk_InstantiateLayout(theme,op->next);
}
if (op->child) {
node->child = Ttk_InstantiateLayout(theme,op->child);
}
return node;
}
/*
* Ttk_ParseLayoutTemplate --
* Convert a Tcl list into a layout template.
*
* Syntax:
* layoutSpec ::= { elementName ?-option value ...? }+
*/
/* NB: This must match bit definitions TTK_PACK_LEFT etc. */
static const char *packSideStrings[] =
{ "left", "right", "top", "bottom", NULL };
Ttk_LayoutTemplate Ttk_ParseLayoutTemplate(Tcl_Interp *interp, Tcl_Obj *objPtr)
{
enum { OP_SIDE, OP_STICKY, OP_EXPAND, OP_BORDER, OP_UNIT, OP_CHILDREN };
static const char *optStrings[] = {
"-side", "-sticky", "-expand", "-border", "-unit", "-children", 0 };
int i = 0, objc;
Tcl_Obj **objv;
Ttk_TemplateNode *head = 0, *tail = 0;
if (Tcl_ListObjGetElements(interp, objPtr, &objc, &objv) != TCL_OK)
return 0;
while (i < objc) {
char *elementName = Tcl_GetString(objv[i]);
unsigned flags = 0x0, sticky = TTK_FILL_BOTH;
Tcl_Obj *childSpec = 0;
/*
* Parse options:
*/
++i;
while (i < objc) {
const char *optName = Tcl_GetString(objv[i]);
int option, value;
if (optName[0] != '-')
break;
if (Tcl_GetIndexFromObj(
interp, objv[i], optStrings, "option", 0, &option)
!= TCL_OK)
{
goto error;
}
if (++i >= objc) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp,
"Missing value for option ",Tcl_GetString(objv[i-1]),
NULL);
goto error;
}
switch (option) {
case OP_SIDE: /* <<NOTE-PACKSIDE>> */
if (Tcl_GetIndexFromObj(interp, objv[i], packSideStrings,
"side", 0, &value) != TCL_OK)
{
goto error;
}
flags |= (TTK_PACK_LEFT << value);
break;
case OP_STICKY:
if (Ttk_GetStickyFromObj(interp,objv[i],&sticky) != TCL_OK)
goto error;
break;
case OP_EXPAND:
if (Tcl_GetBooleanFromObj(interp,objv[i],&value) != TCL_OK)
goto error;
if (value)
flags |= TTK_EXPAND;
break;
case OP_BORDER:
if (Tcl_GetBooleanFromObj(interp,objv[i],&value) != TCL_OK)
goto error;
if (value)
flags |= TTK_BORDER;
break;
case OP_UNIT:
if (Tcl_GetBooleanFromObj(interp,objv[i],&value) != TCL_OK)
goto error;
if (value)
flags |= TTK_UNIT;
break;
case OP_CHILDREN:
childSpec = objv[i];
break;
}
++i;
}
/*
* Build new node:
*/
if (tail) {
tail->next = Ttk_NewTemplateNode(elementName, flags | sticky);
tail = tail->next;
} else {
head = tail = Ttk_NewTemplateNode(elementName, flags | sticky);
}
if (childSpec) {
tail->child = Ttk_ParseLayoutTemplate(interp, childSpec);
if (!tail->child) {
goto error;
}
}
}
return head;
error:
Ttk_FreeLayoutTemplate(head);
return 0;
}
/* Ttk_BuildLayoutTemplate --
* Build a layout template tree from a statically defined
* Ttk_LayoutSpec array.
*/
Ttk_LayoutTemplate Ttk_BuildLayoutTemplate(Ttk_LayoutSpec spec)
{
Ttk_TemplateNode *first = 0, *last = 0;
for ( ; !(spec->opcode & _TTK_LAYOUT_END) ; ++spec) {
if (spec->elementName) {
Ttk_TemplateNode *node =
Ttk_NewTemplateNode(spec->elementName, spec->opcode);
if (last) {
last->next = node;
} else {
first = node;
}
last = node;
}
if (spec->opcode & _TTK_CHILDREN) {
int depth = 1;
last->child = Ttk_BuildLayoutTemplate(spec+1);
/* Skip to end of group:
*/
while (depth) {
++spec;
if (spec->opcode & _TTK_CHILDREN) {
++depth;
}
if (spec->opcode & _TTK_LAYOUT_END) {
--depth;
}
}
}
} /* for */
return first;
}
void Ttk_RegisterLayouts(Ttk_Theme theme, Ttk_LayoutSpec spec)
{
while (!(spec->opcode & _TTK_LAYOUT_END)) {
Ttk_LayoutTemplate layoutTemplate = Ttk_BuildLayoutTemplate(spec+1);
Ttk_RegisterLayoutTemplate(theme, spec->elementName, layoutTemplate);
do {
++spec;
} while (!(spec->opcode & _TTK_LAYOUT));
}
}
Tcl_Obj *Ttk_UnparseLayoutTemplate(Ttk_TemplateNode *node)
{
Tcl_Obj *result = Tcl_NewListObj(0,0);
# define APPENDOBJ(obj) Tcl_ListObjAppendElement(NULL, result, obj)
# define APPENDSTR(str) APPENDOBJ(Tcl_NewStringObj(str,-1))
while (node) {
unsigned flags = node->flags;
APPENDSTR(node->name);
/* Back-compute -side. <<NOTE-PACKSIDE>>
* @@@ NOTES: Ick.
*/
if (flags & TTK_EXPAND) {
APPENDSTR("-expand");
APPENDSTR("1");
} else {
if (flags & _TTK_MASK_PACK) {
int side = 0;
unsigned sideFlags = flags & _TTK_MASK_PACK;
while ((sideFlags & TTK_PACK_LEFT) == 0) {
++side;
sideFlags >>= 1;
}
APPENDSTR("-side");
APPENDSTR(packSideStrings[side]);
}
}
/* In Ttk_ParseLayoutTemplate, default -sticky is "nsew",
* so always include this even if no sticky bits are set.
*/
APPENDSTR("-sticky");
APPENDOBJ(Ttk_NewStickyObj(flags & _TTK_MASK_STICK));
/* @@@ Check again: are these necessary? */
if (flags & TTK_BORDER) { APPENDSTR("-border"); APPENDSTR("1"); }
if (flags & TTK_UNIT) { APPENDSTR("-unit"); APPENDSTR("1"); }
if (node->child) {
APPENDSTR("-children");
APPENDOBJ(Ttk_UnparseLayoutTemplate(node->child));
}
node = node->next;
}
# undef APPENDOBJ
# undef APPENDSTR
return result;
}
/*------------------------------------------------------------------------
* +++ Layouts.
*/
struct Ttk_Layout_
{
Ttk_Style style;
void *recordPtr;
Tk_OptionTable optionTable;
Tk_Window tkwin;
Ttk_LayoutNode *root;
};
static Ttk_Layout TTKNewLayout(
Ttk_Style style,
void *recordPtr,Tk_OptionTable optionTable, Tk_Window tkwin,
Ttk_LayoutNode *root)
{
Ttk_Layout layout = (Ttk_Layout)ckalloc(sizeof(*layout));
layout->style = style;
layout->recordPtr = recordPtr;
layout->optionTable = optionTable;
layout->tkwin = tkwin;
layout->root = root;
return layout;
}
void Ttk_FreeLayout(Ttk_Layout layout)
{
Ttk_FreeLayoutNode(layout->root);
ckfree((ClientData)layout);
}
/*
* Ttk_CreateLayout --
* Create a layout from the specified theme and style name.
* Returns: New layout, 0 on error.
* Leaves an error message in interp's result if there is an error.
*/
Ttk_Layout Ttk_CreateLayout(
Tcl_Interp *interp, /* where to leave error messages */
Ttk_Theme themePtr,
const char *styleName,
void *recordPtr,
Tk_OptionTable optionTable,
Tk_Window tkwin)
{
Ttk_Style style = Ttk_GetStyle(themePtr, styleName);
Ttk_LayoutTemplate layoutTemplate =
Ttk_FindLayoutTemplate(themePtr,styleName);
Ttk_ElementImpl bgelement = Ttk_GetElement(themePtr, "background");
Ttk_LayoutNode *bgnode;
if (!layoutTemplate) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "Layout ", styleName, " not found", NULL);
return 0;
}
bgnode = Ttk_NewLayoutNode(TTK_FILL_BOTH, bgelement);
bgnode->next = Ttk_InstantiateLayout(themePtr, layoutTemplate);
return TTKNewLayout(style, recordPtr, optionTable, tkwin, bgnode);
}
/* Ttk_CreateSublayout --
* Creates a new sublayout.
*
* Sublayouts are used to draw subparts of a compound widget.
* They use the same Tk_Window, but a different option table
* and data record.
*/
Ttk_Layout
Ttk_CreateSublayout(
Tcl_Interp *interp,
Ttk_Theme themePtr,
Ttk_Layout parentLayout,
const char *baseName,
Tk_OptionTable optionTable)
{
Tcl_DString buf;
const char *styleName;
Ttk_Style style;
Ttk_LayoutTemplate layoutTemplate;
Tcl_DStringInit(&buf);
Tcl_DStringAppend(&buf, Ttk_StyleName(parentLayout->style), -1);
Tcl_DStringAppend(&buf, baseName, -1);
styleName = Tcl_DStringValue(&buf);
style = Ttk_GetStyle(themePtr, styleName);
layoutTemplate = Ttk_FindLayoutTemplate(themePtr, styleName);
if (!layoutTemplate) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "Layout ", styleName, " not found", NULL);
return 0;
}
Tcl_DStringFree(&buf);
return TTKNewLayout(
style, 0, optionTable, parentLayout->tkwin,
Ttk_InstantiateLayout(themePtr, layoutTemplate));
}
/* Ttk_RebindSublayout --
* Bind sublayout to new data source.
*/
void Ttk_RebindSublayout(Ttk_Layout layout, void *recordPtr)
{
layout->recordPtr = recordPtr;
}
/*
* Ttk_QueryOption --
* Look up an option from a layout's associated option.
*/
Tcl_Obj *Ttk_QueryOption(
Ttk_Layout layout, const char *optionName, Ttk_State state)
{
return Ttk_QueryStyle(
layout->style,layout->recordPtr,layout->optionTable,optionName,state);
}
/*------------------------------------------------------------------------
* +++ Size computation.
*/
static void Ttk_NodeListSize(
Ttk_Layout layout, Ttk_LayoutNode *node,
Ttk_State state, int *widthPtr, int *heightPtr); /* Forward */
static void Ttk_NodeSize(
Ttk_Layout layout, Ttk_LayoutNode *node, Ttk_State state,
int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
int elementWidth, elementHeight, subWidth, subHeight;
Ttk_Padding elementPadding;
Ttk_ElementSize(node->element,
layout->style, layout->recordPtr,layout->optionTable, layout->tkwin,
state|node->state,
&elementWidth, &elementHeight, &elementPadding);
Ttk_NodeListSize(layout,node->child,state,&subWidth,&subHeight);
subWidth += Ttk_PaddingWidth(elementPadding);
subHeight += Ttk_PaddingHeight(elementPadding);
*widthPtr = MAX(elementWidth, subWidth);
*heightPtr = MAX(elementHeight, subHeight);
*paddingPtr = elementPadding;
}
static void Ttk_NodeListSize(
Ttk_Layout layout, Ttk_LayoutNode *node,
Ttk_State state, int *widthPtr, int *heightPtr)
{
if (!node) {
*widthPtr = *heightPtr = 0;
} else {
int width, height, restWidth, restHeight;
Ttk_Padding unused;
Ttk_NodeSize(layout, node, state, &width, &height, &unused);
Ttk_NodeListSize(layout, node->next, state, &restWidth, &restHeight);
if (node->flags & (TTK_PACK_LEFT|TTK_PACK_RIGHT)) {
*widthPtr = width + restWidth;
} else {
*widthPtr = MAX(width, restWidth);
}
if (node->flags & (TTK_PACK_TOP|TTK_PACK_BOTTOM)) {
*heightPtr = height + restHeight;
} else {
*heightPtr = MAX(height, restHeight);
}
}
}
/*
* Ttk_LayoutNodeInternalPadding --
* Returns the internal padding of a layout node.
*/
Ttk_Padding Ttk_LayoutNodeInternalPadding(
Ttk_Layout layout, Ttk_LayoutNode *node)
{
int unused;
Ttk_Padding padding;
Ttk_ElementSize(node->element,
layout->style, layout->recordPtr, layout->optionTable, layout->tkwin,
0/*state*/, &unused, &unused, &padding);
return padding;
}
/*
* Ttk_LayoutNodeInternalParcel --
* Returns the inner area of a specified layout node,
* based on current parcel and element's internal padding.
*/
Ttk_Box Ttk_LayoutNodeInternalParcel(Ttk_Layout layout, Ttk_LayoutNode *node)
{
Ttk_Padding padding = Ttk_LayoutNodeInternalPadding(layout, node);
return Ttk_PadBox(node->parcel, padding);
}
/* Ttk_LayoutSize --
* Compute requested size of a layout.
*/
void Ttk_LayoutSize(
Ttk_Layout layout, Ttk_State state, int *widthPtr, int *heightPtr)
{
Ttk_NodeListSize(layout, layout->root, state, widthPtr, heightPtr);
}
void Ttk_LayoutNodeReqSize( /* @@@ Rename this */
Ttk_Layout layout, Ttk_LayoutNode *node, int *widthPtr, int *heightPtr)
{
Ttk_Padding unused;
Ttk_NodeSize(layout, node, 0/*state*/, widthPtr, heightPtr, &unused);
}
/*------------------------------------------------------------------------
* +++ Layout placement.
*/
/* Ttk_PlaceNodeList --
* Compute parcel for each node in a layout tree
* according to position specification and overall size.
*/
static void Ttk_PlaceNodeList(
Ttk_Layout layout, Ttk_LayoutNode *node, Ttk_State state, Ttk_Box cavity)
{
for (; node; node = node->next)
{
int width, height;
Ttk_Padding padding;
/* Compute node size: (@@@ cache this instead?)
*/
Ttk_NodeSize(layout, node, state, &width, &height, &padding);
/* Compute parcel:
*/
node->parcel = Ttk_PositionBox(&cavity, width, height, node->flags);
/* Place child nodes:
*/
if (node->child) {
Ttk_Box childBox = Ttk_PadBox(node->parcel, padding);
Ttk_PlaceNodeList(layout,node->child, state, childBox);
}
}
}
void Ttk_PlaceLayout(Ttk_Layout layout, Ttk_State state, Ttk_Box b)
{
Ttk_PlaceNodeList(layout, layout->root, state, b);
}
/*------------------------------------------------------------------------
* +++ Layout drawing.
*/
/*
* Ttk_DrawLayout --
* Draw a layout tree.
*/
static void Ttk_DrawNodeList(
Ttk_Layout layout, Ttk_State state, Ttk_LayoutNode *node, Drawable d)
{
for (; node; node = node->next)
{
int border = node->flags & TTK_BORDER;
int substate = state;
if (node->flags & TTK_UNIT)
substate |= node->state;
if (node->child && border)
Ttk_DrawNodeList(layout, substate, node->child, d);
Ttk_DrawElement(
node->element,
layout->style,layout->recordPtr,layout->optionTable,layout->tkwin,
d, node->parcel, state | node->state);
if (node->child && !border)
Ttk_DrawNodeList(layout, substate, node->child, d);
}
}
void Ttk_DrawLayout(Ttk_Layout layout, Ttk_State state, Drawable d)
{
Ttk_DrawNodeList(layout, state, layout->root, d);
}
/*------------------------------------------------------------------------
* +++ Inquiry and modification.
*/
/*
* Ttk_LayoutIdentify --
* Find the layout node at the specified x,y coordinate.
*/
static Ttk_LayoutNode *
Ttk_LayoutNodeIdentify(Ttk_LayoutNode *node, int x, int y)
{
Ttk_LayoutNode *closest = NULL;
for (; node; node = node->next) {
if (Ttk_BoxContains(node->parcel, x, y)) {
closest = node;
if (node->child && !(node->flags & TTK_UNIT)) {
Ttk_LayoutNode *childNode =
Ttk_LayoutNodeIdentify(node->child, x,y);
if (childNode) {
closest = childNode;
}
}
}
}
return closest;
}
Ttk_LayoutNode *Ttk_LayoutIdentify(Ttk_Layout layout, int x, int y)
{
return Ttk_LayoutNodeIdentify(layout->root, x, y);
}
/*
* tail --
* Return the last component of an element name, e.g.,
* "Scrollbar.thumb" => "thumb"
*/
static const char *tail(const char *elementName)
{
const char *dot;
while ((dot=strchr(elementName,'.')) != NULL)
elementName = dot + 1;
return elementName;
}
/*
* Ttk_LayoutFindNode --
* Look up a layout node by name.
*/
static Ttk_LayoutNode *
Ttk_LayoutNodeFind(Ttk_LayoutNode *node, const char *nodeName)
{
for (; node ; node = node->next) {
if (!strcmp(tail(Ttk_LayoutNodeName(node)), nodeName))
return node;
if (node->child) {
Ttk_LayoutNode *childNode =
Ttk_LayoutNodeFind(node->child, nodeName);
if (childNode)
return childNode;
}
}
return 0;
}
Ttk_LayoutNode *Ttk_LayoutFindNode(Ttk_Layout layout, const char *nodeName)
{
return Ttk_LayoutNodeFind(layout->root, nodeName);
}
const char *Ttk_LayoutNodeName(Ttk_LayoutNode *node)
{
return Ttk_ElementName(node->element);
}
Ttk_Box Ttk_LayoutNodeParcel(Ttk_LayoutNode *node)
{
return node->parcel;
}
void Ttk_PlaceLayoutNode(Ttk_Layout layout, Ttk_LayoutNode *node, Ttk_Box b)
{
node->parcel = b;
if (node->child) {
Ttk_PlaceNodeList(layout, node->child, 0,
Ttk_PadBox(b, Ttk_LayoutNodeInternalPadding(layout, node)));
}
}
void Ttk_ChangeElementState(Ttk_LayoutNode *node,unsigned set,unsigned clr)
{
node->state = (node->state | set) & ~clr;
}
/*EOF*/
|