File: maciviewwindow2.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 (610 lines) | stat: -rw-r--r-- 16,570 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
#include "xlisp.h"
#include "xlstat.h"
#include "xlgraph.h"

/**************************************************************************/
/**                                                                      **/
/**                    Action Installation Functions                     **/
/**                                                                      **/
/**************************************************************************/

VOID StGWSetFreeMem(StGWWinInfo *gwinfo, void (*FreeMem)(IVIEW_WINDOW))
{
  if (gwinfo == nil) return;
  else gwinfo->FreeMem =  FreeMem;
}

int StGWIdleOn(StGWWinInfo *gwinfo)
{
  
  if (gwinfo == nil) return(FALSE);
  else return(gwinfo->idleOn);
}

VOID StGWSetIdleOn(StGWWinInfo *gwinfo, int on)
{
  if (gwinfo != nil) gwinfo->idleOn = on;
}

/**************************************************************************/
/**                                                                      **/
/**                      Window Management Functions                     **/
/**                                                                      **/
/**************************************************************************/

VOID StGWShowWindow(StGWWinInfo *gwinfo)
{
  WindowPtr w;
  
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;
  else {
    MyShowWindow(w);
    if (! gwinfo->initialized)
      graph_update_action(gwinfo, TRUE);
  }
}

VOID StGWRemove(StGWWinInfo *gwinfo)
{
  WindowPtr w;
  
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;
  else SkelRmveWind(w);
}

VOID StGWWhileButtonDown(StGWWinInfo *gwinfo, void (*action) (WindowPtr,int, int),int motionOnly)
{
  Point pt;
  GrafPtr savePort;
  WindowPtr w;
  
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;
  
  GetPort(&savePort);
  SetPort(w);
  while (StillDown()) {
    SetPort(w);
    mac_do_cursor(gwinfo);
    GetMouse(&pt);
    if (gwinfo->mouse_x != pt.h || gwinfo->mouse_y != pt.v || ! motionOnly) {
      gwinfo->mouse_x = pt.h; gwinfo->mouse_y = pt.v;
      if (action != nil) (*action)(w, pt.h, pt.v);
    }
  }
  SetPort(savePort);
}

static TitleBarHeight(WindowPtr w)
{
  Rect r;
  Point pt;
  WindowPeek wind = (WindowPeek) w;
  GrafPtr savePort;
  
  GetPort(&savePort);
  SetPort(w);
  SetPt(&pt, 0, 0);
  LocalToGlobal(&pt);
  r = (*(wind->strucRgn))->rgnBBox;
  SetPort(savePort);
  return(pt.v - r.top);
}

VOID StWSetLocation(WindowPtr w, int left, int top, int frame)
{
  int adjust;
  
  if (w == nil) return;
  adjust = (frame) ? GetMBarHeight() + TitleBarHeight(w) : GetMBarHeight();
  MoveWindow(w, left, top + adjust, FALSE);
}

VOID StWGetLocation(WindowPtr w, int *left, int *top, int frame)
{
  GrafPtr savePort;
  Point pt;
  int adjust;
  
  if (w == nil) return;
  else {
    adjust = (frame) ? GetMBarHeight() + TitleBarHeight(w) : GetMBarHeight();
    GetPort(&savePort);
    SetPort(w);
    pt.h = w->portRect.left;
    pt.v = w->portRect.top;
    LocalToGlobal(&pt);
    if (left != nil) *left = pt.h;
    if (top != nil) *top = pt.v - adjust;
    SetPort(savePort);
  }
}

VOID StGWSetSize(StGWWinInfo *gwinfo, int width, int height, int frame)
{
  WindowPtr w;
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;
  else StWSetSize(w, width, height, frame);
}

VOID StWSetSize(WindowPtr w, int width, int height, int frame)
{
  if (w == nil) return;
  SizeWindow(w, width, height - ((frame) ? TitleBarHeight(w) : 0), TRUE);
  SkelTriggerUpdate(w);
  SkelDoUpdates();
}

