#include once "SimpleGadget.bas"

'InitGFX m u s s aufgerufen worden sein

declare function MessageBox(posx as integer,posy as integer,msgtxt as string="",title as string="",msgtype as integer=1) as integer

'Konstanten für MessageBoxen
'Typen:
'MBType_OK=1
'MBType_YESNO=2
'MBType_CANCELRETRYIGNORE=3
'MBType_OKCANCEL=4
'MBType_RETRYCANCEL=5
'MBType_YESNOCANCEL=6

'Button Returnwerte
'MBButton_OK=1
'MBButton_CANCEL=2
'MBButton_RETRY=3
'MBButton_IGNORE=4
'MBButton_YES=5
'MBButton_NO=6

function MessageBox(posx as integer,posy as integer,msgtxt as string="",title as string="",msgtype as integer=1) as integer
  function=0

  if msgtype<1 then msgtype=1
  if msgtype>6 then msgtype=6

  dim as TextObject ptr t
  dim as integer l,ll,boxw,boxh,result,bw,bh,bs
  dim as EventHandle ptr event
  dim as Gadget ptr ok,yes,no,cancel,retry,ignore
  bw=100
  bh=20
  bs=10

  t=new TextObject

  if t then
    t->SetText(msgtxt,"|")
    l =t->GetLines
    ll=t->GetLongestLine
    boxw=ll*8 + 16
    boxh=bh + (l+1)*fontheight + bh + 2*bs + 2
    if boxw<3*bw+4*bs then boxw=3*bw+4*bs

    dim as integer ptr gfxbackup
    gfxbackup=imagecreate(boxw,boxh)
    if gfxbackup then
      get (posx,posy)-(posx+boxw-1,posy+boxh-1),gfxbackup

      FillA(posx,posy,boxw,boxh,GadgetColor,0)
      draw string (posx+5, posy+3),title,TextColor
      FrameB(posx+2,posy+20,boxw-4,boxh-20-2,1)
      for i as integer=1 to l
        draw string (posx+8, posy+20+fontheight/2+(i-1)*fontheight),t->GetLineContent(i),TextColor
      next i

      event=CreateEventHandle

      select case msgtype
        case MBType_OK
          ok=AddSimpleGadget(event,posx+boxw-bw-bs,posy+boxh-bs-bh,bw,bh,"OK")
          GadgetOn(ok)

        case MBType_YESNO
          yes=AddSimpleGadget(event,posx+boxw-2*bw-2*bs,posy+boxh-bs-bh,bw,bh,"Ja")
          no=AddSimpleGadget(event,posx+boxw-bw-bs,posy+boxh-bs-bh,bw,bh,"Nein")
          GadgetOn(yes,no)

        case MBType_CANCELRETRYIGNORE
          cancel=AddSimpleGadget(event,posx+boxw-3*bw-3*bs,posy+boxh-bs-bh,bw,bh,"Abbruch")
          retry=AddSimpleGadget(event,posx+boxw-2*bw-2*bs,posy+boxh-bs-bh,bw,bh,"Wiederholen")
          ignore=AddSimpleGadget(event,posx+boxw-bw-bs,posy+boxh-bs-bh,bw,bh,"Ignorieren")
          GadgetOn(cancel,ignore)

        case MBType_OKCANCEL
          yes=AddSimpleGadget(event,posx+boxw-2*bw-2*bs,posy+boxh-bs-bh,bw,bh,"Ja")
          cancel=AddSimpleGadget(event,posx+boxw-bw-bs,posy+boxh-bs-bh,bw,bh,"Abbruch")
          GadgetOn(yes,cancel)

        case MBType_RETRYCANCEL
          retry=AddSimpleGadget(event,posx+boxw-2*bw-2*bs,posy+boxh-bs-bh,bw,bh,"Wiederholen")
          cancel=AddSimpleGadget(event,posx+boxw-bw-bs,posy+boxh-bs-bh,bw,bh,"Abbruch")
          GadgetOn(retry,cancel)

        case MBType_YESNOCANCEL
          yes=AddSimpleGadget(event,posx+boxw-3*bw-3*bs,posy+boxh-bs-bh,bw,bh,"Ja")
          no=AddSimpleGadget(event,posx+boxw-2*bw-2*bs,posy+boxh-bs-bh,bw,bh,"Nein")
          cancel=AddSimpleGadget(event,posx+boxw-bw-bs,posy+boxh-bs-bh,bw,bh,"Abbruch")
          GadgetOn(yes,cancel)
      end select

      do
        event->xSleep(1,0)
        if event->GADGETMESSAGE then
          select case event->GADGETMESSAGE
            case ok
              result=MBButton_OK
              event->EXITEVENT=1
            
            case cancel
              result=MBButton_CANCEL
              event->EXITEVENT=1
            
            case retry
              result=MBButton_RETRY
              event->EXITEVENT=1
            
            case ignore
              result=MBButton_IGNORE
              event->EXITEVENT=1
            
            case yes
              result=MBButton_YES
              event->EXITEVENT=1
            
            case no
              result=MBButton_NO
              event->EXITEVENT=1

          end select
        end if
      loop until event->EXITEVENT
      
      function=result

      DestroyEventHandle(event)

      put (posx,posy),gfxbackup,pset
      imagedestroy gfxbackup
    end if
    delete t
  end if
end function
