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
|
/* macdialogs1 - Low Level Dialog Objects for Macintosh */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz */
/* You may give out copies of this software; for conditions see the */
/* file COPYING included with this distribution. */
#include ":dialogs.h"
/* forward declarations */
LOCAL VOID SetDialogItemData _((LVAL dialog));
LOCAL int count_hardware_items _((LVAL items));
LOCAL VOID zero_ptr _((char *p, int n));
LOCAL VOID InstallItemList _((DialogPtr theDialog, LVAL items));
LOCAL int FindItemType _((LVAL item));
LOCAL VOID InstallItem _((DialogPtr theDialog, LVAL item));
LOCAL VOID InstallButtonItem _((DialogPtr theDialog, LVAL item));
LOCAL VOID InstallToggleItem _((DialogPtr theDialog, LVAL item));
LOCAL VOID InstallTextItem _((DialogPtr theDialog, LVAL item));
LOCAL VOID InstallChoiceItem _((DialogPtr theDialog, LVAL item));
LOCAL VOID InstallScrollItem _((DialogPtr theDialog, LVAL item));
LOCAL pascal void drawList _((DialogPtr theDialog, short item));
LOCAL VOID InstallListItem _((DialogPtr theDialog, LVAL item));
LOCAL VOID InstallControlItem _((DialogPtr theDialog, LVAL item, int which));
LOCAL pascal Boolean filter(DialogPtr dlog, EventRecord *evt, short *item);
/* layout definitions */
# define MAX_ENTRY_LENGTH 30
# define SCROLL_MIN 0
# define SCROLL_MAX 100
# define CHOICE_HEIGHT 20
/***********************************************************************/
/***********************************************************************/
/** **/
/** Internal Dialog Functions **/
/** **/
/***********************************************************************/
/***********************************************************************/
/***********************************************************************/
/** **/
/** Utility Functions **/
/** **/
/***********************************************************************/
DialogItemData *GetDialogItemData(DialogPtr theDialog)
{
char *p;
p = (char *) *GetDialogData(theDialog);
return((DialogItemData *) (p + sizeof(DialogData)));
}
DialogItemData FindItemData(DialogPtr theDialog, LVAL item)
{
int n = DialogItemCount(theDialog);
DialogItemData *data, *itemData;
data = GetDialogItemData(theDialog);
for (itemData = nil; n > 0 && itemData == nil; n--, data++)
if (data->object == item) itemData = data;
return(*itemData);
}
VOID truncateListEntry(char *s)
{
if (strlen(s) > MAX_ENTRY_LENGTH) {
s = s + MAX_ENTRY_LENGTH - 3;
s[0] = '.'; s[1] = '.'; s[2] = '.'; s[3] = '\0';
}
}
VOID check_alloc(char *p, int check_nil)
{
if (check_nil && p == nil)
xlfail("allocation failed - null pointer or handle retirned");
if (MemError())
xlfail("allocation failed - MemError reported");
}
/***********************************************************************/
/** **/
/** DIALOG-PROTO Methods **/
/** **/
/***********************************************************************/
static VOID MakeDialogItemData(LVAL dialog, Handle *items, Handle *data)
{
int numItems;
numItems = count_hardware_items(slot_value(dialog, s_items)) + 1;
*items = NewHandle(2 + 14 * numItems);
check_alloc((char *) *items, TRUE);
zero_ptr(**items, 2 + 14 * numItems);
**(short **)(*items) = numItems - 1;
*data = NewHandle(sizeof(DialogData) + numItems * sizeof(DialogItemData));
check_alloc((char *) *data, TRUE);
zero_ptr(**data, sizeof(DialogData) + numItems * sizeof(DialogItemData));
}
static pascal void OutlineDefaultButton(DialogPtr theDialog, short item)
{
Rect r;
short itemType;
Handle theItem;
item = ((DialogPeek) theDialog)->aDefItem;
if (item < 1 || item > DialogItemCount(theDialog)) return;
GetDialogItem(theDialog, item, &itemType, &theItem, &r);
if (itemType != ctrlItem + btnCtrl) return;
PenSize(3, 3);
InsetRect(&r, -4, -4);
FrameRoundRect(&r, 16, 16);
}
static ControlActionUPP OutlineDefaultButtonUPP = NULL;
LOCAL VOID SetDialogItemData(LVAL dialog)
{
DialogPtr theDialog;
Rect r;
DialogItemData *data;
DialogData *dialogData;
LVAL items = slot_value(dialog, s_items);
theDialog = (DialogPtr) GETDIALOGADDRESS(dialog);
if (theDialog == nil) xlfail("dialog not allocated");
SetDialogObject(theDialog, dialog);
SetRect(&r, 0, 0, 0, 0);
if (! OutlineDefaultButtonUPP)
OutlineDefaultButtonUPP = NewControlActionProc((ProcPtr) OutlineDefaultButton);
SetDialogItem(theDialog, 1, userItem, (Handle) OutlineDefaultButtonUPP, &r);
((DialogPeek) theDialog)->aDefItem = 1;
data = GetDialogItemData(theDialog);
dialogData = *GetDialogData(theDialog);
data[0].type = NULL_ITEM;
data[0].itemNumber = 1;
data[0].itemHandle = nil;
dialogData->count = 1;
InstallItemList(theDialog, items);
}
LOCAL VOID InstallItemList(DialogPtr theDialog, LVAL items)
{
for (; consp(items); items = cdr(items))
if (consp(car(items))) InstallItemList(theDialog, car(items));
else InstallItem(theDialog, car(items));
}
LOCAL int FindItemType(LVAL item)
{
if (consp(item)) return(ITEM_LIST);
else if (button_item_p(item)) return(BUTTON_ITEM);
else if (toggle_item_p(item)) return(TOGGLE_ITEM);
else if (text_item_p(item)) return(TEXT_ITEM);
else if (choice_item_p(item)) return(CHOICE_ITEM);
else if (scroll_item_p(item)) return(SCROLL_ITEM);
else if (list_item_p(item)) return(LIST_ITEM);
else xlfail("item of unknown type");
return 0; /* not reached */
}
LOCAL VOID InstallItem(DialogPtr theDialog, LVAL item)
{
int type;
if (! dialog_item_p(item)) xlerror("not a dialog item", item);
type = FindItemType(item);
switch (type) {
case BUTTON_ITEM: InstallButtonItem(theDialog, item); break;
case TOGGLE_ITEM: InstallToggleItem(theDialog, item); break;
case CHOICE_ITEM: InstallChoiceItem(theDialog, item); break;
case MESSAGE_ITEM:
case TEXT_ITEM: InstallTextItem(theDialog, item); break;
case SCROLL_ITEM: InstallScrollItem(theDialog, item); break;
case REAL_SCROLL_ITEM:
case LIST_ITEM: InstallListItem(theDialog, item); break;
default: xlfail("unkown item type");
}
}
LOCAL VOID InstallButtonItem(DialogPtr theDialog, LVAL item)
{
InstallControlItem(theDialog, item, BUTTON_ITEM);
}
LOCAL VOID InstallToggleItem(DialogPtr theDialog, LVAL item)
{
InstallControlItem(theDialog, item, TOGGLE_ITEM);
}
LOCAL VOID InstallTextItem(DialogPtr theDialog, LVAL item)
{
Rect r;
DialogItemData *data;
DialogData *dialogData;
Handle theItem;
int type, itemIndex;
char *text;
Point loc, size;
Str255 pbuf;
dialogData = *GetDialogData(theDialog);
itemIndex = ++(dialogData->count);
if (! stringp(slot_value(item, s_text)))
xlerror("not a string", slot_value(item, s_text));
text = (char *) getstring(slot_value(item, s_text));
loc = ListToPoint(slot_value(item, s_location));
size = ListToPoint(slot_value(item, s_size));
SetRect(&r, loc.h, loc.v, loc.h + size.h, loc.v + size.v);
type = (! null(slot_value(item, s_editable))) ? editText : statText;
theItem = NewHandle(1);
SetDialogItem(theDialog, itemIndex, type, theItem, &r);
check_alloc((char *) theItem, TRUE);
CintoPstring(text, pbuf, sizeof pbuf, TRUE);
SetDialogItemText(theItem, pbuf);
if (type == editText) SelectDialogItemText(theDialog, itemIndex, 0, strlen(text));
check_alloc((char *) theItem, TRUE);
data = GetDialogItemData(theDialog);
data[itemIndex - 1].type = TEXT_ITEM;
data[itemIndex - 1].itemNumber = itemIndex;
data[itemIndex - 1].itemHandle = theItem;
data[itemIndex - 1].object = item;
}
LOCAL VOID InstallChoiceItem(DialogPtr theDialog, LVAL item)
{
LVAL titles, temp;
Rect r;
DialogItemData *data;
DialogData *dialogData;
Handle theItem;
int type, procID, itemIndex, clusterLeader, clusterSize, initial;
char *text;
Point loc, size;
dialogData = *GetDialogData(theDialog);
titles = slot_value(item, s_text);
loc = ListToPoint(slot_value(item, s_location));
size = ListToPoint(slot_value(item, s_size));
size.v = CHOICE_HEIGHT;
type = ctrlItem + radCtrl;
procID = radioButProc;
if (! listp(titles)) xlerror("not a list", titles);
clusterLeader = dialogData->count + 1;
clusterSize = llength(titles);
for (; consp(titles); titles = cdr(titles)) {
if (! stringp(car(titles))) xlerror("not a string", car(titles));
text = (char *) getstring(car(titles));
dialogData = *GetDialogData(theDialog);
itemIndex = ++(dialogData->count);
SetRect(&r, loc.h, loc.v, loc.h + size.h, loc.v + size.v);
loc.v += CHOICE_HEIGHT;
CtoPstr(text);
theItem = (Handle) NewControl(theDialog, &r, (StringPtr) text, TRUE,
0, 0, 1, procID, (long) item);
PtoCstr((StringPtr) text);
check_alloc((char *) theItem, TRUE);
SetDialogItem(theDialog, itemIndex, type, theItem, &r);
data = GetDialogItemData(theDialog);
data[itemIndex - 1].type = CHOICE_ITEM;
data[itemIndex - 1].itemNumber = itemIndex;
data[itemIndex - 1].itemHandle = theItem;
data[itemIndex - 1].object = item;
data[itemIndex - 1].clusterLeader = clusterLeader;
data[itemIndex - 1].clusterSize = clusterSize;
}
data = GetDialogItemData(theDialog);
temp = slot_value(item, s_value);
initial = (fixp(temp)) ? getfixnum(temp) : 0;
initial = ((initial >= 0 && initial < clusterSize) ? initial : 0) + clusterLeader - 1;
SetControlValue((ControlHandle) data[initial].itemHandle, 1);
}
LOCAL VOID InstallScrollItem(DialogPtr theDialog, LVAL item)
{
Rect r;
DialogItemData *data;
DialogData *dialogData;
Handle theItem;
int low, high, value;
int type, procID, itemIndex;
Point loc, size;
LVAL temp;
dialogData = *GetDialogData(theDialog);
itemIndex = ++(dialogData->count);
loc = ListToPoint(slot_value(item, s_location));
size = ListToPoint(slot_value(item, s_size));
type = userItem;
procID = scrollBarProc;
temp = slot_value(item, s_min_value);
low = fixp(temp) ? getfixnum(temp) : SCROLL_MIN;
temp = slot_value(item, s_max_value);
high = fixp(temp) ? getfixnum(temp) : SCROLL_MAX;
temp = slot_value(item, s_value);
value = (fixp(temp)) ? getfixnum(temp) : low;
SetRect(&r, loc.h, loc.v, loc.h + size.h, loc.v + size.v);
theItem = (Handle) NewControl(theDialog, &r, "\p", TRUE,
value, low, high, procID, (long) item);
check_alloc((char *) theItem, TRUE);
SetDialogItem(theDialog, itemIndex, type, nil, &r);
data = GetDialogItemData(theDialog);
data[itemIndex - 1].type = SCROLL_ITEM;
data[itemIndex - 1].itemNumber = itemIndex;
data[itemIndex - 1].itemHandle = theItem;
data[itemIndex - 1].object = item;
}
LOCAL pascal void drawList(DialogPtr theDialog, short item)
{
Rect r;
DialogItemData *data;
int type;
ListHandle theList;
data = GetDialogItemData(theDialog);
theList = (ListHandle) data[item - 1].itemHandle;
type = data[item - 1].type;
if (type == LIST_ITEM) {
LUpdate(thePort->visRgn, theList);
r = (*theList)->rView;
InsetRect(&r, -1, -1);
FrameRect(&r);
}
}
static ControlActionUPP drawListUPP = NULL;
LOCAL VOID InstallListItem(DialogPtr theDialog, LVAL item)
{
Rect r, b;
DialogItemData *data;
DialogData *dialogData;
ListHandle theItem;
int type, itemIndex, columns, n, m, i;
Point loc, size, cell;
LVAL listData, next, temp;
Boolean vscroll, hscroll;
char *s;
dialogData = *GetDialogData(theDialog);
itemIndex = ++(dialogData->count);
loc = ListToPoint(slot_value(item, s_location));
size = ListToPoint(slot_value(item, s_size));
type = userItem;
listData = slot_value(item, s_list_data);
if (seqp(listData)) {
n = seqlen(listData);
m = 1;
}
else if (matrixp(listData)) {
n = numrows(listData);
m = numcols(listData);
}
else xlerror("this form of data is not yet supported", listData);
temp = slot_value(item, s_columns);
if (! fixp(temp) || getfixnum(temp) < 1) columns = 1;
else columns = getfixnum(temp);
hscroll = (columns < m) ? TRUE : FALSE;
vscroll = (n * 16 > size.v - ((hscroll) ? 15 : 0)) ? TRUE : FALSE;
SetRect(&b, 0, 0, columns, n);
SetRect(&r, loc.h, loc.v, loc.h + size.h, loc.v + size.v);
if (vscroll) r.right -= 15;
if (hscroll) r.bottom -= 15;
cell.h = 0; cell.v = 0;
theItem = LNew(&r, &b, cell, 0, theDialog, TRUE, FALSE, hscroll, vscroll);
check_alloc((char *) theItem, TRUE);
if (slot_value(item, s_multiple) != NIL)
(*theItem)->selFlags = lExtendDrag | lNoDisjoint;
else (*theItem)->selFlags = lOnlyOne;
if (columns < m) LAddColumn(m - columns, columns, theItem);
if (vscroll) r.right += 15;
if (hscroll) r.bottom += 15;
check_alloc((char *) theItem, TRUE);
if (! drawListUPP)
drawListUPP = NewControlActionProc((ProcPtr) drawList);
SetDialogItem(theDialog, itemIndex, userItem, (Handle) drawListUPP, &r);
if (darrayp(listData))
listData = getdarraydata(listData);
for (cell.v = 0, i = 0; cell.v < n; cell.v++)
for (cell.h = 0; cell.h < m; cell.h++, i++) {
next = getnextelement(&listData, i);
s = (stringp(next)) ? (char *) getstring(next) : "";
strcpy(buf, s);
truncateListEntry(buf);
LSetCell(buf, strlen(buf), cell, theItem);
check_alloc((char *) theItem, TRUE);
}
data = GetDialogItemData(theDialog);
data[itemIndex - 1].type = LIST_ITEM;
data[itemIndex - 1].itemNumber = itemIndex;
data[itemIndex - 1].itemHandle = (Handle) theItem;
data[itemIndex - 1].object = item;
}
LOCAL VOID InstallControlItem(DialogPtr theDialog, LVAL item, int which)
{
Rect r;
DialogItemData *data;
DialogData *dialogData;
Handle theItem;
int type, value, low, high, procID, itemIndex;
char *text;
Point loc, size;
LVAL val;
dialogData = *GetDialogData(theDialog);
itemIndex = ++(dialogData->count);
if (! stringp(slot_value(item, s_text)))
xlerror("not a string", slot_value(item, s_text));
text = (char *) getstring(slot_value(item, s_text));
loc = ListToPoint(slot_value(item, s_location));
size = ListToPoint(slot_value(item, s_size));
SetRect(&r, loc.h, loc.v, loc.h + size.h, loc.v + size.v);
value = 0; low = 0; high = 1;
switch (which) {
case BUTTON_ITEM: procID = pushButProc; type = ctrlItem + btnCtrl; break;
case TOGGLE_ITEM: procID = checkBoxProc; type = ctrlItem + chkCtrl; break;
default: xlfail("unknown item type");
}
CtoPstr(text);
theItem = (Handle) NewControl(theDialog, &r, (StringPtr) text, TRUE,
value, low, high, procID, (long) item);
PtoCstr((StringPtr) text);
check_alloc((char *) theItem, TRUE);
SetDialogItem(theDialog, itemIndex, type, theItem, &r);
data = GetDialogItemData(theDialog);
data[itemIndex - 1].type = which;
data[itemIndex - 1].itemNumber = itemIndex;
data[itemIndex - 1].itemHandle = theItem;
data[itemIndex - 1].object = item;
if (which == TOGGLE_ITEM) {
val = slot_value(item, s_value);
SetControlValue((ControlHandle) theItem, (val != NIL));
}
}
static VOID zero_ptr(char *p, int n)
{
while (n-- > 0) *p++ = 0;
}
LOCAL int count_hardware_items(LVAL items)
{
LVAL temp;
int n;
if (! consp (items)) return(0);
else
for (n = 0; consp(items); items = cdr(items))
switch(FindItemType(car(items))) {
case CHOICE_ITEM:
temp = slot_value(car(items), s_text);
if (! consp(temp)) xlerror("not a list", temp);
n += llength(temp);
break;
case ITEM_LIST: n += count_hardware_items(car(items)); break;
default: n += 1;
}
return(n);
}
LOCAL pascal Boolean filter(DialogPtr dlog, EventRecord *evt, short *item)
{
if (evt->what == activateEvt) {
LVAL object;
object = GetDialogObject(dlog);
send_message_1L(object,
sk_activate,
((evt->modifiers & activeFlag) != 0) ? s_true : NIL);
}
return FALSE;
}
/***********************************************************************/
/***********************************************************************/
/** **/
/** Public Dialog Functions **/
/** **/
/***********************************************************************/
/***********************************************************************/
VOID DialogAllocate(LVAL dialog)
{
DialogPtr theDialog;
Point loc, size;
Rect bounds;
char *title;
Boolean visible, goAway;
int type, result;
WindowPtr behind;
Handle items, ref;
GrafPtr savePort;
if (check_dialog_address(dialog)) DialogRemove(dialog);
if (! stringp(slot_value(dialog, s_title)))
xlerror("not a string", slot_value(dialog, s_title));
title = (char *) getstring(slot_value(dialog, s_title));
loc = ListToPoint(slot_value(dialog, s_location));
loc.v += GetMBarHeight();
size = ListToPoint(slot_value(dialog, s_size));
SetRect(&bounds, loc.h, loc.v, loc.h + size.h, loc.v + size.v);
visible = TRUE;
goAway = (slot_value(dialog, s_go_away) != NIL) ? TRUE : FALSE;
if (slot_value(dialog, s_type) == s_modeless)
type = noGrowDocProc;
else type = dBoxProc;
behind = (WindowPtr) -1;
MakeDialogItemData(dialog, &items, &ref);
CtoPstr(title);
theDialog = NewDialog(nil, &bounds, (StringPtr) title, FALSE, type,
behind, goAway, (long) ref, items);
PtoCstr((StringPtr) title);
check_alloc((char *) theDialog, TRUE);
GetPort(&savePort);
result = SkelDialog(theDialog, filter, doDialog, closeDialog, clobberDialog);
SetPort(savePort);
if (! result) xlfail("allocation Failed");
set_dialog_address((CPTR) theDialog, dialog);
SetDialogItemData(dialog);
DialogSetDefaultButton(dialog, slot_value(dialog, s_default_button));
if (visible) MyShowWindow(theDialog);
}
VOID DialogRemove(LVAL dialog)
{
if (check_dialog_address(dialog))
SkelRmveDlog((DialogPtr) GETDIALOGADDRESS(dialog));
if (objectp(dialog)) standard_hardware_clobber(dialog);
}
VOID DialogSetDefaultButton(LVAL dialog, LVAL item)
{
DialogItemData itemData;
DialogPtr theDialog;
GrafPtr savePort;
int noDflt;
if (item != NIL && ! button_item_p(item))
xlerror("not a button item", item);
set_slot_value(dialog, s_default_button, item);
theDialog = (DialogPtr) GETDIALOGADDRESS(dialog);
if (theDialog != nil) {
noDflt = (((DialogPeek) theDialog)->aDefItem == 1);
if (item == NIL)
((DialogPeek) theDialog)->aDefItem = 1;
else {
itemData = FindItemData(theDialog, item);
((DialogPeek) theDialog)->aDefItem = itemData.itemNumber;
}
if (! noDflt) {
GetPort(&savePort);
SetPort(theDialog);
EraseRect(&theDialog->portRect);
SetPort(savePort);
DrawDialog(theDialog);
}
}
}
|