VOID StWGetSize(WindowPtr w, int *width, int *height, int frame)
{
  
  if (w == nil) return;
  else {
    if (width != nil) *width = w->portRect.right - w->portRect.left;
    if (height != nil) *height = w->portRect.bottom - w->portRect.top 
                               + ((frame) ? TitleBarHeight(w) : 0);
  }
}

/**************************************************************************/
/**                                                                      **/
/**             Window State Access and Mutation Functions               **/
/**                                                                      **/
/**************************************************************************/

VOID SetHardwareState(StGWWinInfo *gwinfo)
{
  GrafPtr savePort;
  WindowPtr w;
  
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;

  GetPort(&savePort);
  SetPort(w);
  
  set_fore_color(gwinfo);
  set_back_color(gwinfo);
  if (gwinfo->drawMode == 0) w->pnMode = patCopy;
  else w->pnMode = patXor;
  if (gwinfo->drawMode == 0) {
    w->txMode = srcOr;
    gwinfo->symbolMode = srcCopy;
  }
  else {
    w->txMode = srcXor;
    gwinfo->symbolMode = srcXor;
  }
  
  /* set line type */
  if (gwinfo->lineType != 0 && gwinfo->drawColor != gwinfo->backColor)
    PenPat(&gray);
  else PenPat(&black);
  
  /* set pen size */
  PenSize(gwinfo->lineWidth, gwinfo->lineWidth);
  
  adjust_graph_workport(gwinfo);
  SetPort(savePort);
}

static int get_state(StGWWinInfo *gwinfo, int which)
{
  if (gwinfo == nil) return(0);
  switch (which) {
  case 'W': return(gwinfo->canvasWidth);
  case 'H': return(gwinfo->canvasHeight);
  case 'L': return(gwinfo->lineType);
  case 'M': return(gwinfo->drawMode);
  case 'D': return(gwinfo->drawColor);
  case 'B': return(gwinfo->backColor);
  case 'C': return(gwinfo->use_color);
  }
  return 0;
}

int StGWCanvasWidth(StGWWinInfo *gwinfo)  { return (get_state(gwinfo, 'W')); }
int StGWCanvasHeight(StGWWinInfo *gwinfo) { return (get_state(gwinfo, 'H')); }
int StGWLineType(StGWWinInfo *gwinfo)     { return (get_state(gwinfo, 'L')); }
int StGWDrawMode(StGWWinInfo *gwinfo)     { return (get_state(gwinfo, 'M')); }

ColorCode StGWDrawColor(StGWWinInfo *gwinfo)
{
  return ((ColorCode) get_state(gwinfo, 'D'));
}

ColorCode StGWBackColor(StGWWinInfo *gwinfo)
{
  return ((ColorCode) get_state(gwinfo, 'B'));
}

int StGWUseColor(StGWWinInfo *gwinfo) { return (get_state(gwinfo, 'C')); }

VOID StGWGetLineWidth(StGWWinInfo *gwinfo, int *width)
{
  if (gwinfo == nil) return;
  if (width != nil) *width = gwinfo->lineWidth;
}

static VOID set_state(StGWWinInfo *gwinfo, int which, int value)
{
  int changed;
  
  if (gwinfo == nil) return;
  switch (which) {
  case 'L': if ((changed = (value != gwinfo->lineType))) gwinfo->lineType = value;  break;
  case 'M': if ((changed = (value != gwinfo->drawMode))) gwinfo->drawMode = value;  break;
  case 'D': if ((changed = (value != gwinfo->drawColor))) gwinfo->drawColor = value; break;
  case 'B': if ((changed = (value != gwinfo->backColor))) gwinfo->backColor = value; break;
  case 'C': if ((changed = (value != gwinfo->use_color))) 
              gwinfo->use_color = (StScreenHasColor()) ? value : FALSE;
            break;
  }
  if (changed) SetHardwareState(gwinfo);
  if (changed && which == 'B') DrawGWGrowBox(gwinfo);
}

VOID StGWSetLineType(StGWWinInfo *gwinfo, int type)
{
  set_state(gwinfo, 'L', type);
}

