home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / XARCHIE-.1 / SELECTIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-22  |  5.0 KB  |  223 lines

  1. /*
  2.  * selection.c : Code that the X and Curses browsers use, but that
  3.  *    others might be able to do without, or do better.
  4.  *
  5.  * Note that we need to maintain the selections for those levels
  6.  * of the browser that aren't displayed right now. Again, you may
  7.  * get better mileage from your window system.
  8.  *
  9.  * George Ferguson, ferguson@cs.rochester.edu, 23 Apr 1993.
  10.  */
  11. #include <stdio.h>
  12. #include "xtypes.h"
  13. #include "sysdefs.h"
  14. #include "db.h"
  15. #include "display.h"
  16. #include "browser.h"
  17. #include "selection.h"
  18. #include "debug.h"
  19.  
  20. /*
  21.  * Functions defined here:
  22.  */
  23. void resetSelections();
  24. void redrawSelectionsForPane(),resetSelectionsForPane();
  25. void addSelection(),addSelectionInPane(),removeSelection();
  26. SelectedItem *getSelection();
  27. Boolean isSelected(),isSelectedInPane();
  28. Boolean hasSelection(),hasSelectionInPane();
  29. void forEachSelectedItemAtDepth(),forEachSelectedItem();
  30.  
  31. /*
  32.  * Data defined here:
  33.  */
  34. static SelectedItem *selectedItems[MAX_DEPTH];
  35. static int deepest;
  36.  
  37. /*    -    -    -    -    -    -    -    -    */
  38.  
  39. void
  40. resetSelections(depth)
  41. int depth;
  42. {
  43.     SelectedItem *item,*nextItem;
  44.     int i;
  45.  
  46.     DEBUG1("clearing selections for depth >= %d\n",depth);
  47.     for (i=depth; i < MAX_DEPTH; i++) {
  48.     for (item=selectedItems[i]; item != NULL; item = nextItem) {
  49.         nextItem = item->next;
  50.         XtFree((char *)item);
  51.     }
  52.     selectedItems[i] = NULL;
  53.     }
  54.     deepest = depth-1;
  55.     DEBUG1("deepest now = %d\n",deepest);
  56. }
  57.  
  58. void
  59. redrawSelectionsForPane(pane)
  60. int pane;
  61. {
  62.     SelectedItem *item;
  63.  
  64.     DEBUG1("redrawing selections for pane %d\n",pane);
  65.     for (item=selectedItems[paneDepth(pane)]; item != NULL;
  66.      item=item->next) {
  67.     highlightBrowserItem(pane,item->list_index);
  68.     }
  69. }
  70.  
  71. void
  72. resetSelectionsForPane(pane)
  73. int pane;
  74. {
  75.     DEBUG1("clearing selections for pane %d\n",pane);
  76.     resetSelections(paneDepth(pane));
  77. }
  78.  
  79. /*    -    -    -    -    -    -    -    -    */
  80.  
  81. void
  82. addSelection(depth,dbp,list_index)
  83. int depth;
  84. DbEntry *dbp;
  85. int list_index;
  86. {
  87.     SelectedItem *next;
  88.  
  89.     DEBUG3("adding selection \"%s\"(0x%x) at depth %d\n",dbp->name,dbp,depth);
  90.     next = selectedItems[depth];
  91.     selectedItems[depth] = XtNew(SelectedItem);
  92.     selectedItems[depth]->next = next;
  93.     if (next != NULL)
  94.     next->prev = selectedItems[depth];
  95.     selectedItems[depth]->entry = dbp;
  96.     selectedItems[depth]->list_index = list_index;
  97.     dbp->selected = depth;
  98.     if (depth > deepest) {
  99.     deepest = depth;
  100.     DEBUG1("deepest now = %d\n",deepest);
  101.     }
  102. }
  103.  
  104. void
  105. addSelectionInPane(pane,dbp,list_index)
  106. int pane;
  107. DbEntry *dbp;
  108. int list_index;
  109. {
  110.     DEBUG3("adding selection \"%s\"(0x%x) in pane %d\n",dbp->name,dbp,pane);
  111.     addSelection(paneDepth(pane),dbp,list_index);
  112. }
  113.  
  114. void
  115. removeSelection(depth,dbp,list_index)
  116. int depth;
  117. DbEntry *dbp;
  118. int list_index;
  119. {
  120.     SelectedItem *item;
  121.  
  122.     DEBUG3("removing selection \"%s\"(0x%x) at depth %d\n",
  123.        (dbp?dbp->name:"<NIL>"),dbp,depth);
  124.     for (item=selectedItems[depth]; item != NULL; item = item->next)
  125.     if ((dbp == NULL || item->entry == dbp) &&
  126.         (list_index == -1  || item->list_index == list_index))
  127.         break;
  128.     if (item == NULL) {
  129.     fprintf(stderr,"removeSelection: Can't find entry 0x%x (list_index=%d) at depth %d\n",dbp,list_index,depth);
  130.     return;
  131.     }
  132.     if (item == selectedItems[depth]) {
  133.     selectedItems[depth] = item->next;
  134.     if (item->next != NULL)
  135.         item->next->prev = NULL;
  136.     } else {
  137.     item->prev->next = item->next;
  138.     if (item->next != NULL)
  139.         item->next->prev = item->prev;
  140.     }
  141.     XtFree((char *)item);
  142.     if (selectedItems[depth] == NULL) {
  143.     deepest = depth-1;
  144.     DEBUG1("deepest now = %d\n",deepest);
  145.     }
  146. }
  147.  
  148. SelectedItem *
  149. getSelection(depth)
  150. int depth;
  151. {
  152.     return(selectedItems[depth]);
  153. }
  154.  
  155. /*    -    -    -    -    -    -    -    -    */
  156.  
  157. Boolean
  158. isSelected(depth,list_index)
  159. int depth,list_index;
  160. {
  161.     SelectedItem *item;
  162.  
  163.     for (item=selectedItems[depth];
  164.      item != NULL && item->list_index != list_index; item=item->next)
  165.     /*EMPTY*/;
  166.     return(item != NULL);
  167. }
  168.  
  169. Boolean
  170. isSelectedInPane(pane,list_index)
  171. int pane,list_index;
  172. {
  173.     return(isSelected(paneDepth(pane),list_index));
  174. }
  175.  
  176. Boolean
  177. hasSelection(depth)
  178. int depth;
  179. {
  180.     return(selectedItems[depth] != NULL);
  181. }
  182.  
  183. Boolean
  184. hasSelectionInPane(pane)
  185. int pane;
  186. {
  187.     return(hasSelection(paneDepth(pane)));
  188. }
  189.  
  190. /*    -    -    -    -    -    -    -    -    */
  191. /*
  192.  * Calls "func" for each selected item at depth "depth", passing DbEntry
  193.  * and list_index.
  194.  */
  195. void
  196. forEachSelectedItemAtDepth(depth,func)
  197. int depth;
  198. void (*func)();
  199. {
  200.     SelectedItem *item;
  201.  
  202.     for (item=selectedItems[depth]; item != NULL; item=item->next)
  203.     (*func)(item->entry,item->list_index);
  204. }
  205.  
  206. /*
  207.  * Like above, but calls "func" for each item selected at the deepest
  208.  * level.
  209.  */
  210. void
  211. forEachSelectedItem(func)
  212. void (*func)();
  213. {
  214.     SelectedItem *item;
  215.  
  216.     if (deepest == -1) {
  217.     DEBUG0("forEachSelectedItem: nothing selected\n");
  218.        return;
  219.     }
  220.     for (item=selectedItems[deepest]; item != NULL; item=item->next)
  221.     (*func)(item->entry,item->list_index);
  222. }
  223.