home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - TIMEWIN.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_TIME::UIW_TIME(int left, int top, int width, UI_TIME *_time,
- const char *_range, USHORT _tmFlags, USHORT _woFlags,
- int (*_validate)(void *object, int ccode)) :
- UIW_STRING(left, top, width, NULL, 64, STF_NO_FLAGS, _woFlags, _validate),
- tmFlags(_tmFlags)
- {
- windowID[0] = ID_TIME;
- windowID[1] = ID_STRING;
- search.type = ID_TIME;
-
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- {
- text = new char[64];
- text[0] = '\0';
- }
-
- if (!_time)
- {
- time = new UI_TIME();
- woFlags &= ~WOF_NO_ALLOCATE_DATA;
- }
- else if (FlagSet(_woFlags, WOF_NO_ALLOCATE_DATA))
- time = _time;
- else
- time = new UI_TIME(*_time);
- range = ui_strdup(_range);
- }
-
- void UIW_TIME::DataSet(UI_TIME *newTime)
- {
- if (newTime)
- {
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- time = newTime;
- else
- *time = *newTime;
- }
- time->Export(text, tmFlags);
- UIW_STRING::DataSet(text);
- UI_WINDOW_OBJECT::Redisplay(FALSE);
- }
-
- UIW_TIME::~UIW_TIME(void)
- {
- if (range)
- delete range;
- if (text && FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- {
- delete text;
- text = NULL;
- }
- }
-
- int UIW_TIME::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))
- {
- time->Import(0, 0, 0, 0);
- *text = '\0';
- }
- else
- time->Export(text, tmFlags);
- }
- else if (ccode == S_CURRENT)
- {
- if (needValidate)
- (*Validate)(this, ccode);
- woStatus &= ~WOS_UNANSWERED;
- }
- else if (ccode == S_NON_CURRENT)
- {
- int result = time->Import(text, tmFlags);
- if (result == TMI_OK)
- result = TimeRangeCheck();
- if (result != TMI_OK)
- {
- TimeError(result);
- ccode = S_ERROR;
- }
- else
- {
- if (needValidate && (*Validate)(this, ccode) != 0)
- ccode = S_ERROR;
- else
- {
- if (*text == '\0' && !FlagSet(tmFlags, TMF_SYSTEM))
- woStatus |= WOS_UNANSWERED; // Leave unanswered.
- time->Export(text, tmFlags);
- UIW_STRING::DataSet(text);
- if (FlagSet(woStatus, WOS_UNANSWERED))
- *text = '\0';
- }
- }
- }
- if (ccode != S_ERROR)
- {
- woStatus &= ~WOS_INVALID;
- ccode = UIW_STRING::Event(event);
- }
- else
- woStatus |= WOS_INVALID;
- return(ccode);
- }
-
- void UIW_TIME::TimeError(int errorCode)
- {
- static struct
- {
- int code;
- char *msg;
- } errorTable[] =
- {
- { TMI_INVALID, "The time %s, is in an invalid format." },
- { TMI_VALUE_MISSING, "A time value must be entered." },
- { TMI_OK, 0 }
- };
- char formattedString[512];
-
- if (errorCode == TMI_OUT_OF_RANGE)
- TimeRangeError(formattedString);
- else
- {
- strcpy(formattedString, "An unknown time 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_TIME::TimeRangeCheck()
- {
- char minValue[20];
- char maxValue[20];
- int errorCode = TMI_OK;
-
- /* See if a range exists */
- if (!range || range[0] == '\0')
- return (TMI_OK);
-
- int offset = 0;
- int rangeLength = (range) ? strlen(range) : 0;
- while (offset < rangeLength)
- {
- offset = ui_parse_range(range, offset, minValue, maxValue);
- UI_TIME minTime;
- UI_TIME maxTime;
- int minValid = minTime.Import(minValue, tmFlags);
- int maxValid = maxTime.Import(maxValue, tmFlags);
- if (minValid != TMI_OK || maxValid != TMI_OK ||
- *time < minTime || *time > maxTime)
- errorCode = TMI_OUT_OF_RANGE;
- else
- {
- errorCode = TMI_OK;
- break;
- }
- }
- return (errorCode);
- }
-
- void UIW_TIME::TimeRangeError(char *formattedString)
- {
- char formattedMin[40];
- char formattedMax[40];
- char tempString[80];
- static char throughString[] = "through";
- static char orString[] = "or";
- int position;
- int offset = 0;
- int count = 0;
- char badText[64];
-
- /* Set up the initial variables */
- int rangeLength = (range) ? strlen(range) : 0;
- char separator = ',';
-
- time->Export(badText, tmFlags);
- sprintf(formattedString,
- "%s is not valid. The time must be in the range ", badText);
- while (offset < rangeLength)
- {
- // Format the current minimum and maximum values.
- if (count == 1)
- {
- if (!strcmp(formattedMin, formattedMax))
- strcpy(tempString, formattedMin);
- else
- sprintf(tempString, "%s %s %s", formattedMin, throughString,
- formattedMax);
- strcat(formattedString, tempString);
- }
- else if (count != 0)
- {
- if (!strcmp(formattedMin, formattedMax))
- sprintf(tempString, "%c %s", separator, formattedMin);
- else
- sprintf(tempString, "%c %s %s %s", separator,
- formattedMin, throughString, formattedMax);
- strcat(formattedString, tempString);
- }
-
- // Get a new set of minimum and maximum values.
- count++;
- offset = ui_parse_range(range, offset, formattedMin, formattedMax);
- }
-
- // Append the final minimum and maximum values on the string.
- if (count > 2 && !strcmp(formattedMin, formattedMax))
- sprintf(tempString, "%c %s %s.", separator, orString, formattedMin);
- else if (count > 2)
- sprintf(tempString, "%c %s %s %s %s.", separator, orString,
- formattedMin, throughString, formattedMax);
- else if (count == 2 && !strcmp(formattedMin, formattedMax))
- sprintf(tempString, " %s %s.", orString, formattedMin);
- else if (count == 2)
- sprintf(tempString, " %s %s %s %s.", orString, formattedMin,
- throughString, formattedMax);
- else if (count > 0)
- sprintf(tempString, "%s %s %s.", formattedMin, throughString,
- formattedMax);
- else
- {
- position = 0;
- while (formattedString[position] != '\0' &&
- formattedString[position] != '.')
- position++;
- formattedString[++position] = '\0';
- }
- strcat(formattedString, tempString);
- }
-
- #ifdef ZIL_LOAD
- UIW_TIME::UIW_TIME(const char *name, UI_STORAGE *file, USHORT loadFlags) :
- UIW_STRING(name, file, loadFlags | L_SUB_LEVEL)
- {
- windowID[0] = ID_TIME;
- windowID[1] = ID_STRING;
-
- if (!file)
- file = _storage;
- file->Load(&tmFlags);
- file->Load(&range);
- time = new UI_TIME(text, tmFlags);
- if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
- delete file;
- }
- #endif
-
- #ifdef ZIL_STORE
- void UIW_TIME::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
- {
- UIW_STRING::Store(name, file, storeFlags | S_SUB_LEVEL);
- file->Store(tmFlags);
- file->Store(range);
- if (!FlagSet(storeFlags, S_SUB_LEVEL))
- file->ObjectSize(name, search);
- }
- #endif
-