home *** CD-ROM | disk | FTP | other *** search
- #include "ToolManager.h"
-
- /* Structures for window */
- extern UBYTE WindowTitle[];
- extern struct NewWindow nw;
- #define WDRAGBARLEN 60 /* Width of Close & Drag gadget */
- static struct Window *w=NULL;
- static struct AppWindow *aw;
- static struct MsgPort *wp;
- static ULONG wl=20,wt=20; /* Window leftedge & topedge */
- static ULONG ww,wh; /* Window width & height */
- static struct Screen *pubsc; /* Workbench screen */
-
- /* Structures for window gadgets */
- static void *vi; /* Visual information is a *PRIVATE* data field! */
- static struct Gadget *gl; /* Gadget list */
- static char StatusText[]="Active Tools: 00"; /* Text gadget text */
- static struct Gadget *lvgad; /* ListView gadget */
- static WORD lvord=-1; /* LV gadget ordinal number */
- #define LVGAD_ID 1
- static char RTButtonText[]="Remove Tool"; /* Remove Tool gadget text */
- #define RTGAD_ID 2
- static char SCButtonText[]="Save Configuration"; /* Save config gadget test */
- #define SCGAD_ID 3
-
- /* Create status line */
- static void PrintStatusLine(void)
- {
- StatusText[14]=ToolCount/10+'0'; /* Hack */
- StatusText[15]=ToolCount%10+'0';
- }
-
- /* Create all status window gadgets */
- static BOOL CreateWindowGadgets(void)
- {
- struct NewGadget ng;
- struct Gadget *g;
- struct TextAttr *ta=pubsc->Font;
- struct TextFont *f; /* Window font */
- struct RastPort tmprp; /* RastPort for font-sensitive trick */
- UWORD width,fheight;
- UWORD leftedge,topborder;
- UWORD lvright;
-
- /* Open window font */
- if (!(f=OpenFont(ta))) return(FALSE);
- fheight=f->tf_YSize; /* Font height */
- leftedge=pubsc->WBorLeft;
- topborder=pubsc->WBorTop+fheight+1;
-
- /* Initialize temporary RastPort */
- InitRastPort(&tmprp);
- SetFont(&tmprp,f);
-
- /* Calculate window width */
- ww=TextLength(&tmprp,WindowTitle,strlen(WindowTitle))+WDRAGBARLEN;
-
- /* Create gadget list */
- gl=NULL;
- g=CreateContext(&gl);
-
- /* 1. gadget: Text, status line */
- PrintStatusLine();
- width=TextLength(&tmprp,StatusText,sizeof(StatusText)-1)+INTERWIDTH;
- ng.ng_LeftEdge=leftedge+(ww-width)/2;
- ng.ng_TopEdge=topborder+INTERHEIGHT;
- ng.ng_Width=width;
- ng.ng_Height=fheight;
- ng.ng_GadgetText=StatusText;
- ng.ng_TextAttr=ta;
- ng.ng_Flags=PLACETEXT_IN;
- ng.ng_VisualInfo=vi;
- ng.ng_UserData=0;
- g=CreateGadget(TEXT_KIND,g,&ng,TAG_DONE);
- g->GadgetText->DrawMode=JAM2; /* Argh, why doesn't exist a tag for this? */
-
- /* 2. gadget: ListView, tool list */
- ng.ng_LeftEdge=leftedge+WDRAGBARLEN/2;
- ng.ng_TopEdge+=g->Height+INTERHEIGHT;
- ng.ng_Width=ww-WDRAGBARLEN;
- ng.ng_Height=7*fheight;
- ng.ng_GadgetText=NULL;
- ng.ng_GadgetID=LVGAD_ID;
- lvgad=g=CreateGadget(LISTVIEW_KIND,g,&ng,
- GTLV_Labels,&ToolList,
- /* If an item was selected, use it as top item */
- GTLV_Top,lvord<0?0:lvord,
- GTLV_ShowSelected,NULL,
- GTLV_Selected,lvord,
- TAG_DONE);
- lvright=ng.ng_LeftEdge+g->Width;
-
- /* 3. gadget: Button, remove tool */
- ng.ng_TopEdge+=g->Height+fheight+2*INTERHEIGHT;
- ng.ng_Width=TextLength(&tmprp,RTButtonText,sizeof(RTButtonText)-1)+INTERWIDTH;
- ng.ng_Height=fheight+INTERHEIGHT;
- ng.ng_GadgetText=RTButtonText;
- ng.ng_GadgetID=RTGAD_ID;
- g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
-
- /* 4. gadget: Button, save config */
- width=TextLength(&tmprp,SCButtonText,sizeof(SCButtonText)-1)+INTERWIDTH;
- ng.ng_LeftEdge=lvright-width;
- ng.ng_Width=width;
- ng.ng_GadgetText=SCButtonText;
- ng.ng_GadgetID=SCGAD_ID;
- g=CreateGadget(BUTTON_KIND,g,&ng,TAG_DONE);
-
- /* Calculate window height */
- wh=ng.ng_TopEdge+g->Height+INTERHEIGHT-topborder;
-
- /* Close window font */
- CloseFont(f);
-
- /* All gadgets created! */
- if (g) return(TRUE);
-
- /* Something went wrong.... */
- FreeGadgets(gl);
- return(FALSE);
- }
-
- /* Open status window */
- ULONG OpenStatusWindow(void)
- {
- if (!(pubsc=LockPubScreen("Workbench"))) /* Lock Workbench screen */
- goto osw1;
-
- if (!(vi=GetVisualInfo(pubsc,TAG_DONE))) /* Get visual information */
- goto osw2;
-
- if (!CreateWindowGadgets()) /* Create Gadgets */
- goto osw3;
-
- /* Open window */
- if (!(w=OpenWindowTags(&nw,
- WA_Left,wl,
- WA_Top,wt,
- WA_InnerWidth,ww,
- WA_InnerHeight,wh,
- WA_PubScreen,pubsc,
- WA_AutoAdjust,TRUE,
- TAG_DONE)))
- goto osw4;
- wp=w->UserPort; /* Retrieve window port */
-
- /* Circumvent an intuition.library bug. See AutoDocs for LockPubScreen() */
- UnlockPubScreen(NULL,pubsc);
- pubsc=NULL;
-
- /* Notify Workbench about window */
- if (!(aw=AddAppWindow(NULL,NULL,w,MyMP,NULL)))
- goto osw5;
-
- /* Add gadget list to window */
- AddGList(w,gl,(UWORD) -1,(UWORD) -1,NULL);
- RefreshGList(gl,w,NULL,(UWORD) -1);
- GT_RefreshWindow(w,NULL);
-
- /* Window open! */
- return(1L<<wp->mp_SigBit);
-
- /* Something went wrong.... */
- osw5: CloseWindow(w);
- w=NULL;
- osw4: FreeGadgets(gl);
- osw3: FreeVisualInfo(vi);
- osw2: if (pubsc) UnlockPubScreen(NULL,pubsc);
- osw1: return(0);
- }
-
- /* Refresh status window gadgets */
- void RefreshStatusWindow(void)
- {
- PrintStatusLine();
- RefreshGList(gl,w,NULL,2); /* Refresh only the first two gadgets */
- }
-
- /* If the window is open, detach tool list from ListView gadget */
- void DetachToolList(void)
- {
- if (w) GT_SetGadgetAttrs(lvgad,w,NULL,GTLV_Labels,-1,TAG_DONE);
- }
-
- /* If the window is open, attach tool list to ListView gadget */
- void AttachToolList(void)
- {
- if (w) GT_SetGadgetAttrs(lvgad,w,NULL,
- GTLV_Labels,&ToolList,
- /* If an item was selected, use it as top item */
- GTLV_Top,lvord<0?0:lvord,
- GTLV_Selected,lvord,
- TAG_DONE);
- }
-
- /* Handle window events */
- BOOL HandleWindowEvent(void)
- {
- BOOL rc=FALSE; /* True if window close event */
- struct IntuiMessage *msg;
- struct ToolNode *tn;
- FILE *fh;
-
- while (msg=GT_GetIMsg(wp)) /* Pre-process Intuition messages */
- {
- switch (msg->Class)
- {
- case INTUITICKS: /* Intuition tick received */
- break;
- case CLOSEWINDOW: /* User clicked the close gadget */
- wl=w->LeftEdge; /* Retreive window parameters for next open */
- wt=w->TopEdge;
- rc=TRUE; /* Yes, close window */
- break;
- case REFRESHWINDOW: /* Window must be refreshed */
- GT_BeginRefresh(w);
- GT_EndRefresh(w,TRUE);
- break;
- case GADGETUP: /* User released a gadget */
- switch(((struct Gadget *) msg->IAddress)->GadgetID)
- {
- case LVGAD_ID: /* User selected a ListView item */
- lvord=msg->Code; /* Retrieve the ordinal number of the item */
- break;
- case RTGAD_ID: /* User selected the remove tool gadget */
- if (lvord>=0) /* Is the ordinal number valid? */
- { /* Yes, search tool and remove it */
- WORD i=0; /* Counter */
-
- /* Scan tool list until the ordinal number is reached */
- for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn),i++)
- if (i>=lvord) break;
- lvord=-1; /* invalidate ordinal number */
-
- /* Remove tool from list */
- DetachToolList();
- RemToolNode(tn);
- AttachToolList();
-
- /* Refresh Gadgets */
- RefreshStatusWindow();
- }
- break;
- case SCGAD_ID: /* User selected the save config gadget */
- if (fh=fopen(ConfigName,"w")) /* Open config file */
- {
- for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn))
- {
- fputs(tn->tn_Node.ln_Name,fh); /* Write menu entry name */
- if (tn->tn_RealName) /* Exists a real name? */
- {
- fputs(";",fh); /* Yes, append it */
- fputs(tn->tn_RealName,fh);
- }
- fputs("\n",fh); /* Append a new line */
- }
- fclose(fh); /* Close the config file */
- }
- else /* Could not open config file! */
- DisplayBeep(NULL); /* --> Flash display */
- break;
- }
- break;
- }
- GT_ReplyIMsg(msg); /* Reply pre-processed message */
- }
-
- return rc;
- }
-
- /* Close status window */
- void CloseStatusWindow(void)
- {
- RemoveAppWindow(aw);
- CloseWindow(w);
- w=NULL;
- FreeGadgets(gl); /* Release allocated resources */
- FreeVisualInfo(vi);
- }
-