OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [start/] [insight/] [tk/] [generic/] [tkBitmap.c] - Diff between revs 579 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 579 Rev 1765
/*
/*
 * tkBitmap.c --
 * tkBitmap.c --
 *
 *
 *      This file maintains a database of read-only bitmaps for the Tk
 *      This file maintains a database of read-only bitmaps for the Tk
 *      toolkit.  This allows bitmaps to be shared between widgets and
 *      toolkit.  This allows bitmaps to be shared between widgets and
 *      also avoids interactions with the X server.
 *      also avoids interactions with the X server.
 *
 *
 * Copyright (c) 1990-1994 The Regents of the University of California.
 * Copyright (c) 1990-1994 The Regents of the University of California.
 * Copyright (c) 1994-1996 Sun Microsystems, Inc.
 * Copyright (c) 1994-1996 Sun Microsystems, Inc.
 *
 *
 * See the file "license.terms" for information on usage and redistribution
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 *
 * RCS: @(#) $Id: tkBitmap.c,v 1.1.1.1 2002-01-16 10:25:50 markom Exp $
 * RCS: @(#) $Id: tkBitmap.c,v 1.1.1.1 2002-01-16 10:25:50 markom Exp $
 */
 */
 
 
#include "tkPort.h"
#include "tkPort.h"
#include "tkInt.h"
#include "tkInt.h"
 
 
/*
/*
 * The includes below are for pre-defined bitmaps.
 * The includes below are for pre-defined bitmaps.
 *
 *
 * Platform-specific issue: Windows complains when the bitmaps are
 * Platform-specific issue: Windows complains when the bitmaps are
 * included, because an array of characters is being initialized with
 * included, because an array of characters is being initialized with
 * integers as elements.  For lint purposes, the following pragmas
 * integers as elements.  For lint purposes, the following pragmas
 * temporarily turn off that warning message.
 * temporarily turn off that warning message.
 */
 */
 
 
#if defined(__WIN32__) || defined(_WIN32)
#if defined(__WIN32__) || defined(_WIN32)
#pragma warning (disable : 4305)
#pragma warning (disable : 4305)
#endif
#endif
 
 
#include "error.bmp"
#include "error.bmp"
#include "gray12.bmp"
#include "gray12.bmp"
#include "gray25.bmp"
#include "gray25.bmp"
#include "gray50.bmp"
#include "gray50.bmp"
#include "gray75.bmp"
#include "gray75.bmp"
#include "hourglass.bmp"
#include "hourglass.bmp"
#include "info.bmp"
#include "info.bmp"
#include "questhead.bmp"
#include "questhead.bmp"
#include "question.bmp"
#include "question.bmp"
#include "warning.bmp"
#include "warning.bmp"
 
 
#if defined(__WIN32__) || defined(_WIN32)
#if defined(__WIN32__) || defined(_WIN32)
#pragma warning (default : 4305)
#pragma warning (default : 4305)
#endif
#endif
 
 
/*
/*
 * One of the following data structures exists for each bitmap that is
 * One of the following data structures exists for each bitmap that is
 * currently in use.  Each structure is indexed with both "idTable" and
 * currently in use.  Each structure is indexed with both "idTable" and
 * "nameTable".
 * "nameTable".
 */
 */
 
 
typedef struct {
typedef struct {
    Pixmap bitmap;              /* X identifier for bitmap.  None means this
    Pixmap bitmap;              /* X identifier for bitmap.  None means this
                                 * bitmap was created by Tk_DefineBitmap
                                 * bitmap was created by Tk_DefineBitmap
                                 * and it isn't currently in use. */
                                 * and it isn't currently in use. */
    int width, height;          /* Dimensions of bitmap. */
    int width, height;          /* Dimensions of bitmap. */
    Display *display;           /* Display for which bitmap is valid. */
    Display *display;           /* Display for which bitmap is valid. */
    int refCount;               /* Number of active uses of bitmap. */
    int refCount;               /* Number of active uses of bitmap. */
    Tcl_HashEntry *hashPtr;     /* Entry in nameTable for this structure
    Tcl_HashEntry *hashPtr;     /* Entry in nameTable for this structure
                                 * (needed when deleting). */
                                 * (needed when deleting). */
} TkBitmap;
} TkBitmap;
 
 
/*
/*
 * Hash table to map from a textual description of a bitmap to the
 * Hash table to map from a textual description of a bitmap to the
 * TkBitmap record for the bitmap, and key structure used in that
 * TkBitmap record for the bitmap, and key structure used in that
 * hash table:
 * hash table:
 */
 */
 
 
static Tcl_HashTable nameTable;
static Tcl_HashTable nameTable;
typedef struct {
typedef struct {
    Tk_Uid name;                /* Textual name for desired bitmap. */
    Tk_Uid name;                /* Textual name for desired bitmap. */
    Screen *screen;             /* Screen on which bitmap will be used. */
    Screen *screen;             /* Screen on which bitmap will be used. */
} NameKey;
} NameKey;
 
 
/*
/*
 * Hash table that maps from <display + bitmap id> to the TkBitmap structure
 * Hash table that maps from <display + bitmap id> to the TkBitmap structure
 * for the bitmap.  This table is used by Tk_FreeBitmap.
 * for the bitmap.  This table is used by Tk_FreeBitmap.
 */
 */
 
 
