File: AuxPanel.c

package info (click to toggle)
kinput2 3.1-10.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,012 kB
  • ctags: 5,584
  • sloc: ansic: 49,959; makefile: 106; sh: 68
file content (606 lines) | stat: -rw-r--r-- 16,475 bytes parent folder | download | duplicates (10)
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
#ifndef lint
static char *rcsid = "$Id: AuxPanel.c,v 1.23 1994/09/01 06:33:41 ishisone Exp $";
#endif
/*
 * Copyright (c) 1990  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

/* Copyright 1991 NEC Corporation, Tokyo, Japan.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of NEC Corporation
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.  NEC 
 * Corporation makes no representations about the suitability of this
 * software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 * NEC CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN 
 * NO EVENT SHALL NEC CORPORATION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF 
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 
 * OTHER TORTUOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 
 * PERFORMANCE OF THIS SOFTWARE. 
 *
 * Author: Akira Kon, NEC Corporation.  (kon@d1.bs2.mt.nec.co.jp)
 */

/*
  $BLdBjE@(B
  $B!&J8;zIA2h$,A4It=q$-D>$7=hM}$K$J$C$F$$$k!#(B
  $B!&$@$l$b(B Destroy $B$r8F$s$G$/$l$J$$$N$G$A$g$C$H$4$_$,;D$k!#(B
 */

#include <X11/IntrinsicP.h>
#include <X11/StringDefs.h>
#if XtSpecificationRelease > 4
#include <X11/Xfuncs.h>
#endif
#include "AuxPanelP.h"
#include "ConvDisp.h"
#include "WStr.h"

#define DEBUG_VAR debug_AuxPanel
#include "DebugPrint.h"

static XtResource resources[] = {
#define offset(field) XtOffset(AuxPanelWidget, cpanel.field)
    { XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
	offset(foreground), XtRString, XtDefaultForeground },
    { XtNhorizontalSpacing, XtCSpacing, XtRDimension, sizeof(Dimension),
	offset(hspace), XtRString, "6" },
    { XtNverticalSpacing, XtCSpacing, XtRDimension, sizeof(Dimension),
	offset(vspace), XtRString, "4" },
    { XtNdefaultWidth, XtCDefaultWidth, XtRDimension, sizeof(Dimension),
	offset(defaultwidth), XtRString, "400" },
    { XtNcursor, XtCCursor, XtRCursor, sizeof(Cursor),
	offset(cursor), XtRImmediate, (XtPointer)None },
    { XtNcallback, XtCCallback, XtRCallback, sizeof(XtPointer),
	offset(callback), XtRCallback, NULL },
#undef offset
};

static void Initialize(), Destroy();
static void Realize();
static void Redisplay();
static void Resize();
static Boolean SetValues();
static XtGeometryResult QueryGeometry();
static void InsertChild();

static void GetInvGC();
static void ComputeSize();
static void Layout();

static void freeDisplaySegments();
static void UpdateAuxDisplay();

static CompositeClassExtensionRec CompositeExtension = {
    /* next_extension		*/	NULL,
    /* record_type		*/	NULLQUARK,
    /* version			*/	XtCompositeExtensionVersion,
    /* record_size		*/	sizeof(CompositeClassExtensionRec),
    /* accept_objects		*/	True,
};

