/*
 * capp.c
 *
 * A Sapphire application in C!
 *
 *  1995-1998 Straylight
 */

/*----- Licensing note ----------------------------------------------------*
 *
 * CApp is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * CApp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CApp.  If not, write to the Free Software Foundation,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*----- External dependencies ---------------------------------------------*/

/* --- Header files --- */

#include "sapphire.h"
#include "swis.h"

/* --- Sapphire routines --- *
 *
 * Currently we have to declare these by hand.  Maybe things will get better
 * in the future.
 */

#include "defHandler.h"
#include "errorBox.h"
#include "event.h"
#include "ibicon.h"
#include "menu.h"
#include "menuDefs.h"
#include "progInfo.h"
#include "resources.h"

/* --- A SWI --- */

__swi(OS_Exit) void os_exit(void);

/* --- Version strings --- */

extern char cright[];
extern char version[];

/*----- Main code ---------------------------------------------------------*/

/* --- The application name --- *
 *
 * This is picked up by start.s and passed to sapphire_init.
 */

char appname[]="CApp";

/* --- capp__menuHandler --- *
 *
 * The menu handling routine.  This is fairly straightforward.  Equipped
 * with os_exit, we can quit the program on a `Quit' event, and progInfo
 * displays a dialogue if you choose `Info...'.
 */

static _sapph(capp__menuHandler)(regset *r)
{
  switch (r->r[0])
  {
    case mEvent_select:
    case mEvent_subMenu:
      switch (r->r[1])
      {
        case 0:
          _call(progInfo,_inr(0,2),
                msgs_lookup("caPUR"),cright,version);
          break;
        case 1:
          os_exit();
          break;
      }
  }
  return (0);
}

/* --- capp__ibHandler --- *
 *
 * Handles events on the icon bar.  Select displays a `Hello, world!'
 * message.  Menu displays a menu.  The menu is contained within the
 * hexgorp below.
 */

static _sapph(capp__ibHandler)(regset *r)
{
  static const unsigned menu[]={ /* Yuk! */

    0x00000000,					/* Default title style */
    0x70704143,					/* `CApp' */
    0x00000000,

    0x00000200,					/* Send subwarn events */
    0x4E496163,					/* `caINF' */
    0x00000046,

    0x00000000,					/* Normal item style */
    0x55516163,					/* `caQUIT' */
    0x00005449,

    0x80000000,					/* End of the block */

  };

  switch (r->r[0])
  {
    case ibEvent_select:
      _call(errorBox,_inr(0,1),
            strerror(1,"Hello, world! %i0",(int)scratchpad/24),
            1);
      break;
    case ibEvent_menu:
      _call(menu_create,_inr(0,3),menu,capp__menuHandler,0,0);
      break;
    case ibEvent_adjust:
      _swi(Wimp_ReportError,_inr(0,2),
           strerror(1,"%0 seems to be working",appname),1,appname);
      break;
  }

  return (0);
}


/* --- sapph_main --- *
 *
 * This is the main program.  Well, almost.  I couldn't use `main' because
 * the compiler messes up the output.  The code initialises the library
 * units (yes! we can start executing C code before most of Sapphire is
 * awake!), creates an icon on the icon bar, and handles events until
 * something kills the application.
 */

_sapph(sapph_main)(void)
{
  char pollblock[256];
  int e;

  _call(resources_init,0);
  _call(sapphire_libInit,0);
  _call(ibicon_create,_inr(0,6),
        "application",0,
        -1,0,
        capp__ibHandler,0,0);

  for (;;)
  {
    /* --- Use _call here -- it needs testing --- */

    if (!(_call(event_poll,_inr(0,1)|_out(0)|_return(_flags),
                1,pollblock, &e) & _c))
      _call(defHandler,_inr(0,1),e,pollblock);
  }

  return (0);
}

/*----- That's all, folks -------------------------------------------------*/
