home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Commodities / BlackHole / Source / control_panel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  10.6 KB  |  364 lines

  1.  
  2. /*
  3.  * Control_panel.c -- opens a small window giving user general options.
  4.  * $Revision: 1.3
  5.  */
  6.  
  7. #include "header.h"
  8. #include "prog-protos.h"
  9. #include <intuition/intuitionbase.h>
  10.  
  11. /* Prototypes */
  12. Prototype BOOL open_control_panel (struct HolePrefs *);
  13. Prototype void close_control_panel (void);
  14.  
  15. /* Shared variables */
  16. Prototype struct HolePanel *panel;
  17.  
  18. Local struct Gadget *create_gadgets (struct NewGadget *);
  19. Local void refresh_window (void);
  20. Local BOOL initialise (void);
  21.  
  22. /* Globals */
  23. struct Gadget *glist    = NULL;
  24. struct Screen *pubsc    = NULL;
  25. struct HolePanel *panel = NULL;
  26. struct IBox *winsize    = NULL;
  27. struct IBox *basbox;    = NULL;
  28. struct RastPort *TextRP = NULL;
  29. struct TextFont *font    = NULL;
  30. struct DisplayInfo *di    = NULL;
  31. APTR *vi        = NULL;    /* VisualInfo */
  32.  
  33. /* Statics */
  34. static const char *msg_ignoreprot = "Ignore Protection";
  35. static const char *msg_confirm      = "Confirm deletion";
  36. static const char *msg_limit      = "Delete Limit";
  37. static const char *but_snapshot   = "Snapshot";
  38. static const char *but_unsnapshot = "UnSnapshot";
  39. static const char *but_save      = "Save";
  40. static const char *but_use      = "Use";
  41. static const char *but_cancel      = "Cancel";
  42. static const char *but_quit      = "Quit";
  43. static const char *but_about      = "About";
  44.  
  45. /* Fill patterns */
  46. static const UWORD white_grey[] =
  47. {
  48.     0x0000, 0x0000,
  49.     0x9999, 0x9999,
  50. };
  51.  
  52. static const UWORD blue_grey[] =
  53. {
  54.     0xAAAA, 0xAAAA,
  55.     0xAAAA, 0xAAAA,
  56. };
  57.  
  58. /* Functions */
  59. /* General startup - returns TRUE if ok */
  60. BOOL initialise (void)
  61. {
  62.     unless (winsize = malloc (sizeof (struct IBox)))    return (FALSE);
  63.     unless (basbox  = malloc (sizeof (struct IBox)))    return (FALSE);
  64.     unless (panel = malloc (sizeof (struct HolePanel))) return (FALSE);
  65.     unless (pubsc = LockPubScreen ("Workbench"))        return (FALSE);
  66.     unless (TextRP = malloc (sizeof (struct RastPort))) return (FALSE);
  67.     unless (di = malloc (sizeof (struct DisplayInfo)))  return (FALSE);
  68.     unless (GetDisplayInfoData (NULL, (UBYTE *)di,
  69.         sizeof (struct DisplayInfo), DTAG_DISP,
  70.         GetVPModeID (&(pubsc->ViewPort))))          return (FALSE);
  71.     /* Open the font into a rastport so we can use TextLength() */
  72.     unless (font = OpenFont (pubsc->Font))              return (FALSE);
  73.     InitRastPort (TextRP);
  74.     SetFont (TextRP, font);
  75.     vi = GetVisualInfo (pubsc, TAG_END);
  76.  
  77.     return (TRUE);
  78. }
  79.  
  80. void close_control_panel (void)
  81. {
  82.     if (panel && panel->window) CloseWindow (panel->window);
  83.     if (glist)   FreeGadgets (glist);
  84.     if (vi)      FreeVisualInfo (vi);
  85.     if (font)    CloseFont (font);
  86.     if (di)      free (di);
  87.     if (TextRP)  free (TextRP);
  88.     if (pubsc)   UnlockPubScreen (NULL, pubsc);
  89.     if (panel)   free (panel);
  90.     if (basbox)  free (basbox);
  91.     if (winsize) free (winsize);
  92.  
  93.     /* Tell everyone that the panel has closed down */
  94.     panel = glist = vi = font = di = TextRP = pubsc = basbox = winsize = NULL;
  95. }
  96.  
  97. BOOL open_control_panel (struct HolePrefs *prefs_p)
  98. {
  99.     if (panel->window)    /* If window is already opened */
  100.     {
  101.     WindowToFront (panel->window);
  102.     ActivateWindow (panel->window);
  103.     return (TRUE);
  104.     }
  105.  
  106.     unless (initialise()) goto fail;
  107.  
  108.     panel->prefs = *prefs_p;   /* put initial preferences into HolePanel struct */
  109.     struct NewGadget ng;
  110.  
  111.     winsize->Left   = pubsc->WBorLeft;    /* border at left edge of window */
  112.     winsize->Top    = pubsc->WBorTop + pubsc->Font->ta_YSize + 1;
  113.     winsize->Width  = pubsc->WBorRight + winsize->Left;
  114.     winsize->Height = pubsc->WBorBottom + winsize->Top;
  115.  
  116.     ng.ng_TextAttr = pubsc->Font;
  117.     ng.ng_VisualInfo = vi;
  118.     unless (create_gadgets (&ng)) goto fail;
  119.  
  120.     /* Find position of mouse pointer, so window can open over it */
  121.     {
  122.     ULONG IBaseLock = LockIBase (0);
  123.     winsize->Left = IntuitionBase->MouseX - (winsize->Width / 2);
  124.     winsize->Top  = IntuitionBase->MouseY - (winsize->Height / 2);
  125.     UnlockIBase (IBaseLock);
  126.     }
  127.     unless (panel->window = OpenWindowTags
  128.     (
  129.     NULL,
  130.     WA_Left,        winsize->Left,
  131.     WA_Top,         winsize->Top,
  132.     WA_Width,        winsize->Width,
  133.     WA_Height,        winsize->Height,
  134.     WA_MinWidth,        winsize->Width,
  135.     WA_MinHeight,        winsize->Height,
  136.     WA_RMBTrap,        TRUE,
  137.     WA_AutoAdjust,        TRUE,
  138.     WA_PubScreen,        pubsc,
  139.     WA_SmartRefresh,    TRUE,
  140.     WA_DepthGadget,     TRUE,
  141.     WA_IDCMP,        IDCMP_GADGETUP,
  142.     WA_DragBar,        TRUE,
  143.     WA_Activate,        TRUE,
  144.     WA_Title,        "Black Hole Options",
  145.     TAG_DONE)
  146.     ) goto fail;
  147.  
  148.     AddGList (panel->window, glist, ((UWORD) -1), ((UWORD) -1), NULL);
  149.     refresh_window();
  150.     RefreshGList (glist, panel->window, NULL, ((UWORD) -1));
  151.  
  152.     GT_RefreshWindow (panel->window, NULL); /* Perform the GadTools initial refresh */
  153.  
  154.     return (TRUE);
  155.  
  156.     fail:
  157.     close_control_panel();
  158.     return (FALSE);
  159. }
  160.  
  161. void refresh_window (void)
  162. {
  163.     /* Fill window with nice white/grey stipple pattern */
  164.     SetAfPt (panel->window->RPort, white_grey, -1); /* set up rp for pattern fill */
  165.     RectFill (panel->window->RPort,
  166.     pubsc->WBorLeft,
  167.     pubsc->WBorTop + pubsc->Font->ta_YSize + 1,
  168.     winsize->Width - pubsc->WBorRight -1,
  169.     winsize->Height - pubsc->WBorBottom -1
  170.     );
  171.  
  172.     /* Fill indented box with blue/grey pattern */
  173.     SetAfPt (panel->window->RPort, blue_grey, -1);
  174.     RectFill (panel->window->RPort,
  175.     basbox->Left,
  176.     basbox->Top,
  177.     basbox->Left + basbox->Width -1,
  178.     basbox->Top  + basbox->Height -1
  179.     );
  180.  
  181.     DrawBevelBox (panel->window->RPort,
  182.     basbox->Left,
  183.     basbox->Top,
  184.     basbox->Width,
  185.     basbox->Height,
  186.     GTBB_Recessed, TRUE,
  187.     GT_VisualInfo, vi,
  188.     TAG_DONE
  189.     );
  190. }
  191.  
  192. /* Macro to shorten a lot of lines in the next function */
  193. #define textlen(x) (TextLength (TextRP, (x), strlen (x)))
  194.  
  195. struct Gadget *create_gadgets (struct NewGadget *ng)
  196. {
  197.     struct Gadget *gad = NULL;
  198.     int but_width, but_height, xspace, yspace;
  199.  
  200.     /* Work out size of buttons, making them square. */
  201.     but_height = (TextRP->TxHeight * 3)/2; /* 3/2 times font height */
  202.     but_width = (but_height * di->Resolution.y) / di->Resolution.x;
  203.  
  204.     xspace = but_width / 2;    /* general spacing value */
  205.     yspace = but_height / 2;
  206.  
  207.     gad = CreateContext (&glist);
  208.  
  209.     winsize->Left    += xspace;     /* Top corner of winsize is where corner */
  210.     winsize->Top     += yspace;     /* of Quit button will go.             */
  211.  
  212.     /* Now set up the IBox struct for the 3D box that surrounds some options */
  213.     /* I am going to pray that the Snapshot/UnSnapshot line will be the largest! */
  214.     basbox->Left      = winsize->Left;
  215.     basbox->Top       = winsize->Top + but_height + yspace;
  216.     basbox->Width     = textlen (but_snapshot) + textlen (but_unsnapshot);
  217.     basbox->Width    += (3 * xspace) + (2 * but_width);
  218.  
  219.     /* Standard settings for most gadgets */
  220.     ng->ng_Height     = but_height;
  221.     ng->ng_Flags      = PLACETEXT_IN;
  222.  
  223.     /* Up in the corner on Piccalo, Quit! */
  224.     if (gad)
  225.     {
  226.     ng->ng_LeftEdge   = winsize->Left;
  227.     ng->ng_TopEdge      = winsize->Top;
  228.     ng->ng_Width      = but_width + textlen (but_quit);
  229.     ng->ng_GadgetText = but_quit;
  230.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  231.         gad->UserData = (APTR)GAD_QUIT;
  232.     }
  233.  
  234.     /* Over on the right on guitar, About. Oh yes and he's broken a string! */
  235.     if (gad)
  236.     {
  237.     ng->ng_Width      = but_width + textlen (but_about);
  238.     ng->ng_LeftEdge   = winsize->Left + basbox->Width - ng->ng_Width;
  239.     ng->ng_GadgetText = but_about;
  240.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  241.         gad->UserData = (APTR)GAD_ABOUT;
  242.     }
  243.  
  244.     /* The next three want the text to their right */
  245.     ng->ng_Flags = PLACETEXT_RIGHT;
  246.  
  247.     /* Delete Limit */
  248.     if (gad)
  249.     {
  250.     ng->ng_Width      = textlen ("555") + but_width;
  251.     ng->ng_LeftEdge   = winsize->Left + xspace;
  252.     ng->ng_TopEdge      = winsize->Top + (2 * yspace) + but_height;
  253.     ng->ng_GadgetText = msg_limit;
  254.     if (gad = CreateGadget (INTEGER_KIND, gad, ng,
  255.         GTIN_Number,    panel->prefs.deletelimit,
  256.         GTIN_MaxChars,  2,
  257.         TAG_END
  258.     ))
  259.     {
  260.         gad->UserData = (APTR)GAD_LIMIT;
  261.         panel->gad_limit = gad;
  262.     }
  263.     }
  264.  
  265.     /* Then a line below, Confirm Deletion */
  266.     if (gad)    /* has to read some fields from it, so it must exist */
  267.     {
  268.     ng->ng_Width      = but_width;
  269.     ng->ng_TopEdge      = yspace + gad->TopEdge + gad->Height;
  270.     ng->ng_GadgetText = msg_confirm;
  271.     if (gad = CreateGadget (CHECKBOX_KIND, gad, ng,
  272.         GTCB_Checked, panel->prefs.confirm,
  273.         TAG_END
  274.     ))
  275.     {
  276.         gad->UserData = (APTR)GAD_CONFIRM;
  277.         panel->gad_confirm = gad;
  278.     }
  279.     }
  280.  
  281.     /* Ignore Protection */
  282.     if (gad)
  283.     {
  284.     ng->ng_TopEdge      = yspace + gad->TopEdge + gad->Height;
  285.     ng->ng_GadgetText = msg_ignoreprot;
  286.     if (gad = CreateGadget (CHECKBOX_KIND, gad, ng,
  287.         GTCB_Checked, panel->prefs.ignore_prot,
  288.         TAG_END
  289.     ))
  290.     {
  291.         gad->UserData = (APTR)GAD_IGNORE;
  292.         panel->gad_ignore_prot = gad;
  293.     }
  294.     }
  295.  
  296.     /* Go back to putting text inside the hitbox */
  297.     ng->ng_Flags = PLACETEXT_IN;
  298.  
  299.     /* UnSnapShot button */
  300.     if (gad)
  301.     {
  302.     ng->ng_TopEdge      = yspace + gad->TopEdge + gad->Height;
  303.     ng->ng_LeftEdge   = winsize->Left + xspace;
  304.     ng->ng_Width      = but_width + textlen (but_unsnapshot);
  305.     ng->ng_GadgetText = but_unsnapshot;
  306.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  307.         gad->UserData = (APTR)GAD_UNSNAP;
  308.     }
  309.  
  310.     /* Now on drums, Sammy the Snapshot button. */
  311.     if (gad)
  312.     {
  313.     ng->ng_LeftEdge   = winsize->Left + (2 * xspace) + gad->Width;
  314.     ng->ng_Width      = but_width + textlen (but_snapshot);
  315.     ng->ng_GadgetText = but_snapshot;
  316.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  317.         gad->UserData = (APTR)GAD_SNAP;
  318.     }
  319.  
  320.     if (gad) basbox->Height = gad->TopEdge + but_height + yspace - basbox->Top;
  321.  
  322.     /* And down below on Timpani, Save, Use and Cancel. Keep bonging, guys! */
  323.     if (gad)
  324.     {
  325.     int right_edge_save, left_edge_cancel;
  326.  
  327.     ng->ng_TopEdge      = (2 * yspace) + gad->TopEdge + gad->Height;
  328.  
  329.     ng->ng_LeftEdge   = winsize->Left;
  330.     ng->ng_Width      = but_width + textlen (but_save);
  331.     ng->ng_GadgetText = but_save;
  332.     right_edge_save   = ng->ng_LeftEdge + ng->ng_Width;
  333.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  334.         gad->UserData = (APTR)GAD_SAVE;
  335.  
  336.  
  337.     ng->ng_Width      = but_width + textlen (but_cancel);
  338.     ng->ng_LeftEdge   = winsize->Left + basbox->Width - ng->ng_Width;
  339.     ng->ng_GadgetText = but_cancel;
  340.     left_edge_cancel  = ng->ng_LeftEdge;
  341.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  342.         gad->UserData = (APTR)GAD_CANCEL;
  343.  
  344.     ng->ng_Width      = but_width + textlen (but_use);
  345.     ng->ng_LeftEdge   = (right_edge_save + left_edge_cancel - ng->ng_Width) / 2;
  346.     ng->ng_GadgetText = but_use;
  347.     if (gad = CreateGadget (BUTTON_KIND, gad, ng, TAG_END))
  348.         gad->UserData = (APTR)GAD_USE;
  349.     }
  350.  
  351.     /* Set up size of window itself */
  352.     winsize->Width   += (2 * xspace);
  353.     winsize->Width   += basbox->Width;
  354.  
  355.     winsize->Height  += (2 * but_height) + (4 * yspace);
  356.     winsize->Height  += basbox->Height;
  357.  
  358.     return (gad);
  359. }
  360.  
  361. #undef textlen(x)
  362.  
  363.  
  364.