home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / toolmng.lzh / ToolManager / Source / statuswindow.c < prev    next >
C/C++ Source or Header  |  1991-10-10  |  32KB  |  1,139 lines

  1. /*
  2.  * statuswindow.c   V1.5
  3.  *
  4.  * status window
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Structures for window */
  12. static char *wtb;
  13. extern struct NewWindow nw;
  14. static struct Window *w;
  15. static struct AppWindow *aw;
  16. static struct MsgPort *wp;
  17. static ULONG wl=20,wt=20;    /* Window leftedge & topedge */
  18. static ULONG ww,wh;          /* Window width & height */
  19. static struct Screen *pubsc; /* Workbench screen */
  20.  
  21. /* Structures for window gadgets */
  22. static void *vi;             /* Visual information is a *PRIVATE* data field! */
  23. extern struct GfxBase *GfxBase;
  24. static struct TextAttr ta={NULL,0,0,0};
  25. static struct Gadget *gl;    /* Gadget list */
  26. static char StatusText[]="Active Tools: 00"; /* Text gadget text */
  27. static struct Gadget *tlvg;                  /* ListView gadget */
  28. WORD lvord=-1;                               /* LV gadget ordinal number */
  29. static WORD lvtop=0;                         /* LV gadget top item number */
  30. static ULONG oldsecs=0,oldmicros=0;          /* LV double click check */
  31. #define LVGAD_ID  1
  32. static char TPButtonText[]="Top";            /* Move tool to top gadget text */
  33. static struct Gadget *tpbg;
  34. #define TPGAD_ID  2
  35. static char UPButtonText[]="Up";             /* Move tool one up gadget text */
  36. static struct Gadget *upbg;
  37. #define UPGAD_ID  3
  38. static char DNButtonText[]="Down";           /* Move tool one down gad. text */
  39. static struct Gadget *dnbg;
  40. #define DNGAD_ID  4
  41. static char BMButtonText[]="Bottom";         /* Move tool to bottom gad. text */
  42. static struct Gadget *bmbg;
  43. #define BMGAD_ID  5
  44. static char NTButtonText[]="New";            /* New tool gadget text */
  45. #define NTGAD_ID  6
  46. static char ETButtonText[]="Edit";           /* Edit tool gadget text */
  47. static struct Gadget *etbg;
  48. #define ETGAD_ID  7
  49. static char RTButtonText[]="Remove";         /* Remove tool gadget text */
  50. static struct Gadget *rtbg;
  51. #define RTGAD_ID  8
  52. static char ENButtonText[]="Enable";         /* Enable broker gadget text */
  53. static BOOL BrokerState;
  54. static struct Gadget *ebbg;
  55. #define ENGAD_ID  9
  56. static char HKButtonText[]="HotKeys";        /* HotKeys gadget text */
  57. static struct Gadget *hkbg;
  58. #define HKGAD_ID 10
  59. static char DIButtonText[]="Disable";        /* Disable broker gadget text */
  60. static struct Gadget *dbbg;
  61. #define DIGAD_ID 11
  62. static char ODButtonText[]="Dock On";        /* Dock on gadget text */
  63. static struct Gadget *odbg;
  64. #define ODGAD_ID 12
  65. static char DOButtonText[]="Dock H/V";       /* Dock orientation text */
  66. static struct Gadget *dobg;
  67. #define DOGAD_ID 13
  68. static char CDButtonText[]="Dock Off";       /* Dock off gadget text */
  69. static struct Gadget *cdbg;
  70. #define CDGAD_ID 14
  71. static char SCButtonText[]="Save";           /* Save config gadget text */
  72. #define SCGAD_ID 15
  73. static char HIButtonText[]="Hide";           /* Hide gadget text */
  74. #define HIGAD_ID 16
  75. static char QUButtonText[]="Quit";           /* Quit gadget text */
  76. #define QUGAD_ID 17
  77.  
  78. /* Structures for window menu */
  79. static struct Menu *wmn=NULL;
  80. #define NCMENU_ID   1
  81. #define OCMENU_ID   2
  82. #define ACMENU_ID   3
  83. #define SCMENU_ID   4
  84. #define SAMENU_ID   5
  85. #define HIMENU_ID   6
  86. static char ABMenuText[]="About...";
  87. #define ABMENU_ID   7
  88. #define QUMENU_ID   8
  89. #define STMENU_ID   9
  90. #define STMENU_NUM FULLMENUNUM(1,0,0)
  91. #define NTMENU_ID  10
  92. #define ETMENU_ID  11
  93. #define ETMENU_NUM FULLMENUNUM(1,2,0)
  94. #define RTMENU_ID  12
  95. #define RTMENU_NUM FULLMENUNUM(1,3,0)
  96. #define TPMENU_ID  13
  97. #define TPMENU_NUM FULLMENUNUM(1,5,0)
  98. #define UPMENU_ID  14
  99. #define UPMENU_NUM FULLMENUNUM(1,6,0)
  100. #define DNMENU_ID  15
  101. #define DNMENU_NUM FULLMENUNUM(1,7,0)
  102. #define BMMENU_ID  16
  103. #define BMMENU_NUM FULLMENUNUM(1,8,0)
  104. #define ENMENU_ID  17
  105. #define ENMENU_NUM FULLMENUNUM(2,0,0)
  106. #define DIMENU_ID  18
  107. #define DIMENU_NUM FULLMENUNUM(2,1,0)
  108. #define HLMENU_ID  19
  109. #define HLMENU_NUM FULLMENUNUM(2,2,0)
  110. #define ODMENU_ID  20
  111. #define ODMENU_NUM FULLMENUNUM(3,0,0)
  112. #define DOMENU_ID  21
  113. #define DOMENU_NUM FULLMENUNUM(3,1,0)
  114. #define CDMENU_ID  22
  115. #define CDMENU_NUM FULLMENUNUM(3,2,0)
  116. static struct NewMenu mdata[]={
  117.       {NM_TITLE,"Project"   ,NULL,0,~0,NULL},
  118.        {NM_ITEM,NTButtonText,"N",0,~0,NCMENU_ID},
  119.        {NM_ITEM,"Open..."   ,"O",0,~0,OCMENU_ID},
  120.        {NM_ITEM,"Append..." ,NULL,0,~0,ACMENU_ID},
  121.        {NM_ITEM,NM_BARLABEL ,NULL,0,~0,NULL},
  122.        {NM_ITEM,SCButtonText,"S",0,~0,SCMENU_ID},
  123.        {NM_ITEM,"Save As...","A",0,~0,SAMENU_ID},
  124.        {NM_ITEM,NM_BARLABEL ,NULL,0,~0,NULL},
  125.        {NM_ITEM,HIButtonText,"H",0,~0,HIMENU_ID},
  126.        {NM_ITEM,NM_BARLABEL ,NULL,0,~0,NULL},
  127.        {NM_ITEM,ABMenuText  ,NULL,0,~0,ABMENU_ID},
  128.        {NM_ITEM,NM_BARLABEL ,NULL,0,~0,NULL},
  129.        {NM_ITEM,QUButtonText,"Q",0,~0,QUMENU_ID},
  130.       {NM_TITLE,"Tools",NULL,0,~0,NULL},
  131.        {NM_ITEM,"Start"     ,"G",0,~0,STMENU_ID},
  132.        {NM_ITEM,NTButtonText,NULL,0,~0,NTMENU_ID},
  133.        {NM_ITEM,ETButtonText,"E",0,~0,ETMENU_ID},
  134.        {NM_ITEM,RTButtonText,NULL,0,~0,RTMENU_ID},
  135.        {NM_ITEM,NM_BARLABEL ,NULL,0,~0,NULL},
  136.        {NM_ITEM,TPButtonText,"T",0,~0,TPMENU_ID},
  137.        {NM_ITEM,UPButtonText,"<",0,~0,UPMENU_ID},
  138.        {NM_ITEM,DNButtonText,">",0,~0,DNMENU_ID},
  139.        {NM_ITEM,BMButtonText,"B",0,~0,BMMENU_ID},
  140.       {NM_TITLE,HKButtonText,NULL,0,~0,NULL},
  141.        {NM_ITEM,ENButtonText,NULL,0,~0,ENMENU_ID},
  142.        {NM_ITEM,DIButtonText,NULL,0,~0,DIMENU_ID},
  143.        {NM_ITEM,"List"      ,"L",0,~0,HLMENU_ID},
  144.       {NM_TITLE,"Dock",NULL,0,~0,NULL},
  145.        {NM_ITEM,"Open"      ,NULL,0,~0,ODMENU_ID},
  146.        {NM_ITEM,"H/V"       ,NULL,0,~0,DOMENU_ID},
  147.        {NM_ITEM,"Close"     ,"C",0,~0,CDMENU_ID},
  148.       {NM_END,NULL,NULL,0,~0,NULL}};
  149.  
  150. /* Data for about requester */
  151. static UBYTE AboutText[]="ToolManager V" TM_VERSION " (" TM_DATE ")\n"\
  152.                          "⌐ " TM_CRYEAR "  Stefan Becker\n"\
  153.                          "This program is freely distributable";
  154. struct EasyStruct AboutES={sizeof(struct EasyStruct),0,ABMenuText,AboutText,
  155.                            "Ok"};
  156.  
  157. /* Data for dock window */
  158. extern BOOL DockVertical;
  159.  
  160. /* Create status line */
  161. static void PrintStatusLine(void)
  162. {
  163.  StatusText[14]=ToolCount/10+'0'; /* Hack */
  164.  StatusText[15]=ToolCount%10+'0';
  165. }
  166.  
  167. /* Set Menu state depending on Broker state */
  168. void SetBrokerMenuState(void)
  169. {
  170.  /* Broker on or off */
  171.  if (BrokerState)
  172.   {
  173.    OffMenu(w,ENMENU_NUM);
  174.    OnMenu(w,DIMENU_NUM);
  175.    OnMenu(w,HLMENU_NUM);
  176.   }
  177.  else
  178.   {
  179.    OnMenu(w,ENMENU_NUM);
  180.    OffMenu(w,DIMENU_NUM);
  181.    OffMenu(w,HLMENU_NUM);
  182.   }
  183. }
  184.  
  185. /* Set Menu state depending on editwinsig/selection state */
  186. void SetToolMenuState(BOOL state)
  187. {
  188.  if (state)
  189.   { /* Deactivate menu entries */
  190.    OffMenu(w,STMENU_NUM);
  191.    OffMenu(w,ETMENU_NUM);
  192.    OffMenu(w,RTMENU_NUM);
  193.    OffMenu(w,TPMENU_NUM);
  194.    OffMenu(w,UPMENU_NUM);
  195.    OffMenu(w,DNMENU_NUM);
  196.    OffMenu(w,BMMENU_NUM);
  197.   }
  198.  else
  199.   { /* Activate menu entries */
  200.    OnMenu(w,STMENU_NUM);
  201.    OnMenu(w,ETMENU_NUM);
  202.    OnMenu(w,RTMENU_NUM);
  203.    OnMenu(w,TPMENU_NUM);
  204.    OnMenu(w,UPMENU_NUM);
  205.    OnMenu(w,DNMENU_NUM);
  206.    OnMenu(w,BMMENU_NUM);
  207.   }
  208. }
  209.  
  210. /* Set Menu state depending on ShowDock state */
  211. void SetDockMenuState(BOOL open)
  212. {
  213.  if (!open && DockCount)
  214.   OnMenu(w,ODMENU_NUM);
  215.  else
  216.   OffMenu(w,ODMENU_NUM);
  217.  
  218.  if (open && DockCount)
  219.   {
  220.    OnMenu(w,DOMENU_NUM);
  221.    OnMenu(w,CDMENU_NUM);
  222.   }
  223.  else
  224.   {
  225.    OffMenu(w,DOMENU_NUM);
  226.    OffMenu(w,CDMENU_NUM);
  227.   }
  228. }
  229.  
  230. /* Create all status window gadgets & menus */
  231. static BOOL CreateGadgets(void)
  232. {
  233.  struct NewGadget ng;
  234.  struct Gadget *g;
  235.  struct TextFont *f;
  236.  struct RastPort tmprp;
  237.  UWORD wg,width,fheight,fwidth;
  238.  UWORD leftedge,topborder,gadtop;
  239.  BOOL disgad=editwinsig || (lvord<0);
  240.  
  241.  if (!(f=OpenFont(pubsc->Font))) goto cg1;  /* Open window font */
  242.  topborder=pubsc->WBorTop+f->tf_YSize+1;    /* Top border offset */
  243.  
  244.  /* Calculate length of window title */
  245.  InitRastPort(&tmprp);
  246.  SetFont(&tmprp,f);
  247.  ww=TextLength(&tmprp,wtb,strlen(wtb))+WDRAGBARLEN;
  248.  CloseFont(f);                              /* Close font */
  249.  
  250.  f=GfxBase->DefaultFont;                   /* System Default Font */
  251.  ta.ta_Name=f->tf_Message.mn_Node.ln_Name;
  252.  ta.ta_YSize=fheight=f->tf_YSize;          /* Font height */
  253.  fwidth=f->tf_XSize;                       /* Font width */
  254.  leftedge=pubsc->WBorLeft+INTERWIDTH/2;
  255.  
  256.  /* Calculate window & gadget width */
  257.  wg=(sizeof(DOButtonText)-1)*fwidth+INTERWIDTH;
  258.  if ((width=4*wg+5*INTERWIDTH)>ww) ww=width;
  259.  
  260.  /* Create gadget list */
  261.  gl=NULL;
  262.  g=CreateContext(&gl);
  263.  
  264.  /*  1. gadget: Text, status line */
  265.  PrintStatusLine();
  266.  width=(sizeof(StatusText)-1)*fwidth+INTERWIDTH;
  267.  ng.ng_LeftEdge=leftedge+(ww-width)/2;
  268.  ng.ng_TopEdge=topborder+INTERHEIGHT;
  269.  ng.ng_Width=width;
  270.  ng.ng_Height=fheight;
  271.  ng.ng_GadgetText=StatusText;
  272.  ng.ng_TextAttr=&ta;
  273.  ng.ng_Flags=PLACETEXT_IN;
  274.  ng.ng_VisualInfo=vi;
  275.  ng.ng_UserData=0;
  276.  if (!(g=CreateGadget(TEXT_KIND,g,&ng,TAG_DONE))) goto cg2;
  277.  g->GadgetText->DrawMode=JAM2; /* Argh, why doesn't exist a tag for this? */
  278.  
  279.  /*  2. gadget: ListView, tool list */
  280.  ng.ng_LeftEdge=leftedge;
  281.  ng.ng_TopEdge+=fheight+INTERHEIGHT;
  282.  ng.ng_Width=ww-wg-2*INTERWIDTH;
  283.  ng.ng_Height=10*fheight;
  284.  ng.ng_GadgetText=NULL;
  285.  ng.ng_GadgetID=LVGAD_ID;
  286.  if (!(tlvg=g=CreateGadget(LISTVIEW_KIND,g,&ng,
  287.                            GTLV_Labels,&ToolList,
  288.                            /* If an item was selected, use it as top item */
  289.                            GTLV_Top,lvtop,
  290.                            GTLV_ShowSelected,NULL,
  291.                            GTLV_Selected,lvord,
  292.                            TAG_DONE)))
  293.   goto cg2;
  294.  gadtop=ng.ng_TopEdge+10*fheight+INTERHEIGHT;
  295.  
  296.  /*  3. gadget: Button, move tool to top */
  297.  ng.ng_LeftEdge=ww-wg;
  298.  ng.ng_TopEdge+=(5*fheight-7*INTERHEIGHT)/2;
  299.  ng.ng_Width=wg;
  300.  ng.ng_Height=fheight+INTERHEIGHT;
  301.  ng.ng_GadgetText=TPButtonText;
  302.  ng.ng_GadgetID=TPGAD_ID;
  303.  if (!(tpbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  304.                            GA_Disabled,disgad,
  305.                            TAG_DONE)))
  306.   goto cg2;
  307.  
  308.  /*  4. gadget: Button, move tool one up */
  309.  ng.ng_TopEdge+=fheight+2*INTERHEIGHT;
  310.  ng.ng_GadgetText=UPButtonText;
  311.  ng.ng_GadgetID=UPGAD_ID;
  312.  if (!(upbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  313.                            GA_Disabled,disgad,
  314.                            TAG_DONE)))
  315.   goto cg2;
  316.  
  317.  /*  5. gadget: Button, move tool one down */
  318.  ng.ng_TopEdge+=fheight+2*INTERHEIGHT;
  319.  ng.ng_GadgetText=DNButtonText;
  320.  ng.ng_GadgetID=DNGAD_ID;
  321.  if (!(dnbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  322.                            GA_Disabled,disgad,
  323.                            TAG_DONE)))
  324.   goto cg2;
  325.  
  326.  /*  6. gadget: Button, move tool to bottom */
  327.  ng.ng_TopEdge+=fheight+2*INTERHEIGHT;
  328.  ng.ng_GadgetText=BMButtonText;
  329.  ng.ng_GadgetID=BMGAD_ID;
  330.  if (!(bmbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  331.                            GA_Disabled,disgad,
  332.                            TAG_DONE)))
  333.   goto cg2;
  334.  
  335.  /*  7. gadget: Button, new tool */
  336.  ng.ng_LeftEdge=leftedge;
  337.  ng.ng_TopEdge=gadtop;
  338.  ng.ng_GadgetText=NTButtonText;
  339.  ng.ng_GadgetID=NTGAD_ID;
  340.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto cg2;
  341.  
  342.  /*  8. gadget: Button, edit tool definition */
  343.  ng.ng_LeftEdge=leftedge+(ww-wg)/2;
  344.  ng.ng_GadgetText=ETButtonText;
  345.  ng.ng_GadgetID=ETGAD_ID;
  346.  if (!(etbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  347.                            GA_Disabled,disgad,
  348.                            TAG_DONE)))
  349.   goto cg2;
  350.  
  351.  /*  9. gadget: Button, remove tool */
  352.  ng.ng_LeftEdge=ww-wg;
  353.  ng.ng_GadgetText=RTButtonText;
  354.  ng.ng_GadgetID=RTGAD_ID;
  355.  if (!(rtbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  356.                            GA_Disabled,disgad,
  357.                            TAG_DONE)))
  358.   goto cg2;
  359.  
  360.  /* 10. gadget: Button, enable broker */
  361.  ng.ng_LeftEdge=leftedge;
  362.  ng.ng_TopEdge+=fheight+3*INTERHEIGHT;
  363.  ng.ng_GadgetText=ENButtonText;
  364.  ng.ng_GadgetID=ENGAD_ID;
  365.  if (!(ebbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  366.                            GA_Disabled,BrokerState,
  367.                            TAG_DONE)))
  368.   goto cg2;
  369.  
  370.  /* 11. gadget: Button, HotKeys */
  371.  ng.ng_LeftEdge=leftedge+(ww-wg)/2;
  372.  ng.ng_GadgetText=HKButtonText;
  373.  ng.ng_GadgetID=HKGAD_ID;
  374.  if (!(hkbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  375.                            GA_Disabled,!BrokerState,
  376.                            TAG_DONE)))
  377.   goto cg2;
  378.  
  379.  /* 12. gadget: Button, disable broker */
  380.  ng.ng_LeftEdge=ww-wg;
  381.  ng.ng_GadgetText=DIButtonText;
  382.  ng.ng_GadgetID=DIGAD_ID;
  383.  if (!(dbbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  384.                            GA_Disabled,!BrokerState,
  385.                            TAG_DONE)))
  386.   goto cg2;
  387.  
  388.  /* 13. gadget: Button, Dock on */
  389.  ng.ng_LeftEdge=leftedge;
  390.  ng.ng_TopEdge+=fheight+3*INTERHEIGHT;
  391.  ng.ng_GadgetText=ODButtonText;
  392.  ng.ng_GadgetID=ODGAD_ID;
  393.  if (!(odbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  394.                            GA_Disabled,ShowDock || !DockCount,
  395.                            TAG_DONE)))
  396.   goto cg2;
  397.  
  398.  /* 14. gadget: Button, Dock H/V */
  399.  ng.ng_LeftEdge=leftedge+(ww-wg)/2;
  400.  ng.ng_GadgetText=DOButtonText;
  401.  ng.ng_GadgetID=DOGAD_ID;
  402.  if (!(dobg=g=CreateGadget(BUTTON_KIND,g,&ng,
  403.                            GA_Disabled,!ShowDock || !DockCount,
  404.                            TAG_DONE)))
  405.   goto cg2;
  406.  
  407.  /* 15. gadget: Button, Dock off */
  408.  ng.ng_LeftEdge=ww-wg;
  409.  ng.ng_GadgetText=CDButtonText;
  410.  ng.ng_GadgetID=CDGAD_ID;
  411.  if (!(cdbg=g=CreateGadget(BUTTON_KIND,g,&ng,
  412.                            GA_Disabled,!ShowDock || !DockCount,
  413.                            TAG_DONE)))
  414.   goto cg2;
  415.  
  416.  /* 16. gadget: Button, save config */
  417.  ng.ng_LeftEdge=leftedge;
  418.  ng.ng_TopEdge+=fheight+3*INTERHEIGHT;
  419.  ng.ng_GadgetText=SCButtonText;
  420.  ng.ng_GadgetID=SCGAD_ID;
  421.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto cg2;
  422.  
  423.  /* 17. gadget: Button, Hide */
  424.  ng.ng_LeftEdge=leftedge+(ww-wg)/2;
  425.  ng.ng_GadgetText=HIButtonText;
  426.  ng.ng_GadgetID=HIGAD_ID;
  427.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto cg2;
  428.  
  429.  /* 18. gadget: Button, Quit */
  430.  ng.ng_LeftEdge=ww-wg;
  431.  ng.ng_GadgetText=QUButtonText;
  432.  ng.ng_GadgetID=QUGAD_ID;
  433.  if (!(g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE))) goto cg2;
  434.  
  435.  /* Calculate window height */
  436.  wh=ng.ng_TopEdge+fheight+2*INTERHEIGHT-topborder;
  437.  
  438.  /* Create menus */
  439.  if (!(wmn=CreateMenus(mdata,
  440.                        GTMN_FullMenu,TRUE,
  441.                        TAG_DONE))) goto cg2;
  442.  
  443.  /* Layout menus */
  444.  if (!LayoutMenus(wmn,vi,TAG_DONE)) goto cg3;
  445.  
  446.  return(TRUE);        /* All gadgets created! */
  447.  
  448.  /* Something went wrong.... */
  449. cg3: FreeMenus(wmn);
  450. cg2: FreeGadgets(gl);
  451. cg1: return(FALSE);
  452. }
  453.  
  454. /* Open status window */
  455. void OpenStatusWindow(BOOL tofront)
  456. {
  457.  if (!(pubsc=LockPubScreen(WBScreenName))) /* Lock Workbench screen */
  458.   goto osw1;
  459.  
  460.  /* Window already open? */
  461.  if (statwinsig)
  462.   {
  463.    /* Yes. Move it to front and activate it */
  464.    WindowToFront(w);
  465.    ActivateWindow(w);
  466.  
  467.    /* Move Screen to front if needed */
  468.    if (tofront) ScreenToFront(pubsc);
  469.    return;
  470.   }
  471.  
  472.  /* Create window title */
  473.  if (!(wtb=malloc(strlen(MyName)+strlen(PopUpHotKey)+10)))
  474.   goto osw2;
  475.  strcpy(wtb,MyName);
  476.  strcat(wtb,": HotKey=");
  477.  strcat(wtb,PopUpHotKey);
  478.  
  479.  if (!(vi=GetVisualInfo(pubsc,TAG_DONE))) /* Get visual information */
  480.   goto osw3;
  481.  
  482.  if (!CreateGadgets())                    /* Create Gadgets */
  483.   goto osw4;
  484.  
  485.  /* Open window */
  486.  if (!(w=OpenWindowTags(&nw,
  487.                         WA_Left,wl,
  488.                         WA_Top,wt,
  489.                         WA_InnerWidth,ww,
  490.                         WA_InnerHeight,wh,
  491.                         WA_Title,wtb,
  492.                         WA_PubScreen,pubsc,
  493.                         WA_AutoAdjust,TRUE,
  494.                         TAG_DONE)))
  495.   goto osw5;
  496.  
  497.  wp=w->UserPort; /* Retrieve window port */
  498.  
  499.  /* Add gadget list to window */
  500.  AddGList(w,gl,(UWORD) -1,(UWORD) -1,NULL);
  501.  RefreshGList(gl,w,NULL,(UWORD) -1);
  502.  GT_RefreshWindow(w,NULL);
  503.  
  504.  /* Add menu to window */
  505.  if (!SetMenuStrip(w,wmn))
  506.   goto osw6;
  507.  SetBrokerMenuState();
  508.  SetToolMenuState(editwinsig || (lvord<0));
  509.  SetDockMenuState(ShowDock);
  510.  
  511.  /* Notify Workbench about window */
  512.  if (!(aw=AddAppWindowA(STATWINAPPID,NULL,w,MyMP,NULL)))
  513.   goto osw7;
  514.  
  515.  /* Window open! */
  516.  UnlockPubScreen(NULL,pubsc);
  517.  statwinsig=1L<<wp->mp_SigBit;
  518.  globalsigs|=statwinsig;
  519.  return;
  520.  
  521.  /* Something went wrong.... */
  522. osw7: ClearMenuStrip(w);
  523. osw6: CloseWindow(w);
  524. osw5: FreeMenus(wmn);
  525.       FreeGadgets(gl);
  526. osw4: FreeVisualInfo(vi);
  527. osw3: free(wtb);
  528. osw2: UnlockPubScreen(NULL,pubsc);
  529. osw1: return;
  530. }
  531.  
  532. /* Close status window */
  533. void CloseStatusWindow(void)
  534. {
  535.  if (statwinsig)
  536.   {
  537.    wl=w->LeftEdge;      /* Retreive window parameters for next open */
  538.    wt=w->TopEdge;
  539.    RemoveAppWindow(aw);
  540.    ClearMenuStrip(w);
  541.    CloseWindow(w);
  542.    FreeMenus(wmn);      /* Release allocated resources */
  543.    FreeGadgets(gl);
  544.    FreeVisualInfo(vi);
  545.    free(wtb);
  546.    globalsigs&=~statwinsig;
  547.    statwinsig=0;
  548.   }
  549. }
  550.  
  551. /* Refresh status window gadgets if window is open */
  552. void RefreshStatusWindow(void)
  553. {
  554.  if (statwinsig)
  555.   {
  556.    PrintStatusLine();         /* Print new status line */
  557.    RefreshGList(gl,w,NULL,2); /* Refresh only the first two gadgets */
  558.   }
  559. }
  560.  
  561. /* Set Broker state and gadgets */
  562. void SetBrokerState(BOOL state)
  563. {
  564.  /* Set new state */
  565.  BrokerState=state;
  566.  ActivateCxObj(MyBroker,state);
  567.  
  568.  /* Status window open? */
  569.  if (statwinsig)
  570.   {
  571.    /* Yes, set new gadget state */
  572.    GT_SetGadgetAttrs(ebbg,w,NULL,GA_Disabled,state,TAG_DONE);
  573.    GT_SetGadgetAttrs(dbbg,w,NULL,GA_Disabled,!state,TAG_DONE);
  574.    GT_SetGadgetAttrs(hkbg,w,NULL,GA_Disabled,!state,TAG_DONE);
  575.  
  576.    /* Set new menu state */
  577.    SetBrokerMenuState();
  578.   }
  579. }
  580.  
  581. /* If the window is open, detach tool list from ListView gadget */
  582. void DetachToolList(void)
  583. {
  584.  if (statwinsig) GT_SetGadgetAttrs(tlvg,w,NULL,GTLV_Labels,-1,TAG_DONE);
  585. }
  586.  
  587. /* If the window is open, attach tool list to ListView gadget */
  588. void AttachToolList(void)
  589. {
  590.  if (statwinsig) GT_SetGadgetAttrs(tlvg,w,NULL,
  591.                                    GTLV_Labels,&ToolList,
  592.                                    /* If an item was selected, use it as
  593.                                       top item */
  594.                                    GTLV_Top,lvtop,
  595.                                    GTLV_Selected,lvord,
  596.                                    TAG_DONE);
  597. }
  598.  
  599. /* Set activation status of tool gadgets/menus */
  600. static void DisableToolGads(BOOL state)
  601. {
  602.  /* Gadgets */
  603.  GT_SetGadgetAttrs(tpbg,w,NULL,GA_Disabled,state,TAG_DONE);
  604.  GT_SetGadgetAttrs(upbg,w,NULL,GA_Disabled,state,TAG_DONE);
  605.  GT_SetGadgetAttrs(dnbg,w,NULL,GA_Disabled,state,TAG_DONE);
  606.  GT_SetGadgetAttrs(bmbg,w,NULL,GA_Disabled,state,TAG_DONE);
  607.  GT_SetGadgetAttrs(etbg,w,NULL,GA_Disabled,state,TAG_DONE);
  608.  GT_SetGadgetAttrs(rtbg,w,NULL,GA_Disabled,state,TAG_DONE);
  609.  
  610.  /* Menus */
  611.  SetToolMenuState(state);
  612. }
  613.  
  614. /* Set gadget and menu state */
  615. void StatWinDockState(BOOL open)
  616. {
  617.  /* Status window open? */
  618.  if (statwinsig)
  619.   {
  620.    /* Yes, set gadget & menu attributes */
  621.    SetDockMenuState(open);
  622.    GT_SetGadgetAttrs(odbg,w,NULL,GA_Disabled,open || !DockCount,TAG_DONE);
  623.    GT_SetGadgetAttrs(dobg,w,NULL,GA_Disabled,!open || !DockCount,TAG_DONE);
  624.    GT_SetGadgetAttrs(cdbg,w,NULL,GA_Disabled,!open || !DockCount,TAG_DONE);
  625.   }
  626. }
  627.  
  628. /* Re-activate gadget after editing */
  629. void StatWinEndEditing(void)
  630. {
  631.  if (statwinsig) DisableToolGads(FALSE);
  632.  StatWinDockState(ShowDock);
  633. }
  634.  
  635. /* Find tool with lvord */
  636. struct ToolNode *FindTool(void)
  637. {
  638.  if (lvord>=0)             /* Is the ordinal number valid? */
  639.   {                        /* Yes, search tool */
  640.    struct ToolNode *tn;
  641.    WORD i=0;               /* Counter */
  642.  
  643.    /* Scan tool list until the ordinal number is reached */
  644.    for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn),i++)
  645.     if (i>=lvord) break;
  646.  
  647.    return(tn);
  648.   }
  649.  return(NULL);
  650. }
  651.  
  652. /* Clear the configuration */
  653. static BOOL ClearConfig(void)
  654. {
  655.  BOOL canceled;
  656.  
  657.  /* Put up safety requester if needed */
  658.  canceled=ShowSaveConfigRequester(w);
  659.  
  660.  /* If config was saved, then delete all tools */
  661.  if (!canceled)
  662.   {
  663.    /* Close windows */
  664.    CloseEditWindow();
  665.    CloseHotKeysWindow();
  666.    CloseDockWindow();
  667.  
  668.    /* Delete all tools */
  669.    DisableToolGads(TRUE);
  670.    DetachToolList();
  671.    lvord=-1;
  672.    lvtop=0;
  673.    RemoveTools();
  674.    AttachToolList();
  675.    StatWinDockState(FALSE);
  676.    ConfigChanged=FALSE;
  677.   }
  678.  
  679.  return(canceled);
  680. }
  681.  
  682. /* Move tool to top of list */
  683. static void MoveToolTop(void)
  684. {
  685.  struct ToolNode *tn=FindTool();
  686.  
  687.  if (tn)          /* Tool selected? */
  688.   {
  689.    /* We are changing the list */
  690.    DetachToolList();
  691.  
  692.    /* Move tool node */
  693.    Remove((struct Node *) tn);
  694.    AddHead(&ToolList,(struct Node *) tn);
  695.    lvord=lvtop=0;
  696.  
  697.    /* We have changed the list */
  698.    AttachToolList();
  699.   }
  700. }
  701.  
  702. /* Move tool one up in list */
  703. static void MoveToolUp(void)
  704. {
  705.  struct ToolNode *tn=FindTool();
  706.  
  707.  if (tn)          /* Tool selected? */
  708.   {
  709.    /* Get predecessor */
  710.    struct ToolNode *ltn=GetPred(tn);
  711.  
  712.    if (ltn)       /* Got a predecessor? */
  713.     {
  714.      /* Yes, we are changing the list */
  715.      DetachToolList();
  716.  
  717.      /* Move tool node */
  718.      ltn=GetPred(ltn);
  719.      Remove((struct Node *) tn);
  720.      Insert(&ToolList,(struct Node *) tn,(struct Node *) ltn);
  721.      lvtop=--lvord;
  722.  
  723.      /* We have changed the list */
  724.      AttachToolList();
  725.     }
  726.   }
  727. }
  728.  
  729. /* Move tool one down in list */
  730. static void MoveToolDown(void)
  731. {
  732.  struct ToolNode *tn=FindTool();
  733.  
  734.  if (tn)          /* Tool selected? */
  735.   {
  736.    /* Get successor */
  737.    struct ToolNode *ltn=GetSucc(tn);
  738.  
  739.    if (ltn)       /* Got a successor? */
  740.     {
  741.      /* Yes, we are changing the list */
  742.      DetachToolList();
  743.  
  744.      /* Move tool node */
  745.      Remove((struct Node *) tn);
  746.      Insert(&ToolList,(struct Node *) tn,(struct Node *) ltn);
  747.      lvtop=++lvord;
  748.  
  749.      /* We have changed the list */
  750.      AttachToolList();
  751.     }
  752.   }
  753. }
  754.  
  755. /* Move tool to bottom of list */
  756. static void MoveToolBottom(void)
  757. {
  758.  struct ToolNode *tn=FindTool();
  759.  
  760.  if (tn)          /* Tool selected? */
  761.   {
  762.    /* We are changing the list */
  763.    DetachToolList();
  764.  
  765.    /* Move tool node */
  766.    Remove((struct Node *) tn);
  767.    AddTail(&ToolList,(struct Node *) tn);
  768.    lvord=lvtop=ToolCount-1;
  769.  
  770.    /* We have changed the list */
  771.    AttachToolList();
  772.   }
  773. }
  774.  
  775. /* Create new empty tool */
  776. static void AddEmptyTool(void)
  777. {
  778.  struct ConfigBlock *cb;
  779.  
  780.  /* Get memory */
  781.  if (cb=malloc(sizeof(struct ConfigBlock)))
  782.   {
  783.    /* Initialize config block */
  784.    InitConfigBlock(cb);
  785.    cb->cb_Type=TNTYPE_DUMMY;
  786.    strcpy(cb->cb_Alias,"New Tool");
  787.  
  788.    /* Add tool */
  789.    if (AddToolNode(cb,StartupCD))
  790.     {
  791.      struct ToolNode *tn;
  792.  
  793.      /* We are changing the list */
  794.      DetachToolList();
  795.  
  796.      /* Move new tool behind the selected tool */
  797.      if (tn=FindTool())
  798.       {
  799.        struct ToolNode *ntn=RemTail(&ToolList);
  800.  
  801.        /* Insert it after the selected one */
  802.        Insert(&ToolList,(struct Node *) ntn, (struct Node *) tn);
  803.       }
  804.  
  805.      /* Refresh Gadgets */
  806.      RefreshStatusWindow();
  807.  
  808.      /* Set new tool as active ListView item */
  809.      if (!editwinsig)
  810.       {
  811.        /* Set new tool number */
  812.        lvord=(lvord<0)?ToolCount-1:lvord+1;
  813.        lvtop=(lvord>0)?lvord-1:0;
  814.  
  815.        /* Activate gadgets */
  816.        DisableToolGads(FALSE);
  817.       }
  818.  
  819.      /* We have changed the list */
  820.      AttachToolList();
  821.      ConfigChanged=TRUE;
  822.     }
  823.    else DisplayBeep(NULL);
  824.  
  825.    /* Free memory */
  826.    free(cb);
  827.   }
  828. }
  829.  
  830. /* Open edit window */
  831. static void EditTool(void)
  832. {
  833.  struct ToolNode *tn=FindTool();
  834.  
  835.  if (tn)          /* Tool selected? */
  836.   {
  837.    /* Disable gadgets */
  838.    DisableToolGads(TRUE);
  839.  
  840.    /* Open window */
  841.    OpenEditWindow(w->LeftEdge,w->TopEdge,ww,tn);
  842.   }
  843. }
  844.  
  845. /* Remove tool */
  846. static void SWRemoveTool(void)
  847. {
  848.  struct ToolNode *tn=FindTool();
  849.  
  850.  if (tn)          /* Tool selected? */
  851.   {
  852.    lvord=-1;         /* Invalidate ordinal number */
  853.  
  854.    /* Disable Gadgets */
  855.    DisableToolGads(TRUE);
  856.  
  857.    /* Update the HotKeys window */
  858.    RefreshHotKeysWindow(tn,FALSE);
  859.  
  860.    DetachToolList(); /* Remove tool from list */
  861.    RemToolNode(tn);
  862.    AttachToolList();
  863.  
  864.    RefreshStatusWindow(); /* Refresh Gadgets */
  865.   }
  866. }
  867.  
  868. /* Start tool */
  869. static void SWStartTool(void)
  870. {
  871.  struct ToolNode *tn;
  872.  
  873.  tn=FindTool();
  874.  if (tn) StartTool(tn,NULL);
  875. }
  876.  
  877. /* Open dock window */
  878. static void SWDockWindow(BOOL open)
  879. {
  880.  /* Set gadget and menu state */
  881.  StatWinDockState(open);
  882.  
  883.  /* Open/close window */
  884.  if (open)
  885.   OpenDockWindow();
  886.  else
  887.   CloseDockWindow();
  888. }
  889.  
  890. /* Change dock window orientation */
  891. static void ChangeDock(void)
  892. {
  893.  /* Invert boolean variables */
  894.  DockVertical=!DockVertical;
  895.  
  896.  /* Change window */
  897.  if (ShowDock)
  898.   {
  899.    CloseDockWindow();
  900.    OpenDockWindow();
  901.   }
  902. }
  903.  
  904. /* Handle window events */
  905. void HandleStatWinEvent(void)
  906. {
  907.  BOOL clwin=FALSE; /* TRUE if window should be closed */
  908.  struct IntuiMessage *msg;
  909.  
  910.  while (msg=GT_GetIMsg(wp))    /* Pre-process Intuition messages */
  911.   {
  912.    switch (msg->Class)
  913.     {
  914.      case IDCMP_CLOSEWINDOW:   /* User clicked the close gadget */
  915.       clwin=TRUE;              /* Yes, close window */
  916.       break;
  917.      case IDCMP_REFRESHWINDOW: /* Window must be refreshed */
  918.       GT_BeginRefresh(w);
  919.       GT_EndRefresh(w,TRUE);
  920.       break;
  921.      case IDCMP_GADGETUP:      /* User released a gadget */
  922.       switch(((struct Gadget *) msg->IAddress)->GadgetID)
  923.        {
  924.         case LVGAD_ID:         /* User selected ListView item */
  925.          if (editwinsig)       /* Edit window open? */
  926.           {
  927.            /* Yes --> Ignore selection. Note: This is needed, because */
  928.            /* GA_Disabled is NOT SUPPORTED for LISTVIEW_KIND!         */
  929.            GT_SetGadgetAttrs(tlvg,w,NULL,GTLV_Selected,lvord,TAG_DONE);
  930.           }
  931.          else /* Edit window not open --> Normal selection */
  932.           {
  933.            struct ToolNode *tn;
  934.            ULONG secs=msg->Seconds,micros=msg->Micros;
  935.  
  936.            /* If no edit window is open then check for double click */
  937.            if ((lvord==msg->Code) && DoubleClick(oldsecs,oldmicros,secs,micros))
  938.            /* User double clicked the LV gadget --> Start tool */
  939.             SWStartTool();
  940.            else
  941.             {
  942.              /* No double click --> retrieve ordinal number of selected item */
  943.              lvtop=lvord=msg->Code;
  944.  
  945.              /* Activate gadgets */
  946.              DisableToolGads(FALSE);
  947.             }
  948.  
  949.            /* Save current time */
  950.            oldsecs=secs;
  951.            oldmicros=micros;
  952.           }
  953.          break;
  954.         case TPGAD_ID:         /* User selected move tool to top gadget */
  955.          MoveToolTop();
  956.          break;
  957.         case UPGAD_ID:         /* User selected move tool one up gadget */
  958.          MoveToolUp();
  959.          break;
  960.         case DNGAD_ID:         /* User selected move tool one down gadget */
  961.          MoveToolDown();
  962.          break;
  963.         case BMGAD_ID:         /* User selected move tool to bottom gadget */
  964.          MoveToolBottom();
  965.          break;
  966.         case NTGAD_ID:         /* User selected new tool gadget */
  967.          AddEmptyTool();
  968.          break;
  969.         case ETGAD_ID:         /* User selected edit tool definition gadget */
  970.          EditTool();
  971.          break;
  972.         case RTGAD_ID:         /* User selected remove tool gadget */
  973.          SWRemoveTool();
  974.          break;
  975.         case ENGAD_ID:         /* User selected enable broker gadget */
  976.          SetBrokerState(TRUE);
  977.          break;
  978.         case HKGAD_ID:         /* User selected HotKeys gadget */
  979.          OpenHotKeysWindow(w->LeftEdge,w->TopEdge);
  980.          break;
  981.         case DIGAD_ID:         /* User selected enable broker gadget */
  982.          SetBrokerState(FALSE);
  983.          break;
  984.         case ODGAD_ID:         /* User selected Dock on gadget */
  985.          SWDockWindow(TRUE);
  986.          break;
  987.         case DOGAD_ID:         /* User selected Dock H/V gadget */
  988.          ChangeDock();
  989.          break;
  990.         case CDGAD_ID:         /* User selected Dock Off gadget */
  991.          SWDockWindow(FALSE);
  992.          break;
  993.         case SCGAD_ID:         /* User selected save config gadget */
  994.          WriteConfigFile(w,FALSE);
  995.          break;
  996.         case HIGAD_ID:         /* User selected hide gadget */
  997.          clwin=TRUE;
  998.          break;
  999.         case QUGAD_ID:         /* User selected quit gadget */
  1000.          if (!clwin)
  1001.           {
  1002.            SetQuitFlag();
  1003.            if (!running) clwin=TRUE;
  1004.           }
  1005.          break;
  1006.        }
  1007.       break;
  1008.      case IDCMP_MENUPICK:      /* User selected a menu */
  1009.       USHORT mnum=msg->Code;
  1010.  
  1011.       while (mnum!=MENUNULL)   /* Scan all menu events */
  1012.        {
  1013.         struct MenuItem *mi=ItemAddress(wmn,mnum);
  1014.  
  1015.         switch(GTMENUITEM_USERDATA(mi))
  1016.          {
  1017.           case NCMENU_ID:      /* User selected new config menu item */
  1018.            ClearConfig();
  1019.            break;
  1020.           case OCMENU_ID:      /* User selected open config menu item */
  1021.            BOOL OldShowDock=ShowDock;
  1022.            char *cp=ConfigName;
  1023.  
  1024.            /* Pop up load config file requester */
  1025.            if (!ShowFileRequester(w,"Load Configuration",&cp,FREQ_FILE))
  1026.             {
  1027.              if (!ClearConfig()) /* First clear old entrys */
  1028.               {
  1029.                /* Remove old program AppIcon */
  1030.                if (MyAppIcon) RemoveAppIcon(MyAppIcon);
  1031.  
  1032.                /* Set & read in new config file */
  1033.                SetConfigFileName(cp);
  1034.                ReadConfigFile(cp,TRUE);
  1035.  
  1036.                /* Init icon position */
  1037.                MyIcon->do_CurrentX=IconXPos;
  1038.                MyIcon->do_CurrentY=IconYPos;
  1039.  
  1040.                /* Display our icon? Yes, notify Workbench about icon */
  1041.                if (ShowIcon)
  1042.                 MyAppIcon=AddAppIconA(NULL,NULL,IconName,MyMP,NULL,MyIcon,NULL);
  1043.  
  1044.                /* Open dock window? */
  1045.                if (OldShowDock)
  1046.                 OpenDockWindow();
  1047.                else
  1048.                 StatWinDockState(FALSE);
  1049.               }
  1050.              free(cp);
  1051.             }
  1052.            break;
  1053.           case ACMENU_ID:      /* User selected append config menu item */
  1054.            char *cp=ConfigName;
  1055.  
  1056.            /* Pop up file requester */
  1057.            if (!ShowFileRequester(w,"Append Configuration",&cp,FREQ_FILE))
  1058.             {
  1059.              /* Read in another config file */
  1060.              ReadConfigFile(cp,FALSE);
  1061.              free(cp);
  1062.  
  1063.              ConfigChanged=TRUE;
  1064.             }
  1065.            break;
  1066.           case SCMENU_ID:      /* User selected save config menu item */
  1067.            WriteConfigFile(w,TRUE);
  1068.            break;
  1069.           case SAMENU_ID:      /* User selected save config as menu item */
  1070.            WriteConfigFile(w,FALSE);
  1071.            break;
  1072.           case HIMENU_ID:      /* User selected hide menu item */
  1073.            clwin=TRUE;
  1074.            break;
  1075.           case ABMENU_ID:
  1076.            EasyRequest(w,&AboutES,NULL,"");
  1077.            break;
  1078.           case QUMENU_ID:      /* User selected quit menu item */
  1079.            if (!clwin)
  1080.             {
  1081.              SetQuitFlag();
  1082.              if (!running) clwin=TRUE;
  1083.             }
  1084.            break;
  1085.           case STMENU_ID:      /* User selected start tool menu item */
  1086.            SWStartTool();
  1087.            break;
  1088.           case NTMENU_ID:      /* User selected new tool menu item */
  1089.            AddEmptyTool();
  1090.            break;
  1091.           case ETMENU_ID:      /* User selected edit tool def. menu item */
  1092.            EditTool();
  1093.            break;
  1094.           case RTMENU_ID:      /* User selected remove tool menu item */
  1095.            SWRemoveTool();
  1096.            break;
  1097.           case TPMENU_ID:      /* User selected move tool to top menu item */
  1098.            MoveToolTop();
  1099.            break;
  1100.           case UPMENU_ID:      /* User selected move tool one up menu item */
  1101.            MoveToolUp();
  1102.            break;
  1103.           case DNMENU_ID:      /* User selected move tool one down menu item */
  1104.            MoveToolDown();
  1105.            break;
  1106.           case BMMENU_ID:      /* User selected move tool to bottom menu item */
  1107.            MoveToolBottom();
  1108.            break;
  1109.           case ENMENU_ID:      /* User selected enable broker menu item */
  1110.            SetBrokerState(TRUE);
  1111.            break;
  1112.           case DIMENU_ID:      /* User selected disable broker menu item */
  1113.            SetBrokerState(FALSE);
  1114.            break;
  1115.           case HLMENU_ID:      /* User selected HotKeys list menu item */
  1116.            OpenHotKeysWindow(w->LeftEdge,w->TopEdge);
  1117.            break;
  1118.           case ODMENU_ID:      /* User selected Dock on menu item */
  1119.            SWDockWindow(TRUE);
  1120.            break;
  1121.           case DOMENU_ID:      /* User selected Dock H/V menu item */
  1122.            ChangeDock();
  1123.            break;
  1124.           case CDMENU_ID:      /* User selected Dock off menu item */
  1125.            SWDockWindow(FALSE);
  1126.            break;
  1127.          }
  1128.  
  1129.         /* Next selected menu */
  1130.         mnum=mi->NextSelect;
  1131.        }
  1132.       break;
  1133.     }
  1134.    GT_ReplyIMsg(msg);          /* Reply pre-processed message */
  1135.   }
  1136.  
  1137.  if (clwin) CloseStatusWindow();
  1138. }
  1139.