home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (c) Ken W. Marks 1989, 1990.
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <X11/IntrinsicP.h>
- #include <X11/StringDefs.h>
- #include <Chaos.h>
- #include <LocalDefs.h>
- #include <LabelP.h>
-
- #define LABEL_ALLOC_COUNT 10
-
- static void LabelInitialize();
- static void LabelRedisplay();
- static void LabelDestroy();
-
- #define offset(field) XtOffset(LabelWidget, field)
-
- static XtResource resources[] = {
- {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
- offset(label.foreground), XtRString, "XtDefaultForeground"},
- {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
- offset(label.font), XtRString, "chaos-bold"},
- {XtNlabel, XtCLabel, XtRString, sizeof(String),
- offset(label.label), XtRString, NULL},
- {XtNinternalWidth, XtCWidth, XtRDimension, sizeof(Dimension),
- offset(label.internal_width), XtRImmediate, (caddr_t) 4},
- {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
- offset(label.internal_height), XtRImmediate, (caddr_t) 2},
- };
-
- #define superclass (&simpleClassRec)
-
- LabelClassRec labelClassRec = {
- {
- /* core_class fields */
- /* superclass */ (WidgetClass) superclass,
- /* class_name */ "Label",
- /* widget_size */ sizeof(LabelRec),
- /* class_initialize */ NULL,
- /* class_part_initialize */ NULL,
- /* class_inited */ FALSE,
- /* initialize */ LabelInitialize,
- /* initialize_hoo */ NULL,
- /* realize */ XtInheritRealize,
- /* 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 */ LabelDestroy,
- /* resize */ NULL,
- /* expose */ LabelRedisplay,
- /* set_values */ NULL,
- /* 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 */ NULL,
- /* display_accelerator */ XtInheritDisplayAccelerator,
- /* extension */ NULL
- },
- {
- /* Simple class fields initialization */
- /* change_sensitive */ XtInheritChangeSensitive
- }
- };
-
- WidgetClass labelWidgetClass = (WidgetClass) & labelClassRec;
-
-
- /************************************************************/
- /******************** Private Procedures ********************/
- /************************************************************/
-
-
- static void LabelGetGC(w)
- LabelWidget w;
- {
- XGCValues values;
-
- values.foreground = w->label.foreground;
- values.background = w->core.background_pixel;
- values.font = w->label.font->fid;
-
- w->label.normal_gc = XtGetGC((Widget) w, (unsigned) GCForeground |
- GCBackground | GCFont, &values);
- }
-
-
- static void LabelSetSize(w)
- LabelWidget w;
- {
- XtWidgetGeometry my_request;
- char *label = w->label.label;
- char *start = label;
- char *end = label;
- int line_width;
-
- w->label.label_len = strlen(label);
- w->label.num_lines = 0;
- w->label.label_width = 0;
-
- while (1)
- {
- if (*end == '\0' || *end == '\n' || (*end == '\\' && end[1] == 'n'))
- {
- if (w->label.num_lines >= w->label.num_lines_alloced)
- {
- w->label.num_lines_alloced += LABEL_ALLOC_COUNT;
- w->label.line_start =
- (int *) realloc((char *) w->label.line_start,
- w->label.num_lines_alloced * sizeof(int));
- w->label.line_len = (int *) realloc((char *) w->label.line_len,
- w->label.num_lines_alloced * sizeof(int));
- }
- w->label.line_start[w->label.num_lines] = (int) (start - label);
- w->label.line_len[w->label.num_lines] = (int) (end - start);
-
- line_width = (end - start) * w->label.char_width;
- if (line_width > w->label.label_width)
- w->label.label_width = line_width;
- w->label.num_lines++;
- if (*end == '\0')
- break;
- if (*end != '\n')
- ++end;
- start = end + 1;
- }
- ++end;
- }
-
- w->label.label_height = w->label.num_lines * w->label.char_height;
-
- my_request.request_mode = CWWidth | CWHeight;
- my_request.width = w->label.label_width + 2 * w->label.internal_width;
- my_request.height = w->label.label_height + 2 * w->label.internal_height;
-
- XtMakeGeometryRequest((Widget) w, &my_request, NULL);
- }
-
-
- /* ARGSUSED */
- static void LabelInitialize(request, new)
- Widget request, new;
- {
- LabelWidget w = (LabelWidget) new;
- XFontStruct *fs = w->label.font;
-
- if (w->label.label == NULL)
- {
- eprintf("XtNLabel not set\n");
- abort();
- }
-
- w->label.label = STRCOPY(w->label.label);
- w->label.num_lines_alloced = LABEL_ALLOC_COUNT;
- w->label.line_start = (int *) malloc(LABEL_ALLOC_COUNT * sizeof(int));
- w->label.line_len = (int *) malloc(LABEL_ALLOC_COUNT * sizeof(int));
- w->label.char_height = fs->max_bounds.ascent + fs->max_bounds.descent;
- w->label.char_width = fs->max_bounds.width;
- w->label.label_x = w->label.internal_width;
- w->label.label_y = w->label.internal_height + fs->max_bounds.ascent;
-
- LabelGetGC(w);
- LabelSetSize(w);
- }
-
-
- /* ARGSUSED */
- static void LabelRedisplay(widget, event, region)
- Widget widget;
- XEvent *event;
- Region region;
- {
- LabelWidget w = (LabelWidget) widget;
- Display *dpy = XtDisplay(w);
- Window window = XtWindow(w);
- Position line_y = w->label.label_y;
- int *line_start = w->label.line_start;
- int *line_len = w->label.line_len;
- char *label;
- int ii;
-
- if (XtIsRealized(widget) == False)
- return;
-
- for (ii = 0; ii < w->label.num_lines; ++ii)
- {
- label = &(w->label.label[*line_start]);
-
- XDrawImageString(dpy, window, w->label.normal_gc, w->label.label_x,
- line_y, label, *line_len);
-
- line_y += w->label.char_height;
- ++line_start;
- ++line_len;
- }
- }
-
-
- static void LabelDestroy(widget)
- Widget widget;
- {
- LabelWidget w = (LabelWidget) widget;
-
- XtReleaseGC(widget, w->label.normal_gc);
- }
-
-
- /***********************************************************/
- /******************** Public Procedures ********************/
- /***********************************************************/
-
-
- void LabelChangeLabel(widget, label)
- Widget widget;
- char *label;
- {
- LabelWidget w = (LabelWidget) widget;
-
- if (label == NULL)
- {
- eprintf("Label may not be NULL\n");
- abort();
- }
-
- free(w->label.label);
- w->label.label = STRCOPY(label);
- LabelSetSize(w);
- LabelRedisplay(widget, (XEvent *) NULL, (Region) NULL);
- }
-