home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_5.ZIP / WINSRC.ZIP / TIMEWIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  6.7 KB  |  279 lines

  1. //    Zinc Interface Library - TIMEWIN.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_win.hpp"
  6. #include <dos.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. UIW_TIME::UIW_TIME(int left, int top, int width, UI_TIME *_time,
  12.     const char *_range, USHORT _tmFlags, USHORT _woFlags,
  13.     int (*_validate)(void *object, int ccode)) :
  14.     UIW_STRING(left, top, width, NULL, 64, STF_NO_FLAGS, _woFlags, _validate),
  15.     tmFlags(_tmFlags)
  16. {
  17.     windowID[0] = ID_TIME;
  18.     windowID[1] = ID_STRING;
  19.     search.type = ID_TIME;
  20.  
  21.     if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  22.     {
  23.         text = new char[64];
  24.         text[0] = '\0';
  25.     }
  26.  
  27.     if (!_time)
  28.     {
  29.         time = new UI_TIME();
  30.         woFlags &= ~WOF_NO_ALLOCATE_DATA;
  31.     }
  32.     else if (FlagSet(_woFlags, WOF_NO_ALLOCATE_DATA))
  33.         time = _time;
  34.     else 
  35.         time = new UI_TIME(*_time);
  36.     range = ui_strdup(_range);
  37. }
  38.  
  39. void UIW_TIME::DataSet(UI_TIME *newTime)
  40. {
  41.     if (newTime)
  42.     {
  43.         if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  44.             time = newTime;
  45.         else
  46.             *time = *newTime;
  47.     }
  48.     time->Export(text, tmFlags);
  49.     UIW_STRING::DataSet(text);
  50.     UI_WINDOW_OBJECT::Redisplay(FALSE);
  51. }
  52.  
  53. UIW_TIME::~UIW_TIME(void)
  54. {
  55.     if (range)
  56.         delete range;
  57.     if (text && FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  58.     {
  59.         delete text;
  60.         text = NULL;
  61.     }
  62. }
  63.  
  64. int UIW_TIME::Event(const UI_EVENT &event)
  65. {
  66.     // Switch on the event type.
  67.     int needValidate = NeedsValidation();
  68.     int ccode = event.type;
  69.     if (ccode == S_CREATE)
  70.     {
  71.         if (FlagSet(woStatus, WOS_UNANSWERED))
  72.         {
  73.             time->Import(0, 0, 0, 0);
  74.             *text = '\0';
  75.         }
  76.         else
  77.             time->Export(text, tmFlags);
  78.     }
  79.     else if (ccode == S_CURRENT)
  80.     {
  81.         if (needValidate)
  82.             (*Validate)(this, ccode);
  83.         woStatus &= ~WOS_UNANSWERED;
  84.     }
  85.     else if (ccode == S_NON_CURRENT)
  86.     {
  87.         int result = time->Import(text, tmFlags);
  88.         if (result == TMI_OK)
  89.             result = TimeRangeCheck();
  90.         if (result != TMI_OK)
  91.         {
  92.             TimeError(result);
  93.             ccode = S_ERROR;
  94.         }
  95.         else
  96.         {
  97.             if (needValidate && (*Validate)(this, ccode) != 0)
  98.                 ccode = S_ERROR;
  99.             else
  100.             {
  101.                 if (*text == '\0' && !FlagSet(tmFlags, TMF_SYSTEM))
  102.                     woStatus |= WOS_UNANSWERED;        // Leave unanswered.
  103.                 time->Export(text, tmFlags);
  104.                 UIW_STRING::DataSet(text);
  105.                 if (FlagSet(woStatus, WOS_UNANSWERED))
  106.                     *text = '\0';
  107.             }
  108.         }
  109.     }
  110.     if (ccode != S_ERROR)
  111.     {
  112.         woStatus &= ~WOS_INVALID;
  113.         ccode = UIW_STRING::Event(event);
  114.     }
  115.     else
  116.         woStatus |= WOS_INVALID;
  117.     return(ccode);
  118. }
  119.  
  120. void UIW_TIME::TimeError(int errorCode)
  121. {
  122.     static struct
  123.     {
  124.         int code;
  125.         char *msg;
  126.     } errorTable[] =
  127.     {
  128.         { TMI_INVALID,            "The time %s, is in an invalid format."        },
  129.         { TMI_VALUE_MISSING,    "A time value must be entered."                },
  130.         { TMI_OK,                0                                            }
  131.     };
  132.     char formattedString[512];
  133.  
  134.     if (errorCode == TMI_OUT_OF_RANGE)
  135.         TimeRangeError(formattedString);
  136.     else
  137.     {
  138.         strcpy(formattedString, "An unknown time error was received.");
  139.         for (int i = 0; errorTable[i].msg; i++)
  140.             if (errorTable[i].code == errorCode)
  141.             {
  142.                 sprintf(formattedString, errorTable[i].msg, text);
  143.                 break;
  144.             }
  145.     }
  146.     _errorSystem->ReportError(windowManager,
  147.         woFlags & (WOF_NO_UNANSWERED | WOF_NO_INVALID), formattedString);
  148. }
  149.  
  150. int UIW_TIME::TimeRangeCheck()
  151. {
  152.     char minValue[20];
  153.     char maxValue[20];
  154.     int errorCode = TMI_OK;
  155.  
  156.     /* See if a range exists */
  157.     if (!range || range[0] == '\0')
  158.         return (TMI_OK);
  159.  
  160.     int offset = 0;
  161.     int rangeLength = (range) ? strlen(range) : 0;
  162.     while (offset < rangeLength)
  163.     {
  164.         offset = ui_parse_range(range, offset, minValue, maxValue);
  165.         UI_TIME minTime;
  166.         UI_TIME maxTime;
  167.         int minValid = minTime.Import(minValue, tmFlags);
  168.         int maxValid = maxTime.Import(maxValue, tmFlags);
  169.         if (minValid != TMI_OK || maxValid != TMI_OK ||
  170.             *time < minTime || *time > maxTime)
  171.             errorCode = TMI_OUT_OF_RANGE;
  172.         else
  173.         {
  174.             errorCode = TMI_OK;
  175.             break;
  176.         }
  177.     }
  178.     return (errorCode);
  179. }
  180.  
  181. void UIW_TIME::TimeRangeError(char *formattedString)
  182. {
  183.     char formattedMin[40];
  184.     char formattedMax[40];
  185.     char tempString[80];
  186.     static char throughString[] = "through";
  187.     static char orString[] = "or";
  188.     int position;
  189.     int offset = 0;
  190.     int count = 0;
  191.     char badText[64];
  192.  
  193.     /* Set up the initial variables */
  194.     int rangeLength = (range) ? strlen(range) : 0;
  195.     char separator = ',';
  196.  
  197.     time->Export(badText, tmFlags);
  198.     sprintf(formattedString,
  199.         "%s is not valid.  The time must be in the range ", badText);
  200.     while (offset < rangeLength)
  201.     {
  202.         // Format the current minimum and maximum values.
  203.         if (count == 1)
  204.         {
  205.             if (!strcmp(formattedMin, formattedMax))
  206.                 strcpy(tempString, formattedMin);
  207.             else
  208.                 sprintf(tempString, "%s %s %s", formattedMin, throughString,
  209.                 formattedMax);
  210.             strcat(formattedString, tempString);
  211.         }
  212.         else if (count != 0)
  213.         {
  214.             if (!strcmp(formattedMin, formattedMax))
  215.                 sprintf(tempString, "%c %s", separator, formattedMin);
  216.             else
  217.                 sprintf(tempString, "%c %s %s %s", separator,
  218.                     formattedMin, throughString, formattedMax);
  219.             strcat(formattedString, tempString);
  220.         }
  221.  
  222.         // Get a new set of minimum and maximum values.
  223.         count++;
  224.         offset = ui_parse_range(range, offset, formattedMin, formattedMax);
  225.     }
  226.  
  227.     // Append the final minimum and maximum values on the string.
  228.     if (count > 2 && !strcmp(formattedMin, formattedMax))
  229.         sprintf(tempString, "%c %s %s.", separator, orString, formattedMin);
  230.     else if (count > 2)
  231.         sprintf(tempString, "%c %s %s %s %s.", separator, orString,
  232.             formattedMin, throughString, formattedMax);
  233.     else if (count == 2 && !strcmp(formattedMin, formattedMax))
  234.         sprintf(tempString, " %s %s.", orString, formattedMin);
  235.     else if (count == 2)
  236.         sprintf(tempString, " %s %s %s %s.", orString, formattedMin,
  237.             throughString, formattedMax);
  238.     else if (count > 0)
  239.         sprintf(tempString, "%s %s %s.", formattedMin, throughString,
  240.             formattedMax);
  241.     else
  242.     {
  243.         position = 0;        
  244.         while (formattedString[position] != '\0' &&
  245.             formattedString[position] != '.')
  246.             position++;
  247.         formattedString[++position] = '\0';
  248.     }
  249.     strcat(formattedString, tempString);
  250. }
  251.  
  252. #ifdef ZIL_LOAD
  253. UIW_TIME::UIW_TIME(const char *name, UI_STORAGE *file, USHORT loadFlags) :
  254.     UIW_STRING(name, file, loadFlags | L_SUB_LEVEL)
  255. {
  256.     windowID[0] = ID_TIME;
  257.     windowID[1] = ID_STRING;
  258.  
  259.     if (!file)
  260.         file = _storage;
  261.     file->Load(&tmFlags);
  262.     file->Load(&range);
  263.     time = new UI_TIME(text, tmFlags);
  264.     if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
  265.         delete file;
  266. }
  267. #endif
  268.  
  269. #ifdef ZIL_STORE
  270. void UIW_TIME::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
  271. {
  272.     UIW_STRING::Store(name, file, storeFlags | S_SUB_LEVEL);
  273.     file->Store(tmFlags);
  274.     file->Store(range);
  275.     if (!FlagSet(storeFlags, S_SUB_LEVEL))
  276.         file->ObjectSize(name, search);
  277. }
  278. #endif
  279.