home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 458.lha / ToolManager_v1.1 / window.c < prev   
C/C++ Source or Header  |  1991-01-02  |  7KB  |  235 lines

  1. #include "ToolManager.h"
  2.  
  3. /* Structures for window */
  4. extern UBYTE WindowTitle[];
  5. extern struct NewWindow nw;
  6. extern struct TagItem wtags[];
  7. #define WTAG_WIDTH 0
  8. #define WTAG_HEIGHT 1
  9. #define WTAG_PUBSCR 3
  10. #define WDRAGBARLEN 60       /* Width of Close & Drag gadget */
  11. static struct Window *w=NULL;
  12. static struct AppWindow *aw;
  13. static struct MsgPort *wp;
  14. static ULONG ww,wh;          /* Window width & height */
  15. static struct Screen *pubsc; /* Workbench screen */
  16.  
  17. /* Structures for window gadgets */
  18. static void *vi;             /* Visual information is a *PRIVATE* data field! */
  19. static struct Gadget *gl;    /* Gadget list */
  20. static char StatusText[]="Active Tools: 00"; /* Text gadget text */
  21. static struct Gadget *lvgad;                 /* ListView gadget */
  22. #define LVGAD_ID 1
  23. static char ButtonText[]="Remove Tool";      /* Button gadget text */
  24. #define BUGAD_ID 2
  25.  
  26. /* Create status line */
  27. static void PrintStatusLine(void)
  28. {
  29.  StatusText[14]=ToolCount/10+'0'; /* Hack */
  30.  StatusText[15]=ToolCount%10+'0';
  31. }
  32.  
  33. /* Create all status window gadgets */
  34. static BOOL CreateWindowGadgets(void)
  35. {
  36.  struct NewGadget ng;
  37.  struct Gadget *g;
  38.  struct TextAttr *ta=pubsc->Font;
  39.  struct TextFont *f;              /* Window font */
  40.  struct RastPort tmprp;           /* RastPort for font-sensitive trick */
  41.  UWORD width,fheight;
  42.  
  43.  /* Open window font */
  44.  if (!(f=OpenFont(ta))) return(FALSE);
  45.  fheight=f->tf_YSize; /* Font height */
  46.  
  47.  /* Initialize temporary RastPort */
  48.  InitRastPort(&tmprp);
  49.  SetFont(&tmprp,f);
  50.  
  51.  /* Calculate window width */
  52.  ww=TextLength(&tmprp,WindowTitle,strlen(WindowTitle))+WDRAGBARLEN;
  53.  
  54.  /* Create gadget list */
  55.  gl=NULL;
  56.  g=CreateContext(&gl);
  57.  
  58.  /* 1. gadget: Text, status line */
  59.  PrintStatusLine();
  60.  width=TextLength(&tmprp,StatusText,sizeof(StatusText)-1);
  61.  ng.ng_LeftEdge=(ww-width)/2;
  62.  ng.ng_TopEdge=INTERHEIGHT;
  63.  ng.ng_Width=width;
  64.  ng.ng_Height=fheight;
  65.  ng.ng_GadgetText=StatusText;
  66.  ng.ng_TextAttr=ta;
  67.  ng.ng_Flags=PLACETEXT_IN;
  68.  ng.ng_VisualInfo=vi;
  69.  ng.ng_UserData=0;
  70.  g=CreateGadget(TEXT_KIND,g,&ng,TAG_DONE);
  71.  g->GadgetText->DrawMode=JAM2; /* Argh, why doesn't exist a tag for this? */
  72.  
  73.  /* 2. gadget: ListView, tool list */
  74.  ng.ng_LeftEdge=WDRAGBARLEN/2;
  75.  ng.ng_TopEdge+=g->Height+INTERHEIGHT;
  76.  ng.ng_Width=ww-WDRAGBARLEN;
  77.  ng.ng_Height=7*fheight;
  78.  ng.ng_GadgetText=NULL;
  79.  ng.ng_GadgetID=LVGAD_ID;
  80.  lvgad=g=CreateGadget(LISTVIEW_KIND,g,&ng,
  81.                       GTLV_Labels,&ToolList,
  82.                       GTLV_ShowSelected,NULL,
  83.                       TAG_DONE);
  84.  
  85.  /* 3. gadget: Button, remove tool */
  86.  ng.ng_TopEdge+=g->Height+fheight+2*INTERHEIGHT;
  87.  ng.ng_Width=TextLength(&tmprp,ButtonText,sizeof(ButtonText)-1)+INTERWIDTH;
  88.  ng.ng_Height=fheight+INTERHEIGHT;
  89.  ng.ng_GadgetText=ButtonText;
  90.  ng.ng_GadgetID=BUGAD_ID;
  91.  g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
  92.  
  93.  /* Calculate window height */
  94.  wh=ng.ng_TopEdge+g->Height+INTERHEIGHT;
  95.  
  96.  /* Close window font */
  97.  CloseFont(f);
  98.  
  99.  /* All gadgets created! */
  100.  if (g) return(TRUE);
  101.  
  102.  /* Something went wrong.... */
  103.  FreeGadgets(gl);
  104.  return(FALSE);
  105. }
  106.  
  107. /* Open status window */
  108. ULONG OpenStatusWindow(void)
  109. {
  110.  if (!(pubsc=LockPubScreen("Workbench"))) /* Lock Workbench screen */
  111.   goto osw1;
  112.  
  113.  if (!(vi=GetVisualInfo(pubsc,TAG_DONE))) /* Get visual information */
  114.   goto osw2;
  115.  
  116.  if (!CreateWindowGadgets())              /* Create Gadgets */
  117.   goto osw3;
  118.  
  119.  /* Open window */
  120.  wtags[WTAG_WIDTH ].ti_Data=ww;
  121.  wtags[WTAG_HEIGHT].ti_Data=wh;
  122.  wtags[WTAG_PUBSCR].ti_Data=pubsc;
  123.  if (!(w=OpenWindowTagList(&nw,wtags)))
  124.   goto osw4;
  125.  wp=w->UserPort; /* Retrieve window port */
  126.  
  127.  /* Circumvent an intuition.library bug. See AutoDocs for LockPubScreen() */
  128.  UnlockPubScreen(NULL,pubsc);
  129.  pubsc=NULL;
  130.  
  131.  /* Notify Workbench about window */
  132.  if (!(aw=AddAppWindow(NULL,NULL,w,MyMP,NULL)))
  133.   goto osw5;
  134.  
  135.  /* Add gadget list to window */
  136.  AddGList(w,gl,(UWORD) -1,(UWORD) -1,NULL);
  137.  RefreshGList(gl,w,NULL,(UWORD) -1);
  138.  GT_RefreshWindow(w,NULL);
  139.  
  140.  /* Window open! */
  141.  return(1L<<wp->mp_SigBit);
  142.  
  143.  /* Something went wrong.... */
  144. osw5: CloseWindow(w);
  145.       w=NULL;
  146. osw4: FreeGadgets(gl);
  147. osw3: FreeVisualInfo(vi);
  148. osw2: if (pubsc) UnlockPubScreen(NULL,pubsc);
  149. osw1: return(0);
  150. }
  151.  
  152. /* Refresh status window gadgets */
  153. void RefreshStatusWindow(void)
  154. {
  155.  PrintStatusLine();
  156.  RefreshGList(gl,w,NULL,2); /* Refresh only the first two gadgets */
  157. }
  158.  
  159. /* If the window is open, detach tool list from ListView gadget */
  160. void DetachToolList(void)
  161. {
  162.  if (w) GT_SetGadgetAttrs(lvgad,w,NULL,GTLV_Labels,-1,TAG_DONE);
  163. }
  164.  
  165. /* If the window is open, attach tool list to ListView gadget */
  166. void AttachToolList(void)
  167. {
  168.  if (w) GT_SetGadgetAttrs(lvgad,w,NULL,GTLV_Labels,&ToolList,TAG_DONE);
  169. }
  170.  
  171. /* Handle window events */
  172. BOOL HandleWindowEvent(void)
  173. {
  174.  static WORD lvord=-1;      /* ListView gadget ordinal number */
  175.  BOOL rc=FALSE;             /* True if window close event */
  176.  struct IntuiMessage *msg;
  177.  
  178.  while (msg=GT_GetIMsg(wp)) /* Pre-process Intuition messages */
  179.   {
  180.    switch (msg->Class)
  181.     {
  182.      case INTUITICKS:       /* Intuition tick received */
  183.       break;
  184.      case CLOSEWINDOW:      /* User clicked the close gadget */
  185.       rc=TRUE;
  186.       break;
  187.      case REFRESHWINDOW:    /* Window must be refreshed */
  188.       GT_BeginRefresh(w);
  189.       GT_EndRefresh(w,TRUE);
  190.       break;
  191.      case GADGETUP:         /* User released a gadget */
  192.       switch(((struct Gadget *) msg->IAddress)->GadgetID)
  193.        {
  194.         case LVGAD_ID:      /* User selected a ListView item */
  195.          lvord=msg->Code;   /* Retrieve the ordinal number of the item */
  196.          break;
  197.         case BUGAD_ID:      /* User selected the button gadget */
  198.          if (lvord>=0)      /* Is the ordinal number valid? */
  199.           {                 /* Yes, search tool and remove it */
  200.            WORD i=0;        /* Counter */
  201.            struct ToolNode *tn;
  202.  
  203.            /* Scan tool list until the ordinal number is reached */
  204.            for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn),i++)
  205.             if (i>=lvord) break;
  206.  
  207.            /* Remove tool from list */
  208.            DetachToolList();
  209.            RemToolNode(tn);
  210.            AttachToolList();
  211.  
  212.            /* Refresh Gadgets */
  213.            RefreshStatusWindow();
  214.            lvord=-1;        /* invalidate ordinal number */
  215.           }
  216.          break;
  217.        }
  218.       break;
  219.     }
  220.    GT_ReplyIMsg(msg); /* Reply pre-processed message */
  221.   }
  222.  
  223.  return rc;
  224. }
  225.  
  226. /* Close status window */
  227. void CloseStatusWindow(void)
  228. {
  229.  RemoveAppWindow(aw);
  230.  CloseWindow(w);
  231.  w=NULL;
  232.  FreeGadgets(gl);    /* Release allocated resources */
  233.  FreeVisualInfo(vi);
  234. }
  235.