File: sundialogs.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (662 lines) | stat: -rw-r--r-- 18,015 bytes parent folder | download | duplicates (4)
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
/* sundialogs - dialogs support for SunView                            */
/* 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"
#include xlstat.h"
#include "xlgraph.h"
#include <fcntl.h>

extern Frame BaseFrame;

extern LVAL s_text_length;

typedef struct {
  LVAL object;
  int idleOn, frontOnly;
  int mouse_x, mouse_y;
  Panel panel;
} DialogData;

int inModal = FALSE;

/* layout definitions */
# define BUTTON_HEIGHT_PAD 20
# define BUTTON_WIDTH_PAD 15

# define TOGGLE_HEIGHT_PAD 5
# define TOGGLE_WIDTH_PAD 30

# define CHOICE_WIDTH_PAD 20
# define CHOICE_HEIGHT 30

# define TEXT_WIDTH_PAD 20
# define TEXT_HEIGHT_PAD 5

# define SCROLL_WIDTH 250
# define SCROLL_HEIGHT 20

/***********************************************************************/
/**                                                                   **/
/**                         Utility Functions                         **/
/**                                                                   **/
/***********************************************************************/

static Point StringSize(text)
     char *text;
{
  struct pixfont *font = pf_default();
  struct pr_size string_size;
  Point size;

  size.v = font->pf_defaultsize.y;
  string_size = pf_textwidth(strlen(text), font, text);
  size.h = string_size.x;
  return(size);
}

static FindItemType(item)
	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");
}

static Panel_item FindPanelItem(item)
     LVAL item;
{
  Frame frame;
  LVAL dialog;
  DialogData *ddata;
  Panel panel;
  Panel_item panel_item, current_item;

  dialog = slot_value(item, s_dialog);
  if (! objectp(dialog)) xlerror("not a dialog object", dialog);
  frame = (Frame) GETDIALOGADDRESS(dialog);

  if (frame == NULL) return(NULL);
  else {
    ddata = (DialogData *) GetWRefCon(frame);
    panel = ddata->panel;
    
    panel_item = NULL;
    panel_each_item(panel, current_item)
      if ((LVAL) panel_get(current_item, PANEL_CLIENT_DATA) == item)
	panel_item = current_item;
    panel_end_each;
    
    if (panel_item == NULL) xlfail("can't find internal item");
    return(panel_item);
  }
}

/***********************************************************************/
/**                                                                   **/
/**                      General Dialog Functions                     **/
/**                                                                   **/
/***********************************************************************/
static done_proc(frame)
     Frame frame;
{
  DialogData *ddata = (DialogData *) GetWRefCon(frame);
  
  if (ddata != NULL) send_message(ddata->object, sk_close);
}

DialogAllocate(dialog)
  LVAL dialog;
{
  Frame dialog_frame;
  Panel dialog_panel;
  Point loc, size;
  char *title;
  int is_modeless;
  DialogData *ddata;

  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));
  size = ListToPoint(slot_value(dialog, s_size));
  
  is_modeless = (slot_value(dialog, s_type) == s_modeless) ? TRUE : FALSE;
  
  dialog_frame = window_create(BaseFrame, FRAME, 
			       FRAME_NO_CONFIRM, TRUE,
			       FRAME_SHOW_LABEL, is_modeless,
			       FRAME_LABEL, title, 0);
  set_dialog_address(dialog_frame, dialog);
  dialog_panel = window_create(dialog_frame, PANEL, 0);
  if (is_modeless) window_set(dialog_frame, WIN_SHOW, TRUE, 0);
  window_set(dialog_frame, FRAME_DONE_PROC, done_proc, 0);

  ddata = (DialogData *) StCalloc(sizeof(DialogData), 1);
  SetWRefCon(dialog_frame, ddata);
  ddata->object = dialog;
  ddata->panel = dialog_panel;

  InstallDialogItems(dialog);
  window_fit(dialog_panel);
  window_fit(dialog_frame);
}

