/* !ROLibEx.c.!RunImage */

#include <stdlib.h>
#include <string.h>

#include "wimp.h"
#include "baricon.h"
#include "win.h"
#include "template.h"
#include "menu.h" 
#include "wimpt.h"
#include "dbox.h"
#include "event.h"
#include "res.h"
#include "xfersend.h"
#include "saveas.h"

char message[120];
BOOL boxon;
dbox mainw;
                  
void Example_IBarClick(wimp_i i)
{     
  wimp_caretstr cs;
  
  i=i;
  
  if(!boxon) {
    dbox_setfield(mainw,2,message);
    dbox_showstatic(mainw);
    cs.w=dbox_syshandle(mainw);
    cs.i=2;
    cs.height=-1;
    cs.index=strlen(message);
    wimp_set_caret_pos(&cs);
    boxon=TRUE;
  }
}                  
                  
void Example_MenuHandler(void *ref,char hit[])
{        
  dbox progInfo;
  
  ref=ref;
  
  switch(hit[0]) {
    case 1:
      progInfo=dbox_new("progInfo");
      dbox_show(progInfo);
      dbox_fillin(progInfo);
      dbox_dispose(&progInfo);
      break;
    case 2:
      exit(0);
      break;
  }
}                                        

void Example_BoxClick(dbox d,void *ref)
{         
  dbox_field df=dbox_get(d);
  
  ref=ref;    
  
  switch(df) {
    case 0:
      dbox_getfield(d,2,message,120);
      dbox_hide(d);
      boxon=FALSE;
      break;
    case 1:
      dbox_hide(d);
      boxon=FALSE;
      break;
  }
}

BOOL Example_SaveText(char *name,void *ref) 
{  
  FILE *f;
  char buffer[120];
  
  ref=ref;
  
  dbox_getfield(mainw,2,buffer,120);
  
  f=fopen(name,"w");
  fprintf(f,buffer);
  fclose(f);
  
  return TRUE;
}

void Example_BoxMenu(void *ref,char hit[])
{    
  wimp_caretstr cs;
  
  ref=ref;
  
  switch(hit[0]) {
    case 1:
      saveas(0xfff,"TextFile",100,Example_SaveText,0,0,0);
      break;
    case 2:
      dbox_setfield(mainw,2,"");
      cs.w=dbox_syshandle(mainw);
      cs.i=2;
      cs.height=-1;
      cs.index=0;
      wimp_set_caret_pos(&cs);
      break;
  }
}

int main(void)
{
  menu ibarmen,dbmen;
  
  wimpt_init("Example (RISC_OSLib)");
  res_init("rolibex");
  template_init();
  dbox_init();
  
  ibarmen=menu_new("Example",">Info,Quit");
  
  baricon("!rolibex",1,Example_IBarClick);
  event_attachmenu(win_ICONBAR,ibarmen,Example_MenuHandler,0);
                   
  mainw=dbox_new("mainw");
  dbox_eventhandler(mainw,Example_BoxClick,0);
  dbmen=menu_new("Example",">Save,Clear");
  event_attachmenu(dbox_syshandle(mainw),dbmen,Example_BoxMenu,0);
  boxon=FALSE;
  
  strcpy(message,"Hello, World - RISC_OSLib");
  
  for(;;) 
    event_process();
    
  return(0);
}
  
