home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d583 / tpledit.lha / TplEdit / src.lzh / gad.c < prev    next >
C/C++ Source or Header  |  1991-12-03  |  13KB  |  459 lines

  1.  
  2. /*
  3.  * GAD.C
  4.  *
  5.  * Gadget [re]construction
  6.  */
  7.  
  8. #include "defs.h"
  9.  
  10. Prototype void    ReMakeEverything(void);
  11. Prototype Gadget *MakeGadget(TplRegion *, Gadget *);
  12. Prototype Gadget *MakeAllGadgets(void);
  13. Prototype void    DisableGadgets(void);
  14. Prototype void    EnableGadgets(void);
  15. Prototype void    DisableExcl(Gadget *);
  16. Prototype void    ReleaseExcl(Gadget *);
  17. Prototype void    selectgad(Gadget *, short);
  18.  
  19. Prototype short   GadDisabled;
  20.  
  21. short     GadDisabled;
  22.  
  23. /***********************  ReMakeEverything()  ************************/
  24. void
  25. ReMakeEverything()
  26. {
  27.   MakeAllGadgets();
  28.   RefreshGList(GList, Win, NULL, -1);
  29.   GT_RefreshWindow(Win, NULL);
  30. }
  31.  
  32. /***********************  MakeAllGadgets()  **************************/
  33. Gadget *
  34. MakeAllGadgets()
  35. {
  36.   TplRegion *rg;
  37.   Gadget    *gad;
  38.  
  39.   if (GList)            /*  erase all */
  40.   {
  41.     if (Win)
  42.     {
  43.       short  cnt = 0;
  44.  
  45.       for (gad = GList; gad; ++cnt, gad = gad->NextGadget)
  46.       {
  47.         if (gad->GadgetType & GTYP_SYSGADGET)
  48.           break;
  49.       }
  50.       RemoveGList(Win, GList, cnt);
  51.       SetRast(Win->RPort, 0);
  52.       RefreshWindowFrame(Win);
  53.     }
  54.     FreeGadgets(GList);
  55.     GList = NULL;
  56.   }
  57.  
  58.   if (gad = CreateContext(&GList))    /*  make new */
  59.   {
  60.     if (DDebug)
  61.       printf("GLIST %08lx gad %08lx\n", GList, gad);
  62.  
  63.     for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  64.     {
  65.       if (!(rg->rg_LockedTo && (rg->rg_Type == RGT_LISTVIEW)))
  66.         gad = MakeGadget(rg, gad);
  67.     }
  68.  
  69.     for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  70.     {
  71.       if (rg->rg_LockedTo && (rg->rg_Type == RGT_LISTVIEW))
  72.         gad = MakeGadget(rg, gad);
  73.     }
  74.   }
  75.  
  76.   if (Win)
  77.   {
  78.     AddGList(Win, GList, 0, -1, NULL);
  79.     if (GadDisabled)
  80.       DisableGadgets();
  81.     else
  82.       init_test();
  83.   }
  84.   return (gad);
  85. }
  86.  
  87. /***********************  MakeGadget()  ****************************/
  88. Gadget *
  89. MakeGadget(rg, gad)
  90.   TplRegion *rg;
  91.   Gadget *gad;
  92. {
  93.  
  94.   if ((rg->rg_Type == RGT_INVISIBLE) ||
  95.       (rg->rg_Type == RGT_BEVELBOX))
  96.     return (gad);
  97.  
  98.   short highlabel  = (rg->rg_SpecFlags & SPEC_HIGHLABEL) ? NG_HIGHLABEL : 0;
  99.   NewGadget Ng;
  100.  
  101.   Ng.ng_LeftEdge   = rg->rg_Box.bo_Xs;
  102.   Ng.ng_TopEdge    = rg->rg_Box.bo_Ys;
  103.   Ng.ng_Width      = rg->rg_Box.bo_Xe - rg->rg_Box.bo_Xs + 1;
  104.   Ng.ng_Height     = rg->rg_Box.bo_Ye - rg->rg_Box.bo_Ys + 1;
  105.   Ng.ng_GadgetText = rg->rg_LabText;
  106.   Ng.ng_TextAttr   = &rg->rg_TextAttr;
  107.   Ng.ng_VisualInfo = VisInfo;
  108.   Ng.ng_GadgetID   = rg->rg_Id;
  109.   Ng.ng_Flags      = rg->rg_Flags | highlabel;
  110.   Ng.ng_UserData   = rg;
  111.  
  112.   if (Ng.ng_Width < 8 || Ng.ng_Height < 8)
  113.   {
  114.     printf("Size Error %d %d\n", Ng.ng_Width, Ng.ng_Height);
  115.     return (gad);
  116.   }
  117.  
  118.   if (DDebug)
  119.     printf("TYPE  %d\n", rg->rg_Type);
  120.  
  121.   switch (rg->rg_Type)
  122.   {
  123.   case RGT_BUTTON:
  124.     rg->rg_Gad = CreateGadget(BUTTON_KIND, gad, &Ng,
  125.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  126.                               TAG_END);
  127.     break;
  128.  
  129.   case RGT_STRING:
  130.     TplRegion *trg = rg->rg_LockedTo;
  131.  
  132.     rg->rg_Gad = CreateGadget(STRING_KIND, gad, &Ng,
  133.                               GTST_MaxChars,    rg->rg_BufSpace,
  134.                               GTST_String,      rg->rg_DefText,
  135.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  136.                               TAG_END);
  137.     break;
  138.  
  139.   case RGT_LISTVIEW:
  140.     long showselect = (rg->rg_LockedTo)  ? (long)rg->rg_LockedTo->rg_Gad : NULL;
  141.  
  142.     rg->rg_Gad = CreateGadget(LISTVIEW_KIND, gad, &Ng,
  143.                               GTLV_Labels,     &rg->rg_ListSet,
  144.                               GTLV_Selected,    rg->rg_DefVal,
  145.                               ((rg->rg_LockedTo)  ? GTLV_ShowSelected :
  146.                (rg->rg_SpecFlags & SPEC_READONLY) ? GTLV_ShowSelected : TAG_IGNORE),
  147.                                                 showselect,
  148.                               LAYOUTA_Spacing,  rg->rg_BufSpace,
  149.                               TAG_END);
  150.     break;
  151.  
  152.   case RGT_CYCLE:
  153.     rg->rg_Gad = CreateGadget(CYCLE_KIND, gad, &Ng,
  154.                               GTCY_Labels,      rg->rg_CycleAry,
  155.                               GTCY_Active,      rg->rg_DefVal,
  156.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  157.                               TAG_END);
  158.     break;
  159.  
  160.   case RGT_MX:
  161.     rg->rg_Gad = CreateGadget(MX_KIND, gad, &Ng,
  162.                               GTMX_Labels,      rg->rg_CycleAry,
  163.                               GTMX_Active,      rg->rg_DefVal,
  164.                               GTMX_Spacing,     rg->rg_BufSpace,
  165.                               TAG_END);
  166.     break;
  167.  
  168.   case RGT_CHECKBOX:
  169.     rg->rg_Gad = CreateGadget(CHECKBOX_KIND, gad, &Ng,
  170.                               GTCB_Checked,    (rg->rg_SpecFlags & SPEC_CHECKED),
  171.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  172.                               TAG_END);
  173.     break;
  174.  
  175.   case RGT_INTEGER:
  176.     rg->rg_Gad = CreateGadget(INTEGER_KIND, gad, &Ng,
  177.                               GTIN_Number,      rg->rg_DefVal,
  178.                               GTIN_MaxChars,   (rg->rg_BufSpace) ?
  179.                                                (rg->rg_BufSpace  - 1) : 0,
  180.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  181.                               TAG_END);
  182.     break;
  183.  
  184.   case RGT_TEXT:
  185.     rg->rg_Gad = CreateGadget(TEXT_KIND, gad, &Ng,
  186.                               GTTX_Text,       (rg->rg_DefText) ?
  187.                                                 rg->rg_DefText  : "",
  188.                               GTTX_CopyText,    1,
  189.                               GTTX_Border,     (rg->rg_SpecFlags & SPEC_BORDER),
  190.                               TAG_END);
  191.     break;
  192.  
  193.   case RGT_NUMBER:
  194.     rg->rg_Gad = CreateGadget(NUMBER_KIND, gad, &Ng,
  195.                               GTNM_Number,      rg->rg_DefVal,
  196.                               GTNM_Border,     (rg->rg_SpecFlags & SPEC_BORDER),
  197.                               TAG_END);
  198.     break;
  199.  
  200.   case RGT_SLIDER:
  201.     rg->rg_Gad = CreateGadget(SLIDER_KIND, gad, &Ng,
  202.                               GTSL_Min,         0,
  203.                               GTSL_Max,         rg->rg_MaxVal,
  204.                               GTSL_Level,       rg->rg_DefVal,
  205.                               GTSL_DispFunc,    DispFunc,
  206.                               GTSL_LevelFormat, rg->rg_LabText,
  207.                               GTSL_MaxLevelLen, rg->rg_BufSpace,
  208.                               GTSL_LevelPlace,  rg->rg_Flags,
  209.                               PGA_Freedom,     (rg->rg_SpecFlags &
  210.                                                 SPEC_VERTICAL) ?
  211.                                                 LORIENT_VERT : LORIENT_HORIZ,
  212.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  213.                               TAG_END);
  214.     break;
  215.  
  216.   case RGT_SCROLLER:
  217.     Box box = rg->rg_Box;
  218.     short x = box.bo_Xe - box.bo_Xs;
  219.     short y = box.bo_Ye - box.bo_Ys;
  220.     x = (x >  7) ?  8 : x;
  221.     y = (y > 15) ? 16 : y;
  222.  
  223.     rg->rg_Gad = CreateGadget(SCROLLER_KIND, gad, &Ng,
  224.                               GTCY_Labels,      rg->rg_CycleAry,
  225.                               GTSC_Total,       rg->rg_MaxVal,
  226.                               GTSC_Top,         rg->rg_DefVal,
  227.                               GTSC_Visible,     rg->rg_BufSpace,
  228.                               GTSC_Arrows,     (rg->rg_SpecFlags &
  229.                                                 SPEC_VERTICAL) ? y : x,
  230.                               PGA_Freedom,     (rg->rg_SpecFlags &
  231.                                                 SPEC_VERTICAL) ?
  232.                                                 LORIENT_HORIZ : LORIENT_VERT,
  233.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  234.                               TAG_END);
  235.  
  236.   case RGT_PALETTE:
  237.     Box box = rg->rg_Box;
  238.     short x = box.bo_Xe - box.bo_Xs;
  239.     short y = box.bo_Ye - box.bo_Ys;
  240.     x = (x >> 1) - 4;
  241.     if ((y + 4) < x)
  242.       x = y + 4;
  243.  
  244.     short val = (rg->rg_MaxVal > 15) ? 5 :
  245.                 (rg->rg_MaxVal >  7) ? 4 :
  246.                 (rg->rg_MaxVal >  3) ? 3 :
  247.                 (rg->rg_MaxVal >  1) ? 2 : 1;
  248.  
  249.     rg->rg_Gad = CreateGadget(PALETTE_KIND, gad, &Ng,
  250.                               GTPA_Depth,       val,
  251.                               GTPA_Color,       rg->rg_DefVal,
  252.                               GTPA_ColorOffset, rg->rg_BufSpace,
  253.                               GTPA_IndicatorWidth,  x,
  254.                               GTPA_IndicatorHeight, y,
  255.                               GA_Disabled,     (rg->rg_SpecFlags & SPEC_DISABLED),
  256.                               TAG_END);
  257.   default:
  258.     break;
  259.   }
  260.   rg->rg_Gad->MutualExclude |= rg->rg_ExclGroup;
  261.   return (rg->rg_Gad);
  262. }
  263.  
  264. /***********************  DisableGadgets()  **************************/
  265. void
  266. DisableGadgets()
  267. {
  268.   TplRegion *rg;
  269.  
  270.   GadDisabled = 1;
  271.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  272.   {
  273.     switch (rg->rg_Type)
  274.     {
  275.     case RGT_INVISIBLE:
  276.     case RGT_LISTVIEW:
  277.     case RGT_NUMBER:
  278.     case RGT_TEXT:
  279.     case RGT_MX:
  280.       break;
  281.  
  282.     case RGT_BEVELBOX:
  283.       short leftedge   = rg->rg_Box.bo_Xs;
  284.       short topedge    = rg->rg_Box.bo_Ys;
  285.       short width      = rg->rg_Box.bo_Xe - rg->rg_Box.bo_Xs + 1;
  286.       short height     = rg->rg_Box.bo_Ye - rg->rg_Box.bo_Ys + 1;
  287.       DrawBevelBox(Win->RPort, leftedge, topedge, width, height,
  288.                         GT_VisualInfo, VisInfo,
  289.                         ((rg->rg_SpecFlags & SPEC_RECESSED) ?
  290.                                              GTBB_Recessed : TAG_IGNORE), TRUE,
  291.                         TAG_END);
  292.       break;
  293.  
  294.     default:
  295.       GT_SetGadgetAttrs(rg->rg_Gad, Win, NULL,
  296.                         GA_Disabled, 1,
  297.                         TAG_END);
  298.       break;
  299.     }
  300.   }
  301.   GT_RefreshWindow(Win, NULL);
  302. }
  303.  
  304. /***********************  EnableGadgets() ****************************/
  305. void
  306. EnableGadgets()
  307. {
  308.   TplRegion *rg;
  309.  
  310.   GadDisabled = 0;
  311.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  312.   {
  313.     switch (rg->rg_Type)
  314.     {
  315.     case RGT_INVISIBLE:
  316.     case RGT_LISTVIEW:
  317.     case RGT_NUMBER:
  318.     case RGT_TEXT:
  319.       break;
  320.  
  321.     case RGT_MX:
  322.       GT_SetGadgetAttrs(rg->rg_Gad, Win, NULL,
  323.                         GTMX_Active, rg->rg_DefVal,
  324.                         TAG_END);
  325.       break;
  326.  
  327.     case RGT_BEVELBOX:
  328.       short leftedge   = rg->rg_Box.bo_Xs;
  329.       short topedge    = rg->rg_Box.bo_Ys;
  330.       short width      = rg->rg_Box.bo_Xe - rg->rg_Box.bo_Xs + 1;
  331.       short height     = rg->rg_Box.bo_Ye - rg->rg_Box.bo_Ys + 1;
  332.       DrawBevelBox(Win->RPort, leftedge, topedge, width, height,
  333.                         GT_VisualInfo, VisInfo,
  334.                         ((rg->rg_SpecFlags & SPEC_RECESSED) ?
  335.                                              GTBB_Recessed : TAG_IGNORE), TRUE,
  336.                         TAG_END);
  337.       break;
  338.  
  339.     case RGT_BUTTON:
  340.       if (rg->rg_SpecFlags & SPEC_CHECKED)
  341.         selectgad(rg->rg_Gad, RGT_BUTTON);
  342.  
  343.     default:
  344.       if (!(rg->rg_SpecFlags & SPEC_DISABLED))
  345.         GT_SetGadgetAttrs(rg->rg_Gad, Win, NULL,
  346.                         GA_Disabled, 0,
  347.                         TAG_END);
  348.       break;
  349.     }
  350.   }
  351.   GT_RefreshWindow(Win, NULL);
  352. }
  353.  
  354. /***********************  ReleaseExcl(long  **************************/
  355. void
  356. ReleaseExcl(Gadget *gadget)
  357. {
  358.   long excl = gadget->MutualExclude;
  359.   TplRegion *rg;
  360.  
  361.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  362.   {
  363.     Gadget *gad = rg->rg_Gad;
  364.     if ((gad) && (gad != gadget))
  365.     {
  366.       if (excl & gad->MutualExclude)
  367.         GT_SetGadgetAttrs(gad, Win, NULL,
  368.                           GA_Disabled, 0,
  369.                           TAG_END);
  370.     }
  371.   }
  372. }
  373.  
  374. /***********************  DisableExcl(long  **************************/
  375. void
  376. DisableExcl(Gadget *gadget)
  377. {
  378.   long excl = gadget->MutualExclude;
  379.   TplRegion *rg;
  380.   for (rg = GetHead(&RegList); rg; rg = GetSucc(&rg->rg_Node))
  381.   {
  382.     Gadget *gad = rg->rg_Gad;
  383.     if ((gad) && (gad != gadget))
  384.     {
  385.       short gad_pos;
  386.  
  387.       if (excl & gad->MutualExclude)
  388.       {
  389.         switch(rg->rg_Type)
  390.         {
  391.         case RGT_BUTTON:
  392.         case RGT_CHECKBOX:
  393.           if (!(gad->Flags & GFLG_SELECTED))
  394.             break;
  395.           if (DDebug)
  396.             printf("%s exclgroup: %d\n", rg->rg_Node.ln_Name,
  397.                                          rg->rg_ExclGroup);
  398.  
  399.           gad_pos     = RemoveGadget(Win, gad);
  400.           gad->Flags -= GFLG_SELECTED;
  401.           AddGadget(Win, gad, gad_pos);
  402.           RefreshGList(gad, Win, NULL, 1);
  403.           break;
  404.  
  405.         default:
  406.           GT_SetGadgetAttrs(gad, Win, NULL,
  407.                             GA_Disabled, 1,
  408.                             TAG_END);
  409.           break;
  410.         }
  411.       }
  412.     }
  413.   }
  414. }
  415.  
  416. /***********************  selectgad  **************************/
  417. void
  418. selectgad(Gadget *gadget, short gad_type)
  419. {
  420.   short release = FALSE;
  421.  
  422.   switch(gad_type)
  423.   {
  424.   case RGT_CHECKBOX:
  425.     if (!(gadget->Flags & GFLG_SELECTED))
  426.       return;
  427.     else
  428.       break;
  429.  
  430.   case RGT_BUTTON:
  431.     short gad_pos = RemoveGadget(Win, gadget);
  432.  
  433.     if (gadget->Flags & GFLG_SELECTED)
  434.     {
  435.       gadget->Flags -= GFLG_SELECTED;
  436.       release = TRUE;
  437.     }
  438.     else
  439.       gadget->Flags |= GFLG_SELECTED;
  440.  
  441.     AddGadget(Win, gadget, gad_pos);
  442.     RefreshGList(gadget, Win, NULL, 1);
  443.     break;
  444.  
  445.   default:
  446.     return;
  447.   }
  448.  
  449.   if (gadget->MutualExclude)
  450.   {
  451.     if (release)
  452.       ReleaseExcl(gadget);
  453.     else
  454.       DisableExcl(gadget);
  455.   }
  456. }
  457.  
  458.  
  459.