DialogRemove(dialog)
	LVAL dialog;
{
  DialogData *ddata;
  Frame frame;

  if (check_dialog_address(dialog) 
      && (frame = (Frame) GETDIALOGADDRESS(dialog)) != NULL) {
    ddata = (DialogData *) GetWRefCon(frame);
    StFree(ddata);
    window_destroy(frame);
  }
  if (objectp(dialog)) standard_hardware_clobber(dialog);
}

DialogSetDefaultButton(dialog, item)
	LVAL dialog, item;
{
}

DialogReset()
{
  inModal = FALSE;
}

LVAL DialogGetModalItem(dialog) 
     LVAL dialog;
{
  Frame frame;
  int oldInModal = inModal;
  LVAL item = NIL;

  /*++++++ deal with interrupts++++++*/
  /*+++++++++ does not work on dialogs created as modeless ++++*/

  StSunReleaseButton();
  inModal = TRUE;
  frame = (Frame) GETDIALOGADDRESS(dialog);
  if (frame != NULL) {
    /* make sure window is NOT showing before using window_loop */
    if (window_get(frame, WIN_SHOW)) window_set(frame, WIN_SHOW, FALSE, 0);
    item = (LVAL) window_loop(frame);
  }
  inModal = oldInModal;
  return(item);
}

/***********************************************************************/
/**                                                                   **/
/**                    Item Installation Functions                    **/
/**                                                                   **/
/***********************************************************************/

static button_proc(item, event)
     Panel_item item;
     Event *event;
{
  LVAL lisp_item = (LVAL) panel_get(item, PANEL_CLIENT_DATA);

  if (inModal) window_return(lisp_item);
  else errset_send(lisp_item, sk_do_action);
}

static choice_proc(item, event)
     Panel_item item;
     Event *event;
{
  LVAL lisp_item = (LVAL) panel_get(item, PANEL_CLIENT_DATA);

  errset_send(lisp_item, sk_do_action);
}

static scroll_proc(item, value, event)
     Panel_item item;
     int value;
     Event *event;
{
  LVAL lisp_item = (LVAL) panel_get(item, PANEL_CLIENT_DATA);
  LVAL dialog;
  Frame frame;
  Panel panel;
  DialogData *ddata;
  int oldval, increment;
  int fd, oldflags, error;

  dialog = slot_value(lisp_item, s_dialog);
  frame = (Frame) GETDIALOGADDRESS(dialog);
  if (frame == NULL) xlfail("dialog not allocated");

  ddata = (DialogData *) GetWRefCon(frame);
  panel = ddata->panel;

  oldval = (int) panel_get(item, PANEL_VALUE);
  if (oldval < value) increment = 1;
  else if (oldval > value) increment = -1;
  else increment = 0;

  fd = (int) window_get(panel, WIN_FD);
  oldflags = fcntl(fd, F_GETFL, FNDELAY);
  fcntl(fd, F_SETFL, oldflags | FNDELAY);
  value = oldval;
  error = FALSE;
  while (! error && input_readevent(fd, event)) {
    value += increment;
    panel_set(item, PANEL_VALUE, value, 0);
    error = errset_send(lisp_item, sk_do_action);
  }
  error = FALSE;
  while (! error && ! event_is_up(event)) {
    value += increment;
    panel_set(item, PANEL_VALUE, value, 0);
    error = errset_send(lisp_item, sk_do_action);
    while (! error && input_readevent(fd, event)) {
      value += increment;
      panel_set(item, PANEL_VALUE, value, 0);
      error = errset_send(lisp_item, sk_do_action);
    }
  }
  fcntl(fd, F_SETFL, oldflags);

  errset_send(lisp_item, sk_do_action);
}
  
static InstallDialogItems(dialog)
     LVAL dialog;
{
  Frame frame;
  Panel panel;
  DialogData *ddata;
  LVAL items;

  frame = (Frame) GETDIALOGADDRESS(dialog);
  if (frame != NULL) {
    ddata = (DialogData *) GetWRefCon(frame);
    items = slot_value(dialog, s_items);
  
    if (ddata != NULL) {
      panel = ddata->panel;
      InstallItemList(panel, items);
    }
  }
}