static Tcl_HashTable idTable;
static Tcl_HashTable idTable;
typedef struct {
typedef struct {
    Display *display;           /* Display for which bitmap was allocated. */
    Display *display;           /* Display for which bitmap was allocated. */
    Pixmap pixmap;              /* X identifier for pixmap. */
    Pixmap pixmap;              /* X identifier for pixmap. */
} IdKey;
} IdKey;
 
 
/*
/*
 * Hash table create by Tk_DefineBitmap to map from a name to a
 * Hash table create by Tk_DefineBitmap to map from a name to a
 * collection of in-core data about a bitmap.  The table is
 * collection of in-core data about a bitmap.  The table is
 * indexed by the address of the data for the bitmap, and the entries
 * indexed by the address of the data for the bitmap, and the entries
 * contain pointers to TkPredefBitmap structures.
 * contain pointers to TkPredefBitmap structures.
 */
 */
 
 
Tcl_HashTable tkPredefBitmapTable;
Tcl_HashTable tkPredefBitmapTable;
 
 
/*
/*
 * Hash table used by Tk_GetBitmapFromData to map from a collection
 * Hash table used by Tk_GetBitmapFromData to map from a collection
 * of in-core data about a bitmap to a Tk_Uid giving an automatically-
 * of in-core data about a bitmap to a Tk_Uid giving an automatically-
 * generated name for the bitmap:
 * generated name for the bitmap:
 */
 */
 
 
static Tcl_HashTable dataTable;
static Tcl_HashTable dataTable;
typedef struct {
typedef struct {
    char *source;               /* Bitmap bits. */
    char *source;               /* Bitmap bits. */
    int width, height;          /* Dimensions of bitmap. */
    int width, height;          /* Dimensions of bitmap. */
} DataKey;
} DataKey;
 
 
static int initialized = 0;      /* 0 means static structures haven't been
static int initialized = 0;      /* 0 means static structures haven't been
                                 * initialized yet. */
                                 * initialized yet. */
 
 
/*
/*
 * Forward declarations for procedures defined in this file:
 * Forward declarations for procedures defined in this file:
 */
 */
 
 
static void             BitmapInit _ANSI_ARGS_((void));
static void             BitmapInit _ANSI_ARGS_((void));


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tk_GetBitmap --
 * Tk_GetBitmap --
 *
 *
 *      Given a string describing a bitmap, locate (or create if necessary)
 *      Given a string describing a bitmap, locate (or create if necessary)
 *      a bitmap that fits the description.
 *      a bitmap that fits the description.
 *
 *
 * Results:
 * Results:
 *      The return value is the X identifer for the desired bitmap
 *      The return value is the X identifer for the desired bitmap
 *      (i.e. a Pixmap with a single plane), unless string couldn't be
 *      (i.e. a Pixmap with a single plane), unless string couldn't be
 *      parsed correctly.  In this case, None is returned and an error
 *      parsed correctly.  In this case, None is returned and an error
 *      message is left in interp->result.  The caller should never
 *      message is left in interp->result.  The caller should never
 *      modify the bitmap that is returned, and should eventually call
 *      modify the bitmap that is returned, and should eventually call
 *      Tk_FreeBitmap when the bitmap is no longer needed.
 *      Tk_FreeBitmap when the bitmap is no longer needed.
 *
 *
 * Side effects:
 * Side effects:
 *      The bitmap is added to an internal database with a reference count.
 *      The bitmap is added to an internal database with a reference count.
 *      For each call to this procedure, there should eventually be a call
 *      For each call to this procedure, there should eventually be a call
 *      to Tk_FreeBitmap, so that the database can be cleaned up when bitmaps
 *      to Tk_FreeBitmap, so that the database can be cleaned up when bitmaps
 *      aren't needed anymore.
 *      aren't needed anymore.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
