home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - DATEWIN.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_win.hpp"
- #include <dos.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- UIW_DATE::UIW_DATE(int left, int top, int width, UI_DATE *_date,
- const char *_range, USHORT _dtFlags, USHORT _woFlags, int (*_validate)(void *object, int ccode)) :
- UIW_STRING(left, top, width, NULL, 64, STF_NO_FLAGS, _woFlags, _validate),
- dtFlags(_dtFlags)
- {
- // Initialize the date information.
- windowID[0] = ID_DATE;
- windowID[1] = ID_STRING;
- search.type = ID_DATE;
-
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- {
- text = new char[64];
- text[0] = '\0';
- }
-
- if (!_date)
- {
- date = new UI_DATE();
- woFlags &= ~WOF_NO_ALLOCATE_DATA;
- }
- else if (FlagSet(_woFlags, WOF_NO_ALLOCATE_DATA))
- date = _date;
- else
- date = new UI_DATE(*_date);
- range = ui_strdup(_range);
- }
-
- UIW_DATE::~UIW_DATE(void)
- {
- if (range)
- delete range;
- if (text && FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- {
- delete text;
- text = NULL;
- }
- }
-
- void UIW_DATE::DataSet(UI_DATE *newDate)
- {
- if (newDate)
- {
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- date = newDate;
- else
- *date = *newDate;
- }
- date->Export(text, fieldWidth, dtFlags);
- UIW_STRING::DataSet(text);
- UI_WINDOW_OBJECT::Redisplay(FALSE);
- }
-
- int UIW_DATE::Event(const UI_EVENT &event)
- {
- // Switch on the event type.
- int needValidate = NeedsValidation();
- int ccode = event.type;
- if (ccode == S_CREATE)
- {
- if (FlagSet(woStatus, WOS_UNANSWERED))
- {
- date->Import(0, 0, 0);
- *text = '\0';
- }
- else
- date->Export(text, fieldWidth, dtFlags);
- }
- else if (ccode == S_CURRENT)
- {
- if (needValidate)
- (*Validate)(this, ccode);
- woStatus &= ~WOS_UNANSWERED;
- if (!FlagSet(woStatus, WOS_CURRENT))
- {
- date->Export(text, fieldWidth, dtFlags & ~(DTF_DAY_OF_WEEK | DTF_SHORT_DAY));
- UIW_STRING::DataSet(text);
- }
- }
- else if (ccode == S_NON_CURRENT)
- {
- int result = date->Import(text, dtFlags);
- if (result == DTI_OK)
- result = DateRangeCheck();
- if (result != DTI_OK)
- {
- DateError(result);
- ccode = S_ERROR;
- }
- else
- {
- if (needValidate && (*Validate)(this, ccode) != 0)
- ccode = S_ERROR;
- else
- {
- date->Export(text, fieldWidth, dtFlags);
- UIW_STRING::DataSet(text);
- }
- }
- }
- else if (ccode == S_SIZE)
- {
- ccode = UIW_STRING::Event(event);
- date->Export(text, fieldWidth, dtFlags);
- return (ccode);
- }
- if (ccode != S_ERROR)
- {
- woStatus &= ~WOS_INVALID;
- ccode = UIW_STRING::Event(event);
- }
- else
- woStatus |= WOS_INVALID;
- return(ccode);
- }
-
- void UIW_DATE::DateError(int errorCode)
- {
- static struct
- {
- int code;
- char *msg;
- } errorTable[] =
- {
- { DTI_INVALID, "The date %s, is in an invalid format." },
- { DTI_AMBIGUOUS, "The date %s, has an ambiguous month name." },
- { DTI_INVALID_NAME, "The date %s, has an invalid month name." },
- { DTI_VALUE_MISSING,"A date value must be entered." },
- { DTI_OK, 0 }
- };
- char formattedString[512];
-
- if (errorCode == DTI_OUT_OF_RANGE)
- DateRangeError(formattedString);
- else
- {
- strcpy(formattedString, "An unknown date error was received.");
- for (int i = 0; errorTable[i].msg; i++)
- if (errorTable[i].code == errorCode)
- {
- sprintf(formattedString, errorTable[i].msg, text);
- break;
- }
- }
- _errorSystem->ReportError(windowManager,
- woFlags & (WOF_NO_UNANSWERED | WOF_NO_INVALID), formattedString);
- }
-
- int UIW_DATE::DateRangeCheck()
- {
- char minValue[20];
- char maxValue[20];
- int errorCode = DTI_OK;
- int rangeLength;
- int offset;
-
- // See if a range exists.
- if (!range || range[0] == '\0')
- return (DTI_OK);
-
- offset = 0;
- rangeLength = (range) ? strlen(range) : 0;
- while (offset < rangeLength)
- {
- offset = ui_parse_range(range, offset, minValue, maxValue);
- UI_DATE minDate(0, 0, 0);
- UI_DATE maxDate(0, 0, 0);
- int minValid = minDate.Import(minValue);
- int maxValid = maxDate.Import(maxValue);
- if (minValid != DTI_OK || maxValid != DTI_OK ||
- *date < minDate || *date > maxDate)
- errorCode = DTI_OUT_OF_RANGE;
- else
- {
- errorCode = DTI_OK;
- break;
- }
- }
- return errorCode;
- }
-
- void UIW_DATE::DateRangeError(char *fmtStr)
- {
- char fmtMin[40];
- char fmtMax[40];
- char tempStr[80];
- static char thruStr[] = "through";
- static char orStr[] = "or";
- int position;
- int offset = 0;
- int count = 0;
- char sep = ','; // Separator character.
- char badText[64];
- int rangeLength = (range) ? strlen(range) : 0;
-
- date->Export(badText, sizeof(badText), dtFlags);
- sprintf(fmtStr,
- "The date %s is not valid. The date must be in the range ", badText);
- while (offset < rangeLength)
- {
- /* Format the current minimum and maximum values */
- if (count == 1)
- {
- if (!strcmp(fmtMin, fmtMax))
- strcpy(tempStr, fmtMin);
- else
- sprintf(tempStr, "%s %s %s", fmtMin, thruStr, fmtMax);
- strcat(fmtStr, tempStr);
- }
- else if (count != 0)
- {
- if (!strcmp(fmtMin, fmtMax))
- sprintf(tempStr, "%c %s", sep, fmtMin);
- else
- sprintf(tempStr, "%c %s %s %s", sep, fmtMin, thruStr, fmtMax);
- strcat(fmtStr, tempStr);
- }
-
- /* Get a new set of minimum and maximum values */
- count++;
- offset = ui_parse_range(range, offset, fmtMin, fmtMax);
- }
-
- /* Append the final minimum and maximum values on the string */
- if (count > 2 && !strcmp(fmtMin, fmtMax))
- sprintf(tempStr, "%c %s %s.", sep, orStr, fmtMin);
- else if (count > 2)
- sprintf(tempStr, "%c %s %s %s %s.", sep, orStr, fmtMin, thruStr, fmtMax);
- else if (count == 2 && !strcmp(fmtMin, fmtMax))
- sprintf(tempStr, " %s %s.", orStr, fmtMin);
- else if (count == 2)
- sprintf(tempStr, " %s %s %s %s.", orStr, fmtMin, thruStr, fmtMax);
- else if (count > 0)
- sprintf(tempStr, "%s %s %s.", fmtMin, thruStr, fmtMax);
- else
- {
- position = 0;
- while (fmtStr[position] != '\0' && fmtStr[position] != '.')
- position++;
- fmtStr[++position] = '\0';
- }
- strcat(fmtStr, tempStr);
- }
-
- #ifdef ZIL_LOAD
- UIW_DATE::UIW_DATE(const char *name, UI_STORAGE *file, USHORT loadFlags) :
- UIW_STRING(name, file, loadFlags | L_SUB_LEVEL)
- {
- windowID[0] = ID_DATE;
- windowID[1] = ID_STRING;
-
- if (!file)
- file = _storage;
- file->Load(&dtFlags);
- file->Load(&range);
- date = new UI_DATE(text, dtFlags);
- if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
- delete file;
- }
- #endif
-
- #ifdef ZIL_STORE
- void UIW_DATE::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
- {
- UIW_STRING::Store(name, file, storeFlags | S_SUB_LEVEL);
- file->Store(dtFlags);
- file->Store(range);
- if (!FlagSet(storeFlags, S_SUB_LEVEL))
- file->ObjectSize(name, search);
- }
- #endif
-