static InstallItemList(panel, items)
	Panel panel;
	LVAL items;
{
  for (; consp(items); items = cdr(items))
    if (consp(car(items))) InstallItemList(panel, car(items));
    else InstallItem(panel, car(items));
}
  
static InstallItem(panel, item)
     Panel panel;
     LVAL item;
{
  int type;
  
  if (! dialog_item_p(item)) xlerror("not a dialog item", item);
  
  type = FindItemType(item);
  
  switch (type) {
  case BUTTON_ITEM: InstallButtonItem(panel, item); break;
  case TOGGLE_ITEM: InstallToggleItem(panel, item); break;
  case CHOICE_ITEM: InstallChoiceItem(panel, item); break;
  case TEXT_ITEM:   InstallTextItem(panel, item); break;
  case SCROLL_ITEM: InstallScrollItem(panel, item); break;
  default: xlfail("unkown item type");
  }
}

static InstallButtonItem(panel, item)
     Panel panel;
     LVAL item;
{
  char *text;
  Point loc;
  Panel_item button_item;

  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));

  button_item = panel_create_item(panel, PANEL_BUTTON, 
				   PANEL_LABEL_IMAGE, 
				   panel_button_image(panel, text, 0, 0),
				   PANEL_ITEM_X, loc.h,
				   PANEL_ITEM_Y, loc.v,
				   PANEL_CLIENT_DATA, item,
				   PANEL_NOTIFY_PROC, button_proc,
				   0);
}

static InstallToggleItem(panel, item)
     Panel panel;
     LVAL item;
{
  char *text;
  Point loc;
  Panel_item toggle_item;
  int value;

  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));
  value = (slot_value(item, s_value) != NIL) ? TRUE : FALSE;

  toggle_item = panel_create_item(panel, PANEL_TOGGLE, 
				  PANEL_CHOICE_STRINGS, text, 0,
				  PANEL_ITEM_X, loc.h,
				  PANEL_ITEM_Y, loc.v,
				  PANEL_CLIENT_DATA, item,
				  PANEL_NOTIFY_PROC, choice_proc,
				  0);
  DialogToggleItemValue(item, TRUE, value);
}

static InstallChoiceItem(panel, item)
     Panel panel;
     LVAL item;
{
  LVAL text;
  Point loc;
  Panel_item choice_item;
  char *str;
  int i;
  LVAL value;

  loc = ListToPoint(slot_value(item, s_location));

  choice_item = panel_create_item(panel, PANEL_CHOICE, 
				  PANEL_LAYOUT, PANEL_VERTICAL,
				  PANEL_ITEM_X, loc.h,
				  PANEL_ITEM_Y, loc.v,
				  PANEL_CLIENT_DATA, item,
				  PANEL_NOTIFY_PROC, choice_proc,
				  0);
  
  for (text = slot_value(item, s_text), i = 0;
       consp(text); 
       text = cdr(text), i++) {
    str = (char *) getstring(car(text));
    panel_set(choice_item, PANEL_CHOICE_STRING, i, str, 0);
  }
  value = slot_value(item, s_value);
  if (fixp(value)) DialogChoiceItemValue(item, TRUE, getfixnum(value));
}

static InstallTextItem(panel, item)
     Panel panel;
     LVAL item;
{
  char *text;
  Point loc;
  Panel_item panel_item;
  int editable, len;
  LVAL text_length;

  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));
  editable = (slot_value(item, s_editable) != NIL) ? TRUE : FALSE;

  if (editable) {
    text_length = slot_value(item, s_text_length);
    len = (fixp(text_length)) ? getfixnum(text_length) : 0;
    len = max(len, strlen(text));
    panel_item = panel_create_item(panel, PANEL_TEXT,
				   PANEL_VALUE, text,
				   PANEL_VALUE_DISPLAY_LENGTH, len,
				   PANEL_ITEM_X, loc.h,
				   PANEL_ITEM_Y, loc.v,
				   PANEL_CLIENT_DATA, item,
				   0);
  }
  else
    panel_item = panel_create_item(panel, PANEL_MESSAGE, 
				   PANEL_LABEL_STRING, text,
				   PANEL_ITEM_X, loc.h,
				   PANEL_ITEM_Y, loc.v,
				   PANEL_CLIENT_DATA, item,
				   0);
}

