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

  1. /*
  2.  * Copyright (c) 1993-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 "gizmo.h"
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include "uicolor.h"
  44. #include "displaylist.h"
  45.  
  46. /*  M E N U   B A R   F U N C T I O N S  */
  47.  
  48. muiPulldown *newpd(void)
  49. {
  50.     muiPulldown *pd = (muiPulldown *)malloc(sizeof(muiPulldown));
  51.     pd->count = 0;
  52.     return pd;
  53. }
  54.  
  55. void muiAddPulldownEntry(muiObject *obj, char *title, int menu, int ishelp)
  56. {
  57.     muiPulldown *pd = (muiPulldown *)obj->object;
  58.     int space = obj->xmax - obj->xmin - 66;
  59.     int i, delta;
  60.  
  61.     if (ishelp) {
  62.     strcpy(pd->helpmenu.title, title);
  63.     pd->ishelp = 1;
  64.     pd->helpmenu.xoffset = obj->xmax - 58;
  65.     pd->helpmenu.menu = menu;
  66.     return;
  67.     }
  68.     if (pd->count == 29) muiError("muiAddPulldownEntry: more than 29 entries");
  69.     strcpy(pd->menus[pd->count].title, title);
  70.     pd->menus[pd->count].menu = menu;
  71.     pd->count++;
  72.     /* now recalculate spacings */
  73.     if (space > 50*pd->count) {
  74.     for (i = 0; i <= pd->count; i++)
  75.         pd->menus[i].xoffset = 8 + i*50;
  76.     } else {
  77.     delta = space/pd->count;
  78.     for (i = 0; i <= pd->count; i++)
  79.         pd->menus[i].xoffset = 8 + i*delta;
  80.     }
  81. }
  82.  
  83. int activemenu = -1;
  84. extern int menuinuse;
  85.  
  86. /* ARGSUSED2 */
  87. enum muiReturnValue  pdhandler(muiObject *obj, int event, int value, int x, int y)
  88. {
  89.     int i;
  90.     muiPulldown *pd = (muiPulldown *)obj->object;
  91.     
  92.     if (event == MUI_DEVICE_UP) {
  93.     for (i = 0; i < pd->count; i++)
  94.         if (pd->menus[i].xoffset-8 < x && x < pd->menus[i+1].xoffset-8) {
  95.         if (activemenu != pd->menus[i].menu && !menuinuse) {
  96.             glutSetMenu(activemenu = pd->menus[i].menu);
  97.             glutAttachMenu(GLUT_LEFT_BUTTON);
  98.         }
  99.         return MUI_NO_ACTION;
  100.         }
  101.     if (pd->ishelp && (x > pd->helpmenu.xoffset-8)) {
  102.         if ((activemenu != pd->helpmenu.menu) && !menuinuse) {
  103.         glutSetMenu(activemenu = pd->helpmenu.menu);
  104.         glutAttachMenu(GLUT_LEFT_BUTTON);
  105.         }
  106.         return MUI_NO_ACTION;
  107.     }
  108.     if (activemenu && !menuinuse) {
  109.         glutDetachMenu(GLUT_LEFT_BUTTON);
  110.         activemenu = -1;
  111.     }
  112.     }
  113.     return MUI_NO_ACTION;
  114. }
  115.  
  116. void    drawpulldown(muiObject *obj)
  117. {
  118.     int        i;
  119.     int        xmin, xmax, ymin, ymax;
  120.  
  121.     if (!muiGetVisible(obj))
  122.     return;
  123.  
  124.     drawsetup();
  125.  
  126.     xmin = obj->xmin;
  127.     ymin = obj->ymin;
  128.     xmax = obj->xmax;
  129.     ymax = obj->ymax;
  130.  
  131.     drawedges(xmin++,xmax--,ymin++,ymax--,uiDkGray,uiVyDkGray);
  132.     drawedges(xmin++,xmax--,ymin++,ymax--,uiWhite,uiDkGray);
  133.     drawedges(xmin++,xmax--,ymin++,ymax--,uiVyLtGray,uiMmGray);
  134.     drawedges(xmin++,xmax--,ymin++,ymax--,uiVyLtGray,uiMmGray);
  135.  
  136.     uiLtGray();
  137.         uirectfi(xmin,ymin,xmax,ymax);
  138.  
  139.     if (obj->object) {
  140.         muiPulldown *pd = (muiPulldown *)obj->object;
  141.  
  142.         for (i = 0; i < pd->count; i++) {
  143.         uiBlack();
  144.         uicmov2i(obj->xmin + pd->menus[i].xoffset, obj->ymin + 8);
  145.         uicharstr(pd->menus[i].title, UI_FONT_NORMAL);
  146.         }
  147.     if (pd->ishelp) {
  148.         uiBlack();
  149.         uicmov2i(obj->xmin + pd->helpmenu.xoffset, obj->ymin + 8);
  150.         uicharstr(pd->helpmenu.title, UI_FONT_NORMAL);
  151.     }
  152.     }
  153.  
  154.     drawrestore();
  155. }
  156.  
  157. #ifdef NOTDEF
  158.  
  159. /* static variables */
  160. static int offrightside;
  161. static PullDown *basepd;
  162. static int menubut;
  163. static int menuretval;
  164. static int inpdmode = 0;
  165. static int indopd = 0;
  166. static int savep;
  167. static short savedev[SAVELEN];
  168. static short saveval[SAVELEN];
  169.  
  170. extern    MenuBar    *locatedmb;
  171.  
  172. static short pdinit = 0;
  173.  
  174. void    movemenubar(muiObject *obj)
  175. {
  176.     int    i, tx = MENUXENDGAP-9, totaltwidth = 0;
  177.     int    xsize, ysize, width;
  178.  
  179.     mb->xmin = mb->xorg;
  180.     mb->xmax = mb->xorg+xsize-1;
  181.     mb->ymin = mb->yorg+ysize-MENUBARHEIGHT;
  182.     mb->ymax = mb->yorg+ysize-1;
  183.  
  184.     width = mb->xmax - mb->xmin + 1;
  185.  
  186.     font(PULLDOWNFONT);    /* for correct strwidths */
  187.     for (i = 0; i < mb->count; i++)
  188.         totaltwidth += strwidth(mb->pds[i]->title) + 2*MENUXENDGAP;
  189.  
  190.     for (i = 0; i < mb->count; i++) {
  191.     if (mb->pds[i]->title)
  192.         mb->pds[i]->twidth = strwidth(mb->pds[i]->title);
  193.         if ((mb->pds[i]->title) && (!strcmp(mb->pds[i]->title,"Help")))
  194.            mb->pds[i]->txorg = mb->xmax-MENUXENDGAP-mb->pds[i]->twidth-MENUXGAP;
  195.         else
  196.            mb->pds[i]->txorg = mb->xmin + tx;
  197.         mb->pds[i]->xorg = mb->pds[i]->txorg;
  198.     mb->pds[i]->yorg = mb->ymin-TITLESEP;
  199.     mb->pds[i]->orglocked = 1;
  200.     mb->pds[i]->mb = mb;
  201.  
  202.     if (totaltwidth > width)
  203.             tx += (width-2*MENUXENDGAP)/mb->count-2;
  204.         else
  205.             tx += mb->pds[i]->twidth+24;
  206.     } 
  207. }
  208.  
  209. MenuBar *newmenubar(void)
  210. {
  211.     MenuBar    *mb;
  212.  
  213.     mb = (MenuBar *)calloc(1,sizeof(MenuBar));
  214.     mb->count = 0;
  215.     mb->pds = 0;
  216.     mb->locate = -1;
  217.     mb->enable = 1;
  218.     mb->invisible = 0;
  219.  
  220.     return mb;
  221. }
  222.  
  223. /* ENABLE STATE FUNCTIONS FOR MENU BARS */
  224.  
  225. void    enablemb(MenuBar *mb)
  226. {
  227.     if (mb->enable)
  228.     return;
  229.     mb->enable = 1;
  230. }
  231.  
  232. void    disablemb(MenuBar *mb)
  233. {
  234.     if (!mb->enable)
  235.     return;
  236.     mb->enable = 0;
  237. }
  238.  
  239. short    getenablemb(MenuBar *mb)
  240. {
  241.     return mb->enable;
  242. }
  243.  
  244. /* LOCATE STATE FUNCTIONS FOR MENU BARS */
  245.  
  246. short    gethighlightmb(MenuBar *mb)
  247. {
  248.     return mb->locate;
  249. }
  250.  
  251. void    highlightmb(MenuBar *mb, int pdnum)
  252. {
  253.     short oldpdnum = mb->locate;
  254.  
  255.     if (mb->locate == pdnum)
  256.     return;
  257.     mb->locate = pdnum;
  258.     locatedmb = mb;
  259.     if (!mb->invisible) {
  260.     if (pdnum != -1)
  261.         drawmenubartext(mb,pdnum);
  262.     if (oldpdnum != -1)
  263.         drawmenubartext(mb,oldpdnum);
  264.     }
  265. }
  266.  
  267. void    unhighlightmb(MenuBar *mb)
  268. {
  269.     short oldpdnum = mb->locate;
  270.  
  271.     if (mb->locate == -1)
  272.     return;
  273.     mb->locate = -1;
  274.     if (locatedmb == mb)
  275.     locatedmb = 0;
  276.     if (!mb->invisible)
  277.     if (oldpdnum != -1)
  278.         drawmenubartext(mb,oldpdnum);
  279. }
  280.  
  281. /* VISIBLE STATE FUNCTIONS */
  282.  
  283. void    makevisiblemb(MenuBar *mb)
  284. {
  285.     if (!mb->invisible)
  286.     return;
  287.     mb->invisible = 0;
  288. }
  289.  
  290. void    makeinvisiblemb(MenuBar *mb)
  291. {
  292.     if (mb->invisible)
  293.     return;
  294.     mb->invisible = 1;
  295. }
  296.  
  297. short   getvisiblemb(MenuBar *mb)
  298. {
  299.     return 1-mb->invisible;
  300. }
  301.  
  302. void    loadmenubar(MenuBar *mb, int menucount, PullDown **pdarray)
  303. {
  304.     int    i;
  305.  
  306.     drawsetup();
  307.  
  308.     mb->count = menucount;
  309.     mb->pds = (PullDown **)calloc(menucount,sizeof(PullDown));
  310.     mb->locate = -1;
  311.     mb->enable = 1;
  312.     mb->invisible = 0;
  313.  
  314.     for (i = 0; i < mb->count; i++){
  315.     mb->pds[i] = pdarray[i];
  316.     }
  317.     movemenubar(mb);
  318.  
  319.     drawrestore();
  320. }
  321.  
  322. void    addtomenubar(MenuBar *mb, PullDown *pd)
  323. {
  324.     PullDown     **pds;
  325.     int     i;
  326.  
  327.     mb->count++;
  328.     pds = (PullDown **)calloc(mb->count,sizeof(PullDown));
  329.  
  330.     for (i = 0; i < mb->count-1; i++){
  331.     pds[i] = mb->pds[i];
  332.     }
  333.     pds[i] = pd;
  334.  
  335.     free(mb->pds);
  336.     mb->pds = pds;
  337. }
  338.  
  339. void addtopd(PullDown *pd, MenuItem *mi)
  340. {
  341.     MenuItem *m, *tail;
  342.  
  343.     tail = pd->entries;
  344.  
  345.     if (tail) {
  346.         while (tail->next)
  347.             tail = tail->next;
  348.     tail->next = mi;
  349.     } else
  350.     tail = pd->entries = mi;
  351.  
  352.     m = pd->entries;
  353.  
  354.     while (m) {
  355.         pd->nentries++;
  356.         m->no = pd->nentries;
  357.         m = m->next;
  358.     }
  359.  
  360.     fixuppd(pd);
  361. }
  362.  
  363. void    removefrommenubar(MenuBar *mb, PullDown *pd)
  364. {
  365.     PullDown    **pds;
  366.     int        i, j;
  367.  
  368.     mb->count--;
  369.     pds = (PullDown **)calloc(mb->count,sizeof(PullDown));
  370.  
  371.     for (i = 0,j = 0; i <= mb->count; i++) {
  372.     if (mb->pds[i] != pd) {
  373.         pds[j] = mb->pds[i];
  374.         j++;
  375.     }
  376.     }
  377.  
  378.     free(mb->pds);
  379.     mb->pds = pds;
  380. }
  381.  
  382. void    movemenubar(MenuBar *mb)
  383. {
  384.     int    i, tx = MENUXENDGAP-9, totaltwidth = 0;
  385.     int    xsize, ysize, width;
  386.  
  387.     getorigin(&mb->xorg, &mb->yorg);
  388.     getsize(&xsize, &ysize);
  389.     mb->xmin = mb->xorg;
  390.     mb->xmax = mb->xorg+xsize-1;
  391.     mb->ymin = mb->yorg+ysize-MENUBARHEIGHT;
  392.     mb->ymax = mb->yorg+ysize-1;
  393.  
  394.     width = mb->xmax - mb->xmin + 1;
  395.  
  396.     font(PULLDOWNFONT);    /* for correct strwidths */
  397.     for (i = 0; i < mb->count; i++)
  398.         totaltwidth += strwidth(mb->pds[i]->title) + 2*MENUXENDGAP;
  399.  
  400.     for (i = 0; i < mb->count; i++) {
  401.     if (mb->pds[i]->title)
  402.         mb->pds[i]->twidth = strwidth(mb->pds[i]->title);
  403.         if ((mb->pds[i]->title) && (!strcmp(mb->pds[i]->title,"Help")))
  404.            mb->pds[i]->txorg = mb->xmax-MENUXENDGAP-mb->pds[i]->twidth-MENUXGAP;
  405.         else
  406.            mb->pds[i]->txorg = mb->xmin + tx;
  407.         mb->pds[i]->xorg = mb->pds[i]->txorg;
  408.     mb->pds[i]->yorg = mb->ymin-TITLESEP;
  409.     mb->pds[i]->orglocked = 1;
  410.     mb->pds[i]->mb = mb;
  411.  
  412.     if (totaltwidth > width)
  413.             tx += (width-2*MENUXENDGAP)/mb->count-2;
  414.         else
  415.             tx += mb->pds[i]->twidth+24;
  416.     } 
  417. }
  418.  
  419. int    inmenubar(MenuBar *mb, int mx, int my) /* Window coordinates    */
  420. {
  421.     int i;
  422.     
  423.     if (!getenablemb(mb))
  424.     return -1;
  425.  
  426.     if (getdrawmode() == NORMALDRAW) {
  427.     mx += mb->xorg;
  428.     my += mb->yorg;
  429.     }
  430.     if (mb->xmin <= mx && mx <= mb->xmax && 
  431.     mb->ymin+2 <= my && my <= mb->ymax-2)    /* +-2 for locate highlight*/
  432.        for (i = 0; i < mb->count; i++) {
  433.             if ((mx >= mb->pds[i]->txorg) &&
  434.                 (mx <= mb->pds[i]->txorg+mb->pds[i]->twidth+MENUXGAP*2))
  435.         return i;
  436.     }
  437.     return -1;
  438. }
  439.  
  440. short    locatemenubar(MenuBar *mb, int mx, int my) /* window coordinates */
  441. {
  442.     int highlight = gethighlightmb(mb);
  443.     int inmb  = inmenubar(mb,mx,my);
  444.  
  445.     if (!getenablemb(mb) || !getvisiblemb(mb) || (highlight == inmb))
  446.         return 0;
  447.     if (inmb != -1) {
  448.     unlocateall();
  449.         highlightmb(mb,inmb);
  450.     return 1;
  451.     } else {
  452.     unhighlightmb(mb);
  453.     return 0;
  454.     }
  455. }
  456.  
  457. /* DRAWING MENUBAR FUNCTIONS */
  458.  
  459. void    drawmenubarnow(MenuBar *mb)
  460. {
  461.     uifrontbuffer(1);
  462.     drawmenubar(mb);
  463.     uifrontbuffer(0);
  464. }
  465.  
  466.  
  467. #endif /* NOTDEF */
  468.