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
|
/*LINTLIBRARY*/
/*
* SCCS_data: @(#) TableVec.c 1.8 93/01/29 08:10:55
*/
#include <X11/Xp/COPY>
#include <X11/IntrinsicP.h>
#ifdef sun
#include <X11/ObjectP.h> /* why don't they just use X from mit!?! */
#include <X11/RectObjP.h>
#endif
#include <X11/Xp/TableP.h>
/* TableVector Methods
**=====================**
Each Table instance has two TableVectors: one describes the columns, and the
other describes the rows.
The table vectors are created based on information in the real_layout
TableLoc array, hence they must be created after the real_layout, and they
must be updated when the real_layout changes. The real_layout data upon
which the vectors depend is: number of cols, number of rows, options (only
TBL_SM_WIDTH and TBL_SM_HEIGHT).
*/
XpTableVector XpTableVectorNew( size, tw, do_col )
int size;
XpTableWidget tw;
int do_col;
{
int minimize, dontStretch, first_slot, last_slot, slot;
XpTableLoc loc = tw->table.real_layout;
XpTableVector vec;
if ( (XpTableLoc)0 == loc || 0 == size )
return (XpTableVector)0;
vec = (XpTableVector) XtCalloc( size+1, sizeof(XpTableVectorRec));
/* Determine which slots need to be minimized
*/
for ( ; loc->w ; loc++ )
{
if ( do_col )
{
minimize = loc->options & TBL_SM_WIDTH;
dontStretch = loc->options & TBL_LK_WIDTH;
first_slot = loc->col;
last_slot = loc->col + loc->col_span;
}
else
{
minimize = loc->options & TBL_SM_HEIGHT;
dontStretch = loc->options & TBL_LK_HEIGHT;
first_slot = loc->row;
last_slot = loc->row + loc->row_span;
}
if ( minimize )
for ( slot = first_slot ; slot < last_slot ; slot++ )
vec[ slot ].options |= TBL_VEC_MINIMIZE;
if ( dontStretch )
for ( slot = first_slot ; slot < last_slot ; slot++ )
vec[ slot ].options |= TBL_VEC_LOCK;
}
XpTableVectorMinimize( vec, size, tw, do_col );
return vec;
}
void XpTableVectorFree( vec )
XpTableVector vec ;
{
XtFree( (char*)vec );
}
/* Minimize Column Widths and Row Heights
**========================================**
Change the vector to be its minimum size in the direction indicated.
If TBL_VEC_MINIMIZE (i.e., TBL_SM_WIDTH (W) or TBL_SM_HEIGHT (H)) then
the widget is kept to its original size.
TBL_VEC_LOCK (i.e., TBL_LK_WIDTH (w) or TBL_LK_HEIGHT (h) ) not checked,
because such widgets DO grow to the minimum size of the column or row.
*/
void XpTableVectorMinimize( vec, vec_len, tw, do_col )
XpTableVector vec;
int vec_len;
XpTableWidget tw;
int do_col;
{
int i;
XpTableLoc loc = tw->table.real_layout;
Widget resize_child = tw->table.resize_child;
int resize_width = tw->table.resize_width;
int resize_height = tw->table.resize_height;
int resize_border_width = tw->table.resize_border_width;
if ( (XpTableVector)0 == vec || 0 == vec_len )
return;
/* Sort real_layout (in-place) by the number of columns or rows each child
** spans so we first compute sizes of individual columns or rows, then
** compute the spanned columns or rows.
*/
if ( do_col )
qsort( (char*)loc, /* sort real_layout */
XpTableLocLen(loc),
sizeof(XpTableLocRec),
XpTableLocCompareColSpan ); /* compare column spans */
else
qsort( (char*)loc, /* sort real_layout */
XpTableLocLen(loc),
sizeof(XpTableLocRec),
XpTableLocCompareRowSpan ); /* compare row spans */
/* Reset all width|heights to zero, then expand to fit
*/
for ( i = 0 ; i < vec_len ; i++ )
vec[i].value = 0;
for ( ; loc->w ; loc++ )
{
int pref;
/* Check for simple case (span of 1), where col or row just becomes
** large enough for largest child in that col or row.
*/
if ( do_col && loc->col_span == 1 )
{
pref = XpTableLocPreferredWidth( loc, tw );
if ( pref > vec[ loc->col ].value )
vec[ loc->col ].value = pref;
}
else if ( !do_col && loc->row_span == 1 )
{
pref = XpTableLocPreferredHeight( loc, tw );
if ( pref > vec[ loc->row ].value )
vec[ loc->row ].value = pref;
}
else
{
/* Spans multiple columns or rows. We have already set each
** column or row to the individual size requirements, now we can
** see which spanned columns or rows need to be stretched. The
** span width includes inter-column or inter-row spacing.
*/
int to_stretch, span_size, first_slot, stop_before, slot;
int can_stretch = 0;
if ( do_col )
{
span_size = tw->table.col_spacing * (loc->col_span-1);
first_slot = loc->col;
stop_before = loc->col + loc->col_span;
}
else
{
span_size = tw->table.row_spacing * (loc->row_span-1);
first_slot = loc->row;
stop_before = loc->row + loc->row_span;
}
for ( slot = first_slot ; slot < stop_before ; slot++ )
{
if ( 0 == (vec[ slot ].options & TBL_VEC_LOCK) )
can_stretch++;
span_size += vec[ slot ].value;
}
/* If none of the slots can stretch, then we still must force
** them all to stretch at least to the orig_size of this widget.
*/
if ( 0 == can_stretch )
{
if ( do_col )
{
to_stretch = loc->col_span;
if ( loc->w == resize_child )
pref = resize_width + 2 * resize_border_width;
else
pref = loc->orig_width + 2 * loc->orig_border_width;
}
else
{
to_stretch = loc->row_span;
if ( loc->w == resize_child )
pref = resize_height + 2 * resize_border_width;
else
pref = loc->orig_height + 2 * loc->orig_border_width;
}
}
else
{
to_stretch = can_stretch;
if ( do_col )
{
pref = XpTableLocPreferredWidth( loc, tw );
}
else
{
pref = XpTableLocPreferredHeight( loc, tw );
}
}
if ( span_size < pref )
{
/* Increase size of some or all slots: if nothing
** can stretch, expand every slot, else expand only
** those which are not locked small.
*/
int excess = pref - span_size;
int amt = excess / to_stretch;
int truncated = excess - amt*to_stretch;
for ( slot = first_slot ; slot < stop_before ; slot++ )
{
if ( 0 == can_stretch
|| 0 == (vec[ slot ].options & TBL_VEC_LOCK) )
{
if ( truncated )
{
vec[ slot ].value += amt + 1;
--truncated;
}
else
vec[ slot ].value += amt;
}
}
}
}
}
/* The vector is minimized: set pref_value from value
*/
for ( i = 0 ; i < vec_len ; i++ )
vec[i].pref_value = vec[i].value;
}
/* Total Width or Height
**=======================**
Including inter-column and inter-row spacing, and margins. Works
even when there are no columns or rows (vec==num==0).
*/
#define DO_ACTUAL 1
#define DO_PREFERRED 0
static int XpTableVectorSize( vec, num, tw, do_col, do_actual )
XpTableVector vec;
int num;
XpTableWidget tw;
int do_col;
int do_actual;
{
int slot, size, space;
if (do_col)
{
space = tw->table.col_spacing;
size = 2*tw->table.margin_width;
}
else
{
space = tw->table.row_spacing;
size = 2*tw->table.margin_height;
}
if ( 0 != num && (XpTableVector)0 != vec )
{
if (do_actual)
{
for ( size -= space, slot = 0 ; slot < num ; slot++ )
size += vec[ slot ].value + space;
}
else
{
for ( size -= space, slot = 0 ; slot < num ; slot++ )
size += vec[ slot ].pref_value + space;
}
}
if ( size > 0 )
return size;
else
return 1; /* minimum size */
}
int XpTableVectorTotalSize( vec, num, tw, do_col )
XpTableVector vec;
int num;
XpTableWidget tw;
int do_col;
{
return XpTableVectorSize( vec, num, tw, do_col, DO_ACTUAL );
}
int XpTableVectorPreferredSize( vec, num, tw, do_col )
XpTableVector vec;
int num;
XpTableWidget tw;
int do_col;
{
return XpTableVectorSize( vec, num, tw, do_col, DO_PREFERRED );
}
#undef DO_ACTUAL
#undef DO_PREFERRED
/* Adjust rows or columns
**========================**
When a parent re-sizes a Table, it can make it larger or smaller. The
adjustment is distributed as a ratio of the preferred sizes of the
col/row, so small ones change slower than larger ones.
If the table wants to restrict making things smaller than preferred,
then it must simply never respond to resize commands which make
the table smaller than its preferred size. Nowhere in the logic below
is there any mechanism which prevents things from shrinking smaller
than the preferred size. There is, however, mechanisms to prevent any
col or row from becoming smaller than 1.
If resize makes the Table larger than before: First, all col/row
smaller that preferred size are stretched up until their preferred
size. The rest of the change is distributed evenly to un-locked col/row,
but if all are locked, then all are stretched.
If the table is being made smaller, then the inverse is applied: all
unlocked (or all if all are locked) are made smaller down to their
preferred sizes, then all are made smaller by the same amount.
Adjustments to the vectors are made on a relative basis. Big slots
change more than small slots. Therefore, the adjustment delta is
computed for each slot which might change.
While adjusting the vectors, there are too things to watch out for: lots of
slots to change, yet not much change, integer truncation then leaves the
delta zero. In this case we make the delta 1, which means the change gets
used up before all the slots are seen. We use the same algorithm for
growing and shrinking, so there should be no perceivable problems. The
second problem is when the delta would consume too much change, again due
to integer truncation. In this case, we must simply make the delta equal
to the remaining change.
Notice that there are two resize algorithms used: one applies when everything
is smaller than preferred, and another applies when everything is bigger.
When smaller, everything gets changed relatively. When larger, change
is influenced by the table slot being locked (TBL_VEC_LOCK). Slots which
are locked are not adjusted unless ALL slots are locked, then all are
adjusted relatively.
*/
void XpTableVectorAdjust( vec, vec_len, change )
XpTableVector vec;
int vec_len, change;
{
int vec_inx, remaining, amt;
int total_pref;
int can_change, can_change_pref;
int too_small;
int too_big, too_big_pref;
if ( (XpTableVector)0 == vec || 0 == vec_len || 0 == change )
return;
total_pref = can_change = can_change_pref = 0;
too_small = too_big = too_big_pref = 0;
for ( vec_inx=0 ; vec_inx < vec_len ; vec_inx++ )
{
/* NOTE: total_pref can be zero if all pref_value are 0!
*/
total_pref += vec[ vec_inx ].pref_value;
if ( change > 0 )
{
if ( 0 == ( vec[ vec_inx ].options & TBL_VEC_LOCK ) )
{
/* NOTE: can_change_pref can be zero if all pref_value are 0!
*/
can_change++;
can_change_pref += vec[ vec_inx ].pref_value;
}
if ( vec[ vec_inx ].value < vec[ vec_inx ].pref_value )
{
too_small++;
}
}
else
{
if ( vec[ vec_inx ].value > vec[ vec_inx ].pref_value )
{
/* NOTE: too_big_pref can be zero if all pref_value are 0!
*/
too_big++;
too_big_pref += vec[ vec_inx ].pref_value;
}
}
}
if ( change > 0 )
{
/**************** Make columns wider or rows taller ***************
*/
int still_too_small;
remaining = change;
do
{
/* Expand everything smaller than preferred up to preferred
*/
still_too_small = 0;
for ( vec_inx=0 ; vec_inx < vec_len ; vec_inx++ )
{
if ( vec[ vec_inx ].value < vec[ vec_inx ].pref_value )
{
/* Make this one bigger, up to preferred size
*/
if ( 0 == total_pref )
amt = change / (too_small?too_small:vec_len);
else
amt = change * vec[ vec_inx ].pref_value / total_pref;
if ( 0 == amt )
amt = 1;
else if ( remaining < amt )
amt = remaining;
if ( vec[vec_inx].value + amt
< vec[vec_inx].pref_value )
{
vec[ vec_inx ].value += amt;
++still_too_small;
}
else
{
amt = vec[vec_inx].pref_value - vec[vec_inx].value;
vec[vec_inx].value = vec[vec_inx].pref_value;
}
remaining -= amt;
if ( remaining <= 0 )
return; /* used up all change */
}
}
change = remaining;
}
while ( still_too_small );
/* All are at least preferred size, and there is change remaining.
* If none of the vector slots can stretch, then we still must
* force them all to stretch.
*/
if ( 0 == can_change )
can_change_pref = total_pref; /* maintain relative sizes */
do
{
for ( vec_inx = 0 ; vec_inx < vec_len ; vec_inx++ )
{
if ( 0 == can_change
|| 0 == ( vec[ vec_inx ].options & TBL_VEC_LOCK ) )
{
/* Add relative amount to all which can change.
*/
if ( 0 == can_change_pref )
amt = change / (can_change?can_change:vec_len);
else
amt = change * vec[vec_inx].pref_value/can_change_pref;
if ( 0 == amt )
amt = 1;
else if ( remaining < amt )
amt = remaining;
vec[ vec_inx ].value += amt;
remaining -= amt;
if ( remaining <= 0 )
return; /* used up all change */
}
}
/* We have gone thru vector, adding space, but due to truncation
* there may still be more change to distribute.
*/
change = remaining;
}
while ( 1 ); /* until remaining goes to zero or negative above */
/*NOTREACHED*/
}
else /* (change < 0) */
{
/**************** Make columns narrower or rows shorter ***************
*/
int still_too_big, num_larger_than_1;
/* For conceptual clarity, switch the sign on change
*/
change = -change;
remaining = change;
still_too_big = too_big;
while ( still_too_big )
{
/* Shrink all which are larger than preferred
*/
still_too_big = 0;
for ( vec_inx = 0 ; vec_inx < vec_len ; vec_inx++ )
{
if ( vec[ vec_inx ].value > vec[ vec_inx ].pref_value )
{
if ( 0 == too_big_pref )
amt = change / (too_big?too_big:vec_len);
else
amt = change * vec[vec_inx].pref_value / too_big_pref;
if ( 0 == amt )
amt = 1;
else if ( remaining < amt )
amt = remaining;
if ( vec[ vec_inx ].value - amt < vec[ vec_inx ].pref_value)
{
amt = vec[ vec_inx ].value - vec[ vec_inx ].pref_value;
vec[ vec_inx ].value = vec[ vec_inx ].pref_value;
}
else
{
vec[ vec_inx ].value -= amt;
still_too_big++;
}
remaining -= amt;
if ( remaining <= 0 )
return; /* used up all change */
}
}
/* We have made a pass through all slots
*/
change = remaining;
}
/* Now all stretchable are preferred sizes, or all were already smaller
* than preferred sizes, yet more change is remaining to be absorbed.
*
* Shrink evenly, but since none can become smaller than 1, we may need
* to make multiple passes over vector until total change is absorbed,
* or all are of size 1.
*/
do
{
num_larger_than_1 = 0;
for ( vec_inx = 0 ; vec_inx < vec_len ; vec_inx++ )
{
if ( 0 == total_pref )
amt = change / vec_len;
else
amt = change * vec[vec_inx].pref_value / total_pref;
if ( 0 == amt )
amt = 1;
else if ( remaining < amt )
amt = remaining;
if ( amt < vec[vec_inx].value )
{
vec[vec_inx].value -= amt;
++num_larger_than_1;
}
else
{
amt = vec[vec_inx].value - 1;
vec[vec_inx].value = 1;
}
remaining -= amt;
if ( remaining <= 0 )
return; /* used up all change */
}
/* We have made a pass through all slots
*/
change = remaining;
}
while ( num_larger_than_1 );
return; /* all are shrunk to absolute minimum size (1) */
}
/*NOTREACHED*/
}
/* Set Upper Left Corner Coordinates of Each Cell
**================================================**
Note that it is not worth doing this until the actual correct size of
the rows and columns have been computed.
*/
void XpTableVectorComputeOffsets( vec, vec_len, margin, gap )
XpTableVector vec;
int vec_len, margin, gap;
{
int i;
int offset = margin;
if ( (XpTableVector)0 == vec || 0 == vec_len )
return;
for ( i = 0 ; i < vec_len ; i++ )
{
vec[i].offset = offset;
offset = offset + vec[i].value + gap;
}
}
|