static InstallScrollItem(panel, item)
     Panel panel;
     LVAL item;
{
  Point loc, size;
  Panel_item scroll_item;
  int low, high, value;
  LVAL temp;

  loc = ListToPoint(slot_value(item, s_location));
  size = ListToPoint(slot_value(item, s_size));

  temp = slot_value(item, s_min_value);
  low = fixp(temp) ? getfixnum(temp) : 0;
  temp = slot_value(item, s_max_value);
  high = fixp(temp) ? getfixnum(temp) : 100;
  temp = slot_value(item, s_value);
  value = (fixp(temp)) ? getfixnum(temp) : low;

  scroll_item = panel_create_item(panel, PANEL_SLIDER, 
				  PANEL_ITEM_X, loc.h,
				  PANEL_ITEM_Y, loc.v,
				  PANEL_SLIDER_WIDTH, size.h,
				  PANEL_MIN_VALUE, low,
				  PANEL_MAX_VALUE, high,
				  PANEL_VALUE, value,
				  PANEL_CLIENT_DATA, item,
				  PANEL_NOTIFY_LEVEL, PANEL_ALL,
				  PANEL_SHOW_RANGE, FALSE,
				  PANEL_SHOW_VALUE, FALSE,
				  PANEL_NOTIFY_PROC, scroll_proc,
				  0);
}

/***********************************************************************/
/**                                                                   **/
/**                       Dialog Item Functions                       **/
/**                                                                   **/
/***********************************************************************/

DialogButtonGetDefaultSize(item, width, height)
	LVAL item;
	int *width, *height;
{
  LVAL text = slot_value(item, s_text);
  Point sz;
  
  if (! stringp(text)) xlerror("not a string", text);
  sz = StringSize(getstring(text));
  
  if (width != NULL) *width = sz.h + BUTTON_WIDTH_PAD;
  if (height != NULL) *height = sz.v + BUTTON_HEIGHT_PAD;
}

DialogToggleGetDefaultSize(item, width, height)
	LVAL item;
	int *width, *height;
{
  LVAL text = slot_value(item, s_text);
  Point sz;
  
  if (! stringp(text)) xlerror("not a string", text);
  sz = StringSize(getstring(text));
  
  if (width != NULL) *width = sz.h + TOGGLE_WIDTH_PAD;
  if (height != NULL) *height = sz.v + TOGGLE_HEIGHT_PAD;
}

LVAL DialogToggleItemValue(item, set, value) 
     LVAL item;
     int set, value;
{
  Panel_item panel_item = FindPanelItem(item);

  if (set) set_slot_value(item, s_value, (value) ? s_true : NIL);
  if (panel_item != NULL) {
    if (set) panel_set(panel_item, PANEL_TOGGLE_VALUE, 0, value, 0);
    value = (int) panel_get(panel_item, PANEL_TOGGLE_VALUE, 0);
    set_slot_value(item, s_value, (value) ? s_true : NIL);
  }
  return(slot_value(item, s_value));  
}

DialogChoiceGetDefaultSize(item, width, height)
	LVAL item;
	int *width, *height;
{
  Point sz, pt;
  LVAL text = slot_value(item, s_text);
  
  for (sz.h = 0, sz.v = 0; consp(text); text = cdr(text)) {
    pt = StringSize(getstring(car(text)));
    sz.h = max(sz.h, pt.h);
    sz.v += CHOICE_HEIGHT;
  }
  if (width != NULL) *width = sz.h + CHOICE_WIDTH_PAD;
  if (height != NULL) *height = sz.v;
}