Pixmap
Pixmap
Tk_GetBitmap(interp, tkwin, string)
Tk_GetBitmap(interp, tkwin, string)
    Tcl_Interp *interp;         /* Interpreter to use for error reporting,
    Tcl_Interp *interp;         /* Interpreter to use for error reporting,
                                 * this may be NULL. */
                                 * this may be NULL. */
    Tk_Window tkwin;            /* Window in which bitmap will be used. */
    Tk_Window tkwin;            /* Window in which bitmap will be used. */
    Tk_Uid string;              /* Description of bitmap.  See manual entry
    Tk_Uid string;              /* Description of bitmap.  See manual entry
                                 * for details on legal syntax. */
                                 * for details on legal syntax. */
{
{
    NameKey nameKey;
    NameKey nameKey;
    IdKey idKey;
    IdKey idKey;
    Tcl_HashEntry *nameHashPtr, *idHashPtr, *predefHashPtr;
    Tcl_HashEntry *nameHashPtr, *idHashPtr, *predefHashPtr;
    register TkBitmap *bitmapPtr;
    register TkBitmap *bitmapPtr;
    TkPredefBitmap *predefPtr;
    TkPredefBitmap *predefPtr;
    int new;
    int new;
    Pixmap bitmap;
    Pixmap bitmap;
    int width, height;
    int width, height;
    int dummy2;
    int dummy2;
 
 
    if (!initialized) {
    if (!initialized) {
        BitmapInit();
        BitmapInit();
    }
    }
 
 
    nameKey.name = string;
    nameKey.name = string;
    nameKey.screen = Tk_Screen(tkwin);
    nameKey.screen = Tk_Screen(tkwin);
    nameHashPtr = Tcl_CreateHashEntry(&nameTable, (char *) &nameKey, &new);
    nameHashPtr = Tcl_CreateHashEntry(&nameTable, (char *) &nameKey, &new);
    if (!new) {
    if (!new) {
        bitmapPtr = (TkBitmap *) Tcl_GetHashValue(nameHashPtr);
        bitmapPtr = (TkBitmap *) Tcl_GetHashValue(nameHashPtr);
        bitmapPtr->refCount++;
        bitmapPtr->refCount++;
        return bitmapPtr->bitmap;
        return bitmapPtr->bitmap;
    }
    }
 
 
    /*
    /*
     * No suitable bitmap exists.  Create a new bitmap from the
     * No suitable bitmap exists.  Create a new bitmap from the
     * information contained in the string.  If the string starts
     * information contained in the string.  If the string starts
     * with "@" then the rest of the string is a file name containing
     * with "@" then the rest of the string is a file name containing
     * the bitmap.  Otherwise the string must refer to a bitmap
     * the bitmap.  Otherwise the string must refer to a bitmap
     * defined by a call to Tk_DefineBitmap.
     * defined by a call to Tk_DefineBitmap.
     */
     */
 
 
    if (*string == '@') {
    if (*string == '@') {
        Tcl_DString buffer;
        Tcl_DString buffer;
        int result;
        int result;
 
 
        if (Tcl_IsSafe(interp)) {
        if (Tcl_IsSafe(interp)) {
            Tcl_AppendResult(interp, "can't specify bitmap with '@' in a",
            Tcl_AppendResult(interp, "can't specify bitmap with '@' in a",
                    " safe interpreter", (char *) NULL);
                    " safe interpreter", (char *) NULL);
            goto error;
            goto error;
        }
        }
 
 
        string = Tcl_TranslateFileName(interp, string + 1, &buffer);
        string = Tcl_TranslateFileName(interp, string + 1, &buffer);
        if (string == NULL) {
        if (string == NULL) {
            goto error;
            goto error;
        }
        }
        result = TkReadBitmapFile(Tk_Display(tkwin),
        result = TkReadBitmapFile(Tk_Display(tkwin),
                RootWindowOfScreen(nameKey.screen), string,
                RootWindowOfScreen(nameKey.screen), string,
                (unsigned int *) &width, (unsigned int *) &height,
                (unsigned int *) &width, (unsigned int *) &height,
                &bitmap, &dummy2, &dummy2);
                &bitmap, &dummy2, &dummy2);
        if (result != BitmapSuccess) {
        if (result != BitmapSuccess) {
            if (interp != NULL) {
            if (interp != NULL) {
                Tcl_AppendResult(interp, "error reading bitmap file \"", string,
                Tcl_AppendResult(interp, "error reading bitmap file \"", string,
                    "\"", (char *) NULL);
                    "\"", (char *) NULL);
            }
            }
            Tcl_DStringFree(&buffer);
            Tcl_DStringFree(&buffer);
            goto error;
            goto error;
        }
        }
        Tcl_DStringFree(&buffer);
        Tcl_DStringFree(&buffer);
    } else {
    } else {
        predefHashPtr = Tcl_FindHashEntry(&tkPredefBitmapTable, string);
        predefHashPtr = Tcl_FindHashEntry(&tkPredefBitmapTable, string);
        if (predefHashPtr == NULL) {
        if (predefHashPtr == NULL) {
            /*
            /*
             * The following platform specific call allows the user to
             * The following platform specific call allows the user to
             * define bitmaps that may only exist during run time.  If
             * define bitmaps that may only exist during run time.  If
             * it returns None nothing was found and we return the error.
             * it returns None nothing was found and we return the error.
             */
             */
            bitmap = TkpGetNativeAppBitmap(Tk_Display(tkwin), string,
            bitmap = TkpGetNativeAppBitmap(Tk_Display(tkwin), string,
                    &width, &height);
                    &width, &height);
 
 
            if (bitmap == None) {
            if (bitmap == None) {
                if (interp != NULL) {
                if (interp != NULL) {
                    Tcl_AppendResult(interp, "bitmap \"", string,
                    Tcl_AppendResult(interp, "bitmap \"", string,
                        "\" not defined", (char *) NULL);
                        "\" not defined", (char *) NULL);
                }
                }
                goto error;
                goto error;
            }
            }
        } else {
        } else {
            predefPtr = (TkPredefBitmap *) Tcl_GetHashValue(predefHashPtr);
            predefPtr = (TkPredefBitmap *) Tcl_GetHashValue(predefHashPtr);
            width = predefPtr->width;
            width = predefPtr->width;
            height = predefPtr->height;
            height = predefPtr->height;
            if (predefPtr->native) {
            if (predefPtr->native) {
                bitmap = TkpCreateNativeBitmap(Tk_Display(tkwin),
                bitmap = TkpCreateNativeBitmap(Tk_Display(tkwin),
                    predefPtr->source);
                    predefPtr->source);
                if (bitmap == None) {
                if (bitmap == None) {
                    panic("native bitmap creation failed");
                    panic("native bitmap creation failed");
                }
                }
            } else {
            } else {
                bitmap = XCreateBitmapFromData(Tk_Display(tkwin),
                bitmap = XCreateBitmapFromData(Tk_Display(tkwin),
                    RootWindowOfScreen(nameKey.screen), predefPtr->source,
                    RootWindowOfScreen(nameKey.screen), predefPtr->source,
                    (unsigned) width, (unsigned) height);
                    (unsigned) width, (unsigned) height);
            }
            }
        }
        }
    }
    }
 
 
    /*
    /*
     * Add information about this bitmap to our database.
     * Add information about this bitmap to our database.
     */
     */
 
 
    bitmapPtr = (TkBitmap *) ckalloc(sizeof(TkBitmap));
    bitmapPtr = (TkBitmap *) ckalloc(sizeof(TkBitmap));
    bitmapPtr->bitmap = bitmap;
    bitmapPtr->bitmap = bitmap;
    bitmapPtr->width = width;
    bitmapPtr->width = width;
    bitmapPtr->height = height;
    bitmapPtr->height = height;
    bitmapPtr->display = Tk_Display(tkwin);
    bitmapPtr->display = Tk_Display(tkwin);
    bitmapPtr->refCount = 1;
    bitmapPtr->refCount = 1;
    bitmapPtr->hashPtr = nameHashPtr;
    bitmapPtr->hashPtr = nameHashPtr;
    idKey.display = bitmapPtr->display;
    idKey.display = bitmapPtr->display;
    idKey.pixmap = bitmap;
    idKey.pixmap = bitmap;
    idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) &idKey,
    idHashPtr = Tcl_CreateHashEntry(&idTable, (char *) &idKey,
            &new);
            &new);
    if (!new) {
    if (!new) {
        panic("bitmap already registered in Tk_GetBitmap");
        panic("bitmap already registered in Tk_GetBitmap");
    }
    }
    Tcl_SetHashValue(nameHashPtr, bitmapPtr);
    Tcl_SetHashValue(nameHashPtr, bitmapPtr);
    Tcl_SetHashValue(idHashPtr, bitmapPtr);
    Tcl_SetHashValue(idHashPtr, bitmapPtr);
    return bitmapPtr->bitmap;
    return bitmapPtr->bitmap;
 
 
    error:
    error:
    Tcl_DeleteHashEntry(nameHashPtr);
    Tcl_DeleteHashEntry(nameHashPtr);
    return None;
    return None;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tk_DefineBitmap --
 * Tk_DefineBitmap --
 *
 *
 *      This procedure associates a textual name with a binary bitmap
 *      This procedure associates a textual name with a binary bitmap
 *      description, so that the name may be used to refer to the
 *      description, so that the name may be used to refer to the
 *      bitmap in future calls to Tk_GetBitmap.
 *      bitmap in future calls to Tk_GetBitmap.
 *
 *
 * Results:
 * Results:
 *      A standard Tcl result.  If an error occurs then TCL_ERROR is
 *      A standard Tcl result.  If an error occurs then TCL_ERROR is
 *      returned and a message is left in interp->result.
 *      returned and a message is left in interp->result.
 *
 *
 * Side effects:
 * Side effects:
 *      "Name" is entered into the bitmap table and may be used from
 *      "Name" is entered into the bitmap table and may be used from
 *      here on to refer to the given bitmap.
 *      here on to refer to the given bitmap.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
