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
|
/* EBox.c */
#define NDEBUG 1
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef NDEBUG
#include <stdio.h>
#endif
#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#include <X11/Xmu/Misc.h>
#include <X11/Xaw/BoxP.h>
#include <X11/Xaw/XawInit.h>
#include "VBox.h"
#include "VBoxP.h"
/*
* Class Methods
*/
static void VBoxChangeManaged(Widget);
static XtGeometryResult VBoxGeometryManager(Widget, XtWidgetGeometry*,
XtWidgetGeometry*);
static XtGeometryResult VBoxQueryGeometry(Widget, XtWidgetGeometry*,
XtWidgetGeometry*);
static void VBoxResize(Widget);
#define UNUSED __attribute__ ((unused))
#if __GNUC__ > 3
#define SENTINEL __attribute__ ((sentinel(0)))
#else
#define SENTINEL
#endif
/*
* Initialization
*/
static XtResource vbox_resources[] = {
{ XtNresize, XtCResize, XtRBoolean,
sizeof(Boolean), XtOffsetOf(VBoxRec, vbox.resize),
XtRImmediate, (XtPointer)False
}
};
VBoxClassRec vboxClassRec = {
/* core */
{
(WidgetClass)&boxClassRec, /* superclass */
"VBox", /* class_name */
sizeof(VBoxRec), /* widget_size */
NULL, /* class_initialize */
NULL, /* class_part_init */
False, /* class_inited */
NULL, /* initialize */
NULL, /* initialize_hook */
XtInheritRealize, /* realize */
NULL, /* actions */
0, /* num_actions */
vbox_resources, /* resources */
XtNumber(vbox_resources), /* num_resources */
NULLQUARK, /* xrm_class */
True, /* compress_motion */
True, /* compress_exposure */
True, /* compress_enterleave */
False, /* visible_interest */
NULL, /* destroy */
VBoxResize, /* resize */
XtInheritExpose, /* expose */
NULL, /* set_values */
NULL, /* set_values_hook */
XtInheritSetValuesAlmost, /* set_values_almost */
NULL, /* get_values_hook */
NULL, /* accept_focus */
XtVersion, /* version */
NULL, /* callback_private */
NULL, /* tm_table */
VBoxQueryGeometry, /* query_geometry */
XtInheritDisplayAccelerator, /* display_accelerator */
NULL, /* extension */
},
/* composite */
{
VBoxGeometryManager, /* geometry_manager */
VBoxChangeManaged, /* change_managed */
XtInheritInsertChild, /* insert_child */
XtInheritDeleteChild, /* delete_child */
NULL, /* extension */
},
/* box */
{
NULL, /* extension */
},
/* vbox */
{
NULL, /* extension */
},
};
WidgetClass vboxWidgetClass = (WidgetClass)&vboxClassRec;
#define REQUESTS_X(gr) (((gr)->request_mode & CWX ) != 0)
#define REQUESTS_Y(gr) (((gr)->request_mode & CWY ) != 0)
#define REQUESTS_HEIGHT(gr) (((gr)->request_mode & CWHeight ) != 0)
#define REQUESTS_WIDTH(gr) (((gr)->request_mode & CWWidth ) != 0)
#define REQUESTS_BORDER(gr) (((gr)->request_mode & CWBorderWidth ) != 0)
#define QUERY_ONLY(gr) (((gr)->request_mode & XtCWQueryOnly ) != 0)
#define setmax(var,val) {if( var < val ) var = val;}
#ifndef XtX
#define XtX(w) (((RectObj)w)->rectangle.x)
#endif
#ifndef XtY
#define XtY(w) (((RectObj)w)->rectangle.y)
#endif
#ifndef XtWidth
#define XtWidth(w) (((RectObj)w)->rectangle.width)
#endif
#ifndef XtHeight
#define XtHeight(w) (((RectObj)w)->rectangle.height)
#endif
#ifndef XtBorderWidth
#define XtBorderWidth(w) (((RectObj)w)->rectangle.border_width)
#endif
/*
* Calculate preferred size, given constraining box, caching it in the widget
*/
static XtGeometryResult
VBoxQueryGeometry(Widget wid, XtWidgetGeometry *constraint,
XtWidgetGeometry *preferred)
{
VBoxWidget w = (VBoxWidget)wid;
Dimension width, height;
XtWidgetGeometry suggested;
Cardinal i;
#ifndef NDEBUG
fprintf(stderr, "VBoxQueryGeometry(%p, ", wid);
if( REQUESTS_X(constraint) )
fprintf(stderr, "x=%hu ", constraint->x);
if( REQUESTS_Y(constraint) )
fprintf(stderr, "y=%hu ", constraint->y);
if( REQUESTS_WIDTH(constraint) )
fprintf(stderr, "width=%hu ", constraint->width);
if( REQUESTS_HEIGHT(constraint) )
fprintf(stderr, "height=%hu ", constraint->height);
if( REQUESTS_BORDER(constraint) )
fprintf(stderr, "border=%hu ", constraint->border_width);
if( QUERY_ONLY(constraint) )
fprintf(stderr, "query-only ");
fprintf(stderr, ")\n");
#endif // NDEBUG
constraint->request_mode &= CWWidth | CWHeight;
if (constraint->request_mode == 0)
/* parent isn't going to change w or h, so nothing to re-compute */
return (XtGeometryYes);
if (constraint->request_mode == w->box.last_query_mode
&& (!(constraint->request_mode & CWWidth)
|| constraint->width == w->box.last_query_width)
&& (!(constraint->request_mode & CWHeight)
|| constraint->height == w->box.last_query_height)) {
/* same query; current preferences are still valid */
preferred->request_mode = CWWidth | CWHeight;
preferred->width = w->box.preferred_width;
preferred->height = w->box.preferred_height;
if (constraint->request_mode == preferred->request_mode
&& constraint->width == preferred->width
&& constraint->height == preferred->height)
return XtGeometryYes;
else
return XtGeometryAlmost;
}
w->box.last_query_mode = constraint->request_mode;
w->box.last_query_width = constraint->width;
w->box.last_query_height= constraint->height;
if( w->composite.num_children == 0 ) {
/* without children we accept everthing */
return XtGeometryYes;
}
suggested.request_mode = CWHeight;
suggested.width = constraint->width;
width = 0;
height = 0;
if( REQUESTS_WIDTH(constraint) )
suggested.request_mode |= CWWidth;
for (i = 0; i < w->composite.num_children; i++) {
Widget widget = w->composite.children[i];
XtWidgetGeometry wanted;
XtGeometryResult r;
suggested.height = widget->core.height;
#ifndef NDEBUG
fprintf(stderr, " Calling XtQueryGeometry(%p,", widget);
if( REQUESTS_WIDTH(&suggested) )
fprintf(stderr, " width=%hu", suggested.width);
if( REQUESTS_HEIGHT(&suggested) )
fprintf(stderr, " height=%hu", suggested.height);
#endif
r = XtQueryGeometry(widget, &suggested, &wanted);
switch( r ) {
case XtGeometryYes:
if( REQUESTS_WIDTH(&suggested) )
setmax(width, suggested.width);
height += suggested.height;
break;
case XtGeometryAlmost:
if( REQUESTS_WIDTH(&wanted) )
setmax(width, wanted.width);
if( REQUESTS_HEIGHT(&wanted) )
height += wanted.height;
else
height += widget->core.height;
break;
case XtGeometryNo:
default:
setmax(width, widget->core.width);
height += widget->core.height;
break;
}
}
preferred->request_mode = 0;
if( width > constraint->width ) {
preferred->width = width;
preferred->request_mode |= CWWidth;
} else
preferred->width = constraint->width;
w->box.preferred_width = preferred->width;
preferred->request_mode |= CWHeight;
w->box.preferred_height = preferred->height = height;
if ((~constraint->request_mode & preferred->request_mode) == 0
&& constraint->width == preferred->width
&& constraint->height == preferred->height)
return XtGeometryYes;
else
return XtGeometryAlmost;
}
/*
* Actually layout the vbox. Do not care for height, though.
*/
static void
VBoxResize(Widget wid)
{
VBoxWidget vb = (VBoxWidget)wid;
Cardinal i;
Dimension x,y,w,h,b;
#ifndef NDEBUG
fprintf(stderr, "VBoxResize(%p) {\n", wid);
#endif
x = 0; y = 0;
w = XtWidth(vb);
for (i = 0; i < vb->composite.num_children; i++) {
Widget widget = vb->composite.children[i];
if( !widget->core.managed )
continue;
h = XtHeight(widget);
b = XtBorderWidth(widget);
if( h == 0 )
h = 1;
if( w == 0 )
w = 1;
#ifndef NDEBUG
fprintf(stderr, " Calling XtConfigureWidget(%p, %hd, %hd, %hu, %hu, %hu);\n",
widget, x, y, w, h, b);
#endif
XtConfigureWidget(widget, x, y, w, h, b);
y += h + 2*b;
}
vb->box.preferred_height = y;
#ifndef NDEBUG
fprintf(stderr, "} // VBoxResize(%p);\n", wid);
#endif
}
static void
VBoxResizeExcept(VBoxWidget vb, Widget exclude, Dimension eh, Dimension eb)
{
Cardinal i;
Dimension x,y,w,h,b;
#ifndef NDEBUG
fprintf(stderr, "VBoxResizeExcept(%p) {\n", vb);
#endif
x = 0; y = 0;
w = XtWidth(vb);
for (i = 0; i < vb->composite.num_children; i++) {
Widget widget = vb->composite.children[i];
if( !widget->core.managed )
continue;
if( widget == exclude ) {
y += eh + 2*eb;
} else {
h = XtHeight(widget);
b = XtBorderWidth(widget);
#ifndef NDEBUG
fprintf(stderr, " Calling XtConfigureWidget(%p, %hd, %hd, %hu, %hu, %hu);\n",
widget, x, y, w, h, b);
#endif
XtConfigureWidget(widget, x, y, w, h, b);
y += h + 2*b;
}
}
vb->box.preferred_height = y;
#ifndef NDEBUG
fprintf(stderr, "} // VBoxResize(%p);\n", vb);
#endif
}
static void
VBoxResizeExcept2(VBoxWidget vb, Widget exclude, Dimension eh, Dimension eb)
{
Cardinal i;
Dimension x,y,w,h,b;
#ifndef NDEBUG
fprintf(stderr, "VBoxResizeExcept2(%p) {\n", vb);
#endif
x = 0; y = 0;
w = XtWidth(vb);
for (i = 0; i < vb->composite.num_children; i++) {
Widget widget = vb->composite.children[i];
if( !widget->core.managed )
continue;
if( widget == exclude ) {
h = eh;
b = eb;
} else {
h = XtHeight(widget);
b = XtBorderWidth(widget);
}
#ifndef NDEBUG
fprintf(stderr, " Calling XtConfigureWidget(%p, %hd, %hd, %hu, %hu, %hu);\n",
widget, x, y, w, h, b);
#endif
XtConfigureWidget(widget, x, y, w, h, b);
y += h + 2*b;
}
vb->box.preferred_height = y;
#ifndef NDEBUG
fprintf(stderr, "} // VBoxResize2(%p);\n", vb);
#endif
}
/*
* Geometry Manager
*/
static XtGeometryResult
VBoxGeometryManager(Widget w, XtWidgetGeometry *request,
XtWidgetGeometry *reply)
{
VBoxWidget vb = (VBoxWidget)w->core.parent;
XtGeometryResult result = XtGeometryYes;
Dimension newwidth, newheight;
XtWidgetGeometry parentAsk, parentReply;
Boolean secondTry; Boolean secondNoTry;
#ifndef NDEBUG
fprintf(stderr, "VBoxGeometryManager(%p (parent %p), ", w, vb);
if( REQUESTS_X(request) )
fprintf(stderr, "x=%hu ", request->x);
if( REQUESTS_Y(request) )
fprintf(stderr, "y=%hu ", request->y);
if( REQUESTS_WIDTH(request) )
fprintf(stderr, "width=%hu ", request->width);
if( REQUESTS_HEIGHT(request) )
fprintf(stderr, "height=%hu ", request->height);
if( REQUESTS_BORDER(request) )
fprintf(stderr, "border=%hu ", request->border_width);
if( QUERY_ONLY(request) )
fprintf(stderr, "query-only ");
fprintf(stderr, ")\n");
#endif // NDEBUG
if( reply != NULL ) {
*reply = *request;
reply->request_mode = 0;
}
// TODO: does this include width and height?
if( REQUESTS_BORDER(request) &&
request->border_width != XtBorderWidth(w) ) {
if( reply == NULL )
return XtGeometryNo;
result = XtGeometryAlmost;
reply->border_width = XtBorderWidth(w);
reply->request_mode |= CWBorderWidth;
}
if( REQUESTS_X(request) && request->x != XtX(w) ) {
if( reply == NULL )
return XtGeometryNo;
result = XtGeometryAlmost;
reply->x = XtX(w);
reply->request_mode |= CWX;
}
if( REQUESTS_Y(request) && request->y != XtY(w) ) {
if( reply == NULL )
return XtGeometryNo;
result = XtGeometryAlmost;
reply->y = XtY(w);
reply->request_mode |= CWY;
}
if( !REQUESTS_HEIGHT(request) )
request->height = XtHeight(w);
if( !REQUESTS_WIDTH(request) )
request->width = XtWidth(w);
if( !REQUESTS_BORDER(request) )
reply->border_width = request->border_width = XtBorderWidth(w);
newwidth = 0;
if( (request->request_mode & (CWBorderWidth | CWWidth)) != 0 ) {
newwidth = 2*request->border_width + request->width;
if( newwidth == XtWidth(vb) )
newwidth = 0;
else if( !vb->vbox.resize ) {
if( reply == NULL )
return XtGeometryNo;
if( request->border_width >= XtWidth(vb)/2 ) {
reply->border_width = XtWidth(vb)/2;
if( reply->border_width > 0 && (XtWidth(vb)&1) ==0 )
reply->border_width--;
reply->request_mode |= CWBorderWidth;
}
if( REQUESTS_BORDER(request) )
reply->request_mode |= CWBorderWidth;
reply->width = XtWidth(vb) - 2*reply->border_width;
reply->request_mode |= CWWidth;
result = XtGeometryAlmost;
newwidth = 0;
}
}
if( newwidth != 0 && newwidth < XtWidth(vb) ) {
// TODO: ask every child if it is happy with a shrink
/* until then just reject horiz shrinks: */
if( reply == NULL )
return XtGeometryNo;
if( request->border_width >= XtWidth(vb)/2 ) {
reply->border_width = XtWidth(vb)/2;
if( reply->border_width > 0 && (XtWidth(vb)&1) ==0 )
reply->border_width--;
reply->request_mode |= CWBorderWidth;
}
if( REQUESTS_BORDER(request) )
reply->request_mode |= CWBorderWidth;
reply->width = XtWidth(vb) - 2*reply->border_width;
reply->request_mode |= CWWidth;
result = XtGeometryAlmost;
newwidth = 0;
}
newheight = 0;
if( (request->request_mode & (CWBorderWidth | CWHeight)) != 0 ) {
Dimension h = XtHeight(w) + 2*XtBorderWidth(w);
Dimension newh = request->height + 2*request->border_width;
if( newh <= h ) {
newheight = vb->box.preferred_height - (h-newh);
} else if( newh > h ) {
newheight = vb->box.preferred_height + (newh-h);
}
if( newheight == XtHeight(vb) )
newheight = 0;
}
parentAsk.request_mode = 0;
if( newheight != 0 ) {
parentAsk.request_mode |= CWHeight;
parentAsk.height = newheight;
} else // does this have to be set or is it a bug in viewport to need it?
parentAsk.height = XtHeight(vb);
if( newwidth != 0 ) {
parentAsk.request_mode |= CWWidth;
parentAsk.width = newwidth;
} else // dito
parentAsk.width = XtWidth(vb);
if( parentAsk.request_mode == 0 ) {
if( result == XtGeometryAlmost && reply != NULL )
reply->request_mode |= request->request_mode;
return result;
}
/* box needs resizing to fit, ask parent for it */
if( result == XtGeometryAlmost || QUERY_ONLY(request) )
parentAsk.request_mode |= XtCWQueryOnly;
secondTry = False; secondNoTry = False;
do switch( XtMakeGeometryRequest((Widget)vb, &parentAsk, &parentReply) ) {
case XtGeometryNo:
default:
if( newheight != 0 ) {
/* try again without a height setting */
parentAsk.height = XtHeight(vb);
parentAsk.request_mode &= ~(CWHeight|XtCWQueryOnly);
if( secondNoTry )
return XtGeometryNo;
secondNoTry = True;
continue;
}
return XtGeometryNo;
case XtGeometryYes:
if( result == XtGeometryYes && !QUERY_ONLY(request) ) {
if( 0==1 )
VBoxResizeExcept(vb,w,
request->height,
request->border_width);
else {
VBoxResizeExcept2(vb,w,
request->height,
request->border_width);
return XtGeometryDone;
}
}
if( result == XtGeometryAlmost && reply != NULL )
reply->request_mode |= request->request_mode;
return result;
case XtGeometryAlmost:
if( REQUESTS_WIDTH(&parentReply) &&
parentReply.width != newwidth ) {
if( reply == NULL )
return XtGeometryNo;
if( request->border_width >= parentReply.width/2 ) {
reply->border_width = parentReply.width/2;
if( reply->border_width > 0 && (parentReply.width&1) ==0 )
reply->border_width--;
reply->request_mode |= CWBorderWidth;
}
if( REQUESTS_BORDER(request) )
reply->request_mode |= CWBorderWidth;
reply->width = parentReply.width - 2*reply->border_width;
reply->request_mode |= CWWidth;
reply->request_mode |= request->request_mode;
result = XtGeometryAlmost;
}
if( REQUESTS_HEIGHT(&parentReply) &&
parentReply.height != newheight ) {
/* do not care for height */
/* tell client to fix other problems first */
if( result == XtGeometryAlmost ) {
reply->request_mode |= request->request_mode;
return result;
}
/* if it was only a query, tell it works */
if( QUERY_ONLY(request) )
return XtGeometryYes;
/* otherwise choose a height parent likes
* and let things leave the window */
parentAsk.height = parentReply.height;
parentAsk.request_mode &= ~(CWHeight|XtCWQueryOnly);
if( secondTry ) //should not happen, but does
return XtGeometryNo;
secondTry = True;
} else /* strange situation, no idea what to do */
return XtGeometryNo;
break;
} while (True);
/* NOT REACHED */
}
static void
VBoxChangeManaged(Widget w)
{
#ifndef NDEBUG
fprintf(stderr, "VBoxChangeManaged(%p) {\n", w);
#endif // NDEBUG
// TODO: query geometry and try to resize...
VBoxResize(w);
#ifndef NDEBUG
fprintf(stderr, "} // VBoxChangeManaged(%p);\n", w);
#endif // NDEBUG
}
|