home *** CD-ROM | disk | FTP | other *** search
/ MACD 7 / MACD7.iso / mui / mcc_datetext / developer / c / examples / datetext-demo.c
Encoding:
C/C++ Source or Header  |  1998-01-12  |  18.0 KB  |  543 lines

  1. /*
  2. **
  3. ** Copyright © 1997-1998 Kai Hofmann. All rights reserved.
  4. ** Registered MUI custom class!
  5. **
  6. ** $VER: DateText-Demo.c 12.0 (03.01.98)
  7. **
  8. */
  9.  
  10.  #define __MakeLib
  11.  
  12.  
  13.  #include "system.h"
  14.  #include <mui/Date_mcc.h>
  15.  #include <mui/DateText_mcc.h>
  16.  #include <libraries/mui.h>
  17.  #include <proto/muimaster.h>
  18.  #include <proto/date.h>
  19.  #include <exec/libraries.h>
  20.  #include <exec/memory.h>
  21.  #include <proto/exec.h>
  22.  #include <proto/intuition.h>
  23.  #include <utility/tagitem.h>
  24.  #include <proto/utility.h>
  25.  #include <clib/alib_protos.h>
  26.  #include <string.h>
  27.  
  28.  
  29.  #ifdef DEBUG
  30.    void kprintf(UBYTE *fmt,...);
  31.    #define debug(x)    kprintf(x "\n");
  32.  #else
  33.    #define debug(x)
  34.  #endif
  35.  
  36.  
  37.  #ifndef MAKE_ID
  38.    #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  39.  #endif
  40.  
  41.  
  42.  struct MUI_CustomClass *App_CC=NULL,*Win_CC=NULL,*Str_CC=NULL;
  43.  
  44.  
  45.  struct Library *DateBase;
  46.  struct Library *MUIMasterBase;
  47.  static Object *App;
  48.  
  49.  static char *months[13];
  50.  
  51.  /* ------------------------------------------------------------------------ */
  52.  
  53.  static ULONG STACKARGS DoSuperNew(struct IClass *const cl, Object *const obj, ULONG tags, ...)
  54.   {
  55.    return(DoSuperMethod(cl,obj,OM_NEW,&tags,NULL));
  56.   }
  57.  
  58.  /* --- String ------------------------------------------------------------- */
  59.  
  60.  static ULONG Str_New(struct IClass *cl, Object *obj, struct opSet *msg)
  61.   {
  62.    obj = (Object *)DoSuperNew(cl,obj,
  63.                               MUIA_String_Format,    MUIV_String_Format_Left,
  64.                               MUIA_String_MaxLen,    30,
  65.                               MUIA_Frame,        MUIV_Frame_String,
  66.                               MUIA_Background,        MUII_TextBack,
  67.                               TAG_MORE, msg->ops_AttrList
  68.                              );
  69.    return((ULONG)obj);
  70.   }
  71.  
  72.  
  73.  static ULONG Str_DragQuery(struct IClass *cl, Object *obj, struct MUIP_DragQuery *msg)
  74.   {
  75.    /*struct Str_Data *data = (struct Str_Data *)INST_DATA(cl,obj);*/
  76.    /*ULONG result;*/
  77.    ULONG Year,Month,Day;
  78.  
  79.    if (get(msg->obj,MUIA_Date_Year,&Year) && get(msg->obj,MUIA_Date_Month,&Month) && get(msg->obj,MUIA_Date_Day,&Day))
  80.     {
  81.      if (date_ValidHeisDate((unsigned short)Day,(unsigned short)Month,(long)Year))
  82.       {
  83.        return(MUIV_DragQuery_Accept);
  84.       }
  85.     }
  86.    return(MUIV_DragQuery_Refuse);
  87.   }
  88.  
  89.  
  90.  static ULONG Str_DragDrop(struct IClass *cl, Object *obj, struct MUIP_DragDrop *msg)
  91.   {
  92.    /*struct Str_Data *data = (struct Str_Data *)INST_DATA(cl,obj);*/
  93.    /*ULONG result;*/
  94.    char str[13];
  95.    ULONG Day,Month,Year;
  96.  
  97.    /*result =*/ get(msg->obj,MUIA_Date_Year,&Year);
  98.    /*result =*/ get(msg->obj,MUIA_Date_Month,&Month);
  99.    /*result =*/ get(msg->obj,MUIA_Date_Day,&Day);
  100.    date_FormatDate("%Y-%Dmv-%Ddv",(unsigned short)Day,(unsigned short)Month,(long)Year,date_Locale,str);
  101.    /*result =*/ set(obj,MUIA_String_Contents,(ULONG)str);
  102.    return(0);
  103.   }
  104.  
  105.  
  106.  static ULONG SAVEDS_ASM Str_Dispatcher(REG(A0) struct IClass *cl, REG(A2) Object *obj, REG(A1) Msg msg)
  107.   {
  108.    switch (msg->MethodID)
  109.     {
  110.      case OM_NEW            : return(Str_New(cl,obj,(struct opSet *)msg));
  111.      case MUIM_DragQuery        : return(Str_DragQuery(cl,obj,(struct MUIP_DragQuery *)msg));
  112.      case MUIM_DragDrop            : return(Str_DragDrop(cl,obj,(struct MUIP_DragDrop *)msg));
  113.      default                : return(DoSuperMethodA(cl,obj,msg));
  114.     }
  115.   }
  116.  
  117.  /* --- Window ------------------------------------------------------------- */
  118.  
  119.  static ULONG Win_New(struct IClass *cl, Object *obj, struct opSet *msg)
  120.   {
  121.    Object *DateText,*Day,*Month,*Year,*saveobj,*loadobj,*format;
  122.  
  123.    obj = (Object *)DoSuperNew(cl,obj,
  124.                               MUIA_Window_ID,            MAKE_ID('D','E','M','O'),
  125.                               MUIA_Window_Title,        "DateText-Demo",
  126.                               MUIA_Window_ScreenTitle,        "DateText-Demo V12.0",
  127.                               MUIA_Window_RootObject,        VGroup,
  128.                                 MUIA_Group_SameWidth,        TRUE,
  129.                                 MUIA_Group_Child,        DateText = DateTextObject,
  130.                             MUIA_Frame,            MUIV_Frame_Text,
  131.                             MUIA_FrameTitle,        "DateText.mcc",
  132.                             MUIA_Background,        MUII_TextBack,
  133.                             MUIA_Draggable,        TRUE,
  134.                             MUIA_ObjectID,        1,
  135.                         End,
  136.                                 MUIA_Group_Child,        HGroup,
  137.                                   MUIA_Group_SameHeight,    TRUE,
  138.                                   MUIA_Frame,            MUIV_Frame_Group,
  139.                                   MUIA_Group_Child,        RectangleObject,
  140.                                   End,
  141.                                   MUIA_Group_Child,        Day = NumericbuttonObject,
  142.                                     MUIA_Numeric_Format,    "%lu",
  143.                                     MUIA_Numeric_Min,        1,
  144.                                     MUIA_Numeric_Max,        31,
  145.                                     MUIA_Numeric_Default,    1,
  146.                                     /*MUIA_Numeric_Value,    6,*/
  147.                                     MUIA_CycleChain,        1,
  148.                                     MUIA_Font,            MUIV_Font_Button,
  149.                                   End,
  150.                                   MUIA_Group_Child,        Month = NumericbuttonObject,
  151.                                     MUIA_Numeric_Format,    "%lu",
  152.                                     MUIA_Numeric_Min,        1,
  153.                                     MUIA_Numeric_Max,        12,
  154.                                     MUIA_Numeric_Default,    1,
  155.                                     /*MUIA_Numeric_Value,    6,*/
  156.                                     MUIA_CycleChain,        1,
  157.                                     MUIA_Font,            MUIV_Font_Button,
  158.                                   End,
  159.                                   MUIA_Group_Child,        Year = NumericbuttonObject,
  160.                                     MUIA_Numeric_Format,    "%lu",
  161.                                     MUIA_Numeric_Min,        8,
  162.                                     MUIA_Numeric_Max,        8000,
  163.                                     MUIA_Numeric_Default,    1997,
  164.                                     /*MUIA_Numeric_Value,    1997,*/
  165.                                     MUIA_CycleChain,        1,
  166.                                     MUIA_Font,            MUIV_Font_Button,
  167.                                   End,
  168.                                   MUIA_Group_Child,        RectangleObject,
  169.                                   End,
  170.                                 End,
  171.                                 MUIA_Group_Child,        HGroup,
  172.                                   MUIA_Frame,            MUIV_Frame_Group,
  173.                                   MUIA_Background,        MUII_GroupBack,
  174.                                   MUIA_Group_SameHeight,    TRUE,
  175.                                   MUIA_Group_Child,        Label2("Format:"),
  176.                                   MUIA_Group_Child,        format = StringObject,
  177.                                     MUIA_Frame,            MUIV_Frame_String,
  178.                                     MUIA_Background,        MUII_TextBack,
  179.                                     MUIA_String_Accept,        "%.-/, edBbhmyYjwaAUWxDJfvs24nM",
  180.                                     MUIA_String_Format,        MUIV_String_Format_Left,
  181.                                     MUIA_String_MaxLen,        25,
  182.                                     MUIA_CycleChain,        1,
  183.                                   End,
  184.                                 End,
  185.                                 MUIA_Group_Child,        HGroup,
  186.                                   MUIA_Frame,            MUIV_Frame_Group,
  187.                                   MUIA_Background,        MUII_GroupBack,
  188.                                   MUIA_Group_SameHeight,    TRUE,
  189.                                   MUIA_Group_Child,        Label2("Drop here:"),
  190.                                   MUIA_Group_Child,        NewObject(Str_CC->mcc_Class,NULL,
  191.                     MUIA_Dropable,        TRUE,
  192.                   End,
  193.                                 End,
  194.                                 MUIA_Group_Child,        HGroup,
  195.                                   MUIA_Frame,            MUIV_Frame_Group,
  196.                                   MUIA_Background,        MUII_GroupBack,
  197.                                   MUIA_Group_SameHeight,    TRUE,
  198.                                   MUIA_Group_Child,        loadobj = TextObject,
  199.                                     MUIA_Frame,            MUIV_Frame_Button,
  200.                                     MUIA_Background,        MUII_ButtonBack,
  201.                                     MUIA_Font,            MUIV_Font_Button,
  202.                                     MUIA_Text_PreParse,        "\33c",
  203.                                     MUIA_InputMode,        MUIV_InputMode_RelVerify,
  204.                                     MUIA_Text_Contents,        "Load",
  205.                                     MUIA_Text_HiChar,        'L',
  206.                                     MUIA_ControlChar,        'l',
  207.                                     MUIA_CycleChain,        1,
  208.                                   End,
  209.                                   MUIA_Group_Child,        saveobj = TextObject,
  210.                                     MUIA_Frame,            MUIV_Frame_Button,
  211.                                     MUIA_Background,        MUII_ButtonBack,
  212.                                     MUIA_Font,            MUIV_Font_Button,
  213.                                     MUIA_Text_PreParse,        "\33c",
  214.                                     MUIA_InputMode,        MUIV_InputMode_RelVerify,
  215.                                     MUIA_Text_Contents,        "Save",
  216.                                     MUIA_Text_HiChar,        'S',
  217.                                     MUIA_ControlChar,        's',
  218.                                     MUIA_CycleChain,        1,
  219.                                   End,
  220.                                 End,
  221.                               End,
  222.                               TAG_MORE, msg->ops_AttrList
  223.                              );
  224.    if (obj != NULL)
  225.     {
  226.      /*struct Win_Data *data = (struct Win_Data *)INST_DATA(cl,obj);*/
  227.      /*ULONG result;*/
  228.      ULONG day,month,year;
  229.      STRPTR dateformat;
  230.  
  231.      /*result =*/ DoMethod(DateText,MUIM_Date_SetCurrent);
  232.  
  233.      /*result =*/ get(DateText,MUIA_Date_Day,&day);
  234.      /*result =*/ get(DateText,MUIA_Date_Month,&month);
  235.      /*result =*/ get(DateText,MUIA_Date_Year,&year);
  236.      /*result =*/ set(Day,MUIA_Numeric_Value,day);
  237.      /*result =*/ set(Month,MUIA_Numeric_Value,month);
  238.      /*result =*/ set(Year,MUIA_Numeric_Value,year);
  239.  
  240.      /*result =*/ get(DateText,MUIA_DateText_DateFormat,&dateformat);
  241.      /*result =*/ set(format,MUIA_String_Contents,dateformat);
  242.  
  243.      /*result =*/ DoMethod(Day,MUIM_Notify,MUIA_Numeric_Value,MUIV_EveryTime,DateText,3,MUIM_Set,MUIA_Date_Day,MUIV_TriggerValue);
  244.      /*result =*/ DoMethod(Month,MUIM_Notify,MUIA_Numeric_Value,MUIV_EveryTime,DateText,3,MUIM_Set,MUIA_Date_Month,MUIV_TriggerValue);
  245.      /*result =*/ DoMethod(Year,MUIM_Notify,MUIA_Numeric_Value,MUIV_EveryTime,DateText,3,MUIM_Set,MUIA_Date_Year,MUIV_TriggerValue);
  246.  
  247.      /*result =*/ DoMethod(format,MUIM_Notify,MUIA_String_Acknowledge,MUIV_EveryTime,DateText,3,MUIM_Set,MUIA_DateText_DateFormat,MUIV_TriggerValue);
  248.  
  249.      /*result =*/ DoMethod(loadobj,MUIM_Notify,MUIA_Pressed,FALSE,MUIV_Notify_Application,2,MUIM_Application_Load,MUIV_Application_Load_ENV);
  250.      /*result =*/ DoMethod(saveobj,MUIM_Notify,MUIA_Pressed,FALSE,MUIV_Notify_Application,2,MUIM_Application_Save,MUIV_Application_Save_ENV);
  251.     }
  252.    return((ULONG)obj);
  253.   }
  254.  
  255.  
  256.  static ULONG SAVEDS_ASM Win_Dispatcher(REG(A0) struct IClass *cl, REG(A2) Object *obj, REG(A1) Msg msg)
  257.   {
  258.    switch (msg->MethodID)
  259.     {
  260.      case OM_NEW            : return(Win_New(cl,obj,(struct opSet *)msg));
  261.      default                : return(DoSuperMethodA(cl,obj,msg));
  262.     }
  263.   }
  264.  
  265.  /* --- Application -------------------------------------------------------- */
  266.  
  267.  static ULONG App_New(struct IClass *cl, Object *obj, struct opSet *msg)
  268.   {
  269.    Object *win1;
  270.  
  271.    obj = (Object *)DoSuperNew(cl,obj,
  272.                               MUIA_Application_Title,           "DateText-Demo",
  273.                               MUIA_Application_Author,          "Kai Hofmann",
  274.                               MUIA_Application_Copyright,       "© 1997-1998 Kai Hofmann",
  275.                               MUIA_Application_Version,         "$VER: DateText-Demo 12.0 " __AMIGADATE__,
  276.                               MUIA_Application_Description,     "DateText demonstration program",
  277.                               MUIA_Application_Base,            "DSDEMO",
  278.                               MUIA_Application_SingleTask,      TRUE,
  279.                               MUIA_Application_Active,          TRUE,
  280.                               MUIA_Application_Window,        win1 = NewObject(Win_CC->mcc_Class,NULL,TAG_DONE),
  281.                               TAG_MORE, msg->ops_AttrList
  282.                              );
  283.    if (obj != NULL)
  284.     {
  285.      /*struct App_Data *data = (struct App_Data *)INST_DATA(cl,obj);*/
  286.      /*ULONG result;*/
  287.  
  288.      /*result =*/ DoMethod(win1,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,MUIV_Notify_Application,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  289.      set(win1,MUIA_Window_Open,TRUE);
  290.     }
  291.    return((ULONG)obj);
  292.   }
  293.  
  294.  
  295.  static ULONG SAVEDS_ASM App_Dispatcher(REG(A0) struct IClass *cl, REG(A2) Object *obj, REG(A1) Msg msg)
  296.   {
  297.    switch (msg->MethodID)
  298.     {
  299.      case OM_NEW            : return(App_New(cl,obj,(struct opSet *)msg));
  300.      default                : return(DoSuperMethodA(cl,obj,msg));
  301.     }
  302.   }
  303.  
  304.  /* ------------------------------------------------------------------------ */
  305.  
  306.  static void OpenReq(char *const str)
  307.   {
  308.    struct EasyStruct req =
  309.     {
  310.      sizeof(struct EasyStruct),
  311.      0,
  312.      "DateText-Demo",
  313.      NULL,
  314.      "OK",
  315.     };
  316.  
  317.    req.es_TextFormat = str;
  318.    EasyRequest(NULL,&req,NULL,(ULONG)MUIMASTER_VMIN);
  319.   }
  320.  
  321.  /* ------------------------------------------------------------------------ */
  322.  
  323.  static int muiclasses_Init(void)
  324.   {
  325.    int retstat = RETURN_OK;
  326.  
  327.    App_CC = MUI_CreateCustomClass(NULL,MUIC_Application,NULL,0,App_Dispatcher);
  328.    if (App_CC == NULL)
  329.     {
  330.      OpenReq("Can not create 'App' privat custom class!");
  331.      retstat = RETURN_ERROR;
  332.     }
  333.    else
  334.     {
  335.      Win_CC = MUI_CreateCustomClass(NULL,MUIC_Window,NULL,0,Win_Dispatcher);
  336.      if (Win_CC == NULL)
  337.       {
  338.        OpenReq("Can not create 'Win' privat custom class!");
  339.        retstat = RETURN_ERROR;
  340.       }
  341.      else
  342.       {
  343.        Str_CC = MUI_CreateCustomClass(NULL,MUIC_String,NULL,0,Str_Dispatcher);
  344.        if (Str_CC == NULL)
  345.         {
  346.          OpenReq("Can not create 'Str' privat custom class!");
  347.          retstat = RETURN_ERROR;
  348.         }
  349.       }
  350.     }
  351.  
  352.    return(retstat);
  353.   }
  354.  
  355.  
  356.  static int muiclasses_Cleanup(void)
  357.   {
  358.    int retstat = RETURN_OK;
  359.  
  360.    if (Str_CC != NULL)
  361.     {
  362.      if (!MUI_DeleteCustomClass(Str_CC))
  363.       {
  364.        OpenReq("Can not delete 'Str' privat custom class!");
  365.        retstat = RETURN_ERROR;
  366.       }
  367.     }
  368.    if (Win_CC != NULL)
  369.     {
  370.      if (!MUI_DeleteCustomClass(Win_CC))
  371.       {
  372.        OpenReq("Can not delete 'Win' privat custom class!");
  373.        retstat = RETURN_ERROR;
  374.       }
  375.     }
  376.    if (App_CC != NULL)
  377.     {
  378.      if (!MUI_DeleteCustomClass(App_CC))
  379.       {
  380.        OpenReq("Can not delete 'App' privat custom class!");
  381.        retstat = RETURN_ERROR;
  382.       }
  383.     }
  384.  
  385.    return(retstat);
  386.   }
  387.  
  388.  /* ------------------------------------------------------------------------ */
  389.  
  390.  static int gui_Init(void)
  391.   {
  392.    int retstat;
  393.  
  394.    MUIMasterBase = OpenLibrary((UBYTE *)MUIMASTER_NAME,(unsigned long)MUIMASTER_VMIN);
  395.    if (MUIMasterBase != NULL)
  396.     {
  397.      retstat = muiclasses_Init();
  398.      if (retstat == RETURN_OK)
  399.       {
  400.        Object *ds;
  401.  
  402.        ds = MUI_NewObject(MUIC_DateText,
  403.                           TAG_DONE
  404.                          );
  405.        if (ds != NULL)
  406.         {
  407.          MUI_DisposeObject(ds);
  408.          App = NewObject(App_CC->mcc_Class,NULL,TAG_DONE);
  409.          if (App == NULL)
  410.           {
  411.            /*retstat =*/ muiclasses_Cleanup();
  412.            CloseLibrary(MUIMasterBase);
  413.            OpenReq("Can not create application object!");
  414.            retstat = RETURN_FAIL;
  415.           }
  416.         }
  417.        else
  418.         {
  419.          /*retstat =*/ muiclasses_Cleanup();
  420.          CloseLibrary(MUIMasterBase);
  421.          OpenReq("Missing DateText.mcc!");
  422.          retstat = RETURN_FAIL;
  423.         }
  424.       }
  425.      else
  426.       {
  427.        /*retstat =*/ muiclasses_Cleanup();
  428.        CloseLibrary(MUIMasterBase);
  429.       }
  430.     }
  431.    else
  432.     {
  433.      OpenReq("Can not open muimaster.library V%lu!");
  434.      retstat = RETURN_FAIL;
  435.     }
  436.    return(retstat);
  437.   }
  438.  
  439.  
  440.  static int gui_Cleanup(void)
  441.   {
  442.    int retstat = RETURN_OK;
  443.  
  444.    if (MUIMasterBase != NULL)
  445.     {
  446.      if (App != NULL)
  447.       {
  448.        MUI_DisposeObject(App);
  449.       }
  450.      retstat = muiclasses_Cleanup();
  451.      if (retstat == RETURN_OK)
  452.       {
  453.        CloseLibrary(MUIMasterBase);
  454.       }
  455.      else
  456.       {
  457.        OpenReq("Can not close muimaster.library!");
  458.        retstat = RETURN_FAIL;
  459.       }
  460.     }
  461.    return(retstat);
  462.   }
  463.  
  464.  
  465.  static void gui_MainLoop(void)
  466.   {
  467.    if (App != NULL)
  468.     {
  469.      ULONG signals=0;
  470.  
  471.      while (DoMethod(App,(unsigned long)MUIM_Application_NewInput,&signals) != MUIV_Application_ReturnID_Quit)
  472.       {
  473.        if (signals)
  474.         {
  475.          signals = Wait(signals | SIGBREAKF_CTRL_C);
  476.          if (signals & SIGBREAKF_CTRL_C)
  477.           {
  478.            /*ULONG result;*/
  479.  
  480.            /*result =*/ DoMethod(App,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  481.           }
  482.         }
  483.       }
  484.     }
  485.   }
  486.  
  487.  /* ------------------------------------------------------------------------ */
  488.  
  489.  void main(void)
  490.   {
  491.    BOOL error = FALSE;
  492.  
  493.    DateBase = OpenLibrary(DATE_NAME,33);
  494.    if (DateBase != NULL)
  495.     {
  496.      if (((DateBase->lib_Version > 33)) || ((DateBase->lib_Version == 33) && (DateBase->lib_Revision >= 290)))
  497.       {
  498.        short i;
  499.        char mn[15];
  500.        APTR mempool;
  501.  
  502.        mempool = LibCreatePool(MEMF_PUBLIC,180,15);
  503.        if (mempool != NULL)
  504.         {
  505.          for (i=0;i<12;i++)
  506.           {
  507.            date_MonthText(i+1,mn,date_Locale);
  508.            months[i] = (char *)LibAllocPooled(mempool,(ULONG)strlen(mn)+1);
  509.            if (months[i] != NULL)
  510.             {
  511.              strcpy(months[i],mn);
  512.             }
  513.            else
  514.             {
  515.              OpenReq("Out of memory error!");
  516.              error = TRUE;
  517.              break;
  518.             }
  519.           }
  520.          if (!error)
  521.           {
  522.            months[12] = NULL;
  523.            if (gui_Init() == RETURN_OK)
  524.             {
  525.              gui_MainLoop();
  526.              /*result =*/ gui_Cleanup();
  527.             }
  528.           }
  529.      LibDeletePool(mempool);
  530.         }
  531.       }
  532.      else
  533.       {
  534.        OpenReq("Can not open date.library V33.290");
  535.       }
  536.      CloseLibrary(DateBase);
  537.     }
  538.    else
  539.     {
  540.      OpenReq("Can not open date.library V33");
  541.     }
  542.   }
  543.