home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / bgui-1.1a.lha / BGUI / demos / multicol.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  36KB  |  844 lines

  1. ;/*
  2. dcc MultiCol.c -proto -mi -ms -mRR -lbgui
  3. quit
  4. */
  5. /*
  6. **         $RCSfile: multicol.c,v $
  7. **      Description: Shows you how to code a multi-column listview
  8. **                   object using the hooks that the listview class
  9. **                   provide.
  10. **        Copyright: (C) Copyright 1994 Jaba Development.
  11. **                   (C) Copyright 1994 Jan van den Baard.
  12. **                   All Rights Reserved.
  13. **
  14. **          $Author: jaba $
  15. **        $Revision: 1.3 $
  16. **            $Date: 1994/11/20 13:06:11 $
  17. **/
  18.  
  19. #include <exec/types.h>
  20. #include <exec/memory.h>
  21. #include <dos/dos.h>
  22. #include <dos/exall.h>
  23. #include <libraries/bgui.h>
  24. #include <libraries/bgui_macros.h>
  25. #include <utility/hooks.h>
  26.  
  27. #include <clib/alib_protos.h>
  28. #include <clib/macros.h>
  29.  
  30. #include <proto/exec.h>
  31. #include <proto/intuition.h>
  32. #include <proto/graphics.h>
  33. #include <proto/dos.h>
  34. #include <proto/bgui.h>
  35.  
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <stdio.h>
  39.  
  40. /*
  41. **      SAS<->DICE muck.
  42. **/
  43. #ifdef _DCC
  44. #define SAVEDS __geta4
  45. #define ASM
  46. #define REG(x) __ ## x
  47. #else
  48. #define SAVEDS __saveds
  49. #define ASM __asm
  50. #define REG(x) register __ ## x
  51. #endif
  52.  
  53. /*
  54. **      This is the data were going to add
  55. **      to the listview object. It is a simple
  56. **      structure in which the file-information
  57. **      is stored. This data is created in the
  58. **      LISTV_Resource hook from a pointer
  59. **      to a ExAllData structure.
  60. **/
  61. typedef struct {
  62.         UBYTE                   fi_FileName[ 108 ];
  63.         UBYTE                   fi_Size[ 11 ];
  64.         UBYTE                   fi_Date[ 32 ];
  65.         BOOL                    fi_IsDir;
  66. } FILEINFO;
  67.  
  68. /*
  69. **      The LISTV_Resource hook is used to create
  70. **      the FILEINFO structures from a struct ExAllData
  71. **      at create time. At delete time the FILEINFO
  72. **      structure is simply deallocated.
  73. **/
  74. SAVEDS ASM APTR ResourceHookFunc( REG(a0) struct Hook *hook, REG(a2) Object *obj, REG(a1) struct lvResource *lvr )
  75. {
  76.         struct ExAllData        *ead;
  77.         FILEINFO                *fi;
  78.         struct DateTime          dt;
  79.         APTR                     return_code = NULL;
  80.  
  81.         /*
  82.         **      What must we do?
  83.         **/
  84.         switch ( lvr->lvr_Command ) {
  85.  
  86.                 case    LVRC_MAKE:
  87.                         /*
  88.                         **      Create a FILEINFO structure.
  89.                         **      BGUI has passed us a pointer to a
  90.                         **      ExAllData structure. Here we
  91.                         **      convert it to a FILEINFO structure
  92.                         **      which, eventually, get's added to
  93.                         **      the listview.
  94.                         **/
  95.                         if ( fi = ( FILEINFO * )AllocVec( sizeof( FILEINFO ), MEMF_PUBLIC )) {
  96.                                 /*
  97.                                 **      Pick up the ExAllData.
  98.                                 **/
  99.                                 ead = ( struct ExAllData * )lvr->lvr_Entry;
  100.                                 /*
  101.                                 **      Copy the name.
  102.                                 **/
  103.                                 strcpy( &fi->fi_FileName[ 0 ], ead->ed_Name );
  104.                                 /*
  105.                                 **      Format the size text. We can do all sorts of
  106.                                 **      fancy stuff here like using the locale.library
  107.                                 **      formatting stuff but hey, it's just a demo ;)
  108.                                 **/
  109.                                 if ( ead->ed_Type < 0 ) {
  110.                                         fi->fi_IsDir = FALSE;
  111.                                         sprintf( &fi->fi_Size[ 0 ], "%ld", ead->ed_Size );
  112.                                 } else {
  113.                                         fi->fi_IsDir = TRUE;
  114.                                         strcpy( &fi->fi_Size[ 0 ], "(dir)" );
  115.                                 }
  116.                                 /*
  117.                                 **      Convert the date to a string.
  118.                                 **/
  119.                                 dt.dat_Stamp    = *(( struct DateStamp * )&ead->ed_Days );
  120.                                 dt.dat_Format   = FORMAT_CDN;
  121.                                 dt.dat_Flags    = DTF_SUBST | DTF_FUTURE;
  122.                                 dt.dat_StrDay   = NULL;
  123.                                 dt.dat_StrDate  = &fi->fi_Date[ 0 ];
  124.                                 dt.dat_StrTime  = NULL;
  125.                                 /*
  126.                                 **      Format date.
  127.                                 **/
  128.                                 DateToStr( &dt );
  129.                                 /*
  130.                                 **      Return a pointer to the created
  131.                                 **      FILEINFO structure.
  132.                                 **/
  133.                                 return_code = ( APTR )fi;
  134.                         }
  135.                         break;
  136.  
  137.                 case    LVRC_KILL:
  138.                         /*
  139.                         **      Simply deallocate the FILEINFO
  140.                         **      structure which has been created with
  141.                         **      LVRC_MAKE above.
  142.                         **/
  143.                         FreeVec( lvr->lvr_Entry );
  144.                         break;
  145.         }
  146.         /*
  147.         **      Pointer to FILEINFO or NULL.
  148.         **/
  149.         return( return_code );
  150. }
  151.  
  152. /*
  153. **      The listview will have three columns.
  154. **
  155. **      Name            Size            Date
  156. **
  157. **      The following globals will contain the maximum
  158. **      width of each of these columns.
  159. **/
  160. UWORD   MaxName, MaxSize, MaxDate;
  161.  
  162. /*
  163. **      This global stores the total width of the
  164. **      listview drawing area.
  165. **/
  166. UWORD   TotalWidth = 0;
  167.  
  168. /*
  169. **      This boolean determines wether the hook must
  170. **      re-compute the column sizes.
  171. **/
  172. BOOL    ReCompCols = TRUE;
  173.  
  174. /*
  175. **      We use 16 pixels as a minimum inner-column spacing.
  176. **/
  177. #define INNER_SPACE                     16
  178.  
  179. /*
  180. **      This routine re-computes the minimum column
  181. **      sizes when necessary.
  182. **/
  183. VOID ReComputeColumns( struct RastPort *rp, Object *obj, UWORD list_width )
  184. {
  185.         FILEINFO                        *fi;
  186.         UWORD                            tmp, total;
  187.  
  188.         /*
  189.         **      A re-computation is necessary when:
  190.         **
  191.         **      1) The ReCompCols flag is TRUE.
  192.         **      2) The with of the listview has changed.
  193.         **/
  194.         if ( ReCompCols || ( TotalWidth != list_width )) {
  195.                 /*
  196.                 **      Our listview also has a title entry.
  197.                 **      Here we compute the default column
  198.                 **      sizes accoording to this title.
  199.                 **/
  200.                 MaxName = TextLength( rp, "Name:", 6 ) + INNER_SPACE;
  201.                 MaxSize = TextLength( rp, "Size:", 6 ) + INNER_SPACE;
  202.                 MaxDate = TextLength( rp, "Date:", 6 );
  203.                 /*
  204.                 **      Now we loop through the entries to find
  205.                 **      out the largest width of the three columns.
  206.                 **/
  207.                 if ( fi = ( FILEINFO * )FirstEntry( obj )) {
  208.                         /*
  209.                         **      Loop until all are done.
  210.                         **/
  211.                         while ( fi ) {
  212.                                 /*
  213.                                 **      Compute width of the Name: column
  214.                                 **      for this entry.
  215.                                 **/
  216.                                 tmp = TextLength( rp, &fi->fi_FileName[ 0 ], strlen( &fi->fi_FileName[ 0 ] )) + INNER_SPACE;
  217.                                 /*
  218.                                 **      Is it bigger than the last one?
  219.                                 **      If so store it.
  220.                                 **/
  221.                                 if ( tmp > MaxName ) MaxName = tmp;
  222.                                 /*
  223.                                 **      Compute width of the Size: column
  224.                                 **      for this entry.
  225.                                 **/
  226.                                 tmp = TextLength( rp, &fi->fi_