TstChooserImg

/* TstChooserImg.c */
/*
gcc -o TstChooserImg  TstChooserImg.c -lauto -lraauto
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#include <classes/window.h>
#include <proto/exec.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <proto/window.h>
#include <proto/layout.h>
#include <proto/checkbox.h>
#include <proto/label.h>
#include <gadgets/button.h>
#include <gadgets/checkbox.h>
#include <gadgets/chooser.h>
#include <images/bitmap.h>
#include <images/label.h>
#include <reaction/reaction_macros.h>
 
enum
{
  GID_MAIN=0,
  GID_BUTTON,
  GID_DROPDOWN,
  GID_CHECKBOX0,
  GID_CHECKBOX1,
  GID_LAST
};
 
static Object          *gadgets[GID_LAST];
static Object          *butImg = NULL;
static Object          *dropdownImg = NULL;
static CONST_STRPTR     ddLabels[] = {"L0", "L1", "L2", NULL};
 
static STRPTR          butFilename[3];
static STRPTR          ddFilename[3];
 
static struct DrawInfo *drwInfoGlobal = NULL;
static struct Screen   *pubscrGlobal = NULL;
 
/*----------------------------------*/
 VOID GetDrawInfo(VOID)
/*----------------------------------*/
{
 
pubscrGlobal = IIntuition->LockPubScreen(NULL);
drwInfoGlobal = IIntuition->GetScreenDrawInfo(pubscrGlobal);
 
};
 
/*------------------------------------------------------*/
 Object *GetAISSImage(char *basename, STRPTR FileNames[])
/*------------------------------------------------------*/
{
struct TagItem Tags[6];
 
FileNames[0] = malloc(80);
strcpy(FileNames[0], "TBImages:"); strncat(FileNames[0], basename, 80-strlen(FileNames[0]));
printf("%s\n", FileNames[0]);
Tags[0].ti_Tag = BITMAP_SourceFile;         Tags[0].ti_Data = (ULONG)FileNames[0];
 
FileNames[1] = malloc(80);
strcpy(FileNames[1], "TBImages:"); strncat(FileNames[1], basename, 80-strlen(FileNames[1])); strncat(FileNames[1], "_s", 80-strlen(FileNames[1]));
printf("%s\n", FileNames[1]);
Tags[1].ti_Tag = BITMAP_SelectSourceFile;   Tags[1].ti_Data = (ULONG)FileNames[1];
//Tags[1].ti_Tag = TAG_IGNORE;
 
FileNames[2] = malloc(80);
strcpy(FileNames[2], "TBImages:"); strncat(FileNames[2], basename, 80-strlen(FileNames[2])); strncat(FileNames[2], "_g", 80-strlen(FileNames[2]));
printf("%s\n", FileNames[2]);
Tags[2].ti_Tag = BITMAP_DisabledSourceFile; Tags[2].ti_Data = (ULONG)FileNames[2];
//Tags[2].ti_Tag = TAG_IGNORE;
 
Tags[3].ti_Tag = BITMAP_Screen;             Tags[3].ti_Data = (ULONG)drwInfoGlobal->dri_Screen;
Tags[4].ti_Tag = BITMAP_Masking;            Tags[4].ti_Data = TRUE;
 
return IIntuition->NewObjectA(NULL, "bitmap.image", (struct TagItem *)&Tags);
}
 
/*-------------------------------------------*/
 Object *MakeImgButton(uint Gid, Object *Img)
/*-------------------------------------------*/
{
 
gadgets[Gid] = IIntuition->NewObject(NULL, "button.gadget",
          GA_ID,              GID_BUTTON,
          GA_RelVerify,       TRUE,
          GA_TabCycle,        TRUE,
          BUTTON_RenderImage, Img, 
          BUTTON_Transparent, TRUE,
          TAG_END, 0);
}
 
/*--------------------------------------------*/
 Object *MakeImgDropDown(uint Gid, Object *Img)
/*--------------------------------------------*/
{
 
gadgets[Gid] = IIntuition->NewObject(NULL, "chooser.gadget",
          GA_ID,              GID_DROPDOWN,
          GA_RelVerify,       TRUE,
          GA_TabCycle,        TRUE,
          GA_Image,           Img,
          CHOOSER_DropDown,   TRUE,
          CHOOSER_LabelArray, ddLabels,
          TAG_END, 0);
}
 
/*-------------*/
 int main(void)
