home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / vibrant / palette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-05  |  8.6 KB  |  358 lines  |  [TEXT/R*ch]

  1. /*   palette.c
  2. * ===========================================================================
  3. *
  4. *                            PUBLIC DOMAIN NOTICE
  5. *            National Center for Biotechnology Information (NCBI)
  6. *
  7. *  This software/database is a "United States Government Work" under the
  8. *  terms of the United States Copyright Act.  It was written as part of
  9. *  the author's official duties as a United States Government employee and
  10. *  thus cannot be copyrighted.  This software/database is freely available
  11. *  to the public for use. The National Library of Medicine and the U.S.
  12. *  Government do not place any restriction on its use or reproduction.
  13. *  We would, however, appreciate having the NCBI and the author cited in
  14. *  any work or product based on this material
  15. *
  16. *  Although all reasonable efforts have been taken to ensure the accuracy
  17. *  and reliability of the software and data, the NLM and the U.S.
  18. *  Government do not and cannot warrant the performance or results that
  19. *  may be obtained by using this software or data. The NLM and the U.S.
  20. *  Government disclaim all warranties, express or implied, including
  21. *  warranties of performance, merchantability or fitness for any particular
  22. *  purpose.
  23. *
  24. * ===========================================================================
  25. *
  26. * File Name:  palette.c
  27. *
  28. * Author:  Jonathan Kans
  29. *
  30. * Version Creation Date:   1/1/91
  31. *
  32. * $Revision: 2.2 $
  33. *
  34. * File Description:   Active graphics objects
  35. *
  36. * Modifications:  
  37. * --------------------------------------------------------------------------
  38. * Date     Name        Description of modification
  39. * -------  ----------  -----------------------------------------------------
  40. *
  41. *
  42. * ==========================================================================
  43. */
  44.  
  45. #include "panels.h"
  46.  
  47. typedef  struct  paletteData {
  48.   Int2         drawCount;
  49.   Handle       drawHandles;
  50.   Int2         leftMargin;
  51.   Int2         rightMargin;
  52.   Int2         width;
  53.   PaletteProc  action;
  54. } PaletteData;
  55.  
  56. typedef  struct  drawRec {
  57.   PaintProc    draw;
  58.   PaletteProc  click;
  59.   RecT         rect;
  60.   Int2         value;
  61. } DrawRec, PNTR DrawPtr;
  62.  
  63. static void DrawPalette (PaneL p)
  64.  
  65. {
  66.   DrawPtr      dptr;
  67.   PaintProc    drw;
  68.   Handle       hdld;
  69.   Int2         i;
  70.   Int2         numd;
  71.   Int2         off;
  72.   PaletteData  pdata;
  73.   RecT         r;
  74.   SlatE        s;
  75.   RecT         sr;
  76.  
  77.   s = (SlatE) Parent (p);
  78.   GetPanelExtra (p, &pdata);
  79.   numd = pdata.drawCount;
  80.   hdld = pdata.drawHandles;
  81.   ObjectRect (s, &sr);
  82.   InsetRect (&sr, 4, 4);
  83.   GetOffset (p, NULL, &off);
  84.   if (hdld != NULL) {
  85.     dptr = (DrawPtr) HandLock (hdld);
  86.     i = 0;
  87.     while (i < numd) {
  88.       drw = dptr [i].draw;
  89.       if (drw != NULL) {
  90.         r = dptr [i].rect;
  91.         OffsetRect (&r, 0, -off);
  92.         if (RectInRect (&r, &sr) &&
  93.             (updateRgn == NULL || RectInRgn (&r, updateRgn))) {
  94.           drw ((PalettE) p, i + 1, &r);
  95.         }
  96.       }
  97.       i++;
  98.     }
  99.     HandUnlock (hdld);
  100.   }
  101. }
  102.  
  103. extern void AppendPalette (PalettE p, RectPtr rct,
  104.                            PaintProc draw, PaletteProc click,
  105.                            Int2 value)
  106.  
  107. {
  108.   Int2         chunk;
  109.   DrawPtr      dptr;
  110.   RecT         dr;
  111.   Handle       hdld;
  112.   Int2         numd;
  113.   Int2         off;
  114.   PaletteData  pdata;
  115.   RecT         r;
  116.   SlatE        s;
  117.   RecT         sr;
  118.   WindoW       tempPort;
  119.  
  120.   if (p != NULL && rct != NULL) {
  121.     tempPort = SavePort (p);
  122.     s = (SlatE) Parent (p);
  123.     GetPanelExtra ((PaneL) p, &pdata);
  124.     numd = pdata.drawCount;
  125.     hdld = pdata.drawHandles;
  126.     chunk = 128;
  127.     if (hdld != NULL) {
  128.       if (numd % chunk == 0) {
  129.         hdld = HandMore (hdld, sizeof (DrawRec) *
  130.                          (numd / chunk + 1) * chunk);
  131.       }
  132.     } else {
  133.       hdld = HandNew (sizeof (DrawRec) * chunk);
  134.     }
  135.     numd++;
  136.     pdata.drawHandles = hdld;
  137.     pdata.drawCount = numd;
  138.     SetPanelExtra ((PaneL) p, &pdata);
  139.     dptr = (DrawPtr) HandLock (hdld);
  140.     dptr [numd - 1].draw = draw;
  141.     dptr [numd - 1].click = click;
  142.     dptr [numd - 1].rect = *rct;
  143.     dptr [numd - 1].value = value;
  144.     HandUnlock (hdld);
  145.     r = *rct;
  146.     RegisterRect ((PaneL) p, &r);
  147.     if (draw != NULL && Enabled (p) && AllParentsEnabled (p) &&
  148.         Visible (p) && AllParentsVisible (p)) {
  149.       ObjectRect (s, &sr);
  150.       InsetRect (&sr, 4, 4);
  151.       GetOffset (p, NULL, &off);
  152.       OffsetRect (&r, 0, -off);
  153.       SectRect (&r, &sr, &dr);
  154.       if (RectInRect (&dr, &sr)) {
  155.         Select (p);
  156.         InvalRect (&dr);
  157.       }
  158.     }
  159.     RestorePort (tempPort);
  160.   }
  161. }
  162.  
  163. extern void SetPaletteValue (PalettE p, Int2 num, Int2 value)
  164.  
  165. {
  166.   DrawPtr      dptr;
  167.   Handle       hdld;
  168.   Int2         numd;
  169.   Int2         off;
  170.   PaletteData  pdata;
  171.   RecT         r;
  172.   SlatE        s;
  173.   RecT         sr;
  174.   WindoW       tempPort;
  175.  
  176.   if (p != NULL && num > 0) {
  177.     tempPort = SavePort (p);
  178.     num--;
  179.     s = (SlatE) Parent (p);
  180.     GetPanelExtra ((PaneL) p, &pdata);
  181.     ObjectRect (s, &sr);
  182.     InsetRect (&sr, 4, 4);
  183.     GetOffset (p, NULL, &off);
  184.     numd = pdata.drawCount;
  185.     hdld = pdata.drawHandles;
  186.     if (hdld != NULL && num < numd) {
  187.       dptr = (DrawPtr) HandLock (hdld);
  188.       if (dptr [num].value != value) {
  189.         dptr [num].value = value;
  190.         r = dptr [num].rect;
  191.         OffsetRect (&r, 0, -off);
  192.         if (dptr [num].draw != NULL && Enabled (p) && AllParentsEnabled (p) &&
  193.             Visible (p) && AllParentsVisible (p) &&
  194.             RectInRect (&r, &sr)) {
  195.           Select (p);
  196.           InvalRect (&r);
  197.         }
  198.       }
  199.       HandUnlock (hdld);
  200.     }
  201.     RestorePort (tempPort);
  202.   }
  203. }
  204.  
  205. extern Int2 GetPaletteValue (PalettE p, Int2 num)
  206.  
  207. {
  208.   DrawPtr      dptr;
  209.   Handle       hdld;
  210.   Int2         numd;
  211.   PaletteData  pdata;
  212.   Int2         rsult;
  213.  
  214.   rsult = 0;
  215.   if (p != NULL && num > 0) {
  216.     num--;
  217.     GetPanelExtra ((PaneL) p, &pdata);
  218.     numd = pdata.drawCount;
  219.     hdld = pdata.drawHandles;
  220.     if (hdld != NULL && num < numd) {
  221.       dptr = (DrawPtr) HandLock (hdld);
  222.       rsult = dptr [num].value;
  223.       HandUnlock (hdld);
  224.     }
  225.   }
  226.   return rsult;
  227. }
  228.  
  229. extern Int2 PaletteNumItems (PalettE p)
  230.  
  231. {
  232.   PaletteData  pdata;
  233.   Int2         rsult;
  234.  
  235.   rsult = 0;
  236.   if (p != NULL) {
  237.     GetPanelExtra ((PaneL) p, &pdata);
  238.     rsult = pdata.drawCount;
  239.   }
  240.   return rsult;
  241. }
  242.  
  243. static void PaletteClick (PaneL p, PoinT pt)
  244.  
  245. {
  246.   PaletteProc  actn;
  247.   DrawPtr      dptr;
  248.   Boolean      goOn;
  249.   Handle       hdld;
  250.   Int2         i;
  251.   Int2         numd;
  252.   Int2         off;
  253.   PaletteData  pdata;
  254.   RecT         r;
  255.   SlatE        s;
  256.   RecT         sr;
  257.  
  258.   if (p != NULL) {
  259.     actn = NULL;
  260.     s = (SlatE) Parent (p);
  261.     GetPanelExtra (p, &pdata);
  262.     ObjectRect (s, &sr);
  263.     InsetRect (&sr, 4, 4);
  264.     GetOffset (p, NULL, &off);
  265.     numd = pdata.drawCount;
  266.     hdld = pdata.drawHandles;
  267.     if (hdld != NULL) {
  268.       dptr = (DrawPtr) HandLock (hdld);
  269.       i = 0;
  270.       goOn = TRUE;
  271.       while (i < numd && goOn) {
  272.         actn = dptr [i].click;
  273.         r = dptr [i].rect;
  274.         OffsetRect (&r, 0, -off);
  275.         if (dptr [i].draw != NULL) {
  276.           if (RectInRect (&r, &sr) && PtInRect (pt, &r)) {
  277.             goOn = FALSE;
  278.           } else {
  279.             i++;
  280.           }
  281.         } else {
  282.           if (PtInRect (pt, &r) && PtInRect (pt, &sr)) {
  283.             goOn = FALSE;
  284.           } else {
  285.             i++;
  286.           }
  287.         }
  288.       }
  289.       HandUnlock (hdld);
  290.       if (i < numd) {
  291.         if (actn != NULL) {
  292.           actn ((PalettE) p, i + 1);
  293.         }
  294.         actn = pdata.action;
  295.         if (actn != NULL) {
  296.           actn ((PalettE) p, i + 1);
  297.         }
  298.       }
  299.     }
  300.   }
  301. }
  302.  
  303. static void NewPalette (PalettE p, Int2 minwid, PaletteProc actn)
  304.  
  305. {
  306.   PoinT        npt;
  307.   PaletteData  pdata;
  308.   RecT         r;
  309.   SlatE        s;
  310.  
  311.   SelectFont (systemFont);
  312.   s = (SlatE) Parent (p);
  313.   ObjectRect (s, &r);
  314.   InsetRect (&r, 4, 4);
  315.   GetNextPosition (p, &npt);
  316.   pdata.drawCount = 0;
  317.   pdata.drawHandles = NULL;
  318.   pdata.leftMargin = npt.x;
  319.   pdata.rightMargin = npt.x + minwid;
  320.   pdata.width = pdata.rightMargin - pdata.leftMargin;
  321.   pdata.action = actn;
  322.   SetPanelExtra ((PaneL) p, &pdata);
  323.   LoadRect (&r, npt.x, npt.y, pdata.rightMargin, npt.y);
  324.   RegisterRect ((PaneL) p, &r);
  325.   Break (p);
  326. }
  327.  
  328. static void ResetPalette (PaneL p)
  329.  
  330. {
  331.   PaletteData  pdata;
  332.  
  333.   GetPanelExtra (p, &pdata);
  334.   if (pdata.drawHandles != NULL) {
  335.     HandFree (pdata.drawHandles);
  336.   }
  337.   NewPalette ((PalettE) p, pdata.width, pdata.action);
  338. }
  339.  
  340. extern PalettE PalettePanel (SlatE s, Int2 pixwidth, PaletteProc actn)
  341.  
  342. {
  343.   PalettE  p;
  344.   WindoW   tempPort;
  345.  
  346.   p = NULL;
  347.   if (s != NULL) {
  348.     tempPort = SavePort (s);
  349.     p = (PalettE) CustomPanel (s,  DrawPalette, sizeof (PaletteData), ResetPalette);
  350.     if (p != NULL) {
  351.       SetPanelClick ((PaneL) p, PaletteClick, NULL, NULL, NULL);
  352.       NewPalette (p, pixwidth, actn);
  353.     }
  354.     RestorePort (tempPort);
  355.   }
  356.   return p;
  357. }
  358.