int
int
Tk_DefineBitmap(interp, name, source, width, height)
Tk_DefineBitmap(interp, name, source, width, height)
    Tcl_Interp *interp;         /* Interpreter to use for error reporting. */
    Tcl_Interp *interp;         /* Interpreter to use for error reporting. */
    Tk_Uid name;                /* Name to use for bitmap.  Must not already
    Tk_Uid name;                /* Name to use for bitmap.  Must not already
                                 * be defined as a bitmap. */
                                 * be defined as a bitmap. */
    char *source;               /* Address of bits for bitmap. */
    char *source;               /* Address of bits for bitmap. */
    int width;                  /* Width of bitmap. */
    int width;                  /* Width of bitmap. */
    int height;                 /* Height of bitmap. */
    int height;                 /* Height of bitmap. */
{
{
    int new;
    int new;
    Tcl_HashEntry *predefHashPtr;
    Tcl_HashEntry *predefHashPtr;
    TkPredefBitmap *predefPtr;
    TkPredefBitmap *predefPtr;
 
 
    if (!initialized) {
    if (!initialized) {
        BitmapInit();
        BitmapInit();
    }
    }
 
 
    predefHashPtr = Tcl_CreateHashEntry(&tkPredefBitmapTable, name, &new);
    predefHashPtr = Tcl_CreateHashEntry(&tkPredefBitmapTable, name, &new);
    if (!new) {
    if (!new) {
        Tcl_AppendResult(interp, "bitmap \"", name,
        Tcl_AppendResult(interp, "bitmap \"", name,
                "\" is already defined", (char *) NULL);
                "\" is already defined", (char *) NULL);
        return TCL_ERROR;
        return TCL_ERROR;
    }
    }
    predefPtr = (TkPredefBitmap *) ckalloc(sizeof(TkPredefBitmap));
    predefPtr = (TkPredefBitmap *) ckalloc(sizeof(TkPredefBitmap));
    predefPtr->source = source;
    predefPtr->source = source;
    predefPtr->width = width;
    predefPtr->width = width;
    predefPtr->height = height;
    predefPtr->height = height;
    predefPtr->native = 0;
    predefPtr->native = 0;
    Tcl_SetHashValue(predefHashPtr, predefPtr);
    Tcl_SetHashValue(predefHashPtr, predefPtr);
    return TCL_OK;
    return TCL_OK;
}
}