VOID StGWSetDrawMode(StGWWinInfo *gwinfo, int mode)
{
  set_state(gwinfo, 'M', mode);
}

VOID StGWSetDrawColor(StGWWinInfo *gwinfo, ColorCode color)
{
  set_state(gwinfo, 'D', (ColorCode) color);
}

VOID StGWSetBackColor(StGWWinInfo *gwinfo, ColorCode color)
{
  set_state(gwinfo, 'B', (ColorCode) color);
}

VOID StGWSetUseColor(StGWWinInfo *gwinfo, int use)
{
  set_state(gwinfo, 'C', use);
}

VOID StGWSetLineWidth(StGWWinInfo *gwinfo, int width)
{
  int changed;
  
  if (gwinfo == nil) return;
  changed = (width != gwinfo->lineWidth);
  if (changed) {
    gwinfo->lineWidth = width;
    SetHardwareState(gwinfo);
  }
}

VOID StGWReverseColors(StGWWinInfo *gwinfo)
{
  ColorCode backColor, drawColor;
  LVAL object;
  
  object = StGWGetObject(gwinfo);

  backColor = StGWBackColor(gwinfo);
  drawColor = StGWDrawColor(gwinfo);
  if (backColor != drawColor) {
    StGWSetBackColor(gwinfo, drawColor);
    StGWSetDrawColor(gwinfo, backColor);
    StGWObRedraw(object);
  }
}

VOID StGWGetViewRect(StGWWinInfo *gwinfo, int *left, int *top, int *width, int *height)
{
  WindowPtr w;
  
  if (gwinfo != nil && (w = gwinfo->window) != nil) {
    if (left != nil) *left = w->portRect.left;
    if (top != nil) *top = w->portRect.top;
    if (width != nil) *width = w->portRect.right - w->portRect.left - 15;
    if (height != nil) *height = w->portRect.bottom - w->portRect.top;
    if (height != nil && StGWHasHscroll(gwinfo)) *height -= 15;
  }
  else {
    if (left != nil) *left = 0;
    if (top != nil) *top = 0;
    if (width != nil) *width = 1;
    if (height != nil) *height = 1;
  }
}

/**************************************************************************/
/**                                                                      **/
/**                       Window Scrolling Functions                     **/
/**                                                                      **/
/**************************************************************************/

static VOID set_has_scroll(StGWWinInfo *gwinfo, int which, int has, int size)
{
  WindowPtr w;
  GrafPtr savePort;
  int view_size, value, old_has;
  ControlHandle ctl;
  LVAL object;
  
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;

  GetPort(&savePort);
  SetPort(w);
  
  if (has && size <= 0) xlfail("size must be positive");
  object = StGWGetObject(gwinfo);

  ClipRect(&w->portRect);

  ctl = (which == 'H') ? gwinfo->hscroll : gwinfo->vscroll;
   
  old_has = (which == 'H') ? gwinfo->hasHscroll : gwinfo->hasVscroll;
  if (which == 'H') gwinfo->hasHscroll = has;
  else gwinfo->hasVscroll = has;
    
  if (has) {
    if (w == FrontWindow()) HiliteControl(ctl, 0);
    else HiliteControl(ctl, 255);
    if (which == 'H') {
      if (! old_has) gwinfo->canvasHeight -= 15;
      if (gwinfo->hasVscroll) {
        StGWGetViewRect(gwinfo, nil, nil, nil, &view_size);
        value = (gwinfo->canvasHeight - view_size > 0)
              ? gwinfo->canvasHeight - view_size : 0;
        SetControlMaximum(gwinfo->vscroll, value);
      }
      gwinfo->canvasWidth = size;
      StGWGetViewRect(gwinfo, nil, nil, &view_size, nil);
    }
    else {
      gwinfo->canvasHeight = size;
      StGWGetViewRect(gwinfo, nil, nil, nil, &view_size);
    }
    value = (size - view_size > 0) ? size - view_size : 0;
    SetControlMaximum(ctl, value);
    ShowControl(ctl);
  }
  else {
    if (which == 'H') {
      StGWGetViewRect(gwinfo, nil, nil, &gwinfo->canvasWidth, nil);
      if (old_has) gwinfo->canvasHeight += 15;
      StGWSetScroll(gwinfo, 0, gwinfo->view_v, TRUE);
    }
    else {
      StGWGetViewRect(gwinfo, nil, nil, nil, &gwinfo->canvasHeight);
      StGWSetScroll(gwinfo, gwinfo->view_h, 0, TRUE);
    }
    HideControl(ctl);
    SetControlMaximum(ctl, 0);
  }
  InvalRect(&w->portRect);
  reset_clip_rect(gwinfo);
  SetPort(savePort);

  StGWObResize(object);
}

