home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 3 / CD_Magazyn_EXEC_nr_3.iso / Recent / util / mouse / FreeWheel.lha / FreeWheel / GUI.c < prev    next >
C/C++ Source or Header  |  2000-05-26  |  14KB  |  464 lines

  1.  
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <exec/types.h>
  6. #include <libraries/gadtools.h>
  7. #include <intuition/GadgetClass.h>
  8. #include <intuition/Screens.h>
  9.  
  10. #include <clib/gadtools_protos.h>
  11. #include <clib/intuition_protos.h>
  12. #include <clib/graphics_protos.h>
  13.  
  14. #include "GUI.h"
  15.  
  16. struct TextAttr GUI_DefaultFont = { "topaz.font", 8, 0, 0};
  17.  
  18. void GUI_Dispose(struct GUIContext *gui);
  19.  
  20.  
  21. void GUI_Attach(struct GUIContext *gui,struct Window *win)
  22. {
  23.   AddGList(win,gui->ContextGadget,0,-1,NULL);
  24.   RefreshGList(gui->ContextGadget,win,NULL,-1);
  25.   GT_RefreshWindow(win, NULL);
  26. }
  27.  
  28.  
  29. struct Gadget *GUI_BuildButton(struct GUIContext *gui,char *text,long id)
  30. {
  31.   struct NewGadget newgad;
  32.   int topedge,leftedge,textwidth,gadgetheight;
  33.  
  34.   if(gui->LastGadget)
  35.   {
  36.     textwidth=32+TextLength(&gui->Screen->RastPort,text,strlen(text));
  37.  
  38.     topedge=gui->BorderTop+gui->InnerHeight;
  39.     leftedge=gui->BorderLeft+gui->InnerWidth/2-textwidth/2;
  40.     gadgetheight=gui->TextAttr->ta_YSize+6;
  41.     gui->InnerHeight+=8+gadgetheight;
  42.  
  43.     newgad.ng_TextAttr   = gui->TextAttr;
  44.     newgad.ng_VisualInfo = gui->VisualInfo;
  45.     newgad.ng_LeftEdge   = leftedge;
  46.     newgad.ng_TopEdge    = topedge;
  47.     newgad.ng_Width      = textwidth;
  48.     newgad.ng_Height     = gadgetheight;
  49.     newgad.ng_GadgetText = text;
  50.     newgad.ng_GadgetID   = id;
  51.     newgad.ng_Flags      = 0;
  52.  
  53.     gui->LastGadget = CreateGadget(BUTTON_KIND, gui->LastGadget, &newgad,
  54.                              GA_RelVerify,TRUE,
  55.                              TAG_DONE);
  56.     gui->IDCMP|=BUTTONIDCMP;
  57.   }
  58.   return(gui->LastGadget);
  59. }
  60.  
  61.  
  62. struct Gadget *GUI_BuildWideButton(struct GUIContext *gui,char *text,long id)
  63. {
  64.   struct NewGadget newgad;
  65.   int topedge,leftedge,textwidth,gadgetheight;
  66.  
  67.   if(gui->LastGadget)
  68.   {
  69.     textwidth=16+TextLength(&gui->Screen->RastPort,text,strlen(text));
  70.     if(textwidth<(gui->InnerWidth-16))
  71.       textwidth=gui->InnerWidth-16;
  72.     else
  73.       gui->InnerWidth=textwidth+16;
  74.     topedge=gui->BorderTop+gui->InnerHeight;
  75.     leftedge=gui->BorderLeft+gui->InnerWidth/2-textwidth/2;
  76.     gadgetheight=gui->TextAttr->ta_YSize+6;
  77.     gui->InnerHeight+=8+gadgetheight;
  78.  
  79.     newgad.ng_TextAttr   = gui->TextAttr;
  80.     newgad.ng_VisualInfo = gui->VisualInfo;
  81.     newgad.ng_LeftEdge   = leftedge;
  82.     newgad.ng_TopEdge    = topedge;
  83.     newgad.ng_Width      = textwidth;
  84.     newgad.ng_Height     = gadgetheight;
  85.     newgad.ng_GadgetText = text;
  86.     newgad.ng_GadgetID   = id;
  87.     newgad.ng_Flags      = 0;
  88.  
  89.     gui->LastGadget = CreateGadget(BUTTON_KIND, gui->LastGadget, &newgad,
  90.                              GA_RelVerify,TRUE,
  91.                              TAG_DONE);
  92.     gui->IDCMP|=BUTTONIDCMP;
  93.   }
  94.   return(gui->LastGadget);
  95. }
  96.  
  97.  
  98. struct Gadget *GUI_BuildText(struct GUIContext *gui,char *text)
  99. {
  100.   struct NewGadget newgad;
  101.   int topedge,leftedge,textwidth,gadgetheight;
  102.  
  103.   if(gui->LastGadget)
  104.   {
  105.     textwidth=32+TextLength(&gui->Screen->RastPort,text,strlen(text));
  106.     if(textwidth<(gui->InnerWidth-16))
  107.       textwidth=gui->InnerWidth-16;
  108.     else
  109.       gui->InnerWidth=textwidth+16;
  110.  
  111.     topedge=gui->BorderTop+gui->InnerHeight;
  112.     leftedge=gui->BorderLeft+gui->InnerWidth/2-textwidth/2;
  113.     gadgetheight=gui->TextAttr->ta_YSize+6;
  114.     gui->InnerHeight+=gadgetheight+4;
  115.  
  116.     newgad.ng_TextAttr   = gui->TextAttr;
  117.     newgad.ng_VisualInfo = gui->VisualInfo;
  118.     newgad.ng_LeftEdge   = leftedge;
  119.     newgad.ng_TopEdge    = topedge;
  120.     newgad.ng_Width      = textwidth;
  121.     newgad.ng_Height     = gadgetheight;
  122.     newgad.ng_GadgetText = 0;
  123.     newgad.ng_GadgetID   = 0;
  124.     newgad.ng_Flags      = 0;
  125.  
  126.     gui->LastGadget = CreateGadget(TEXT_KIND, gui->LastGadget, &newgad,
  127.                              GTTX_Border,FALSE,
  128.                              GTTX_Text,text,
  129.                              GTTX_Justification,GTJ_LEFT,
  130.                              GTTX_Clipped,TRUE,
  131.                              TAG_DONE);
  132.     gui->IDCMP|=TEXTIDCMP;
  133.   }
  134.   return(gui->LastGadget);
  135. }
  136.  
  137.  
  138. struct Gadget *GUI_BuildCheckBox(struct GUIContext *gui,char *text,long id)
  139. {
  140.   struct NewGadget newgad;
  141.   int topedge,leftedge,textwidth,guiwidth,gadgetwidth,gadgetheight;
  142.  
  143.   textwidth=0;
  144.   if(gui->LastGadget)
  145.   {
  146.     gadgetwidth=gadgetheight=gui->TextAttr->ta_YSize+8;
  147.     if(text)
  148.       textwidth=8+TextLength(&gui->Screen->RastPort,text,strlen(text));
  149.     if(gui->TabStop>textwidth)
  150.       textwidth=gui->TabStop;
  151.     if(textwidth>(gui->InnerWidth-16))
  152.       gui->InnerWidth=textwidth+gadgetwidth+16;
  153.     guiwidth=gui->InnerWidth-16;
  154.  
  155.     topedge=gui->BorderTop+gui->InnerHeight;
  156.     leftedge=8+gui->BorderLeft+textwidth;
  157.     gui->InnerHeight+=6+gadgetheight;
  158.  
  159.     newgad.ng_TextAttr   = gui->TextAttr;
  160.     newgad.ng_VisualInfo = gui->VisualInfo;
  161.     newgad.ng_LeftEdge   = leftedge;
  162.     newgad.ng_TopEdge    = topedge;
  163.     newgad.ng_Width      = gadgetwidth;
  164.     newgad.ng_Height     = gadgetheight;
  165.     newgad.ng_GadgetText = text;
  166.     newgad.ng_GadgetID   = id;
  167.     newgad.ng_Flags      = PLACETEXT_LEFT;
  168.  
  169.     gui->LastGadget = CreateGadget(CHECKBOX_KIND, gui->LastGadget, &newgad,
  170.                              GA_RelVerify,TRUE,
  171.                              GTCB_Scaled,TRUE,
  172.                              TAG_DONE);
  173.     gui->IDCMP|=CHECKBOXIDCMP;
  174.   }
  175.   return(gui->LastGadget);
  176. }
  177.  
  178.  
  179. struct Gadget *GUI_BuildSlider(struct GUIContext *gui,char *text,int min,int max,int level,long id)
  180. {
  181.   struct NewGadget newgad;
  182.   int topedge,leftedge,textwidth,guiwidth,gadgetwidth,gadgetheight;
  183.  
  184.   textwidth=0;
  185.   if(gui->LastGadget)
  186.   {
  187.     if(text)
  188.       textwidth=8+TextLength(&gui->Screen->RastPort,text,strlen(text));
  189.     if(gui->TabStop>textwidth)
  190.       textwidth=gui->TabStop;
  191.     if(textwidth>(gui->InnerWidth-16))
  192.       gui->InnerWidth=textwidth+100;
  193.     guiwidth=gui->InnerWidth-16;
  194.  
  195.     gadgetwidth=guiwidth-textwidth;
  196.     topedge=gui->BorderTop+gui->InnerHeight;
  197.     leftedge=8+gui->BorderLeft+textwidth;
  198.     gadgetheight=gui->TextAttr->ta_YSize+8;
  199.     gui->InnerHeight+=6+gadgetheight;
  200.  
  201.     newgad.ng_TextAttr   = gui->TextAttr;
  202.     newgad.ng_VisualInfo = gui->VisualInfo;
  203.     newgad.ng_LeftEdge   = leftedge;
  204.     newgad.ng_TopEdge    = topedge;
  205.     newgad.ng_Width      = gadgetwidth;
  206.     newgad.ng_Height     = gadgetheight;
  207.     newgad.ng_GadgetText = text;
  208.     newgad.ng_GadgetID   = id;
  209.     newgad.ng_Flags      = PLACETEXT_LEFT;
  210.  
  211.     gui->LastGadget = CreateGadget(SLIDER_KIND, gui->LastGadget, &newgad,
  212.                              GA_RelVerify,TRUE,
  213.                              GTSL_Min,min,
  214.                              GTSL_Max,max,
  215.                              GTSL_Level,level,
  216.                              TAG_DONE);
  217.     gui->IDCMP|=SLIDERIDCMP;
  218.   }
  219.   return(gui->LastGadget);
  220. }
  221.  
  222.  
  223. struct Gadget *GUI_BuildSliderFormatted(struct GUIContext *gui,char *text,int min,int max,int level,long id)
  224. {
  225.   struct NewGadget newgad;
  226.   int topedge,leftedge,textwidth,guiwidth,gadgetwidth,gadgetheight;
  227.  
  228.   textwidth=0;
  229.   if(gui->LastGadget)
  230.   {
  231.     if(text)
  232.       textwidth=8+TextLength(&gui->Screen->RastPort,text,strlen(text));
  233.     if(gui->TabStop>textwidth)
  234.       textwidth=gui->TabStop;
  235.     if(textwidth>(gui->InnerWidth-16))
  236.       gui->InnerWidth=textwidth+100;
  237.     guiwidth=gui->InnerWidth-16;
  238.  
  239.     gadgetwidth=guiwidth-textwidth;
  240.     topedge=gui->BorderTop+gui->InnerHeight;
  241.     leftedge=8+gui->BorderLeft+textwidth;
  242.     gadgetheight=gui->TextAttr->ta_YSize+8;
  243.     gui->InnerHeight+=6+gadgetheight;
  244.  
  245.     newgad.ng_TextAttr   = gui->TextAttr;
  246.     newgad.ng_VisualInfo = gui->VisualInfo;
  247.     newgad.ng_LeftEdge   = leftedge;
  248.     newgad.ng_TopEdge    = topedge;
  249.     newgad.ng_Width      = gadgetwidth;
  250.     newgad.ng_Height     = gadgetheight;
  251.     newgad.ng_GadgetText = NULL;
  252.     newgad.ng_GadgetID   = id;
  253.     newgad.ng_Flags      = PLACETEXT_LEFT;
  254.  
  255.     gui->LastGadget = CreateGadget(SLIDER_KIND, gui->LastGadget, &newgad,
  256.                              GA_RelVerify,TRUE,
  257.                              GTSL_Min,min,
  258.                              GTSL_Max,max,
  259.                              GTSL_Level,level,
  260.                              GTSL_LevelFormat,text,
  261.                              GTSL_MaxLevelLen,strlen(text)+4,
  262.                              TAG_DONE);
  263.     gui->IDCMP|=SLIDERIDCMP;
  264.   }
  265.   return(gui->LastGadget);
  266. }
  267.  
  268.  
  269. struct Gadget *GUI_BuildCycleGadget(struct GUIContext *gui,char *text,char **options,long id)
  270. {
  271.   struct NewGadget newgad;
  272.   int topedge,leftedge,textwidth,gadgetheight,gadgetwidth,guiwidth;
  273.  
  274.   if(gui->LastGadget)
  275.   {
  276.     textwidth=gui->InnerWidth-16;
  277.  
  278.     if(text)
  279.       textwidth=8+TextLength(&gui->Screen->RastPort,text,strlen(text));
  280.     if(gui->TabStop>textwidth)
  281.       textwidth=gui->TabStop;
  282.     if(textwidth>(gui->InnerWidth-16))
  283.       gui->InnerWidth=textwidth+GUI_MaxStringWidth(gui,options)+48;
  284.     guiwidth=gui->InnerWidth-16;
  285.  
  286.     gadgetwidth=guiwidth-textwidth;
  287.     topedge=gui->BorderTop+gui->InnerHeight;
  288.     leftedge=8+gui->BorderLeft+textwidth;
  289.     gadgetheight=gui->TextAttr->ta_YSize+8;
  290.     gui->InnerHeight+=6+gadgetheight;
  291.  
  292.     newgad.ng_TextAttr   = gui->TextAttr;
  293.     newgad.ng_VisualInfo = gui->VisualInfo;
  294.     newgad.ng_LeftEdge   = leftedge;
  295.     newgad.ng_TopEdge    = topedge;
  296.     newgad.ng_Width      = gadgetwidth;
  297.     newgad.ng_Height     = gadgetheight;
  298.     newgad.ng_GadgetText = text;
  299.     newgad.ng_GadgetID   = id;
  300.     newgad.ng_Flags      = 0;
  301.  
  302.     gui->LastGadget = CreateGadget(CYCLE_KIND, gui->LastGadget, &newgad,
  303.                          GA_RelVerify,TRUE,
  304.                          GTCY_Labels,options,TAG_DONE);
  305.  
  306.     gui->IDCMP|=CYCLEIDCMP;
  307.   }
  308.   return(gui->LastGadget);
  309. }
  310.  
  311.  
  312. struct Gadget *GUI_BuildString(struct GUIContext *gui,char *text,int len,long id)
  313. {
  314.   struct NewGadget newgad;
  315.   int topedge,leftedge,textwidth,guiwidth,gadgetwidth,gadgetheight;
  316.   textwidth=0;
  317.   if(gui->LastGadget)
  318.   {
  319.     if(text)
  320.       textwidth=8+TextLength(&gui->Screen->RastPort,text,strlen(text));
  321.     if(gui->TabStop>textwidth)
  322.       textwidth=gui->TabStop;
  323.     if(textwidth>(gui->InnerWidth-16))
  324.       gui->InnerWidth=textwidth+100;
  325.     guiwidth=gui->InnerWidth-16;
  326.  
  327.     gadgetwidth=guiwidth-textwidth;
  328.     topedge=gui->BorderTop+gui->InnerHeight;
  329.     leftedge=8+gui->BorderLeft+textwidth;
  330.     gadgetheight=gui->TextAttr->ta_YSize+8;
  331.     gui->InnerHeight+=6+gadgetheight;
  332.  
  333.     newgad.ng_TextAttr   = gui->TextAttr;
  334.     newgad.ng_VisualInfo = gui->VisualInfo;
  335.     newgad.ng_LeftEdge   = leftedge;
  336.     newgad.ng_Width      = gadgetwidth;
  337.     newgad.ng_TopEdge    = topedge;
  338.     newgad.ng_Height     = gadgetheight;
  339.     newgad.ng_GadgetText = text;
  340.     newgad.ng_GadgetID   = id;
  341.     newgad.ng_Flags      = 0;
  342.  
  343.     gui->LastGadget = CreateGadget(STRING_KIND, gui->LastGadget, &newgad,
  344.                          GTST_MaxChars,len,TAG_DONE);
  345.  
  346.     gui->IDCMP|=STRINGIDCMP;
  347.   }
  348.   return(gui->LastGadget);
  349. }
  350.  
  351.  
  352. struct Gadget *GUI_BuildInteger(struct GUIContext *gui,char *text,int len,long id)
  353. {
  354.   struct NewGadget newgad;
  355.   int topedge,leftedge,textwidth,gadgetheight;
  356.  
  357.   if(gui->LastGadget)
  358.   {
  359.     textwidth=gui->InnerWidth-16;
  360.  
  361.     topedge=gui->BorderTop+gui->InnerHeight;
  362.     leftedge=gui->BorderLeft+gui->InnerWidth/2-textwidth/2;
  363.     gadgetheight=gui->TextAttr->ta_YSize+6;
  364.     gui->InnerHeight+=8+gadgetheight;
  365.  
  366.     newgad.ng_TextAttr   = gui->TextAttr;
  367.     newgad.ng_VisualInfo = gui->VisualInfo;
  368.     newgad.ng_LeftEdge   = leftedge+textwidth/3;
  369.     newgad.ng_TopEdge    = topedge;
  370.     newgad.ng_Width      = (2*textwidth)/3;
  371.     newgad.ng_Height     = gadgetheight;
  372.     newgad.ng_GadgetText = text;
  373.     newgad.ng_GadgetID   = id;
  374.     newgad.ng_Flags      = 0;
  375.  
  376.     gui->LastGadget = CreateGadget(INTEGER_KIND, gui->LastGadget, &newgad,
  377.                              GTIN_MaxChars,len,TAG_DONE);
  378.     gui->IDCMP|=INTEGERIDCMP;
  379.   }
  380.   return(gui->LastGadget);
  381. }
  382.  
  383.  
  384. struct GUIContext *GUI_Create(struct Screen *screen,struct TextAttr *ta,int initwidth,int initheight)
  385. {
  386.   struct GUIContext *gui;
  387.   struct DrawInfo *mydri;
  388.   if(!(gui=malloc(sizeof(struct GUIContext))))
  389.     return(NULL);
  390.   memset(gui,0,sizeof(struct GUIContext));
  391.  
  392.   gui->Dispose=GUI_Dispose;
  393.   gui->Attach=GUI_Attach;
  394.   gui->Screen=screen;
  395.   if(ta)
  396.     gui->TextAttr=ta;
  397.   else
  398.     gui->TextAttr=&GUI_DefaultFont;
  399.  
  400.   gui->BackgroundPen=0; gui->ShinePen=1; gui->ShadowPen=3; gui->ShinePen=4;
  401.   if(mydri=GetScreenDrawInfo(screen))
  402.   {
  403.     gui->BackgroundPen=mydri->dri_Pens[BACKGROUNDPEN];
  404.     gui->ShinePen=mydri->dri_Pens[SHINEPEN];
  405.     gui->ShadowPen=mydri->dri_Pens[SHADOWPEN];
  406.     gui->FillPen=mydri->dri_Pens[FILLPEN];
  407.     FreeScreenDrawInfo(screen,mydri);
  408.   }
  409.  
  410.   gui->InnerWidth=initwidth; gui->InnerHeight=initheight;
  411.   gui->VisualInfo=GetVisualInfo(screen,TAG_DONE);
  412.   gui->BorderLeft = screen->WBorLeft;
  413.   gui->BorderRight = screen->WBorRight;
  414.   gui->BorderBottom = screen->WBorBottom;
  415.   gui->BorderTop = screen->WBorTop + (screen->Font->ta_YSize + 1);
  416.   gui->TabStop=0;
  417.  
  418.   if(!(gui->ContextGadget = CreateContext(&gui->LastGadget)))
  419.   {
  420.     gui->Dispose(gui);
  421.     return(NULL);
  422.   }
  423.  
  424.   gui->IDCMP=IDCMP_REFRESHWINDOW|IDCMP_CLOSEWINDOW;
  425.  
  426.   return(gui);
  427. }
  428.  
  429.  
  430. void GUI_Dispose(struct GUIContext *gui)
  431. {
  432.   if(gui)
  433.   {
  434.     if(gui->ContextGadget)
  435.       FreeGadgets(gui->ContextGadget);
  436.     if(gui->VisualInfo)
  437.       FreeVisualInfo(gui->VisualInfo);
  438.     free(gui);
  439.   }
  440. }
  441.  
  442.  
  443. int GUI_MaxStringWidth(struct GUIContext *gui,char **strings)
  444. {
  445.   int maxwidth=0,textwidth;
  446.   char *string;
  447.   if(strings)
  448.   {
  449.     while(string=*strings++)
  450.     {
  451.       textwidth=TextLength(&gui->Screen->RastPort,string,strlen(string));
  452.       if(textwidth>maxwidth) maxwidth=textwidth;
  453.     }
  454.   }
  455.   return(maxwidth);
  456. }
  457.  
  458.  
  459. void GUI_StringTab(struct GUIContext *gui,char **text)
  460. {
  461.   gui->TabStop=GUI_MaxStringWidth(gui,text)+8;
  462. }
  463.  
  464.