/*
/*
 *--------------------------------------------------------------
 *--------------------------------------------------------------
 *
 *
 * Tk_NameOfBitmap --
 * Tk_NameOfBitmap --
 *
 *
 *      Given a bitmap, return a textual string identifying the
 *      Given a bitmap, return a textual string identifying the
 *      bitmap.
 *      bitmap.
 *
 *
 * Results:
 * Results:
 *      The return value is the string name associated with bitmap.
 *      The return value is the string name associated with bitmap.
 *
 *
 * Side effects:
 * Side effects:
 *      None.
 *      None.
 *
 *
 *--------------------------------------------------------------
 *--------------------------------------------------------------
 */
 */
 
 
Tk_Uid
Tk_Uid
Tk_NameOfBitmap(display, bitmap)
Tk_NameOfBitmap(display, bitmap)
    Display *display;                   /* Display for which bitmap was
    Display *display;                   /* Display for which bitmap was
                                         * allocated. */
                                         * allocated. */
    Pixmap bitmap;                      /* Bitmap whose name is wanted. */
    Pixmap bitmap;                      /* Bitmap whose name is wanted. */
{
{
    IdKey idKey;
    IdKey idKey;
    Tcl_HashEntry *idHashPtr;
    Tcl_HashEntry *idHashPtr;
    TkBitmap *bitmapPtr;
    TkBitmap *bitmapPtr;
 
 
    if (!initialized) {
    if (!initialized) {
        unknown:
        unknown:
        panic("Tk_NameOfBitmap received unknown bitmap argument");
        panic("Tk_NameOfBitmap received unknown bitmap argument");
    }
    }
 
 
    idKey.display = display;
    idKey.display = display;
    idKey.pixmap = bitmap;
    idKey.pixmap = bitmap;
    idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
    idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
    if (idHashPtr == NULL) {
    if (idHashPtr == NULL) {
        goto unknown;
        goto unknown;
    }
    }
    bitmapPtr = (TkBitmap *) Tcl_GetHashValue(idHashPtr);
    bitmapPtr = (TkBitmap *) Tcl_GetHashValue(idHashPtr);
    return ((NameKey *) bitmapPtr->hashPtr->key.words)->name;
    return ((NameKey *) bitmapPtr->hashPtr->key.words)->name;
}
}


/*
/*
 *--------------------------------------------------------------
 *--------------------------------------------------------------
 *
 *
 * Tk_SizeOfBitmap --
 * Tk_SizeOfBitmap --
 *
 *
 *      Given a bitmap managed by this module, returns the width
 *      Given a bitmap managed by this module, returns the width
 *      and height of the bitmap.
 *      and height of the bitmap.
 *
 *
 * Results:
 * Results:
 *      The words at *widthPtr and *heightPtr are filled in with
 *      The words at *widthPtr and *heightPtr are filled in with
 *      the dimenstions of bitmap.
 *      the dimenstions of bitmap.
 *
 *
 * Side effects:
 * Side effects:
 *      If bitmap isn't managed by this module then the procedure
 *      If bitmap isn't managed by this module then the procedure
 *      panics..
 *      panics..
 *
 *
 *--------------------------------------------------------------
 *--------------------------------------------------------------
 */
 */
 
 
