menu.e

Denna kod är public domain. Om ni hittar fel eller vill ändra något i koden blir jag jätteglad om ni skickar dessa ändringar till jesper [at] fantasi [punkt] se.


MODULE 'intuition/intuition'
MODULE 'dos/dos'

CONST MAX = 20
CONST GBS = MAX * GADGETSIZE

OBJECT mbutton
   name  : PTR TO CHAR
   event : PTR TO CHAR
   next  : PTR TO mbutton
ENDOBJECT

OBJECT menu
   used
   name : PTR TO CHAR
   butt : PTR TO mbutton
ENDOBJECT

OBJECT exeptr
   exe : PTR TO mbutton
ENDOBJECT

DEF menus[10] : ARRAY OF menu

PROC openMenu(n,x,y)
   DEF buf[GBS]  : ARRAY,
       next      : PTR TO gadget,
       win       : PTR TO gadget,
       mess      : PTR TO gadget,
       a         : PTR TO gadget,
       gad       : PTR TO gadget,
       kn        : PTR TO mbutton,
       exec[MAX] : ARRAY OF exeptr,
       tmp       : PTR TO CHAR

   a := 0
   tmp := String(210)
   kn := menus[n].butt
   IF kn
      next := Gadget(buf, NIL, 1, 0, 10, 16, 160, kn.name)
      exec[a].exe := kn
      a := 1
      kn := kn.next
      WHILE kn
         exec[a].exe := kn
         a++
         EXIT a > MAX
         next := Gadget(next, buf, a, 0, 10, 3 + (13 * a), 160, kn.name)
         kn := kn.next
      ENDWHILE
   ENDIF

   IF a > MAX THEN a := MAX
   y := y - ((a * 13) / 2)
   IF y < 0 THEN y := 0

   win := OpenW(x, y, 180, 20 + (13 * a), IDCMP_CLOSEWINDOW OR
                                          IDCMP_GADGETUP,
                                          WFLG_ACTIVATE OR
                                          WFLG_DRAGBAR OR
                                    /*    WFLG_DEPTHGADGET OR  */
                                          WFLG_CLOSEGADGET,
                                          menus[n].name, NIL, 1, buf)

   WHILE (mess := WaitIMessage(win)) <> IDCMP_CLOSEWINDOW
      gad := MsgIaddr()

      IF mess = $40
         kn := exec[gad.userdata - 1].exe
         IF StrCmp(kn.event, 'menu=', 5)
            RightStr(tmp, kn.event, 1)
            openMenu(Val(tmp, NIL), x + 60, y + (13 * gad.userdata))
         ELSE
            Execute(kn.event, NIL, NIL)
         ENDIF
      ENDIF
   ENDWHILE

   CloseW(win)
ENDPROC

PROC main()
   DEF kn : PTR TO mbutton,
       ko : PTR TO mbutton,
       n, oldStd,
       in : PTR TO CHAR

   in := String(200)
   oldStd := SetStdIn(Open('s:menu.dat', MODE_OLDFILE))

   ReadStr(stdin, in)
   IF StrCmp(in, 'JiVe menuSystem v1.0', ALL)
      WHILE ReadStr(stdin, in) <> -1
         ko := NIL
         n  := Val(in,NIL)
         menus[n].used := TRUE
         ReadStr(stdin, in)
         menus[n].name := String(20)
         StrCopy(menus[n].name, in, ALL)
         ReadStr(stdin, in)
         WHILE StrCmp(in, 'ENDM', ALL) = FALSE
            NEW kn
            kn.name := String(20)
            StrCopy(kn.name, in, ALL)
            ReadStr(stdin, in)
            kn.event := String(200)
            StrCopy(kn.event, in, ALL)
            kn.next := ko
            ko := kn
            ReadStr(stdin, in)
         ENDWHILE
         menus[n].butt := ko
      ENDWHILE
   ELSE
      WriteF('The file S:Menu.dat is not of the right type or does not exist!\n')
   ENDIF

   Close(stdin)
   stdin := oldStd
   IF menus[0].used THEN openMenu(0, 100, 100)
ENDPROC

CHAR '$VER: JiVe MenuSystem v1.0 -<Jesper Wilhelmsson>- (98.05.04)'