AuxPanelClassRec auxPanelClassRec = {
  { /* core fields */
    /* superclass		*/	(WidgetClass) &compositeClassRec,
    /* class_name		*/	"AuxPanel",
    /* widget_size		*/	sizeof(AuxPanelRec),
    /* class_initialize		*/	NULL,
    /* class_part_initialize	*/	NULL,
    /* class_inited		*/	FALSE,
    /* initialize		*/	Initialize,
    /* initialize_hook		*/	NULL,
    /* realize			*/	Realize,
    /* actions			*/	NULL,
    /* num_actions		*/	0,
    /* resources		*/	resources,
    /* num_resources		*/	XtNumber(resources),
    /* xrm_class		*/	NULLQUARK,
    /* compress_motion		*/	TRUE,
    /* compress_exposure	*/	TRUE,
    /* compress_enterleave	*/	TRUE,
    /* visible_interest		*/	FALSE,
    /* destroy			*/	Destroy,
    /* resize			*/	Resize,
    /* expose			*/	Redisplay,
    /* set_values		*/	SetValues,
    /* set_values_hook		*/	NULL,
    /* set_values_almost	*/	XtInheritSetValuesAlmost,
    /* get_values_hook		*/	NULL,
    /* accept_focus		*/	NULL,
    /* version			*/	XtVersion,
    /* callback_private		*/	NULL,
    /* tm_table			*/	NULL,
    /* query_geometry		*/	QueryGeometry,
    /* display_accelerator	*/	XtInheritDisplayAccelerator,
    /* extension		*/	NULL
  },
  { /* composite fields */
    /* geometry_manager		*/	NULL,
    /* change_managed		*/	NULL,
    /* insert_child		*/	InsertChild,
    /* delete_child		*/	XtInheritDeleteChild,
    /* extension		*/	(XtPointer)&CompositeExtension,
  },
  { /* auxpanel fields */
    /* empty			*/	0
  }
};

WidgetClass auxPanelWidgetClass = (WidgetClass)&auxPanelClassRec;

/* ARGSUSED */
static void
Initialize(req, new, args, num_args)
Widget req;
Widget new;
ArgList args;
Cardinal *num_args;
{
    AuxPanelWidget cpw = (AuxPanelWidget)new;

    TRACE(("AuxPanel:Initialize()\n"));

    cpw->cpanel.displayobj = NULL;
    cpw->cpanel.dispauxsegments = NULL;
    cpw->cpanel.dispauxsegmentsize = 0;
    cpw->cpanel.numauxsegments = 0;
    GetInvGC(cpw);
}

static void
Destroy(w)
Widget w;
{
    AuxPanelWidget cpw = (AuxPanelWidget)w;

    TRACE(("AuxPanel:Destroy()\n"));

    freeDisplaySegments(cpw);
    if (cpw->cpanel.invgc != NULL) XtReleaseGC(w, cpw->cpanel.invgc);
}

static void
Realize(w, mask, value)
Widget w;
XtValueMask *mask;
XSetWindowAttributes *value;
{
    AuxPanelWidget cpw = (AuxPanelWidget)w;
    CompositeWidgetClass super = (CompositeWidgetClass)XtClass(w)->core_class.superclass;
    String params[1];
    Cardinal num_params;

    TRACE(("AuxPanel:Realize()\n"));

    if (cpw->cpanel.displayobj == NULL) {
	params[0] = XtClass(w)->core_class.class_name;
	num_params = 1;
	XtAppErrorMsg(XtWidgetToApplicationContext(w),
		      "childError", "number", "WidgetError",
		      "%s: has no child (ConvDisplayObject)",
		      params, &num_params);
    }

    if (cpw->cpanel.cursor != None) {
	*mask |= CWCursor;
	value->cursor = cpw->cpanel.cursor;
    }

    (*super->core_class.realize)(w, mask, value);
}

/* ARGSUSED */
static void
Redisplay(w, ev, region)
Widget w;
XEvent *ev;
Region region;
{
    AuxPanelWidget cpw = (AuxPanelWidget)w;
    XExposeEvent *event = (XExposeEvent *)ev;
    DisplaySegment *dseg;
    int cheight, hspace, vspace;
    int x, i;

    TRACE(("AuxPanel:Redisplay()\n"));

    cheight = cpw->cpanel.fontheight;
    hspace = cpw->cpanel.hspace;
    vspace = cpw->cpanel.vspace;

    if (event->y + event->height < vspace / 2
	|| cheight + vspace / 2 < event->y)
      return;

    for (i = 0, x = hspace / 2, dseg = cpw->cpanel.dispauxsegments;
	 i < cpw->cpanel.numauxsegments ; i++, dseg++) {
      if (x < event->x + event->width && event->x < x + dseg->width) {
	dseg->redrawpos = 0;
      }
      else {
	dseg->redrawpos = -1; /* $B=q$-D>$9I,MW$,$J$$(B */
      }
      x += dseg->width;
    }

    UpdateAuxDisplay(cpw);
}

