/* Xaw3dXft.h,v 1.0 10/11/07 08:07:27 demailly Exp $ */

/***********************************************************

Copyright (c) 2010  J.-P. Demailly

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
X CONSORTIUM 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.

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/

#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>

#define DEFAULTFONT "Liberation-9"
#ifndef Pixel
typedef unsigned long Pixel;
#endif

int Xaw3dXftEncoding = 0;
int Xaw3dXftStringHilight = 0;
int Xaw3dXftStringUsePixmap = 0;
int Xaw3dXftMenuSpacing = 1;
int Xaw3dXftShowTips = 1;
int Xaw3dXftTipDoGrab = 1;
int Xaw3dXftBorderHack = 1;
int Xaw3dXftNoHilitReverse = 0;

Pixel Xaw3dXftTipBackgroundColor = -1;

unsigned char * Xaw3dXftHilitColor = NULL;
unsigned short Xaw3dXftInsensitiveTwist[4] = { 0, 0, 0, 0};

char * Xaw3dXftDefaultFontName = NULL;
XftFont * Xaw3dXftDefaultFont = NULL;

void Xaw3dXftSetDefaultHilitColor()
{
    Xaw3dXftHilitColor = strdup("#000000");
}

void Xaw3dXftSetHilitColor(char * value)
{
    if (Xaw3dXftHilitColor)
        free(Xaw3dXftHilitColor);
    Xaw3dXftHilitColor = strdup(value);
}

void Xaw3dXftSetDefaultFontName(char *name)
{
    if (Xaw3dXftDefaultFontName)
        free(Xaw3dXftDefaultFontName);
    Xaw3dXftDefaultFontName = strdup(name);
}

static int hexa(char c) 
{
    if (c>='0' && c<='9') return c-'0'; else
    if (c>='a' && c<='f') return c-'a'+10; else
    if (c>='A' && c<='F') return c-'A'+10; else
    return 0;
}

void Xaw3dXftSetInsensitiveTwist(char *value)
{
    int i;
    if (!value) return;
    if (*value == '|') 
        Xaw3dXftInsensitiveTwist[0] = 1;
    else
    if (*value == '&') 
        Xaw3dXftInsensitiveTwist[0] = 2;
    else
    if (*value == '^') 
        Xaw3dXftInsensitiveTwist[0] = 3;
    else
    if (*value == '~') {
        if (strlen(value)<3) return;
        Xaw3dXftInsensitiveTwist[0] = 4;
        Xaw3dXftInsensitiveTwist[1] = (hexa(value[1])*16+hexa(value[2]))*257;
        return;
    } else
        Xaw3dXftInsensitiveTwist[0] = 0;
    if (strlen(value)<7) return;
    for (i=1; i<=3; i++)
        Xaw3dXftInsensitiveTwist[i] = (hexa(value[2*i-1])*16+hexa(value[2*i]))*257;
}

XftFont * Xaw3dXftGetFont(Display *dpy, char *name)
{
    if (name) {
        if (strncasecmp(name, "core:",5))
            return XftFontOpenName(dpy, DefaultScreen(dpy), name);
        else
            return XftFontOpenXlfd(dpy, DefaultScreen(dpy), name+5);
    }
    if (!Xaw3dXftDefaultFont) {
        if (!Xaw3dXftDefaultFontName)
	    Xaw3dXftDefaultFontName = DEFAULTFONT;
        if (strncasecmp(Xaw3dXftDefaultFontName, "xlfd:",5))
            Xaw3dXftDefaultFont = 
                XftFontOpenName(dpy, DefaultScreen(dpy), Xaw3dXftDefaultFontName);
        else
            Xaw3dXftDefaultFont = 
                XftFontOpenXlfd(dpy, DefaultScreen(dpy), Xaw3dXftDefaultFontName+5);
        if (!Xaw3dXftDefaultFont)
            Xaw3dXftDefaultFont = 
                XftFontOpenName(dpy, DefaultScreen(dpy), DEFAULTFONT);
    }
    return Xaw3dXftDefaultFont;
}
