home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Libraries / mui / textlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  10.3 KB  |  432 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1990,1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. #include "glut.h"
  39. #include <ctype.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include "displaylist.h"
  43. #include "uicolor.h"
  44. #include "gizmo.h"
  45. #include "textlist.h"
  46. #include <string.h>
  47.  
  48. extern  short sharefont1;
  49.  
  50. extern    muiTextList *locatedtl;
  51.  
  52. int    gettlheightcount(int ymin, int ymax)
  53. {
  54.     if (ymin > ymax) {
  55.     int tmp = ymin;
  56.     ymin     = ymax;
  57.     ymax     = tmp;
  58.     }
  59.     return ((ymax-ymin+1-7)/TEXTHEIGHT);
  60. }
  61.  
  62. void    movetl(muiTextList *tl, int ymin, int ymax)
  63. {
  64.     tl->listheight = gettlheightcount(ymin, ymax);
  65. }
  66.  
  67. int    gettextlistheight(int count)
  68. {
  69.     return (count*TEXTHEIGHT+7);
  70. }
  71.  
  72. muiTextList    *newtl(muiObject *obj, int listheight)
  73. {
  74.     muiTextList    *tl;
  75.  
  76.     tl = (muiTextList *)malloc(sizeof(muiTextList));
  77.  
  78.     tl->strs = 0;
  79.     tl->top = 0;
  80.     tl->selecteditem = -1;
  81.     tl->locateditem = -1;
  82.     tl->listheight = listheight;
  83.  
  84.     movetl(tl, obj->ymin, obj->ymax = obj->ymin+gettextlistheight(listheight));
  85.  
  86.     return tl;
  87. }
  88.  
  89. void    resettl(muiTextList *tl)
  90. {
  91.     tl->selecteditem = -1;
  92.     tl->top = 0;
  93. }
  94.  
  95. void    drawtl(muiObject *obj)
  96. {
  97.     muiTextList *tl = (muiTextList *)obj->object;
  98.     int    xmin = obj->xmin, xmax = obj->xmax, 
  99.         ymin = obj->ymin, ymax = obj->ymax;
  100.     int    item = 0, ybot, xright;
  101.     char    **s = &tl->strs[tl->top];
  102.  
  103.     if (!muiGetVisible(obj))
  104.     return;
  105.  
  106.     drawsetup();
  107.  
  108.     drawedges(xmin++, xmax--, ymin++, ymax--, uiMmGray, uiWhite);
  109.     drawedges(xmin++, xmax--, ymin++, ymax--, uiDkGray, uiVyLtGray);
  110.     drawedges(xmin++, xmax--, ymin++, ymax--, uiVyDkGray, uiDkGray);
  111.  
  112.     drawedges(xmin++, xmax--, ymin++, ymax--, uiVyLtGray, uiMmGray);
  113.  
  114.     uiLtGray();
  115.         uirectfi(xmin, ymin, xmax, ymax);
  116.  
  117.     uiBlack();
  118.     uipushviewport();
  119.     uiviewport(xmin+3, ymin-1, xmax-xmin-6, ymax-ymin);
  120.     if (s) while (*s && item < tl->listheight) {
  121.     if (item == tl->selecteditem - tl->top) {
  122.         ybot = ymin + 1 + (tl->listheight - item - 1)*TEXTHEIGHT;
  123.         xright = xmin + 11 + FONTWIDTH*(int)strlen(*s);
  124.             uiWhite();
  125.             uirectfi(xmin+4, ybot, xright, ybot + TEXTHEIGHT - 3);
  126.     }
  127.     
  128.     /* locate highlight */
  129.     if (item == tl->locateditem - tl->top) {
  130.         ybot = ymin + 1 + (tl->listheight - item - 1)*TEXTHEIGHT;
  131.             xright = xmin + 11 + FONTWIDTH*(int)strlen(*s);
  132.             uiWhite();
  133.                 uirecti(xmin+3, ybot-1, xright+1, ybot + TEXTHEIGHT - 2);
  134.                 uirecti(xmin+4, ybot, xright, ybot + TEXTHEIGHT - 3);
  135.     }
  136.         uiBlack();
  137.         uicmov2i(xmin+8, ymin + 6 + (tl->listheight - item - 1)*TEXTHEIGHT);
  138.         uicharstr(*s, UI_FONT_NORMAL);
  139.     item++; s++;
  140.     }
  141.     uiBlack();
  142.     uipopviewport();
  143.     drawrestore();
  144. }
  145.  
  146. int    muiGetTLSelectedItem(muiObject *obj)
  147. {
  148.     muiTextList *tl = (muiTextList *)obj->object;
  149.     return tl->selecteditem;
  150. }
  151.  
  152. void    muiSetTLStrings(muiObject *obj, char **s)
  153. {
  154.     muiTextList *tl = (muiTextList *)obj->object;
  155.     tl->strs = s;
  156.  
  157.     tl->count = 0;
  158.     while(*s) {
  159.     tl->count++;
  160.     s++;
  161.     }
  162.     resettl(tl);
  163.     tl->selecteditem = 0;
  164. }
  165.  
  166. /* ARGSUSED3 */
  167. enum muiReturnValue tlhandler(muiObject *obj, int event, int value, int x, int y)
  168. {
  169.     int    i;
  170.     muiTextList *tl = (muiTextList *)obj->object;
  171.  
  172.     if (event == MUI_DEVICE_DOUBLE_CLICK) {
  173.     i = (y - obj->ymin - 3)/TEXTHEIGHT;
  174.     tl->selecteditem = (tl->listheight - i - 1) + tl->top;
  175.     return MUI_TEXTLIST_RETURN_CONFIRM;
  176.     }
  177.     if (event == MUI_DEVICE_RELEASE) {
  178.     i = (y - obj->ymin - 3)/TEXTHEIGHT;
  179.     tl->selecteditem = (tl->listheight - i - 1) + tl->top;
  180.     return MUI_TEXTLIST_RETURN;
  181.     }
  182.     if (event == MUI_DEVICE_PRESS || event == MUI_DEVICE_DOWN) {
  183.     i = (y - obj->ymin - 3)/TEXTHEIGHT;
  184.     tl->selecteditem = (tl->listheight - i - 1) + tl->top;
  185.     return MUI_NO_ACTION;
  186.     }
  187.     return MUI_NO_ACTION;
  188. }
  189.  
  190. void    muiSetTLTopInt(muiObject *obj, int top)
  191. {
  192.     muiTextList *tl = (muiTextList *)obj->object;
  193.     if (top < 0) top = 0;
  194.     if (top >= tl->count) top = tl->count - 1;
  195.     tl->top = top;
  196. }
  197.  
  198. void    muiSetTLTop(muiObject *obj, float p)
  199. {
  200.     muiTextList *tl = (muiTextList *)obj->object;
  201.     if (tl->count <= tl->listheight) { tl->top = 0; return; }
  202.     tl->top = (1.0-p)*(tl->count + 1 - tl->listheight);
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. #ifdef NOTDEF
  211.  
  212. static  short   intlboundaries(TextList *, long, long);
  213.  
  214. /* SELECT STATE FUNCTIONS */
  215.  
  216. void    unselecttl(TextList *tl)
  217. {
  218.     int oldselect = tl->selecteditem;
  219.  
  220.     if (tl->selecteditem == -1)
  221.     return;
  222.     tl->selecteditem = -1;
  223.     if (!tl->invisible)
  224.     if (oldselect != -1)
  225.         drawparttl(tl,oldselect);
  226. }
  227.  
  228. void    selecttlitem(TextList *tl, int item)
  229. {
  230.     int oldselect = tl->selecteditem;
  231.  
  232.     if (tl->selecteditem == item)
  233.     return;
  234.     tl->selecteditem = item;
  235.     if (!tl->invisible) {
  236.     if (tl->selecteditem != -1)
  237.         drawparttl(tl,tl->selecteditem);
  238.     if (oldselect != -1)
  239.         drawparttl(tl,oldselect);
  240.     }
  241. }
  242.  
  243. short    getselectedtlitem(TextList *tl)
  244. {
  245.     return tl->selecteditem;        /* returns -1 if none selected */
  246. }
  247.  
  248. void    highlighttl(TextList *tl, int item)
  249. {
  250.     int oldhighlight = tl->locateditem;
  251.  
  252.     if (tl->locateditem == item)
  253.     return;
  254.     tl->locateditem = item;
  255.     locatedtl = tl;
  256.     if (!tl->invisible) {
  257.     if (tl->locateditem != -1)
  258.         drawparttl(tl,tl->locateditem);
  259.     if (oldhighlight != -1)
  260.             drawparttl(tl,oldhighlight);
  261.     }
  262. }
  263.  
  264. void    unhighlighttl(TextList *tl)
  265. {
  266.     int oldhighlight = tl->locateditem;
  267.  
  268.     if (tl->locateditem == -1)
  269.     return;
  270.     tl->locateditem = -1;
  271.     if (locatedtl == tl)
  272.     locatedtl = 0;
  273.     if (!tl->invisible) {
  274.         if (oldhighlight != -1)
  275.             drawparttl(tl,oldhighlight);
  276.     }
  277. }
  278.  
  279. short    gethighlighttl(TextList *tl)
  280. {
  281.     return tl->locateditem;
  282. }
  283.  
  284. void    makevisibletl(TextList *tl)
  285. {
  286.     if (!tl->invisible)
  287.     return;
  288.     tl->invisible = 0;
  289. }
  290.  
  291. void    makeinvisibletl(TextList *tl)
  292. {
  293.     if (tl->invisible)
  294.     return;
  295.     tl->invisible = 1;
  296. }
  297.  
  298. /* whether or not the text list is showing */
  299. short   getvisibletl(TextList *tl)
  300. {
  301.     return 1-tl->invisible;
  302. }
  303.  
  304. /* whether or not the item is showing */
  305. short    getitemvisibletl(TextList *tl, int item)
  306. {
  307.     if ((item < tl->top) || (item >= (tl->top+tl->listheight)) || 
  308.     (item >= tl->count) || (item == -1))
  309.     return 0;
  310.     else
  311.     return 1;
  312. }
  313.  
  314. short    locatetl(TextList *tl, int x, int y)
  315. {
  316.     int highlight = gethighlighttl(tl);
  317.     int intextlist  = intl(tl,x,y);
  318.  
  319.     if ((intextlist == highlight) || !getvisibletl(tl))
  320.     return 0;
  321.     if (intextlist != -1) {
  322.     unlocateall();
  323.         highlighttl(tl,intextlist);
  324.         return 1;
  325.     } else {
  326.     unhighlighttl(tl);
  327.     return 0;
  328.     }
  329. }
  330.  
  331. char *selectedstring(TextList *tl)
  332. {
  333.     if (tl->selecteditem == -1) return 0;
  334.     return tl->strs[tl->selecteditem];
  335. }
  336.  
  337. void    drawtlnow(TextList *tl)
  338. {
  339.     uifrontbuffer(1);
  340.     drawtl(tl);
  341.     uifrontbuffer(0);
  342. }
  343.  
  344. void    drawparttl(TextList *tl,int item)
  345. {
  346.     int    xmin = tl->xmin+8, xmax = tl->xmax-8-SLIDERWIDTH;
  347.     int    ymin = 
  348.         tl->ymin+5+(tl->listheight - item + tl->top - 1)*TEXTHEIGHT;
  349.     int     ymax = ymin + TEXTHEIGHT - 3;
  350.     char        **s;
  351.  
  352.     if (!getvisibletl(tl) || !getitemvisibletl(tl,item))
  353.     return;
  354.  
  355.     s = &tl->strs[item];
  356.     drawsetup();
  357.  
  358.     uifrontbuffer(1);
  359.  
  360.     uiLtGray();
  361.         uirectfi(xmin-1, ymin-1, xmax+1, ymax+1);
  362.  
  363.     font(TEXTLISTFONT);
  364.     pushviewport();
  365.     scrmask(xmin-1, xmax+1, ymin-1, ymax+1);
  366.     if (s) {
  367.         xmax = xmin + 7 + strwidth(*s);
  368.         if (item == tl->selecteditem) {
  369.             uiWhite();
  370.                 uirectfi(xmin, ymin, xmax, ymax);
  371.         }
  372.         if (item == tl->locateditem) {
  373.             uiWhite();
  374.                 uirecti(xmin-1, ymin-1, xmax+1, ymax+1);
  375.                 uirecti(xmin, ymin, xmax, ymax);
  376.         }
  377.         uiBlack();
  378.             uicmov2i(xmin+4, ymin + 5);
  379.             uicharstr(*s);
  380.     }
  381.     popviewport();
  382.  
  383.     uifrontbuffer(0);
  384.  
  385.     drawrestore();
  386. }
  387.  
  388. void    settlstrslinkhandle(char **l, TextList *tl)
  389. {
  390.     int    vshalf, d;
  391.     float    p;
  392.  
  393.     settlstrings(tl, l);
  394.  
  395.     if (tl->count <= tl->listheight) {
  396.     vshalf = ((tl->listheight*TEXTHEIGHT - 36)/2) - 2;
  397.     d = 0;
  398.     p = 1.0;
  399.     disablevs(tl->vs);
  400.     } else {
  401.     enablevs(tl->vs);
  402.     p = getvsval(tl->vs);
  403.     vshalf = tl->listheight*(tl->listheight*TEXTHEIGHT-36)/(2*tl->count);
  404.     d = (tl->listheight*TEXTHEIGHT - 2*vshalf)/(tl->count - tl->listheight);
  405.     if (d == 0) d = 1;
  406.     }
  407.     setvsarrowdelta(tl->vs, d);
  408.     setvshalf(tl->vs, vshalf);
  409.     movevsval(tl->vs, p);
  410. }
  411.  
  412. void    adjustslider(TextList *tl, VSlider *sl)
  413. {
  414.     setvsval(sl, (float) (1.0 - (float) tl->top/ (float) tl->count));
  415. }
  416.  
  417. short    selectedtl(TextList *tl, int x, int y, int val)
  418. {
  419.     if (intl(tl, x, y) == -1) {
  420.     if (intlboundaries(tl, x, y)) {
  421.             tl->selecteditem = -1;
  422.         drawtlnow(tl);
  423.     }
  424.     } else {
  425.         if (val == UIBUTTONUP) return 0;
  426.         return handletl(tl, LEFTMOUSE, val);
  427.     }
  428.     return 0;
  429. }
  430.  
  431. #endif /* NOTDEF */
  432.