static void
Resize(w)
Widget w;
{
    AuxPanelWidget cpw = (AuxPanelWidget)w;

    TRACE(("AuxPanel:Resize()\n"));

    Layout(cpw, True, True);
}

/* ARGSUSED */
static Boolean
SetValues(cur, req, wid, args, num_args)
Widget cur;
Widget req;
Widget wid;
ArgList args;
Cardinal *num_args;
{
    AuxPanelWidget old = (AuxPanelWidget)cur;
    AuxPanelWidget new = (AuxPanelWidget)wid;
    Boolean redisplay = False;

    TRACE(("AuxPanel:SetValues()\n"));

    if (new->cpanel.displayobj == NULL) return False;

    if (new->cpanel.foreground != old->cpanel.foreground ||
	new->core.background_pixel != old->core.background_pixel) {
	XtVaSetValues(new->cpanel.displayobj,
		      XtNforeground, new->cpanel.foreground,
		      XtNbackground, new->core.background_pixel,
		      NULL);
	redisplay = True;
    }
		      
    if (new->cpanel.numauxsegments > 0 ||
	new->cpanel.hspace != old->cpanel.hspace ||
	new->cpanel.vspace != old->cpanel.vspace ||
	new->cpanel.defaultwidth != old->cpanel.defaultwidth) {
	Layout(new, True, True);
	redisplay = True;
    }

    if (new->cpanel.cursor != old->cpanel.cursor && XtIsRealized(wid)) {
	XDefineCursor(XtDisplay(wid), XtWindow(wid), new->cpanel.cursor);
    }

    return redisplay;
}

static XtGeometryResult
QueryGeometry(w, req, ret)
Widget w;
XtWidgetGeometry *req;
XtWidgetGeometry *ret;
{
    Dimension width, height;
    Dimension owidth, oheight;

    TRACE(("AuxPanel:QueryGeometry()\n"));

    ret->request_mode = 0;

    if ((req->request_mode & (CWWidth | CWHeight)) == 0) return XtGeometryYes;

    width = (req->request_mode & CWWidth) ? req->width : w->core.width;
    height = (req->request_mode & CWHeight) ? req->height : w->core.height;

    owidth = width;
    oheight = height;
    ComputeSize((AuxPanelWidget)w,
		(req->request_mode & CWWidth) != 0,
		(req->request_mode & CWHeight) != 0,
		&width, &height);
    ret->request_mode = CWWidth | CWHeight;
    ret->width = width;
    ret->height = height;

    if (width != owidth || height != oheight) return XtGeometryAlmost;

    return XtGeometryYes;
}

static void
InsertChild(w)
Widget w;
{
    AuxPanelWidget cpw = (AuxPanelWidget)XtParent(w);
    CompositeWidgetClass super = (CompositeWidgetClass)XtClass(cpw)->core_class.superclass;
    String params[1];
    Cardinal num_params;

    TRACE(("AuxPanel:InsertChild()\n"));

    if (!XtIsSubclass(w, convDisplayObjectClass)) {
	params[0] = XtClass(cpw)->core_class.class_name;
	num_params = 1;
	XtAppErrorMsg(XtWidgetToApplicationContext(w),
		      "childError", "class", "WidgetError",
		      "%s: child must be subclass of ConvDisplayObject",
		      params, &num_params);
    }
    if (cpw->composite.num_children != 0) {
	params[0] = XtClass(cpw)->core_class.class_name;
	num_params = 1;
	XtAppErrorMsg(XtWidgetToApplicationContext(w),
		      "childError", "number", "WidgetError",
		      "%s: can only take one child",
		      params, &num_params);
    }

    (*super->composite_class.insert_child)(w);

    cpw->cpanel.displayobj = w;
    cpw->cpanel.fontheight = CDLineHeight(w, (Position *)NULL);

#ifdef notdef
    {
    Pixel fg, bg;
    XtVaGetValues(w, XtNforeground, &fg, XtNbackground, &bg, NULL);
    GetInvGC(cpw, fg, bg);
    }
#endif

    if (cpw->cpanel.numauxsegments > 0) {
	Layout(cpw, cpw->core.width == 0, cpw->core.height == 0);
    }
}

