home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / utils / dialoglib / button.c next >
C/C++ Source or Header  |  1993-03-06  |  3KB  |  135 lines

  1. #include <libraries/gadtools.h>
  2. #include <graphics/text.h>
  3. #include <proto/diskfont.h>
  4. #include <proto/gadtools.h>
  5. #include <proto/graphics.h>
  6. #include <proto/utility.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include "dialog.h"
  10.  
  11. static VOID setupButton( DialogElement *de )
  12. {
  13.     struct TextExtent te;
  14.     struct RastPort rp;
  15.     struct TextAttr *ta;
  16.     struct TextFont *tf;
  17.     STRPTR text;
  18.     LONG textwidth, texttop, textbottom;
  19.  
  20.     if( !de )
  21.         return;
  22.  
  23.     de->idcmp_mask |= BUTTONIDCMP | IDCMP_REFRESHWINDOW | IDCMP_VANILLAKEY;
  24.  
  25.     ta = (struct TextAttr *)GetTagData( NGDA_TextAttr, 0, de->taglist );
  26.     if( !ta )
  27.         return;
  28.     tf = OpenDiskFont( ta );
  29.     if( !tf )
  30.         return;
  31.     InitRastPort( &rp );
  32.     SetFont( &rp, tf );
  33.  
  34.     text = (STRPTR)GetTagData( NGDA_GadgetText, 0, de->taglist );
  35.     if( text )
  36.         TextExtent( &rp, text, strlen( text ), &te );
  37.     else
  38.     {
  39.         te.te_Extent.MinX = te.te_Extent.MinY = 0;
  40.         te.te_Extent.MaxX = te.te_Extent.MaxY = -1;
  41.     }
  42.  
  43.     CloseFont( tf );
  44.  
  45.     textwidth = te.te_Extent.MaxX + 1 - te.te_Extent.MinX;
  46.     texttop = - te.te_Extent.MinY;
  47.     textbottom = te.te_Extent.MaxY + 1;
  48.  
  49.     setMinWidth( de, textwidth + 8 );
  50.     setMaxWidth( de, MAX_SPACE );
  51.     setMinTopExtent( de, texttop + 2 );
  52.     setMaxTopExtent( de, texttop + 2 );
  53.     setMinBottomExtent( de, textbottom + 2 );
  54.     setMaxBottomExtent( de, textbottom + 2 );
  55. }
  56.  
  57. static ULONG layoutButton( DialogElement *de, LayoutMessage *lm )
  58. {
  59.     struct NewGadget ng;
  60.     ULONG error = DIALOGERR_OK;
  61.  
  62.     if( !de )
  63.         return DIALOGERR_BAD_ARGS;
  64.     if( !lm )
  65.         return DIALOGERR_BAD_ARGS;
  66.  
  67.     ng.ng_GadgetText = (UBYTE *)GetTagData( NGDA_GadgetText, 0, de->taglist );
  68.     ng.ng_TextAttr = (struct TextAttr *)GetTagData( NGDA_TextAttr, 0, de->taglist );
  69.     ng.ng_VisualInfo = (APTR)GetTagData( NGDA_VisualInfo, 0, de->taglist );
  70.     ng.ng_Flags = GetTagData( NGDA_Flags, 0, de->taglist );
  71.     ng.ng_LeftEdge = lm->lm_X;
  72.     ng.ng_TopEdge = lm->lm_Y - lm->lm_Top;
  73.     ng.ng_Width = lm->lm_Width;
  74.     ng.ng_Height = lm->lm_Top + lm->lm_Bottom;
  75.     de->object = CreateGadgetA( BUTTON_KIND, *lm->lm_PreviousPtr, &ng, de->taglist );
  76.     *lm->lm_PreviousPtr = de->object;    /* advance "previous" pointer to new object */
  77.     if( !de->object )
  78.         error = DIALOGERR_NO_MEMORY;
  79.     return error;
  80. }
  81.  
  82. static DialogElement *matchButton( DialogElement *de, MatchMessage *mm )
  83. {
  84.     struct IntuiMessage *imsg;
  85.     struct TagItem *tag;
  86.     DialogElement *match = NULL;
  87.  
  88.     if( !de )
  89.         return NULL;
  90.     if( !mm )
  91.         return NULL;
  92.  
  93.     imsg = mm->mm_IntuiMsg;
  94.     switch( imsg->Class )
  95.     {
  96.     case IDCMP_GADGETUP:
  97.         if( de->object == imsg->IAddress )
  98.             match = de;
  99.         break;
  100.     case IDCMP_VANILLAKEY:
  101.         if( tag = FindTagItem( DA_EquivalentKey, de->taglist ) )
  102.             if( tolower( imsg->Code ) == tolower( tag->ti_Data ) )
  103.                 match = de;
  104.         break;
  105.     }
  106.     return match;
  107. }
  108.  
  109. ULONG dispatchButton( struct Hook *hook, DialogElement *de, DialogMessage *dm )
  110. {
  111.     ULONG result;
  112.  
  113.     switch( dm->dm_MethodID )
  114.     {
  115.     case DIALOGM_GETSTRUCT:
  116.         result = DESF_HBaseline;
  117.         break;
  118.     case DIALOGM_SETUP:
  119.         setupButton( de );
  120.         break;
  121.     case DIALOGM_LAYOUT:
  122.         result = layoutButton( de, (LayoutMessage *)dm );
  123.         break;
  124.     case DIALOGM_MATCH:
  125.         result = (ULONG)matchButton( de, (MatchMessage *)dm );
  126.         break;
  127.     case DIALOGM_CLEAR:
  128.         break;
  129.     case DIALOGM_SETATTRS:
  130.         setGTAttrs( de, (SetAttrsMessage *)dm );
  131.         break;
  132.     }
  133.     return result;
  134. }
  135.