home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume13 / xfbrowse / part02 / list.c < prev    next >
C/C++ Source or Header  |  1993-05-04  |  3KB  |  157 lines

  1.  
  2. #include "xfbrows.h"
  3. #include "bitmaps.h"
  4.  
  5. #define DBLCLKTIME 500
  6. #define LISTW        330
  7.  
  8. #define INACTIVE(lptr, item) ((lptr)->filetypes && (lptr)->dirsonly && \
  9.                   (item) >= 0 && (item) < (lptr)->nstr && \
  10.                   (lptr)->str[(item)][0] != C_DIR && \
  11.                   (lptr)->str[(item)][0] != C_LNK)
  12.  
  13. void LSCreate (lp, win, x, y, w, h, nlines, strlist, nstr, fg, bg, fptr, 
  14.     typ, donly)
  15. LIST *lp;
  16. Window win;
  17. int x, y, w, h, nlines, nstr, typ, donly;
  18. unsigned long fg, bg;
  19. char **strlist;
  20. void (*fptr) ();
  21. {
  22.     lp->win = XCreateSimpleWindow (theDisp, win, x, y, w, h, 1, infofg, infobg);
  23.     if (!lp->win) FatalError ("can't create list window!");
  24.  
  25.     lp->x = x;
  26.     lp->y = y; 
  27.     lp->w = w;
  28.     lp->h = h;
  29.     lp->fg = fg;
  30.     lp->bg = bg;
  31.     lp->str = strlist;
  32.     lp->nstr = nstr;
  33.     lp->selected = 0;
  34.     lp->nlines = nlines;
  35.     lp->filetypes= typ;
  36.     lp->dirsonly = donly;
  37.  
  38.     XSelectInput (theDisp, lp->win, ExposureMask | ButtonPressMask
  39.         | ButtonReleaseMask | Button1MotionMask | EnterWindowMask
  40.         | LeaveWindowMask);
  41.  
  42.     SCCreate (&lp->scrl, win, x+w, y, 1, h, 0, nstr-nlines, curname, nlines-1, 
  43.         fg, bg, fptr);
  44. }
  45.  
  46. void LSNewData (lp, strlist, nstr)
  47. LIST *lp;
  48. char **strlist;
  49. int nstr;
  50. {
  51.     lp->str = strlist;
  52.     lp->nstr = nstr;
  53.     lp->selected = 0;
  54.     SCSetRange (&lp->scrl, 0, nstr - lp->nlines, 0, lp->nlines-1);
  55. }
  56.  
  57. static void drawSel (lp, j)
  58. LIST *lp;
  59. int j;
  60. {
  61.     int i, k, inactive;
  62.     unsigned long fg, bg;
  63.     char s[2];
  64.  
  65.     inactive = INACTIVE (lp, j);
  66.  
  67.     i = j - lp->scrl.val;
  68.     if (i<0 || i>=lp->nlines) return;
  69.  
  70.     if (j == lp->selected && !inactive && j<lp->nstr && !isfont)
  71.     {
  72.         fg = lp->bg;
  73.         bg = lp->fg;
  74.     }
  75.     else
  76.     {
  77.         fg = lp->fg;
  78.         bg = lp->bg;
  79.     }
  80.  
  81.     XSetForeground (theDisp, theGC, bg);
  82.     XFillRectangle (theDisp, lp->win, theGC, 0, i*LINEHIGH, lp->w, LINEHIGH);
  83.  
  84.     if (j>=0 && j<lp->nstr)
  85.     {
  86.         XSetForeground (theDisp, theGC, fg);
  87.         XSetBackground (theDisp, theGC, bg);
  88.  
  89.         if (!lp->filetypes) 
  90.             XDrawString (theDisp, lp->win, theGC, 3, i*LINEHIGH + ASCENT + 1, 
  91.                 lp->str [j], strlen (lp->str [j]));
  92.         else
  93.         {
  94.             if (isfont)
  95.                 for (k=0; k<16; k++)
  96.                 {
  97.                     s[0] = lp->str [j][k];
  98.                     s[1] = '\0';
  99.                     XDrawString (theDisp, lp->win, theGC, 3+k*WIDTH, 
  100.                         i*LINEHIGH + ASCENT + 1, 
  101.                         s, strlen (s));
  102.                 }
  103.             else
  104.             XDrawString (theDisp, lp->win, theGC, 6, 
  105.                 i*LINEHIGH + ASCENT + 1, 
  106.                 lp->str [j], strlen (lp->str [j]));
  107.         }
  108.     }
  109. }
  110.  
  111. void LSRedraw (lp)
  112. LIST *lp;
  113. {
  114.     int i;
  115.  
  116.     for (i = lp->scrl.val;i < lp->scrl.val + lp->nlines;i++) drawSel (lp, i);
  117. }
  118.  
  119. int LSClick (lp, ev)
  120. LIST *lp;
  121. XButtonEvent *ev;
  122. {
  123.     Window rW, cW;
  124.     int rx, ry, x, y, sel, oldsel;
  125.     unsigned int mask;
  126.     static Time lasttime=0;
  127.     static int lastsel = -1;
  128.  
  129.     x = ev->x;
  130.     y = ev->y;
  131.     sel = lp->scrl.val + y/LINEHIGH;
  132.     if (sel >= lp->nstr) sel = lp->selected;
  133.  
  134.     if (ev->time - lasttime < DBLCLKTIME && sel==lastsel 
  135.         && (lp->scrl.val + y/LINEHIGH) < lp->nstr
  136.         && !INACTIVE (lp, sel)) return (sel);
  137.  
  138.     lasttime = ev->time;lastsel = sel;
  139.  
  140.     if (sel != lp->selected)
  141.     {
  142.         oldsel = lp->selected;
  143.         lp->selected = sel;
  144.         drawSel (lp, sel);
  145.         drawSel (lp, oldsel);
  146.         XFlush (theDisp);
  147.     }
  148.  
  149.     while (XQueryPointer (theDisp, lp->win, &rW, &cW, &rx, &ry, &x, &y, &mask))
  150.     {
  151.         if (! (mask & Button1Mask)) break;
  152.     }
  153.  
  154.     return (sel);
  155. }
  156.  
  157.