static void
GetInvGC(cpw)
AuxPanelWidget cpw;
{
    XGCValues values;

    TRACE(("AuxPanel:GetInvGC()\n"));

    values.function = GXinvert;
    values.plane_mask = cpw->cpanel.foreground ^ cpw->core.background_pixel;
    cpw->cpanel.invgc = XtGetGC((Widget)cpw, GCFunction|GCPlaneMask, &values);
}

/* ARGSUSED */
static void
ComputeSize(cpw, resizex, resizey, width_inout, height_inout)
AuxPanelWidget cpw;
int resizex;
int resizey;
Dimension *width_inout;
Dimension *height_inout;
{
    int width, height;
    int i, minwidth = 0;
    DisplaySegment *dseg = cpw->cpanel.dispauxsegments;


    TRACE(("AuxPanel:ComputeSize()\n"));

    if (cpw->cpanel.displayobj == NULL) return;

    width = *width_inout;
    height = *height_inout;

    for (i = 0 ; i < cpw->cpanel.numauxsegments ; i++, dseg++) {
      minwidth += CDStringWidth(cpw->cpanel.displayobj, &dseg->seg, 0, -1);
    }
    minwidth += cpw->cpanel.hspace * 2;
    if (resizex) {
      width = minwidth;
    } else {
      width = (cpw->core.width < minwidth) ? minwidth : cpw->core.width;
    }
    height = cpw->cpanel.fontheight + cpw->cpanel.vspace;
    *width_inout = width;
    *height_inout = height;
}

static void
Layout(cpw, resizex, resizey)
AuxPanelWidget cpw;
int resizex;
int resizey;
{
    Dimension width, height;
    Dimension owidth, oheight;
    XtGeometryResult re;

    TRACE(("AuxPanel:Layout()\n"));

    if (cpw->cpanel.displayobj == NULL) return;

    width = cpw->core.width;
    height = cpw->core.height;
    ComputeSize(cpw, resizex, resizey, &width, &height);
    if (width != cpw->core.width || height != cpw->core.height) {
	owidth = width;
	oheight = height;
	re = XtMakeResizeRequest((Widget)cpw, owidth, oheight,
				 &width, &height);
	switch (re) {
	case XtGeometryYes:
	  /* no problem */
	  break;
	}
    }
}

/*- allocDisplaySegments: prepare specified number of display segments -*/
static void
allocDisplaySegments(cpw, n)
AuxPanelWidget cpw;
Cardinal n;
{
  if (cpw->cpanel.dispauxsegmentsize > n) return;
  n = ((n + 3) / 4) * 4 ;
  if (cpw->cpanel.dispauxsegments == NULL) {
    cpw->cpanel.dispauxsegments = 
      (DisplaySegment *)XtCalloc(n, sizeof(DisplaySegment));
  } else {
    cpw->cpanel.dispauxsegments = 
      (DisplaySegment *)XtRealloc((char *)cpw->cpanel.dispauxsegments,
				  n * sizeof(DisplaySegment));
    bzero((char *)(cpw->cpanel.dispauxsegments + cpw->cpanel.numauxsegments),
	  (n - cpw->cpanel.numauxsegments) * sizeof(DisplaySegment));
  }
  cpw->cpanel.dispauxsegmentsize = n;
}

