/*
**
** Copyright © 1996 Kai Hofmann. All rights reserved.
** Demo for a registered MUI custom class!
**
** $VER: MonthNavigator-Demo.c 16.4 (12.12.96)
**
*/

 #define __MakeLib

 #include "system.h"
 #include "MonthNavigator_mcc.h"
 #include <clib/alib_protos.h>
 #include <proto/muimaster.h>
 #include <libraries/mui.h>
 #include "date.h"
 #include "datesys.h"
 #include "date_pragmas.h"
 #include <exec/libraries.h>
 #include <proto/utility.h>
 #include <utility/tagitem.h>
 #include <proto/exec.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>


 #ifdef DEBUG
   void kprintf(UBYTE *fmt,...);
   #define debug(x)	kprintf(x "\n");
 #else
   #define debug(x)
 #endif


 #ifndef MAKE_ID
   #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
 #endif


 struct MUI_CustomClass *App_CC,*Win_CC,*Str_CC;


 struct Library *MUIMasterBase;
 static Object *App;


 #define MUIA_Win_Type			((TAG_USER | 494 << 16) | 0x8001)
 #define MUIV_Win_Type_ReadOnly		0
 #define MUIV_Win_Type_RelVerify	1
 #define MUIV_Win_Type_Immediate	2

 #define MUIA_Win_MNObjectID		((TAG_USER | 494 << 16) | 0x8002)

 #define MUIM_Win_Update		((TAG_USER | 494 << 16) | 0x8003)
 #define MUIM_Win_UpdateDay		((TAG_USER | 494 << 16) | 0x8004)
 #define MUIM_Win_MonthUpdate		((TAG_USER | 494 << 16) | 0x8005)


 struct MUIP_Win_MonthUpdate		{ULONG MethodID; ULONG Month;};


 struct Win_Data {
                  Object *monthnavigator;
                  Object *month,*year,*day;
                 };


 struct App_Data {
                  Object *win1,*win2,*win3;
                 };


 static char *months[13];

 /* ------------------------------------------------------------------------ */

 static ULONG STACKARGS DoSuperNew(struct IClass *cl, Object *obj, ULONG tags, ...)
  {
   return(DoSuperMethod(cl,obj,OM_NEW,&tags,NULL));
  }

 /* --- MarkHook ----------------------------------------------------------- */

 static ULONG SAVEDS_ASM MarkFkt(REG(A0) struct Hook *hook, REG(A2) struct MUIS_MonthNavigator_MarkDay *day)
  {
   if ((day->Year >= 1970) && (day->Month == 9) && (day->Day == 18))
    {
     day->PreParse   = "\033c\0338\033n";
     day->ShortHelp  = "Kai's birthday";
     return(TRUE);
    }
   return(FALSE);
  }


 struct Hook MarkHook = {{NULL,NULL},(HOOKFUNC)MarkFkt,NULL,NULL};

 /* --- DragQueryHook ------------------------------------------------------ */

 static ULONG SAVEDS_ASM DragQueryFkt(REG(A0) struct Hook *hook, REG(A1) Object *obj, REG(A2) struct MUIS_MonthNavigator_MarkDay *day)
  {
   /*ULONG result;*/
   ULONG Year,Month,Day;

   if (get(obj,MUIA_MonthNavigator_Year,&Year) && get(obj,MUIA_MonthNavigator_Month,&Month) && get(obj,MUIA_MonthNavigator_Day,&Day))
    {
     return(MUIV_DragQuery_Accept);
    }
   return(MUIV_DragQuery_Refuse);
  }


 struct Hook DragQueryHook = {{NULL,NULL},(HOOKFUNC)DragQueryFkt,NULL,NULL};

 /* --- DragDropHook ------------------------------------------------------- */

 static ULONG SAVEDS_ASM DragDropFkt(REG(A0) struct Hook *hook, REG(A1) Object *obj, REG(A2) struct MUIS_MonthNavigator_MarkDay *day)
  {
   /*ULONG result;*/
   static char date[11];
   ULONG Day,Month,Year;

   day->PreParse = "\033c\0338\033n";
   /*result =*/ get(obj,MUIA_MonthNavigator_Day,&Day);
   /*result =*/ get(obj,MUIA_MonthNavigator_Month,&Month);
   /*result =*/ get(obj,MUIA_MonthNavigator_Year,&Year);
   sprintf(date,"%04lu-%02lu-%02lu",Year,Month,Day);
   day->ShortHelp = date;
   return(TRUE);
  }


 struct Hook DragDropHook = {{NULL,NULL},(HOOKFUNC)DragDropFkt,NULL,NULL};

 /* --- String ------------------------------------------------------------- */

 static ULONG Str_New(struct IClass *cl, Object *obj, struct opSet *msg)
  {
   obj = (Object *)DoSuperNew(cl,obj,
                              MUIA_String_Format,	MUIV_String_Format_Left,
                              MUIA_String_MaxLen,	30,
                              MUIA_Frame,		MUIV_Frame_String,
                              MUIA_Background,		MUII_TextBack,
                              TAG_MORE, 		msg->ops_AttrList
                             );
   return((ULONG)obj);
  }


 static ULONG Str_DragQuery(struct IClass *cl, Object *obj, struct MUIP_DragQuery *msg)
  {
   /*struct Str_Data *data = (struct Str_Data *)INST_DATA(cl,obj);*/
   /*ULONG result;*/
   ULONG Year,Month,Day;

   if (get(msg->obj,MUIA_MonthNavigator_Year,&Year) && get(msg->obj,MUIA_MonthNavigator_Month,&Month) && get(msg->obj,MUIA_MonthNavigator_Day,&Day))
    {
     if (Day > 0)
      {
       return(MUIV_DragQuery_Accept);
      }
    }
   return(MUIV_DragQuery_Refuse);
  }


 static ULONG Str_DragDrop(struct IClass *cl, Object *obj, struct MUIP_DragDrop *msg)
  {
   /*struct Str_Data *data = (struct Str_Data *)INST_DATA(cl,obj);*/
   /*ULONG result;*/
   char str[30];
   ULONG Day,Month,Year;

   /*result =*/ get(msg->obj,MUIA_MonthNavigator_Year,&Year);
   /*result =*/ get(msg->obj,MUIA_MonthNavigator_Month,&Month);
   /*result =*/ get(msg->obj,MUIA_MonthNavigator_Day,&Day);
   sprintf(str,"%lu-%lu-%lu",Year,Month,Day);
   /*result =*/ set(obj,MUIA_String_Contents,(ULONG)str);
   return(0);
  }


 static ULONG SAVEDS_ASM Str_Dispatcher(REG(A0) struct IClass *cl, REG(A2) Object *obj, REG(A1) Msg msg)
  {
   switch (msg->MethodID)
    {
     case OM_NEW			: return(Str_New(cl,obj,(struct opSet *)msg));
     case MUIM_DragQuery		: return(Str_DragQuery(cl,obj,(struct MUIP_DragQuery *)msg));
     case MUIM_DragDrop			: return(Str_DragDrop(cl,obj,(struct MUIP_DragDrop *)msg));
     default				: return(DoSuperMethodA(cl,obj,msg));
    }
  }

 /* --- Window ------------------------------------------------------------- */

 static ULONG Win_Update(struct IClass *cl, Object *obj, Msg msg)
  {
   struct Win_Data *data = (struct Win_Data *)INST_DATA(cl,obj);
   ULONG year,month;

   get(data->month,MUIA_Cycle_Active,&month);
   month++;
   get(data->year,MUIA_Numeric_Value,&year);
   set(data->monthnavigator,MUIA_MonthNavigator_Month,month);
   set(data->monthnavigator,MUIA_MonthNavigator_Year,year);
   if (data->day != NULL)
    {
     set(data->day,MUIA_Text_Contents,"0");
    }
   return(0);
  }


 static ULONG Win_UpdateDay(struct IClass *cl, Object *obj, Msg msg)
  {
   struct Win_Data *data = (struct Win_Data *)INST_DATA(cl,obj);
   ULONG day;
   char daystr[3];

   get(data->monthnavigator,MUIA_MonthNavigator_Day,&day);
   sprintf(daystr,"%lu",day);
   set(data->day,MUIA_Text_Contents,daystr);
   return(0);
  }


 static ULONG Win_MonthUpdate(struct IClass *cl, Object *obj, struct MUIP_Win_MonthUpdate *msg)
  {
   struct Win_Data *data = (struct Win_Data *)INST_DATA(cl,obj);
   /*ULONG result;*/

   /*result =*/ DoMethod(data->month,MUIM_NoNotifySet,MUIA_Cycle_Active,msg->Month-1);
   return(0);
  }


 static ULONG Win_New(struct IClass *cl, Object *obj, struct opSet *msg)
  {
   struct TagItem *tags,*tag;
   ULONG type = MUIV_Win_Type_ReadOnly,objectid=0;
   Object *monthnavigator,*monthobj,*yearobj,*pyearobj,*myearobj,
	  *dayobj = NULL,*saveobj,*loadobj;

   tags = msg->ops_AttrList;
   while (tag = NextTagItem(&tags))
    {
     switch (tag->ti_Tag)
      {
       case MUIA_Win_Type	: type = (ULONG)tag->ti_Data;
                                  break;
       case MUIA_Win_MNObjectID : objectid = (ULONG)tag->ti_Data;
                                  break;
      }
    }
   obj = (Object *)DoSuperNew(cl,obj,
                              MUIA_Window_ID,			MAKE_ID('D','E','M','0'+type),
                              MUIA_Window_Title,		(type == MUIV_Win_Type_ReadOnly) ? "ReadOnly" : ((type == MUIV_Win_Type_RelVerify) ? "RelVerify" : "Immediate"),
                              MUIA_Window_ScreenTitle,		"MonthNavigator-Demo V2.4",
                              MUIA_Window_RootObject,		VGroup,
                                MUIA_Group_SameWidth,		TRUE,
                                MUIA_Group_Child,		HGroup,
                                  MUIA_Frame,			MUIV_Frame_Group,
                                  MUIA_Background,		MUII_GroupBack,
                                  MUIA_Group_SameHeight,	TRUE,
                                  MUIA_Group_Child,		RectangleObject,
                                    MUIA_HorizWeight,		(type == MUIV_Win_Type_RelVerify) ? 100 : 50,
                                  End,
                                  MUIA_Group_Child,		(type == MUIV_Win_Type_RelVerify) ? dayobj = TextObject,
                                    MUIA_Text_PreParse,		"\33r",
                                    MUIA_Frame,			MUIV_Frame_Text,
                                    MUIA_Background,		MUII_TextBack,
                                    MUIA_FixWidthTxt,		"MM",
                                  End :
                                  RectangleObject,
                                    MUIA_HorizWeight,		50,
                                  End,
                                  MUIA_Group_Child,		monthobj = CycleObject,
                                    MUIA_Cycle_Entries,		months,
                                    MUIA_Font,			MUIV_Font_Button,
                                    MUIA_CycleChain,		1,
                                  End,
                                  MUIA_Group_Child,		yearobj = NumericbuttonObject,
                                    MUIA_Numeric_Format,	"%lu",
                                    MUIA_Numeric_Min,		8,
                                    MUIA_Numeric_Max,		8000,
                                    MUIA_Font,			MUIV_Font_Button,
                                    MUIA_CycleChain,		1,
                                  End,
                                  MUIA_Group_Child,		myearobj = ImageObject,
                                    MUIA_Frame,           	MUIV_Frame_Button,
                                    MUIA_InputMode,       	MUIV_InputMode_RelVerify,
                                    MUIA_Image_Spec,      	MUII_ArrowLeft,
                                    MUIA_Image_FreeVert,  	TRUE,
                                    MUIA_Background,      	MUII_ButtonBack,
                                    MUIA_ShowSelState,    	FALSE,
                                    MUIA_CycleChain,		1,
                                  End,
                                  MUIA_Group_Child,		pyearobj = ImageObject,
                                    MUIA_Frame,           	MUIV_Frame_Button,
                                    MUIA_InputMode,       	MUIV_InputMode_RelVerify,
                                    MUIA_Image_Spec,      	MUII_ArrowRight,
                                    MUIA_Image_FreeVert,  	TRUE,
                                    MUIA_Background,      	MUII_ButtonBack,
                                    MUIA_ShowSelState,    	FALSE,
                                    MUIA_CycleChain,		1,
                                  End,
                                  MUIA_Group_Child,		RectangleObject,
                                  End,
                                End,
                                MUIA_Group_Child,		monthnavigator = MonthNavigatorObject,
                                  MUIA_Frame,				MUIV_Frame_Group,
                                  MUIA_FrameTitle,			"MonthNavigator.mcc",
                                  MUIA_Background,			MUII_GroupBack,
                                  MUIA_MonthNavigator_InputMode,	type,
                                  MUIA_MonthNavigator_Draggable,	TRUE,
                                  MUIA_ObjectID,			objectid,
                                  MUIA_MonthNavigator_MarkHook,		/*&MarkHook*/MUIV_MonthNavigator_MarkHook_HiToday,
                                  /*MUIA_Dropable,			FALSE,*/
                                  MUIA_MonthNavigator_DragQueryHook,	&DragQueryHook,
                                  MUIA_MonthNavigator_DragDropHook,	&DragDropHook,
                                  MUIA_MonthNavigator_Dropable,		TRUE,
                                  MUIA_CycleChain,			1,
                                End,
                                MUIA_Group_Child,		RectangleObject,
                                End,
                                MUIA_Group_Child,		HGroup,
                                  MUIA_Frame,			MUIV_Frame_Group,
                                  MUIA_Background,		MUII_GroupBack,
                                  MUIA_Group_SameHeight,	TRUE,
                                  MUIA_Group_Child,		Label2("Drop here:"),
                                  MUIA_Group_Child,		NewObject(Str_CC->mcc_Class,NULL,
				    MUIA_Dropable,		TRUE,
				  TAG_DONE),
                                End,
                                MUIA_Group_Child,		HGroup,
                                  MUIA_Frame,			MUIV_Frame_Group,
                                  MUIA_Background,		MUII_GroupBack,
                                  MUIA_Group_SameHeight,	TRUE,
                                  MUIA_Group_Child,		loadobj = TextObject,
                                    MUIA_Frame,			MUIV_Frame_Button,
                                    MUIA_Background,		MUII_ButtonBack,
                                    MUIA_Font,			MUIV_Font_Button,
                                    MUIA_Text_PreParse,		"\33c",
                                    MUIA_InputMode,		MUIV_InputMode_RelVerify,
                                    MUIA_Text_Contents,		"Load",
                                    MUIA_Text_HiChar,		'L',
                                    MUIA_ControlChar,		'l',
                                    MUIA_CycleChain,		1,
                                  End,
                                  MUIA_Group_Child,		saveobj = TextObject,
                                    MUIA_Frame,			MUIV_Frame_Button,
                                    MUIA_Background,		MUII_ButtonBack,
                                    MUIA_Font,			MUIV_Font_Button,
                                    MUIA_Text_PreParse,		"\33c",
                                    MUIA_InputMode,		MUIV_InputMode_RelVerify,
                                    MUIA_Text_Contents,		"Save",
                                    MUIA_Text_HiChar,		'S',
                                    MUIA_ControlChar,		's',
                                    MUIA_CycleChain,		1,
                                  End,
                                End,
                              End,
                              TAG_MORE, msg->ops_AttrList
                             );
   if (obj != NULL)
    {
     struct Win_Data *data = (struct Win_Data *)INST_DATA(cl,obj);
     ULONG month,year,day;
     char daystr[3];
     /*ULONG result;*/

     data->monthnavigator = monthnavigator;
     data->month = monthobj;
     data->year = yearobj;
     data->day = dayobj;

     get(monthnavigator,MUIA_MonthNavigator_Month,&month);
     set(monthobj,MUIA_Cycle_Active,month-1);
     get(monthnavigator,MUIA_MonthNavigator_Year,&year);
     set(yearobj,MUIA_Numeric_Default,year);
     set(yearobj,MUIA_Numeric_Value,year);
     if (dayobj != NULL)
      {
       get(monthnavigator,MUIA_MonthNavigator_Day,&day);
       sprintf(daystr,"%lu",day);
       set(dayobj,MUIA_Text_Contents,daystr);
       /*result =*/ DoMethod(monthnavigator,MUIM_Notify,MUIA_MonthNavigator_Day,MUIV_EveryTime,MUIV_Notify_Window,1,MUIM_Win_UpdateDay);
      }
     /*result =*/ DoMethod(monthobj,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,MUIV_Notify_Window,1,MUIM_Win_Update);
     /*result =*/ DoMethod(yearobj,MUIM_Notify,MUIA_Numeric_Value,MUIV_EveryTime,MUIV_Notify_Window,1,MUIM_Win_Update);
     /*result =*/ DoMethod(myearobj,MUIM_Notify,MUIA_Pressed,FALSE,yearobj,2,MUIM_Numeric_Decrease,1);
     /*result =*/ DoMethod(pyearobj,MUIM_Notify,MUIA_Pressed,FALSE,yearobj,2,MUIM_Numeric_Increase,1);
     /*result =*/ DoMethod(monthnavigator,MUIM_Notify,MUIA_MonthNavigator_Year,MUIV_EveryTime,yearobj,3,MUIM_NoNotifySet,MUIA_Numeric_Value,MUIV_TriggerValue);
     /*result =*/ DoMethod(monthnavigator,MUIM_Notify,MUIA_MonthNavigator_Month,MUIV_EveryTime,MUIV_Notify_Window,2,MUIM_Win_MonthUpdate,MUIV_TriggerValue);
     /*result =*/ DoMethod(loadobj,MUIM_Notify,MUIA_Pressed,FALSE,MUIV_Notify_Application,2,MUIM_Application_Load,MUIV_Application_Load_ENV);
     /*result =*/ DoMethod(saveobj,MUIM_Notify,MUIA_Pressed,FALSE,MUIV_Notify_Application,2,MUIM_Application_Save,MUIV_Application_Save_ENV);
    }
   return((ULONG)obj);
  }


 static ULONG SAVEDS_ASM Win_Dispatcher(REG(A0) struct IClass *cl, REG(A2) Object *obj, REG(A1) Msg msg)
  {
   switch (msg->MethodID)
    {
     case OM_NEW			: return(Win_New(cl,obj,(struct opSet *)msg));
     case MUIM_Win_Update		: return(Win_Update(cl,obj,msg));
     case MUIM_Win_UpdateDay		: return(Win_UpdateDay(cl,obj,msg));
     case MUIM_Win_MonthUpdate		: return(Win_MonthUpdate(cl,obj,(struct MUIP_Win_MonthUpdate *)msg));
     default				: return(DoSuperMethodA(cl,obj,msg));
    }
  }

 /* --- Application -------------------------------------------------------- */

 static ULONG App_New(struct IClass *cl, Object *obj, struct opSet *msg)
  {
   Object *win1,*win2,*win3;

   obj = (Object *)DoSuperNew(cl,obj,
                              MUIA_Application_Title,           "MonthNavigator-Demo",
                              MUIA_Application_Author,          "Kai Hofmann",
                              MUIA_Application_Copyright,       "© 1996 Kai Hofmann",
                              MUIA_Application_Version,         "$VER: MonthNavigator-Demo 2.4 " __AMIGADATE__,
                              MUIA_Application_Description,     "MonthNavigator demonstration program",
                              MUIA_Application_Base,            "MNDEMO",
                              MUIA_Application_SingleTask,      TRUE,
                              MUIA_Application_Active,          TRUE,
                              MUIA_Application_Window,		win1 = NewObject(Win_CC->mcc_Class,NULL,
										   MUIA_Win_Type,	MUIV_Win_Type_ReadOnly,
										   MUIA_Win_MNObjectID,	1,
										 TAG_DONE
										),
                              MUIA_Application_Window,		win2 = NewObject(Win_CC->mcc_Class,NULL,
										   MUIA_Win_Type,	MUIV_Win_Type_RelVerify,
										   MUIA_Win_MNObjectID,	2,
										 TAG_DONE
										),
                              MUIA_Application_Window,		win3 = NewObject(Win_CC->mcc_Class,NULL,
										   MUIA_Win_Type,	MUIV_Win_Type_Immediate,
										   MUIA_Win_MNObjectID,	3,
										 TAG_DONE
										),
                              TAG_MORE, msg->ops_AttrList
                             );
   if (obj != NULL)
    {
     struct App_Data *data = (struct App_Data *)INST_DATA(cl,obj);
     /*ULONG result;*/

     data->win1 = win1;
     data->win2 = win2;
     data->win3 = win3;

     /*result =*/ DoMethod(win1,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,MUIV_Notify_Application,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
     set(win1,MUIA_Window_Open,TRUE);
     /*result =*/ DoMethod(win2,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,MUIV_Notify_Application,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
     set(win2,MUIA_Window_Open,TRUE);
     /*result =*/ DoMethod(win3,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,MUIV_Notify_Application,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
     set(win3,MUIA_Window_Open,TRUE);
    }
   return((ULONG)obj);
  }


 static ULONG SAVEDS_ASM App_Dispatcher(REG(A0) struct IClass *cl, REG(A2) Object *obj, REG(A1) Msg msg)
  {
   switch (msg->MethodID)
    {
     case OM_NEW			: return(App_New(cl,obj,(struct opSet *)msg));
     default				: return(DoSuperMethodA(cl,obj,msg));
    }
  }

 /* ------------------------------------------------------------------------ */

 long muiclasses_Init(void)
  {
   long retstat = RETURN_OK;

   App_CC = MUI_CreateCustomClass(NULL,MUIC_Application,NULL,sizeof(struct App_Data),App_Dispatcher);
   if (App_CC == NULL)
    {
     printf("Can not create 'App' privat custom class!\n");
     retstat = RETURN_ERROR;
    }
   else
    {
     Win_CC = MUI_CreateCustomClass(NULL,MUIC_Window,NULL,sizeof(struct Win_Data),Win_Dispatcher);
     if (Win_CC == NULL)
      {
       printf("Can not create 'Win' privat custom class!\n");
       retstat = RETURN_ERROR;
      }
     else
      {
       Str_CC = MUI_CreateCustomClass(NULL,MUIC_String,NULL,0,Str_Dispatcher);
       if (Str_CC == NULL)
        {
         printf("Can not create 'Str' privat custom class!\n");
         retstat = RETURN_ERROR;
        }
      }
    }
   return(retstat);
  }


 long muiclasses_Cleanup(void)
  {
   long retstat = RETURN_OK;

   if (Str_CC != NULL)
    {
     if (!MUI_DeleteCustomClass(Str_CC))
      {
       printf("Can not delete 'Str' privat custom class!\n");
       retstat = RETURN_ERROR;
      }
    }
   if (Win_CC != NULL)
    {
     if (!MUI_DeleteCustomClass(Win_CC))
      {
       printf("Can not delete 'Win' privat custom class!\n");
       retstat = RETURN_ERROR;
      }
    }
   if (App_CC != NULL)
    {
     if (!MUI_DeleteCustomClass(App_CC))
      {
       printf("Can not delete 'App' privat custom class!\n");
       retstat = RETURN_ERROR;
      }
    }
   return(retstat);
  }

 /* ------------------------------------------------------------------------ */

 long gui_Init(void)
  {
   long retstat;

   MUIMasterBase = OpenLibrary((UBYTE *)MUIMASTER_NAME,(unsigned long)MUIMASTER_VMIN);
   if (MUIMasterBase != NULL)
    {
     retstat = muiclasses_Init();
     if (retstat == RETURN_OK)
      {
       Object *mn;

       mn = MUI_NewObject(MUIC_MonthNavigator,
                          TAG_DONE
                         );
       if (mn != NULL)
        {
         MUI_DisposeObject(mn);
         App = NewObject(App_CC->mcc_Class,NULL,TAG_DONE);
         if (App == NULL)
          {
           printf("Can not create application object!\n");
           retstat = RETURN_FAIL;
          }
        }
       else
        {
         printf("Missing MonthNavigator.mcc!\n");
         retstat = RETURN_FAIL;
        }
      }
    }
   else
    {
     printf("Can not open muimaster.library V%lu!\n",(ULONG)MUIMASTER_VMIN);
     retstat = RETURN_FAIL;
    }
   return(retstat);
  }


 long gui_Cleanup(void)
  {
   long retstat = RETURN_OK;

   if (MUIMasterBase != NULL)
    {
     if (App != NULL)
      {
       MUI_DisposeObject(App);
      }
     retstat = muiclasses_Cleanup();
     if (retstat == RETURN_OK)
      {
       CloseLibrary(MUIMasterBase);
      }
     else
      {
       printf("Can not close muimaster.library!\n");
       retstat = RETURN_FAIL;
      }
    }
   return(retstat);
  }


 void gui_MainLoop(void)
  {
   if (App != NULL)
    {
     ULONG signals=0;

     while (DoMethod(App,(unsigned long)MUIM_Application_NewInput,&signals) != MUIV_Application_ReturnID_Quit)
      {
       if (signals)
        {
         signals = Wait(signals | SIGBREAKF_CTRL_C);
         if (signals & SIGBREAKF_CTRL_C)
          {
           /*ULONG result;*/

           /*result =*/ DoMethod(App,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
          }
        }
      }
    }
  }

 /* ------------------------------------------------------------------------ */

 void main(void)
  {
   struct Library *DateBase;
   BOOL error = FALSE;

   DateBase = OpenLibrary(DATE_NAME,33);
   if (DateBase != NULL)
    {
     if (DateBase->lib_Revision >= 215)
      {
       short i;
       char mn[15];

       for (i=0;i<12;i++)
        {
         date_MonthText(i+1,mn,date_Locale);
         months[i] = (char *)malloc(strlen(mn)+1);
         if (months[i] != NULL)
          {
           strcpy(months[i],mn);
          }
         else
          {
           i--;
           while (i>=0)
            {
             free(months[i]);
             i--;
            }
           printf("Out of memory error!\n");
           error = TRUE;
           break;
          }
        }
       if (!error)
        {
         months[12] = NULL;
         if (gui_Init() == RETURN_OK)
          {
           gui_MainLoop();
           /*retstat =*/ gui_Cleanup();
          }
         for (i=0;i<12;i++)
          {
           free(months[i]);
          }
        }
      }
     else
      {
       printf("Can not open date.library V33.215\n");
      }
     CloseLibrary(DateBase);
    }
   else
    {
     printf("Can not open date.library V33\n");
    }
  }
