home *** CD-ROM | disk | FTP | other *** search
/ Amiga Inside! / Amiga FD Inside (1995)(Ultramax).iso / berndspd / devtools / precognition / src / library / scrollinglist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-16  |  10.9 KB  |  371 lines

  1. #include <string.h>
  2. #include "minmax.h"
  3. #include "ScrollingList.h"
  4. #include "ScrollingListClass.h"
  5. #include "EmbossedGadgetClass.h"
  6. #include "precognition.h"
  7. #ifndef __GNUC__
  8. #include <clib/exec_protos.h>
  9. #include <clib/intuition_protos.h>
  10. #include <clib/graphics_protos.h>
  11. #endif
  12. #ifdef __GNUC__
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15. #include <proto/graphics.h>
  16. #endif
  17. #ifdef __SASC
  18. #include <proto/exec.h>
  19. #include <proto/intuition.h>
  20. #include <proto/graphics.h>
  21. #endif
  22. #include "amigamem.h"
  23.  
  24. #define CHAR_WIDTH  8   /* Width  of Topaz 80 font. */
  25. #define CHAR_HEIGHT 9   /* Height of Topaz 80 font. */
  26. #define BROWSER_ENTRY_HEIGHT (CHAR_HEIGHT + 1)
  27. #define BORDER_WIDTH 3
  28. #define BASELINE    7
  29.  
  30.  
  31. void ScrollingList_SetKnobInfo( ScrollingList *self )
  32. {
  33.    USHORT position, knobsize, nElts, nRows;
  34.  
  35.    nElts = self->list.List.nEntries;
  36.  
  37.    if( nElts == 0 )
  38.    {
  39.       position = 0;
  40.       knobsize = 0xFFFF;
  41.    }
  42.    else
  43.    {
  44.       nRows = self->list.nRows;
  45.  
  46.       position  = ( (ULONG) self->list.YOffset << 16 ) / nElts;
  47.  
  48.       if( nElts <= nRows )
  49.       {
  50.          knobsize = 0xFFFF;
  51.       }
  52.       else
  53.       {
  54.          knobsize = ( (ULONG) nRows << 16 ) / nElts;
  55.       }
  56.    }
  57.    SetKnobSize( (Positioner *)&self->vscroller, knobsize );
  58.    SetValue( (Valuator *)&self->vscroller, position );
  59. }
  60.  
  61.  
  62. void ScrollingList_PositionList( ScrollingList *self )
  63. {
  64.    USHORT position,     /* Position of self->vscroller.   */
  65.           new_YOffset,  /* new list YOffset.              */
  66.           nElts,        /* # of elements in list.         */
  67.           nRows,        /* # of rows in display           */
  68.           range;        /* range of positions for YOffset */
  69.  
  70.    ULONG temp;
  71.  
  72.    nElts    = self->list.List.nEntries;
  73.  
  74.    if( nElts )
  75.    {
  76.       nRows    = self->list.nRows;
  77.  
  78.       if( ( range = nElts - nRows +1 ) > 0 )
  79.       {
  80.          position = Value( (Valuator *)&self->vscroller );
  81.  
  82.          temp = position * range;
  83.          new_YOffset = temp >> 16;
  84.          new_YOffset = MIN( new_YOffset, nElts -1 );
  85.  
  86.          if( new_YOffset != self->list.YOffset )
  87.          {
  88.             self->list.YOffset = new_YOffset;
  89.             Refresh( (Interactor *)&self->list );
  90.          }
  91.       }
  92.    }
  93. }
  94.  
  95.  
  96. void ScrollingList_Init(  ScrollingList *self,
  97.                           PIXELS         LeftEdge,
  98.                           PIXELS         TopEdge,
  99.                           PIXELS         Width,
  100.                           PIXELS         Height,
  101.                           pcg_3DPens     Pens,
  102.                           BOOL           SelectMany )
  103. {
  104.    Gadget *g;
  105.  
  106.    StringLister_Init( (StringLister *)self );
  107.    self->slister.isa  = ScrollingListClass();
  108.    self->Pens         = Pens;
  109.  
  110.    ListBrowser_Init( &self->list, 0,0, 0,0, Pens, SelectMany );
  111.  
  112.    VScroller_Init( &self->vscroller, 0,0, Height, Pens, NULL );
  113.  
  114.    for ( g = FirstGadget( (Interactor *)&self->list );
  115.          g->NextGadget != NULL;
  116.          g = g->NextGadget );
  117.  
  118.    g->NextGadget = FirstGadget( (Interactor *)&self->vscroller );
  119.    self->list.eg.Next = &self->vscroller;
  120.  
  121.    SetSize( (GraphicObject *)self, Width, Height );
  122.    SetLocation( (GraphicObject *)self, LeftEdge, TopEdge );
  123. }
  124.  
  125. void ScrollingList_CleanUp( ScrollingList *self )
  126. {
  127.    CleanUp( (PObject *)&self->list );
  128.    CleanUp( (PObject *)&self->vscroller );
  129. }
  130.  
  131. USHORT ScrollingList_nGadgets( ScrollingList *self )
  132. {
  133.    return( (USHORT)( nGadgets( (Interactor *)&self->list ) +
  134.                      nGadgets( (Interactor *)&self->vscroller ) ) );
  135. }
  136.  
  137. Gadget *ScrollingList_FirstGadget( ScrollingList *self )
  138. {
  139.    return FirstGadget( (Interactor *)&self->list );
  140. }
  141.  
  142. Point ScrollingList_AskSize( ScrollingList *self,
  143.                              PIXELS         Width,
  144.                              PIXELS         Height )
  145. {
  146.    Point size, list_size, vscroller_size;
  147.  
  148.    vscroller_size = AskSize( (GraphicObject *)&self->vscroller, Width, Height );
  149.    /* vscroller's have a fixed width */
  150.  
  151.    list_size.x = Width - vscroller_size.x;
  152.    list_size.y = vscroller_size.y;
  153.  
  154.    list_size = AskSize( (GraphicObject *)&self->list, list_size.x, list_size.y );
  155.  
  156.    size.x = list_size.x + vscroller_size.x;
  157.    size.y = list_size.y;
  158.  
  159.    return size;
  160. }
  161.  
  162.  
  163. Point ScrollingList_SetSize( ScrollingList *self,
  164.                               PIXELS       Width,
  165.                               PIXELS       Height )
  166. {
  167.    Point size, list_size, vscroller_size;
  168.  
  169.    size = AskSize( (GraphicObject *)self, Width, Height );
  170.  
  171.    vscroller_size = SetSize( (GraphicObject *)&self->vscroller, size.x, size.y );
  172.    list_size  = SetSize( (GraphicObject *)&self->list, size.x - vscroller_size.x, size.y );
  173.    self->slister.Size = size;
  174.  
  175.    SetLocation( (GraphicObject *)self, self->slister.Location.x, self->slister.Location.y );
  176.    ScrollingList_SetKnobInfo( self );
  177.  
  178.    return size;
  179. }
  180.  
  181. Point ScrollingList_SetLocation( ScrollingList *self,
  182.                                   PIXELS         LeftEdge,
  183.                                   PIXELS         TopEdge )
  184. {
  185.    Point size, location;
  186.  
  187.    size = Size( (GraphicObject *)&self->list );
  188.    SetLocation( (GraphicObject *)&self->list, LeftEdge, TopEdge );
  189.    SetLocation( (GraphicObject *)&self->vscroller, LeftEdge+size.x, TopEdge );
  190.  
  191.    location.x             = LeftEdge;
  192.    location.y             = TopEdge;
  193.    self->slister.Location = location;
  194.  
  195.    return location;
  196. }
  197.  
  198.  
  199. USHORT ScrollingList_SetYOffset( ScrollingList *self,
  200.                                  USHORT         YOffset )
  201. {
  202.    return ListBrowser_SetYOffset( &self->list, YOffset );
  203. }
  204.  
  205.  
  206. void ScrollingList_Render( ScrollingList  *self,
  207.                            RastPort       *RPort )
  208. {
  209.    Render( (GraphicObject *)&self->list,       RPort );
  210.    Render( (GraphicObject *)&self->vscroller,  RPort );
  211. }
  212.  
  213.  
  214. void ScrollingList_SelectAll( ScrollingList *self, BOOL Select )
  215. /* Selects or deselects everything in the list. */
  216. {
  217.    ListBrowser_SelectAll( &self->list, Select );
  218. }
  219.  
  220.  
  221. const StringList *ScrollingList_StringList_of( ScrollingList *self )
  222. {
  223.    return StringList_of( (StringLister *)&self->list );
  224. }
  225.  
  226. /* Changed to BOOL from void -- EDB */
  227. BOOL ScrollingList_AddString( ScrollingList  *self,
  228.                               char           *entry,
  229.                               UBYTE           qualifier )
  230. {
  231.    BOOL test = FALSE;
  232.    test = AddString( (StringLister *)&self->list, entry, qualifier );
  233.    ScrollingList_SetKnobInfo( self );
  234.    return test;  /* added return -- EDB */
  235. }
  236.  
  237.  
  238. /* Changed to BOOL from void -- EDB */
  239. BOOL ScrollingList_DeleteString( ScrollingList  *self,
  240.                                  USHORT          entry )
  241. {
  242.    BOOL test = FALSE;
  243.    test = DeleteString( (StringLister *)&self->list, entry );
  244.    ScrollingList_SetKnobInfo( self );
  245.    return test;  /* added return -- EDB */
  246. }
  247.  
  248. /* Changed to BOOL from void -- EDB */
  249. BOOL ScrollingList_DeleteAllStrings( ScrollingList  *self )
  250. {
  251.    BOOL test = FALSE;
  252.    test = DeleteAllStrings( (StringLister *)&self->list );
  253.    ScrollingList_SetKnobInfo( self );
  254.    return test;  /* added return -- EDB */
  255. }
  256.  
  257. void ScrollingList_SelectString( ScrollingList  *self,
  258.                                  USHORT          entry,
  259.                                  BOOL            Select   )
  260. {
  261.    SelectString( (StringLister *)&self->list, entry, Select );
  262. }
  263.  
  264.  
  265. void ScrollingList_Refresh( Interactor *us )
  266. {
  267.    ScrollingList *self = NULL;
  268.    self = (ScrollingList *)us;
  269.    Refresh( (Interactor *)&self->list      );
  270.    Refresh( (Interactor *)&self->vscroller );
  271. }
  272.  
  273. USHORT ScrollingList_ClaimEvent(  ScrollingList  *self,
  274.                                  IntuiMessage   *event )
  275. {
  276.    return( (USHORT)( ClaimEvent( (Interactor *)&self->list,event )
  277.         | ClaimEvent( (Interactor *)&self->vscroller, event ) ) );
  278. }
  279.  
  280.  
  281. #define CLAIM_EVENT(b,e) ( &b->g == (Gadget *)e->IAddress )
  282.  
  283. USHORT ScrollingList_Respond( ScrollingList  *self,
  284.                               IntuiMessage   *Event )
  285. {
  286.    USHORT        list_response;
  287.    USHORT        vscroller_response = 0;
  288.  
  289.    list_response = Respond( (Interactor *)&self->list, Event );
  290.  
  291.    if( ! ( list_response & CONSUMED_EVENT ) )
  292.    {
  293.       vscroller_response = Respond( (Interactor *)&self->vscroller, Event );
  294.       if( vscroller_response & CHANGED_STATE )
  295.       {
  296.          ScrollingList_PositionList( self );
  297.          /*  scroll list */
  298.          vscroller_response = RESPONDED; /* Don't propagate the CHANGED_STATE. */
  299.       }
  300.    }
  301.    return( (USHORT)( list_response | vscroller_response ) );
  302. }
  303.  
  304.  
  305. void ScrollingList_SetInteractorWindow( ScrollingList *self,
  306.                                         pcgWindow     *window )
  307. {
  308.    self->slister.IaWindow = window;
  309.    SetInteractorWindow( (Interactor *)&self->list, window );
  310.    SetInteractorWindow( (Interactor *)&self->vscroller, window );
  311. }
  312.  
  313. ULONG ScrollingList_IDCMPFlags( ScrollingList *self )
  314. {
  315.    return IDCMPFlags( (Interactor *)&self->list ) |
  316.           IDCMPFlags( (Interactor *)&self->vscroller );
  317. }
  318.  
  319. BOOL ScrollingList_EnableIactor( ScrollingList *self, BOOL enable )
  320. {
  321.    EnableIactor( (Interactor *)&self->list,      enable );
  322.    EnableIactor( (Interactor *)&self->vscroller, enable );
  323.    return enable;
  324. }
  325.  
  326.  
  327. BOOL ScrollingList_elaborated = FALSE;
  328.  
  329. struct StringListerClass ScrollingList_Class;
  330.  
  331. void ScrollingListClass_Init( struct StringListerClass *class )
  332. {
  333.    StringListerClass_Init( class );
  334.    class->isa         = StringListerClass();
  335.    class->CleanUp     = (void(*)(PObject *))ScrollingList_CleanUp;
  336.    class->ClassName   = "ScrollingList";
  337.    class->AskSize     = (Point(*)(GraphicObject *, PIXELS, PIXELS))ScrollingList_AskSize;
  338.    class->SetSize     = (Point(*)(GraphicObject *, PIXELS, PIXELS))ScrollingList_SetSize;
  339.    class->SetLocation = (Point(*)(GraphicObject *, PIXELS, PIXELS))ScrollingList_SetLocation;
  340.    class->Respond     = (USHORT(*)(Interactor *, IntuiMessage *))ScrollingList_Respond;
  341.    class->Refresh     = (void(*)(Interactor *))ScrollingList_Refresh;
  342.    class->Render      = (void(*)(GraphicObject *, RastPort *))ScrollingList_Render;
  343.    class->ClaimEvent  = (USHORT(*)(Interactor *, IntuiMessage *))ScrollingList_ClaimEvent;
  344.    class->nGadgets    = (USHORT(*)(Interactor *))ScrollingList_nGadgets;
  345.    class->FirstGadget = (Gadget*(*)(Interactor *))ScrollingList_FirstGadget;
  346.    class->SetInteractorWindow = (void(*)(Interactor *, struct pcgWindow *))ScrollingList_SetInteractorWindow;
  347.    class->IDCMPFlags  = (ULONG(*)(Interactor *))ScrollingList_IDCMPFlags;
  348.    class->isEnabled   = (BOOL(*)(Interactor *))EmbossedGadget_isEnabled;
  349.    class->EnableIactor  = (BOOL(*)(Interactor *, BOOL))ScrollingList_EnableIactor;
  350.  
  351.    class->StringList_of = (StringList*(*)(StringLister *))ScrollingList_StringList_of;
  352.    class->AddString     = (BOOL(*)(StringLister *, char *, UBYTE))ScrollingList_AddString;
  353.    class->DeleteString  = (BOOL(*)(StringLister *, USHORT))ScrollingList_DeleteString;
  354.    class->DeleteAllStrings = (BOOL(*)(StringLister *))ScrollingList_DeleteAllStrings;
  355.    class->SelectString  = (void(*)(StringLister *, USHORT, BOOL))ScrollingList_SelectString;
  356.  
  357. }
  358.  
  359.  
  360.  
  361. struct StringListerClass *ScrollingListClass( void )
  362. {
  363.    if( ! ScrollingList_elaborated )
  364.    {
  365.       ScrollingListClass_Init( &ScrollingList_Class );
  366.       ScrollingList_elaborated = TRUE;
  367.    }
  368.  
  369.    return &ScrollingList_Class;
  370. }
  371.