program MUITest;

{$mode objfpc}

uses
  Classes, SysUtils, Exec, AmigaDos, Intuition, Mui, Utility;

const
  Esc33c = #27#99;

procedure Buttonfunc(Hook: PHook; Obj: PObject_; Msg:Pointer);
begin
  writeln('button clicked');
end;

function MyGet(o: pObject_; tag: LongWord): LongWord;
var
  Res: LongWord;
begin
  GetAttr(tag, o, @Res);
  MyGet := Res;
end;

var
  wnd, app, but, txt, grp: pObject_;
  sigs: LongWord;
  ButtonHook: THook;
begin
  ButtonHook.h_Entry := @buttonFunc;

  but := MUI_MakeObject(MUIO_Button, [PChar('_Ok')]);

  txt := MUI_NewObject(MUIC_Text,
    [LongInt(MUIA_Text_Contents), PChar(Esc33c + 'Hello world'#10'How are you?'),
     TAG_END]);

  grp := MUI_NewObject(MUIC_Group,
    [LongInt(MUIA_Group_Child), txt,
     LongInt(MUIA_Group_Child), but,
     TAG_END]);

  wnd := MUI_NewObject(MUIC_Window,
    [LongInt(MUIA_Window_Title), PChar('Hello World'),
     LongInt(MUIA_Window_RootObject), grp,
     TAG_END]);

  app := MUI_NewObject(MUIC_Application,
    [LongInt(MUIA_Application_Window), wnd,
     TAG_END]);

  if app <> nil then
  begin
    CallHookPkt(PHook(OCLASS(wnd)), wnd,
      [MUIM_Notify, MUIA_Window_CloseRequest, True,
      LongWord(app), 2,
      MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit]);
    CallHookPkt(PHook(OCLASS(but)), but,
      [MUIM_Notify, MUIA_Pressed, False,
      LongWord(app), 2,
      MUIM_CallHook, @ButtonHook]);

    sigs := 0;
    SetAttrs(wnd, [MUIA_Window_Open, True, TAG_END]);
    if myGet(wnd, MUIA_Window_Open)<>0 then
    begin
      while Integer(CallHookPkt(PHook(OCLASS(app)), app,
                     [MUIM_Application_NewInput, @sigs]))
            <> MUIV_Application_ReturnID_Quit do
      begin
        if (sigs <> 0) then
        begin
          sigs := Wait(sigs or SIGBREAKF_CTRL_C);
          if (Sigs and SIGBREAKF_CTRL_C) <>0 then
            Break;
        end;
      end;
    end;
    MUI_DisposeObject(app);
  end;
end.