VOID StGWSetHasHscroll(StGWWinInfo *gwinfo, int has, int size)
{
  set_has_scroll(gwinfo, 'H', has, size);
}

VOID StGWSetHasVscroll(StGWWinInfo *gwinfo, int has, int size)
{
  set_has_scroll(gwinfo, 'V', has, size);
}

int StGWHasHscroll(StGWWinInfo *gwinfo)
{
  if (gwinfo == nil) return(FALSE);
  else return(gwinfo->hasHscroll);
}

int StGWHasVscroll(StGWWinInfo *gwinfo)
{
  if (gwinfo == nil) return(FALSE);
  else return(gwinfo->hasVscroll);
}

VOID StGWSetScroll(StGWWinInfo *gwinfo, int h, int v, int move)
{
  WindowPtr w;
  GrafPtr savePort;
  Rect r;
  
  if (gwinfo == nil || (w = gwinfo->window) == nil) return;
  gwinfo->view_h = (gwinfo->hasHscroll) ? h : 0;
  gwinfo->view_v = (gwinfo->hasVscroll) ? v : 0;
    
  GetPort(&savePort);
  SetPort(w);
  ClipRect(&w->portRect);
  SetControlValue(gwinfo->hscroll, h);
  SetControlValue(gwinfo->vscroll, v);
  gwinfo->view_h = GetControlValue(gwinfo->hscroll);
  gwinfo->view_v = GetControlValue(gwinfo->vscroll);
  SetOrigin(gwinfo->view_h, gwinfo->view_v);
  reset_clip_rect(gwinfo);
  if (move) {
    r = scroll_bar_bounds(w, 'H');
    MoveControl(gwinfo->hscroll, r.left, r.top);
    r = scroll_bar_bounds(w, 'V');
    MoveControl(gwinfo->vscroll, r.left, r.top);
  }
  SetPort(savePort);
}

VOID StGWGetScroll(StGWWinInfo *gwinfo, int *h, int *v)
{
  
  if (gwinfo != nil) {
    if (h != nil) *h = gwinfo->view_h;
    if (v != nil) *v = gwinfo->view_v;
  }
}

VOID StGWSetHscrollIncs(StGWWinInfo *gwinfo, int inc, int pageInc)
{
  if (gwinfo == nil) return;
  gwinfo->h_scroll_inc[0] = inc;
  gwinfo->h_scroll_inc[1] = pageInc;
}

VOID StGWGetHscrollIncs(StGWWinInfo *gwinfo, int *inc, int *pageInc)
{
  if (gwinfo == nil) return;
  if (inc != 0) *inc = gwinfo->h_scroll_inc[0];
  if (pageInc != 0) *pageInc = gwinfo->h_scroll_inc[1];
}

VOID StGWSetVscrollIncs(StGWWinInfo *gwinfo, int inc, int pageInc)
{
  if (gwinfo == nil) return;
  gwinfo->v_scroll_inc[0] = inc;
  gwinfo->v_scroll_inc[1] = pageInc;
}

VOID StGWGetVscrollIncs(StGWWinInfo *gwinfo, int *inc, int *pageInc)
{
  if (gwinfo == nil) return;
  if (inc != 0) *inc = gwinfo->v_scroll_inc[0];
  if (pageInc != 0) *pageInc = gwinfo->v_scroll_inc[1];
}