void
void
Tk_SizeOfBitmap(display, bitmap, widthPtr, heightPtr)
Tk_SizeOfBitmap(display, bitmap, widthPtr, heightPtr)
    Display *display;                   /* Display for which bitmap was
    Display *display;                   /* Display for which bitmap was
                                         * allocated. */
                                         * allocated. */
    Pixmap bitmap;                      /* Bitmap whose size is wanted. */
    Pixmap bitmap;                      /* Bitmap whose size is wanted. */
    int *widthPtr;                      /* Store bitmap width here. */
    int *widthPtr;                      /* Store bitmap width here. */
    int *heightPtr;                     /* Store bitmap height here. */
    int *heightPtr;                     /* Store bitmap height here. */
{
{
    IdKey idKey;
    IdKey idKey;
    Tcl_HashEntry *idHashPtr;
    Tcl_HashEntry *idHashPtr;
    TkBitmap *bitmapPtr;
    TkBitmap *bitmapPtr;
 
 
    if (!initialized) {
    if (!initialized) {
        unknownBitmap:
        unknownBitmap:
        panic("Tk_SizeOfBitmap received unknown bitmap argument");
        panic("Tk_SizeOfBitmap received unknown bitmap argument");
    }
    }
 
 
    idKey.display = display;
    idKey.display = display;
    idKey.pixmap = bitmap;
    idKey.pixmap = bitmap;
    idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
    idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
    if (idHashPtr == NULL) {
    if (idHashPtr == NULL) {
        goto unknownBitmap;
        goto unknownBitmap;
    }
    }
    bitmapPtr = (TkBitmap *) Tcl_GetHashValue(idHashPtr);
    bitmapPtr = (TkBitmap *) Tcl_GetHashValue(idHashPtr);
    *widthPtr = bitmapPtr->width;
    *widthPtr = bitmapPtr->width;
    *heightPtr = bitmapPtr->height;
    *heightPtr = bitmapPtr->height;
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tk_FreeBitmap --
 * Tk_FreeBitmap --
 *
 *
 *      This procedure is called to release a bitmap allocated by
 *      This procedure is called to release a bitmap allocated by
 *      Tk_GetBitmap or TkGetBitmapFromData.
 *      Tk_GetBitmap or TkGetBitmapFromData.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      The reference count associated with bitmap is decremented, and
 *      The reference count associated with bitmap is decremented, and
 *      it is officially deallocated if no-one is using it anymore.
 *      it is officially deallocated if no-one is using it anymore.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
void
void
Tk_FreeBitmap(display, bitmap)
Tk_FreeBitmap(display, bitmap)
    Display *display;                   /* Display for which bitmap was
    Display *display;                   /* Display for which bitmap was
                                         * allocated. */
                                         * allocated. */
    Pixmap bitmap;                      /* Bitmap to be released. */
    Pixmap bitmap;                      /* Bitmap to be released. */
{
{
    Tcl_HashEntry *idHashPtr;
    Tcl_HashEntry *idHashPtr;
    register TkBitmap *bitmapPtr;
    register TkBitmap *bitmapPtr;
    IdKey idKey;
    IdKey idKey;
 
 
    if (!initialized) {
    if (!initialized) {
        panic("Tk_FreeBitmap called before Tk_GetBitmap");
        panic("Tk_FreeBitmap called before Tk_GetBitmap");
    }
    }
 
 
    idKey.display = display;
    idKey.display = display;
    idKey.pixmap = bitmap;
    idKey.pixmap = bitmap;
    idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
    idHashPtr = Tcl_FindHashEntry(&idTable, (char *) &idKey);
    if (idHashPtr == NULL) {
    if (idHashPtr == NULL) {
        panic("Tk_FreeBitmap received unknown bitmap argument");
        panic("Tk_FreeBitmap received unknown bitmap argument");
    }
    }
    bitmapPtr = (TkBitmap *) Tcl_GetHashValue(idHashPtr);
    bitmapPtr = (TkBitmap *) Tcl_GetHashValue(idHashPtr);
    bitmapPtr->refCount--;
    bitmapPtr->refCount--;
    if (bitmapPtr->refCount == 0) {
    if (bitmapPtr->refCount == 0) {
        Tk_FreePixmap(bitmapPtr->display, bitmapPtr->bitmap);
        Tk_FreePixmap(bitmapPtr->display, bitmapPtr->bitmap);
        Tcl_DeleteHashEntry(idHashPtr);
        Tcl_DeleteHashEntry(idHashPtr);
        Tcl_DeleteHashEntry(bitmapPtr->hashPtr);
        Tcl_DeleteHashEntry(bitmapPtr->hashPtr);
        ckfree((char *) bitmapPtr);
        ckfree((char *) bitmapPtr);
    }
    }
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * Tk_GetBitmapFromData --
 * Tk_GetBitmapFromData --
 *
 *
 *      Given a description of the bits for a bitmap, make a bitmap that
 *      Given a description of the bits for a bitmap, make a bitmap that
 *      has the given properties. *** NOTE:  this procedure is obsolete
 *      has the given properties. *** NOTE:  this procedure is obsolete
 *      and really shouldn't be used anymore. ***
 *      and really shouldn't be used anymore. ***
 *
 *
 * Results:
 * Results:
 *      The return value is the X identifer for the desired bitmap
 *      The return value is the X identifer for the desired bitmap
 *      (a one-plane Pixmap), unless it couldn't be created properly.
 *      (a one-plane Pixmap), unless it couldn't be created properly.
 *      In this case, None is returned and an error message is left in
 *      In this case, None is returned and an error message is left in
 *      interp->result.  The caller should never modify the bitmap that
 *      interp->result.  The caller should never modify the bitmap that
 *      is returned, and should eventually call Tk_FreeBitmap when the
 *      is returned, and should eventually call Tk_FreeBitmap when the
 *      bitmap is no longer needed.
 *      bitmap is no longer needed.
 *
 *
 * Side effects:
 * Side effects:
 *      The bitmap is added to an internal database with a reference count.
 *      The bitmap is added to an internal database with a reference count.
 *      For each call to this procedure, there should eventually be a call
 *      For each call to this procedure, there should eventually be a call
 *      to Tk_FreeBitmap, so that the database can be cleaned up when bitmaps
 *      to Tk_FreeBitmap, so that the database can be cleaned up when bitmaps
 *      aren't needed anymore.
 *      aren't needed anymore.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
        /* ARGSUSED */
        /* ARGSUSED */
Pixmap
Pixmap
Tk_GetBitmapFromData(interp, tkwin, source, width, height)
Tk_GetBitmapFromData(interp, tkwin, source, width, height)
    Tcl_Interp *interp;         /* Interpreter to use for error reporting. */
    Tcl_Interp *interp;         /* Interpreter to use for error reporting. */
    Tk_Window tkwin;            /* Window in which bitmap will be used. */
    Tk_Window tkwin;            /* Window in which bitmap will be used. */
    char *source;               /* Bitmap data for bitmap shape. */
    char *source;               /* Bitmap data for bitmap shape. */
    int width, height;          /* Dimensions of bitmap. */
    int width, height;          /* Dimensions of bitmap. */
{
{
    DataKey nameKey;
    DataKey nameKey;
    Tcl_HashEntry *dataHashPtr;
    Tcl_HashEntry *dataHashPtr;
    Tk_Uid name;
    Tk_Uid name;
    int new;
    int new;
    char string[20];
    char string[20];
    static int autoNumber = 0;
    static int autoNumber = 0;
 
 
    if (!initialized) {
    if (!initialized) {
        BitmapInit();
        BitmapInit();
    }
    }
 
 
    nameKey.source = source;
    nameKey.source = source;
    nameKey.width = width;
    nameKey.width = width;
    nameKey.height = height;
    nameKey.height = height;
    dataHashPtr = Tcl_CreateHashEntry(&dataTable, (char *) &nameKey, &new);
    dataHashPtr = Tcl_CreateHashEntry(&dataTable, (char *) &nameKey, &new);
    if (!new) {
    if (!new) {
        name = (Tk_Uid) Tcl_GetHashValue(dataHashPtr);
        name = (Tk_Uid) Tcl_GetHashValue(dataHashPtr);
    } else {
    } else {
        autoNumber++;
        autoNumber++;
        sprintf(string, "_tk%d", autoNumber);
        sprintf(string, "_tk%d", autoNumber);
        name = Tk_GetUid(string);
        name = Tk_GetUid(string);
        Tcl_SetHashValue(dataHashPtr, name);
        Tcl_SetHashValue(dataHashPtr, name);
        if (Tk_DefineBitmap(interp, name, source, width, height) != TCL_OK) {
        if (Tk_DefineBitmap(interp, name, source, width, height) != TCL_OK) {
            Tcl_DeleteHashEntry(dataHashPtr);
            Tcl_DeleteHashEntry(dataHashPtr);
            return TCL_ERROR;
            return TCL_ERROR;
        }
        }
    }
    }
    return Tk_GetBitmap(interp, tkwin, name);
    return Tk_GetBitmap(interp, tkwin, name);
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * BitmapInit --
 * BitmapInit --
 *
 *
 *      Initialize the structures used for bitmap management.
 *      Initialize the structures used for bitmap management.
 *
 *
 * Results:
 * Results:
 *      None.
 *      None.
 *
 *
 * Side effects:
 * Side effects:
 *      Read the code.
 *      Read the code.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
static void
static void
BitmapInit()
BitmapInit()
{
{
    Tcl_Interp *dummy;
    Tcl_Interp *dummy;
 
 
    dummy = Tcl_CreateInterp();
    dummy = Tcl_CreateInterp();
    initialized = 1;
    initialized = 1;
    Tcl_InitHashTable(&nameTable, sizeof(NameKey)/sizeof(int));
    Tcl_InitHashTable(&nameTable, sizeof(NameKey)/sizeof(int));
    Tcl_InitHashTable(&dataTable, sizeof(DataKey)/sizeof(int));
    Tcl_InitHashTable(&dataTable, sizeof(DataKey)/sizeof(int));
    Tcl_InitHashTable(&tkPredefBitmapTable, TCL_ONE_WORD_KEYS);
    Tcl_InitHashTable(&tkPredefBitmapTable, TCL_ONE_WORD_KEYS);
 
 
    /*
    /*
     * The call below is tricky:  can't use sizeof(IdKey) because it
     * The call below is tricky:  can't use sizeof(IdKey) because it
     * gets padded with extra unpredictable bytes on some 64-bit
     * gets padded with extra unpredictable bytes on some 64-bit
     * machines.
     * machines.
     */
     */
 
 
    Tcl_InitHashTable(&idTable, (sizeof(Display *) + sizeof(Pixmap))
    Tcl_InitHashTable(&idTable, (sizeof(Display *) + sizeof(Pixmap))
            /sizeof(int));
            /sizeof(int));
 
 
    Tk_DefineBitmap(dummy, Tk_GetUid("error"), (char *) error_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("error"), (char *) error_bits,
            error_width, error_height);
            error_width, error_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("gray75"), (char *) gray75_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("gray75"), (char *) gray75_bits,
            gray75_width, gray75_height);
            gray75_width, gray75_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("gray50"), (char *) gray50_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("gray50"), (char *) gray50_bits,
            gray50_width, gray50_height);
            gray50_width, gray50_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("gray25"), (char *) gray25_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("gray25"), (char *) gray25_bits,
            gray25_width, gray25_height);
            gray25_width, gray25_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("gray12"), (char *) gray12_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("gray12"), (char *) gray12_bits,
            gray12_width, gray12_height);
            gray12_width, gray12_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("hourglass"), (char *) hourglass_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("hourglass"), (char *) hourglass_bits,
            hourglass_width, hourglass_height);
            hourglass_width, hourglass_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("info"), (char *) info_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("info"), (char *) info_bits,
            info_width, info_height);
            info_width, info_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("questhead"), (char *) questhead_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("questhead"), (char *) questhead_bits,
            questhead_width, questhead_height);
            questhead_width, questhead_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("question"), (char *) question_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("question"), (char *) question_bits,
            question_width, question_height);
            question_width, question_height);
    Tk_DefineBitmap(dummy, Tk_GetUid("warning"), (char *) warning_bits,
    Tk_DefineBitmap(dummy, Tk_GetUid("warning"), (char *) warning_bits,
            warning_width, warning_height);
            warning_width, warning_height);
 
 
    TkpDefineNativeBitmaps();
    TkpDefineNativeBitmaps();
 
 
    Tcl_DeleteInterp(dummy);
    Tcl_DeleteInterp(dummy);
}
}