LVAL DialogChoiceItemValue(item, set, value)
     LVAL item;
     int set, value;
{
  Panel_item panel_item = FindPanelItem(item);

  if (set) set_slot_value(item, s_value, cvfixnum((FIXTYPE) value));
  if (panel_item != NULL) {
    if (set) panel_set(panel_item, PANEL_VALUE, value, 0);
    value = (int) panel_get(panel_item, PANEL_VALUE);
    set_slot_value(item, s_value, cvfixnum((FIXTYPE) value));
  }
  return(slot_value(item, s_value));
}

DialogTextGetDefaultSize(item, width, height)
	LVAL item;
	int *width, *height;
{
  Point sz;
  LVAL text_length = slot_value(item, s_text_length);
  int len;

  sz = StringSize(getstring(slot_value(item, s_text)));
  len = sz.h;
  if (fixp(text_length)) {
    sz = StringSize("M");
    len = max(len, getfixnum(text_length) * sz.h);
  }
  if (width != NULL) *width = len + TEXT_WIDTH_PAD;
  if (height != NULL) *height = sz.v + TEXT_HEIGHT_PAD;
}

LVAL DialogTextItemText(item, set, text)
     LVAL item;
     int set;
     char *text;
{
  Panel_item panel_item = FindPanelItem(item);
  int editable = (slot_value(item, s_editable) != NIL) ? TRUE : FALSE;

  if (set) set_slot_value(item, s_text, cvstring(text));
  if (panel_item != NULL) {
    if (editable) {
      if (set) panel_set(panel_item, PANEL_VALUE, text, 0);
      text = panel_get(panel_item, PANEL_VALUE);
    }
    else {
      if (set) panel_set(panel_item, PANEL_LABEL_STRING, text, 0);
      text = panel_get(panel_item, PANEL_LABEL_STRING);
    }
    set_slot_value(item, s_text, cvstring(text));
  }
  return(slot_value(item, s_text));
}

DialogScrollGetDefaultSize(item, width, height)
     LVAL item;
     int *width, *height;
{
  Point sz;

  if (width != NULL) *width = SCROLL_WIDTH;
  if (height != NULL) *height = SCROLL_HEIGHT;
}

LVAL DialogScrollItemValue(item, set, value)
     LVAL item;
     int set, value;
{
  Panel_item panel_item = FindPanelItem(item);
  
  if (set) set_slot_value(item, s_value, cvfixnum((FIXTYPE) value));
  if (panel_item != NULL) {
    if (set) panel_set(panel_item, PANEL_VALUE, value, 0);
    value = (int) panel_get(panel_item, PANEL_VALUE);
    set_slot_value(item, s_value, cvfixnum((FIXTYPE) value));
  }
  return(slot_value(item, s_value));
}

LVAL DialogScrollItemMax(item, set, value)
     LVAL item;
     int set, value;
{
  Panel_item panel_item = FindPanelItem(item);

  if (set) set_slot_value(item, s_min_value, cvfixnum((FIXTYPE) value));
  if (panel_item != NULL) {
    if (set) panel_set(panel_item, PANEL_MIN_VALUE, value, 0);
    value = (int) panel_get(panel_item, PANEL_MIN_VALUE);
    set_slot_value(item, s_min_value, cvfixnum((FIXTYPE) value));
  }
  return(slot_value(item, s_min_value));
}

LVAL DialogScrollItemMin(item, set, value)
     LVAL item;
     int set, value;
{
  Panel_item panel_item = FindPanelItem(item);

  if (set) set_slot_value(item, s_max_value, cvfixnum((FIXTYPE) value));
  if (panel_item != NULL) {
    if (set) panel_set(panel_item, PANEL_MAX_VALUE, value, 0);
    value = (int) panel_get(panel_item, PANEL_MAX_VALUE);
    set_slot_value(item, s_max_value, cvfixnum((FIXTYPE) value));
  }
  return(slot_value(item, s_max_value));
}

DialogListItemSetText() {}
LVAL DialogListItemSelection() {}
DialogListGetDefaultSize() {}

#ifdef COMMENT
multiple lines in static text items
#endif COMMENT