home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl_DR-0.10.tar.gz / enl_DR-0.10.tar / enl / menus.c < prev    next >
C/C++ Source or Header  |  1997-06-22  |  8KB  |  351 lines

  1. #include "enlightenment.h"
  2.  
  3. void InitMenuList()
  4. {
  5.    mlist.next=NULL;
  6.    mlist.menu=NULL;
  7.    active_mlist.next=NULL;
  8.    active_mlist.menu=NULL;
  9. }
  10.  
  11. void AddMenuToList(Menu *menu)
  12. {
  13.    struct menulist *ptr;
  14.    
  15.    ptr=malloc(sizeof(struct menulist));
  16.    ptr->menu=menu;
  17.    ptr->next=mlist.next;
  18.    mlist.next=ptr;
  19. }
  20.  
  21. Menu *FindMenu(char *name)
  22. {
  23.    struct menulist *ptr;
  24.    
  25.    ptr=mlist.next;
  26.    while(ptr)
  27.      {
  28.     if (!strcmp(name,ptr->menu->name)) return ptr->menu;
  29.     ptr=ptr->next;
  30.      }
  31.    return NULL;
  32. }
  33.  
  34. Menu *GetMenuWin(Window w)
  35. {
  36.    struct menulist *ptr;
  37.    
  38.    ptr=mlist.next;
  39.    while(ptr)
  40.      {
  41.     if (w==ptr->menu->win) return ptr->menu;
  42.     ptr=ptr->next;
  43.      }
  44.    return NULL;
  45. }
  46.  
  47. void AddActiveMenuToList(Menu *menu)
  48. {
  49.    struct menulist *ptr;
  50.    
  51.    ptr=malloc(sizeof(struct menulist));
  52.    ptr->menu=menu;
  53.    ptr->next=active_mlist.next;
  54.    active_mlist.next=ptr;
  55. }
  56.  
  57. void DeleteToActiveMenu(char *name)
  58. {
  59.    struct menulist *ptr;
  60.    struct menulist *pptr;
  61.    
  62.    ptr=active_mlist.next;
  63.    while(ptr)
  64.      {
  65.     if (!strcmp(name,ptr->menu->name)) 
  66.       {
  67.          active_mlist.next=ptr;
  68.          return;
  69.       }
  70.     pptr=ptr;
  71.     HideMenu(ptr->menu);
  72.     ptr=ptr->next;
  73.     free(pptr);
  74.      }
  75.    active_mlist.next=NULL;
  76. }
  77.  
  78. Menu *GetActiveMenuWin(Window w)
  79. {
  80.    struct menulist *ptr;
  81.    
  82.    ptr=mlist.next;
  83.    while(ptr)
  84.      {
  85.     if (w==ptr->menu->win) return ptr->menu;
  86.     ptr=ptr->next;
  87.      }
  88.    return NULL;
  89. }
  90.  
  91. Menu *CreateMenu(char *name, int type, int num_items)
  92. {
  93.    Menu *m;
  94.    int i;
  95.    
  96.    m=malloc(sizeof(Menu));
  97.    if (!m) return NULL;
  98.    m->type=type;
  99.    m->num_items=num_items;
  100.    m->width=0;
  101.    m->height=0;
  102.    if (name) strncpy(m->name,name,255);
  103.    else name[0]=0; 
  104.    m->popup_x=0;
  105.    m->popup_y=0;
  106.    m->win=0;
  107.    m->mask=0;
  108.    m->items=malloc(sizeof(MenuItem *)*num_items);
  109.    m->sel_item=-1;
  110.    for (i=0;i<num_items;i++)
  111.      {
  112.     m->items[i]=NULL;
  113.      }
  114.    return m;
  115. }
  116.  
  117. void AddMenuItem(Menu *m, MenuItem* mi)
  118. {
  119.    int i;
  120.  
  121.    for (i=0;i<m->num_items;i++)
  122.      {
  123.     if (!m->items[i]) 
  124.       {
  125.          m->items[i]=mi;
  126.          break;
  127.       }
  128.      }
  129. }
  130.  
  131. void RenderMenu(Menu *m)
  132. {
  133.    int i;
  134.    XGCValues gcv;
  135.    GC gc;
  136.    GC or;
  137.    
  138.    if (m->mask) XFreePixmap(disp,m->mask);
  139.    if (m->win) XDestroyWindow(disp,m->win);
  140.    m->win=XCreateSimpleWindow(disp,root,0,0,m->width,m->height,0,0,0);
  141.    m->mask=XCreatePixmap(disp,m->win,m->width,m->height,1);
  142.    
  143.    gcv.function=GXor;
  144.    gc=XCreateGC(disp,m->mask,0,&gcv);
  145.    or=XCreateGC(disp,m->mask,GCFunction,&gcv);
  146.    XSetForeground(disp,gc,0);
  147.    XSetForeground(disp,or,1);
  148.    XFillRectangle(disp,m->mask,gc,0,0,m->width,m->height);
  149.    XSetForeground(disp,gc,1);
  150.    for(i=0;i<m->num_items;i++)
  151.      {
  152.     if (m->items[i])
  153.       {
  154.          if (m->items[i]->unsel_mask)
  155.            XCopyArea(disp,m->items[i]->unsel_mask,m->mask,or,
  156.              0,0,m->items[i]->width,m->items[i]->height,
  157.              m->items[i]->x,m->items[i]->y);
  158.          else
  159.            XFillRectangle(disp,m->mask,gc,m->items[i]->x,
  160.                   m->items[i]->y,m->items[i]->width,
  161.                   m->items[i]->height);
  162.          XReparentWindow(disp,m->items[i]->win,m->win,
  163.                  m->items[i]->x,m->items[i]->y);
  164.          XMapWindow(disp,m->items[i]->win);
  165.       }
  166.      }
  167.    XShapeCombineMask(disp,m->win,ShapeBounding,0,0,m->mask,ShapeSet);
  168.    XFreeGC(disp,gc);
  169.    XFreeGC(disp,or);
  170. }
  171.  
  172. MenuItem *CreateMenuItem(int type, int x, int y, int width, int height, char *text, 
  173.             int text_x, int text_y, int text_w, int text_h, 
  174.             char *unsel, ImColor *unsel_icl,
  175.             char *sel, ImColor *sel_icl)
  176. {
  177.    MenuItem *mi;
  178.    Image *im1,*im2;
  179.    
  180.    mi=malloc(sizeof(MenuItem));
  181.    mi->x=x;
  182.    mi->y=y;
  183.    mi->width=width;
  184.    mi->height=height;
  185.    mi->text=NULL;
  186.    mi->type=type;
  187.    mi->unsel_pmap=0;
  188.    mi->unsel_mask=0;
  189.    mi->sel_pmap=0;
  190.    mi->sel_mask=0;
  191.    
  192.    if (mi->type==DECOR) sel=NULL;
  193.    mi->win=XCreateSimpleWindow(disp,root,0,0,mi->width,mi->height,0,0,0);
  194.    im1=LoadImage(imd,unsel,unsel_icl);
  195.    ImlibRender(imd,im1,mi->width,mi->height);
  196.    mi->unsel_pmap=ImlibMoveImageToPixmap(imd,im1);
  197.    mi->unsel_mask=ImlibMoveMaskToPixmap(imd,im1);
  198.    ImlibDestroyImage(imd,im1);
  199.    if (sel)
  200.      {
  201.     im2=LoadImage(imd,sel,sel_icl);
  202.     ImlibRender(imd,im2,mi->width,mi->height);
  203.     mi->sel_pmap=ImlibMoveImageToPixmap(imd,im2);
  204.     mi->sel_mask=ImlibMoveMaskToPixmap(imd,im2);
  205.     ImlibDestroyImage(imd,im2);
  206.      }
  207.    if (text)
  208.      {
  209.     mi->text=malloc(strlen(text)+1);
  210.     strcpy(mi->text,text);
  211.     DrawText(mi->unsel_pmap,mi->text,text_x,text_y,text_w,text_h);
  212.     if (mi->unsel_mask) DrawTextMask(mi->unsel_mask,mi->text,text_x,text_y,text_w,text_h);
  213.     if (mi->sel_pmap) DrawText(mi->sel_pmap,mi->text,text_x,text_y,text_w,text_h);
  214.     if (mi->sel_mask) DrawTextMask(mi->sel_mask,mi->text,text_x,text_y,text_w,text_h);
  215.      } 
  216.    XSetWindowBackgroundPixmap(disp,mi->win,mi->unsel_pmap);
  217.    if (mi->unsel_mask) 
  218.      XShapeCombineMask(disp,mi->win,ShapeBounding,0,0,mi->unsel_mask,ShapeSet);
  219.    return mi;
  220. }
  221.  
  222. void DrawMenuItem(Menu *m, int num, int state)
  223. {
  224.    XGCValues gcv;
  225.    GC gc;
  226.    GC or;
  227.    int i;
  228.    int in;
  229.    int inx,iny;
  230.    int xx1,xx2,yy1,yy2;
  231.     
  232.    gcv.function=GXor;
  233.    gc=XCreateGC(disp,m->mask,0,&gcv);
  234.    or=XCreateGC(disp,m->mask,GCFunction,&gcv);
  235.    XSetForeground(disp,gc,0);
  236.    XSetForeground(disp,or,1);
  237.    XFillRectangle(disp,m->mask,gc,m->items[num]->x,m->items[num]->y,
  238.           m->items[num]->width,m->items[num]->height);
  239.    XSetForeground(disp,gc,1);
  240.    
  241.    if (state==0)
  242.      {
  243.     XSetWindowBackgroundPixmap(disp,m->items[num]->win,m->items[num]->unsel_pmap);
  244.     if (m->items[num]->unsel_mask) 
  245.       XShapeCombineMask(disp,m->items[num]->win,ShapeBounding,0,0,m->items[num]->unsel_mask,ShapeSet);
  246.      }
  247.    else
  248.      {
  249.     XSetWindowBackgroundPixmap(disp,m->items[num]->win,m->items[num]->sel_pmap);
  250.     if (m->items[num]->sel_mask) 
  251.       XShapeCombineMask(disp,m->items[num]->win,ShapeBounding,0,0,m->items[num]->sel_mask,ShapeSet);
  252.      }
  253.    XClearWindow(disp,m->items[num]->win);
  254.    xx1=m->items[num]->x;
  255.    xx2=m->items[num]->x+m->items[num]->width;
  256.    yy1=m->items[num]->y;
  257.    yy2=m->items[num]->y+m->items[num]->height;
  258.    for(i=0;i<m->num_items;i++)
  259.      {
  260.     int x1,x2,y1,y2;
  261.     inx=0,iny=0;in=0;
  262.     
  263.     x1=m->items[i]->x;
  264.     x2=m->items[i]->x+m->items[i]->width;
  265.     y1=m->items[i]->y;
  266.     y2=m->items[i]->y+m->items[i]->height;
  267.     if (((x1>=xx1)&&(x1<=xx2))||
  268.         ((x2>=xx1)&&(x2<=xx2))||
  269.         ((xx1>=x1)&&(xx2<=x2))) inx=1;
  270.     if (((y1>=yy1)&&(y1<=yy2))||
  271.         ((y2>=yy1)&&(y2<=yy2))||
  272.         ((yy1>=y1)&&(yy2<=y2))) iny=1;
  273.     if (inx&&iny) in=1;
  274.     if (in)
  275.       {
  276.          if ((i==num)&&(m->items[i]->type!=DECOR))
  277.            {
  278.           if (state==0)
  279.             {
  280.                if (m->items[i]->unsel_mask)
  281.              XCopyArea(disp,m->items[i]->unsel_mask,m->mask,or,
  282.                    0,0,m->items[i]->width,m->items[i]->height,
  283.                    m->items[i]->x,m->items[i]->y);
  284.                else
  285.              XFillRectangle(disp,m->mask,gc,m->items[i]->x,
  286.                     m->items[i]->y,m->items[i]->width,
  287.                     m->items[i]->height);
  288.             }
  289.           else
  290.             {
  291.                if (m->items[i]->sel_mask)
  292.              XCopyArea(disp,m->items[i]->sel_mask,m->mask,or,
  293.                    0,0,m->items[i]->width,m->items[i]->height,
  294.                    m->items[i]->x,m->items[i]->y);
  295.                else if (m->items[i]->type==DECOR)
  296.              XCopyArea(disp,m->items[i]->unsel_mask,m->mask,or,
  297.                   0,0,m->items[i]->width,m->items[i]->height,
  298.                    m->items[i]->x,m->items[i]->y);
  299.                else
  300.              XFillRectangle(disp,m->mask,gc,m->items[i]->x,
  301.                     m->items[i]->y,m->items[i]->width,
  302.                     m->items[i]->height);
  303.             }
  304.            }
  305.          else
  306.            {
  307.           if (m->items[i]->unsel_mask)
  308.             XCopyArea(disp,m->items[i]->unsel_mask,m->mask,or,
  309.                   0,0,m->items[i]->width,m->items[i]->height,
  310.                   m->items[i]->x,m->items[i]->y);
  311.           else
  312.             XFillRectangle(disp,m->mask,gc,m->items[i]->x,
  313.                    m->items[i]->y,m->items[i]->width,
  314.                    m->items[i]->height);
  315.            }
  316.       }
  317.      }
  318.    XShapeCombineMask(disp,m->win,ShapeBounding,0,0,m->mask,ShapeSet);
  319.    XFreeGC(disp,gc);
  320.    XFreeGC(disp,or);
  321.    XSync(disp,False);
  322. }
  323.  
  324. void ShowMenu(Menu *menu)
  325. {
  326.    XRaiseWindow(disp,menu->win);
  327.    XMapWindow(disp,menu->win);
  328.    XSync(disp,False);
  329. }
  330.  
  331. void HideMenu(Menu *menu)
  332. {
  333.    XUnmapWindow(disp,menu->win);
  334.    if (menu->sel_item>=0) DrawMenuItem(menu,menu->sel_item,0);
  335.    menu->sel_item=-1;
  336.    XSync(disp,False);
  337. }
  338.  
  339. void HideAllMenus()
  340. {
  341.    struct menulist *ptr;
  342.    
  343.    ptr=mlist.next;
  344.    while(ptr)
  345.      {
  346.     HideMenu(ptr->menu);
  347.     ptr=ptr->next;
  348.      }
  349. }
  350.  
  351.