/*
  menutest by waschtl
  (waschtl@web.de)
  
  Just a test to see how to realise menus in the graphic mode
  of mShell
  to customize just change the global variables of colors.
  to enter a new Menu just use the function 'Menu'
  
  menu can be used with keys or with a touchscreen

*/
use array, ui
use graph as g

//init global variables
bgCol = g.black
MenuFont   = ['SwissA', 14, true, false]

MenuBrush = 0xd3d3d3       
MenuActBrush = 0xf5f5f5   
MenuPen = g.black      

    //keycode to chancel Menu
ChancelKey = 63564;

/*
draws a button with a text. All param excepted x,y,text are optional
the return value is an array with the coordinates for the upper left
point and the downright point of the button
*/
function DrawButton(x, y, text, lengh = false, brush = false, borderpen = false, font = false, fontpen = false)

  if borderpen = false then
    g.pen(.MenuPen);
  else
    g.pen(borderpen);
  end;
  if brush = false then
    g.brush(.MenuBrush);
  else
    g.brush(brush);
  end;
  if font = false then
    g.font(.MenuFont);
  else
    g.font(font);
  end;
  if lengh = false then
    lengh = g.size(text)[0] + 12;
  end;
  height = g.size(text)[1] + 6;
  x1 = x; y1 = y;
  x2 = x1 + lengh; y2 = y1 + height;

  ButtonPos = ['x1' : x1,
               'x2' : x2,
               'y1' : y1,
               'y2' : y2
              ];
  g.rect(x, y, lengh, height);

  if fontpen = false then
    g.pen(g.black);
  else
    g.pen(fontpen);
  end;
  g.text(x + 3, y + g.size(text)[1] +1, text);
  return ButtonPos;
end

/*
check if the forwarded point (x,y) is on the
forwarded button
*/
function CheckClick(x,y, Button)
  if (Button['x1'] <= x  and x <= Button['x2'])
  and (Button['y1'] <= y and y <= Button['y2'])then
    return true;
  else
    return false;
  end;
end

/*
Draw a menu starting on Point P(x,y)
returns a value corresponding the number of the choosen item
in the array
*/
function Menu(x, y, arr)
  g.font(.MenuFont);
  lengh = 0;
  for elem in arr do
    if g.size(elem)[0] > lengh then
      lengh = g.size(elem)[0]+6;
    end;
  end; 
  active = 0;
  //draw menu
  M = array.create(len(arr), false);
  M[0] = DrawButton(x, y, arr[0], lengh, .MenuActBrush);
  for i = 1 to len(arr) -1  do
    M[i] = DrawButton(x,M[i-1]['y2'], arr[i], lengh);
  end;
  g.show();

  //interpret the useraction and return
  while true do
    IN = ui.cmd();
    case IN
    in ui.downkey:
      if active = len(M) -1 then
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuBrush);
        active = 0;
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuActBrush);
        g.show();
      else
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuBrush);
        active ++;
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuActBrush);
        g.show();
      end;

    in ui.upkey:
      if active = 0 then
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuBrush);
        active = len(M) -1;
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuActBrush);
        g.show();
      else
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuBrush);
        active --;
        DrawButton(M[active]['x1'], M[active]['y1'],
                   arr[active], lengh, .MenuActBrush);
        g.show();
      end;

    in ui.gokey:
      return active;
    in .ChancelKey:
      return false;
    else
      if isarray(IN) then              //ptr- event
        if IN['buttons'] = 1 then
          x = IN['x']; y = IN['y'];
          for i = 0 to len(arr) -1 do
            if CheckClick(x, y, M[i]) then
              return i;
            end;
          end;
          return false;
        end;
      end;
    end;
  end;
end


//Mainprogram

ui.ptr(false);
ui.keys(false);

//arrays for menus
  MDatei = ['open file',
            'new file',
            'save',
            'sace config',
            'laod config',
            'quit'
            ];
            

x = Menu( 10, 1, MDatei);
print 'coose: ' + MDatei[x];;