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 / iconify.c < prev    next >
C/C++ Source or Header  |  1997-06-24  |  14KB  |  505 lines

  1. #include "enlightenment.h"
  2.  
  3. Icon *GrabClient(EWin *ewin) {
  4.    Icon *ic;
  5.    Pixmap tmp;
  6.    Pixmap grab;
  7.    XImage *xim;
  8.    int w,h;
  9.    int ww,hh;
  10.    int cw,ch;
  11.    int gw,gh,gx,gy;
  12.    int i,j;
  13.    float x,y;
  14.    float inc;
  15.    GC gc;
  16.    XGCValues gcv;
  17.    int r,g,b;
  18.    
  19.    ic=malloc(sizeof(Icon));
  20.    ic->ewin=ewin;ic->pmap=0;ic->mask=0;ic->win=0;
  21.    ic->ewin->icon=ic;
  22.    cw=ewin->client_width;
  23.    ch=ewin->client_height;
  24.    gc=XCreateGC(disp,ewin->client_win,0,&gcv);
  25.    
  26.    if (icfg.orientation) {
  27.     if ((4*ch)>(3*cw)) {
  28.          /* if client is higher that it is wide, limit the widht to 4/3 */
  29.          /* times the width of the iconbox */
  30.          h=(((icfg.width-(icfg.bx*2))*3)/4);
  31.          w=((cw*h)/ch);
  32.     } else {
  33.          w=(icfg.width-(icfg.bx*2));
  34.          h=((ch*w)/cw);
  35.         }
  36.    } else {
  37.     if ((3*cw)>(4*ch)) {
  38.          /* if client is wider that it is high, limit the widht to 4/3 */
  39.          /* times the height of the iconbox */
  40.          w=(((icfg.height-(icfg.by*2))*4)/3);
  41.          h=((ch*w)/cw);
  42.     } else {
  43.          h=(icfg.height-(icfg.by*2));
  44.          w=((cw*h)/ch);
  45.     }
  46.    }
  47.    ww=w-1;if (ww<1) w=1;
  48.    hh=h-1;if (hh<1) h=1;
  49.    ic->pmap=XCreatePixmap(disp,ewin->client_win,w,h,depth);
  50.    ic->mask=0;
  51.    ic->win=XCreateSimpleWindow(disp,icfg.bg_win,0,0,w,h,0,0,0);
  52.    ic->width=w;
  53.    ic->height=h;
  54.    ic->x=0;
  55.    ic->y=0;
  56.    tmp=XCreatePixmap(disp,ewin->client_win,cw,hh,depth);
  57.    grab=XCreatePixmap(disp,ewin->client_win,cw,ch,depth);
  58.    gx=0;gy=0;gw=cw;gh=ch;
  59.    if (ewin->frame_x+ewin->client_x<0) {
  60.     gx=-(ewin->frame_x+ewin->client_x);
  61.     gw-=gx;
  62.    }
  63.    if (ewin->frame_y+ewin->client_y<0) {
  64.     gy=-(ewin->frame_y+ewin->client_y);
  65.     gh-=gy;
  66.    }
  67.    if (ewin->frame_x+ewin->client_x+ewin->client_width>=DisplayWidth(disp,screen))
  68.      gw=DisplayWidth(disp,screen)-(ewin->frame_x+ewin->client_x+gx);
  69.    if (ewin->frame_y+ewin->client_y+ewin->client_height>=DisplayHeight(disp,screen))
  70.      gh=DisplayHeight(disp,screen)-(ewin->frame_y+ewin->client_y+gy);
  71.    xim=NULL;
  72.    if ((gw>0)&&(gh>0))
  73.      xim=XGetImage(disp,ewin->client_win,gx,gy,gw,gh,0xffffffff,ZPixmap);
  74.    if (!xim) {
  75.     XFreePixmap(disp,grab);
  76.     grab=ewin->client_win;
  77.    } else {
  78.     XPutImage(disp,grab,gc,xim,0,0,gx,gy,gw,gh);
  79.     XDestroyImage(xim);
  80.    }
  81.    inc=(float)ch/(float)hh;
  82.    j=0;
  83.    for (y=0;;y+=inc) {
  84.     i=(int)y;
  85.     if (i>=ch) break;
  86.     XCopyArea(disp,grab,tmp,gc,0,i,cw,1,0,j++);
  87.    }
  88.    inc=(float)cw/(float)ww;
  89.    j=0;
  90.    for (x=0;;x+=inc) {
  91.     i=(int)x;
  92.     if (i>=cw) break;
  93.     XCopyArea(disp,tmp,ic->pmap,gc,i,0,1,hh,1+j++,1);
  94.    }
  95.    /* draw bevels around icon */
  96.    r=255,g=255,b=255;
  97.    XSetForeground(disp,gc,ImlibBestColorMatch(imd,&r,&g,&b));
  98.    XDrawLine(disp,ic->pmap,gc,0,0,ic->width-1,0);
  99.    XDrawLine(disp,ic->pmap,gc,0,0,0,ic->height-1);
  100.    r=0,g=0,b=0;
  101.    XSetForeground(disp,gc,ImlibBestColorMatch(imd,&r,&g,&b));
  102.    XSetWindowBackgroundPixmap(disp,ic->win,ic->pmap);
  103.    XDrawLine(disp,ic->pmap,gc,0,ic->height-1,ic->width-1,ic->height-1);
  104.    XDrawLine(disp,ic->pmap,gc,ic->width-1,0,ic->width-1,ic->height-1);
  105.    /* free up after uourselves */
  106.    XFreePixmap(disp,tmp);
  107.    XFreePixmap(disp,grab);
  108.    XFreeGC(disp,gc); 
  109.    return ic;
  110. }
  111.  
  112. void LoadImageWithImlib(char *reg_im, ImColor *reg_trans, Pixmap *pmap, Pixmap *mask, int *width, int *height) 
  113. {
  114. /* This function basically sums up everything you need to load an image using
  115.  * imlib -- made by Mandrake some time around 06/21/97 or so ... */
  116.    Image *im;
  117.    char s[10240];
  118.  
  119.    im=LoadImage(imd,reg_im,reg_trans);
  120.    if (!im) 
  121.      {
  122.     Alert("EEEEK! Someone Save me!\n I wasn't able to load the file:\n%s\n I'm having a nervous breakdown over this... AAARGH\n(Enlightenment is seen fleeing from the screen\n in an uncrontrollable fit).\n",reg_im);
  123.     exit(1);
  124.      }
  125.    ImlibRender(imd,im,im->rgb_width,im->rgb_height);
  126.    *pmap=ImlibMoveImageToPixmap(imd,im);
  127.    *mask=ImlibMoveMaskToPixmap(imd,im);
  128.    *width=im->rgb_width;
  129.    *height=im->rgb_height;
  130.    ImlibDestroyImage(imd,im);
  131. }
  132. void LoadImageSizeWithImlib(char *reg_im, ImColor *reg_trans, Pixmap *pmap, Pixmap *mask, int width, int height) 
  133. {
  134. /* This function basically sums up everything you need to load an image using
  135.  * imlib -- made by Mandrake some time around 06/21/97 or so ... */
  136.    Image *im;
  137.    char s[10240];
  138.  
  139.    im=LoadImage(imd,reg_im,reg_trans);
  140.    if (!im) 
  141.      {
  142.     Alert("EEEEK! Someone Save me!\n I wasn't able to load the file:\n%s\n I'm having a nervous breakdown over this... AAARGH\n(Enlightenment is seen fleeing from the screen\n in an uncrontrollable fit).\n",reg_im);
  143.     exit(1);
  144.      }
  145.    ImlibRender(imd,im,width,height);
  146.    *pmap=ImlibMoveImageToPixmap(imd,im);
  147.    *mask=ImlibMoveMaskToPixmap(imd,im);
  148.    ImlibDestroyImage(imd,im);
  149. }
  150.  
  151. void InitIcons() 
  152. {
  153.    Pixmap dummy;
  154.  
  155. /* 06/22/97 - finally abstracted out the button loading to use (mandrake)
  156.  * LoadImageWithImlib - cut down on like 100 or so lines of code
  157.  * Still lots of work to do in here */
  158.  
  159.    LoadImageWithImlib(icfg.left_arrow_im, &icfg.left_transparent, &icfg.left_pmap, &icfg.left_mask, &icfg.left_w, &icfg.left_h);
  160.    LoadImageWithImlib(icfg.left_sel_arrow_im, &icfg.left_sel_transparent, &icfg.left_sel_pmap, &icfg.left_sel_mask, &icfg.left_sel_w, &icfg.left_sel_h);
  161.    LoadImageWithImlib(icfg.left_clk_arrow_im, &icfg.left_clk_transparent, &icfg.left_clk_pmap, &icfg.left_clk_mask, &icfg.left_clk_w, &icfg.left_clk_h);
  162.    LoadImageWithImlib(icfg.right_arrow_im, &icfg.right_transparent, &icfg.right_pmap, &icfg.right_mask, &icfg.right_w, &icfg.right_h);
  163.    LoadImageWithImlib(icfg.right_sel_arrow_im, &icfg.right_sel_transparent, &icfg.right_sel_pmap, &icfg.right_sel_mask, &icfg.right_sel_w, &icfg.right_sel_h);
  164.    LoadImageWithImlib(icfg.right_clk_arrow_im, &icfg.right_clk_transparent, &icfg.right_clk_pmap, &icfg.right_clk_mask, &icfg.right_clk_w, &icfg.right_clk_h);
  165.    LoadImageSizeWithImlib(icfg.background_im, NULL, &icfg.bg_pmap, &dummy, icfg.width, icfg.height);
  166.    
  167.    icfg.bg_win=XCreateSimpleWindow(disp,root,icfg.x,icfg.y,
  168.                    icfg.width,icfg.height,0,0,0);
  169.    XSelectInput(disp,icfg.bg_win,SubstructureNotifyMask|ButtonPressMask|
  170.         ButtonReleaseMask|EnterWindowMask|LeaveWindowMask|
  171.         ButtonMotionMask|FocusChangeMask|ColormapChangeMask|
  172.         PropertyChangeMask|SubstructureRedirectMask|
  173.         PointerMotionMask);
  174.    if (icfg.orientation) 
  175.      {
  176.     icfg.left_win=XCreateSimpleWindow(disp,root,
  177.                       icfg.x+((icfg.width-icfg.left_w)/2),
  178.                       icfg.y-icfg.left_h,
  179.                       icfg.left_w,icfg.left_h,0,0,0);
  180.     icfg.right_win=XCreateSimpleWindow(disp,root,
  181.                       icfg.x+((icfg.width-icfg.right_w)/2),
  182.                       icfg.y+icfg.height,
  183.                       icfg.right_w,icfg.right_h,0,0,0);
  184.    } else {
  185.     icfg.left_win=XCreateSimpleWindow(disp,root,icfg.x-icfg.left_w,
  186.                       icfg.y+((icfg.height-icfg.left_h)/2),
  187.                       icfg.left_w,icfg.left_h,0,0,0);
  188.     icfg.right_win=XCreateSimpleWindow(disp,root,icfg.x+icfg.width,
  189.                       icfg.y+((icfg.height-icfg.right_h)/2),
  190.                       icfg.right_w,icfg.right_h,0,0,0);
  191.    }
  192.    XSelectInput(disp,icfg.left_win,LeaveWindowMask|
  193.         EnterWindowMask|SubstructureNotifyMask|
  194.         PropertyChangeMask|SubstructureRedirectMask);
  195.    XSelectInput(disp,icfg.right_win,LeaveWindowMask|
  196.         EnterWindowMask|SubstructureNotifyMask|
  197.         PropertyChangeMask|SubstructureRedirectMask);
  198.    XSetWindowBackgroundPixmap(disp,icfg.bg_win,icfg.bg_pmap);
  199.    XSetWindowBackgroundPixmap(disp,icfg.left_win,icfg.left_pmap);
  200.    XSetWindowBackgroundPixmap(disp,icfg.right_win,icfg.right_pmap);
  201.    if (icfg.left_mask)
  202.      XShapeCombineMask(disp,icfg.left_win,ShapeBounding,0,0,icfg.left_mask,ShapeSet);
  203.    if (icfg.right_mask)
  204.      XShapeCombineMask(disp,icfg.right_win,ShapeBounding,0,0,icfg.right_mask,ShapeSet);
  205. }
  206.  
  207. Icon *GetIconWinID(Window w) {
  208.    struct i_member *ptr;
  209.    
  210.    ptr=ilist.first;
  211.    while(ptr) {
  212.     if (ptr->icon->win==w) return ptr->icon;
  213.     ptr=ptr->next;
  214.    }
  215.    return NULL;
  216. }
  217.  
  218. void AddIcon(Icon *ic) 
  219. {
  220.    struct i_member *ptr;
  221.    struct i_member *pptr;
  222.    int x,y;
  223.    
  224.    if (ilist.first) 
  225.      {
  226.     ptr=ilist.first;
  227.     x=ptr->icon->x;
  228.     y=ptr->icon->y;
  229.     while(ptr) 
  230.       {
  231.          pptr=ptr;
  232.          x+=ptr->icon->width+(icfg.bx*2);
  233.          y+=ptr->icon->height+(icfg.by*2);
  234.          ptr=ptr->next;
  235.       }
  236.     pptr->next=malloc(sizeof(struct i_member));
  237.     pptr->next->next=NULL;
  238.     pptr->next->icon=ic;
  239.     ilist.last=pptr->next;
  240.     if (icfg.orientation) 
  241.       {
  242.          pptr->next->icon->x=icfg.bx;
  243.          pptr->next->icon->y=y;
  244.       } 
  245.     else 
  246.       {
  247.          pptr->next->icon->x=x;
  248.          pptr->next->icon->y=icfg.by;
  249.       }
  250.     XMoveWindow(disp,pptr->next->icon->win,pptr->next->icon->x,pptr->next->icon->y);
  251.     XMapWindow(disp,pptr->next->icon->win);
  252.     if (ic->mask) XShapeCombineMask(disp,ic->win,ShapeBounding,0,0,ic->mask,ShapeSet);
  253.      } 
  254.    else 
  255.      {
  256.     ilist.first=malloc(sizeof(struct i_member));
  257.     ilist.first->next=NULL;
  258.     ilist.first->icon=ic;
  259.     ilist.first->icon->y=icfg.by;
  260.     ilist.first->icon->x=icfg.bx;
  261.     ilist.last=ilist.first;
  262.     XMoveWindow(disp,ilist.first->icon->win,ilist.first->icon->x,ilist.first->icon->y);
  263.     XMapWindow(disp,ilist.first->icon->win);
  264.     if (ic->mask) XShapeCombineMask(disp,ic->win,ShapeBounding,0,0,ic->mask,ShapeSet);
  265.      }
  266.    XSync(disp,False);
  267. }
  268.  
  269. void DelIcon(Window w) {
  270.    struct i_member *ptr;
  271.    struct i_member *pptr;
  272.    int found;
  273.    int wid;
  274.    
  275.    pptr=NULL;
  276.    found=0;
  277.    if (ilist.first) {
  278.     ptr=ilist.first;
  279.     while(ptr) {
  280.          if (ptr->icon->win==w) {
  281.           found=1;
  282.           break;
  283.          }
  284.          pptr=ptr;
  285.          ptr=ptr->next;
  286.     }
  287.     if (pptr) pptr->next=ptr->next;
  288.     else ilist.first=ptr->next;
  289.     if (icfg.orientation)
  290.       wid=ptr->icon->height;
  291.     else
  292.       wid=ptr->icon->width;
  293.     if (ptr->icon->win) XDestroyWindow(disp,ptr->icon->win);
  294.     if (ptr->icon->pmap) XFreePixmap(disp,ptr->icon->pmap);
  295.     if (ptr->icon->mask) XFreePixmap(disp,ptr->icon->mask);
  296.     ptr->icon->ewin->icon=NULL;
  297.     free(ptr->icon);
  298.     if (ilist.last==ptr) 
  299.     {
  300.        if (pptr) ilist.last=pptr;
  301.        else ilist.last=NULL;
  302.     }
  303.     free(ptr);
  304.     if (pptr) ptr=pptr->next;
  305.     else ptr=ilist.first;
  306.     while(ptr) 
  307.     {
  308.        if (icfg.orientation)
  309.          ptr->icon->y-=wid+(icfg.by*2);
  310.        else
  311.          ptr->icon->x-=wid+(icfg.bx*2);
  312.        XMoveWindow(disp,ptr->icon->win,ptr->icon->x,ptr->icon->y);
  313.        XMapWindow(disp,ptr->icon->win);
  314.        XSync(disp,False);
  315.        pptr=ptr;
  316.        ptr=ptr->next;
  317.     }
  318.    }
  319.    XSync(disp,False);
  320. }
  321.  
  322. void Msg_Iconify(EWin *ewin) {
  323.    XSync(disp,False);
  324.    ewin->state|=~MAPPED;
  325.    ewin->state|=ICONIFIED;
  326. }
  327.  
  328. void Msg_DeIconify(EWin *ewin) {
  329.    XSync(disp,False);
  330.    ewin->state&=~ICONIFIED;
  331.    ewin->state|=MAPPED;
  332. }
  333.  
  334. Icon *LoadIconFromDisk(EWin *ewin, char *IconInfo, ImColor *icl)
  335. {
  336.    Icon *ic;
  337.    Pixmap pmap;
  338.    Pixmap mask;
  339.    int w,h;
  340.    
  341.    ic=malloc(sizeof(Icon));
  342.    LoadImageWithImlib(IconInfo,icl,&pmap,&mask,&w,&h);
  343.    ic->ewin=ewin;
  344.    ic->ewin->icon=ic;
  345.    ic->win=XCreateSimpleWindow(disp,icfg.bg_win,0,0,w,h,0,0,0);
  346.    ic->width=w;
  347.    ic->height=h;
  348.    ic->pmap=pmap;
  349.    ic->mask=mask;
  350.    ic->x=0;
  351.    ic->y=0;
  352.    XSetWindowBackgroundPixmap(disp,ic->win,ic->pmap);
  353.    
  354.    return ic;
  355. }
  356.  
  357. Icon *PredefinedIconLoad(EWin *ewin, char *TitleBarContents) 
  358. {
  359. /* Originally created 06/11/97 by Mandrake (mandrake@mandrake.net)
  360.  * this function should return a 1 if found, 0 if not found.
  361.  * If found, the icon is loaded with LoadIconFromDisk
  362.  */
  363.    char *IconInfo;
  364.    ImColor *icl;
  365.    
  366.    IconInfo = ReturnIconPath(TitleBarContents,&icl);
  367.    if (!IconInfo) return NULL;
  368.    if (IconInfo[0]) 
  369.      {
  370.       /* RIGHT HERE NEEDS TO CHANGE TO USE LoadImageWithImlib!!!!!!!!!!!
  371.        * LoadIconFromDisk doesn't exist!!! */
  372.         return LoadIconFromDisk(ewin,IconInfo,icl);
  373.      } 
  374.    return NULL;
  375. }
  376.  
  377. char *ReturnIconPath(char *TitleBarContents, ImColor **icl) 
  378. {
  379. /*   
  380.  * this function should parse through the predefined icon configuration
  381.  * and determine if the window has a predefined icon associated with it
  382.  * NOTE: consider adding a PreDefIconPath to struct Ewin - next release?
  383.  * This function returns the line number containing information about
  384.  * the particular window's predefined icon
  385.  */
  386.    int i;
  387.    
  388.    for(i=0;i<predef_num;i++)
  389.      {
  390.     if (matchregexp(predef_list[i].title,TitleBarContents))
  391.       {
  392.          *icl=predef_list[i].transp;
  393.          return predef_list[i].image_file;
  394.       }
  395.      }
  396.    return NULL;
  397. }
  398.  
  399. void Finish_Iconify() {
  400.    Icon *ic;
  401.    
  402. /* This code was modified by Mandrake - mandrake@mandrake.net 
  403.  * added 06/10/97 - ability to have predefined icon definitions
  404.  * (currently does not support shaped pixmaps)
  405.  */
  406.    ic=PredefinedIconLoad(newicon.ewin,newicon.ewin->title);
  407.    if(!ic) ic=GrabClient(newicon.ewin);
  408. /** end mod **/
  409.    AddIcon(ic);
  410.    XUnmapWindow(disp,newicon.ewin->frame_win);
  411.    if (fx.shadow.on) XUnmapWindow(disp,newicon.ewin->fx.shadow_win);
  412.    Msg_Iconify(newicon.ewin);
  413.    /* allow other iconifications ot go ahead */
  414.    if (newicon.kill) Do_KillWin(newicon.ewin, newicon.kill-1);
  415.    newicon.ewin=NULL;
  416.    newicon.win=0;
  417.    newicon.kill=0;
  418. }
  419.  
  420. void MapIconBox()
  421. {
  422.    XMapWindow(disp,icfg.bg_win);
  423.    XMapWindow(disp,icfg.left_win);
  424.    XMapWindow(disp,icfg.right_win);
  425.    XSync(disp,False);
  426. }
  427.  
  428. void IconLeft()
  429. {
  430.    Window dummyw;
  431.    int dummy,btns;
  432.    
  433.    btns=1;
  434.    while (btns) {
  435.     IconsScroll(8);
  436.     XQueryPointer(disp,root,&dummyw,&dummyw,&dummy,&dummy,&dummy,&dummy,&btns);
  437.     usleep(icfg.scroll_speed*1000);
  438.    }
  439. }
  440.  
  441. void IconRight() {
  442.    Window dummyw;
  443.    int dummy,btns;
  444.    
  445.    btns=1;
  446.    while (btns) {
  447.     IconsScroll(-8);
  448.     XQueryPointer(disp,root,&dummyw,&dummyw,&dummy,&dummy,&dummy,&dummy,&btns);
  449.     usleep(icfg.scroll_speed*1000);
  450.    }
  451. }
  452.  
  453. void IconsScroll(int x)
  454. {
  455.    struct i_member *ptr;
  456.    int ok=0;
  457.    
  458.    ptr=ilist.first;
  459.    if (ptr) {
  460.     if (icfg.orientation) {
  461.          if (x<0) {
  462.           if (ilist.last->icon->y>(icfg.height-icfg.by-ilist.last->icon->height+x)) 
  463.             ok=1;
  464.          }
  465.          if (x>0) {
  466.           if (ilist.first->icon->y<(icfg.by+x)) 
  467.             ok=1;
  468.          }
  469.     } else {
  470.          if (x<0) {
  471.           if (ilist.last->icon->x>(icfg.width-icfg.bx-ilist.last->icon->width+x)) 
  472.             ok=1;
  473.          }
  474.          if (x>0) {
  475.           if (ilist.first->icon->x<(icfg.bx+x)) 
  476.             ok=1;
  477.          }
  478.     }
  479.    }
  480.    if (!ok) return;
  481.    while(ptr) {
  482.     if (icfg.orientation)
  483.       ptr->icon->y+=x;
  484.     else
  485.       ptr->icon->x+=x;
  486.     XMoveWindow(disp,ptr->icon->win,ptr->icon->x,ptr->icon->y);
  487.     ptr=ptr->next;
  488.    }
  489.    XSync(disp,False);
  490. }
  491.  
  492. void RaiseLowerIcons() {
  493.    if (icfg.level) {
  494.     XRaiseWindow(disp,icfg.bg_win);
  495.     XRaiseWindow(disp,icfg.left_win);
  496.     XRaiseWindow(disp,icfg.right_win);
  497.     XSync(disp,False);
  498.    } else {
  499.     XLowerWindow(disp,icfg.bg_win);
  500.     XLowerWindow(disp,icfg.left_win);
  501.     XLowerWindow(disp,icfg.right_win);
  502.     XSync(disp,False);
  503.    }
  504. }
  505.