home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 492.lha / ToolManager_v1.3 / src / window.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-06  |  12.5 KB  |  430 lines

  1. /*
  2.  * window.c   V1.3
  3.  *
  4.  * status window
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Structures for window */
  12. extern UBYTE WindowTitle[];
  13. extern struct NewWindow nw;
  14. #define WDRAGBARLEN 60       /* Width of Close & Drag gadget */
  15. static struct Window *w=NULL;
  16. static struct AppWindow *aw;
  17. static struct MsgPort *wp;
  18. static ULONG wl=20,wt=20;    /* Window leftedge & topedge */
  19. static ULONG ww,wh;          /* Window width & height */
  20. static struct Screen *pubsc; /* Workbench screen */
  21.  
  22. /* Structures for window gadgets */
  23. static void *vi;             /* Visual information is a *PRIVATE* data field! */
  24. static struct Gadget *gl;    /* Gadget list */
  25. static char StatusText[]="Active Tools: 00"; /* Text gadget text */
  26. static struct Gadget *lvgad;                 /* ListView gadget */
  27. static WORD lvord=-1;                        /* LV gadget ordinal number */
  28. static WORD lvtop=0;                         /* LV gadget top item number */
  29. #define LVGAD_ID 1
  30. static char ETButtonText[]="Edit Tool Definition"; /* Edit Tool gadget text */
  31. #define ETGAD_ID 2
  32. static char RTButtonText[]="Remove Tool";    /* Remove Tool gadget text */
  33. #define RTGAD_ID 3
  34. char SCButtonText[]="Save Configuration";    /* Save config gadget text */
  35. #define SCGAD_ID 4
  36.  
  37. /* Create & handle edit window */
  38. static BOOL EditToolWindow(struct ToolNode *tn)
  39. {
  40.  struct NewGadget ng;
  41.  struct Gadget *g,*gl=NULL;
  42.  static struct TextAttr ta={"topaz.font",8,0,0};
  43.  struct TextFont *f;              /* Window font */
  44.  UWORD topborder,fheight;
  45.  ULONG wh;
  46.  struct Window *ew;
  47.  struct MsgPort *wp;
  48.  BOOL rc=TRUE,end=TRUE;
  49.  char buf[BUFLEN];
  50.  
  51.  if (!(pubsc=LockPubScreen("Workbench"))) /* Lock Workbench screen */
  52.   goto e1;
  53.  
  54.  if (!(f=OpenFont(pubsc->Font))) goto e2; /* Open window font */
  55.  topborder=pubsc->WBorTop+f->tf_YSize+1;
  56.  CloseFont(f);                            /* Close font */
  57.  
  58.  if (!(f=OpenFont(&ta))) goto e2;         /* Open window font */
  59.  fheight=f->tf_YSize;                     /* Font height */
  60.  CloseFont(f);                            /* Close font */
  61.  
  62.  /* Create gadget list */
  63.  gl=NULL;
  64.  g=CreateContext(&gl);
  65.  
  66.  /* Create edit gadget */
  67.  BuildConfigLine(tn,buf);
  68.  ng.ng_LeftEdge=pubsc->WBorLeft+INTERWIDTH;
  69.  ng.ng_TopEdge=topborder+INTERHEIGHT;
  70.  ng.ng_Height=fheight+INTERHEIGHT+2;
  71.  ng.ng_Width=ww;
  72.  ng.ng_TextAttr=&ta;
  73.  ng.ng_GadgetText=NULL;
  74.  ng.ng_Flags=PLACETEXT_IN;
  75.  ng.ng_VisualInfo=vi;
  76.  ng.ng_UserData=0;
  77.  if (!(g=CreateGadget(STRING_KIND,g,&ng,
  78.                       GTST_String,buf,
  79.                       GTST_MaxChars,BUFLEN-1,
  80.                       TAG_DONE)))
  81.   goto e3;
  82.  
  83.  /* Calculate window height */
  84.  wh=ng.ng_TopEdge+g->Height+2*INTERHEIGHT-topborder;
  85.  
  86.  /* Open window */
  87.  if (!(ew=OpenWindowTags(&nw,
  88.                          WA_Left,w->LeftEdge-INTERWIDTH,
  89.                          WA_Top,w->TopEdge+4*wh,
  90.                          WA_InnerWidth,ww+2*INTERWIDTH,
  91.                          WA_InnerHeight,wh,
  92.                          WA_PubScreen,pubsc,
  93.                          WA_AutoAdjust,TRUE,
  94.                          WA_Title,ETButtonText,
  95.                          TAG_DONE)))
  96.   goto e3;
  97.  
  98.  wp=ew->UserPort; /* Retrieve window port */
  99.  
  100.  /* Circumvent an intuition.library bug. See AutoDocs for LockPubScreen() */
  101.  UnlockPubScreen(NULL,pubsc);
  102.  pubsc=NULL;
  103.  
  104.  /* Add gadget list to window */
  105.  AddGList(ew,gl,(UWORD) -1,(UWORD) -1,NULL);
  106.  RefreshGList(gl,ew,NULL,(UWORD) -1);
  107.  GT_RefreshWindow(ew,NULL);
  108.  ActivateGadget(g,ew,NULL);
  109.  
  110.  /* All OK */
  111.  rc=FALSE;
  112.  
  113.  while (end)
  114.   {
  115.    struct IntuiMessage *msg;
  116.  
  117.    WaitPort(wp);
  118.  
  119.    while (msg=GT_GetIMsg(wp))
  120.     {
  121.      switch (msg->Class)
  122.       {
  123.        case INTUITICKS:           /* Intuition tick received */
  124.         break;
  125.        case CLOSEWINDOW:          /* User clicked the close gadget */
  126.         end=FALSE;                /* Yes, close window */
  127.         break;
  128.        case REFRESHWINDOW:        /* Window must be refreshed */
  129.         GT_BeginRefresh(ew);
  130.         GT_EndRefresh(ew,TRUE);
  131.         break;
  132.        case GADGETUP:             /* User released the gadget */
  133.         /* Config line changed? */
  134.         if (strcmp(buf,((struct StringInfo *) g->SpecialInfo)->Buffer))
  135.          {
  136.           /* Yes, copy new config line */
  137.           strcpy(buf,((struct StringInfo *) g->SpecialInfo)->Buffer);
  138.  
  139.           /* Build new ToolNode */
  140.           DetachToolList();
  141.           if (ScanConfigLine(buf,tn->tn_DirLock))
  142.            {                        /* ToolNode created? */
  143.             RemToolNode(tn);        /* Yes, remove old tool */
  144.             lvord=-1;               /* Invalidate ordinal number */
  145.             end=FALSE;              /* Close window */
  146.            }
  147.           else                      /* No, reactivate Gadget */
  148.            {
  149.             DisplayBeep(NULL);
  150.             ActivateGadget(g,ew,NULL);
  151.            }
  152.  
  153.           AttachToolList();
  154.          }
  155.         else end=FALSE; /* Definition not changed, but close edit window */
  156.         break;
  157.       }
  158.      GT_ReplyIMsg(msg);
  159.     }
  160.   }
  161.  
  162.     CloseWindow(ew);
  163. e3: FreeGadgets(gl);
  164. e2: if (pubsc) UnlockPubScreen(NULL,pubsc);
  165. e1: return(rc);
  166. }
  167.  
  168. /* Create status line */
  169. static void PrintStatusLine(void)
  170. {
  171.  StatusText[14]=ToolCount/10+'0'; /* Hack */
  172.  StatusText[15]=ToolCount%10+'0';
  173. }
  174.  
  175. /* Create all status window gadgets */
  176. static BOOL CreateWindowGadgets(void)
  177. {
  178.  struct NewGadget ng;
  179.  struct Gadget *g;
  180.  struct TextAttr *ta=pubsc->Font;
  181.  struct TextFont *f;              /* Window font */
  182.  struct RastPort tmprp;           /* RastPort for font-sensitive trick */
  183.  UWORD width,fheight;
  184.  UWORD leftedge,topborder;
  185.  UWORD lvright;
  186.  
  187.  /* Open window font */
  188.  if (!(f=OpenFont(ta))) return(FALSE);
  189.  fheight=f->tf_YSize; /* Font height */
  190.  leftedge=pubsc->WBorLeft;
  191.  topborder=pubsc->WBorTop+fheight+1;
  192.  
  193.  /* Initialize temporary RastPort */
  194.  InitRastPort(&tmprp);
  195.  SetFont(&tmprp,f);
  196.  
  197.  /* Calculate window width */
  198.  ww=TextLength(&tmprp,WindowTitle,strlen(WindowTitle))+WDRAGBARLEN;
  199.  
  200.  /* Create gadget list */
  201.  gl=NULL;
  202.  g=CreateContext(&gl);
  203.  
  204.  /* 1. gadget: Text, status line */
  205.  PrintStatusLine();
  206.  width=TextLength(&tmprp,StatusText,sizeof(StatusText)-1)+INTERWIDTH;
  207.  ng.ng_LeftEdge=leftedge+(ww-width)/2;
  208.  ng.ng_TopEdge=topborder+INTERHEIGHT;
  209.  ng.ng_Width=width;
  210.  ng.ng_Height=fheight;
  211.  ng.ng_GadgetText=StatusText;
  212.  ng.ng_TextAttr=ta;
  213.  ng.ng_Flags=PLACETEXT_IN;
  214.  ng.ng_VisualInfo=vi;
  215.  ng.ng_UserData=0;
  216.  if (!(g=CreateGadget(TEXT_KIND,g,&ng,TAG_DONE))) goto e1;
  217.  g->GadgetText->DrawMode=JAM2; /* Argh, why doesn't exist a tag for this? */
  218.  
  219.  /* 2. gadget: ListView, tool list */
  220.  ng.ng_LeftEdge=leftedge+WDRAGBARLEN/2;
  221.  ng.ng_TopEdge+=g->Height+INTERHEIGHT;
  222.  ng.ng_Width=ww-WDRAGBARLEN;
  223.  ng.ng_Height=7*fheight;
  224.  ng.ng_GadgetText=NULL;
  225.  ng.ng_GadgetID=LVGAD_ID;
  226.  if (!(lvgad=g=CreateGadget(LISTVIEW_KIND,g,&ng,
  227.                             GTLV_Labels,&ToolList,
  228.                             /* If an item was selected, use it as top item */
  229.                             GTLV_Top,lvtop,
  230.                             GTLV_ShowSelected,NULL,
  231.                             GTLV_Selected,lvord,
  232.                             TAG_DONE))) goto e1;
  233.  lvright=ng.ng_LeftEdge+g->Width;
  234.  
  235.  /* 3. gadget: Button, edit tool definition */
  236.  ng.ng_TopEdge+=g->Height+fheight+2*INTERHEIGHT;
  237.  ng.ng_Width=TextLength(&tmprp,ETButtonText,sizeof(ETButtonText)-1)+INTERWIDTH;
  238.  ng.ng_Height=fheight+INTERHEIGHT;
  239.  ng.ng_GadgetText=ETButtonText;
  240.  ng.ng_GadgetID=ETGAD_ID;
  241.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto e1;
  242.  
  243.  /* 4. gadget: Button, remove tool */
  244.  width=TextLength(&tmprp,RTButtonText,sizeof(RTButtonText)-1)+INTERWIDTH;
  245.  ng.ng_LeftEdge=lvright-width;
  246.  ng.ng_Width=width;
  247.  ng.ng_GadgetText=RTButtonText;
  248.  ng.ng_GadgetID=RTGAD_ID;
  249.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto e1;
  250.  
  251.  /* 5. gadget: Button, save config */
  252.  width=TextLength(&tmprp,SCButtonText,sizeof(SCButtonText)-1)+INTERWIDTH;
  253.  ng.ng_LeftEdge=leftedge+(ww-width)/2;
  254.  ng.ng_TopEdge+=g->Height+INTERHEIGHT;
  255.  ng.ng_Width=width;
  256.  ng.ng_GadgetText=SCButtonText;
  257.  ng.ng_GadgetID=SCGAD_ID;
  258.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto e1;
  259.  
  260.  /* Calculate window height */
  261.  wh=ng.ng_TopEdge+g->Height+INTERHEIGHT-topborder;
  262.  
  263.  CloseFont(f);        /* Close window font */
  264.  if (g) return(TRUE); /* All gadgets created! */
  265.  
  266.  /* Something went wrong.... */
  267. e1: CloseFont(f);
  268.     FreeGadgets(gl);
  269.     return(FALSE);
  270. }
  271.  
  272. /* Open status window */
  273. ULONG OpenStatusWindow(void)
  274. {
  275.  if (!(pubsc=LockPubScreen("Workbench"))) /* Lock Workbench screen */
  276.   goto osw1;
  277.  
  278.  if (!(vi=GetVisualInfo(pubsc,TAG_DONE))) /* Get visual information */
  279.   goto osw2;
  280.  
  281.  if (!CreateWindowGadgets())              /* Create Gadgets */
  282.   goto osw3;
  283.  
  284.  /* Open window */
  285.  if (!(w=OpenWindowTags(&nw,
  286.                         WA_Left,wl,
  287.                         WA_Top,wt,
  288.                         WA_InnerWidth,ww,
  289.                         WA_InnerHeight,wh,
  290.                         WA_PubScreen,pubsc,
  291.                         WA_AutoAdjust,TRUE,
  292.                         TAG_DONE)))
  293.   goto osw4;
  294.  
  295.  wp=w->UserPort; /* Retrieve window port */
  296.  
  297.  /* Circumvent an intuition.library bug. See AutoDocs for LockPubScreen() */
  298.  UnlockPubScreen(NULL,pubsc);
  299.  pubsc=NULL;
  300.  
  301.  /* Add gadget list to window */
  302.  AddGList(w,gl,(UWORD) -1,(UWORD) -1,NULL);
  303.  RefreshGList(gl,w,NULL,(UWORD) -1);
  304.  GT_RefreshWindow(w,NULL);
  305.  
  306.  /* Notify Workbench about window */
  307.  if (!(aw=AddAppWindow(NULL,NULL,w,MyMP,TAG_DONE)))
  308.   goto osw5;
  309.  
  310.  /* Window open! */
  311.  return(1L<<wp->mp_SigBit);
  312.  
  313.  /* Something went wrong.... */
  314. osw5: CloseWindow(w);
  315.       w=NULL;
  316. osw4: FreeGadgets(gl);
  317. osw3: FreeVisualInfo(vi);
  318. osw2: if (pubsc) UnlockPubScreen(NULL,pubsc);
  319. osw1: return(0);
  320. }
  321.  
  322. /* Refresh status window gadgets */
  323. void RefreshStatusWindow(void)
  324. {
  325.  PrintStatusLine();
  326.  RefreshGList(gl,w,NULL,2); /* Refresh only the first two gadgets */
  327. }
  328.  
  329. /* Find tool with lvord */
  330. static struct ToolNode *FindTool(void)
  331. {
  332.  if (lvord>=0)             /* Is the ordinal number valid? */
  333.   {                        /* Yes, search tool */
  334.    struct ToolNode *tn;
  335.    WORD i=0;               /* Counter */
  336.  
  337.    /* Scan tool list until the ordinal number is reached */
  338.    for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn),i++)
  339.     if (i>=lvord) break;
  340.  
  341.    return(tn);
  342.   }
  343.  return(NULL);
  344. }
  345.  
  346. /* If the window is open, detach tool list from ListView gadget */
  347. void DetachToolList(void)
  348. {
  349.  if (w) GT_SetGadgetAttrs(lvgad,w,NULL,GTLV_Labels,-1,TAG_DONE);
  350. }
  351.  
  352. /* If the window is open, attach tool list to ListView gadget */
  353. void AttachToolList(void)
  354. {
  355.  if (w) GT_SetGadgetAttrs(lvgad,w,NULL,
  356.                           GTLV_Labels,&ToolList,
  357.                           /* If an item was selected, use it as top item */
  358.                           GTLV_Top,lvtop,
  359.                           GTLV_Selected,lvord,
  360.                           TAG_DONE);
  361. }
  362.  
  363. /* Handle window events */
  364. BOOL HandleWindowEvent(void)
  365. {
  366.  BOOL rc=FALSE;             /* True if window close event */
  367.  struct IntuiMessage *msg;
  368.  struct ToolNode *tn;
  369.  
  370.  while (msg=GT_GetIMsg(wp)) /* Pre-process Intuition messages */
  371.   {
  372.    switch (msg->Class)
  373.     {
  374.      case INTUITICKS:       /* Intuition tick received */
  375.       break;
  376.      case CLOSEWINDOW:      /* User clicked the close gadget */
  377.       wl=w->LeftEdge;       /* Retreive window parameters for next open */
  378.       wt=w->TopEdge;
  379.       rc=TRUE;              /* Yes, close window */
  380.       break;
  381.      case REFRESHWINDOW:    /* Window must be refreshed */
  382.       GT_BeginRefresh(w);
  383.       GT_EndRefresh(w,TRUE);
  384.       break;
  385.      case GADGETUP:         /* User released a gadget */
  386.       switch(((struct Gadget *) msg->IAddress)->GadgetID)
  387.        {
  388.         case LVGAD_ID:      /* User selected a ListView item */
  389.          lvtop=lvord=msg->Code; /* Retrieve the ordinal number of the item */
  390.          break;
  391.         case RTGAD_ID:      /* User selected the remove tool gadget */
  392.          tn=FindTool();     /* Search tool */
  393.          if (tn)            /* Tool found? */
  394.           {
  395.            lvord=-1;              /* Invalidate ordinal number */
  396.  
  397.            DetachToolList();      /* Remove tool from list */
  398.            RemToolNode(tn);
  399.            AttachToolList();
  400.  
  401.            RefreshStatusWindow(); /* Refresh Gadgets */
  402.           }
  403.          break;
  404.         case ETGAD_ID:      /* User selected the edit tool definition gadget */
  405.          tn=FindTool();
  406.          if (tn)
  407.           if (EditToolWindow(tn)) DisplayBeep(NULL);
  408.          break;
  409.         case SCGAD_ID:      /* User selected the save config gadget */
  410.          if (WriteConfigFile(w)) DisplayBeep(NULL);
  411.          break;
  412.        }
  413.       break;
  414.     }
  415.    GT_ReplyIMsg(msg); /* Reply pre-processed message */
  416.   }
  417.  
  418.  return rc;
  419. }
  420.  
  421. /* Close status window */
  422. void CloseStatusWindow(void)
  423. {
  424.  RemoveAppWindow(aw);
  425.  CloseWindow(w);
  426.  w=NULL;
  427.  FreeGadgets(gl);     /* Release allocated resources */
  428.  FreeVisualInfo(vi);
  429. }
  430.