home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcltk805.zip / tcl805s.zip / tk8.0.5 / os2 / tkOS2Init.c < prev    next >
C/C++ Source or Header  |  2001-01-12  |  14KB  |  387 lines

  1. /* 
  2.  * tkOS2Init.c --
  3.  *
  4.  *    This file contains OS/2-specific interpreter initialization
  5.  *    functions.
  6.  *
  7.  * Copyright (c) 1996-2000 Illya Vaes
  8.  * Copyright (c) 1995-1997 Sun Microsystems, Inc.
  9.  *
  10.  * See the file "license.terms" for information on usage and redistribution
  11.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12.  */
  13.  
  14.  
  15. #include "tkOS2Int.h"
  16.  
  17. /*
  18.  * Global variables necessary in modules in the DLL
  19.  */
  20. HAB        tkHab;            /* Anchor block (instance handle) */
  21. HMQ        tkHmq;            /* Handle to message queue */
  22. LONG        aDevCaps[CAPS_DEVICE_POLYSET_POINTS+1]; /* Device Capabilities*/
  23. LONG        nextLogicalFont = 1;    /* First free logical font ID */
  24. PFNWP        oldFrameProc = WinDefWindowProc;/* subclassed frame procedure */
  25. LONG        xScreen;        /* System Value Screen width */
  26. LONG        yScreen;        /* System Value Screen height */
  27. LONG        titleBar;        /* System Value Title Bar */
  28. LONG        xBorder;        /* System Value X nominal border */
  29. LONG        yBorder;        /* System Value Y nominal border */
  30. LONG        xSizeBorder;        /* System Value X Sizing border */
  31. LONG        ySizeBorder;        /* System Value Y Sizing border */
  32. LONG        xDlgBorder;        /* System Value X dialog-frame border */
  33. LONG        yDlgBorder;        /* System Value Y dialog-frame border */
  34. HDC        hScreenDC;        /* Device Context for screen */
  35. HPS        globalPS;        /* Global PS for Fonts (Gpi*Char*) */
  36. HBITMAP        globalBitmap;        /* Bitmap for global PS */
  37. TkOS2Font    logfonts[255];        /* List of logical fonts */
  38. LONG        nextColor;        /* Next free index in color table */
  39. #ifdef IGNOREPMRES
  40.     LONG        overrideResolution= 72; /* If IGNOREPMRES is defined */
  41. #endif
  42. LONG        rc;            /* For checking return values */
  43.  
  44. /*
  45.  * The Init script (common to OS/2, Windows and Unix platforms) is
  46.  * defined in tkInitScript.h
  47.  */
  48.  
  49. #include "tkInitScript.h"
  50.  
  51.  
  52. /*
  53.  *----------------------------------------------------------------------
  54.  *
  55.  * TkpInit --
  56.  *
  57.  *    Performs OS/2-specific interpreter initialization related to the
  58.  *      tk_library variable.
  59.  *
  60.  * Results:
  61.  *    A standard Tcl completion code (TCL_OK or TCL_ERROR).  Also
  62.  *      leaves information in interp->result.
  63.  *
  64.  * Side effects:
  65.  *    Sets "tk_library" Tcl variable, runs "tk.tcl" script.
  66.  *
  67.  *----------------------------------------------------------------------
  68.  */
  69.  
  70. int
  71. TkpInit(interp)
  72.     Tcl_Interp *interp;
  73. {
  74.     return Tcl_Eval(interp, initScript);
  75. }
  76.  
  77. /*
  78.  *----------------------------------------------------------------------
  79.  *
  80.  * TkpGetAppName --
  81.  *
  82.  *      Retrieves the name of the current application from a platform
  83.  *      specific location.  For OS/2, the application name is the
  84.  *      root of the tail of the path contained in the tcl variable argv0.
  85.  *
  86.  * Results:
  87.  *      Returns the application name in the given Tcl_DString.
  88.  *
  89.  * Side effects:
  90.  *      None.
  91.  *
  92.  *----------------------------------------------------------------------
  93.  */
  94.  
  95. void
  96. TkpGetAppName(interp, namePtr)
  97.     Tcl_Interp *interp;
  98.     Tcl_DString *namePtr;       /* A previously initialized Tcl_DString. */
  99. {
  100.     int argc;
  101.     char **argv = NULL, *name, *p;
  102.  
  103.     name = Tcl_GetVar(interp, "argv0", TCL_GLOBAL_ONLY);
  104.     if (name != NULL) {
  105.         Tcl_SplitPath(name, &argc, &argv);
  106.         if (argc > 0) {
  107.             name = argv[argc-1];
  108.             p = strrchr(name, '.');
  109.             if (p != NULL) {
  110.                 *p = '\0';
  111.             }
  112.         } else {
  113.             name = NULL;
  114.         }
  115.     }
  116.     if ((name == NULL) || (*name == 0)) {
  117.         name = "tk";
  118.     }
  119.     Tcl_DStringAppend(namePtr, name, -1);
  120.     if (argv != NULL) {
  121.         ckfree((char *)argv);
  122.     }
  123. }
  124.  
  125. /*
  126.  *----------------------------------------------------------------------
  127.  *
  128.  * TkpDisplayWarning --
  129.  *
  130.  *      This routines is called from Tk_Main to display warning
  131.  *      messages that occur during startup.
  132.  *
  133.  * Results:
  134.  *      None.
  135.  *
  136.  * Side effects:
  137.  *      Displays a message box.
  138.  *
  139.  *----------------------------------------------------------------------
  140.  */
  141.  
  142. void
  143. TkpDisplayWarning(msg, title)
  144.     char *msg;                  /* Message to be displayed. */
  145.     char *title;                /* Title of warning. */
  146. {
  147.     WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, title, 0L,
  148.                   MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL | MB_MOVEABLE);
  149. }
  150.  
  151. /*
  152.  *----------------------------------------------------------------------
  153.  *
  154.  * TkOS2InitPM --
  155.  *
  156.  *    Performs OS/2 Presentation Manager intialisation.
  157.  *
  158.  * Results:
  159.  *    None.
  160.  *
  161.  * Side effects:
  162.  *    Fills the global variables tkHab and tkHmq.
  163.  *
  164.  *----------------------------------------------------------------------
  165.  */
  166.  
  167. HAB
  168. TkOS2InitPM (void)
  169. {
  170.     BOOL rc;
  171.     HDC hScreenDC;
  172.     LONG lStart, lCount;
  173.     DEVOPENSTRUC doStruc= {0L, (PSZ)"DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
  174.     SIZEL sizel = {0,0};
  175.     BITMAPINFOHEADER2 bmpInfo;
  176. #ifdef IGNOREPMRES
  177.     char *tkPmPixRes;
  178. #endif
  179. #ifdef VERBOSE
  180.     LONG *aBitmapFormats;
  181. #endif
  182.  
  183.         PPIB pibPtr;
  184.         PTIB tibPtr;
  185.  
  186.         /*
  187.          * Warp ourselves to PM; only of interest for CLI that really want
  188.          * to use PM services etc. and using the Tk DLL.
  189.          */
  190.         rc = DosGetInfoBlocks(&tibPtr, &pibPtr);
  191.         pibPtr->pib_ultype = 3;
  192.     /* Initialize PM */
  193.     if (!TclOS2PMInitialize()) {
  194.         return NULLHANDLE;
  195.     }
  196.     tkHab = TclOS2GetHAB();
  197.     if (tkHab == NULLHANDLE) {
  198.         return NULLHANDLE;
  199.     }
  200.     tkHmq = TclOS2GetHMQ(tkHab);
  201.     if (tkHmq == NULLHANDLE) {
  202.         return NULLHANDLE;
  203.     }
  204. #ifdef VERBOSE
  205.     printf("tkHab %x, tkHmq %x\n", tkHab, tkHmq);
  206. #endif
  207.  
  208.     /* Determine system values */
  209.     xScreen = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
  210.     yScreen = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
  211.     titleBar = WinQuerySysValue(HWND_DESKTOP, SV_CYTITLEBAR);
  212.     xBorder = WinQuerySysValue(HWND_DESKTOP, SV_CXBORDER);
  213.     yBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYBORDER);
  214.     xSizeBorder = WinQuerySysValue(HWND_DESKTOP, SV_CXSIZEBORDER);
  215.     ySizeBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYSIZEBORDER);
  216.     xDlgBorder = WinQuerySysValue(HWND_DESKTOP, SV_CXDLGFRAME);
  217.     yDlgBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYDLGFRAME);
  218. #ifdef VERBOSE
  219.     printf("xScreen %d, yScreen %d, titleBar %d, xBorder %d, yBorder %d,
  220.            xSizeBorder %d, ySizeBorder %d, xDlgBorder %d, yDlgBorder %d\n",
  221.            xScreen, yScreen, titleBar, xBorder, yBorder, xSizeBorder,
  222.            ySizeBorder, xDlgBorder, yDlgBorder);
  223. #endif
  224.  
  225.     /* Get device characteristics from PM */
  226.     hScreenDC= DevOpenDC(tkHab, OD_MEMORY, (PSZ)"*", 0, (PDEVOPENDATA)&doStruc,
  227.                          NULLHANDLE);
  228.     lStart= CAPS_FAMILY; lCount= CAPS_DEVICE_POLYSET_POINTS+1;
  229.     rc= DevQueryCaps (hScreenDC, lStart, lCount, aDevCaps);
  230. #ifdef IGNOREPMRES
  231.     s = getenv("TK_PM_PIXRES");
  232.     if (s) {
  233.         overrideResolution = atol(s);
  234.     }
  235. #endif
  236.     globalPS = GpiCreatePS(tkHab, hScreenDC, &sizel, PU_PELS | GPIA_ASSOC);
  237. #ifdef VERBOSE
  238.     printf("globalPS %x\n", globalPS);
  239.     printf("%d bitmap formats: ", aDevCaps[CAPS_BITMAP_FORMATS]);
  240.     if ((aBitmapFormats =
  241.          (PLONG)ckalloc(2*aDevCaps[CAPS_BITMAP_FORMATS]*sizeof(LONG)))!=NULL &&
  242.         GpiQueryDeviceBitmapFormats(globalPS, 2*aDevCaps[CAPS_BITMAP_FORMATS],
  243.                                     aBitmapFormats)) {
  244.         for (lCount=0; lCount < 2*aDevCaps[CAPS_BITMAP_FORMATS]; lCount++) {
  245.             printf("(%d,", aBitmapFormats[lCount]);
  246.             lCount++;
  247.             printf("%d) ", aBitmapFormats[lCount]);
  248.         }
  249.         ckfree((char *)aBitmapFormats);
  250.     }
  251.     printf("\n");
  252.     printf("  CAPS_FAMILY %x, CAPS_IO_CAPS %x, CAPS_TECHNOLOGY %x\n",
  253.            aDevCaps[CAPS_FAMILY], aDevCaps[CAPS_IO_CAPS],
  254.            aDevCaps[CAPS_TECHNOLOGY]);
  255.     printf("  CAPS_DRIVER_VERSION %x, CAPS_WIDTH %d, CAPS_HEIGHT %d\n",
  256.            aDevCaps[CAPS_DRIVER_VERSION], aDevCaps[CAPS_WIDTH],
  257.            aDevCaps[CAPS_HEIGHT]);
  258.     printf("  CAPS_WIDTH_IN_CHARS %d, CAPS_HEIGHT_IN_CHARS %d\n",
  259.            aDevCaps[CAPS_WIDTH_IN_CHARS], aDevCaps[CAPS_HEIGHT_IN_CHARS]);
  260.     printf("  CAPS_HORIZONTAL_RESOLUTION %d, CAPS_VERTICAL_RESOLUTION %d\n",
  261.            aDevCaps[CAPS_HORIZONTAL_RESOLUTION],
  262.            aDevCaps[CAPS_VERTICAL_RESOLUTION]);
  263.     printf("  => (hor) 1cm = %d pixels, 1in = %d pixels\n",
  264.            aDevCaps[CAPS_HORIZONTAL_RESOLUTION] / 100,
  265.            aDevCaps[CAPS_HORIZONTAL_RESOLUTION] / 39);
  266.     printf("  CAPS_CHAR_WIDTH %d, CAPS_CHAR_HEIGHT %d\n",
  267.            aDevCaps[CAPS_CHAR_WIDTH], aDevCaps[CAPS_CHAR_HEIGHT]);
  268.     printf("  CAPS_SMALL_CHAR_WIDTH %d, CAPS_SMALL_CHAR_HEIGHT %d\n",
  269.            aDevCaps[CAPS_SMALL_CHAR_WIDTH], aDevCaps[CAPS_SMALL_CHAR_HEIGHT]);
  270.     printf("  CAPS_COLORS %d, CAPS_COLOR_PLANES %d, CAPS_COLOR_BITCOUNT %d\n",
  271.            aDevCaps[CAPS_COLORS], aDevCaps[CAPS_COLOR_PLANES],
  272.            aDevCaps[CAPS_COLOR_BITCOUNT]);
  273.     printf("  CAPS_COLOR_TABLE_SUPPORT %x, CAPS_MOUSE_BUTTONS %d\n",
  274.            aDevCaps[CAPS_COLOR_TABLE_SUPPORT], aDevCaps[CAPS_MOUSE_BUTTONS]);
  275.     printf("  CAPS_FOREGROUND_MIX_SUPPORT %x, CAPS_BACKGROUND_MIX_SUPPORT %x\n",
  276.            aDevCaps[CAPS_FOREGROUND_MIX_SUPPORT],
  277.        aDevCaps[CAPS_BACKGROUND_MIX_SUPPORT]);
  278.     printf("  CAPS_VIO_LOADABLE_FONTS %d, CAPS_WINDOW_BYTE_ALIGNMENT %x\n",
  279.            aDevCaps[CAPS_VIO_LOADABLE_FONTS],
  280.            aDevCaps[CAPS_WINDOW_BYTE_ALIGNMENT]);
  281.     printf("  CAPS_BITMAP_FORMATS %d, CAPS_RASTER_CAPS %x\n",
  282.            aDevCaps[CAPS_BITMAP_FORMATS], aDevCaps[CAPS_RASTER_CAPS]);
  283.     printf("  CAPS_MARKER_WIDTH %d, CAPS_MARKER_HEIGHT %d\n",
  284.            aDevCaps[CAPS_MARKER_WIDTH], aDevCaps[CAPS_MARKER_HEIGHT]);
  285.     printf("  CAPS_DEVICE_FONTS %d, CAPS_GRAPHICS_SUBSET %x\n",
  286.            aDevCaps[CAPS_DEVICE_FONTS], aDevCaps[CAPS_GRAPHICS_SUBSET]);
  287.     printf("  CAPS_GRAPHICS_VERSION %x, CAPS_GRAPHICS_VECTOR_SUBSET %x\n",
  288.            aDevCaps[CAPS_GRAPHICS_VERSION],
  289.        aDevCaps[CAPS_GRAPHICS_VECTOR_SUBSET]);
  290.     printf("  CAPS_DEVICE_WINDOWING %x, CAPS_ADDITIONAL_GRAPHICS %x\n",
  291.            aDevCaps[CAPS_DEVICE_WINDOWING], aDevCaps[CAPS_ADDITIONAL_GRAPHICS]);
  292.     printf("  (CAPS_ADDITIONAL_GRAPHICS & CAPS_COSMETIC_WIDELINE_SUPPORT %d)\n",
  293.            aDevCaps[CAPS_ADDITIONAL_GRAPHICS] & CAPS_COSMETIC_WIDELINE_SUPPORT);
  294.     printf("  CAPS_PHYS_COLORS %d, CAPS_COLOR_INDEX %d\n",
  295.            aDevCaps[CAPS_PHYS_COLORS], aDevCaps[CAPS_COLOR_INDEX]);
  296.     printf("  CAPS_GRAPHICS_CHAR_WIDTH %d, CAPS_GRAPHICS_CHAR_HEIGHT %d\n",
  297.            aDevCaps[CAPS_GRAPHICS_CHAR_WIDTH],
  298.            aDevCaps[CAPS_GRAPHICS_CHAR_HEIGHT]);
  299.     printf("  CAPS_HORIZONTAL_FONT_RES %d, CAPS_VERTICAL_FONT_RES %d\n",
  300.            aDevCaps[CAPS_HORIZONTAL_FONT_RES],
  301.            aDevCaps[CAPS_VERTICAL_FONT_RES]);
  302.     printf("  CAPS_DEVICE_FONT_SIM %x, CAPS_LINEWIDTH_THICK %d\n",
  303.            aDevCaps[CAPS_DEVICE_FONT_SIM], aDevCaps[CAPS_LINEWIDTH_THICK]);
  304.     printf("  CAPS_DEVICE_POLYSET_POINTS %x\n",
  305.            aDevCaps[CAPS_DEVICE_POLYSET_POINTS]);
  306. #endif
  307.  
  308.     if (globalPS == GPI_ERROR) {
  309. #ifdef VERBOSE
  310.         printf("globalPS ERROR %x\n", WinGetLastError(tkHab));
  311. #endif
  312.         return NULLHANDLE;
  313.     }
  314.     GpiSetCharMode(globalPS, CM_MODE2);
  315.     bmpInfo.cbFix = 16L;
  316.     bmpInfo.cx = xScreen;
  317.     bmpInfo.cy = yScreen;
  318.     bmpInfo.cPlanes = 1;
  319.     bmpInfo.cBitCount = aDevCaps[CAPS_COLOR_BITCOUNT];
  320.     globalBitmap = GpiCreateBitmap(globalPS, &bmpInfo, 0L, NULL, NULL);
  321. #ifdef VERBOSE
  322.     if (globalBitmap!=GPI_ERROR) {
  323.         printf("GpiCreateBitmap globalBitmap OK (%x)\n", globalBitmap);
  324.     } else {
  325.         printf("GpiCreateBitmap globalBitmap GPI_ERROR, error %x\n",
  326.                WinGetLastError(tkHab));
  327.     }
  328. #endif
  329.     rc = GpiSetBitmap(globalPS, globalBitmap);
  330. #ifdef VERBOSE
  331.     if (rc!=GPI_ALTERROR) {
  332.         printf("GpiSetBitmap globalBitmap OK\n");
  333.     } else {
  334.         printf("GpiSetBitmap globalBitmap GPI_ALTERROR, error %x\n",
  335.                WinGetLastError(tkHab));
  336.     }
  337. #endif
  338.     /* Determine color table if no palette support but color table support */
  339.     if (!(aDevCaps[CAPS_ADDITIONAL_GRAPHICS] & CAPS_PALETTE_MANAGER) &&
  340.         aDevCaps[CAPS_COLOR_TABLE_SUPPORT]) {
  341.         LONG aClrData[4];
  342.  
  343.         nextColor = 16;    /* Assume VGA color table */
  344.         rc = GpiQueryColorData(globalPS, 4, aClrData);
  345. #ifdef VERBOSE
  346.         if (rc!=TRUE) {
  347.             printf("GpiQueryColorData ERROR %x\n", WinGetLastError(tkHab));
  348.         } else {
  349.             printf("GpiQueryColorData: format %x, loind %d, hiind %d, options %x\n",
  350.                     aClrData[QCD_LCT_FORMAT], aClrData[QCD_LCT_LOINDEX],
  351.                     aClrData[QCD_LCT_HIINDEX], aClrData[QCD_LCT_OPTIONS]); 
  352.         }
  353. #endif
  354.         nextColor = aClrData[QCD_LCT_HIINDEX] + 1;
  355.     }
  356.  
  357.     return tkHab;
  358. }
  359.  
  360. /*
  361.  *----------------------------------------------------------------------
  362.  *
  363.  * TkOS2ExitPM --
  364.  *
  365.  *    Performs OS/2 Presentation Manager sign-off routines.
  366.  *
  367.  * Results:
  368.  *    None.
  369.  *
  370.  * Side effects:
  371.  *    Resets global variables tkHab and tkHmq.
  372.  *
  373.  *----------------------------------------------------------------------
  374.  */
  375.  
  376. void
  377. TkOS2ExitPM (appHab)
  378.     HAB appHab;
  379. {
  380.     GpiSetBitmap(globalPS, NULLHANDLE);
  381.     GpiDestroyPS(globalPS);
  382.     DevCloseDC(hScreenDC);
  383.     WinDestroyMsgQueue(tkHmq);
  384.     WinTerminate(tkHab);
  385.     tkHmq= tkHab= 0;
  386. }
  387.