home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xtici.zip / xtici / show.c < prev    next >
C/C++ Source or Header  |  1991-08-28  |  14KB  |  527 lines

  1. /*
  2.  * Code and supporting documentation (c) Copyright 1990 1991 Tektronix, Inc.
  3.  *     All Rights Reserved
  4.  * 
  5.  * This file is a component of an X Window System client which uses the Xcms 
  6.  * Color Management System.  TekColor is a trademark of Tektronix, Inc.  The
  7.  * TekColor Editor is the subject of U.S. and foreign patents pending.  The
  8.  * term "TekHVC" designates a particular color space that is the subject of
  9.  * U.S. Patent No. 4,985,853 (equivalent foreign patents pending).
  10.  * Permission is hereby granted to use, copy, modify, sell, and otherwise
  11.  * distribute this software and its documentation for the X Window System
  12.  * environment, for any purpose and without fee, provided that:
  13.  * 
  14.  * 1.    The code and documentation are only used to implement a 
  15.  *      TekColor Editor in an X Window System environment; and
  16.  * 2.    This copyright and permission notice is reproduced in all copies
  17.  *     of the code and in supporting documentation.
  18.  * 
  19.  * Permission is granted to modify this code as required to allow it to
  20.  * be compiled on any host computer, provided that the functionality of
  21.  * the TekColor Editor is not modified in any way.  A description of any 
  22.  * modifications must be sent to Tektronix, Inc.  Contact 
  23.  * Tektronix Inc., P.O. Box 1000, Mail Station 60-850, 
  24.  * Network Displays Division Engineering, Wilsonville, OR 97070.
  25.  *
  26.  * Tektronix makes no representation about the suitability of this software
  27.  * for any purpose.  It is provided "as is" and with all faults.
  28.  * 
  29.  * TEKTRONIX DISCLAIMS ALL WARRANTIES APPLICABLE TO THIS SOFTWARE,
  30.  * INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  31.  * PARTICULAR PURPOSE.  IN NO EVENT SHALL TEKTRONIX BE LIABLE FOR ANY
  32.  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  33.  * RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN ACTION OF
  34.  * CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  35.  * CONNECTION WITH THE USE OR THE PERFORMANCE OF THIS SOFTWARE.
  36.  * 
  37.  *    NAME
  38.  *        show - create and manage coordinate display widgets
  39.  *
  40.  *    DESCRIPTION
  41.  *        Create and manage all coordinate displays.
  42.  *        Respond to user inputs.
  43.  */
  44. #ifndef LINT
  45. static char *copy_notice = "Copyright 1991 Tektronix, Inc.";
  46. #ifdef RCS_ID
  47. static char *rcsid=  "$Header: show.c,v 1.3 91/08/28 10:03:42 adamsc Exp $";
  48. #endif /* RCS_ID */
  49. #endif /* LINT */
  50.  
  51. /*
  52.  *      EXTERNAL INCLUDES
  53.  */
  54.  
  55.  
  56. /*
  57.  *      INTERNAL INCLUDES
  58.  */
  59.  
  60. #include "xtici.h"
  61.  
  62. #include <X11/Xaw/Form.h>
  63. #include "widgets/TriText.h"
  64. #include "patch.h"
  65.  
  66. /*
  67.  *      EXTERNS
  68.  */
  69. extern void AllowFloat();
  70. extern void AllowInt();
  71.  
  72. /*
  73.  *      GLOBALS
  74.  */
  75. #ifdef DEC
  76. #  define GLOBAL    global
  77. #else
  78. #  define GLOBAL
  79. #endif
  80.  
  81. GLOBAL int showMask = (1 << HvcWidget);
  82.  
  83. /*
  84.  *      LOCAL DEFINES
  85.  */
  86.  
  87. #define ConstrainRgb(c) {if ((c) > 255) (c) = 255;}
  88.  
  89. #define MaxShort 0xffff
  90.  
  91. /*
  92.  *      LOCAL TYPEDEFS
  93.  */
  94.  
  95.  
  96. /*
  97.  *      LOCALS VARIABLES
  98.  */
  99. #ifdef DEBUG
  100. #  define STATIC
  101. #else
  102. #  define STATIC        static
  103. #endif
  104.  
  105. STATIC int lastFocus = GestureNone;
  106.  
  107. /*
  108.  * All are initially attached to upper right of form simply to insure
  109.  * proper sizes are created.  These default attachments are overridden 
  110.  * when managed.
  111.  */
  112. STATIC Arg hvcArgs[] = {
  113.     {XtNlabelList, NULL},
  114.     {XtNfloatList, NULL},
  115.     {XtNformat, (XtArgVal)"%.1f"},
  116.     {XtNlength, 5},
  117. };
  118. STATIC Arg uvyArgs[] = {
  119.     {XtNlabelList, NULL},
  120.     {XtNfromHoriz, NULL},
  121.     {XtNformat, (XtArgVal)"%.3f"},
  122.     {XtNfloatList, NULL},
  123.     {XtNlength, 5},
  124. };
  125. STATIC Arg rgbArgs[] = {
  126.     {XtNlabelList, NULL},
  127.     {XtNfromHoriz, NULL},
  128.     {XtNformat, (XtArgVal)"%.0f"},    /* TODO: display from 0 to 1.0 ? */
  129.     {XtNfloatList, NULL},
  130.     {XtNlength, 3},
  131. };
  132.  
  133.  
  134. /************************************************************************
  135.  *                                    *
  136.  *            PRIVATE ROUTINES                *
  137.  *                                    *
  138.  ************************************************************************/
  139. /* ARGSUSED */
  140. STATIC void setShowGesture(wid, psource, pcall)
  141. Widget wid;
  142. XtPointer psource;
  143. XtPointer pcall;
  144. {
  145.     Widget source = (Widget)psource;
  146.     TriCallbackStruct *call = (TriCallbackStruct *)pcall;
  147.     if (source == hvcShow) {
  148.         switch (call->index) {
  149.             case 0:
  150.             gesture = GestureHue;
  151.             break;
  152.             case 1:
  153.         gesture = GestureValue;
  154.                 break;
  155.             case 2:
  156.         gesture = GestureChroma;
  157.                 break;
  158.             default:
  159.         gesture = GestureNone;
  160.                 return;
  161.         }
  162.     } else if (source == uvyShow) {
  163.         switch (call->index) {
  164.             case 0:
  165.             gesture = GestureUprime;
  166.             break;
  167.             case 1:
  168.             gesture = GestureVprime;
  169.                 break;
  170.             case 2:
  171.             gesture = GestureCapY;
  172.                 break;
  173.             default:
  174.             gesture = GestureNone;
  175.                 return;
  176.         }
  177.     } else if (source == rgbShow) {
  178.         switch (call->index) {
  179.             case 0:
  180.             gesture = GestureRed;
  181.             break;
  182.             case 1:
  183.             gesture = GestureGreen;
  184.                 break;
  185.             case 2:
  186.             gesture = GestureBlue;
  187.                 break;
  188.             default:
  189.             gesture = GestureNone;
  190.                 return;
  191.         }
  192.     } else {
  193.         gesture = GestureNone;
  194.     }
  195. }
  196.  
  197. /*
  198.  * This routine is not needed for the local client, because its text
  199.  * widgets detect a premature change in focus.
  200.  */
  201.  
  202. /* ARGSUSED */
  203. STATIC void showFocus(w, pgestureCode, pcall)
  204. Widget w;
  205. XtPointer pgestureCode;
  206. XtPointer pcall;
  207. {
  208.     int gestureCode = (int)pgestureCode;
  209.     TriCallbackStruct *call = (TriCallbackStruct *)pcall;
  210.     int code = gestureCode + call->index;
  211.  
  212.     /*
  213.      * Detect when the focus is moved from another text widget with
  214.      * an active gesture.  However,
  215.      * focus is lost and gained when application looses/gains focus,
  216.      * so check if the gesture truly is a change (from another text widget)
  217.      * before reacting as if it is a new gesture.
  218.      *
  219.      * lastFocus tracks exactly which text widget has the focus.
  220.      * This allows detecting changes within the TriText widget.
  221.      * So use lastFocus, not gesture.
  222.      */
  223.     if ( (gesture > GestureMouse) && (lastFocus != code) )
  224.     TerminateGesture();
  225.  
  226.     lastFocus = code;
  227. }
  228.  
  229.  
  230. /************************************************************************
  231.  *                                    *
  232.  *            PUBLIC ROUTINES                    *
  233.  *                                    *
  234.  ************************************************************************/
  235.  
  236. STATIC String hvcLabels[] = {
  237.     "HUE:",
  238.     "VALUE:",
  239.     "CHROMA:"
  240. };
  241. STATIC String uvYLabels[] = {
  242.     "u':",
  243.     "v':",
  244.     "Y:"
  245. };
  246. STATIC String rgbLabels[] = {
  247.     "RED:",
  248.     "GREEN:",
  249.     "BLUE:"
  250. };
  251. /*
  252.  * Create these so that the tab ordering is natural.
  253.  * Displayed in order (patch) - HVC - u'v'Y - RGB - (form)
  254.  */
  255. Widget CreateShowWidgets(parent)
  256. Widget parent;
  257. {
  258.     Widget self = XtCreateWidget ("show", formWidgetClass, parent, NULL, 0);
  259.     XtTranslations mytrans;
  260.     STATIC char defaults[] = "#augment\n\
  261.            <Leave>:   leave()";
  262.  
  263.     /* 
  264.      * only one coordinate system is displayed initially, HVC.
  265.      */
  266.     XtSetArg(hvcArgs[0], XtNlabelList, hvcLabels);
  267.     XtSetArg(hvcArgs[1], XtNfloatList, ¤tHvc.spec.TekHVC);
  268.     hvcShow = XtCreateManagedWidget("hvc", triTextWidgetClass, self,
  269.                      hvcArgs, XtNumber(hvcArgs));
  270.     XtAddCallback(hvcShow, XtNactivateCallback, setShowGesture,
  271.           (XtPointer)hvcShow);
  272.     XtAddCallback(hvcShow, XtNfocusCallback, showFocus,
  273.           (XtPointer)GestureHue);
  274.     XtAddCallback(hvcShow, XtNmodifyVerifyCallback, AllowFloat,
  275.           (XtPointer)GestureHue);
  276.     mytrans = XtParseTranslationTable(defaults);
  277.     XtAugmentTranslations (hvcShow, mytrans);
  278.  
  279.     XtSetArg(uvyArgs[0], XtNlabelList, uvYLabels);
  280.     XtSetArg(uvyArgs[1], XtNfromHoriz, hvcShow);
  281.     /*********************************************************************
  282.      * Since this widget is only used when selected from the 
  283.      * Coordinates menu it is only managed when needed.
  284.      *********************************************************************/
  285.     uvyShow = XtCreateWidget("uvy", triTextWidgetClass, self,
  286.                 uvyArgs, XtNumber(uvyArgs));
  287.     XtAddCallback(uvyShow, XtNactivateCallback, setShowGesture,
  288.           (XtPointer)uvyShow);
  289.     XtAddCallback(uvyShow, XtNfocusCallback, showFocus,
  290.           (XtPointer)GestureUprime);
  291.     XtAddCallback(uvyShow, XtNmodifyVerifyCallback, AllowFloat,
  292.           (XtPointer)GestureUprime);
  293.     XtAugmentTranslations (uvyShow, mytrans);
  294.  
  295.     XtSetArg(rgbArgs[0], XtNlabelList, rgbLabels);
  296.     XtSetArg(rgbArgs[1], XtNfromHoriz, uvyShow);
  297.     /*********************************************************************
  298.      * Since this widget is only used when selected from the 
  299.      * Coordinates menu it is only managed when needed.
  300.      *********************************************************************/
  301.     rgbShow = XtCreateWidget("rgb", triTextWidgetClass, self,
  302.                 rgbArgs, XtNumber(rgbArgs));
  303.     XtAddCallback(rgbShow, XtNactivateCallback, setShowGesture,
  304.           (XtPointer)rgbShow);
  305.     XtAddCallback(rgbShow, XtNfocusCallback, showFocus,
  306.           (XtPointer)GestureRed);
  307.     XtAddCallback(rgbShow, XtNmodifyVerifyCallback, AllowInt,
  308.           (XtPointer)GestureRed);
  309.     XtAugmentTranslations (rgbShow, mytrans);
  310.  
  311.     XtManageChild(self);
  312.  
  313.     return(self);
  314. }
  315.  
  316. /*
  317.  * Change which show widgets are displayed
  318.  */
  319. void ChangeShowed(which)
  320. int which;
  321. {
  322.     int flag;
  323.     Widget left, target, right;
  324.     Arg leftArgs[2], targetArgs[2];
  325.     Boolean show;    /* if true, manage; if false, unmanage */
  326.             /* set show to False to turn off and True to turn on */
  327.     switch (which) {
  328.     case RgbWidget:
  329.     target = rgbShow;
  330.     if (UvyShowing)
  331.         left = uvyShow;
  332.     else
  333.         left = NULL;
  334.     right = hvcShow;
  335.     flag = 1 << RgbWidget;
  336.     if (RgbShowing)
  337.         show = False;
  338.     else
  339.         show = True;
  340.     break;
  341.     case UvyWidget:
  342.     target = uvyShow;
  343.     left = NULL;
  344.     if (RgbShowing)
  345.         right = rgbShow;
  346.     else 
  347.         right = hvcShow;
  348.     flag = 1 << UvyWidget;
  349.     if (UvyShowing)
  350.         show = False;
  351.     else
  352.         show = True;
  353.     break;
  354.     }
  355.  
  356.     if (show) {
  357.     XtSetArg(targetArgs[0], XtNfromHoriz, left);
  358.     XtSetValues(target, targetArgs, 1);
  359.     XtSetArg(leftArgs[0], XtNfromHoriz, target);
  360.     XtSetValues(right, leftArgs, 1);
  361.     XtManageChild(target);
  362.     showMask |= flag;
  363.     } else {
  364.     XtSetArg(leftArgs[0], XtNfromHoriz, left);
  365.     XtSetValues(right, leftArgs, 1);
  366.     XtUnmanageChild(target);
  367.     showMask &= ~flag;
  368.     }
  369. }
  370.  
  371. void GetShowHvc(hvc)
  372. XcmsColor *hvc;
  373. {
  374.     XcmsFloat list[3];
  375.  
  376.     TriTextGetList((TriTextWidget)hvcShow, list);
  377.     hvc->spec.TekHVC.H = list[0];
  378.     hvc->spec.TekHVC.V = list[1];
  379.     hvc->spec.TekHVC.C = list[2];
  380.     hvc->format = XcmsTekHVCFormat;
  381. }
  382.  
  383. void GetShowRgb(rgb)
  384. XColor *rgb;
  385. {
  386.     XcmsFloat list[3];
  387.  
  388.     TriTextGetList((TriTextWidget)rgbShow, list);
  389.     rgb->red = (unsigned short)list[0] << 8;
  390.     rgb->green = (unsigned short)list[1] << 8;
  391.     rgb->blue = (unsigned short)list[2] << 8;
  392. }
  393.  
  394. void GetShowUvy(uvy)
  395. XcmsCIEuvY *uvy;
  396. {
  397.     XcmsFloat list[3];
  398.  
  399.     TriTextGetList((TriTextWidget)uvyShow, list);
  400.     uvy->u_prime = list[0];
  401.     uvy->v_prime = list[1];
  402.     uvy->Y = list[2];
  403. }
  404.  
  405. void NewRgb(which, newPiece, where)
  406. int which;    /* identifies component */
  407. unsigned short newPiece;
  408. int where;
  409. {
  410.     XcmsColor color;
  411.  
  412.     if (newPiece > MaxShort)
  413.     newPiece = MaxShort;
  414.  
  415.     switch (which) {
  416.     case RedChange:
  417.     if (newPiece == currentRgb.red)
  418.         return;
  419.     currentRgb.red = newPiece;
  420.     break;
  421.     case GreenChange:
  422.     if (newPiece == currentRgb.green)
  423.         return;
  424.     currentRgb.green = newPiece;
  425.     break;
  426.     case BlueChange:
  427.     if (newPiece == currentRgb.blue)
  428.         return;
  429.     currentRgb.blue = newPiece;
  430.     break;
  431.     }
  432.  
  433.     EditRgbCells(¤tRgb, 1);
  434.  
  435.     /* update the interface */
  436.     bcopy((char *)¤tMap[currentIndex],(char *)&color,sizeof(XcmsColor));
  437.  
  438.     /* don't know how a change in a RGB component affects HVC */
  439.     NewColor(&color, where, UnknownChange);
  440. }
  441.  
  442. void NewUvy(which, newPiece, where)
  443. int which;    /* identifies component */
  444. XcmsFloat newPiece;
  445. int where;
  446. {
  447.     XcmsColor uvy;
  448.     XcmsColor color;
  449.  
  450.     switch (which) {
  451.     case UprimeChange:
  452.     if (newPiece == currentUvy.u_prime)
  453.         return;
  454.     currentUvy.u_prime = newPiece;
  455.     break;
  456.     case VprimeChange:
  457.     if (newPiece == currentUvy.v_prime)
  458.         return;
  459.     currentUvy.v_prime = newPiece;
  460.     break;
  461.     case CapYChange:
  462.     if (newPiece == currentUvy.Y)
  463.         return;
  464.     currentUvy.Y = newPiece;
  465.     break;
  466.     }
  467.  
  468.     uvy.pixel = currentIndex;
  469.     uvy.format = XcmsCIEuvYFormat;
  470.     uvy.spec.CIEuvY = currentUvy;
  471.     EditUvyCells(&uvy, 1);
  472.  
  473.     /* update the interface */
  474.     bcopy((char *)¤tMap[currentIndex],(char *)&color,sizeof(XcmsColor));
  475.     /* don't know how a change in a uvY component affects HVC */
  476.     NewColor(&color, where, UnknownChange);
  477. }
  478.  
  479. /* Set displayed coordinates to current values */
  480. void UpdateShowWidget(which)
  481. int which;
  482. {
  483.     Widget w;
  484.     XcmsFloat showFloat[3], *XcmsFloatp;
  485.     XcmsColor uvy;
  486.  
  487.     switch (which) {
  488.     case HvcWidget:
  489.     w = hvcShow;
  490.     XcmsFloatp = (XcmsFloat *)¤tHvc.spec.TekHVC;
  491.     break;
  492.     case RgbWidget:
  493.     w = rgbShow;
  494.     currentRgb.pixel = currentIndex;
  495.     XQueryColor(dpy, cmap, ¤tRgb);
  496.     /* scale to 0-255; unsigned shift should not propogate sign bit */
  497.     showFloat[0] = (XcmsFloat)(currentRgb.red >> 8);
  498.     showFloat[1] = (XcmsFloat)(currentRgb.green >> 8);
  499.     showFloat[2] = (XcmsFloat)(currentRgb.blue >> 8);
  500.     XcmsFloatp = showFloat;
  501.     break;
  502.     case UvyWidget:
  503.     w = uvyShow;
  504.     uvy.pixel = currentIndex;
  505.     uvy.format = XcmsCIEuvYFormat;
  506.     if (XcmsQueryColor(dpy, cmap, &uvy, XcmsCIEuvYFormat) == XcmsFailure) {
  507. #ifdef XDEBUG
  508.         fprintf (stderr, "Warning: XcmsQueryColor failed in UpdateShowWidget\n");
  509. #endif
  510.         break;
  511.     }
  512.     currentUvy = uvy.spec.CIEuvY;
  513.     showFloat[0] = currentUvy.u_prime;
  514.     showFloat[1] = currentUvy.v_prime;
  515.     showFloat[2] = currentUvy.Y;
  516.     XcmsFloatp = showFloat;
  517.     break;
  518.     default:
  519.     return;
  520.     }
  521.  
  522.     /* HACK: mark that program is changing text */
  523.     textProgramInput++;
  524.     TriTextSetList((TriTextWidget)w, XcmsFloatp);
  525.     --textProgramInput;
  526. }
  527.