/*- freeDisplaySegments: free display segments -*/
static void
freeDisplaySegments(cpw)
AuxPanelWidget cpw;
{
  Cardinal i;
  DisplaySegment *dseg = cpw->cpanel.dispauxsegments;

  for (i = 0 ; i < cpw->cpanel.dispauxsegmentsize ; i++, dseg++) {
    if (dseg->seg.data != NULL) XtFree(dseg->seg.data);
  }
  XtFree((char *)cpw->cpanel.dispauxsegments);
  cpw->cpanel.dispauxsegments = (DisplaySegment *)0;
  cpw->cpanel.dispauxsegmentsize = 0;
}

/*- copyString: copy ICString -*/
static void
copyString(from, to)
ICString *from;
ICString *to;
{
  if (to->data != NULL) XtFree(to->data);
  *to = *from;
  to->data = XtMalloc(to->nbytes);
  (void)bcopy(from->data, to->data, to->nbytes);
}

static void
UpdateAuxDisplay(cpw)
AuxPanelWidget cpw;
{
  Widget dispobj = cpw->cpanel.displayobj;
  DisplaySegment *dseg;
  int hspace, vspace;
  int x, y;
  Cardinal i;

  if (dispobj == NULL) return;

  hspace = cpw->cpanel.hspace;
  vspace = cpw->cpanel.vspace;

  x = hspace / 2;
  y = vspace / 2;

  for (i = 0, dseg = cpw->cpanel.dispauxsegments;
       i < cpw->cpanel.numauxsegments ; i++, dseg++) {
    if (dseg->redrawpos >= 0) {
      int offset = 0;
      int idx = dseg->redrawpos;

      if (dseg->width == 0) {
	dseg->width = CDStringWidth(dispobj, &dseg->seg, 0, -1);
      }
      if (idx > 0) {
	offset = CDStringWidth(dispobj, &dseg->seg, 0, idx);
      }
      CDDrawString(dispobj, (Widget)cpw, &dseg->seg, 0, -1, x + offset, y);
    }
    x += dseg->width;
  }
  return;
}

/*
 * Public Functions
 */

/* ARGSUSED */
void
APanelStart(w, segments, nseg, curseg, cursorpos)
Widget w;
ICString *segments;
Cardinal nseg, curseg, cursorpos;
{
    AuxPanelWidget cpw = (AuxPanelWidget)w;
    Cardinal i;
    DisplaySegment *dseg;

    cpw->cpanel.numauxsegments = nseg;
    allocDisplaySegments(cpw, nseg);
    for (i = 0, dseg = cpw->cpanel.dispauxsegments ; i < nseg ; i++, dseg++) {
      copyString(segments +i, &dseg->seg);
      dseg->redrawpos = 0;
      dseg->width = 0;
    }
    Layout(cpw, True, True);
    if (XtIsRealized(w)) XClearWindow(XtDisplay(w), XtWindow(w));
    return;
}

void
APanelEnd(w)
Widget w;
{
  AuxPanelWidget cpw = (AuxPanelWidget)w;

  cpw->cpanel.numauxsegments = 0;
  return;
}

/* ARGSUSED */
void
APanelChange(w, segments, nseg, curseg, cursorpos)
Widget w;
ICString *segments;
Cardinal nseg, curseg, cursorpos;
{
  AuxPanelWidget cpw = (AuxPanelWidget)w;
  Cardinal i;
  DisplaySegment *dseg;

  cpw->cpanel.numauxsegments = nseg;
  allocDisplaySegments(cpw, nseg);
  for (i = 0, dseg = cpw->cpanel.dispauxsegments ; i < nseg ; i++, dseg++) {
    /* $BA4=q$-49$($K$J$C$F$7$^$&!#(BICCompareSegment $B$,;H$($k$J$i(B
       $B;H$$$?$$$N$@$,!#(B */
    copyString(segments +i, &dseg->seg);
    dseg->redrawpos = 0;
    dseg->width = 0;
  }
  Layout(cpw, False, False);
  if (XtIsRealized(w)) XClearWindow(XtDisplay(w), XtWindow(w));
  UpdateAuxDisplay(cpw);
  return;
}