togl.c

来自「CNC 的开放码,EMC2 V2.2.8版」· C语言 代码 · 共 2,204 行 · 第 1/5 页

C
2,204
字号
         return t;      t = t->Next;   }   return NULL;}#if defined(X11)/* * Return an X colormap to use for OpenGL RGB-mode rendering. * Input:  dpy - the X display *         scrnum - the X screen number *         visinfo - the XVisualInfo as returned by glXChooseVisual() * Return:  an X Colormap or 0 if there's a _serious_ error. */static Colormapget_rgb_colormap( Display *dpy,                  int scrnum,                  const XVisualInfo *visinfo,                  Tk_Window tkwin){   Atom hp_cr_maps;   Status status;   int numCmaps;   int i;   XStandardColormap *standardCmaps;   Window root = XRootWindow(dpy,scrnum);   int using_mesa;   /*    * First check if visinfo's visual matches the default/root visual.    */   if (visinfo->visual==Tk_Visual(tkwin)) {      /* use the default/root colormap */      Colormap cmap;      cmap = Tk_Colormap(tkwin);#ifdef MESA_COLOR_HACK      (void) get_free_color_cells( dpy, scrnum, cmap);#endif      return cmap;   }   /*    * Check if we're using Mesa.    */   if (strstr(glXQueryServerString( dpy, scrnum, GLX_VERSION ), "Mesa")) {      using_mesa = 1;   }   else {      using_mesa = 0;   }   /*    * Next, if we're using Mesa and displaying on an HP with the "Color    * Recovery" feature and the visual is 8-bit TrueColor, search for a    * special colormap initialized for dithering.  Mesa will know how to    * dither using this colormap.    */   if (using_mesa) {      hp_cr_maps = XInternAtom( dpy, "_HP_RGB_SMOOTH_MAP_LIST", True );      if (hp_cr_maps#ifdef __cplusplus	  && visinfo->visual->c_class==TrueColor#else	  && visinfo->visual->class==TrueColor#endif	  && visinfo->depth==8) {	 status = XGetRGBColormaps( dpy, root, &standardCmaps,				    &numCmaps, hp_cr_maps );	 if (status) {	    for (i=0; i<numCmaps; i++) {	       if (standardCmaps[i].visualid == visinfo->visual->visualid) {                  Colormap cmap = standardCmaps[i].colormap;                  XFree( standardCmaps );		  return cmap;	       }	    }            XFree(standardCmaps);	 }      }   }   /*    * Next, try to find a standard X colormap.    */#if !HP && !SUN#ifndef SOLARIS_BUG   status = XmuLookupStandardColormap( dpy, visinfo->screen,				       visinfo->visualid, visinfo->depth,				       XA_RGB_DEFAULT_MAP,				       /* replace */ False, /* retain */ True);   if (status == 1) {      status = XGetRGBColormaps( dpy, root, &standardCmaps,				 &numCmaps, XA_RGB_DEFAULT_MAP);      if (status == 1) {         for (i = 0; i < numCmaps; i++) {	    if (standardCmaps[i].visualid == visinfo->visualid) {               Colormap cmap = standardCmaps[i].colormap;	       XFree(standardCmaps);	       return cmap;	    }	 }         XFree(standardCmaps);      }   }#endif#endif   /*    * If we get here, give up and just allocate a new colormap.    */   return XCreateColormap( dpy, root, visinfo->visual, AllocNone );}#elif defined(WIN32)/* Code to create RGB palette is taken from the GENGL sample program   of Win32 SDK */static unsigned char threeto8[8] = {    0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377};static unsigned char twoto8[4] = {    0, 0x55, 0xaa, 0xff};static unsigned char oneto8[2] = {    0, 255};static int defaultOverride[13] = {    0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91};static PALETTEENTRY defaultPalEntry[20] = {    { 0,   0,   0,    0 },    { 0x80,0,   0,    0 },    { 0,   0x80,0,    0 },    { 0x80,0x80,0,    0 },    { 0,   0,   0x80, 0 },    { 0x80,0,   0x80, 0 },    { 0,   0x80,0x80, 0 },    { 0xC0,0xC0,0xC0, 0 },    { 192, 220, 192,  0 },    { 166, 202, 240,  0 },    { 255, 251, 240,  0 },    { 160, 160, 164,  0 },    { 0x80,0x80,0x80, 0 },    { 0xFF,0,   0,    0 },    { 0,   0xFF,0,    0 },    { 0xFF,0xFF,0,    0 },    { 0,   0,   0xFF, 0 },    { 0xFF,0,   0xFF, 0 },    { 0,   0xFF,0xFF, 0 },    { 0xFF,0xFF,0xFF, 0 }};static unsigned charComponentFromIndex(int i, UINT nbits, UINT shift){    unsigned char val;    val = (unsigned char) (i >> shift);    switch (nbits) {    case 1:        val &= 0x1;        return oneto8[val];    case 2:        val &= 0x3;        return twoto8[val];    case 3:        val &= 0x7;        return threeto8[val];    default:        return 0;    }}static Colormap Win32CreateRgbColormap(PIXELFORMATDESCRIPTOR pfd){    TkWinColormap *cmap = (TkWinColormap *) ckalloc(sizeof(TkWinColormap));    LOGPALETTE *pPal;    int n, i;    n = 1 << pfd.cColorBits;    pPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +            n * sizeof(PALETTEENTRY));    pPal->palVersion = 0x300;    pPal->palNumEntries = n;    for (i=0; i<n; i++) {        pPal->palPalEntry[i].peRed =                ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);        pPal->palPalEntry[i].peGreen =                ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);        pPal->palPalEntry[i].peBlue =                ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);        pPal->palPalEntry[i].peFlags = 0;    }    /* fix up the palette to include the default GDI palette */    if ((pfd.cColorBits == 8)                           &&        (pfd.cRedBits   == 3) && (pfd.cRedShift   == 0) &&        (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&        (pfd.cBlueBits  == 2) && (pfd.cBlueShift  == 6)       ) {        for (i = 1 ; i <= 12 ; i++)            pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];    }    cmap->palette = CreatePalette(pPal);    LocalFree(pPal);    cmap->size = n;    cmap->stale = 0;    /* Since this is a private colormap of a fix size, we do not need       a valid hash table, but a dummy one */    Tcl_InitHashTable(&cmap->refCounts, TCL_ONE_WORD_KEYS);    return (Colormap)cmap;}static Colormap Win32CreateCiColormap(struct Togl *togl){    /* Create a colormap with size of togl->CiColormapSize and set all       entries to black */    LOGPALETTE logPalette;    TkWinColormap *cmap = (TkWinColormap *) ckalloc(sizeof(TkWinColormap));    logPalette.palVersion = 0x300;    logPalette.palNumEntries = 1;    logPalette.palPalEntry[0].peRed = 0;    logPalette.palPalEntry[0].peGreen = 0;    logPalette.palPalEntry[0].peBlue = 0;    logPalette.palPalEntry[0].peFlags = 0;    cmap->palette = CreatePalette(&logPalette);    cmap->size = togl->CiColormapSize;    ResizePalette(cmap->palette, cmap->size);  /* sets new entries to black */    cmap->stale = 0;    /* Since this is a private colormap of a fix size, we do not need       a valid hash table, but a dummy one */    Tcl_InitHashTable(&cmap->refCounts, TCL_ONE_WORD_KEYS);    return (Colormap)cmap;}#endif /*X11*//* * Togl_Init * *   Called upon system startup to create Togl command. */int Togl_Init(Tcl_Interp *interp){   int major,minor,patchLevel,releaseType;#ifdef USE_TCL_STUBS   if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {return TCL_ERROR;}#endif#ifdef USE_TK_STUBS   if (Tk_InitStubs(interp, "8.1", 0) == NULL) {return TCL_ERROR;}#endif   /* Skip all this on Tcl/Tk 8.0 or older.  Seems to work */#if TCL_MAJOR_VERSION * 100 + TCL_MINOR_VERSION > 800   Tcl_GetVersion(&major,&minor,&patchLevel,&releaseType);#ifndef HAVE_TK_SETCLASSPROCS   if (major >= 8 && minor >= 4) {     TCL_ERR(interp,"Sorry, this instance of Togl was not compiled to work with Tcl/Tk 8.4 or higher.");   }#endif#endif   if (Tcl_PkgProvide(interp, "Togl", TOGL_VERSION) != TCL_OK) {      return TCL_ERROR;   }   Tcl_CreateCommand(interp, "togl", (Tcl_CmdProc *)Togl_Cmd,                     (ClientData) Tk_MainWindow(interp), NULL);   Tcl_InitHashTable(&CommandTable, TCL_STRING_KEYS);   return TCL_OK;}/* * Register a C function to be called when an Togl widget is realized. */void Togl_CreateFunc( Togl_Callback *proc ){   DefaultCreateProc = proc;}/* * Register a C function to be called when an Togl widget must be redrawn. */void Togl_DisplayFunc( Togl_Callback *proc ){   DefaultDisplayProc = proc;}/* * Register a C function to be called when an Togl widget is resized. */void Togl_ReshapeFunc( Togl_Callback *proc ){   DefaultReshapeProc = proc;}/* * Register a C function to be called when an Togl widget is destroyed. */void Togl_DestroyFunc( Togl_Callback *proc ){   DefaultDestroyProc = proc;}/* * Register a C function to be called from TimerEventHandler. */void Togl_TimerFunc( Togl_Callback *proc ){   DefaultTimerProc = proc;}/* * Reset default callback pointers to NULL. */void Togl_ResetDefaultCallbacks( void ){   DefaultCreateProc = NULL;   DefaultDisplayProc = NULL;   DefaultReshapeProc = NULL;   DefaultDestroyProc = NULL;   DefaultOverlayDisplayProc = NULL;   DefaultTimerProc = NULL;   DefaultClientData = NULL;}/* * Chnage the create callback for a specific Togl widget. */void Togl_SetCreateFunc( struct Togl *togl, Togl_Callback *proc ){   togl->CreateProc = proc;}/* * Change the display/redraw callback for a specific Togl widget. */void Togl_SetDisplayFunc( struct Togl *togl, Togl_Callback *proc ){   togl->DisplayProc = proc;}/* * Change the reshape callback for a specific Togl widget. */void Togl_SetReshapeFunc( struct Togl *togl, Togl_Callback *proc ){   togl->ReshapeProc = proc;}/* * Change the destroy callback for a specific Togl widget. */void Togl_SetDestroyFunc( struct Togl *togl, Togl_Callback *proc ){   togl->DestroyProc = proc;}/* * Togl_Timer * * Gets called from Tk_CreateTimerHandler. */static void Togl_Timer( ClientData clientData ){   struct Togl *togl = (struct Togl *) clientData;   if (togl->TimerProc) {      togl->TimerProc(togl);      /* Re-register this callback since Tcl/Tk timers are "one-shot".       * That is, after the timer callback is called it not normally       * called again.  That's not the behavior we want for Togl.       */#if (TK_MAJOR_VERSION * 100 + TK_MINOR_VERSION) >= 401      togl->timerHandler =         Tcl_CreateTimerHandler( togl->TimerInterval, Togl_Timer, (ClientData)togl );#else      togl->timerHandler =         Tk_CreateTimerHandler( togl->TimeInterval, Togl_Timer, (ClientData)togl );#endif   }}/* * Change the timer callback for a specific Togl widget. * Pass NULL to disable the callback. */void Togl_SetTimerFunc( struct Togl *togl, Togl_Callback *proc ){   togl->TimerProc = proc;   if (proc) {#if (TK_MAJOR_VERSION * 100 + TK_MINOR_VERSION) >= 401      togl->timerHandler =         Tcl_CreateTimerHandler( togl->TimerInterval, Togl_Timer, (ClientData)togl );#else      togl->timerHandler =         Tk_CreateTimerHandler( togl->TimeInterval, Togl_Timer, (ClientData)togl );#endif   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?