home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / programming / c / oui / listview.cc < prev    next >
C/C++ Source or Header  |  1998-04-23  |  5KB  |  182 lines

  1. // $Id: listview.cc 1.4 1998/01/13 20:01:07 dlorre Exp dlorre $
  2. #include <exec/lists.h>
  3. #include <exec/libraries.h>
  4. #include <libraries/gadtools.h>
  5.  
  6. #include "gadgets/listview.h"
  7. #include "gadgetlist.h"
  8. #include "slist.h"
  9. #include <proto/exec.h>
  10. #include <proto/gadtools.h>
  11. #include <clib/alib_protos.h>
  12.  
  13. // ========================================================================
  14. // ==========================  LIST CLASS =================================
  15. // ========================================================================
  16.  
  17. listview::listview(gadgetlist *gl,
  18.            void (window::*func)(gadget *, unsigned long, unsigned short),
  19.            const char *title,
  20.            unsigned long place,
  21.            nlist *liste,
  22.            unsigned short top,
  23.            unsigned short sel,
  24.            short readonly,
  25.            unsigned short swidth,
  26.            Gadget *showsel) : gadget(gl, func)
  27. {
  28. BOOL badinit = FALSE ;
  29. nlink_iter it(liste) ;
  30.  
  31.     underkey(title) ;
  32.     count = 0 ;
  33.     l = new List ;
  34.     if (l) {
  35.         NewList(l) ;
  36.         while ((sl = it++) && !badinit) {
  37.             if (n = new Node) {
  38.                 n->ln_Name = sl->label ;
  39.                 AddTail(l, n) ;
  40.                 count++ ;
  41.             }
  42.             else
  43.                 badinit = TRUE ;
  44.         }
  45.         if (badinit) {
  46.             n = l->lh_Head ;
  47.             while (n->ln_Succ) {
  48.                 c = n->ln_Succ ;
  49.                 Remove(n) ;
  50.                 delete n ;
  51.                 n = c ;
  52.             }
  53.             delete l ;
  54.             l = NULL ;
  55.         }
  56.     }
  57.     if (l) {
  58.         if (sel != ~0) cursel = sel ; else cursel = -1 ;
  59.         curtop = top ;
  60.         gl->ng->ng_Flags = place ;
  61.         gl->ng->ng_GadgetText = (UBYTE *)title ;
  62.         gad = gl->gad = CreateGadget(LISTVIEW_KIND, gl->gad, gl->ng,
  63.             GTLV_Labels,        l,
  64.             GTLV_Top,           top,
  65.             GTLV_MakeVisible,   top,
  66.             GTLV_Selected,      sel,
  67.             GTLV_ReadOnly,      readonly,
  68.             GTLV_ScrollWidth,   swidth,
  69.             GTLV_ShowSelected,  showsel,
  70.             GT_Underscore,  '_',
  71.             TAG_END) ;
  72.     }
  73. }
  74.  
  75. listview::~listview()
  76. {
  77.     if (l) {
  78.         n = l->lh_Head ;
  79.         while (n->ln_Succ) {
  80.             c = n->ln_Succ ;
  81.             Remove(n) ;
  82.             delete n ;
  83.             n = c ;
  84.         }
  85.         delete l ;
  86.         l = NULL ;
  87.     }
  88. }
  89.  
  90. void listview::set(unsigned short top, unsigned short sel)
  91. {
  92.     if (top != -1) curtop = top ;
  93.     cursel = sel ;
  94.     GT_SetGadgetAttrs(gad, w, NULL,
  95.         GTLV_Top,           curtop,
  96.         GTLV_MakeVisible,   curtop,
  97.         GTLV_Selected,      cursel,
  98.         TAG_DONE) ;
  99. }
  100.  
  101. void listview::reset(nlist *liste, unsigned short top, unsigned short sel)
  102. {
  103. BOOL badinit = FALSE ;
  104. nlink_iter it(liste) ;
  105.  
  106.     count = 0 ;
  107.     GT_SetGadgetAttrs(gad, w, NULL,
  108.         GTLV_Labels,    ~0,
  109.         TAG_DONE) ;
  110.     if (l) {
  111.         n = l->lh_Head ;
  112.         while (n->ln_Succ) {
  113.             c = n->ln_Succ ;
  114.             Remove(n) ;
  115.             delete n ;
  116.             n = c ;
  117.         }
  118.         delete l ;
  119.         l = NULL ;
  120.     }
  121.     l = new List ;
  122.     if (l) {
  123.         NewList(l) ;
  124.         while ((sl = it++) && !badinit) {
  125.             if (n = new Node) {
  126.                 n->ln_Name = sl->label ;
  127.                 AddTail(l, n) ;
  128.                 count++ ;
  129.             }
  130.             else
  131.                 badinit = TRUE ;
  132.         }
  133.         if (badinit) {
  134.             n = l->lh_Head ;
  135.             while (n->ln_Succ) {
  136.                 c = n->ln_Succ ;
  137.                 Remove(n) ;
  138.                 delete n ;
  139.                 n = c ;
  140.             }
  141.             delete l ;
  142.             l = NULL ;
  143.         }
  144.     }
  145.     if (l) {
  146.         if (top != -1) curtop = top ;
  147.         if (sel != ~0) cursel = sel ; else cursel = -1 ;
  148.         GT_SetGadgetAttrs(gad, w, NULL,
  149.             GTLV_Labels,        l,
  150.             GTLV_Top,           top,
  151.             GTLV_MakeVisible,   top,
  152.             GTLV_Selected,      sel,
  153.             TAG_DONE) ;
  154.     }
  155. }
  156.  
  157.  
  158.  
  159. void listview::keystroke(BOOL shifted)
  160. {
  161.     if (shifted) {
  162.         if (cursel)
  163.             cursel-- ;
  164.     }
  165.     else {
  166.         if (cursel < count-1)
  167.             cursel++ ;
  168.     }
  169.     if (GadToolsBase->lib_Version >= 39) {
  170.         GT_GetGadgetAttrs(gad, w, NULL,
  171.             GTLV_Top,       &curtop,
  172.             TAG_DONE) ;
  173.     }
  174.     set(curtop, cursel) ;
  175.     gadget::action(NULL, cursel) ;
  176. }
  177. void listview::action(unsigned long classe, unsigned short code)
  178. {
  179.     cursel = code ;
  180.     gadget::action(classe, code) ;
  181. }
  182.