/*
/*
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 *
 *
 * TkReadBitmapFile --
 * TkReadBitmapFile --
 *
 *
 *      Loads a bitmap image in X bitmap format into the specified
 *      Loads a bitmap image in X bitmap format into the specified
 *      drawable.  This is equivelent to the XReadBitmapFile in X.
 *      drawable.  This is equivelent to the XReadBitmapFile in X.
 *
 *
 * Results:
 * Results:
 *      Sets the size, hotspot, and bitmap on success.
 *      Sets the size, hotspot, and bitmap on success.
 *
 *
 * Side effects:
 * Side effects:
 *      Creates a new bitmap from the file data.
 *      Creates a new bitmap from the file data.
 *
 *
 *----------------------------------------------------------------------
 *----------------------------------------------------------------------
 */
 */
 
 
int
int
TkReadBitmapFile(display, d, filename, width_return, height_return,
TkReadBitmapFile(display, d, filename, width_return, height_return,
        bitmap_return, x_hot_return, y_hot_return)
        bitmap_return, x_hot_return, y_hot_return)
    Display* display;
    Display* display;
    Drawable d;
    Drawable d;
    CONST char* filename;
    CONST char* filename;
    unsigned int* width_return;
    unsigned int* width_return;
    unsigned int* height_return;
    unsigned int* height_return;
    Pixmap* bitmap_return;
    Pixmap* bitmap_return;
    int* x_hot_return;
    int* x_hot_return;
    int* y_hot_return;
    int* y_hot_return;
{
{
    char *data;
    char *data;
 
 
    data = TkGetBitmapData(NULL, NULL, (char *) filename,
    data = TkGetBitmapData(NULL, NULL, (char *) filename,
            (int *) width_return, (int *) height_return, x_hot_return,
            (int *) width_return, (int *) height_return, x_hot_return,
            y_hot_return);
            y_hot_return);
    if (data == NULL) {
    if (data == NULL) {
        return BitmapFileInvalid;
        return BitmapFileInvalid;
    }
    }
 
 
    *bitmap_return = XCreateBitmapFromData(display, d, data, *width_return,
    *bitmap_return = XCreateBitmapFromData(display, d, data, *width_return,
            *height_return);
            *height_return);
 
 
    ckfree(data);
    ckfree(data);
    return BitmapSuccess;
    return BitmapSuccess;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.