home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Enlightenment / enl14.tgz / enl14.tar / enl14 / alert.c < prev    next >
C/C++ Source or Header  |  1997-11-21  |  9KB  |  304 lines

  1. #include "enl.h"
  2.  
  3. void Alert(char *fmt, ...)
  4. {
  5.    char text[10240];
  6.    va_list ap;
  7.     
  8.    va_start( ap, fmt );
  9. #ifdef __USE_GNU
  10.    vsnprintf( text, 10240, fmt, ap );
  11. #else
  12.    vsprintf( text, fmt, ap );
  13. #endif
  14.    va_end( ap );
  15.    ShowAlert(text);
  16. }
  17.  
  18. void ShowAlert(char *text)
  19. {
  20.    Window win;
  21.    Display *dd;
  22.    int wid,hih,w,h,i,j,k,mask;
  23.    XGCValues gcv;
  24.    GC gc;
  25.    char line[1024];
  26.    XEvent ev;
  27.    XFontStruct *xfs;
  28.    XCharStruct xcs;
  29.    Font font;
  30.    XSetWindowAttributes att;
  31.    double d;
  32.    char colorful;
  33.    XColor xcl;
  34.    Colormap cmap;
  35.    int cols[256];
  36.    int cnum,r,g,b,fh,x,y,ww,hh;
  37.    
  38.    if (!text) return;
  39.    dd=XOpenDisplay(NULL);
  40.    if (!dd)
  41.      {
  42.          fprintf(stderr,text);
  43.          fflush(stderr);
  44.          return;
  45.      }
  46.    cnum=0;cmap=0;
  47.    colorful=0;
  48.    if (DefaultDepth(dd,DefaultScreen(dd))>4) colorful=1; 
  49.    if (colorful)
  50.      {
  51.          cmap=DefaultColormap(disp,DefaultScreen(dd));
  52.          r=220;g=220;b=220;xcl.red=(r<<8)|r;xcl.green=(g<<8)|g;xcl.blue=(b<<8)|b;
  53.          XAllocColor(dd,cmap,&xcl);cols[cnum++]=xcl.pixel;
  54.          r=160;g=160;b=160;xcl.red=(r<<8)|r;xcl.green=(g<<8)|g;xcl.blue=(b<<8)|b;
  55.          XAllocColor(dd,cmap,&xcl);cols[cnum++]=xcl.pixel;
  56.          r=100;g=100;b=100;xcl.red=(r<<8)|r;xcl.green=(g<<8)|g;xcl.blue=(b<<8)|b;
  57.          XAllocColor(dd,cmap,&xcl);cols[cnum++]=xcl.pixel;
  58.          r=0;  g=0;  b=0;  xcl.red=(r<<8)|r;xcl.green=(g<<8)|g;xcl.blue=(b<<8)|b;
  59.          XAllocColor(dd,cmap,&xcl);cols[cnum++]=xcl.pixel;
  60.          r=255;g=255;b=255;xcl.red=(r<<8)|r;xcl.green=(g<<8)|g;xcl.blue=(b<<8)|b;
  61.          XAllocColor(dd,cmap,&xcl);cols[cnum++]=xcl.pixel;
  62.          r=255;g=80; b=20; xcl.red=(r<<8)|r;xcl.green=(g<<8)|g;xcl.blue=(b<<8)|b;
  63.          XAllocColor(dd,cmap,&xcl);cols[cnum++]=xcl.pixel;
  64.      }
  65.    
  66.    wid=DisplayWidth(dd,DefaultScreen(dd));
  67.    hih=DisplayHeight(dd,DefaultScreen(dd));
  68.    w=(wid-600)/2;h=(hih-440)/2;
  69.    mask=CWBackPixel|CWBorderPixel|CWOverrideRedirect|CWSaveUnder|CWBackingStore;
  70.    if (colorful) att.background_pixel=cols[1];
  71.    else att.background_pixel=BlackPixel(dd,DefaultScreen(dd));
  72.    if (colorful) att.border_pixel=cols[3];
  73.    else att.border_pixel=WhitePixel(dd,DefaultScreen(dd));
  74.    att.backing_store=Always;
  75.    att.save_under=True;
  76.    att.override_redirect=True;
  77.    win=XCreateWindow(dd,DefaultRootWindow(dd),-100,-100,1,1,1,
  78.                             DefaultDepth(dd,DefaultScreen(dd)),InputOutput,
  79.                             DefaultVisual(dd,DefaultScreen(dd)),mask,&att);
  80.    gc=XCreateGC(dd,win,0,&gcv);
  81.    if (colorful) XSetForeground(dd,gc,cols[3]);
  82.    else XSetForeground(dd,gc,att.border_pixel);
  83.    fh=0;
  84.     xfs=XLoadQueryFont(dd,"-*-e-*-*-*-*-17-*-*-*-p-*-*-*");
  85.    if (!xfs)
  86.      {
  87.          xfs=XLoadQueryFont(dd,"fixed");
  88.      }
  89.     font=xfs->fid;
  90.     fh=xfs->ascent+xfs->descent;
  91.    XSetFont(dd,gc,font);
  92.    XSync(dd,False);
  93.    i=rand()%3;
  94.    XMapWindow(dd,win);
  95.    XGrabServer(dd);
  96.    if (i==0)
  97.      {
  98.          for (d=0;d<10;d+=0.4)
  99.           {
  100.               x=(wid>>1)+(int)(((10-d)*(double)wid)*cos(d)/40);
  101.               y=(hih>>1)+(int)(((10-d)*(double)wid)*sin(d)/40);
  102.               ww=(int)((d)*600/10);
  103.               hh=(int)((d)*440/10);
  104.               x-=ww>>1;
  105.               y-=hh>>1;
  106.               XMoveResizeWindow(dd,win,x,y,ww,hh);
  107.               if (colorful)
  108.                 {
  109.                     XClearWindow(disp,win);
  110.                     XSetForeground(dd,gc,cols[0]);
  111.                     XDrawLine(dd,win,gc,0,0,0,hh-2);
  112.                     XDrawLine(dd,win,gc,0,0,ww-2,0);
  113.                     XSetForeground(dd,gc,cols[2]);
  114.                     XDrawLine(dd,win,gc,1,hh-1,600,hh-1);
  115.                     XDrawLine(dd,win,gc,ww-1,1,ww-1,440);
  116.                 }
  117.               XSync(dd,False);
  118.           } 
  119.      }
  120.    else if (i==1)
  121.      {
  122.          ww=600;hh=440;
  123.          XResizeWindow(dd,win,ww,hh);
  124.          for(i=-600;i<(wid-600)>>1;i+=20)
  125.           {      
  126.               x=i;
  127.               y=(hih-440)>>1;
  128.               XMoveWindow(dd,win,x,y);
  129.               if (colorful)
  130.                 {
  131.                     XClearWindow(disp,win);
  132.                     XSetForeground(dd,gc,cols[0]);
  133.                     XDrawLine(dd,win,gc,0,0,0,hh-2);
  134.                     XDrawLine(dd,win,gc,0,0,ww-2,0);
  135.                     XSetForeground(dd,gc,cols[2]);
  136.                     XDrawLine(dd,win,gc,1,hh-1,600,hh-1);
  137.                     XDrawLine(dd,win,gc,ww-1,1,ww-1,440);
  138.                 }
  139.               XSync(dd,False);
  140.           }
  141.      }
  142.    else if (i==2)
  143.      {
  144.          for(i=0;i<600;i+=20)
  145.           {
  146.               ww=i;hh=(i*440)/600;
  147.               x=(wid-ww)>>1;y=(hih-hh)>>1;
  148.               XMoveResizeWindow(dd,win,x,y,ww,hh);
  149.               if (colorful)
  150.                 {
  151.                     XClearWindow(disp,win);
  152.                     XSetForeground(dd,gc,cols[0]);
  153.                     XDrawLine(dd,win,gc,0,0,0,hh-2);
  154.                     XDrawLine(dd,win,gc,0,0,ww-2,0);
  155.                     XSetForeground(dd,gc,cols[2]);
  156.                     XDrawLine(dd,win,gc,1,hh-1,600,hh-1);
  157.                     XDrawLine(dd,win,gc,ww-1,1,ww-1,440);
  158.                 }
  159.               XSync(dd,False);
  160.           }
  161.      }
  162.    ww=600;hh=440;
  163.    x=(wid-600)>>1;y=(hih-440)>>1;
  164.    XMoveResizeWindow(dd,win,x,y,ww,hh);
  165.    XMapWindow(dd,win);
  166.    if (colorful)
  167.      {
  168.          XClearWindow(disp,win);
  169.          XSetForeground(dd,gc,cols[0]);
  170.          XDrawLine(dd,win,gc,0,0,0,hh-2);
  171.          XDrawLine(dd,win,gc,0,0,ww-2,0);
  172.          XSetForeground(dd,gc,cols[2]);
  173.          XDrawLine(dd,win,gc,1,hh-1,600,hh-1);
  174.          XDrawLine(dd,win,gc,ww-1,1,ww-1,440);
  175.      }
  176.    XUngrabServer(dd);
  177.    XSync(dd,False);
  178.    i=0;j=0;k=0;
  179.    while(text[i])
  180.      {
  181.          line[j++]=text[i++];
  182.          if (line[j-1]=='\n')
  183.           {
  184.               line[j-1]=0;j=0;
  185.               if (colorful)
  186.                 {
  187.                     XSetForeground(dd,gc,cols[2]);
  188.                     XDrawString(dd,win,gc,6+1+2,6+k+fh+2,line,strlen(line));
  189.                     XDrawString(dd,win,gc,6+2,6+k+fh+1+2,line,strlen(line));
  190.                     XDrawString(dd,win,gc,6-1+2,6+k+fh+2,line,strlen(line));
  191.                     XDrawString(dd,win,gc,6+2,6+k+fh-1+2,line,strlen(line));
  192.                     XSetForeground(dd,gc,cols[3]);
  193.                     XDrawString(dd,win,gc,6+1,6+k+fh,line,strlen(line));
  194.                     XDrawString(dd,win,gc,6,6+k+fh+1,line,strlen(line));
  195.                     XDrawString(dd,win,gc,6-1,6+k+fh,line,strlen(line));
  196.                     XDrawString(dd,win,gc,6,6+k+fh-1,line,strlen(line));
  197.                     XSetForeground(dd,gc,cols[4]);
  198.                     XDrawString(dd,win,gc,6,6+k+fh,line,strlen(line));
  199.                 }
  200.               else
  201.                  XDrawString(dd,win,gc,6,6+k+fh,line,strlen(line));
  202.               k+=fh;
  203.           }
  204.      }
  205.    XQueryTextExtents(dd,font,"LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62,
  206.                             &w,&i,&j,&xcs);
  207.    h=XTextWidth(xfs,"LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  208.    w=(600-h)/2;
  209.    if (colorful)
  210.      {
  211.          XSetForeground(dd,gc,cols[2]);
  212.          XDrawString(dd,win,gc,w+1+2,433-j+2,
  213.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  214.          XDrawString(dd,win,gc,w+2,433-j+1+2,
  215.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  216.          XDrawString(dd,win,gc,w-1+2,433-j+2,
  217.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  218.          XDrawString(dd,win,gc,w+2,433-j-1+2,
  219.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  220.          XSetForeground(dd,gc,cols[3]);
  221.          XDrawString(dd,win,gc,w+1,433-j,
  222.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  223.          XDrawString(dd,win,gc,w,433-j+1,
  224.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  225.          XDrawString(dd,win,gc,w-1,433-j,
  226.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  227.          XDrawString(dd,win,gc,w,433-j-1, 
  228.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  229.          XSetForeground(dd,gc,cols[5]);
  230.          XDrawString(dd,win,gc,w,433-j,
  231.                          "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  232.          XSetForeground(dd,gc,cols[3]);
  233.          XDrawLine(dd,win,gc,w-4,433-j-i-4,w+h+4,433-j-i-4);
  234.          XDrawLine(dd,win,gc,w-4,433-j-i-4,w-4,433+4);
  235.          XDrawLine(dd,win,gc,w+h+4,433-j-i-4,w+h+4,433+4);
  236.          XDrawLine(dd,win,gc,w-4,433+4,w+h+4,433+4);
  237.          XSetForeground(dd,gc,cols[0]);
  238.          XDrawLine(dd,win,gc,w-3,433-j-i-3,w+h+3,433-j-i-3);
  239.          XDrawLine(dd,win,gc,w-3,433-j-i-3,w-3,433+3);
  240.          XSetForeground(dd,gc,cols[2]);
  241.          XDrawLine(dd,win,gc,w+h+3,433-j-i-3,w+h+3,433+3);
  242.          XDrawLine(dd,win,gc,w-3,433+3,w+h+3,433+3);
  243.      }
  244.    else
  245.      XDrawString(dd,win,gc,w,433-j,
  246.                       "LeftButton: OK     MiddleButton: Restart     RightButton: Quit",62);
  247.    XSync(dd,False);
  248.    XSelectInput(dd,win,ButtonPressMask);
  249.    XWindowEvent(dd,win,ButtonPressMask,&ev);
  250.    XDestroyWindow(dd,win);
  251.    AlertHandleClick(ev.xbutton.button);
  252.    XFreeGC(dd,gc);
  253.    XFreeFont(dd,xfs);
  254.    XUnloadFont(dd,font);
  255.    if (cnum>0) XFreeColors(dd,cmap,(unsigned long *)cols,cnum,0);
  256.    XCloseDisplay(dd);
  257. }
  258.  
  259. void AlertHandleClick(int button)
  260. {
  261.    switch (button)
  262.      {
  263.       case 1:
  264.           printf("left\n");
  265.           break;
  266.       case 2:
  267.           printf("middle\n");
  268.           break;
  269.       case 3:
  270.           printf("right\n");
  271.           break;
  272.       default:
  273.           break;
  274.      }
  275. }
  276.  
  277. void ErrAlert(int erno)
  278. {
  279.    switch (erno)
  280.      {
  281.       case EERR_OUT_OF_MEMORY:
  282.          Alert("***  OUT OF MEMORY  ***\n\n"
  283.                  "The system has run out of memory whilst allocating space to\n"
  284.                  "work in. Enlightenemnt can at times briefly allocate large\n"
  285.                  "chunks of RAM as scratch space.\n"
  286.                  "This could be a bad thing. Please make sure that virtual\n"
  287.                  "memory is enabled, and that there is a sufficient amount of\n"
  288.                  "Swap Space (virtual memory). A good rule of thumb is to\n"
  289.                  "double the REAL RAM on the system and have that amount of\n"
  290.                  "Swap Space. A suggested minimum TOTAL of RAM and Swap to\n"
  291.                  "ensure that this does not happen would be 80Mb.\n\n"
  292.                  "You may also wish to run top or ps (ps -auwx or ps -ef) to\n"
  293.                  "ensure that no runaway process is eating up most of your\n"
  294.                  "system's memeory. If this is the case you should kill it.\n\n"
  295.                  "For more assistance please contact your system administrator.\n");
  296.          EExit(1);
  297.          break;
  298.       case 999:
  299.          break;
  300.       default:
  301.          break;
  302.      }
  303. }
  304.