/*
(c) Copyright 1988-1994 Microsoft Corporation.

Microsoft Bars&Pipes Professional Source Code License

This License governs use of the accompanying Software.  
Microsoft hopes you find this Software useful.

You are licensed to do anything you want with the Software. 

In return, we simply require that you agree:

1.      Not to remove any copyright notices from the Software.

2.      That the Software comes "as is", with no warranties.  
        None whatsoever. This means no implied warranty of merchantability or 
        fitness for a particular purpose or any warranty of non-infringement.  
        Also, you must pass this disclaimer on whenever you distribute the Software.

3.      That we will not be liable for any of those types of damages known as indirect, 
        special, consequential, or incidental related to the Software or this License, 
        to the maximum extent the law permits. Also, you must pass this limitation of 
        liability on whenever you distribute the Software.

4.      That if you sue anyone over patents that you think may apply to the Software 
        for that person’s use of the Software, your license to the Software ends automatically.

5.      That the patent rights Microsoft is licensing only apply to the Software, 
        not to any derivatives you make.

6.      That your rights under the License end automatically if you breach this in any way.
*/

#include "/CompilerSpecific.h"
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/graphics.h>
#include "all.h"
#include "/v.h"
#include "edit.h"

extern struct Functions functions;

static struct IntuiMessage *msgbox_GetIntuiMessage(struct Window *w)
{
  struct IntuiMessage *msg;

  msg=(struct IntuiMessage *)GetMsg(w->UserPort);
  if(msg==NULL)
  {
    WaitPort(w->UserPort);
    msg=(struct IntuiMessage *)GetMsg(w->UserPort);
  }
  return msg;
}

int msgbox(char *text,char *b0Text,char *b1Text,
               char *b2Text,short center,short backcolor,short textcolor,
               short buttoncolor)
