home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / lib / PEX / ws / wsx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-01  |  7.3 KB  |  247 lines

  1. /* $XConsortium: wsx.c,v 5.3 91/07/01 16:23:46 hersh Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1989, 1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24.  
  25. ******************************************************************/
  26.  
  27. /* PEX/PHIGS workstation utility functions. */
  28.  
  29. #include "phg.h"
  30. #include "cp.h"
  31. #include "ws.h"
  32. #include "PEX.h"
  33. #include "PEXproto.h"
  34. #include "PEXmacs.h"
  35. #include "PEXfuncs.h"
  36. #include "phigspex.h"
  37.  
  38.  
  39. static void
  40. shutdown_tool( ws )
  41.     Ws    *ws;
  42. {
  43.     if ( ws->display ) {
  44.     if ( ws->drawable_id ) {
  45.         XDestroyWindow( ws->display, ws->drawable_id );
  46.         ws->drawable_id = 0;
  47.     }
  48.     }
  49. }
  50.  
  51. int
  52. phg_wsx_setup_tool( ws, conn_info, wst )
  53.     Ws            *ws;
  54.     Phg_args_conn_info    *conn_info;
  55.     Wst            *wst;
  56. {
  57.     int                status = 0;
  58.     XSizeHints            size_hints;
  59.     Wst_xwin_dt            *xdt = &wst->desc_tbl.xwin_dt;
  60.     Display            *display = ws->display;
  61.     XID                drawable_id;
  62.     XEvent            event;
  63.     Visual            best_visual;
  64.     XVisualInfo            *best_info, template;
  65.     int                nvisuals;
  66.     XSetWindowAttributes    attrs;
  67.     Pint            err;
  68.     Phg_pex_ext_info        pex_info;
  69.     Colormap            cmap;
  70.  
  71.     /* Create the window. */
  72.     if (!phg_wsx_find_best_visual(ws, wst, &best_visual, &cmap, &err)) {
  73.     ERR_BUF(ws->erh, err);
  74.     return status;
  75.     }
  76.     /* Need the depth of this visual */
  77.     template.visualid = best_visual.visualid;
  78.     best_info = XGetVisualInfo(display, (long)VisualIDMask, &template, &nvisuals);
  79.     attrs.border_pixel       = WhitePixel(display, DefaultScreen(display));
  80.     attrs.background_pixel = BlackPixel(display, DefaultScreen(display));
  81.     attrs.colormap = cmap;
  82.     /* TODO: background and border colour attributes. */
  83.  
  84.     if ( !(drawable_id = XCreateWindow(display,
  85.     RootWindow(display, DefaultScreen(display)),
  86.     xdt->tool.x, xdt->tool.y, xdt->tool.width, xdt->tool.height,
  87.     xdt->tool.border_width, best_info->depth,
  88.     InputOutput, best_info->visual,
  89.     CWColormap | CWBackPixel | CWBorderPixel, &attrs)) ) {
  90.     ERR_BUF(ws->erh,ERRN203);
  91.     return status;
  92.     }
  93.  
  94.     /* Finish the setup. */
  95.     size_hints.flags = USPosition | USSize;
  96.     size_hints.x = xdt->tool.x;
  97.     size_hints.y = xdt->tool.y;
  98.     size_hints.width = xdt->tool.width;
  99.     size_hints.height = xdt->tool.height;
  100.     XSetStandardProperties( display, drawable_id, xdt->tool.label,
  101.     xdt->tool.icon_label, None, NULL, 0, &size_hints );
  102.     XSelectInput( display, drawable_id, (long)ExposureMask );
  103.     XMapWindow( display, drawable_id );
  104.     XSync( display, False );
  105.     XWindowEvent( display, drawable_id, ExposureMask, &event );
  106.     XSelectInput( display, drawable_id, (long)0 );
  107.     ws->drawable_id = drawable_id;
  108.  
  109.     return 1;
  110. }
  111.  
  112.  
  113. static void
  114. shutdown_drawable( ws )
  115.     Ws        *ws;
  116. {
  117.     ws->drawable_id = 0;
  118. }
  119.  
  120.  
  121. void
  122. phg_wsx_release_window( ws )
  123.     Ws        *ws;
  124. {
  125.     if ( ws->type ) {
  126.     switch ( ws->type->base_type ) {
  127.         case WST_BASE_TYPE_X_TOOL:
  128.         shutdown_tool( ws );
  129.         break;
  130.         case WST_BASE_TYPE_X_DRAWABLE:
  131.         shutdown_drawable( ws );
  132.         break;
  133.     }
  134.     }
  135. }
  136.  
  137.  
  138. void
  139. phg_wsx_inq_text_extent( cph, args, ret, ws )
  140.     Cp_handle            cph;
  141.     Phg_args_q_text_extent    *args;
  142.     Phg_ret            *ret;
  143.     Ws                *ws;
  144. {
  145.     pexExtentInfo    *extents;
  146.     unsigned        size;
  147.     CARD32        *card32_p;
  148.     pexMonoEncoding    *encodings;
  149.  
  150.     /* Convert the string to MONO_ENCODING format then issue a PEX Query
  151.      * Text Extent request.  Assumes string is not NULL or zero-length.
  152.      */
  153.     /* TODO: Only works for one string and one monoencoding. */
  154.     ret->err = 0;
  155.     size = args->length - 1;    /* don't count the terminator */
  156.     size += (size % 4 == 0) ? 0 : 4 - (size % 4);    /* padding */
  157.     size += sizeof(pexMonoEncoding) + sizeof(CARD32);
  158.     if ( !PHG_SCRATCH_SPACE( &ws->scratch, size + sizeof(CARD32) ) ) {
  159.     ret->err = ERR900;
  160.     return;
  161.     }
  162.  
  163.     card32_p = (CARD32 *)ws->scratch.buf;
  164.     encodings = (pexMonoEncoding *)(card32_p + 1);
  165.     if ( phg_utx_encode_text( args->length, args->str, encodings ) ) {
  166.     *card32_p = 1;    /* only one list of mono-encodings supported. */
  167.     if ( !PEXQueryTextExtents( ws->display, ws->rid,
  168.         (pexTableIndex)args->font,
  169.         (CARD16)PEX_CONV_FROM_Ptxpath(args->path),
  170.         (PEXFLOAT)args->char_expan, (PEXFLOAT)args->spacing,
  171.         (PEXFLOAT)args->height,
  172.         (pexTextHAlignment)PEX_CONV_FROM_Ptxhor(args->hor),
  173.         (pexTextVAlignment)PEX_CONV_FROM_Ptxver(args->ver),
  174.         (CARD32)1, size, (char *)ws->scratch.buf, &extents ) ) {
  175.         ret->err = ERR900; /* TODO: Use phg_pex_errno. */
  176.     } else {
  177.         PEX_CONV_TO_Ppoint( &extents->lowerLeft,
  178.         &ret->data.text_extent.rect.p )
  179.         PEX_CONV_TO_Ppoint( &extents->upperRight,
  180.         &ret->data.text_extent.rect.q )
  181.         PEX_CONV_TO_Ppoint( &extents->concatpoint,
  182.         &ret->data.text_extent.offset )
  183.     }
  184.     }
  185. }
  186.  
  187.  
  188. void
  189. phg_wsx_inq_colr_map_meth_st( cph, args, ret, ws )
  190.     Cp_handle            cph;
  191.     Phg_args_q_colr_map_meth_st    *args;
  192.     Phg_ret            *ret;
  193.     Ws                *ws;
  194. {
  195.     Display        *dpy = ws->display;
  196.     XWindowAttributes    attrs;
  197.     unsigned long    pixel;
  198.     unsigned int    nplanes, ncolors;
  199.     unsigned long    plane_masks[24]; /* max for 24 bit colors */
  200.     register int i;
  201.  
  202.     ret->err = 0;
  203.  
  204.     switch( args->map_method ) {
  205.     /* TRUE and PSEUDO_N are done in c_binding */
  206.     case PCOLR_MAP_PSEUDO:
  207.         /* what a hack! 
  208.          * phigs assumes the application has complete 
  209.          * control of the colormap. in X, colormaps are
  210.          * shared. So, the best we can do is to find
  211.          * out the max number of entries which can be
  212.          * allocated at this time and return that as
  213.          * the number of available entries.  There is
  214.          * no guarantee that these entries will still
  215.          * be free by the time the application tries
  216.          * to do something with them
  217.          * Colormap entries must be allocated in one
  218.          * chunk, so start large and work down.
  219.          */
  220.  
  221.         XGetWindowAttributes(dpy, ws->drawable_id, &attrs);
  222.  
  223.         ncolors = ws->type->desc_tbl.phigs_dt.out_dt.num_colours;
  224.         /* calculate the max number of planes available */
  225.         for (nplanes=0, i=1; i<ncolors; i=i<<1, nplanes++);
  226.  
  227.         /* assume PseudoColor Visual and ColorSpace mapping */
  228.         while (nplanes && !XAllocColorCells(dpy, attrs.colormap, 
  229.                        True, plane_masks, nplanes, &pixel, 1)) {
  230.         nplanes--;
  231.         }
  232.         if (!nplanes)
  233.             ret->data.idata = 0;
  234.         else {
  235.             ret->data.idata = 1<<nplanes;
  236.         for (i=1; i<nplanes; i++)
  237.             plane_masks[0] |= plane_masks[i];
  238.         XFreeColors(dpy, attrs.colormap, &pixel, 1, plane_masks[0]);
  239.         }
  240.     break;
  241.     case PCOLR_MAP_TRUE:
  242.     case PCOLR_MAP_PSEUDO_N:
  243.         ret->data.idata = 0;
  244.     break;
  245.     }
  246. }
  247.