/**************************************************************************/
/**                                                                      **/
/**                    Graph Window RefCon Functions                     **/
/**                                                                      **/
/**************************************************************************/

VOID StGWSetRefCon(StGWWinInfo *gwinfo, long x)
{
  if (gwinfo == nil) return;
  else gwinfo->RefCon = x;
}

long StGWGetRefCon(StGWWinInfo *gwinfo)
{
  if (gwinfo == nil) return((long) nil);
  else return(gwinfo->RefCon);
}
 
VOID StGWSetObject(StGWWinInfo *gwinfo, LVAL x)
{
  if (gwinfo == nil) return;
  else gwinfo->Object = x;
}

LVAL IViewWindowGetObject(WindowPtr w)
{
  StGWWinInfo *gwinfo = (StGWWinInfo *) GetWRefCon(w);
  if (gwinfo == nil) return(NIL);
  else return(gwinfo->Object);
}

LVAL StGWGetObject(StGWWinInfo *gwinfo)
{
  return((gwinfo != nil) ? gwinfo->Object : NIL);
}


/**************************************************************************/
/**                                                                      **/
/**                    Graph Window Color Functions                      **/
/**                                                                      **/
/**************************************************************************/

#define NumBasicColors 8
#define NumRGBColors 256
# define MULTIPLIER 62535

static int NumColors;

typedef struct {
  union {
    long old;
    RGBColor rgb;
  } value;
  long refcon;
} ctab_entry;

static ctab_entry *ctable;

VOID init_mac_colors(void)
{
  NumColors = NumBasicColors;
  if (StScreenHasColor()) NumColors += NumRGBColors;
  ctable = (ctab_entry *) StCalloc(NumColors, sizeof(ctab_entry));
  
  ctable[0].value.old = whiteColor;
  ctable[1].value.old = blackColor;
  ctable[2].value.old = redColor;
  ctable[3].value.old = greenColor;
  ctable[4].value.old = blueColor;
  ctable[5].value.old = cyanColor;
  ctable[6].value.old = magentaColor;
  ctable[7].value.old = yellowColor;
}

int StGWMakeColor(double red, double green, double blue, long refcon)
{
  int index;
  
  for (index = NumBasicColors; 
       index < NumColors && StGWGetColRefCon(index) != (long) nil; 
       index++);
  if (index >= NumColors) return(-1);
  else {
    StGWSetColRefCon(index, refcon);
    ctable[index].value.rgb.red = MULTIPLIER * red;
    ctable[index].value.rgb.green = MULTIPLIER * green;
    ctable[index].value.rgb.blue = MULTIPLIER * blue;
    return(index);
  }
}

VOID StGWFreeColor(unsigned int index)
{
  if (index < NumColors && index >= NumBasicColors)
    StGWSetColRefCon(index, (long) nil);
  else xlfail("can't free standard color");
}

VOID StGWSetColRefCon(unsigned int index, long rc)
{
  if (index < NumColors) ctable[index].refcon = rc;
}

long StGWGetColRefCon(unsigned int index)
{	
  if (index < NumColors) return(ctable[index].refcon);
  else return((long) nil);
}

static long get_color(unsigned int index)
{
  if (index < NumBasicColors) return(ctable[index].value.old);
  else return(ctable[1].value.old);
}

static RGBColor *get_rgb_color(unsigned int index)
{
  if (index >= NumColors) xlfail("bad rgb color");
  return(&ctable[index].value.rgb);
}

VOID set_fore_color(StGWWinInfo *gwinfo)
{
  if (gwinfo->drawColor < NumBasicColors)
    ForeColor(get_color(gwinfo->drawColor));
  else RGBForeColor(get_rgb_color(gwinfo->drawColor));
}

VOID set_back_color(StGWWinInfo *gwinfo)
{
  if (gwinfo->backColor < NumBasicColors)
    BackColor(get_color(gwinfo->backColor));
  else RGBBackColor(get_rgb_color(gwinfo->backColor));
}