/* returns buttons numbered (from left to right): 0, 1, 2 */
/* Caution!: the standard numbering scheme of 1=Yes and 0=No doesn't work
* here!
*/
{
  struct Screen *scr = functions.screen;
  short cwid=scr->RastPort.TxWidth;
  int wid,height;
  int len[3];
  char *p,*op;
  struct NewWindow nw;
  struct Window *w;
  struct Gadget gad[3];
  struct Border brd[3];
  struct IntuiText it[3];
  short vect[3][10];
  struct IntuiMessage *msg;
  len[0]=TextLength(&scr->RastPort,b0Text,strlen(b0Text)) + (cwid<<1);
  wid=len[0] + (cwid<<1);
  if(b1Text)
  {
    len[1]=TextLength(&scr->RastPort,b1Text,strlen(b1Text)) + (cwid<<1);
    len[1] = max(len[1],len[0]);
    len[0] = max(len[1],len[0]);
    wid+=len[1] + (cwid<<1);
    if(b2Text)
    {
      len[2]=TextLength(&scr->RastPort,b2Text,strlen(b2Text)) + (cwid<<1);
      len[2] = max(len[1],len[2]);
      len[0] = len[1] = max(len[1],len[2]);
      wid+=len[2] + (cwid<<1);
    }
    else
        len[2]=0;
  }
  else
      len[1]=len[2]=0;
  for(p=op=text,height=4 ; p=strchr(p,'\n') ; ++height,op=p)
  {
    wid=max(wid,TextLength(&scr->RastPort,op,p-op)+(cwid<<2));
    ++p;
  }
  wid=max(wid,TextLength(&scr->RastPort,op,strlen(op))+(cwid<<2));
  height*=scr->RastPort.TxHeight;
  height+=3+(scr->RastPort.TxHeight>>1);
  memset(&nw,'\0',sizeof(nw));
  nw.LeftEdge=(scr->Width - wid)>>1;
  nw.TopEdge=(scr->Height - height)>>1;
  nw.Width=wid;
  nw.Height=height;
  nw.Screen=scr;
  nw.IDCMPFlags=GADGETUP|INACTIVEWINDOW|RAWKEY;
  nw.Flags=ACTIVATE;
  nw.Type=CUSTOMSCREEN;
  w=OpenWindow(&nw);
  if(w==NULL)
      return -1;

  SetRast(w->RPort,backcolor);
  RefreshWindowFrame(w);

  height=w->RPort->TxHeight+w->RPort->TxBaseline;
  SetAPen(w->RPort,textcolor);
  SetDrMd(w->RPort,JAM1);
  for(p=op=text ; p=strchr(p,'\n') ; op=p)
  {
    if(center)
        wid=(w->Width - TextLength(w->RPort,op,p-op))>>1;
    else
        wid=cwid<<1;
    Move(w->RPort,wid,height);
    Text(w->RPort,op,p-op);
    height+=w->RPort->TxHeight;
    ++p;
  }
  if(center)
      wid=(w->Width - TextLength(w->RPort,op,strlen(op)))>>1;
  else
      wid=cwid<<1;
  Move(w->RPort,wid,height);
  Text(w->RPort,op,strlen(op));

  memset(gad,'\0',sizeof(gad));
  memset(brd,'\0',sizeof(brd));
  memset(it,'\0',sizeof(it));
  for(wid=0 ; wid<3 ; ++wid)
  {
    gad[wid].Height=w->RPort->TxHeight+3;
    gad[wid].Width=len[wid];
    gad[wid].TopEdge=-((w->RPort->TxHeight>>1)+gad[wid].Height);
    gad[wid].Flags=GADGHCOMP|GRELBOTTOM|GRELRIGHT;
    gad[wid].Activation=RELVERIFY;
    gad[wid].GadgetType=BOOLGADGET;
    gad[wid].GadgetRender=&brd[wid];
    gad[wid].GadgetText=&it[wid];
    gad[wid].GadgetID=wid+1;
    vect[wid][0]=0;                  vect[wid][1]=0;
    vect[wid][2]=gad[wid].Width+1;   vect[wid][3]=0;
    vect[wid][4]=gad[wid].Width+1;   vect[wid][5]=gad[wid].Height+1;
    vect[wid][6]=0;                  vect[wid][7]=gad[wid].Height+1;
    vect[wid][8]=0;                  vect[wid][9]=0;
    brd[wid].LeftEdge=brd[wid].TopEdge=-1;
    brd[wid].FrontPen=buttoncolor;
    brd[wid].DrawMode=JAM1;
    brd[wid].Count=5;
    brd[wid].XY=vect[wid];
    it[wid].FrontPen=buttoncolor;
    it[wid].DrawMode=JAM1;
//    it[wid].LeftEdge=cwid;
    it[wid].TopEdge=2;
  }
  it[0].IText=b0Text;
  it[0].LeftEdge = (gad[0].Width - strlen(b0Text)*cwid)/2;
  if (len[1])
  {
    it[1].IText=b1Text;
    it[1].LeftEdge = (gad[1].Width - strlen(b1Text)*cwid)/2;
  }
  if (len[2])
  {
    it[2].IText=b2Text;
    it[2].LeftEdge = (gad[2].Width - strlen(b2Text)*cwid)/2;
  }
  if(len[2])          /** all three buttons **/
  {
    gad[2].LeftEdge=-(cwid+gad[2].Width);
    gad[1].LeftEdge=gad[2].LeftEdge-gad[1].Width-4;
    gad[0].LeftEdge=gad[1].LeftEdge-gad[0].Width-4;
    AddGadget(w,&gad[0],-1);
    AddGadget(w,&gad[1],-1);
    AddGadget(w,&gad[2],-1);
    EmbossOn(w,gad[0].GadgetID,1);
    EmbossOn(w,gad[1].GadgetID,1);
    EmbossOn(w,gad[2].GadgetID,1);
  }
  else if(len[1])     /** two buttons **/
  {
    gad[1].LeftEdge=-(cwid+gad[1].Width);
    gad[0].LeftEdge=gad[1].LeftEdge-gad[0].Width-4;
    AddGadget(w,&gad[0],-1);
    AddGadget(w,&gad[1],-1);
    EmbossOn(w,gad[0].GadgetID,1);
    EmbossOn(w,gad[1].GadgetID,1);
  }
  else                /** just 1 button **/
  {
    gad[0].LeftEdge=-((w->Width - gad[0].Width)>>1);
    AddGadget(w,&gad[0],-1);
    EmbossOn(w,gad[0].GadgetID,1);
  }
  RefreshGadgets(w->FirstGadget,w,NULL);

  for(wid=0 ; wid==0 ;)
  {
    msg=msgbox_GetIntuiMessage(w);
    switch(msg->Class)
    {
    case INACTIVEWINDOW:
      ActivateWindow(w);
      break;
    case GADGETUP:
      wid=((struct Gadget *)msg->IAddress)->GadgetID;
      break;
    case RAWKEY:
      switch (msg->Code)
      {
        case RK_Y:
          wid=1;
          break;
        case RK_N:
          wid=2;
          break;
      }
    }
    ReplyMsg((struct Message *)msg);
  }
  if (len[2])
  {
    EmbossOff(w,gad[2].GadgetID);
  }
  if (len[1])
  {
    EmbossOff(w,gad[1].GadgetID);
  }
  EmbossOff(w,gad[0].GadgetID);
  CloseWindow(w);

  return wid-1;
}

int quick_ask(char *text)
/* Returns 1=Yes and 0=No */

{
  return(!msgbox(text,"Yes","No",NULL,1,2,0,4));
}

int quick_msg(char *text)
/* Return value is unimportant */
{
  return(msgbox(text,"Okay",NULL,NULL,1,2,0,4));
}
