/*
 * Title:   C_Demo
 * Using ANSI C Part 13
 * by Lee Calcraft
 * RISC User Aug/Sept 1992
 * Copyright Lee Calcraft
 */

/*
 **fp   function prototypes
 **fd   function definitions
 **gv   global variables
 
 **main    main()
 **in      INITIALISATION 
 **im      icon menu handler
 **ic      icon click handler
 **th      text handler
 */

#include "event.h"
#include "baricon.h"
#include "res.h"
#include "resspr.h"
#include "win.h"
#include "wimpt.h"
#include "menu.h"
#include "template.h"
#include "dbox.h"
 
#include <stdio.h>
#include <stdlib.h>
 
/****************************************/
/**              CONSTANTS              */
/****************************************/
 
#define APP_NAME        "C_Demo"
#define ICON_NAME       "!C_Demo"
#define MENU_ITEMS      "Test,Quit"
 
#define i_menu_test     1
#define i_menu_quit     2
 
/****************************************/
/**fp       FUNCTION PROTOTYPES         */
/****************************************/

static void i_menuproc(void *handle,\
                               char *hit);
static BOOL cd_initialise(void);
static void text_handler(dbox d,\
                            void *handle);
 
/****************************************/
/**gv      GLOBAL VARIABLES             */
/****************************************/

static menu i_menu;
static dbox text_box; 

/****************************************/
/**fd       FUNCTION DEFINITIONS        */
/****************************************/

/*--------------------------------------*/
/**th          text handler             */
/*--------------------------------------*/

static void text_handler(dbox d,\
                            void *handle)
{

  if (dbox_get(d)==dbox_CLOSE) dbox_hide(d);

}
 
/*--------------------------------------*/
/**im     icon menu handler             */
/*--------------------------------------*/
 
static void i_menuproc(void *handle,\
                                char *hit)
{
  switch (hit[0])
  {
    case i_menu_test:
      dbox_showstatic(text_box);
      break;
          
    case i_menu_quit:
      dbox_dispose(&text_box);
      exit(0);
      break;

    default:
      break;
  }
}
 
 
/*--------------------------------------*/
/**ic     icon click handler            */
/*--------------------------------------*/
 
static void cd_iconclick(wimp_i icon)
{
  icon=icon;
  dbox_setfield(text_box,0,\
                    "First line of text");
  dbox_setfield(text_box,1,\
                   "Second line of text");
  dbox_setfield(text_box,24,\
             "Twenty-fifth line of text");
  
}
 
/****************************************/
/**in           INITIALISATION          */
/****************************************/
 
static BOOL cd_initialise(void)
{
  wimpt_init(APP_NAME);/*initialise task*/

  res_init(APP_NAME);      /* resources */
  template_init();         /* templates */
  dbox_init();        /* dialogue boxes */
 
               /* create menu structure */
  if (i_menu = menu_new(APP_NAME,
              MENU_ITEMS), i_menu == NULL)
    return FALSE;
    
                /* put icon on icon bar */
     /* and register icon click handler */
   baricon(ICON_NAME, (int)resspr_area(),\
                            cd_iconclick);
   
/* establish icon menu handler */
  if (!event_attachmenu(win_ICONBAR,\
                     i_menu,i_menuproc,0))
    return FALSE;

  if ((text_box=dbox_new("text"))==NULL)
    return FALSE;
  dbox_eventhandler(text_box,\
                          text_handler,0);
  return TRUE;
}
 
/*--------------------------------------*/
/**main    The main() function          */
/*--------------------------------------*/
 
int main(void)
{
  if (cd_initialise())
  {
    while (TRUE)
      event_process();
  }
  return 0;
}
