#include <exec/memory.h>
#include <intuition/intuition.h>
#include <workbench/startup.h>
#include <workbench/workbench.h>

#ifdef LATTICE
#include <stdio.h>
  int CXBRK(void){ return (0); }
  int chkabort(void){ return (0); }

  #include <clib/exec_protos.h>
  #include <clib/intuition_protos.h>
  #include <clib/icon_protos.h>
  #include <clib/wb_protos.h>
#endif

struct IntuitionBase *IntuitionBase=NULL;
struct WorkbenchBase *WorkbenchBase=NULL;

void main() {
  struct MsgPort *msgport;
  struct Window  *window;
  struct AppWindow *appwindow;
  struct IntuiMessage *imsg;
  struct AppMessage *appmsg;
  struct WBArg   *argptr;
  ULONG  WindowID = 1, UserData = 0;
  BOOL   ABORT = FALSE;
  UBYTE  i;

  if( IntuitionBase=OpenLibrary("intuition.library", 37)) {
    if( WorkbenchBase=OpenLibrary("workbench.library", 37)) {
      /*
       * Den benötigten Message-Port erstellen
       */
      if( msgport=CreateMsgPort() ) {
        if( window=OpenWindowTags( NULL, WA_Left,  10,   WA_Top, 10,
                                         WA_Width, 320, WA_Height, 50,
                                         WA_IDCMP, CLOSEWINDOW,
                                         WA_Flags, WINDOWCLOSE | WINDOWDRAG,
                                         WA_Title, "AmigaAppWindow", TAG_END)) {
          /*
           * Hier erfolgt der Eintrag des Fensters als Application-Window
           */
          if( appwindow=AddAppWindow(WindowID, UserData, window, msgport, NULL)) {
            do {
              /*
               * Nun warten wir entweder auf eine IDCMP-Message
               * oder eine AppMessage
               */
              Wait(1 << window->UserPort->mp_SigBit | 1 << msgport->mp_SigBit);
              while( imsg=(struct IntuiMessage *)GetMsg(window->UserPort)) {
                if( imsg->Class=CLOSEWINDOW )
                  ABORT=TRUE;
                  ReplyMsg((struct Message *) imsg);
              }
              while( appmsg=(struct AppMessage *)GetMsg(msgport)) {
                /*
                 * Die AppMessage kann nur vom Typ MTYPE_APPWINDOW sein.
                 * ID und userdata sind die von uns vorgegebenen Werte.
                 * Mit NumArgs lassen sich die Workbench-Argumente aus-
                 * lesen.
                 */
                printf("AppMsg=%lx, Typ=%ld, ID=%ld, UserData=%ld, NumArgs=%ld\n",
                              appmsg, appmsg->am_Type, appmsg->am_ID,
                              appmsg->am_UserData, appmsg->am_NumArgs);

                /*
                 * Jetzt die Argumente auswerten
                 */
                argptr = appmsg->am_ArgList;
                for(i = 0; i < appmsg->am_NumArgs; i++) {
                   printf("Argument(%ld): Name='%s', Lock=%lx\n",
                                i, argptr->wa_Name, argptr->wa_Lock);
                   argptr++;
                }
                ReplyMsg((struct Message *)appmsg);
              }
            } while (ABORT == FALSE);
            RemoveAppWindow(appwindow);
          } else printf("Das Fenster konnte nicht als AppWindow "
                        "AppWindow deklariert werden\n");
          CloseWindow(window);
        } else printf("Konnte Fenster nicht öffnen\n");
        DeleteMsgPort(msgport);
      } else printf("Konnte Message-Port nicht einrichten\n");
      CloseLibrary(WorkbenchBase);
    } else printf("Konnte workbench.library nicht öffnen\n");
    CloseLibrary(IntuitionBase);
  } else printf("Konnte intuition.library nicht öffnen\n");
}