/*-------------*/
{
  struct Window *window = NULL;
  Object *object = NULL;
 
  ULONG  ButtonEnabled = TRUE;
  ULONG  DropDownEnabled = TRUE;
  ULONG  SupportsDisable;
 
  ULONG wait, signal;
  ULONG result;
  UWORD code;
 
  int done = FALSE;
 
  GetDrawInfo();
  butImg = GetAISSImage("add", butFilename);
  MakeImgButton(GID_BUTTON, butImg);
  dropdownImg = GetAISSImage("add"/*"remove"*/, ddFilename);
  MakeImgDropDown(GID_DROPDOWN, dropdownImg);
 
  object = (Object *)WindowObject,
    WA_Title,        "TstChooserImg",
    WA_Activate,     TRUE,
    WA_DepthGadget,  TRUE,
    WA_DragBar,      TRUE,
    WA_CloseGadget,  TRUE,
    WA_SizeGadget,   TRUE,
    WINDOW_Position, WPOS_CENTERMOUSE,
    WINDOW_ParentGroup, gadgets[GID_MAIN] = VGroupObject,
 
      LAYOUT_AddChild, HGroupObject,
        LAYOUT_AddChild, gadgets[GID_BUTTON],
           CHILD_MaxWidth, 36,
           CHILD_MaxHeight, 36,
        LAYOUT_AddChild, gadgets[GID_CHECKBOX0] = CheckBoxObject,
           GA_ID, GID_CHECKBOX0,
           GA_RelVerify, TRUE,
           GA_TabCycle, TRUE,
           CHECKBOX_Checked, TRUE,
           EndObject,
        LAYOUT_AddImage, LabelObject,
           LABEL_Text, " Button ",
           EndObject,
      EndGroup,
 
      LAYOUT_AddChild, HGroupObject,
        LAYOUT_AddChild, gadgets[GID_DROPDOWN],
           CHILD_MaxWidth, 36,
           CHILD_MaxHeight, 36,
        LAYOUT_AddChild, gadgets[GID_CHECKBOX1] = CheckBoxObject,
           GA_ID,            GID_CHECKBOX1,
           GA_RelVerify,     TRUE,
           GA_TabCycle,      TRUE,
           CHECKBOX_Checked, TRUE,
           EndObject,
        LAYOUT_AddImage, LabelObject,
           LABEL_Text, " DropDown ",
           EndObject,
      EndGroup,
 
    EndGroup,
 
    EndWindow;
 
  if (object == NULL)
  { 
    return 0;
  }
 
  if ((window = (struct Window *) RA_OpenWindow(object)) == NULL)
  {
    IIntuition->DisposeObject(object);
    IIntuition->FreeScreenDrawInfo(pubscrGlobal, drwInfoGlobal);
    IIntuition->UnlockPubScreen(NULL, pubscrGlobal);
    return 0;
  }
 
  IIntuition->GetAttr(WINDOW_SigMask, object, &signal);
 
  IIntuition->GetAttr(IA_SupportsDisable, butImg, &SupportsDisable);
  printf("butImg SupportsDisable: %lu\n", SupportsDisable);
  IIntuition->GetAttr(IA_SupportsDisable, dropdownImg, &SupportsDisable);
  printf("dropdownImg SupportsDisable: %lu\n", SupportsDisable);
 
  while (!done)
  {
    wait = IExec->Wait( signal | SIGBREAKF_CTRL_C );
 
    if ( wait & SIGBREAKF_CTRL_C )
      done = TRUE;
    else
    {
      while ( (result = RA_HandleInput(object, &code) ) != WMHI_LASTMSG )
      {
        switch (result & WMHI_CLASSMASK)
        {
          case WMHI_CLOSEWINDOW:
            done = TRUE;
            break;
          case WMHI_GADGETUP:
            switch (result & WMHI_GADGETMASK)
            {
              case GID_BUTTON:
                printf("button\n");
                break;
              case GID_CHECKBOX0:
                ButtonEnabled = !ButtonEnabled;
                IIntuition->RefreshSetGadgetAttrs((struct Gadget *)gadgets[GID_BUTTON], window, NULL, GA_Disabled, !ButtonEnabled);
                IIntuition->RefreshSetGadgetAttrs((struct Gadget *)gadgets[GID_CHECKBOX0], window, NULL, CHECKBOX_Checked, ButtonEnabled);
                break;
              case GID_DROPDOWN:
                printf("dropdown\n");
                break;
              case GID_CHECKBOX1:
                DropDownEnabled = !DropDownEnabled;
                IIntuition->RefreshSetGadgetAttrs((struct Gadget *)gadgets[GID_DROPDOWN], window, NULL, GA_Disabled, !DropDownEnabled);
                IIntuition->RefreshSetGadgetAttrs((struct Gadget *)gadgets[GID_CHECKBOX1], window, NULL, CHECKBOX_Checked, DropDownEnabled);
                break;
              default:;
            }
            break;
          default:;
        }
      }
    }
  }
 
  IIntuition->DisposeObject(object);
 
  IIntuition->FreeScreenDrawInfo(pubscrGlobal, drwInfoGlobal);
  IIntuition->UnlockPubScreen(NULL, pubscrGlobal);
 
  return 0;
}