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 / gizmo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  10.6 KB  |  427 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1992, 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.  /* jot text editor source code. */
  39.  /*        Tom Davis         */
  40.  /*     February 7, 1992     */
  41.  
  42. #include "glut.h"
  43. #include "gizmo.h"
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "uicolor.h"
  47. #include "displaylist.h"
  48.  
  49. #define BUTTONUP        0
  50. #define BUTTONDOWN      1
  51. #define BUTTONCLICK     2
  52. #define BUTTONDOUBLE    3
  53.  
  54. void drawtb(muiObject *);
  55.  
  56. int     definescaledfont = 0;
  57. char    *tmpfilename;
  58.  
  59. /*   NEW BUTTON PROCEDURES    */
  60.  
  61. int strwidth(char *s)
  62. {
  63.     int len = 0;
  64.     while (*s) {
  65.     len += glutBitmapWidth(GLUT_BITMAP_HELVETICA_12, *s++);
  66.     }
  67.     return len;
  68. }
  69.  
  70. muiButton    *newbut(void)
  71. {
  72.     muiButton *b;
  73.     b = newbed();
  74.     b->type = BUTTON;
  75.     b->link = 0;
  76.     return b;
  77. }
  78.  
  79. muiButton    *newradiobut(void)
  80. {
  81.     muiButton *b;
  82.     b = newbed();
  83.     b->type = RADIOBUTTON;
  84.     b->link = 0;
  85.     return b;
  86. }
  87.  
  88. muiButton    *newbed(void)
  89. {
  90.     muiButton    *b;
  91.  
  92.     b = (muiButton *)malloc(sizeof(muiButton));
  93.     b->str[0] = 0;
  94.     b->type = BED;
  95.     return b;
  96. }
  97.  
  98. void muiLoadButton(muiObject *b, char *s)
  99. {
  100.     int temp;
  101.     muiButton *but = (muiButton *)b->object;
  102.  
  103.     strcpy(but->str, s);
  104.     switch (but->type) {
  105.     case PUSHBUTTON:
  106.         temp = b->xmin + strwidth(but->str) + 20;
  107.         if (temp > b->xmax)
  108.         b->xmax = temp;
  109.         break;
  110.     default:
  111.         break;
  112.     }
  113. }
  114.  
  115. void drawbuttonbackground(muiObject *b)
  116. {
  117.     int xmin = b->xmin, xmax = b->xmax, ymin = b->ymin, ymax = b->ymax;
  118.  
  119.     if (b->locate) {
  120.     if (b->select) {
  121.         drawedges(xmin++,xmax--,ymin++,ymax--,uiVyDkGray,uiWhite);
  122.         drawedges(xmin++,xmax--,ymin++,ymax--,uiDkGray,uiWhite);
  123.         drawedges(xmin++,xmax--,ymin++,ymax--,uiLtGray,uiBlack);
  124.         drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiLtGray);
  125.         drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiVyLtGray);
  126.     } else {
  127.         drawedges(xmin++,xmax--,ymin++,ymax--,uiDkGray,uiVyDkGray);
  128.         drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiDkGray);
  129.         drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiLtGray);
  130.         drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiLtGray);
  131.     }
  132.     } else {
  133.     drawedges(xmin++,xmax--,ymin++,ymax--,uiDkGray,uiVyDkGray);
  134.     drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiDkGray);
  135.     drawedges(xmin++,xmax--,ymin++,ymax--,uiVyLtGray,uiMmGray);
  136.     drawedges(xmin++,xmax--,ymin++,ymax--,uiVyLtGray,uiMmGray);
  137.     }
  138.     (b->locate)?uiVyLtGray():uiLtGray();
  139.     uirectfi(xmin,ymin,xmax+1,ymax+1);
  140. }
  141.  
  142. void drawpushbut(muiObject *b)
  143. {
  144.     muiButton *but = (muiButton *)b->object;
  145.  
  146.     drawbuttonbackground(b);
  147.     uiBlack();
  148.     uicmov2i(b->xmin+ (b->xmax - b->xmin - strwidth(but->str))/2 + 1, b->ymin+9);
  149.     uicharstr(but->str, UI_FONT_NORMAL);
  150. }
  151.  
  152. void drawbut(muiObject *b)
  153. {
  154.     switch(b->type) {
  155.     case BUTTON:
  156.     case PUSHBUTTON:    
  157.         drawpushbut(b);
  158.         break;
  159.     case BED:    
  160.         break;
  161.     default:
  162.         drawpushbut(b);
  163.         break;
  164.     }
  165. }
  166.  
  167. void muiChangeLabel(muiObject *obj, char *s)
  168. {
  169.     Label *l;
  170.     
  171.     if (obj->type != MUI_LABEL && obj->type != MUI_BOLDLABEL)
  172.     muiError("muiChangeLabel: not a label");
  173.     l = (Label *)obj->object;
  174.     strncpy(l->str, s, LABELSTRLEN);
  175.     l->str[LABELSTRLEN] = 0;
  176.     return;
  177. }
  178.  
  179. Label *newlabel(char *s)
  180. {
  181.     Label *l = (Label *)malloc(sizeof(Label));
  182.     strncpy(l->str, s, LABELSTRLEN);
  183.     l->str[LABELSTRLEN] = 0;
  184.     return l;
  185. }
  186.  
  187. muiTextBox *activetb = 0;
  188.  
  189. muiTextBox    *newtb(int xmin, int xmax)
  190. {
  191.     muiTextBox    *tb;
  192.     static int inited = 0;
  193.  
  194.     if (!inited) {
  195.     inited = 1;
  196.     }
  197.  
  198.     tb = (muiTextBox *)malloc(sizeof(muiTextBox));
  199.     tb->charWidth = (xmax - xmin - 9)/FONTWIDTH;
  200.     tb->tp1 = tb->tp2 = 0;
  201.     *tb->str = 0;
  202.     *tb->label = 0;
  203.     return tb;
  204. }
  205.  
  206. char *muiGetTBString(muiObject *obj)
  207. {
  208.     muiTextBox *tb = (muiTextBox *)obj->object;
  209.     return tb->str;
  210. }
  211.  
  212. void muiClearTBString(muiObject *obj)
  213. {
  214.     muiTextBox *tb = (muiTextBox *)obj->object;
  215.     *tb->str = 0;
  216.     tb->tp1 = tb->tp2 = 0;
  217. }
  218.  
  219. void loadtb(muiTextBox *tb, char *s)
  220. {
  221.     if (s == 0)
  222.     *tb->str = 0;
  223.     else
  224.     strcpy(tb->str, s);
  225.     tb->tp1 = tb->tp2 = (int) strlen(s);
  226. }
  227.  
  228. void muiSetTBString(muiObject *obj, char *s)
  229. {
  230.     muiTextBox *tb = (muiTextBox *)obj->object;
  231.     loadtb(tb, s);
  232. }
  233.  
  234. void backspacetb(muiTextBox *tb)
  235. {
  236.     char *s1, *s2, *stemp;
  237.  
  238.     if ((tb->tp1 == tb->tp2) && tb->tp1 > 0) {
  239.     s1 = &tb->str[tb->tp1-1];
  240.     while (*s1) {
  241.         *s1 = *(s1+1);
  242.         s1++;
  243.     }
  244.     tb->tp1--; tb->tp2--;
  245.     return;
  246.     }
  247.     s1 = &tb->str[tb->tp1];
  248.     s2 = &tb->str[tb->tp2];
  249.     if (s1 > s2) { stemp = s1; s1 = s2; s2 = stemp; }
  250.     stemp = s1;
  251.     while (*s2) {*s1++ = *s2++;}
  252.     *s1 = 0;
  253.     tb->tp1 = tb->tp2 = (int) (stemp - tb->str);
  254. }
  255.  
  256. void inserttbchar(muiTextBox *tb, char c)
  257. {
  258.     char    *s1, *s2;
  259.     int    len;
  260.  
  261.     if (tb->tp1 != tb->tp2) backspacetb(tb);
  262.     len = (int) strlen(tb->str);
  263.     if (len == TBSTRLEN) return;
  264.     s1 = &tb->str[tb->tp1];
  265.     s2 = &tb->str[len+1];
  266.     while (s2 != s1) {
  267.     *s2 = *(s2 - 1);
  268.     s2--;
  269.     }
  270.     *s1 = c;
  271.     tb->tp1++; tb->tp2++;
  272. }
  273.  
  274. int findtp(muiObject *obj, int x)
  275. {
  276.     muiTextBox *tb = (muiTextBox *)obj->object;
  277.     int tp, sl = (int) strlen(tb->str);
  278.  
  279.     tp = (x - obj->xmin)/FONTWIDTH;
  280.     if (tp < 0) tp = 0;
  281.     if (tp > tb->charWidth) tp = tb->charWidth;
  282.     if (tp > sl) tp = sl;
  283.     return tp;
  284. }
  285.  
  286. void drawtbcontents(muiObject *obj)
  287. {
  288.     int    xmin = obj->xmin, ymin = obj->ymin; 
  289.     int    ymax = ymin+TEXTBOXHEIGHT;
  290.     int    s1, s2;
  291.     char    str[160], *s;
  292.     muiTextBox    *tb = (muiTextBox *)obj->object;
  293.  
  294.     strncpy(str, tb->str, (unsigned int)tb->charWidth);
  295.     for (s = str; *s; s++)
  296.         if (*s < ' ' || *s >= '\177') *s = '*';
  297.     str[tb->charWidth] = 0;
  298.     s1 = tb->tp1; s2 = tb->tp2;
  299.     if (s1 > tb->charWidth) s1 = tb->charWidth;
  300.     if (s2 > tb->charWidth) s2 = tb->charWidth;
  301.  
  302.     /* selected area */
  303.     if (obj->active && (s1 != s2)) { 
  304.     uiVyLtGray();
  305.     uirectfi(xmin+6+FONTWIDTH*s1, ymin+7, xmin+6+FONTWIDTH*s2, ymax-6);
  306.     }
  307.  
  308.     /* contents of text box */
  309.     uiBlack();
  310.     uicmov2i(xmin+6, ymin+9);
  311.     uicharstr(str, UI_FONT_FIXED_PITCH);
  312.  
  313.     /* Blue bar */
  314.     if ((obj->active == 0) || (s1 != s2)) return;
  315.     uiBlue();
  316.     uimove2i(xmin+4+FONTWIDTH*s1, ymin+7); uidraw2i(xmin+4+FONTWIDTH*s1, ymax-6); uiendline();
  317.     uimove2i(xmin+5+FONTWIDTH*s1, ymin+7); uidraw2i(xmin+5+FONTWIDTH*s1, ymax-6); uiendline();
  318. }
  319.  
  320. void drawtb(muiObject *tb)
  321. {
  322.     int    xmin = tb->xmin, xmax = tb->xmax, ymin = tb->ymin;
  323.     int     ymax = ymin+TEXTBOXHEIGHT;
  324.  
  325.     drawedges(xmin++,xmax--,ymin++,ymax--,uiDkGray,uiWhite);
  326.     drawedges(xmin++,xmax--,ymin++,ymax--,uiBlack,uiVyLtGray);
  327.     drawedges(xmin++,xmax--,ymin++,ymax--,uiLtGray,uiDkGray);
  328.     drawedges(xmin++,xmax--,ymin++,ymax--,uiTerraCotta,uiTerraCotta);
  329.     uiTerraCotta();
  330.     uirectfi(xmin, ymin, xmax+1, ymax+1);
  331.  
  332.     drawtbcontents(tb);
  333. }
  334.  
  335. void muiActivateTB(muiObject *obj)
  336. {
  337.     muiCons *mcons;
  338.  
  339.     if ((mcons = muiGetListCons(muiGetUIList(obj))) == (muiCons *)0) return;   
  340.     muiSetActive(obj, 1);
  341.     while (mcons) {
  342.     if (mcons->object != obj && mcons->object->type == MUI_TEXTBOX)
  343.         muiSetActive(mcons->object, 0);
  344.     mcons = mcons->next;
  345.     }
  346. }
  347.  
  348. muiObject *muiGetActiveTB(void)
  349. {
  350.     muiCons *mcons;
  351.     int list = muiGetActiveUIList();
  352.  
  353.     if (list == 0) return 0;
  354.     if ((mcons = muiGetListCons(list)) == (muiCons *)0) return 0;
  355.     while (mcons) {
  356.     if (mcons->object->type == MUI_TEXTBOX && muiGetActive(mcons->object))
  357.         return mcons->object;
  358.     mcons = mcons->next;
  359.     }
  360.     return 0;
  361. }
  362.  
  363. enum muiReturnValue textboxhandler(muiObject *obj, int event, int value, int x, int y)
  364. {
  365.     int tp;
  366.     muiTextBox *tb = (muiTextBox *)obj->object;
  367.  
  368.     switch (event) {
  369.     case MUI_DEVICE_DOWN:
  370.         tp = findtp(obj, x);
  371.         tb->tp2 = tp;
  372.         break;
  373.     case MUI_DEVICE_UP:
  374.         break;
  375.     case MUI_DEVICE_PRESS:
  376.         muiActivateTB(obj);
  377.         tp = findtp(obj, x);
  378.         tb->tp1 = tb->tp2 = tp;
  379.         break;
  380.     case MUI_DEVICE_RELEASE:
  381.         break;
  382.     case MUI_DEVICE_CLICK:
  383.     case MUI_DEVICE_DOUBLE_CLICK:
  384.         muiActivateTB(obj);
  385.         tp = findtp(obj, x);
  386.         tb->tp1 = tb->tp2 = tp;
  387.         break;
  388.     case MUI_KEYSTROKE:
  389.         if (value == '\n' || value == '\r')    /* carriage return */
  390.         return MUI_TEXTBOX_RETURN;
  391.         if (value == '\025') { muiClearTBString(obj); }
  392.         else if (value == '\b') { backspacetb((muiTextBox *)obj->object); }
  393.         else inserttbchar((muiTextBox *)obj->object, (char)value);
  394.         break;
  395.     }
  396.     x = y;  /* for lint's sake */
  397.     return MUI_NO_ACTION;
  398. }
  399.  
  400. void    helpdrawlabel(char *s, int x, int y)
  401. {
  402.     uiBlack();
  403.         uicmov2i(x, y);
  404.         uicharstr(s, UI_FONT_NORMAL);
  405. }
  406.  
  407. void    helpdrawboldlabel(char *s, int x, int y)
  408. {
  409.     uiBlack();    /* XXX Hack! -- no bold font in GLUT */
  410.         uicmov2i(x, y);
  411.         uicharstr(s, UI_FONT_NORMAL);
  412.         uicmov2i(x+1, y);
  413.         uicharstr(s, UI_FONT_NORMAL);
  414. }
  415.  
  416. void drawlabel(muiObject *lab)
  417. {
  418.     Label *l = (Label *)lab->object;
  419.     helpdrawlabel(l->str, lab->xmin, lab->ymin);
  420. }
  421.  
  422. void drawboldlabel(muiObject *lab)
  423. {
  424.     Label *l = (Label *)lab->object;
  425.     helpdrawboldlabel(l->str, lab->xmin, lab->ymin);
  426. }
  427.