home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / WBStartup+ / Source / WBStartup+Prefs / ChooseGroupWindow.c < prev    next >
C/C++ Source or Header  |  1998-10-26  |  7KB  |  205 lines

  1. #include <exec/types.h>
  2.  
  3. #include <string.h>
  4. #include <intuition/gadgetclass.h>
  5. #include <math.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <proto/gadtools.h>
  9. #include <proto/intuition.h>
  10. #include <proto/icon.h>
  11. #include <proto/graphics.h>
  12. #include <proto/wb.h>
  13. #include <proto/commodities.h>
  14. #include <proto/diskfont.h>
  15. #include <proto/graphics.h>
  16. #include <proto/utility.h>
  17.  
  18. #include "WBStartup+Prefs.h"
  19. #include "WBStartupPlusPrefs_Cat.h"
  20.  
  21. struct Node *ChooseGroup(UWORD x, UWORD y, struct List *list, char *windowtitle);
  22. struct Gadget *createChooseGroupGadgets(struct Gadget **glistptr, void *vi, struct Window *win, struct Gadget *my_gads[], struct List *list);
  23.  
  24.  
  25. struct Node *ChooseGroup(UWORD x, UWORD y, struct List *list, char *windowtitle)
  26. {
  27.   /* WORD x         The current X mouse position */
  28.   /* WORD y         The current Y mouse position */
  29.   /* char *windowtitle  The title of the window */
  30.   /* RETURNS a pointer to choosen node, or NULL if canceled */
  31.  
  32.   ULONG signals;
  33.   ULONG winsigflag;
  34.   BOOL LOOP;
  35.   struct IntuiMessage *winmsg;
  36.   struct Window *win;
  37.   struct Screen *scr;
  38.   struct VisualInfo *vi;
  39.   ULONG winheight,winwidth;
  40.   struct Gadget *glist=NULL, *my_gads[TOTALGADGETS];
  41.   BOOL  success=TRUE;  /* FALSE if cancel was selected */
  42.   struct Node *n = NULL;
  43.   ULONG selectedline;
  44.   ULONG oldsecs=0, oldmicros=0;
  45.   ULONG oldselection=~0;
  46.  
  47.   if (IsListEmpty(list))
  48.     return(NULL);
  49.  
  50.   if (scr=LockPubScreen(NULL))
  51.   {
  52.     if (vi=GetVisualInfo(scr,TAG_END))
  53.     {
  54.  
  55.       winheight = BestWindowHeight(scr,list);
  56.       winwidth  = (2 * TextLength(&scr->RastPort,windowtitle,strlen(windowtitle))) + 40; /* 40 is for the close and depth gadgets */
  57.       if (win = OpenWindowTags(NULL,
  58.           WA_Left,   x - (winwidth/2),
  59.           WA_Top,    y - (winheight/2),
  60.           WA_Width, winwidth,
  61.           WA_Height, winheight,
  62.           WA_MinWidth, 264,
  63.           WA_MinHeight, scr->WBorTop + (2 * scr->Font->ta_YSize) + 1 + 160 + scr->WBorBottom,
  64.           WA_MaxWidth, ~0,
  65.           WA_MaxHeight, ~0,
  66.           WA_AutoAdjust, TRUE,
  67.           WA_IDCMP,  IDCMP_NEWSIZE | IDCMP_GADGETUP | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | LISTVIEWIDCMP,
  68.           WA_Flags,  WFLG_SIZEGADGET | WFLG_SIZEBBOTTOM | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH | WFLG_ACTIVATE,
  69.           WA_Title,  windowtitle,
  70.           WA_PubScreen, scr,
  71.           TAG_END))
  72.       {
  73.         winsigflag = 1L<<win->UserPort->mp_SigBit;
  74.  
  75.         if (RenderGadgets(&glist, vi, win, my_gads, list, createChooseGroupGadgets))
  76.         {
  77.  
  78.           LOOP=TRUE;
  79.           while (LOOP)
  80.           {
  81.             signals=Wait(SIGBREAKF_CTRL_C | winsigflag);
  82.  
  83.             if (signals & SIGBREAKF_CTRL_C)
  84.               LOOP=FALSE;
  85.  
  86.             if (signals & winsigflag)
  87.             {
  88.               while (winmsg = (struct IntuiMessage *)GT_GetIMsg((struct MsgPort *)win->UserPort))
  89.               {
  90.                 switch(winmsg->Class)
  91.                 {
  92.                   case IDCMP_NEWSIZE:
  93.                     RemoveGList(win,glist,-1);
  94.                     FreeGadgets(glist);
  95.                     RefreshWindowFrame(win);
  96.                     RenderGadgets(&glist, vi, win, my_gads, list, createChooseGroupGadgets);
  97.                     break;
  98.                   case IDCMP_GADGETUP:
  99.                     switch(((struct Gadget *)winmsg->IAddress)->GadgetID)
  100.                     {
  101.                       case SAVE_GAD:
  102.                         LOOP=FALSE;
  103.                         break;
  104.                       case CANCEL_GAD:
  105.                         success=FALSE;
  106.                         LOOP=FALSE;
  107.                         break;
  108.                       case LIST_GAD:
  109.                         if ((winmsg->Code == oldselection) && (DoubleClick(oldsecs, oldmicros, winmsg->Seconds, winmsg->Micros)))
  110.                             LOOP= FALSE;
  111.                           else
  112.                           {
  113.                             oldsecs = winmsg->Seconds;
  114.                             oldmicros = winmsg->Micros;
  115.                             oldselection = winmsg->Code;
  116.                           }
  117.                         break;
  118.                     }
  119.                     break;
  120.                   case IDCMP_REFRESHWINDOW:
  121.                     BeginRefresh(win);
  122.                     EndRefresh(win, TRUE);
  123.                     break;
  124.                   case IDCMP_CLOSEWINDOW:
  125.                     LOOP=FALSE;
  126.                     break;
  127.                 }
  128.                 GT_ReplyIMsg(winmsg);
  129.               }
  130.             }  /* End of process window events */
  131.           }   /* End of while (LOOP) */
  132.  
  133.           GT_GetGadgetAttrs(my_gads[LIST_GAD],win,NULL,GTLV_Selected, &selectedline);
  134.  
  135.           if ((selectedline == ((ULONG)~0)) || (!success))
  136.             n=NULL;
  137.           else
  138.             for (n = list->lh_Head; selectedline; selectedline--)
  139.               n = n->ln_Succ;
  140.  
  141.           RemoveGList(win,glist,-1);
  142.         }
  143.         FreeGadgets(glist);
  144.         CloseWindow(win);
  145.       }  /* End of if (OpenWindowTags()) */
  146.       FreeVisualInfo(vi);
  147.     }
  148.     UnlockPubScreen(NULL,scr);
  149.   }
  150.  
  151.  
  152.   return(n);
  153. }
  154.  
  155. struct Gadget *createChooseGroupGadgets(struct Gadget **glistptr, void *vi, struct Window *win, struct Gadget *my_gads[], struct List *list)
  156. {
  157.   struct NewGadget ng;
  158.   struct Gadget *gad;
  159.   ULONG  buttonwidth,maxbuttonwidth;
  160.   ULONG  oktextwidth,canceltextwidth;
  161.   #define OUTSIDEBORDER 10
  162.   #define GADTEXTBORDERSUM 6
  163.  
  164.   gad = CreateContext(glistptr);
  165.  
  166.   ng.ng_LeftEdge = win->BorderLeft+OUTSIDEBORDER;
  167.   ng.ng_TopEdge = win->BorderTop+OUTSIDEBORDER;
  168.   ng.ng_Width = win->Width - win->BorderRight-win->BorderLeft-(2 * OUTSIDEBORDER);
  169.   ng.ng_Height = win->Height-win->BorderTop-OUTSIDEBORDER-8-win->WScreen->Font->ta_YSize - GADTEXTBORDERSUM - 4 - win->BorderBottom;
  170.   ng.ng_GadgetText = NULL;
  171.   ng.ng_TextAttr = win->WScreen->Font;
  172.   ng.ng_VisualInfo = vi;
  173.   ng.ng_GadgetID = LIST_GAD;
  174.   ng.ng_Flags = 0;
  175.  
  176.   my_gads[LIST_GAD] = gad = CreateGadget(LISTVIEW_KIND,gad,&ng,
  177.       GTLV_Labels,list,
  178.       GTLV_ShowSelected,0,
  179.       LAYOUTA_Spacing,2,
  180.       TAG_DONE);
  181.  
  182.   /* Determine the optimal width of the Ok and Cancel gadgets */
  183.   canceltextwidth = TextLength(&win->WScreen->RastPort,GetString(STRCancel),strlen(GetString(STRCancel)));
  184.   oktextwidth   = TextLength(&win->WScreen->RastPort,GetString(STROk),strlen(GetString(STROk)));
  185.   maxbuttonwidth = 2 * max(canceltextwidth,oktextwidth);
  186.   buttonwidth = min(maxbuttonwidth , ((win->Width - win->BorderLeft - win->BorderRight- (OUTSIDEBORDER*2))/2)-8);
  187.  
  188.   ng.ng_TopEdge = win->Height - (win->WScreen->Font->ta_YSize + GADTEXTBORDERSUM + 4 + win->BorderBottom);
  189.   ng.ng_Width = buttonwidth;
  190.   ng.ng_Height = win->WScreen->Font->ta_YSize + GADTEXTBORDERSUM;
  191.   ng.ng_GadgetText = GetString(STROk);
  192.   ng.ng_GadgetID = SAVE_GAD;
  193.  
  194.   my_gads[SAVE_GAD] = gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  195.  
  196.   ng.ng_LeftEdge = win->Width-win->BorderRight - OUTSIDEBORDER-buttonwidth;
  197.   ng.ng_GadgetText = GetString(STRCancel);
  198.   ng.ng_GadgetID = CANCEL_GAD;
  199.  
  200.   my_gads[CANCEL_GAD] = gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_DONE);
  201.  
  202.   return(gad);
  203. }
  204.  
  205.