diff options
author | Kaleb Keithley <kaleb@freedesktop.org> | 2003-11-14 16:48:48 +0000 |
---|---|---|
committer | Kaleb Keithley <kaleb@freedesktop.org> | 2003-11-14 16:48:48 +0000 |
commit | 81ad93fde745d556aaa3880deabf3674bb3db49e (patch) | |
tree | 24120e85c169c58dedc31dfb61df4e533b058b99 /src | |
parent | b6e0280a57c4dfa572770241a42fe6a476109516 (diff) | |
download | xorg-lib-libXaw-81ad93fde745d556aaa3880deabf3674bb3db49e.tar.gz |
Initial revisionXORG-STABLE
Diffstat (limited to 'src')
-rw-r--r-- | src/Actions.c | 1132 | ||||
-rw-r--r-- | src/Converters.c | 698 | ||||
-rw-r--r-- | src/DisplayList.c | 2250 | ||||
-rw-r--r-- | src/OS.c | 60 | ||||
-rw-r--r-- | src/Pixmap.c | 984 | ||||
-rw-r--r-- | src/Private.h | 154 | ||||
-rw-r--r-- | src/Tip.c | 631 |
7 files changed, 5909 insertions, 0 deletions
diff --git a/src/Actions.c b/src/Actions.c new file mode 100644 index 0000000..8e51063 --- /dev/null +++ b/src/Actions.c @@ -0,0 +1,1132 @@ +/* + * Copyright (c) 1998 by The XFree86 Project, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Except as contained in this notice, the name of the XFree86 Project shall + * not be used in advertising or otherwise to promote the sale, use or other + * dealings in this Software without prior written authorization from the + * XFree86 Project. + */ + +/* $XFree86: xc/lib/Xaw/Actions.c,v 3.16 2001/10/30 04:56:38 paulo Exp $ */ + +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <X11/Xmd.h> +#include <X11/IntrinsicP.h> +#include <X11/StringDefs.h> +#include <X11/CoreP.h> +#include <X11/Constraint.h> +#include <X11/Xmu/CharSet.h> +#include <X11/Xmu/SysUtil.h> +#include <X11/Xfuncs.h> +#include "Private.h" + +#ifndef OLDXAW + +/* + * Definitions + */ +#define ERROR -2 +#define END -1 +#define BOOLEAN 0 +#define AND '&' +#define OR '|' +#define XOR '^' +#define NOT '~' +#define LP '(' +#define RP ')' + +/* + * Types + */ +/* boolean expressions */ +typedef struct _XawEvalInfo { + Widget widget; + XawActionResList *rlist; + XawActionVarList *vlist; + XawParseBooleanProc parse_proc; + XEvent *event; + char *cp, *lp; + int token; + Bool value; +} XawEvalInfo; + +/* resources */ +typedef struct _XawActionRes { + XrmQuark qname; + XrmQuark qtype; + Cardinal size; +} XawActionRes; + +struct _XawActionResList { + WidgetClass widget_class; + XawActionRes **resources; + Cardinal num_common_resources; + Cardinal num_constraint_resources; +}; + +/* variables */ +typedef struct _XawActionVar { + XrmQuark qname; + XrmQuark qvalue; +} XawActionVar; + +struct _XawActionVarList { + Widget widget; + Cardinal num_variables; + XawActionVar **variables; +}; + +/* + * Private methods + */ +/* expressions */ +static int get_token(XawEvalInfo*); +static Bool expr(XawEvalInfo*); +static Bool and(XawEvalInfo*); +static Bool prim(XawEvalInfo*); + +/* resources */ +static String XawConvertActionRes(XawActionResList*, Widget w, String); + +static String _XawEscapeActionVarValue(String); +static String _XawUnescapeActionVarValue(String); +static XawActionResList *_XawCreateActionResList(WidgetClass); +static XawActionResList *_XawFindActionResList(WidgetClass); +static void _XawBindActionResList(XawActionResList*); +static XawActionRes *_XawFindActionRes(XawActionResList*, Widget, String); +static int qcmp_action_resource_list(_Xconst void*, _Xconst void*); +static int bcmp_action_resource_list(_Xconst void*, _Xconst void*); +static int qcmp_action_resource(_Xconst void*, _Xconst void*); +static int bcmp_action_resource(_Xconst void*, _Xconst void*); + +/* variables */ +static String XawConvertActionVar(XawActionVarList*, String); +static void XawDeclareActionVar(XawActionVarList*, String, String); + +static XawActionVarList *_XawCreateActionVarList(Widget); +static XawActionVarList *_XawFindActionVarList(Widget); +static XawActionVar *_XawCreateActionVar(XawActionVarList*, String); +static XawActionVar *_XawFindActionVar(XawActionVarList*, String); +static void _XawDestroyActionVarList(Widget, XtPointer, XtPointer); + +/* + * Initialization + */ +/* resources */ +static XawActionResList **resource_list; +static Cardinal num_resource_list; + +/* variables */ +static XawActionVarList **variable_list; +static Cardinal num_variable_list; + +/* + * Implementation + */ +/* + * Start of Boolean Expression Evaluation Implementation Code + */ +Bool +XawParseBoolean(Widget w, String param, XEvent *event, Bool *succed) +{ + char *tmp = param; + int value; + + if (!param) + return (False); + + value = (int)strtod(param, &tmp); + if (*tmp == '\0') + return (value); + + if (XmuCompareISOLatin1(param, "true") == 0 + || XmuCompareISOLatin1(param, "yes") == 0 + || XmuCompareISOLatin1(param, "on") == 0 + || XmuCompareISOLatin1(param, "in") == 0 + || XmuCompareISOLatin1(param, "up") == 0) + return (True); + else if (XmuCompareISOLatin1(param, "false") == 0 + || XmuCompareISOLatin1(param, "no") == 0 + || XmuCompareISOLatin1(param, "off") == 0 + || XmuCompareISOLatin1(param, "out") == 0 + || XmuCompareISOLatin1(param, "down") == 0) + ; + else if (XmuCompareISOLatin1(param, "my") == 0 + || XmuCompareISOLatin1(param, "mine") == 0) + return (event->xany.window == XtWindow(w)); + else if (XmuCompareISOLatin1(param, "faked") == 0) + return (event->xany.send_event != 0); + else + *succed = False; + + return (False); +} + +Bool +XawBooleanExpression(Widget w, String param, XEvent *event) +{ + XawEvalInfo info; + Bool retval; + + if (!param) + return (False); + + info.widget = w; + + info.rlist = XawGetActionResList(XtClass(w)); + info.vlist = XawGetActionVarList(w); + + /* + * Verify widget class, in case we will allow the parse proc procedure + * as a widget class element, or if we allow overriding the default + * parse boolean proc. + */ + info.parse_proc = XawParseBoolean; + + info.event = event; + info.cp = info.lp = param; + +#ifdef DIAGNOSTIC + fprintf(stderr, "(*) Parsing expression \"%s\"\n", param); +#endif + + (void)get_token(&info); + if (info.token == ERROR) + return (False); + retval = expr(&info); + + return (info.token != ERROR ? retval : False); +} + +static int +get_token(XawEvalInfo *info) +{ + int ch; + char *p, name[256]; + + info->lp = info->cp; + + /*COSTCOND*/ + while (1) /* eat white spaces */ + { + ch = *info->cp++; + if (isspace(ch)) + continue; + break; + } + + switch (ch) + { + case AND: case OR: case XOR: case NOT: case LP: case RP: + return (info->token = ch); + } + + /* It's a symbol name, resolve it. */ + if (ch == XAW_PRIV_VAR_PREFIX || isalnum(ch) || ch == '_' || ch == '\\') + { + Bool succed = True; + + p = info->cp - 1; + + while ((ch = *info->cp) && (isalnum(ch) || ch == '_')) + ++info->cp; + + strncpy(name, p, XawMin((int)sizeof(name) - 1, + (unsigned)(info->cp - p))); + name[XawMin((int)sizeof(name) -1, info->cp - p)] = '\0'; + + if (name[0] == XAW_PRIV_VAR_PREFIX) + { + String value = XawConvertActionVar(info->vlist, name); + + info->value = info->parse_proc(info->widget, value, info->event, + &succed) & 1; + } + else + { + info->value = info->parse_proc(info->widget, name, info->event, + &succed) & 1; + if (!succed) + { + String value = + XawConvertActionRes(info->rlist, info->widget, + name[0] == '\\' ? &name[1] : name); + /* '\\' may have been used to escape a resource name. + */ + + succed = True; + info->value = info->parse_proc(info->widget, value, info->event, + &succed) & 1; + if (!succed) + { + /* not a numeric value or boolean string */ + info->value = True; + succed = True; + } + } + } + if (succed) + return (info->token = BOOLEAN); + } + else if (ch == '\0') + return (info->token = END); + + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "evaluate(): bad token \"%c\" at \"%s\"", ch, info->cp - 1); + + XtAppWarning(XtWidgetToApplicationContext(info->widget), msg); + } + + return (info->token = ERROR); +} + +static Bool +expr(XawEvalInfo *info) +{ + Bool left = and(info); + + for (;;) + switch (info->token) + { + case OR: + (void)get_token(info); + left |= and(info); + break; + case XOR: + (void)get_token(info); + left ^= and(info); + break; + default: + return (left); + } + /* NOTREACHED */ +} + +static Bool +and(XawEvalInfo *info) +{ + Bool left = prim(info); + + for (;;) + switch (info->token) + { + case AND: + (void)get_token(info); + left &= prim(info); + break; + default: + return (left); + } + /* NOTREACHED */ +} + +static Bool +prim(XawEvalInfo *info) +{ + Bool e; + + switch (info->token) + { + case BOOLEAN: + e = info->value; + (void)get_token(info); + return (e); + case NOT: + (void)get_token(info); + return (!prim(info)); + case LP: + (void)get_token(info); + e = expr(info); + if (info->token != RP) + { + char msg[256]; + + info->token = ERROR; + XmuSnprintf(msg, sizeof(msg), + "evaluate(): expecting ), at \"%s\"", info->lp); + XtAppWarning(XtWidgetToApplicationContext(info->widget), msg); + return (False); + } + (void)get_token(info); + return (e); + case END: + return (True); + default: + { + char msg[256]; + + info->token = ERROR; + XmuSnprintf(msg, sizeof(msg), + "evaluate(): sintax error, at \"%s\"", info->lp); + XtAppWarning(XtWidgetToApplicationContext(info->widget), msg); + } return (False); + } + /* NOTREACHED */ +} + +/* + * Start of Resources Implementation Code + */ +void +XawSetValuesAction(Widget w, XEvent *event, + String *params, Cardinal *num_params) +{ + Arg *arglist; + Cardinal num_args, count; + XawActionResList *rlist; + XawActionVarList *vlist; + XawActionRes *resource; + XrmValue from, to; + String value; + char c_1; + short c_2; + int c_4; +#ifdef LONG64 + long c_8; +#endif + + if (!(*num_params & 1)) + { + XawPrintActionErrorMsg("set-values", w, params, num_params); + return; + } + + if (!XawBooleanExpression(w, params[0], event)) + return; + + rlist = XawGetActionResList(XtClass(w)); + vlist = XawGetActionVarList(w); + + num_args = 0; + arglist = (Arg *)XtMalloc(sizeof(Arg) * ((*num_params) >> 1)); + + for (count = 1; count < *num_params; count += 2) + { + if ((resource = _XawFindActionRes(rlist, w, params[count])) == NULL) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "set-values(): bad resource name \"%s\"", + params[count]); + XtAppWarning(XtWidgetToApplicationContext(w), msg); + continue; + } + value = XawConvertActionVar(vlist, params[count + 1]); + from.size = strlen(value) + 1; + from.addr = value; + to.size = resource->size; + switch (to.size) + { + case 1: to.addr = (XPointer)&c_1; break; + case 2: to.addr = (XPointer)&c_2; break; + case 4: to.addr = (XPointer)&c_4; break; +#ifdef LONG64 + case 8: to.addr = (XPointer)&c_8; break; +#endif + default: + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "set-values(): bad resource size for \"%s\"", + params[count]); + XtAppWarning(XtWidgetToApplicationContext(w), msg); + } continue; + } + + if (strcmp(XtRString, XrmQuarkToString(resource->qtype)) == 0) +#ifdef LONG64 + c_8 = (long)from.addr; +#else + c_4 = (int)from.addr; +#endif + else if (!XtConvertAndStore(w, XtRString, &from, + XrmQuarkToString(resource->qtype), &to)) + continue; + + switch (to.size) + { + case 1: + XtSetArg(arglist[num_args], XrmQuarkToString(resource->qname), c_1); + break; + case 2: + XtSetArg(arglist[num_args], XrmQuarkToString(resource->qname), c_2); + break; + case 4: + XtSetArg(arglist[num_args], XrmQuarkToString(resource->qname), c_4); + break; +#ifdef LONG64 + case 8: + XtSetArg(arglist[num_args], XrmQuarkToString(resource->qname), c_8); + break; +#endif + } + ++num_args; + } + + XtSetValues(w, arglist, num_args); + XtFree((char *)arglist); +} + +void +XawGetValuesAction(Widget w, XEvent *event, + String *params, Cardinal *num_params) +{ + XawActionResList *rlist; + XawActionVarList *vlist; + String value; + Cardinal count; + + if (!(*num_params & 1)) + { + XawPrintActionErrorMsg("get-values", w, params, num_params); + return; + } + if (!XawBooleanExpression(w, params[0], event)) + return; + + rlist = XawGetActionResList(XtClass(w)); + vlist = XawGetActionVarList(w); + + for (count = 1; count < *num_params; count += 2) + { + if ((value = XawConvertActionRes(rlist, w, params[count + 1])) == NULL) + continue; + XawDeclareActionVar(vlist, params[count], value); + } +} + +void +XawDeclareAction(Widget w, XEvent *event, + String *params, Cardinal *num_params) +{ + XawActionVarList *vlist; + Cardinal count; + + if (!(*num_params & 1)) + { + XawPrintActionErrorMsg("declare", w, params, num_params); + return; + } + if (!XawBooleanExpression(w, params[0], event)) + return; + + vlist = XawGetActionVarList(w); + + for (count = 1; count < *num_params; count += 2) + XawDeclareActionVar(vlist, params[count], params[count + 1]); +} + +void +XawCallProcAction(Widget w, XEvent *event, + String *params, Cardinal *num_params) +{ + String *args; + Cardinal num_args; + + if (*num_params < 2) + { + XawPrintActionErrorMsg("call-proc", w, params, num_params); + return; + } + + if (*num_params && !XawBooleanExpression(w, params[0], event)) + return; + + if (*num_params > 2) + { + args = ¶ms[2]; + num_args = *num_params - 2; + } + else + { + args = NULL; + num_args = 0; + } + + XtCallActionProc(w, params[1], event, args, num_args); +} + +static String +XawConvertActionRes(XawActionResList *list, Widget w, String name) +{ + XawActionRes *resource; + XrmValue from, to; + Arg arg; + char c_1; + short c_2; + int c_4; +#ifdef LONG64 + long c_8; +#endif + + if ((resource = _XawFindActionRes(list, w, name)) == NULL) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "convert(): bad resource name \"%s\"", name); + XtAppWarning(XtWidgetToApplicationContext(w), msg); + return (NULL); + } + + from.size = resource->size; + switch (from.size) + { + case 1: + XtSetArg(arg, XrmQuarkToString(resource->qname), + from.addr = (XPointer)&c_1); + break; + case 2: + XtSetArg(arg, XrmQuarkToString(resource->qname), + from.addr = (XPointer)&c_2); + break; + case 4: + XtSetArg(arg, XrmQuarkToString(resource->qname), + from.addr = (XPointer)&c_4); + break; +#ifdef LONG64 + case 8: + XtSetArg(arg, XrmQuarkToString(resource->qname), + from.addr = (XPointer)&c_8); + break; +#endif + default: + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "convert(): bad resource size for \"%s\"", name); + XtAppWarning(XtWidgetToApplicationContext(w), name); + } return (NULL); + } + + XtGetValues(w, &arg, 1); + to.size = sizeof(String); + to.addr = NULL; + + if (strcmp(XtRString, XrmQuarkToString(resource->qtype)) == 0) + to.addr = *(char **)from.addr; + else if (!XtConvertAndStore(w, XrmQuarkToString(resource->qtype), + &from, XtRString, &to)) + return (NULL); + + return ((String)to.addr); +} + +void +XawPrintActionErrorMsg(String action_name, Widget w, + String *params, Cardinal *num_params) +{ + char msg[1024]; + unsigned int size, idx; + + size = XmuSnprintf(msg, sizeof(msg), "%s(): bad number of parameters.\n\t(", + action_name, action_name); + + idx = 0; + while (idx < *num_params - 1 && size < sizeof(msg)) + size += XmuSnprintf(&msg[size], sizeof(msg) - size, "%s, ", + params[idx++]); + if (*num_params) + XmuSnprintf(&msg[size], sizeof(msg) - size, "%s)", params[idx]); + else + XmuSnprintf(&msg[size], sizeof(msg) - size, ")"); + XtAppWarning(XtWidgetToApplicationContext(w), msg); +} + +XawActionResList * +XawGetActionResList(WidgetClass wc) +{ + XawActionResList *list; + + list = _XawFindActionResList(wc); + + if (!list) + list = _XawCreateActionResList(wc); + + return (list); +} + +static int +qcmp_action_resource_list(register _Xconst void *left, + register _Xconst void *right) +{ + return ((char *)((*(XawActionResList **)left)->widget_class) - + (char *)((*(XawActionResList **)right)->widget_class)); +} + +static XawActionResList * +_XawCreateActionResList(WidgetClass wc) +{ + XawActionResList *list; + + list = (XawActionResList *)XtMalloc(sizeof(XawActionResList)); + list->widget_class = wc; + list->num_common_resources = list->num_constraint_resources = 0; + list->resources = NULL; + + if (!resource_list) + { + num_resource_list = 1; + resource_list = (XawActionResList **)XtMalloc(sizeof(XawActionResList*)); + resource_list[0] = list; + } + else + { + ++num_resource_list; + resource_list = (XawActionResList **)XtRealloc((char *)resource_list, + sizeof(XawActionResList*) + * num_resource_list); + resource_list[num_resource_list - 1] = list; + qsort(resource_list, num_resource_list, sizeof(XawActionResList*), + qcmp_action_resource_list); + } + + _XawBindActionResList(list); + + return (list); +} + +static int +bcmp_action_resource_list(register _Xconst void *wc, + register _Xconst void *list) +{ + return ((char *)wc - (char *)((*(XawActionResList **)list)->widget_class)); +} + +static XawActionResList * +_XawFindActionResList(WidgetClass wc) +{ + XawActionResList **list; + + if (!resource_list) + return (NULL); + + list = (XawActionResList **)bsearch(wc, resource_list, + num_resource_list, + sizeof(XawActionResList*), + bcmp_action_resource_list); + + return (list ? *list : NULL); +} + +static int +qcmp_action_resource(register _Xconst void *left, + register _Xconst void *right) +{ + return (strcmp(XrmQuarkToString((*(XawActionRes **)left)->qname), + XrmQuarkToString((*(XawActionRes **)right)->qname))); +} + +static void +_XawBindActionResList(XawActionResList *list) +{ + XtResourceList xt_list, cons_list; + Cardinal i, num_xt, num_cons; + +#ifdef DIAGNOSTIC + fprintf(stderr, "(*) Creating resource list for class \'%s\'\n---------\n", + list->widget_class->core_class.class_name); +#endif + + XtGetResourceList(list->widget_class, &xt_list, &num_xt); + XtGetConstraintResourceList(list->widget_class, &cons_list, &num_cons); + list->num_common_resources = num_xt; + list->num_constraint_resources = num_cons; + + list->resources = (XawActionRes **) + XtMalloc(sizeof(XawActionRes*) * (num_xt + num_cons)); + +#ifdef DIAGNOSTIC + fprintf(stderr, "Common resources\n---\n"); +#endif + + for (i = 0; i < num_xt; i++) + { + list->resources[i] = (XawActionRes *)XtMalloc(sizeof(XawActionRes)); + list->resources[i]->qname = + XrmPermStringToQuark(xt_list[i].resource_name); + list->resources[i]->qtype = + XrmPermStringToQuark(xt_list[i].resource_type); + list->resources[i]->size = xt_list[i].resource_size; + +#ifdef DIAGNOSTIC + fprintf(stderr, "%-20s\t%-20s\t(%d)\n", + xt_list[i].resource_name, + xt_list[i].resource_type, + xt_list[i].resource_size); +#endif + } + +#ifdef DIAGNOSTIC + fprintf(stderr, "---\nContraint resources\n---"); +#endif + + for (; i < num_xt + num_cons; i++) + { + list->resources[i] = (XawActionRes *)XtMalloc(sizeof(XawActionRes)); + list->resources[i]->qname = + XrmPermStringToQuark(cons_list[i - num_xt].resource_name); + list->resources[i]->qtype = + XrmPermStringToQuark(cons_list[i - num_xt].resource_type); + list->resources[i]->size = cons_list[i - num_xt].resource_size; + +#ifdef DIAGNOSTIC + fprintf(stderr, "%-20s\t%-20s\t(%d)\n", + cons_list[i - num_xt].resource_name, + cons_list[i - num_xt].resource_type, + cons_list[i - num_xt].resource_size); +#endif + } + +#ifdef DIAGNOSTIC + fprintf(stderr, "---\n"); +#endif + + XtFree((char *)xt_list); + if (cons_list) + XtFree((char *)cons_list); + + qsort(list->resources, list->num_common_resources, sizeof(XawActionRes*), + qcmp_action_resource); + if (num_cons) + qsort(&list->resources[num_xt], list->num_constraint_resources, + sizeof(XawActionRes*), qcmp_action_resource); +} + +static int +bcmp_action_resource(register _Xconst void *string, + register _Xconst void *resource) +{ + return (strcmp((String)string, + XrmQuarkToString((*(XawActionRes **)resource)->qname))); +} + +static XawActionRes * +_XawFindActionRes(XawActionResList *list, Widget detail, String name) +{ + XawActionRes **res; + + if (!list->resources) + return (NULL); + + res = (XawActionRes **)bsearch(name, list->resources, + list->num_common_resources, + sizeof(XawActionRes*), bcmp_action_resource); + + if (!res && XtParent(detail) + && XtIsSubclass(XtParent(detail), constraintWidgetClass)) + { + XawActionResList *cons = XawGetActionResList(XtClass(XtParent(detail))); + + if (cons) + res = (XawActionRes **) + bsearch(name, &cons->resources[cons->num_common_resources], + cons->num_constraint_resources, + sizeof(XawActionRes*), bcmp_action_resource); + } + + return (res ? *res : NULL); +} + +/* + * Start of Variables Implementation Code + */ +/* For speed, only does memory allocation when really required */ +static String +_XawEscapeActionVarValue(String value) +{ + String escape; + + if (value[0] == '$' || value[0] == '\\') + { + escape = XtMalloc(strlen(value) + 2); + escape[0] = '\\'; + strcpy(escape + 1, value); + return (escape); + } + return (NULL); +} + +/* For speed, only does memory allocation when really required */ +static String +_XawUnescapeActionVarValue(String value) +{ + String unescape; + + if (value[0] == '\\') + { + unescape = XtMalloc(strlen(value)); + strcpy(unescape, value + 1); + return (unescape); + } + return (NULL); +} + +static void +XawDeclareActionVar(XawActionVarList *list, String name, String value) +{ + XawActionVar *variable; + String escape = NULL; + + if (name[0] != XAW_PRIV_VAR_PREFIX) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), "declare(): variable name must begin with " + "\'%c\', at %s = %s", XAW_PRIV_VAR_PREFIX, name, value); + XtAppWarning(XtWidgetToApplicationContext(list->widget), msg); + return; + } + variable = _XawFindActionVar(list, name); + if (!variable) + variable = _XawCreateActionVar(list, name); + if (value) + escape = _XawEscapeActionVarValue(value); + + if (variable->qvalue) + { + String val = escape ? escape : value; + + if (strcmp(XrmQuarkToString(variable->qvalue), val) == 0) + { + if (escape) + XtFree(escape); + return; + } + } + variable->qvalue = (escape ? XrmStringToQuark(escape) : + (value ? XrmStringToQuark(value) : NULLQUARK)); + if (escape) + XtFree(escape); +} + +static String +XawConvertActionVar(XawActionVarList *list, String name) +{ + XawActionVar *variable; + String unescape; + XrmQuark quark; + + if (name[0] != XAW_PRIV_VAR_PREFIX) + return (name); + + variable = _XawFindActionVar(list, name); + if (!variable || variable->qvalue == NULLQUARK) + return (name); + unescape = _XawUnescapeActionVarValue(XrmQuarkToString(variable->qvalue)); + if (unescape) + { + quark = XrmStringToQuark(unescape); + XtFree(unescape); + } + else + quark = variable->qvalue; + + return (XrmQuarkToString(quark)); +} + +XawActionVarList * +XawGetActionVarList(Widget w) +{ + XawActionVarList *list; + + list = _XawFindActionVarList(w); + if (!list) + list = _XawCreateActionVarList(w); + + return (list); +} + +static int +qcmp_action_variable_list(register _Xconst void *left, + register _Xconst void *right) +{ + return ((char *)((*(XawActionVarList **)left)->widget) - + (char *)((*(XawActionVarList **)right)->widget)); +} + +static XawActionVarList * +_XawCreateActionVarList(Widget w) +{ + XawActionVarList *list; + +#ifdef DIAGNOSTIC + fprintf(stderr, "(*) Creating action variable list for widget %s (%p)\n", + XtName(w), w); +#endif + + list = (XawActionVarList *)XtMalloc(sizeof(XawActionVarList)); + list->widget = w; + list->num_variables = 0; + list->variables = NULL; + + if (!variable_list) + { + num_variable_list = 1; + variable_list = (XawActionVarList **)XtMalloc(sizeof(XawActionVarList*)); + variable_list[0] = list; + } + else + { + ++num_variable_list; + variable_list = (XawActionVarList **) + XtRealloc((char *)variable_list, + sizeof(XawActionVarList *) * num_variable_list); + variable_list[num_variable_list - 1] = list; + qsort(variable_list, num_variable_list, sizeof(XawActionVarList*), + qcmp_action_variable_list); + } + + XtAddCallback(w, XtNdestroyCallback, _XawDestroyActionVarList, + (XtPointer)list); + + return (list); +} + +static int +bcmp_action_variable_list(register _Xconst void *widget, + register _Xconst void *list) +{ + return ((char *)widget - (char *)((*(XawActionVarList **)list)->widget)); +} + +static XawActionVarList * +_XawFindActionVarList(Widget w) +{ + XawActionVarList **list; + + if (!num_variable_list) + return (NULL); + + list = (XawActionVarList **)bsearch(w, variable_list, num_variable_list, + sizeof(XawActionVarList*), + bcmp_action_variable_list); + + return (list ? *list : NULL); +} + +static int +qcmp_action_variable(register _Xconst void *left, + register _Xconst void *right) +{ + return (strcmp(XrmQuarkToString((*(XawActionVar **)left)->qname), + XrmQuarkToString((*(XawActionVar **)right)->qname))); +} + +static XawActionVar * +_XawCreateActionVar(XawActionVarList *list, String name) +{ + XawActionVar *variable; + +#ifdef DIAGNOSTIC + fprintf(stderr, "(*) Creating action variable '%s' for widget %s (%p)\n", + name, XtName(list->widget), list->widget); +#endif + + variable = (XawActionVar *)XtMalloc(sizeof(XawActionVar)); + variable->qname = XrmStringToQuark(name); + variable->qvalue = NULLQUARK; + + if (!list->variables) + { + list->num_variables = 1; + list->variables = (XawActionVar **)XtMalloc(sizeof(XawActionVar*)); + list->variables[0] = variable; + } + else + { + ++list->num_variables; + list->variables = (XawActionVar **)XtRealloc((char *)list->variables, + sizeof(XawActionVar *) * + list->num_variables); + list->variables[list->num_variables - 1] = variable; + qsort(list->variables, list->num_variables, sizeof(XawActionVar*), + qcmp_action_variable); + } + return (variable); +} + +static int +bcmp_action_variable(register _Xconst void *string, + register _Xconst void *variable) +{ + return (strcmp((String)string, + XrmQuarkToString((*(XawActionVar **)variable)->qname))); +} + +static XawActionVar * +_XawFindActionVar(XawActionVarList *list, String name) +{ + XawActionVar **var; + + if (!list->variables) + return (NULL); + + var = (XawActionVar **)bsearch(name, list->variables, list->num_variables, + sizeof(XawActionVar*), bcmp_action_variable); + + return (var ? *var : NULL); +} + +/*ARGSUSED*/ +static void +_XawDestroyActionVarList(Widget w, XtPointer client_data, XtPointer call_data) +{ + XawActionVarList *list = (XawActionVarList *)client_data; + Cardinal i; + + for (i = 0; i < num_variable_list; i++) + if (variable_list[i] == list) + break; + if (i >= num_variable_list || list->widget != w + || variable_list[i]->widget != w) + { + XtWarning("destroy-variable-list(): Bad widget argument."); + return; + } + if (--num_variable_list > 0) + { + memmove(&variable_list[i], &variable_list[i + 1], + (num_variable_list - i) * sizeof(XawActionVarList *)); + variable_list = (XawActionVarList **) + XtRealloc((char *)variable_list, sizeof(XawActionVarList *) * + num_variable_list); + } + else + { + XtFree((char *)variable_list); + variable_list = NULL; + } + + XtFree((char *)list->variables); + XtFree((char *)list); +} + +#endif /* OLDXAW */ diff --git a/src/Converters.c b/src/Converters.c new file mode 100644 index 0000000..99ea5aa --- /dev/null +++ b/src/Converters.c @@ -0,0 +1,698 @@ +/* + * Copyright (c) 1998 by The XFree86 Project, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Except as contained in this notice, the name of the XFree86 Project shall + * not be used in advertising or otherwise to promote the sale, use or other + * dealings in this Software without prior written authorization from the + * XFree86 Project. + */ + +/* $XFree86: xc/lib/Xaw/Converters.c,v 3.14 1999/06/20 08:40:59 dawes Exp $ */ + +#include <stdio.h> +#include <X11/IntrinsicP.h> +#include <X11/StringDefs.h> +#include <X11/Xmu/CharSet.h> +#include <X11/Xmu/SysUtil.h> +#include <X11/Xaw/Simple.h> +#include <X11/Xaw/XawInit.h> +#include "Private.h" + +#ifndef OLDXAW + +/* + * Definitions + */ +#define done(type, value) \ +{ \ + if (toVal->addr != NULL) \ + { \ + if (toVal->size < sizeof(type)) \ + { \ + toVal->size = sizeof(type); \ + return (False); \ + } \ + *(type *)(toVal->addr) = (value); \ + } \ + else \ + { \ + static type static_val; \ + \ + static_val = (value); \ + toVal->addr = (XPointer)&static_val; \ + } \ + toVal->size = sizeof(type); \ + return (True); \ +} + +#define string_done(value) \ +{ \ + if (toVal->addr != NULL) \ + { \ + if (toVal->size < size) \ + { \ + toVal->size = size; \ + return (False); \ + } \ + strcpy((char *)toVal->addr, (value)); \ + } \ + else \ + toVal->addr = (XPointer)(value); \ + toVal->size = size; \ + return (True); \ +} + +/* + * Prototypes + */ +static Boolean _XawCvtAtomToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtBooleanToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtBoolToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtCARD32ToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtCardinalToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtDimensionToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtDisplayListToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtFontStructToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtIntToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtPixelToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtPixmapToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtShortToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtPositionToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtStringToDisplayList(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtStringToPixmap(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static Boolean _XawCvtUnsignedCharToString(Display*, XrmValue*, Cardinal*, + XrmValue*, XrmValue*, XtPointer*); +static void TypeToStringNoArgsWarning(Display*, String); + +/* + * Initialization + */ +static XtConvertArgRec PixelArgs[] = { + {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap), + sizeof(Colormap)}, +}; + +static XtConvertArgRec DLArgs[] = { + {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.screen), + sizeof(Screen *)}, + {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap), + sizeof(Colormap)}, + {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.depth), + sizeof(int)}, +}; +#endif /* OLDXAW */ + +static String XtCToolkitError = "ToolkitError"; +static String XtNconversionError = "conversionError"; + +#ifndef OLDXAW +static String XtNwrongParameters = "wrongParameters"; + +/* + * Implementation + */ +void +XawInitializeDefaultConverters(void) +{ + static Boolean first_time = True; + + if (first_time == False) + return; + + first_time = False; + + /* Replace with more apropriate converters */ + XtSetTypeConverter(XtRCallback, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRColormap, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRFunction, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRPointer, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRScreen, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRStringArray, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRVisual, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRWidget, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRWidgetList, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRWindow, XtRString, _XawCvtCARD32ToString, + NULL, 0, XtCacheNone, NULL); + + XtSetTypeConverter(XtRAtom, XtRString, _XawCvtAtomToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRBool, XtRString, _XawCvtBoolToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRBoolean, XtRString, _XawCvtBooleanToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRCardinal, XtRString, _XawCvtCardinalToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRDimension, XtRString, _XawCvtDimensionToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XawRDisplayList, XtRString, _XawCvtDisplayListToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRFontStruct, XtRString, _XawCvtFontStructToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRInt, XtRString, _XawCvtIntToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRPixel, XtRString, _XawCvtPixelToString, + &PixelArgs[0], XtNumber(PixelArgs), XtCacheNone, NULL); + XtSetTypeConverter(XtRPixmap, XtRString, _XawCvtPixmapToString, + &DLArgs[0], XtNumber(DLArgs), XtCacheNone, NULL); + XtSetTypeConverter(XtRPosition, XtRString, _XawCvtPositionToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRShort, XtRString, _XawCvtShortToString, + NULL, 0, XtCacheNone, NULL); + XtSetTypeConverter(XtRString, XawRDisplayList, _XawCvtStringToDisplayList, + &DLArgs[0], XtNumber(DLArgs), XtCacheAll, NULL); + XtSetTypeConverter(XtRString, XtRPixmap, _XawCvtStringToPixmap, + &DLArgs[0], XtNumber(DLArgs), XtCacheAll, NULL); + XtSetTypeConverter(XtRUnsignedChar, XtRString, _XawCvtUnsignedCharToString, + NULL, 0, XtCacheNone, NULL); +} +#endif /* OLDXAW */ + +void +XawTypeToStringWarning(Display *dpy, String type) +{ + char fname[64]; + String params[1]; + Cardinal num_params; + + XmuSnprintf(fname, sizeof(fname), "cvt%sToString", type); + + params[0] = type; + num_params = 1; + XtAppWarningMsg(XtDisplayToApplicationContext(dpy), + XtNconversionError, fname, XtCToolkitError, + "Cannot convert %s to String", + params, &num_params); +} + +#ifndef OLDXAW +static void +TypeToStringNoArgsWarning(Display *dpy, String type) +{ + char fname[64]; + String params[1]; + Cardinal num_params; + + XmuSnprintf(fname, sizeof(fname), "cvt%sToString", type); + + params[0] = type; + num_params = 1; + XtAppWarningMsg(XtDisplayToApplicationContext(dpy), + XtNconversionError, fname, + XtCToolkitError, + "%s to String conversion needs no extra arguments", + params, &num_params); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtBooleanToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[6]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRBoolean); + + XmuSnprintf(buffer, sizeof(buffer), "%s", + *(Boolean *)fromVal->addr ? XtEtrue : XtEfalse); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtBoolToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[6]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRBool); + + XmuSnprintf(buffer, sizeof(buffer), "%s", + *(Bool *)fromVal->addr ? XtEtrue : XtEfalse); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtPositionToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[7]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRPosition); + + XmuSnprintf(buffer, sizeof(buffer), "%d", *(Position *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtShortToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[7]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRShort); + + XmuSnprintf(buffer, sizeof(buffer), "%d", *(short *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtDimensionToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[6]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRDimension); + + XmuSnprintf(buffer, sizeof(buffer), "%u", *(Dimension *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtCARD32ToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[11]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, "CARD32"); + + XmuSnprintf(buffer, sizeof(buffer), "0x%08hx", *(int *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtIntToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[12]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRInt); + + XmuSnprintf(buffer, sizeof(buffer), "%d", *(int *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtCardinalToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[11]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRCardinal); + + XmuSnprintf(buffer, sizeof(buffer), "%u", *(Cardinal *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtAtomToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char *buffer = NULL; + static char *nullatom = "NULL"; + Cardinal size; + Atom atom; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRAtom); + + if (buffer && buffer != nullatom) + XFree(buffer); + + atom = *(Atom *)fromVal[0].addr; + if (atom == 0) + buffer = nullatom; + else if ((buffer = XGetAtomName(dpy, *(Atom *)fromVal[0].addr)) == NULL) + { + XawTypeToStringWarning(dpy, XtRAtom); + toVal->addr = NULL; + toVal->size = sizeof(String); + return (False); + } + + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtPixelToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[19]; + Cardinal size; + Colormap colormap; + XColor color; + + if (*num_args != 1) + { + XtAppWarningMsg(XtDisplayToApplicationContext(dpy), + XtNwrongParameters, "cvtPixelToString", + XtCToolkitError, + "Pixel to String conversion needs colormap argument", + NULL, NULL); + return (False); + } + + colormap = *(Colormap *)args[0].addr; + color.pixel = *(Pixel *)fromVal->addr; + + /* Note: + * If we know the visual type, we can calculate the xcolor + * without asking Xlib. + */ + XQueryColor(dpy, colormap, &color); + XmuSnprintf(buffer, sizeof(buffer), "rgb:%04hx/%04hx/%04hx", + color.red, color.green, color.blue); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtFontStructToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[128]; + Cardinal size; + Atom atom; + unsigned long value; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRFontStruct); + + if ((atom = XInternAtom(dpy, "FONT", True)) == None) + return (False); + + size = 0; + + if (XGetFontProperty(*(XFontStruct **)fromVal->addr, atom, &value)) + { + char *tmp = XGetAtomName(dpy, value); + + if (tmp) + { + XmuSnprintf(buffer, sizeof(buffer), "%s", tmp); + size = strlen(tmp); + XFree(tmp); + } + } + + if (size) + { + ++size; + string_done(buffer); + } + + XawTypeToStringWarning(dpy, XtRFontStruct); + + return (False); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtUnsignedCharToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + static char buffer[4]; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XtRUnsignedChar); + + XmuSnprintf(buffer, sizeof(buffer), "%u", + *(unsigned char *)fromVal->addr); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtStringToDisplayList(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + XawDisplayList *dlist; + Screen *screen; + Colormap colormap; + int depth; + String commands; + + if (*num_args != 3) + { + XtAppWarningMsg(XtDisplayToApplicationContext(dpy), + XtNwrongParameters, "cvtStringToDisplayList", + XtCToolkitError, + "String to DisplayList conversion needs screen, " + "colormap, and depth arguments", + NULL, NULL); + return (False); + } + + screen = *(Screen **)args[0].addr; + colormap = *(Colormap *)args[1].addr; + depth = *(int *)args[2].addr; + + commands = (String)(fromVal[0].addr); + + dlist = XawCreateDisplayList(commands, screen, colormap, depth); + + if (!dlist) + { + XtDisplayStringConversionWarning(dpy, (String)fromVal->addr, + XawRDisplayList); + toVal->addr = NULL; + toVal->size = sizeof(XawDisplayList*); + return (False); + } + + done(XawDisplayList*, dlist); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtDisplayListToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + String buffer; + Cardinal size; + + if (*num_args != 0) + TypeToStringNoArgsWarning(dpy, XawRDisplayList); + + buffer = XawDisplayListString(*(XawDisplayList **)(fromVal[0].addr)); + size = strlen(buffer) + 1; + + string_done(buffer); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtStringToPixmap(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + XawPixmap *xaw_pixmap; + Pixmap pixmap; + Screen *screen; + Colormap colormap; + int depth; + String name; + + if (*num_args != 3) + { + XtAppWarningMsg(XtDisplayToApplicationContext(dpy), + XtNwrongParameters, "cvtStringToPixmap", + XtCToolkitError, + "String to Pixmap conversion needs screen, " + "colormap, and depth arguments", + NULL, NULL); + return (False); + } + + screen = *(Screen **)args[0].addr; + colormap = *(Colormap *)args[1].addr; + depth = *(int *)args[2].addr; + + name = (String)(fromVal[0].addr); + + if (XmuCompareISOLatin1(name, "None") == 0) + pixmap = None; + else if (XmuCompareISOLatin1(name, "ParentRelative") == 0) + pixmap = ParentRelative; + else if (XmuCompareISOLatin1(name, "XtUnspecifiedPixmap") == 0) + pixmap = XtUnspecifiedPixmap; + else + { + xaw_pixmap = XawLoadPixmap(name, screen, colormap, depth); + if (!xaw_pixmap) + { + XtDisplayStringConversionWarning(dpy, (String)fromVal->addr, + XtRPixmap); + toVal->addr = (XtPointer)XtUnspecifiedPixmap; + toVal->size = sizeof(Pixmap); + return (False); + } + else + pixmap = xaw_pixmap->pixmap; + } + + done(Pixmap, pixmap); +} + +/*ARGSUSED*/ +static Boolean +_XawCvtPixmapToString(Display *dpy, XrmValue *args, Cardinal *num_args, + XrmValue *fromVal, XrmValue *toVal, + XtPointer *converter_data) +{ + XawPixmap *xaw_pixmap; + Pixmap pixmap; + Screen *screen; + Colormap colormap; + int depth; + String buffer = NULL; + Cardinal size; + + if (*num_args != 3) + { + XtAppWarningMsg(XtDisplayToApplicationContext(dpy), + XtNwrongParameters, "cvtPixmapToString", + XtCToolkitError, + "Pixmap to String conversion needs screen, " + "colormap, and depth arguments", + NULL, NULL); + return (False); + } + + screen = *(Screen **)args[0].addr; + colormap = *(Colormap *)args[1].addr; + depth = *(int *)args[2].addr; + + pixmap = *(Pixmap *)(fromVal[0].addr); + + switch (pixmap) + { + case None: + buffer = "None"; + break; + case ParentRelative: + buffer = "ParentRelative"; + break; + case XtUnspecifiedPixmap: + buffer = "XtUnspecifiedPixmap"; + break; + default: + xaw_pixmap = XawPixmapFromXPixmap(pixmap, screen, colormap, depth); + if (xaw_pixmap) + buffer = xaw_pixmap->name; + break; + } + + if (!buffer) + /* Bad Pixmap or Pixmap was not loaded by XawLoadPixmap() */ + return (_XawCvtCARD32ToString(dpy, args, num_args, fromVal, toVal, + converter_data)); + + size = strlen(buffer) + 1; + + string_done(buffer); +} + +#endif /* OLDXAW */ diff --git a/src/DisplayList.c b/src/DisplayList.c new file mode 100644 index 0000000..559095d --- /dev/null +++ b/src/DisplayList.c @@ -0,0 +1,2250 @@ +/* + * Copyright (c) 1998 by The XFree86 Project, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Except as contained in this notice, the name of the XFree86 Project shall + * not be used in advertising or otherwise to promote the sale, use or other + * dealings in this Software without prior written authorization from the + * XFree86 Project. + * + * Author: Paulo César Pereira de Andrade + */ + +/* $XFree86: xc/lib/Xaw/DisplayList.c,v 3.16 2000/09/26 15:56:54 tsi Exp $ */ + +#include <ctype.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <X11/IntrinsicP.h> +#include <X11/StringDefs.h> +#include <X11/CoreP.h> +#include <X11/Xfuncs.h> +#include <X11/Xmu/CharSet.h> +#include <X11/Xmu/SysUtil.h> +#include "Private.h" + +#ifndef OLDXAW + +/* + * Types + */ +typedef struct _XawDLProc XawDLProc; +typedef struct _XawDLData XawDLData; +typedef struct _XawDLInfo XawDLInfo; + +struct _XawDLProc { + XrmQuark qname; + String *params; + Cardinal num_params; + XawDisplayListProc proc; + XtPointer args; + XawDLData *data; +}; + +struct _XawDLData { + XawDLClass *dlclass; + XtPointer data; +}; + +struct _XawDLInfo { + String name; + XrmQuark qname; + XawDisplayListProc proc; +}; + +struct _XawDL { + XawDLProc **procs; + Cardinal num_procs; + XawDLData **data; + Cardinal num_data; + Screen *screen; + Colormap colormap; + int depth; + XrmQuark qrep; /* for cache lookup */ +}; + +struct _XawDLClass { + String name; + XawDLInfo **infos; + Cardinal num_infos; + XawDLArgsInitProc args_init; + XawDLArgsDestructor args_destructor; + XawDLDataInitProc data_init; + XawDLDataDestructor data_destructor; +}; + +/* + * Private Methods + */ +static XawDLClass *_XawFindDLClass(String); +static int qcmp_dlist_class(_Xconst void*, _Xconst void*); +static int bcmp_dlist_class(_Xconst void*, _Xconst void*); +static XawDLInfo *_XawFindDLInfo(XawDLClass*, String); +static int qcmp_dlist_info(_Xconst void*, _Xconst void*); +static int bcmp_dlist_info(_Xconst void*, _Xconst void*); +static void *_Xaw_Xlib_ArgsInitProc(String, String*, Cardinal*, + Screen*, Colormap, int); +static void _Xaw_Xlib_ArgsDestructor(Display*, String, XtPointer, + String*, Cardinal*); +static void *_Xaw_Xlib_DataInitProc(String, Screen*, Colormap, int); +static void _Xaw_Xlib_DataDestructor(Display*, String, XtPointer); + +/* + * Initialization + */ +static XawDLClass **classes; +static Cardinal num_classes; +static String xlib = "xlib"; + +/* + * Implementation + */ +void +XawRunDisplayList(Widget w, _XawDisplayList *list, + XEvent *event, Region region) +{ + XawDLProc *proc; + Cardinal i; + + if (!XtIsRealized(w)) + return; + + for (i = 0; i < list->num_procs; i++) + { + proc = list->procs[i]; + proc->proc(w, proc->args, proc->data->data, event, region); + } +} + +#define DLERR -2 +#define DLEOF -1 +#define DLEND 1 +#define DLNAME 2 +#define DLARG 3 +static char * +read_token(char *src, char *dst, Cardinal size, int *status) +{ + int ch; + Bool esc, quote; + Cardinal i; + + i = 0; + esc = quote = False; + + /*CONSTCOND*/ + while (1) + { + ch = *src; + if (ch != '\n' && isspace(ch)) + ++src; + else + break; + } + + for (; i < size - 1; src++) + { + ch = *src; + if (ch == '"') + { + if (quote) + { + quote = False; + continue; + } + quote = True; + continue; + } + if (ch == '\\') + { + if (esc) + { + dst[i++] = ch; + esc = False; + continue; + } + esc = True; + continue; + } + if (ch == '\0') + { + *status = DLEOF; + dst[i] = '\0'; + return (src); + } + else if (!esc) + { + if (!quote) + { + if (ch == ',') + { + *status = DLARG; + dst[i] = '\0'; + return (++src); + } + else if (ch == ' ' || ch == '\t') + { + *status = DLNAME; + dst[i] = '\0'; + return (++src); + } + else if (ch == ';' || ch == '\n') + { + *status = DLEND; + dst[i] = '\0'; + return (++src); + } + } + } + else + esc = False; + dst[i++] = ch; + } + + *status = DLERR; + dst[i] = '\0'; + + return (src); +} + +_XawDisplayList *XawCreateDisplayList(String string, Screen *screen, + Colormap colormap, int depth) +{ + _XawDisplayList *dlist; + XawDLClass *lc, *xlibc; + XawDLData *data; + XawDLInfo *info; + XawDLProc *proc; + char cname[64], fname[64], aname[1024]; + Cardinal i; + char *cp, *fp, *lp; + int status; + + xlibc = XawGetDisplayListClass(xlib); + if (!xlibc) + { + XawDisplayListInitialize(); + xlibc = XawGetDisplayListClass(xlib); + } + + dlist = (_XawDisplayList *)XtMalloc(sizeof(_XawDisplayList)); + dlist->procs = NULL; + dlist->num_procs = 0; + dlist->data = NULL; + dlist->num_data = 0; + dlist->screen = screen; + dlist->colormap = colormap; + dlist->depth = depth; + dlist->qrep = NULLQUARK; + if (!string || !string[0]) + return (dlist); + + cp = string; + + status = 0; + while (status != DLEOF) + { + lp = cp; + cp = read_token(cp, fname, sizeof(fname), &status); + + if (status != DLNAME && status != DLEND && status != DLEOF) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "Error parsing displayList at \"%s\"", lp); + XtAppWarning(XtDisplayToApplicationContext(DisplayOfScreen(screen)), + msg); + XawDestroyDisplayList(dlist); + return (NULL); + } + fp = fname; + /*CONSTCOND*/ + while (1) + { + fp = strchr(fp, ':'); + if (!fp || (fp == cp || fp[-1] != '\\')) + break; + ++fp; + } + if (fp) + { + XmuSnprintf(cname, fp - fname + 1, fname); + memmove(fname, fp + 1, strlen(fp)); + lc = cname[0] ? XawGetDisplayListClass(cname) : xlibc; + if (!lc) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "Cannot find displayList class \"%s\"", cname); + XtAppWarning(XtDisplayToApplicationContext + (DisplayOfScreen(screen)), msg); + XawDestroyDisplayList(dlist); + return (NULL); + } + } + else + lc = xlibc; + + if (status == DLEOF && !fname[0]) + break; + + if ((info = _XawFindDLInfo(lc, fname)) == NULL) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "Cannot find displayList procedure \"%s\"", fname); + XtAppWarning(XtDisplayToApplicationContext(DisplayOfScreen(screen)), + msg); + XawDestroyDisplayList(dlist); + return (NULL); + } + + proc = (XawDLProc *)XtMalloc(sizeof(XawDLProc)); + proc->qname = info->qname; + proc->params = NULL; + proc->num_params = 0; + proc->proc = info->proc; + proc->args = NULL; + proc->data = NULL; + + if (!dlist->procs) + { + dlist->num_procs = 1; + dlist->procs = (XawDLProc**)XtMalloc(sizeof(XawDLProc*)); + } + else + { + ++dlist->num_procs; + dlist->procs = (XawDLProc**) + XtRealloc((char *)dlist->procs, sizeof(XawDLProc*) * + dlist->num_procs); + } + dlist->procs[dlist->num_procs - 1] = proc; + + while (status != DLEND && status != DLEOF) + { + lp = cp; + cp = read_token(cp, aname, sizeof(aname), &status); + + if (status != DLARG && status != DLEND && status != DLEOF) + { + char msg[256]; + + XmuSnprintf(msg, sizeof(msg), + "Error parsing displayList at \"%s\"", lp); + XtAppWarning(XtDisplayToApplicationContext + (DisplayOfScreen(screen)), msg); + XawDestroyDisplayList(dlist); + return (NULL); + } + + if (!proc->num_params) + { + proc->num_params = 1; + proc->params = (String *)XtMalloc(sizeof(String)); + } + else + { + ++proc->num_params; + proc->params = (String *)XtRealloc((char *)proc->params, + sizeof(String) * + proc->num_params); + } + proc->params[proc->num_params - 1] = XtNewString(aname); + } + + /* verify if data is already created for lc */ + data = NULL; + for (i = 0; i < dlist->num_data; i++) + if (dlist->data[i]->dlclass == lc) + { + data = dlist->data[i]; + break; + } + + if (!data) + { + data = (XawDLData *)XtMalloc(sizeof(XawDLData)); + data->dlclass = lc; + if (lc->data_init) + data->data = lc->data_init(lc->name, screen, colormap, depth); + else + data->data = NULL; + + if (!dlist->data) + { + dlist->num_data = 1; + dlist->data = (XawDLData **)XtMalloc(sizeof(XawDLData*)); + } + else + { + ++dlist->num_data; + dlist->data = (XawDLData **) + XtRealloc((char *)dlist->data, sizeof(XawDLData*) * + dlist->num_data); + } + dlist->data[dlist->num_data - 1] = data; + } + + if (lc->args_init) + { + proc->args = lc->args_init(fname, proc->params, &proc->num_params, + screen, colormap, depth); + if (proc->args == XAWDL_CONVERT_ERROR) + { + char msg[256]; + + proc->args = NULL; + XmuSnprintf(msg, sizeof(msg), + "Cannot convert arguments to displayList function \"%s\"", fname); + XtAppWarning(XtDisplayToApplicationContext + (DisplayOfScreen(screen)), msg); + XawDestroyDisplayList(dlist); + return (NULL); + } + } + else + proc->args = NULL; + + proc->data = data; + } + + dlist->qrep = XrmStringToQuark(string); + return (dlist); +} + +String +XawDisplayListString(_XawDisplayList *dlist) +{ + if (!dlist || dlist->qrep == NULLQUARK) + return (""); + return (XrmQuarkToString(dlist->qrep)); +} + +void +XawDestroyDisplayList(_XawDisplayList *dlist) +{ + Cardinal i, j; + XawDLProc *proc; + XawDLData *data; + + if (!dlist) + return; + + for (i = 0; i < dlist->num_procs; i++) + { + proc = dlist->procs[i]; + data = proc->data; + + if (data) + { + if (data->dlclass->args_destructor) + data->dlclass->args_destructor(DisplayOfScreen(dlist->screen), + XrmQuarkToString(proc->qname), + proc->args, + proc->params, &proc->num_params); + if (data->data) + { + if (data->dlclass->data_destructor) + { + data->dlclass + ->data_destructor(DisplayOfScreen(dlist->screen), + data->dlclass->name, data->data); + data->data = NULL; + } + } + } + + for (j = 0; j < proc->num_params; j++) + XtFree(proc->params[j]); + if (proc->num_params) + XtFree((char *)proc->params); + XtFree((char *)proc); + } + + if (dlist->num_procs) + XtFree((char *)dlist->procs); + + XtFree((char *)dlist); +} + +/********************************************************************** + * If you want to implement your own class of procedures, look at + * the code bellow. + **********************************************************************/ +/* Start of Implementation of class "xlib" */ +typedef struct _XawXlibData { + GC gc; + unsigned long mask; + XGCValues values; + int shape; + int mode; + char *dashes; + /* these fields can be used for optimization, to + * avoid unnecessary coordinates recalculation. + */ + Position x, y; + Dimension width, height; +} XawXlibData; + +typedef struct _XawDLPosition { + Position pos; + short denom; + Boolean high; +} XawDLPosition; + +typedef struct _XawDLPositionPtr { + XawDLPosition *pos; + Cardinal num_pos; +} XawDLPositionPtr; + +typedef struct _XawDLArcArgs { + XawDLPosition pos[4]; + int angle1; + int angle2; +} XawDLArcArgs; + +typedef struct _XawDLStringArgs { + XawDLPosition pos[2]; + char *string; + int length; +} XawDLStringArgs; + +typedef struct _XawDLCopyArgs { + XawPixmap *pixmap; + XawDLPosition pos[6]; + int plane; +} XawDLCopyArgs; + +typedef struct _XawDLImageArgs { + XawPixmap *pixmap; + XawDLPosition pos[4]; + int depth; +} XawDLImageArgs; + +#define X_ARG(x) (Position)(((x).denom != 0) ? \ + ((float)XtWidth(w) * ((float)(x).pos / (float)(x).denom)) : \ + ((x).high ? XtWidth(w) - (x).pos : (x).pos)) +#define Y_ARG(x) (Position)(((x).denom != 0) ? \ + ((float)XtHeight(w) * ((float)(x).pos / (float)(x).denom)): \ + ((x).high ? XtHeight(w) - (x).pos : (x).pos)) +#define DRECT 0 +#define FRECT 1 +#define LINE 2 +#define GCFG 3 +#define GCBG 4 +#define FPOLY 5 +#define DARC 6 +#define FARC 7 +#define DLINES 8 +#define MASK 9 +#define UMASK 10 +#define LWIDTH 11 +#define POINT 12 +#define POINTS 13 +#define SEGMENTS 14 +#define ARCMODE 15 +#define COORDMODE 16 +#define SHAPEMODE 17 +#define LINESTYLE 18 +#define CAPSTYLE 19 +#define JOINSTYLE 20 +#define FILLSTYLE 21 +#define FILLRULE 22 +#define TILE 23 +#define STIPPLE 24 +#define TSORIGIN 25 +#define FUNCTION 26 +#define PLANEMASK 27 +#define DSTRING 28 +#define PSTRING 29 +#define FONT 30 +#define DASHES 31 +#define SUBWMODE 32 +#define EXPOSURES 33 +#define CLIPORIGIN 34 +#define CLIPMASK 35 +#define CLIPRECTS 36 +#define COPYAREA 37 +#define COPYPLANE 38 +#define IMAGE 39 + +static void +Dl1Point(Widget w, XtPointer args, XtPointer data, int id) +{ + XawDLPosition *pos = (XawDLPosition *)args; + XawXlibData *xdata = (XawXlibData *)data; + Display *display; + Window window; + Position x, y; + + x = X_ARG(pos[0]); + y = Y_ARG(pos[1]); + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + x += xpad; + x += xpad; + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + if (id == POINT) + XDrawPoint(display, window, xdata->gc, x, y); + else if (id == TSORIGIN) + { + xdata->values.ts_x_origin = x; + xdata->values.ts_y_origin = y; + xdata->mask |= GCTileStipXOrigin | GCTileStipYOrigin; + XSetTSOrigin(display, xdata->gc, x, y); + } + else if (id == CLIPORIGIN) + { + xdata->values.clip_x_origin = x; + xdata->values.clip_y_origin = y; + xdata->mask |= GCClipXOrigin | GCClipYOrigin; + XSetClipOrigin(display, xdata->gc, x, y); + } +} + +static void +Dl2Points(Widget w, XtPointer args, XtPointer data, int id) +{ + XawDLPosition *pos = (XawDLPosition *)args; + XawXlibData *xdata = (XawXlibData *)data; + Display *display; + Window window; + Position x1, y1, x2, y2; + + x1 = X_ARG(pos[0]); + y1 = Y_ARG(pos[1]); + x2 = X_ARG(pos[2]); + y2 = Y_ARG(pos[3]); + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + x1 += xpad; y1 += ypad; + x2 += xpad; y2 += ypad; + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + if (id == DRECT) + XDrawRectangle(display, window, xdata->gc, x1, y1, x2 - x1, y2 - y1); + else if (id == FRECT) + XFillRectangle(display, window, xdata->gc, x1, y1, x2 - x1, y2 - y1); + else if (id == LINE) + XDrawLine(display, window, xdata->gc, x1, y1, x2, y2); +} + +/* ARGSUSED */ +static void +DlLine(Widget w, XtPointer args, XtPointer data, XEvent *event, Region region) +{ + Dl2Points(w, args, data, LINE); +} + +/* ARGSUSED */ +static void +DlDrawRectangle(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + Dl2Points(w, args, data, DRECT); +} + +/* ARGSUSED */ +static void +DlFillRectangle(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + Dl2Points(w, args, data, FRECT); +} + +static void +DlXPoints(Widget w, XtPointer args, XtPointer data, int id) +{ + XawDLPositionPtr *pos_ptr = (XawDLPositionPtr *)args; + XawXlibData *xdata = (XawXlibData *)data; + XawDLPosition *pos; + XPoint points_buf[16]; + XPoint *points; + Display *display; + Window window; + Cardinal num_points, i, j; + + num_points = pos_ptr->num_pos>>1; + points = (XPoint *)XawStackAlloc(sizeof(XPoint) * num_points, points_buf); + + for (i = j = 0; i < num_points; i++, j = i << 1) + { + pos = &pos_ptr->pos[j]; + points[i].x = X_ARG(pos[0]); + points[i].y = Y_ARG(pos[1]); + } + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + if (xdata->mode != CoordModePrevious) + { + for (i = 0; i < num_points; i++) + { + points[i].x += xpad; + points[i].y += ypad; + } + } + else + { + points[0].x += xpad; + points[0].y += ypad; + } + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + if (id == FPOLY) + XFillPolygon(display, window, xdata->gc, points, num_points, + xdata->shape, xdata->mode); + else if (id == DLINES) + XDrawLines(display, window, xdata->gc, points, num_points, xdata->mode); + else if (id == POINTS) + XDrawPoints(display, window, xdata->gc, points, num_points, xdata->mode); + + XawStackFree(points, points_buf); +} + +/* ARGSUSED */ +static void +DlFillPolygon(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlXPoints(w, args, data, FPOLY); +} + +/* ARGSUSED */ +static void +DlDrawLines(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlXPoints(w, args, data, DLINES); +} + +/* ARGSUSED */ +static void +DlDrawPoints(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlXPoints(w, args, data, POINTS); +} + +/* ARGSUSED */ +static void +DlForeground(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + Pixel foreground = (Pixel)args; + + if (xdata->values.foreground != foreground) + { + xdata->mask |= GCForeground; + xdata->values.foreground = foreground; + XSetForeground(XtDisplayOfObject(w), xdata->gc, foreground); + } +} + +/* ARGSUSED */ +static void +DlBackground(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + Pixel background = (Pixel)args; + + if (xdata->values.background != background) + { + xdata->mask |= GCBackground; + xdata->values.background = background; + XSetBackground(XtDisplayOfObject(w), xdata->gc, background); + } +} + +static void +DlArc(Widget w, XtPointer args, XtPointer data, Bool fill) +{ + XawXlibData *xdata = (XawXlibData *)data; + XawDLArcArgs *arc = (XawDLArcArgs *)args; + Position x1, y1, x2, y2; + Display *display; + Window window; + + x1 = X_ARG(arc->pos[0]); + y1 = Y_ARG(arc->pos[1]); + x2 = X_ARG(arc->pos[2]); + y2 = Y_ARG(arc->pos[3]); + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + x1 += xpad; + y1 += ypad; + x2 += xpad; + y2 += ypad; + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + if (fill) + XFillArc(display, window, xdata->gc, x1, y1, x2 - x1, y2 - y1, + arc->angle1, arc->angle2); + else + XDrawArc(display, window, xdata->gc, x1, y1, x2 - x1, y2 - y1, + arc->angle1, arc->angle2); +} + +/* ARGSUSED */ +static void +DlDrawArc(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlArc(w, args, data, False); +} + +/* ARGSUSED */ +static void +DlFillArc(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlArc(w, args, data, True); +} + +/*ARGSUSED*/ +static void +DlMask(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + Display *display = XtDisplayOfObject(w); + + if (region) + XSetRegion(display, xdata->gc, region); + else if (event) + { + XRectangle rect; + + rect.x = event->xexpose.x; + rect.y = event->xexpose.y; + rect.width = event->xexpose.width; + rect.height = event->xexpose.height; + XSetClipRectangles(display, xdata->gc, 0, 0, &rect, 1, Unsorted); + } +} + +/* ARGSUSED */ +static void +DlUmask(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + + XSetClipMask(XtDisplayOfObject(w), xdata->gc, None); +} + +/* ARGSUSED */ +static void +DlLineWidth(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + unsigned line_width = (unsigned long)args; + + if (xdata->values.line_width != line_width) + { + xdata->mask |= GCLineWidth; + xdata->values.line_width = line_width; + XChangeGC(XtDisplayOfObject(w), xdata->gc, GCLineWidth, &xdata->values); + } +} + +/* ARGSUSED */ +static void +DlDrawPoint(Widget w, XtPointer args, XtPointer data, XEvent *event, Region region) +{ + Dl1Point(w, args, data, POINT); +} + +/* ARGSUSED */ +static void +DlDrawSegments(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawDLPositionPtr *pos_ptr = (XawDLPositionPtr *)args; + XawXlibData *xdata = (XawXlibData *)data; + XawDLPosition *pos; + XSegment *segments; + XSegment segments_buf[8]; + Display *display; + Window window; + Cardinal num_segments, i, j; + + num_segments = pos_ptr->num_pos>>2; + segments = (XSegment *)XawStackAlloc(sizeof(XSegment) * num_segments, segments_buf); + + for (i = j = 0; i < num_segments; i++, j = i << 2) + { + pos = &pos_ptr->pos[j]; + segments[i].x1 = X_ARG(pos[0]); + segments[i].y1 = Y_ARG(pos[1]); + segments[i].x2 = X_ARG(pos[2]); + segments[i].y2 = Y_ARG(pos[3]); + } + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + for (i = 0; i < num_segments; i++) + { + segments[i].x1 += xpad; + segments[i].y1 += ypad; + segments[i].x2 += xpad; + segments[i].y2 += ypad; + } + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + XDrawSegments(display, window, xdata->gc, segments, num_segments); + + XawStackFree(segments, segments_buf); +} + +/* ARGSUSED */ +static void +DlArcMode(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int arc_mode = (long)args; + + if (xdata->values.arc_mode != arc_mode) + { + xdata->mask |= GCArcMode; + xdata->values.arc_mode = arc_mode; + XSetArcMode(XtDisplayOfObject(w), xdata->gc, arc_mode); + } +} + +/* ARGSUSED */ +static void +DlCoordMode(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int mode = (long)args; + + xdata->mode = mode; +} + +/* ARGSUSED */ +static void +DlShapeMode(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int shape = (long)args; + + xdata->shape = shape; +} + +/* ARGSUSED */ +static void +DlLineStyle(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int line_style = (long)args; + + if (xdata->values.line_style != line_style) + { + xdata->mask |= GCLineStyle; + xdata->values.line_style = line_style; + XChangeGC(XtDisplayOfObject(w), xdata->gc, GCLineStyle, &xdata->values); + } +} + +/* ARGSUSED */ +static void +DlCapStyle(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int cap_style = (long)args; + + if (xdata->values.cap_style != cap_style) + { + xdata->mask |= GCCapStyle; + xdata->values.cap_style = cap_style; + XChangeGC(XtDisplayOfObject(w), xdata->gc, GCCapStyle, &xdata->values); + } +} + +/* ARGSUSED */ +static void +DlJoinStyle(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int join_style = (long)args; + + if (xdata->values.join_style != join_style) + { + xdata->mask |= GCJoinStyle; + xdata->values.join_style = join_style; + XChangeGC(XtDisplayOfObject(w), xdata->gc, GCJoinStyle, &xdata->values); + } +} + +/* ARGSUSED */ +static void +DlFillStyle(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int fill_style = (long)args; + + if (xdata->values.fill_style != fill_style) + { + xdata->mask |= GCFillStyle; + xdata->values.fill_style = fill_style; + XSetFillStyle(XtDisplayOfObject(w), xdata->gc, fill_style); + } +} + +/* ARGSUSED */ +static void +DlFillRule(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int fill_rule = (long)args; + + if (xdata->values.fill_rule != fill_rule) + { + xdata->mask |= GCFillRule; + xdata->values.fill_rule = fill_rule; + XSetFillRule(XtDisplayOfObject(w), xdata->gc, fill_rule); + } +} + +/* ARGSUSED */ +static void +DlTile(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + XawPixmap *pixmap = (XawPixmap *)args; + + if (pixmap && xdata->values.tile != pixmap->pixmap) + { + xdata->mask |= GCTile; + xdata->values.tile = pixmap->pixmap; + XSetTile(XtDisplayOfObject(w), xdata->gc, xdata->values.tile); + } +} + +/* ARGSUSED */ +static void +DlStipple(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + XawPixmap *pixmap = (XawPixmap *)args; + + if (pixmap && xdata->values.stipple != pixmap->pixmap) + { + xdata->mask |= GCStipple; + xdata->values.stipple = pixmap->pixmap; + XSetStipple(XtDisplayOfObject(w), xdata->gc, xdata->values.stipple); + } +} + +/* ARGSUSED */ +static void +DlTSOrigin(Widget w, XtPointer args, XtPointer data, XEvent *event, Region region) +{ + Dl1Point(w, args, data, TSORIGIN); +} + +/* ARGSUSED */ +static void +DlFunction(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int function = (long)args; + + if (function != xdata->values.function) + { + xdata->mask |= GCFunction; + xdata->values.function = function; + XSetFunction(XtDisplayOfObject(w), xdata->gc, function); + } +} + +/* ARGSUSED */ +static void +DlPlaneMask(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + unsigned long plane_mask = (unsigned long)args; + + if (xdata->values.plane_mask != plane_mask) + { + xdata->mask |= GCPlaneMask; + xdata->values.plane_mask = plane_mask; + XSetPlaneMask(XtDisplayOfObject(w), xdata->gc, plane_mask); + } +} + +static void +DlString(Widget w, XtPointer args, XtPointer data, Bool image) +{ + XawDLStringArgs *string = (XawDLStringArgs *)args; + XawXlibData *xdata = (XawXlibData *)data; + Display *display; + Window window; + Position x, y; + + x = X_ARG(string->pos[0]); + y = Y_ARG(string->pos[1]); + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + x += xpad; + x += xpad; + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + if (image) + XDrawImageString(display, window, xdata->gc, x, y, string->string, string->length); + else + XDrawString(display, window, xdata->gc, x, y, string->string, string->length); +} + +/* ARGSUSED */ +static void +DlDrawString(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlString(w, args, data, False); +} + +/* ARGSUSED */ +static void +DlPaintString(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlString(w, args, data, True); +} + +/* ARGSUSED */ +static void +DlFont(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + Font font = (Font)args; + + if (xdata->values.font != font) + { + xdata->mask |= GCFont; + xdata->values.font = font; + XSetFont(XtDisplayOfObject(w), xdata->gc, font); + } +} + +/* ARGSUSED */ +static void +DlDashes(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + char *dashes = args; + + if (xdata->dashes != dashes) + { + xdata->mask |= GCDashOffset | GCDashList; + xdata->dashes = dashes; + XSetDashes(XtDisplayOfObject(w), xdata->gc, 0, dashes + 1, *dashes); + } +} + +/* ARGSUSED */ +static void +DlSubwindowMode(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + int subwindow_mode = (long)args; + + if (xdata->values.subwindow_mode != subwindow_mode) + { + xdata->mask |= GCSubwindowMode; + xdata->values.subwindow_mode = subwindow_mode; + XSetSubwindowMode(XtDisplayOfObject(w), xdata->gc, subwindow_mode); + } +} + +/* ARGSUSED */ +static void +DlExposures(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + Bool graphics_exposures = (Bool)(long)args; + + if (xdata->values.graphics_exposures != graphics_exposures) + { + xdata->mask |= GCGraphicsExposures; + xdata->values.graphics_exposures = graphics_exposures; + XSetGraphicsExposures(XtDisplayOfObject(w), xdata->gc, graphics_exposures); + } +} + +/* ARGSUSED */ +static void +DlClipOrigin(Widget w, XtPointer args, XtPointer data, XEvent *event, Region region) +{ + Dl1Point(w, args, data, CLIPORIGIN); +} + +/* ARGSUSED */ +static void +DlClipMask(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawXlibData *xdata = (XawXlibData *)data; + XawPixmap *pixmap = (XawPixmap *)args; + Pixmap clip_mask; + + if (pixmap) + clip_mask = pixmap->mask ? pixmap->mask : pixmap->pixmap; + else + clip_mask = None; + + if (xdata->values.clip_mask != clip_mask) + { + xdata->mask |= GCClipMask; + XSetClipMask(XtDisplayOfObject(w), xdata->gc, clip_mask); + } +} + +/* ARGSUSED */ +static void +DlClipRectangles(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + XawDLPositionPtr *pos_ptr = (XawDLPositionPtr *)args; + XawXlibData *xdata = (XawXlibData *)data; + XawDLPosition *pos; + XRectangle *rects; + XRectangle rects_buf[8]; + Position x1, y1, x2, y2; + Cardinal num_rects, i, j; + + num_rects = pos_ptr->num_pos>>2; + rects = (XRectangle *)XawStackAlloc(sizeof(XRectangle) * num_rects, rects_buf); + + for (i = j = 0; i < num_rects; i++, j = i << 2) + { + pos = &pos_ptr->pos[j]; + x1 = X_ARG(pos[0]); + y1 = Y_ARG(pos[1]); + x2 = X_ARG(pos[2]); + y2 = Y_ARG(pos[3]); + rects[i].x = XawMin(x1, x2); + rects[i].y = XawMin(y1, y2); + rects[i].width = XawMax(x1, x2) - rects[i].x; + rects[i].height = XawMax(y1, y2) - rects[i].y; + } + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + for (i = 0; i < num_rects; i++) + { + rects[i].x += xpad; + rects[i].y += ypad; + } + } + + XSetClipRectangles(XtDisplayOfObject(w), xdata->gc, 0, 0, rects, num_rects, Unsorted); + + XawStackFree(rects, rects_buf); +} + +static void +DlCopy(Widget w, XtPointer args, XtPointer data, Bool plane) +{ + XawDLCopyArgs *copy = (XawDLCopyArgs *)args; + XawXlibData *xdata = (XawXlibData *)data; + int src_x, src_y, dst_x, dst_y, width, height, tmp1, tmp2; + + tmp1 = X_ARG(copy->pos[0]); + tmp2 = X_ARG(copy->pos[2]); + dst_x = XawMin(tmp1, tmp2); + width = XawMax(tmp1, tmp2) - dst_x; + + tmp1 = Y_ARG(copy->pos[1]); + tmp2 = Y_ARG(copy->pos[3]); + dst_y = XawMin(tmp1, tmp2); + height = XawMax(tmp1, tmp2) - dst_y; + + src_x = X_ARG(copy->pos[4]); + src_y = Y_ARG(copy->pos[5]); + + if (width <= 0) + { + if (copy->pixmap) + width = copy->pixmap->width; + else + { + if ((width = XtWidth(w) - src_x) < 0) + width = 0; + } + } + if (height <= 0) + { + if (copy->pixmap) + height = copy->pixmap->height; + else + { + if ((height = XtHeight(w) - src_y) < 0) + height = 0; + } + } + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + src_x += xpad; + src_y += ypad; + dst_x += xpad; + dst_y += ypad; + } + + if (plane) + XCopyPlane(XtDisplayOfObject(w), XtWindowOfObject(w), + copy->pixmap ? copy->pixmap->pixmap : XtWindowOfObject(w), + xdata->gc, src_x, src_y, width, height, dst_x, dst_y, + copy->plane ? copy->plane : 1); + else + XCopyArea(XtDisplayOfObject(w), + copy->pixmap ? copy->pixmap->pixmap : XtWindowOfObject(w), + XtWindowOfObject(w), xdata->gc, src_x, src_y, width, height, dst_x, dst_y); +} + +/* ARGSUSED */ +static void +DlCopyArea(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlCopy(w, args, data, False); +} + +/* ARGSUSED */ +static void +DlCopyPlane(Widget w, XtPointer args, XtPointer data, + XEvent *event, Region region) +{ + DlCopy(w, args, data, True); +} + +/*ARGSUSED*/ +/* Note: + * This function is destructive if you set the ts_x_origin, ts_y_origin, + * and/or clip-mask. It is meant to be the only function used in a display + * list. If you need to use other functions (and those values), be sure to + * set them after calling this function. + */ +static void +DlImage(Widget w, XtPointer args, XtPointer data, XEvent *event, Region region) +{ + XawDLImageArgs *image = (XawDLImageArgs *)args; + XawXlibData *xdata = (XawXlibData *)data; + int x, y, xs, ys, xe, ye, width, height; + Display *display; + Window window; + + width = image->pixmap->width; + height = image->pixmap->height; + xs = X_ARG(image->pos[0]); + ys = Y_ARG(image->pos[1]); + xe = X_ARG(image->pos[2]); + ye = Y_ARG(image->pos[3]); + + if (xe <= 0) + xe = xs + width; + if (ye <= 0) + ye = ys + height; + + if (!XtIsWidget(w)) + { + Position xpad, ypad; + + xpad = XtX(w) + XtBorderWidth(w); + ypad = XtY(w) + XtBorderWidth(w); + xe += xpad; + ye += ypad; + xe += xpad; + ye += ypad; + display = XtDisplayOfObject(w); + window = XtWindowOfObject(w); + } + else + { + display = XtDisplay(w); + window = XtWindow(w); + } + + for (y = ys; y < ye; y += height) + for (x = xs; x < xe; x += width) + { + XSetClipOrigin(display, xdata->gc, x, y); + if (image->pixmap->mask) + XSetClipMask(display, xdata->gc, image->pixmap->mask); + if (image->depth == 1) + XCopyPlane(display, image->pixmap->pixmap, window, xdata->gc, + 0, 0, XawMin(width, xe - x), XawMin(height, ye - y), + x, y, 1L); + else + XCopyArea(display, image->pixmap->pixmap, window, xdata->gc, 0, 0, + XawMin(width, xe - x), XawMin(height, ye - y), x, y); + } + + XSetClipMask(display, xdata->gc, None); +} + +typedef struct _Dl_init Dl_init; +struct _Dl_init { + String name; + XawDisplayListProc proc; + Cardinal id; +}; + +static Dl_init dl_init[] = +{ + {"arc-mode", DlArcMode, ARCMODE}, + {"background", DlBackground, GCBG}, + {"bg", DlBackground, GCBG}, + {"cap-style", DlCapStyle, CAPSTYLE}, + {"clip-mask", DlClipMask, CLIPMASK}, + {"clip-origin", DlClipOrigin, CLIPORIGIN}, + {"clip-rectangles", DlClipRectangles, CLIPRECTS}, + {"clip-rects", DlClipRectangles, CLIPRECTS}, + {"coord-mode", DlCoordMode, COORDMODE}, + {"copy-area", DlCopyArea, COPYAREA}, + {"copy-plane", DlCopyPlane, COPYPLANE}, + {"dashes", DlDashes, DASHES}, + {"draw-arc", DlDrawArc, DARC}, + {"draw-line", DlLine, LINE}, + {"draw-lines", DlDrawLines, DLINES}, + {"draw-point", DlDrawPoint, POINT}, + {"draw-points", DlDrawPoints, POINTS}, + {"draw-rect", DlDrawRectangle, DRECT}, + {"draw-rectangle", DlDrawRectangle, DRECT}, + {"draw-segments", DlDrawSegments, SEGMENTS}, + {"draw-string", DlDrawString, DSTRING}, + {"exposures", DlExposures, EXPOSURES}, + {"fg", DlForeground, GCFG}, + {"fill-arc", DlFillArc, FARC}, + {"fill-poly", DlFillPolygon, FPOLY}, + {"fill-polygon", DlFillPolygon, FPOLY}, + {"fill-rect", DlFillRectangle, FRECT}, + {"fill-rectangle", DlFillRectangle, FRECT}, + {"fill-rule", DlFillRule, FILLRULE}, + {"fill-style", DlFillStyle, FILLSTYLE}, + {"font", DlFont, FONT}, + {"foreground", DlForeground, GCFG}, + {"function", DlFunction, FUNCTION}, + {"image", DlImage, IMAGE}, + {"join-style", DlJoinStyle, JOINSTYLE}, + {"line", DlLine, LINE}, + {"line-style", DlLineStyle, LINESTYLE}, + {"line-width", DlLineWidth, LWIDTH}, + {"lines", DlDrawLines, DLINES}, + {"mask", DlMask, MASK}, + {"paint-string", DlPaintString, PSTRING}, + {"plane-mask", DlPlaneMask, PLANEMASK}, + {"point", DlDrawPoint, POINT}, + {"points", DlDrawPoints, POINTS}, + {"segments", DlDrawSegments, SEGMENTS}, + {"shape-mode", DlShapeMode, SHAPEMODE}, + {"stipple", DlStipple, STIPPLE}, + {"subwindow-mode", DlSubwindowMode, SUBWMODE}, + {"tile", DlTile, TILE}, + {"ts-origin", DlTSOrigin, TSORIGIN}, + {"umask", DlUmask, UMASK}, +}; + +void +XawDisplayListInitialize(void) +{ + static Bool first_time = True; + XawDLClass *lc; + Cardinal i; + + if (first_time == False) + return; + + first_time = False; + + lc = XawCreateDisplayListClass(xlib, + _Xaw_Xlib_ArgsInitProc, + _Xaw_Xlib_ArgsDestructor, + _Xaw_Xlib_DataInitProc, + _Xaw_Xlib_DataDestructor); + for (i = 0; i < sizeof(dl_init) / sizeof(dl_init[0]); i++) + (void)XawDeclareDisplayListProc(lc, dl_init[i].name, dl_init[i].proc); +} + +static int +bcmp_cvt_proc(register _Xconst void *string, + register _Xconst void *dlinfo) +{ + return (strcmp((String)string, ((Dl_init*)dlinfo)->name)); +} + +static long +read_int(char *cp, char **cpp) +{ + long value = 0, sign = 1; + + if (*cp == '-') + { + sign = -1; + ++cp; + } + else if (*cp == '+') + ++cp; + value = 0; + while (*cp >= '0' && *cp <= '9') + { + value = value * 10 + *cp - '0'; + ++cp; + } + if (cpp) + *cpp = cp; + return (value * sign); +} + +static void +read_position(char *arg, XawDLPosition *pos) +{ + int ch; + char *str = arg; + + ch = *str; + if (ch == '-' || ch == '+') + { + ++str; + if (ch == '-') + pos->high = True; + pos->pos = read_int(str, NULL); + } + else if (isdigit(ch)) + { + pos->pos = read_int(str, &str); + ch = *str++; + if (ch == '/') + pos->denom = read_int(str, NULL); + } +} + +/* ARGSUSED */ +static void * +_Xaw_Xlib_ArgsInitProc(String proc_name, String *params, Cardinal *num_params, + Screen *screen, Colormap colormap, int depth) +{ + Cardinal id, i; + Dl_init *init; + void *retval = XAWDL_CONVERT_ERROR; + + init = (Dl_init *)bsearch(proc_name, dl_init, + sizeof(dl_init) / sizeof(dl_init[0]), + sizeof(dl_init[0]), + bcmp_cvt_proc); + + id = init->id; + + switch (id) + { + case LINE: + case DRECT: + case FRECT: + if (*num_params == 4) + { + XawDLPosition *pos = (XawDLPosition *)XtCalloc(1, sizeof(XawDLPosition) * 4); + + for (i = 0; i < 4; i++) + read_position(params[i], &pos[i]); + retval = (void *)pos; + } + break; + case POINT: + case TSORIGIN: + case CLIPORIGIN: + if (*num_params == 2) + { + XawDLPosition *pos = (XawDLPosition *)XtCalloc(1, sizeof(XawDLPosition) * 2); + + read_position(params[0], &pos[0]); + read_position(params[1], &pos[1]); + retval = (void *)pos; + } + break; + case DLINES: + case FPOLY: + case POINTS: + if (*num_params >= 4 && !(*num_params & 1)) + { + XawDLPositionPtr *pos = XtNew(XawDLPositionPtr); + + pos->pos = (XawDLPosition *)XtCalloc(1, sizeof(XawDLPosition) * + *num_params); + pos->num_pos = *num_params; + for (i = 0; i < *num_params; i++) + read_position(params[i], &pos->pos[i]); + retval = (void *)pos; + } + break; + case SEGMENTS: + case CLIPRECTS: + if (*num_params >= 4 && !(*num_params % 4)) + { + XawDLPositionPtr *pos = XtNew(XawDLPositionPtr); + + pos->pos = (XawDLPosition *)XtCalloc(1, sizeof(XawDLPosition) * + *num_params); + pos->num_pos = *num_params; + for (i = 0; i < *num_params; i++) + read_position(params[i], &pos->pos[i]); + retval = (void *)pos; + } + break; + case DARC: + case FARC: + if (*num_params >= 4 && *num_params <= 6) + { + XawDLArcArgs *args = (XawDLArcArgs *)XtCalloc(1, sizeof(XawDLArcArgs)); + + args->angle1 = 0; + args->angle2 = 360; + for (i = 0; i < 4; i++) + read_position(params[i], &args->pos[i]); + if (*num_params > 4) + args->angle1 = read_int(params[4], NULL); + if (*num_params > 5) + args->angle2 = read_int(params[5], NULL); + args->angle1 *= 64; + args->angle2 *= 64; + retval = (void *)args; + } + break; + case GCFG: + case GCBG: + { + XColor xcolor; + + if (*num_params == 1 && + XAllocNamedColor(DisplayOfScreen(screen), colormap, + params[0], &xcolor, &xcolor)) + retval = (void *)xcolor.pixel; + } break; + case MASK: + case UMASK: + if (*num_params == 0) + retval = NULL; + break; + case LWIDTH: + if (*num_params == 1) + retval = (void *)read_int(params[0], NULL); + break; + case ARCMODE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "pieslice") == 0) + retval = (void *)ArcPieSlice; + else if (XmuCompareISOLatin1(params[0], "chord") == 0) + retval = (void *)ArcChord; + } + break; + case COORDMODE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "origin") == 0) + retval = (void *)CoordModeOrigin; + else if (XmuCompareISOLatin1(params[0], "previous") == 0) + retval = (void *)CoordModePrevious; + } + break; + case SHAPEMODE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "complex") == 0) + retval = (void *)Complex; + else if (XmuCompareISOLatin1(params[0], "convex") == 0) + retval = (void *)Convex; + else if (XmuCompareISOLatin1(params[0], "nonconvex") == 0) + retval = (void *)Nonconvex; + } + break; + case LINESTYLE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "solid") == 0) + retval = (void *)LineSolid; + else if (XmuCompareISOLatin1(params[0], "onoffdash") == 0) + retval = (void *)LineOnOffDash; + else if (XmuCompareISOLatin1(params[0], "doubledash") == 0) + retval = (void *)LineDoubleDash; + } + break; + case CAPSTYLE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "notlast") == 0) + retval = (void *)CapNotLast; + else if (XmuCompareISOLatin1(params[0], "butt") == 0) + retval = (void *)CapButt; + else if (XmuCompareISOLatin1(params[0], "round") == 0) + retval = (void *)CapRound; + else if (XmuCompareISOLatin1(params[0], "projecting") == 0) + retval = (void *)CapProjecting; + } + break; + case JOINSTYLE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "miter") == 0) + retval = (void *)JoinMiter; + else if (XmuCompareISOLatin1(params[0], "round") == 0) + retval = (void *)JoinRound; + else if (XmuCompareISOLatin1(params[0], "bevel") == 0) + retval = (void *)JoinBevel; + } + break; + case FILLSTYLE: + if (*num_params == 1) + { + if (*num_params && XmuCompareISOLatin1(params[0], "solid") == 0) + retval = (void *)FillSolid; + else if (*num_params && XmuCompareISOLatin1(params[0], "tiled") == 0) + retval = (void *)FillTiled; + else if (*num_params && XmuCompareISOLatin1(params[0], "stippled") == 0) + retval = (void *)FillStippled; + else if (*num_params && XmuCompareISOLatin1(params[0], "opaquestippled") == 0) + retval = (void *)FillOpaqueStippled; + } + break; + case FILLRULE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "evenodd") == 0) + retval = (void *)EvenOddRule; + else if (XmuCompareISOLatin1(params[0], "winding") == 0) + retval = (void *)WindingRule; + } + break; + case TILE: + if (*num_params == 1) + retval = (void *)XawLoadPixmap(params[0], screen, colormap, depth); + if (retval == NULL) + { + XtDisplayStringConversionWarning(DisplayOfScreen(screen), (String)params[0], + XtRPixmap); + retval = XAWDL_CONVERT_ERROR; + } + break; + case STIPPLE: + if (*num_params == 1) + retval = (void *)XawLoadPixmap(params[0], screen, colormap, 1); + if (retval == NULL) + { + XtDisplayStringConversionWarning(DisplayOfScreen(screen), (String)params[0], + XtRBitmap); + retval = XAWDL_CONVERT_ERROR; + } + break; + case FUNCTION: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "set") == 0) + retval = (void *)GXset; + else if (XmuCompareISOLatin1(params[0], "clear") == 0) + retval = (void *)GXclear; + else if (XmuCompareISOLatin1(params[0], "and") == 0) + retval = (void *)GXand; + else if (XmuCompareISOLatin1(params[0], "andreverse") == 0) + retval = (void *)GXandReverse; + else if (XmuCompareISOLatin1(params[0], "copy") == 0) + retval = (void *)GXcopy; + else if (XmuCompareISOLatin1(params[0], "andinverted") == 0) + retval = (void *)GXandInverted; + else if (XmuCompareISOLatin1(params[0], "noop") == 0) + retval = (void *)GXnoop; + else if (XmuCompareISOLatin1(params[0], "xor") == 0) + retval = (void *)GXxor; + else if (XmuCompareISOLatin1(params[0], "or") == 0) + retval = (void *)GXor; + else if (XmuCompareISOLatin1(params[0], "nor") == 0) + retval = (void *)GXnor; + else if (XmuCompareISOLatin1(params[0], "equiv") == 0) + retval = (void *)GXequiv; + else if (XmuCompareISOLatin1(params[0], "invert") == 0) + retval = (void *)GXinvert; + else if (XmuCompareISOLatin1(params[0], "orreverse") == 0) + retval = (void *)GXorReverse; + else if (XmuCompareISOLatin1(params[0], "copyinverted") == 0) + retval = (void *)GXcopyInverted; + else if (XmuCompareISOLatin1(params[0], "nand") == 0) + retval = (void *)GXnand; + } + break; + case PLANEMASK: + if (*num_params == 1) + retval = (void *)read_int(params[0], NULL); + break; + case DSTRING: + case PSTRING: + if (*num_params == 3) + { + XawDLStringArgs *string = (XawDLStringArgs *) + XtCalloc(1, sizeof(XawDLStringArgs)); + + read_position(params[0], &string->pos[0]); + read_position(params[1], &string->pos[1]); + string->string = XtNewString(params[2]); + string->length = strlen(string->string); + retval = string; + } + break; + case FONT: + if (*num_params == 1) + retval = (void *)XLoadFont(DisplayOfScreen(screen), params[0]); + break; + case DASHES: + if (*num_params && *num_params < 127) + { + char *dashes; + + dashes = XtMalloc(*num_params + 1); + + for (i = 0; i < *num_params; i++) + dashes[i + 1] = read_int(params[i], NULL); + *dashes = *num_params; + retval = dashes; + } + break; + case SUBWMODE: + if (*num_params == 1) + { + if (XmuCompareISOLatin1(params[0], "clipbychildren") == 0) + retval = (void *)ClipByChildren; + else if (XmuCompareISOLatin1(params[0], "includeinferiors") == 0) + retval = (void *)IncludeInferiors; + } + break; + case EXPOSURES: + if (*num_params == 1) + { + if (isdigit(params[0][0]) || params[0][0] == '+' || params[0][0] == '-') + retval = (void *)read_int(params[0], NULL); + else if (XmuCompareISOLatin1(params[0], "true") == 0 || + XmuCompareISOLatin1(params[0], "on") == 0) + retval = (void *)True; + else if (XmuCompareISOLatin1(params[0], "false") == 0 || + XmuCompareISOLatin1(params[0], "off") == 0) + retval = (void *)False; + } + break; + case CLIPMASK: + if (*num_params == 1) + retval = (void *)XawLoadPixmap(params[0], screen, colormap, 1); + if (retval == NULL) + { + retval = XAWDL_CONVERT_ERROR; + XtDisplayStringConversionWarning(DisplayOfScreen(screen), (String)params[0], + XtRPixmap); + } + break; + case COPYAREA: + case COPYPLANE: + if (*num_params > 2 && *num_params <= 7 + (id == COPYPLANE)) + { + XawDLCopyArgs *args = (XawDLCopyArgs *) + XtCalloc(1, sizeof(XawDLCopyArgs)); + + retval = args; + if (params[0][0] == '\0' || strcmp(params[0], ".") == 0) + args->pixmap = NULL; + else + { + args->pixmap = XawLoadPixmap(params[0], screen, colormap, id == COPYPLANE ? 1 : depth); + if (args->pixmap == NULL) + { + XtDisplayStringConversionWarning(DisplayOfScreen(screen), (String)params[0], + XtRBitmap); + retval = XAWDL_CONVERT_ERROR; + XtFree((char *)args); + } + } + if (retval != XAWDL_CONVERT_ERROR) + { + for (i = 1; i < *num_params && i < 7; i++) + read_position(params[i], &args->pos[i - 1]); + if (*num_params > 7) + args->plane = read_int(params[7], NULL); + } + } + break; + case IMAGE: + if (*num_params > 2 && *num_params <= 7) + { + XawDLImageArgs *args = (XawDLImageArgs *) + XtCalloc(1, sizeof(XawDLImageArgs)); + + retval = args; + args->pixmap = XawLoadPixmap(params[0], screen, colormap, depth); + if (args->pixmap == NULL) + { + XtDisplayStringConversionWarning(DisplayOfScreen(screen), + (String)params[0], XtRPixmap); + retval = XAWDL_CONVERT_ERROR; + XtFree((char *)args); + } + else + { + args->depth = depth; + for (i = 1; i < *num_params && i < 5; i++) + read_position(params[i], &args->pos[i - 1]); + } + } + break; + } + + return (retval); +} + +/* ARGSUSED */ +static void * +_Xaw_Xlib_DataInitProc(String class_name, + Screen *screen, Colormap colormap, int depth) +{ + XawXlibData *data; + Window tmp_win; + + data = (XawXlibData *)XtMalloc(sizeof(XawXlibData)); + + tmp_win = XCreateWindow(DisplayOfScreen(screen), + RootWindowOfScreen(screen), + 0, 0, 1, 1, 1, depth, + InputOutput, CopyFromParent, 0, NULL); + data->mask = 0; + data->gc = XCreateGC(DisplayOfScreen(screen), tmp_win, 0, &data->values); + XDestroyWindow(DisplayOfScreen(screen), tmp_win); + data->shape = Complex; + data->mode = CoordModeOrigin; + data->dashes = NULL; + + return ((void *)data); +} + +/* ARGSUSED */ +static void +_Xaw_Xlib_ArgsDestructor(Display *display, String proc_name, XtPointer args, + String *params, Cardinal *num_params) +{ + Cardinal id; + Dl_init *init; + + init = (Dl_init *)bsearch(proc_name, dl_init, + sizeof(dl_init) / sizeof(dl_init[0]), + sizeof(dl_init[0]), + bcmp_cvt_proc); + + id = init->id; + + switch (id) + { + case LINE: + case DRECT: + case FRECT: + case DARC: + case FARC: + case POINT: + case TSORIGIN: + case DASHES: + case CLIPORIGIN: + case COPYAREA: + case COPYPLANE: + case IMAGE: + XtFree(args); + break; + case DSTRING: + case PSTRING: + { + XawDLStringArgs *string = (XawDLStringArgs *)args; + XtFree(string->string); + XtFree(args); + } break; + case DLINES: + case FPOLY: + case POINTS: + case SEGMENTS: + case CLIPRECTS: + { + XawDLPositionPtr *ptr = (XawDLPositionPtr *)args; + + XtFree((char *)ptr->pos); + XtFree(args); + } break; + } +} + +/* ARGSUSED */ +static void +_Xaw_Xlib_DataDestructor(Display *display, String class_name, XtPointer data) +{ + if (data) + { + XawXlibData *xdata = (XawXlibData *)data; + + XFreeGC(display, xdata->gc); + if (xdata->dashes) + XtFree(xdata->dashes); + XtFree((char *)data); + } +} + +/* Start of DLInfo Management Functions */ +static int +qcmp_dlist_info(register _Xconst void *left, register _Xconst void *right) +{ + return (strcmp((*(XawDLInfo **)left)->name, (*(XawDLInfo **)right)->name)); +} + +Bool XawDeclareDisplayListProc(XawDLClass *lc, String name, + XawDisplayListProc proc) +{ + XawDLInfo *info; + + if (!lc || !proc || !name || name[0] == '\0') + return (False); + + if ((info = _XawFindDLInfo(lc, name)) != NULL) + /* Since the data structures to the displayList classes are(should be) + * opaque, it is not a good idea to allow overriding a displayList + * procedure; it's better to choose another name or class name! + */ + return (False); + + info = (XawDLInfo *)XtMalloc(sizeof(XawDLInfo)); + info->name = XtNewString(name); + info->qname = XrmStringToQuark(info->name); + info->proc = proc; + + if (!lc->num_infos) + { + lc->num_infos = 1; + lc->infos = (XawDLInfo **)XtMalloc(sizeof(XawDLInfo*)); + } + else + { + ++lc->num_infos; + lc->infos = (XawDLInfo **) + XtRealloc((char *)lc->infos, sizeof(XawDLInfo*) * lc->num_infos); + } + lc->infos[lc->num_infos - 1] = info; + + if (lc->num_infos > 1) + qsort(lc->infos, lc->num_infos, sizeof(XawDLInfo*), qcmp_dlist_info); + + return (True); +} + +static int +bcmp_dlist_info(register _Xconst void *string, + register _Xconst void *dlinfo) +{ + return (strcmp((String)string, (*(XawDLClass **)dlinfo)->name)); +} + +static XawDLInfo * +_XawFindDLInfo(XawDLClass *lc, String name) +{ + XawDLInfo **info; + + if (!lc->infos) + return (NULL); + + info = (XawDLInfo **)bsearch(name, lc->infos, lc->num_infos, + sizeof(XawDLInfo*), bcmp_dlist_info); + + return (info ? *info : NULL); +} + +/* Start of DLClass Management Functions */ +XawDLClass * +XawGetDisplayListClass(String name) +{ + return (_XawFindDLClass(name)); +} + +static int +qcmp_dlist_class(register _Xconst void *left, register _Xconst void *right) +{ + return (strcmp((*(XawDLClass **)left)->name, (*(XawDLClass **)right)->name)); +} + +XawDLClass * +XawCreateDisplayListClass(String name, + XawDLArgsInitProc args_init, + XawDLArgsDestructor args_destructor, + XawDLDataInitProc data_init, + XawDLDataDestructor data_destructor) +{ + XawDLClass *lc; + + if (!name || name[0] == '\0') + return (NULL); + + lc = (XawDLClass *)XtMalloc(sizeof(XawDLClass)); + lc->name = XtNewString(name); + lc->infos = NULL; + lc->num_infos = 0; + lc->args_init = args_init; + lc->args_destructor = args_destructor; + lc->data_init = data_init; + lc->data_destructor = data_destructor; + + if (!classes) + { + num_classes = 1; + classes = (XawDLClass **)XtMalloc(sizeof(XawDLClass)); + } + else + { + ++num_classes; + classes = (XawDLClass **)XtRealloc((char *)classes, + sizeof(XawDLClass) * num_classes); + } + classes[num_classes - 1] = lc; + + if (num_classes > 1) + qsort(&classes[0], num_classes, sizeof(XawDLClass*), qcmp_dlist_class); + + return (lc); +} + +static int +bcmp_dlist_class(register _Xconst void *string, + register _Xconst void *dlist) +{ + return (strcmp((String)string, (*(XawDLClass **)dlist)->name)); +} + +static XawDLClass * +_XawFindDLClass(String name) +{ + XawDLClass **lc; + + if (!classes) + return (NULL); + + lc = (XawDLClass **)bsearch(name, &classes[0], num_classes, + sizeof(XawDLClass*), bcmp_dlist_class); + + return (lc ? *lc : NULL); +} + +#endif /* OLDXAW */ diff --git a/src/OS.c b/src/OS.c new file mode 100644 index 0000000..e91f36d --- /dev/null +++ b/src/OS.c @@ -0,0 +1,60 @@ +/* $XFree86: xc/lib/Xaw/OS.c,v 1.2 1998/12/06 11:24:32 dawes Exp $ */ + +/* Some OS-dependent utility code */ + +#include <X11/Xosdefs.h> +#include <X11/IntrinsicP.h> +#include "Private.h" + +#ifndef X_NOT_POSIX +#include <unistd.h> /* for sysconf(), and getpagesize() */ +#endif + +#if defined(linux) +#include <asm/page.h> /* for PAGE_SIZE */ +#define HAS_GETPAGESIZE +#define HAS_SC_PAGESIZE /* _SC_PAGESIZE may be an enum for Linux */ +#endif + +#if defined(CSRG_BASED) +#define HAS_GETPAGESIZE +#endif + +#if defined(sun) +#define HAS_GETPAGESIZE +#endif + +int +_XawGetPageSize() +{ + static int pagesize = -1; + + if (pagesize != -1) + return pagesize; + + /* Try each supported method in the preferred order */ + +#if defined(_SC_PAGESIZE) || defined(HAS_SC_PAGESIZE) + pagesize = sysconf(_SC_PAGESIZE); +#endif + +#ifdef _SC_PAGE_SIZE + if (pagesize == -1) + pagesize = sysconf(_SC_PAGE_SIZE); +#endif + +#ifdef HAS_GETPAGESIZE + if (pagesize == -1) + pagesize = getpagesize(); +#endif + +#ifdef PAGE_SIZE + if (pagesize == -1) + pagesize = PAGE_SIZE; +#endif + + if (pagesize == -1) + pagesize = 0; + + return pagesize; +} diff --git a/src/Pixmap.c b/src/Pixmap.c new file mode 100644 index 0000000..feb0fc2 --- /dev/null +++ b/src/Pixmap.c @@ -0,0 +1,984 @@ +/* + * Copyright (c) 1998 by The XFree86 Project, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Except as contained in this notice, the name of the XFree86 Project shall + * not be used in advertising or otherwise to promote the sale, use or other + * dealings in this Software without prior written authorization from the + * XFree86 Project. + */ + +/* $XFree86: xc/lib/Xaw/Pixmap.c,v 3.17 2002/05/18 02:05:39 paulo Exp $ */ + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <X11/IntrinsicP.h> +#include <X11/Xmu/CharSet.h> +#include <X11/Xfuncs.h> +#include <X11/extensions/shape.h> +#include <X11/xpm.h> +#include "Private.h" + +#ifndef OLDXAW + +/* + * Types + */ +typedef struct _XawCache { + long value; + XtPointer *elems; + unsigned int num_elems; +} XawCache; + +typedef struct _XawPixmapLoaderInfo { + XawPixmapLoader loader; + String type; + String ext; +} XawPixmapLoaderInfo; + +/* + * Private Methods + */ +static Bool BitmapLoader(XawParams*, Screen*, Colormap, int, + Pixmap*, Pixmap*, Dimension*, Dimension*); +static Bool GradientLoader(XawParams*, Screen*, Colormap, int, + Pixmap*, Pixmap*, Dimension*, Dimension*); +static Bool XPixmapLoader(XawParams*, Screen*, Colormap, int, + Pixmap*, Pixmap*, Dimension*, Dimension*); +static XawPixmap *_XawFindPixmap(String, Screen*, Colormap, int); +static void _XawCachePixmap(XawPixmap*, Screen*, Colormap, int); +static int _XawFindPixmapLoaderIndex(String, String); +static int qcmp_long(register _Xconst void*, register _Xconst void *); +static int bcmp_long(register _Xconst void*, register _Xconst void *); +static int qcmp_string(register _Xconst void*, register _Xconst void *); +static int bcmp_string(register _Xconst void*, register _Xconst void *); +static void GetResourcePixmapPath(Display*); + +/* + * Initialization + */ +static XawCache xaw_pixmaps; +static XawCache x_pixmaps; /* for fast reverse search */ +static XawPixmapLoaderInfo **loader_info; +static Cardinal num_loader_info; + +/* + * Implementation + */ +Bool +XawPixmapsInitialize(void) +{ + static Boolean first_time = True; + + if (!first_time) + return (False); + + (void)XawAddPixmapLoader(NULL, NULL, BitmapLoader); + (void)XawAddPixmapLoader("bitmap", NULL, BitmapLoader); + (void)XawAddPixmapLoader("gradient", NULL, GradientLoader); + (void)XawAddPixmapLoader("xpm", "xpm", XPixmapLoader); + + return (True); +} + +XawParams * +XawParseParamsString(String name) +{ + XawParams *xaw_params; + char *tok, *str, *type = NULL, *ext = NULL, *params = NULL; + + if (!name) + return (NULL); + + xaw_params = (XawParams *)XtMalloc(sizeof(XawParams)); + + str = XtNewString(name); + + /* Find type */ + tok = str; + while (tok = strchr(tok, ':'), tok) + { + if (tok == str || tok[-1] != '\\') + break; + memmove(&tok[-1], tok, strlen(tok) + 1); + } + if (tok) + { + *tok = '\0'; + if (strchr(str, '?')) + { + *tok = ':'; + } + else + { + ++tok; + type = XtNewString(str); + memmove(str, tok, strlen(tok) + 1); + } + } + + /* Find params */ + tok = str; + while (tok = strchr(tok, '?'), tok) + { + if (tok == str || tok[-1] != '\\') + params = tok; + if (tok != str && tok[-1] == '\\') + memmove(&tok[-1], tok, strlen(tok) + 1); + else + break; + } + if (params) + { + *params = '\0'; + ++params; + } + + /* Find ext */ + tok = str; + while (tok = strchr(tok, '.'), tok) + { + if (tok == str || tok[-1] != '\\') + ext = tok; + if (tok != str && tok[-1] == '\\') + memmove(&tok[-1], tok, strlen(tok) + 1); + else + break; + } + if (ext) + { + ++ext; + if (strchr(ext, '/')) + ext = NULL; + } + + xaw_params->name = XtNewString(str); + xaw_params->type = type; + xaw_params->ext = ext ? XtNewString(ext) : ext; + xaw_params->args = NULL; + xaw_params->num_args = 0; + + /* Parse params */ + if (params) + { + char *arg, *val; + XawArgVal *xaw_arg; + + for (tok = strtok(params, "&"); tok; tok = strtok(NULL, "&")) + { + val = strchr(tok, '='); + if (val) + { + *val = '\0'; + ++val; + if (*val != '\0') + val = XtNewString(val); + else + val = NULL; + } + arg = XtNewString(tok); + xaw_arg = (XawArgVal *)XtMalloc(sizeof(XawArgVal)); + xaw_arg->name = arg; + xaw_arg->value = val; + if (!xaw_params->num_args) + { + xaw_params->num_args = 1; + xaw_params->args = (XawArgVal **) + XtMalloc(sizeof(XawArgVal*)); + } + else + { + ++xaw_params->num_args; + xaw_params->args = (XawArgVal **) + XtRealloc((char *)xaw_params->args, + sizeof(XawArgVal*) * xaw_params->num_args); + } + xaw_params->args[xaw_params->num_args - 1] = xaw_arg; + } + } + + if (xaw_params->num_args > 1) + qsort(xaw_params->args, xaw_params->num_args, sizeof(XtPointer), + qcmp_string); + + XtFree(str); + + return (xaw_params); +} + +void +XawFreeParamsStruct(XawParams *params) +{ + unsigned int i; + + if (!params) + return; + + for (i = 0; i < params->num_args; i++) + { + XtFree(params->args[i]->name); + if (params->args[i]->value) + XtFree(params->args[i]->value); + XtFree((char *)params->args[i]); + } + + if (params->args) + XtFree((char *)params->args); + XtFree((char *)params); +} + +XawArgVal * +XawFindArgVal(XawParams *params, String name) +{ + XawArgVal **arg_val; + + if (!params->args) + return (NULL); + + arg_val = (XawArgVal **)bsearch((void *)name, params->args, + params->num_args, sizeof(XtPointer*), + bcmp_string); + if (!arg_val) + return (NULL); + + return (*arg_val); +} + +XawPixmap * +XawLoadPixmap(String name, Screen *screen, Colormap colormap, int depth) +{ + int idx; + Bool success; + XawPixmap *xaw_pixmap; + Pixmap pixmap, mask; + Dimension width, height; + XawParams *xaw_params; + + if (!name) + return (False); + + xaw_pixmap = _XawFindPixmap(name, screen, colormap, depth); + + if (xaw_pixmap) + return (xaw_pixmap); + + if ((xaw_params = XawParseParamsString(name)) == NULL) + return (NULL); + + idx = _XawFindPixmapLoaderIndex(xaw_params->type, xaw_params->ext); + if (idx < 0) + return (NULL); + +#ifdef DIAGNOSTIC + fprintf(stderr, "(*) Loading pixmap \"%s\": ", name); +#endif + + success = loader_info[idx]->loader(xaw_params, screen, colormap, depth, + &pixmap, &mask, &width, &height); + if (success) + { + xaw_pixmap = (XawPixmap *)XtMalloc(sizeof(XawPixmap)); + xaw_pixmap->name = XtNewString(name); + xaw_pixmap->pixmap = pixmap; + xaw_pixmap->mask = mask; + xaw_pixmap->width = width; + xaw_pixmap->height = height; + _XawCachePixmap(xaw_pixmap, screen, colormap, depth); + } + + XawFreeParamsStruct(xaw_params); + +#ifdef DIAGNOSTIC + fprintf(stderr, "%s", success ? "success\n" : "failed\n"); +#endif + + return (success ? xaw_pixmap : NULL); +} + +Bool +XawAddPixmapLoader(String type, String ext, XawPixmapLoader loader) +{ + XawPixmapLoaderInfo *info; + int i; + + if (!loader) + return (False); + + i = _XawFindPixmapLoaderIndex(type, ext); + + if (i >= 0) + { + loader_info[i]->loader = loader; + if (loader_info[i]->type) + XtFree(loader_info[i]->type); + if (loader_info[i]->ext) + XtFree(loader_info[i]->ext); + loader_info[i]->type = type ? XtNewString(type) : NULL; + loader_info[i]->ext = ext ? XtNewString(ext) : NULL; + return (True); + } + + if ((info = (XawPixmapLoaderInfo *)XtMalloc(sizeof(XawPixmapLoaderInfo))) + == NULL) + return (False); + + info->loader = loader; + info->type = type ? XtNewString(type) : NULL; + info->ext = ext ? XtNewString(ext) : NULL; + + if (!loader_info) + { + num_loader_info = 1; + loader_info = (XawPixmapLoaderInfo**) + XtMalloc(sizeof(XawPixmapLoaderInfo*)); + } + else + { + ++num_loader_info; + loader_info = (XawPixmapLoaderInfo**) + XtRealloc((char *)loader_info, + sizeof(XawPixmapLoaderInfo) * num_loader_info); + } + loader_info[num_loader_info - 1] = info; + + return (True); +} + +static int +_XawFindPixmapLoaderIndex(String type, String ext) +{ + Cardinal i; + + if (!loader_info) + return (-1); + + for (i = 0; i < num_loader_info; i++) + if ((type && loader_info[i]->type && strcmp(type, loader_info[i]->type) == 0) + || (ext && loader_info[i]->ext && strcmp(ext, loader_info[i]->ext) == 0)) + return ((int)i); + + if (!type) + return (0); /* try a bitmap */ + + return (-1); +} + +static int +qcmp_x_cache(register _Xconst void *left, register _Xconst void *right) +{ + return ((int)((*(XawPixmap **)left)->pixmap) - + (int)((*(XawPixmap **)right)->pixmap)); +} + +static int +bcmp_x_cache(register _Xconst void *pixmap, register _Xconst void *xaw) +{ + return (int)((long)pixmap - (long)((*(XawPixmap **)xaw)->pixmap)); +} + +static int +qcmp_long(register _Xconst void *left, register _Xconst void *right) +{ + return ((long)((*(XawCache **)left)->value) - + (long)((*(XawCache **)right)->value)); +} + +static int +qcmp_string(register _Xconst void *left, register _Xconst void *right) +{ + return (strcmp((String)((*(XawCache **)left)->value), + (String)((*(XawCache **)right)->value))); +} + +static int +bcmp_long(register _Xconst void *value, register _Xconst void *cache) +{ + return ((long)value - (long)((*(XawCache **)cache)->value)); +} + +static int +bcmp_string(register _Xconst void *string, + register _Xconst void *cache) +{ + return (strcmp((String)string, (String)((*(XawCache **)cache)->value))); +} + +#define FIND_ALL 0 +#define FIND_SCREEN 1 +#define FIND_COLORMAP 2 +#define FIND_DEPTH 3 +static XawCache * +_XawFindCache(XawCache *xaw, + Screen *screen, Colormap colormap, int depth, int flags) +{ + XawCache **cache; + + if (!xaw->num_elems) + return (NULL); + + /* Screen */ + cache = (XawCache **)bsearch(screen, xaw->elems, + xaw->num_elems, sizeof(XtPointer), + bcmp_long); + if (!cache || !(*cache)->num_elems) + return (NULL); + if (flags == FIND_SCREEN) + return (*cache); + + /* Colormap */ + cache = (XawCache **)bsearch((void *)colormap, (*cache)->elems, + (*cache)->num_elems, sizeof(XtPointer), + bcmp_long); + if (!cache || !(*cache)->num_elems) + return (NULL); + if (flags == FIND_COLORMAP) + return (*cache); + + /* Depth */ + cache = (XawCache **)bsearch((void *)(long)depth, (*cache)->elems, + (*cache)->num_elems, sizeof(XtPointer), + bcmp_long); + + if (!cache || !(*cache)->num_elems) + return (NULL); + return (*cache); +} + +static XawCache * +_XawGetCache(XawCache *xaw, Screen *screen, Colormap colormap, int depth) +{ + XawCache *s_cache, *c_cache, *d_cache, *cache, *pcache; + + cache = _XawFindCache(xaw, screen, colormap, depth, FIND_ALL); + + if (!cache) + { + s_cache = _XawFindCache(xaw, + screen, colormap, depth, FIND_SCREEN); + if (!s_cache) + { + pcache = (XawCache *)XtMalloc(sizeof(XawCache)); + if (!xaw->num_elems) + { + xaw->num_elems = 1; + xaw->elems = (XtPointer*)XtMalloc(sizeof(XtPointer)); + } + else + { + ++xaw->num_elems; + xaw->elems = (XtPointer*) + XtRealloc((char *)xaw->elems, + sizeof(XtPointer) * xaw->num_elems); + } + pcache->value = (long)screen; + pcache->elems = NULL; + pcache->num_elems = 0; + xaw->elems[xaw->num_elems - 1] = (XtPointer)pcache; + s_cache = (XawCache *)xaw->elems[xaw->num_elems - 1]; + if (xaw->num_elems > 1) + qsort(xaw->elems, xaw->num_elems, sizeof(XtPointer), qcmp_long); + } + + c_cache = _XawFindCache(xaw, + screen, colormap, depth, FIND_COLORMAP); + if (!c_cache) + { + pcache = (XawCache *)XtMalloc(sizeof(XawCache)); + if (!s_cache->num_elems) + { + s_cache->num_elems = 1; + s_cache->elems = (XtPointer*)XtMalloc(sizeof(XtPointer)); + } + else + { + ++s_cache->num_elems; + s_cache->elems = (XtPointer*) + XtRealloc((char *)s_cache->elems, + sizeof(XtPointer) * s_cache->num_elems); + } + pcache->value = (long)colormap; + pcache->elems = NULL; + pcache->num_elems = 0; + s_cache->elems[s_cache->num_elems - 1] = (XtPointer)pcache; + c_cache = (XawCache *)s_cache->elems[s_cache->num_elems - 1]; + if (s_cache->num_elems > 1) + qsort(s_cache->elems, s_cache->num_elems, + sizeof(XtPointer), qcmp_long); + } + + d_cache = _XawFindCache(xaw, + screen, colormap, depth, FIND_DEPTH); + if (!d_cache) + { + pcache = (XawCache *)XtMalloc(sizeof(XawCache)); + if (!c_cache->num_elems) + { + c_cache->num_elems = 1; + c_cache->elems = (XtPointer*)XtMalloc(sizeof(XtPointer)); + } + else + { + ++c_cache->num_elems; + c_cache->elems = (XtPointer*) + XtRealloc((char *)c_cache->elems, + sizeof(XtPointer) * c_cache->num_elems); + } + pcache->value = (long)depth; + pcache->elems = NULL; + pcache->num_elems = 0; + c_cache->elems[c_cache->num_elems - 1] = (XtPointer)pcache; + d_cache = (XawCache *)c_cache->elems[c_cache->num_elems - 1]; + if (c_cache->num_elems > 1) + qsort(c_cache->elems, c_cache->num_elems, + sizeof(XtPointer), qcmp_long); + } + + cache = d_cache; + } + + return (cache); +} + +static XawPixmap * +_XawFindPixmap(String name, Screen *screen, Colormap colormap, int depth) +{ + XawCache *cache; + XawPixmap **pixmap; + + cache = _XawFindCache(&xaw_pixmaps, screen, colormap, depth, FIND_ALL); + + if (!cache) + return (NULL); + + /* Name */ + pixmap = (XawPixmap **)bsearch((void *)name, cache->elems, + cache->num_elems, sizeof(XtPointer), + bcmp_string); + if (!pixmap) + return (NULL); + + return (*pixmap); +} + +XawPixmap * +XawPixmapFromXPixmap(Pixmap pixmap, + Screen *screen, Colormap colormap, int depth) +{ + XawCache *cache; + XawPixmap **x_pixmap; + + cache = _XawFindCache(&x_pixmaps, screen, colormap, depth, FIND_ALL); + + if (!cache) + return (NULL); + + /* Pixmap */ + x_pixmap = (XawPixmap **)bsearch((void *)pixmap, cache->elems, + cache->num_elems, sizeof(XtPointer), + bcmp_x_cache); + if (!x_pixmap) + return (NULL); + + return (*x_pixmap); +} + +static void +_XawCachePixmap(XawPixmap *pixmap, + Screen *screen, Colormap colormap, int depth) +{ + XawCache *xaw_cache, *x_cache; + + xaw_cache = _XawGetCache(&xaw_pixmaps, screen, colormap, depth); + x_cache = _XawGetCache(&x_pixmaps, screen, colormap, depth); + + if (!xaw_cache->num_elems) + { + xaw_cache->num_elems = 1; + xaw_cache->elems = (XtPointer*)XtMalloc(sizeof(XtPointer)); + } + else + { + ++xaw_cache->num_elems; + xaw_cache->elems = (XtPointer*)XtRealloc((char *)xaw_cache->elems, + sizeof(XtPointer) * + xaw_cache->num_elems); + } + + xaw_cache->elems[xaw_cache->num_elems - 1] = (XtPointer)pixmap; + if (xaw_cache->num_elems > 1) + qsort(xaw_cache->elems, xaw_cache->num_elems, + sizeof(XtPointer), qcmp_string); + + + if (!x_cache->num_elems) + { + x_cache->num_elems = 1; + x_cache->elems = (XtPointer*)XtMalloc(sizeof(XtPointer)); + } + else + { + ++x_cache->num_elems; + x_cache->elems = (XtPointer*)XtRealloc((char *)x_cache->elems, + sizeof(XtPointer) * + x_cache->num_elems); + } + + x_cache->elems[x_cache->num_elems - 1] = (XtPointer)pixmap; + if (x_cache->num_elems > 1) + qsort(x_cache->elems, x_cache->num_elems, sizeof(XtPointer), qcmp_x_cache); +} + +#ifndef PROJECT_ROOT +#define PROJECT_ROOT "/usr/X11R6" +#endif + +static char *pixmap_path = NULL; + +static void +GetResourcePixmapPath(Display *display) +{ + XrmName xrm_name[2]; + XrmClass xrm_class[2]; + XrmRepresentation rep_type; + XrmValue value; + static char *default_path = + "%H/%T/%N:%P/include/X11/%T/%N:/usr/X11R6/include/X11/%T/%N:/usr/include/X11/%T/%N:%N"; + + xrm_name[0] = XrmPermStringToQuark("pixmapFilePath"); + xrm_name[1] = NULLQUARK; + xrm_class[0] = XrmPermStringToQuark("PixmapFilePath"); + xrm_class[1] = NULLQUARK; + if (!XrmGetDatabase(display)) + (void) XGetDefault(display, "", ""); + if (XrmQGetResource(XrmGetDatabase(display), xrm_name, xrm_class, + &rep_type, &value) && + rep_type == XrmPermStringToQuark("String")) { + int length = 0; + char *tok, *buffer = XtNewString(value.addr); + + for (tok = strtok(buffer, ":"); tok; tok = strtok(NULL, ":")) { + int toklen = strlen(tok); + + if (toklen) { + pixmap_path = XtRealloc(pixmap_path, length + toklen + 5); + strcpy(pixmap_path + length, tok); + if (length) + pixmap_path[length++] = ':'; + sprintf(pixmap_path + length, "%s/%%N", tok); + length += strlen(tok) + 3; + } + } + pixmap_path = XtRealloc(pixmap_path, length + strlen(default_path) + 2); + if (length) + pixmap_path[length++] = ':'; + strcpy(pixmap_path + length, default_path); + } + else + pixmap_path = default_path; +} + +static Bool +BitmapLoader(XawParams *params, Screen *screen, Colormap colormap, int depth, + Pixmap *pixmap_return, Pixmap *mask_return, + Dimension *width_return, Dimension *height_return) +{ + Pixel fg, bg; + XColor color, exact; + Pixmap pixmap; + unsigned int width, height; + unsigned char *data = NULL; + int hotX, hotY; + XawArgVal *argval; + Bool retval = False; + static SubstitutionRec sub[] = { + {'H', NULL}, + {'N', NULL}, + {'T', "bitmaps"}, + {'P', PROJECT_ROOT}, + }; + char *filename; + + fg = BlackPixelOfScreen(screen); + bg = WhitePixelOfScreen(screen); + + if ((argval = XawFindArgVal(params, "foreground")) != NULL + && argval->value) + { + if (XAllocNamedColor(DisplayOfScreen(screen), colormap, argval->value, + &color, &exact)) + fg = color.pixel; + else + return (False); + } + if ((argval = XawFindArgVal(params, "background")) != NULL + && argval->value) + { + if (XAllocNamedColor(DisplayOfScreen(screen), colormap, argval->value, + &color, &exact)) + bg = color.pixel; + else + return (False); + } + + if (params->name[0] != '/' && params->name[0] != '.') + { + if (!sub[0].substitution) + sub[0].substitution = getenv("HOME"); + sub[1].substitution = params->name; + if (pixmap_path == NULL) + GetResourcePixmapPath(DisplayOfScreen(screen)); + filename = XtFindFile(pixmap_path, sub, XtNumber(sub), NULL); + if (!filename) + return (FALSE); + } + else + filename = params->name; + + if (XReadBitmapFileData(filename, &width, &height, &data, + &hotX, &hotY) == BitmapSuccess) + { + pixmap = XCreatePixmapFromBitmapData(DisplayOfScreen(screen), + RootWindowOfScreen(screen), + (char *)data, + width, height, fg, bg, depth); + if (data) + XFree(data); + *pixmap_return = pixmap; + *mask_return = None; + *width_return = width; + *height_return = height; + + retval = True; + } + + if (filename != params->name) + XtFree(filename); + + return (retval); +} + +#define VERTICAL 1 +#define HORIZONTAL 2 +static Bool +GradientLoader(XawParams *params, Screen *screen, Colormap colormap, int depth, + Pixmap *pixmap_return, Pixmap *mask_return, + Dimension *width_return, Dimension *height_return) +{ + double ired, igreen, iblue, red, green, blue; + XColor start, end, color; + XGCValues values; + GC gc; + double i, inc, x, y, xend, yend; + Pixmap pixmap; + XawArgVal *argval; + int orientation, dimension, steps; + char *value; + + if (XmuCompareISOLatin1(params->name, "vertical") == 0) + orientation = VERTICAL; + else if (XmuCompareISOLatin1(params->name, "horizontal") == 0) + orientation = HORIZONTAL; + else + return (False); + + if ((argval = XawFindArgVal(params, "dimension")) != NULL + && argval->value) + { + dimension = atoi(argval->value); + if (dimension <= 0) + return (False); + } + else + dimension = 50; + + if ((argval = XawFindArgVal(params, "steps")) != NULL + && argval->value) + { + steps = atoi(argval->value); + if (steps <= 0) + return (False); + } + else + steps = dimension; + + steps = XawMin(steps, dimension); + + value = NULL; + if ((argval = XawFindArgVal(params, "start")) != NULL) + value = argval->value; + if (value && !XAllocNamedColor(DisplayOfScreen(screen), colormap, value, + &start, &color)) + return (False); + else if (!value) + { + start.pixel = WhitePixelOfScreen(screen); + XQueryColor(DisplayOfScreen(screen), colormap, &start); + } + value = NULL; + if ((argval = XawFindArgVal(params, "end")) != NULL) + value = argval->value; + if (value && !XAllocNamedColor(DisplayOfScreen(screen), colormap, value, + &end, &color)) + return (False); + else if (!value) + { + end.pixel = BlackPixelOfScreen(screen); + XQueryColor(DisplayOfScreen(screen), colormap, &end); + } + + if ((pixmap = XCreatePixmap(DisplayOfScreen(screen), + RootWindowOfScreen(screen), + orientation == VERTICAL ? 1 : dimension, + orientation == VERTICAL ? dimension : 1, depth)) + == 0) + return (False); + + ired = (double)(end.red - start.red) / (double)steps; + igreen = (double)(end.green - start.green) / (double)steps; + iblue = (double)(end.blue - start.blue) / (double)steps; + + red = color.red = start.red; + green = color.green = start.green; + blue = color.blue = start.blue; + + inc = (double)dimension / (double)steps; + + gc = XCreateGC(DisplayOfScreen(screen), pixmap, 0, &values); + + x = y = 0.0; + if (orientation == VERTICAL) + { + xend = 1; + yend = 0; + } + else + { + xend = 0; + yend = 1; + } + + color.flags = DoRed | DoGreen | DoBlue; + + XSetForeground(DisplayOfScreen(screen), gc, start.pixel); + for (i = 0.0; i < dimension; i += inc) + { + if ((int)color.red != (int)red || (int)color.green != (int)green + || (int)color.blue != (int)blue) + { + XFillRectangle(DisplayOfScreen(screen), pixmap, gc, (int)x, (int)y, + (unsigned int)xend, (unsigned int)yend); + color.red = (unsigned short)red; + color.green = (unsigned short)green; + color.blue = (unsigned short)blue; + if (!XAllocColor(DisplayOfScreen(screen), colormap, &color)) + { + XFreePixmap(DisplayOfScreen(screen), pixmap); + return (False); + } + XSetForeground(DisplayOfScreen(screen), gc, color.pixel); + if (orientation == VERTICAL) + y = yend; + else + x = xend; + } + red += ired; + green += igreen; + blue += iblue; + if (orientation == VERTICAL) + yend += inc; + else + xend += inc; + } + XFillRectangle(DisplayOfScreen(screen), pixmap, gc, (int)x, (int)y, + (unsigned int)xend, (unsigned int)yend); + + *pixmap_return = pixmap; + *mask_return = None; + *width_return = orientation == VERTICAL ? 1 : dimension; + *height_return = orientation == VERTICAL ? dimension : 1; + + XFreeGC(DisplayOfScreen(screen), gc); + + return (True); +} + +static Bool +XPixmapLoader(XawParams *params, Screen *screen, Colormap colormap, int depth, + Pixmap *pixmap_return, Pixmap *mask_return, + Dimension *width_return, Dimension *height_return) +{ + XpmAttributes xpm_attributes; + XawArgVal *argval; + unsigned int closeness = 4000; + static SubstitutionRec sub[] = { + {'H', NULL}, + {'N', NULL}, + {'T', "pixmaps"}, + {'P', PROJECT_ROOT}, + }; + char *filename; + + if ((argval = XawFindArgVal(params, "closeness")) != NULL + && argval->value) + closeness = atoi(argval->value); + + if (params->name[0] != '/' && params->name[0] != '.') + { + if (!sub[0].substitution) + sub[0].substitution = getenv("HOME"); + sub[1].substitution = params->name; + if (pixmap_path == NULL) + GetResourcePixmapPath(DisplayOfScreen(screen)); + filename = XtFindFile(pixmap_path, sub, XtNumber(sub), NULL); + if (!filename) + return (False); + } + else + filename = params->name; + + xpm_attributes.colormap = colormap; + xpm_attributes.closeness = closeness; + xpm_attributes.valuemask = XpmSize | XpmColormap | XpmCloseness; + if (XpmReadFileToPixmap(DisplayOfScreen(screen), + RootWindowOfScreen(screen), filename, pixmap_return, + mask_return, &xpm_attributes) == XpmSuccess) + { + *width_return = xpm_attributes.width; + *height_return = xpm_attributes.height; + + return (True); + } + + return (False); +} + +void +XawReshapeWidget(Widget w, XawPixmap *pixmap) +{ + if (!pixmap || pixmap->mask == None) + XShapeCombineMask(XtDisplay(w), XtWindow(w), ShapeBounding, 0, 0, + None, ShapeSet); + else + XShapeCombineMask(XtDisplay(w), XtWindow(w), ShapeBounding, 0, 0, + pixmap->mask, ShapeSet); +} + +#endif /* OLDXAW */ diff --git a/src/Private.h b/src/Private.h new file mode 100644 index 0000000..5bdf73c --- /dev/null +++ b/src/Private.h @@ -0,0 +1,154 @@ +/* + * Copyright (c) 1998 by The XFree86 Project, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Except as contained in this notice, the name of the XFree86 Project shall + * not be used in advertising or otherwise to promote the sale, use or other + * dealings in this Software without prior written authorization from the + * XFree86 Project. + */ + +/* $XFree86: xc/lib/Xaw/Private.h,v 3.10 1999/06/06 08:48:07 dawes Exp $ */ + +#ifndef _XawPrivate_h +#define _XawPrivate_h + +#define XawMax(a, b) ((a) > (b) ? (a) : (b)) +#define XawMin(a, b) ((a) < (b) ? (a) : (b)) +#define XawAbs(a) ((a) < 0 ? -(a) : (a)) + +#define XawStackAlloc(size, stk_buffer) \ +((size) <= sizeof(stk_buffer) \ + ? (XtPointer)(stk_buffer) \ + : XtMalloc((unsigned)(size))) + +#define XawStackFree(pointer, stk_buffer) \ +do { \ + if ((pointer) != (XtPointer)(stk_buffer)) \ + XtFree((char *)pointer); \ +} while (0) + +#ifndef XtX +#define XtX(w) (((RectObj)w)->rectangle.x) +#endif +#ifndef XtY +#define XtY(w) (((RectObj)w)->rectangle.y) +#endif +#ifndef XtWidth +#define XtWidth(w) (((RectObj)w)->rectangle.width) +#endif +#ifndef XtHeight +#define XtHeight(w) (((RectObj)w)->rectangle.height) +#endif +#ifndef XtBorderWidth +#define XtBorderWidth(w) (((RectObj)w)->rectangle.border_width) +#endif + +#ifndef OLDXAW +#define XAW_PRIV_VAR_PREFIX '$' + +typedef Bool (*XawParseBooleanProc)(Widget, String, XEvent*, Bool*); + +typedef struct _XawActionVarList XawActionVarList; +typedef struct _XawActionResList XawActionResList; + +/* Boolean expressions */ +Bool XawParseBoolean(Widget, String, XEvent*, Bool*); +Bool XawBooleanExpression(Widget, String, XEvent*); + +/* actions */ +void XawPrintActionErrorMsg(String, Widget, String*, Cardinal*); +XawActionResList *XawGetActionResList(WidgetClass); +XawActionVarList *XawGetActionVarList(Widget); + +void XawSetValuesAction(Widget, XEvent*, String*, Cardinal*); +void XawGetValuesAction(Widget, XEvent*, String*, Cardinal*); +void XawDeclareAction(Widget, XEvent*, String*, Cardinal*); +void XawCallProcAction(Widget, XEvent*, String*, Cardinal*); + +/* display lists */ +#define XAWDL_CONVERT_ERROR (XtPointer)-1 +typedef struct _XawDL _XawDisplayList; +typedef struct _XawDLClass XawDLClass, XawDisplayListClass; + +typedef void (*XawDisplayListProc)(Widget, XtPointer, XtPointer, + XEvent*, Region); +typedef void *(*XawDLArgsInitProc)(String, String*, Cardinal*, + Screen*, Colormap, int); +typedef void *(*XawDLDataInitProc)(String, + Screen*, Colormap, int); +typedef void (*XawDLArgsDestructor)(Display*, String, XtPointer, + String*, Cardinal*); +typedef void (*XawDLDataDestructor)(Display*, String, XtPointer); + +void XawRunDisplayList(Widget, _XawDisplayList*, XEvent*, Region); +void XawDisplayListInitialize(void); + +_XawDisplayList *XawCreateDisplayList(String, Screen*, Colormap, int); +void XawDestroyDisplayList(_XawDisplayList*); +String XawDisplayListString(_XawDisplayList*); +XawDLClass *XawGetDisplayListClass(String); +XawDLClass *XawCreateDisplayListClass(String, + XawDLArgsInitProc, XawDLArgsDestructor, + XawDLDataInitProc, XawDLDataDestructor); +Bool XawDeclareDisplayListProc(XawDLClass*, String, XawDisplayListProc); + +/* pixmaps */ +typedef struct _XawArgVal { + String name; + String value; +} XawArgVal; + +typedef struct _XawParams { + String name; + String type; + String ext; + XawArgVal **args; + Cardinal num_args; +} XawParams; + +typedef struct _XawPixmap { + String name; + Pixmap pixmap; + Pixmap mask; + Dimension width; + Dimension height; +} XawPixmap; + +typedef Bool (*XawPixmapLoader)(XawParams*, Screen*, Colormap, int, + Pixmap*, Pixmap*, + Dimension*, Dimension*); +Bool XawPixmapsInitialize(void); +Bool XawAddPixmapLoader(String, String, XawPixmapLoader); +XawPixmap *XawLoadPixmap(String, Screen*, Colormap, int); +XawPixmap *XawPixmapFromXPixmap(Pixmap, Screen*, Colormap, int); +XawParams *XawParseParamsString(String name); +void XawFreeParamsStruct(XawParams *params); +XawArgVal *XawFindArgVal(XawParams *params, String name); +void XawReshapeWidget(Widget, XawPixmap*); +#endif /* OLDXAW */ + +/* misc */ +void XawTypeToStringWarning(Display*, String); + +/* OS.c */ +int _XawGetPageSize(void); + +#endif /* _XawPrivate_h */ diff --git a/src/Tip.c b/src/Tip.c new file mode 100644 index 0000000..761492a --- /dev/null +++ b/src/Tip.c @@ -0,0 +1,631 @@ +/* + * Copyright (c) 1999 by The XFree86 Project, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF + * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Except as contained in this notice, the name of the XFree86 Project shall + * not be used in advertising or otherwise to promote the sale, use or other + * dealings in this Software without prior written authorization from the + * XFree86 Project. + * + * Author: Paulo César Pereira de Andrade + */ + +/* $XFree86: xc/lib/Xaw/Tip.c,v 1.5 2000/05/18 16:29:53 dawes Exp $ */ + +#include <X11/IntrinsicP.h> +#include <X11/StringDefs.h> +#include <X11/Xos.h> +#include <X11/Xaw/TipP.h> +#include <X11/Xaw/XawInit.h> +#include <X11/Xmu/Converters.h> +#include "Private.h" + +#define TIP_EVENT_MASK (ButtonPressMask | \ + ButtonReleaseMask | \ + PointerMotionMask | \ + ButtonMotionMask | \ + KeyPressMask | \ + KeyReleaseMask | \ + EnterWindowMask | \ + LeaveWindowMask) + +/* + * Types + */ +typedef struct _XawTipInfo { + Screen *screen; + TipWidget tip; + Widget widget; + Bool mapped; + struct _XawTipInfo *next; +} XawTipInfo; + +/* + * Class Methods + */ +static void XawTipClassInitialize(void); +static void XawTipInitialize(Widget, Widget, ArgList, Cardinal*); +static void XawTipDestroy(Widget); +static void XawTipExpose(Widget, XEvent*, Region); +static void XawTipRealize(Widget, Mask*, XSetWindowAttributes*); +static Boolean XawTipSetValues(Widget, Widget, Widget, ArgList, Cardinal*); + +/* + * Prototypes + */ +static void TipEventHandler(Widget, XtPointer, XEvent*, Boolean*); +static void TipShellEventHandler(Widget, XtPointer, XEvent*, Boolean*); +static XawTipInfo *CreateTipInfo(Widget); +static XawTipInfo *FindTipInfo(Widget); +static void ResetTip(XawTipInfo*, Bool); +static void TipTimeoutCallback(XtPointer, XtIntervalId*); +static void TipLayout(XawTipInfo*); +static void TipPosition(XawTipInfo*); + +/* + * Initialization + */ +#define offset(field) XtOffsetOf(TipRec, tip.field) +static XtResource resources[] = { + { + XtNforeground, + XtCForeground, + XtRPixel, + sizeof(Pixel), + offset(foreground), + XtRString, + XtDefaultForeground, + }, + { + XtNfont, + XtCFont, + XtRFontStruct, + sizeof(XFontStruct*), + offset(font), + XtRString, + XtDefaultFont + }, + { + XtNfontSet, + XtCFontSet, + XtRFontSet, + sizeof(XFontSet), + offset(fontset), + XtRString, + XtDefaultFontSet + }, + { + XtNtopMargin, + XtCVerticalMargins, + XtRDimension, + sizeof(Dimension), + offset(top_margin), + XtRImmediate, + (XtPointer)2 + }, + { + XtNbottomMargin, + XtCVerticalMargins, + XtRDimension, + sizeof(Dimension), + offset(bottom_margin), + XtRImmediate, + (XtPointer)2 + }, + { + XtNleftMargin, + XtCHorizontalMargins, + XtRDimension, + sizeof(Dimension), + offset(left_margin), + XtRImmediate, + (XtPointer)6 + }, + { + XtNrightMargin, + XtCHorizontalMargins, + XtRDimension, + sizeof(Dimension), + offset(right_margin), + XtRImmediate, + (XtPointer)6 + }, + { + XtNbackingStore, + XtCBackingStore, + XtRBackingStore, + sizeof(int), + offset(backing_store), + XtRImmediate, + (XtPointer)(Always + WhenMapped + NotUseful) + }, + { + XtNtimeout, + XtCTimeout, + XtRInt, + sizeof(int), + offset(timeout), + XtRImmediate, + (XtPointer)500 + }, + { + XawNdisplayList, + XawCDisplayList, + XawRDisplayList, + sizeof(XawDisplayList*), + offset(display_list), + XtRImmediate, + NULL + }, +}; +#undef offset + +TipClassRec tipClassRec = { + /* core */ + { + (WidgetClass)&widgetClassRec, /* superclass */ + "Tip", /* class_name */ + sizeof(TipRec), /* widget_size */ + XawTipClassInitialize, /* class_initialize */ + NULL, /* class_part_initialize */ + False, /* class_inited */ + XawTipInitialize, /* initialize */ + NULL, /* initialize_hook */ + XawTipRealize, /* realize */ + NULL, /* actions */ + 0, /* num_actions */ + resources, /* resources */ + XtNumber(resources), /* num_resources */ + NULLQUARK, /* xrm_class */ + True, /* compress_motion */ + True, /* compress_exposure */ + True, /* compress_enterleave */ + False, /* visible_interest */ + XawTipDestroy, /* destroy */ + NULL, /* resize */ + XawTipExpose, /* expose */ + XawTipSetValues, /* set_values */ + NULL, /* set_values_hook */ + XtInheritSetValuesAlmost, /* set_values_almost */ + NULL, /* get_values_hook */ + NULL, /* accept_focus */ + XtVersion, /* version */ + NULL, /* callback_private */ + NULL, /* tm_table */ + XtInheritQueryGeometry, /* query_geometry */ + XtInheritDisplayAccelerator, /* display_accelerator */ + NULL, /* extension */ + }, + /* tip */ + { + NULL, /* extension */ + }, +}; + +WidgetClass tipWidgetClass = (WidgetClass)&tipClassRec; + +static XawTipInfo *first_tip; + +/* + * Implementation + */ +static void +XawTipClassInitialize(void) +{ + XawInitializeWidgetSet(); + XtAddConverter(XtRString, XtRBackingStore, XmuCvtStringToBackingStore, + NULL, 0); + XtSetTypeConverter(XtRBackingStore, XtRString, XmuCvtBackingStoreToString, + NULL, 0, XtCacheNone, NULL); +} + +/*ARGSUSED*/ +static void +XawTipInitialize(Widget req, Widget w, ArgList args, Cardinal *num_args) +{ + TipWidget tip = (TipWidget)w; + XGCValues values; + + tip->tip.timer = 0; + + values.foreground = tip->tip.foreground; + values.background = tip->core.background_pixel; + values.font = tip->tip.font->fid; + values.graphics_exposures = False; + + tip->tip.gc = XtAllocateGC(w, 0, GCForeground | GCBackground | GCFont | + GCGraphicsExposures, &values, GCFont, 0); +} + +static void +XawTipDestroy(Widget w) +{ + XawTipInfo *info = FindTipInfo(w); + TipWidget tip = (TipWidget)w; + + if (tip->tip.timer) + XtRemoveTimeOut(tip->tip.timer); + + XtReleaseGC(w, tip->tip.gc); + + XtRemoveEventHandler(XtParent(w), KeyPressMask, False, TipShellEventHandler, + (XtPointer)NULL); + if (info == first_tip) + first_tip = first_tip->next; + else { + XawTipInfo *p = first_tip; + + while (p && p->next != info) + p = p->next; + if (p) + p->next = info->next; + } + XtFree((char*)info); +} + +static void +XawTipRealize(Widget w, Mask *mask, XSetWindowAttributes *attr) +{ + TipWidget tip = (TipWidget)w; + + if (tip->tip.backing_store == Always || + tip->tip.backing_store == NotUseful || + tip->tip.backing_store == WhenMapped) { + *mask |= CWBackingStore; + attr->backing_store = tip->tip.backing_store; + } + else + *mask &= ~CWBackingStore; + *mask |= CWOverrideRedirect; + attr->override_redirect = True; + + XtWindow(w) = XCreateWindow(DisplayOfScreen(XtScreen(w)), + RootWindowOfScreen(XtScreen(w)), + XtX(w), XtY(w), + XtWidth(w) ? XtWidth(w) : 1, + XtHeight(w) ? XtHeight(w) : 1, + XtBorderWidth(w), + DefaultDepthOfScreen(XtScreen(w)), + InputOutput, + CopyFromParent, + *mask, attr); +} + +static void +XawTipExpose(Widget w, XEvent *event, Region region) +{ + TipWidget tip = (TipWidget)w; + GC gc = tip->tip.gc; + char *nl, *label = tip->tip.label; + Position y = tip->tip.top_margin + tip->tip.font->max_bounds.ascent; + int len; + + if (tip->tip.display_list) + XawRunDisplayList(w, tip->tip.display_list, event, region); + + if (tip->tip.international == True) { + Position ksy = tip->tip.top_margin; + XFontSetExtents *ext = XExtentsOfFontSet(tip->tip.fontset); + + ksy += XawAbs(ext->max_ink_extent.y); + + while ((nl = index(label, '\n')) != NULL) { + XmbDrawString(XtDisplay(w), XtWindow(w), tip->tip.fontset, + gc, tip->tip.left_margin, ksy, label, + (int)(nl - label)); + ksy += ext->max_ink_extent.height; + label = nl + 1; + } + len = strlen(label); + if (len) + XmbDrawString(XtDisplay(w), XtWindow(w), tip->tip.fontset, gc, + tip->tip.left_margin, ksy, label, len); + } + else { + while ((nl = index(label, '\n')) != NULL) { + if (tip->tip.encoding) + XDrawString16(XtDisplay(w), XtWindow(w), gc, + tip->tip.left_margin, y, + (XChar2b*)label, (int)(nl - label) >> 1); + else + XDrawString(XtDisplay(w), XtWindow(w), gc, + tip->tip.left_margin, y, label, (int)(nl - label)); + y += tip->tip.font->max_bounds.ascent + + tip->tip.font->max_bounds.descent; + label = nl + 1; + } + len = strlen(label); + if (len) { + if (tip->tip.encoding) + XDrawString16(XtDisplay(w), XtWindow(w), gc, + tip->tip.left_margin, y, (XChar2b*)label, len >> 1); + else + XDrawString(XtDisplay(w), XtWindow(w), gc, + tip->tip.left_margin, y, label, len); + } + } +} + +/*ARGSUSED*/ +static Boolean +XawTipSetValues(Widget current, Widget request, Widget cnew, + ArgList args, Cardinal *num_args) +{ + TipWidget curtip = (TipWidget)current; + TipWidget newtip = (TipWidget)cnew; + Boolean redisplay = False; + + if (curtip->tip.font->fid != newtip->tip.font->fid || + curtip->tip.foreground != newtip->tip.foreground) { + XGCValues values; + + values.foreground = newtip->tip.foreground; + values.background = newtip->core.background_pixel; + values.font = newtip->tip.font->fid; + values.graphics_exposures = False; + XtReleaseGC(cnew, curtip->tip.gc); + newtip->tip.gc = XtAllocateGC(cnew, 0, GCForeground | GCBackground | + GCFont | GCGraphicsExposures, &values, + GCFont, 0); + redisplay = True; + } + if (curtip->tip.display_list != newtip->tip.display_list) + redisplay = True; + + return (redisplay); +} + +static void +TipLayout(XawTipInfo *info) +{ + XFontStruct *fs = info->tip->tip.font; + int width = 0, height; + char *nl, *label = info->tip->tip.label; + + if (info->tip->tip.international == True) { + XFontSet fset = info->tip->tip.fontset; + XFontSetExtents *ext = XExtentsOfFontSet(fset); + + height = ext->max_ink_extent.height; + if ((nl = index(label, '\n')) != NULL) { + /*CONSTCOND*/ + while (True) { + int w = XmbTextEscapement(fset, label, (int)(nl - label)); + + if (w > width) + width = w; + if (*nl == '\0') + break; + label = nl + 1; + if (*label) + height += ext->max_ink_extent.height; + if ((nl = index(label, '\n')) == NULL) + nl = index(label, '\0'); + } + } + else + width = XmbTextEscapement(fset, label, strlen(label)); + } + else { + height = fs->max_bounds.ascent + fs->max_bounds.descent; + if ((nl = index(label, '\n')) != NULL) { + /*CONSTCOND*/ + while (True) { + int w = info->tip->tip.encoding ? + XTextWidth16(fs, (XChar2b*)label, (int)(nl - label) >> 1) : + XTextWidth(fs, label, (int)(nl - label)); + if (w > width) + width = w; + if (*nl == '\0') + break; + label = nl + 1; + if (*label) + height += fs->max_bounds.ascent + fs->max_bounds.descent; + if ((nl = index(label, '\n')) == NULL) + nl = index(label, '\0'); + } + } + else + width = info->tip->tip.encoding ? + XTextWidth16(fs, (XChar2b*)label, strlen(label) >> 1) : + XTextWidth(fs, label, strlen(label)); + } + XtWidth(info->tip) = width + info->tip->tip.left_margin + + info->tip->tip.right_margin; + XtHeight(info->tip) = height + info->tip->tip.top_margin + + info->tip->tip.bottom_margin; +} + +#define DEFAULT_TIP_Y_OFFSET 12 +static void +TipPosition(XawTipInfo *info) +{ + Window r, c; + int rx, ry, wx, wy; + unsigned mask; + Position x, y; + + XQueryPointer(XtDisplay((Widget)info->tip), XtWindow((Widget)info->tip), + &r, &c, &rx, &ry, &wx, &wy, &mask); + x = rx - (XtWidth(info->tip) >> 1); + y = ry + DEFAULT_TIP_Y_OFFSET; + + if (x >= 0) { + int scr_width = WidthOfScreen(XtScreen(info->tip)); + + if (x + XtWidth(info->tip) + XtBorderWidth(info->tip) > scr_width) + x = scr_width - XtWidth(info->tip) - XtBorderWidth(info->tip); + } + if (x < 0) + x = 0; + if (y >= 0) { + int scr_height = HeightOfScreen(XtScreen(info->tip)); + + if (y + XtHeight(info->tip) + XtBorderWidth(info->tip) > scr_height) + y -= XtHeight(info->tip) + XtBorderWidth(info->tip) + + (DEFAULT_TIP_Y_OFFSET << 1); + } + if (y < 0) + y = 0; + + XMoveResizeWindow(XtDisplay(info->tip), XtWindow(info->tip), + (int)(XtX(info->tip) = x), (int)(XtY(info->tip) = y), + (unsigned)XtWidth(info->tip), (unsigned)XtHeight(info->tip)); +} + +static XawTipInfo * +CreateTipInfo(Widget w) +{ + XawTipInfo *info = XtNew(XawTipInfo); + Widget shell = w; + + info->screen = XtScreen(w); + + while (XtParent(shell)) + shell = XtParent(shell); + + info->tip = (TipWidget)XtCreateWidget("tip", tipWidgetClass, shell, NULL, 0); + XtRealizeWidget((Widget)info->tip); + info->widget = NULL; + info->mapped = False; + info->next = NULL; + XtAddEventHandler(shell, KeyPressMask, False, TipShellEventHandler, + (XtPointer)NULL); + + return (info); +} + +static XawTipInfo * +FindTipInfo(Widget w) +{ + XawTipInfo *ptip, *tip = first_tip; + Screen *screen = XtScreenOfObject(w); + + if (tip == NULL) + return (first_tip = tip = CreateTipInfo(w)); + + for (ptip = tip; tip; ptip = tip, tip = tip->next) + if (tip->screen == screen) + return (tip); + + return (ptip->next = CreateTipInfo(w)); +} + +static void +ResetTip(XawTipInfo *info, Bool add_timeout) +{ + if (info->tip->tip.timer) { + XtRemoveTimeOut(info->tip->tip.timer); + info->tip->tip.timer = 0; + } + if (info->mapped) { + XtRemoveGrab(XtParent((Widget)info->tip)); + XUnmapWindow(XtDisplay((Widget)info->tip), XtWindow((Widget)info->tip)); + info->mapped = False; + } + if (add_timeout) { + info->tip->tip.timer = + XtAppAddTimeOut(XtWidgetToApplicationContext((Widget)info->tip), + info->tip->tip.timeout, TipTimeoutCallback, + (XtPointer)info); + } +} + +static void +TipTimeoutCallback(XtPointer closure, XtIntervalId *id) +{ + XawTipInfo *info = (XawTipInfo*)closure; + Arg args[3]; + + info->tip->tip.label = NULL; + info->tip->tip.international = False; + info->tip->tip.encoding = 0; + XtSetArg(args[0], XtNtip, &info->tip->tip.label); + XtSetArg(args[1], XtNinternational, &info->tip->tip.international); + XtSetArg(args[2], XtNencoding, &info->tip->tip.encoding); + XtGetValues(info->widget, args, 3); + + if (info->tip->tip.label) { + TipLayout(info); + TipPosition(info); + XMapRaised(XtDisplay((Widget)info->tip), XtWindow((Widget)info->tip)); + XtAddGrab(XtParent((Widget)info->tip), True, True); + info->mapped = True; + } +} + +/*ARGSUSED*/ +static void +TipShellEventHandler(Widget w, XtPointer client_data, XEvent *event, + Boolean *continue_to_dispatch) +{ + ResetTip(FindTipInfo(w), False); +} + +/*ARGSUSED*/ +static void +TipEventHandler(Widget w, XtPointer client_data, XEvent *event, + Boolean *continue_to_dispatch) +{ + XawTipInfo *info = FindTipInfo(w); + Boolean add_timeout; + + if (info->widget != w) { + ResetTip(info, False); + info->widget = w; + } + + switch (event->type) { + case EnterNotify: + add_timeout = True; + break; + case MotionNotify: + /* If any button is pressed, timer is 0 */ + if (info->mapped) + return; + add_timeout = info->tip->tip.timer != 0; + break; + default: + add_timeout = False; + break; + } + ResetTip(info, add_timeout); +} + +/* + * Public routines + */ +void +XawTipEnable(Widget w) +{ + XtAddEventHandler(w, TIP_EVENT_MASK, False, TipEventHandler, + (XtPointer)NULL); +} + +void +XawTipDisable(Widget w) +{ + XawTipInfo *info = FindTipInfo(w); + + XtRemoveEventHandler(w, TIP_EVENT_MASK, False, TipEventHandler, + (XtPointer)NULL); + if (info->widget == w) + ResetTip(info, False); +} |