home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - STRING.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_win.hpp"
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
-
- UIW_STRING::UIW_STRING(int left, int top, int width, char *_text,
- short _maxLength, USHORT _stFlags, USHORT woFlags,
- int (*_validateFunction)(void *object, int ccode)) :
- UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS),
- maxLength(_maxLength)
- {
- windowID[0] = ID_STRING;
- search.type = ID_STRING;
- stFlags = _stFlags;
-
- // Match with the appropriate Windows 3.0 flags.
- MSWindowsStyle |= ES_AUTOHSCROLL;
-
- if (FlagSet(woFlags, WOF_BORDER) && !FlagSet(woFlags, WOF_NON_FIELD_REGION))
- MSWindowsStyle |= WS_BORDER;
- if (FlagSet(woFlags, WOF_JUSTIFY_CENTER))
- MSWindowsStyle |= ES_CENTER;
- if (FlagSet(woFlags, WOF_JUSTIFY_RIGHT))
- MSWindowsStyle |= ES_RIGHT;
-
- if (FlagSet(stFlags, STF_LOWER_CASE))
- MSWindowsStyle |= ES_LOWERCASE;
- if (FlagSet(stFlags, STF_UPPER_CASE))
- MSWindowsStyle |= ES_UPPERCASE;
- if (FlagSet(stFlags, STF_PASSWORD))
- MSWindowsStyle |= ES_PASSWORD;
-
- Validate = _validateFunction;
- if (maxLength < 0)
- maxLength = ui_strlen(_text) + 1;
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- text = _text;
- else
- {
- text = new char[maxLength];
- if (_text)
- {
- int srcLen = ui_strlen(_text) + 1;
- memcpy(text, _text, Min(srcLen, maxLength));
- }
- else
- text[0] = '\0';
- }
- insertMode = TRUE;
- fieldWidth = _maxLength;
- }
-
- UIW_STRING::~UIW_STRING(void)
- {
- if (!FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- delete text;
- }
-
- // Member functions ---------------------------------------------------------
-
- int UIW_STRING::Event(const UI_EVENT &event)
- {
- int ccode = UI_WINDOW_OBJECT::LogicalEvent(event, ID_STRING);
- int needValidate = NeedsValidation();
-
- // Switch on the event type.
- switch (ccode)
- {
- case S_CREATE:
- case S_SIZE:
- ccode = UI_WINDOW_OBJECT::Event(event);
- true.bottom += 1;
- true.right += 1;
- if (FlagSet(woFlags, WOF_BORDER))
- true.bottom -= 2 * HIWORD(GetDialogBaseUnits()) / 8;
-
- UI_WINDOW_OBJECT *root = parent;
- while (root->parent && !root->hWnd)
- root = root->parent;
- if (!root->hWnd)
- break;
- POINT client = { 0, 0 };
- ClientToScreen(root->hWnd, &client);
-
- if (!hWnd)
- {
- hWnd = CreateWindow("Edit", text,
- WS_CHILD | WS_VISIBLE | MSWindowsStyle,
- true.left - client.x, true.top - client.y,
- true.right - true.left, true.bottom - true.top,
- root->hWnd, 1, ((UI_MSWINDOWS_DISPLAY *)display)->hInstance, NULL);
- screenID = hWnd;
-
- SendMessage(hWnd, EM_LIMITTEXT, maxLength, 0L);
- }
- else
- {
- RECT windowRect;
- GetWindowRect(hWnd, &windowRect);
- if (true.left != windowRect.left || true.top != windowRect.top ||
- true.right != windowRect.right || true.bottom != windowRect.bottom)
- {
- MoveWindow(hWnd, true.left - client.x, true.top - client.y,
- true.right - true.left, true.bottom - true.top, TRUE);
- SendMessage(hWnd, WM_NCPAINT, 0, 0x0L);
- }
- }
-
- if (FlagSet(woAdvancedStatus, WOAS_TOO_SMALL))
- {
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- SendMessage(hWnd, WM_ENABLE, 0, 0);
- }
- else
- SendMessage(hWnd, WM_ENABLE, 1, 0);
- break;
-
- case S_ERROR_RESPONSE:
- woStatus |= event.rawCode;
- if (FlagSet(woStatus, WOS_UNANSWERED))
- {
- SetWindowText(hWnd, "");
- GetWindowText(hWnd, text, maxLength);
- }
- break;
-
- case S_CURRENT:
- if (needValidate)
- (*Validate)(this, ccode);
- if (!FlagSet(woFlags, WOF_VIEW_ONLY))
- {
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- SendMessage(hWnd, WM_SETFOCUS, parent->hWnd, 0L);
- if (FlagSet(woFlags, WOF_AUTO_CLEAR))
- SendMessage(hWnd, EM_SETSEL, 0, 0x7FFF0000L);
- }
-
- // Clear the WOS_UNANSWERED and WOS_NO_AUTO_CLEAR bits.
- woStatus &= ~(WOS_UNANSWERED | WOS_NO_AUTO_CLEAR);
- break;
-
- case S_DISPLAY_ACTIVE:
- if (FlagSet(woStatus, WOS_CURRENT) && !FlagSet(woFlags, WOF_VIEW_ONLY))
- {
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- SendMessage(hWnd, WM_SETFOCUS, parent->hWnd, 0L);
- }
- break;
-
- case S_NON_CURRENT:
- if (needValidate && (*Validate)(this, ccode) != 0 )
- {
- ccode = S_ERROR;
- woStatus |= WOS_INVALID;
- }
- else
- woStatus &= ~WOS_INVALID;
- // Continue on to S_DISPLAY_INACTIVE.
-
- case S_DISPLAY_INACTIVE:
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- break;
-
- case L_MARK:
- if (SendMessage(hWnd, EM_GETSEL, 0, 0L))
- SendMessage(hWnd, EM_SETSEL, 0, 0L);
- else
- SendMessage(hWnd, EM_SETSEL, 0, 0x7FFF0000L);
- break;
-
- case L_BEGIN_MARK:
- SendMessage(hWnd, WM_LBUTTONDOWN, 0, event.position.column -
- true.left + ((DWORD)(event.position.line - true.top) << 16));
- break;
-
- case L_CONTINUE_MARK:
- SendMessage(hWnd, WM_MOUSEMOVE, 0, event.position.column -
- true.left + ((DWORD)(event.position.line - true.top) << 16));
- break;
-
- case L_END_MARK:
- SendMessage(hWnd, WM_RBUTTONDOWN, 0, event.position.column -
- true.left + ((DWORD)(event.position.line - true.top) << 16));
- break;
-
- case L_INSERT_TOGGLE:
- insertMode = !insertMode;
- // Continue on to E_KEY.
-
- case E_KEY:
- case L_DELETE:
- case L_DELETE_EOL:
- case L_DELETE_WORD:
- if (FlagSet(woFlags, WOF_VIEW_ONLY))
- break;
-
- case L_LEFT:
- case L_RIGHT:
- case L_BOL:
- case L_EOL:
- case L_WORD_LEFT:
- case L_WORD_RIGHT:
- if (event.key.value != VK_TAB && event.key.value != VK_RETURN)
- {
- if (event.rawCode)
- SendMessage(hWnd, WM_KEYDOWN, event.key.value, 0L);
- else
- SendMessage(hWnd, WM_CHAR, event.key.value, 0L);
- GetWindowText(hWnd, text, maxLength);
- }
- break;
-
- default:
- ccode = UI_WINDOW_OBJECT::Event(event);
- break;
- }
- return (ccode);
- }
-
- #pragma argsused
- void UIW_STRING::DataSet(char *newText, short _maxLength)
- {
- if (newText)
- {
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- text = newText;
- else
- {
- if (_maxLength != -1 && _maxLength > maxLength)
- {
- delete text;
- text = new char[_maxLength];
- }
- int srcLen = ui_strlen(newText) + 1;
- maxLength = (_maxLength == -1) ? maxLength : _maxLength;
- memcpy(text, newText, Min(srcLen, maxLength));
- }
-
- if (hWnd)
- {
- SetWindowText(hWnd, text);
- SendMessage(hWnd, EM_LIMITTEXT, maxLength, 0L);
- }
- }
- UI_WINDOW_OBJECT::Redisplay(FALSE);
- }
-
- #ifdef ZIL_LOAD
- UIW_STRING::UIW_STRING(const char *name, UI_STORAGE *file, USHORT loadFlags) :
- UI_WINDOW_OBJECT(name, file, loadFlags | L_SUB_LEVEL)
- {
- windowID[0] = ID_STRING;
-
- if (!file)
- file = _storage;
- file->Load(&stFlags);
- file->Load(&maxLength);
- text = new char[maxLength];
- short size;
- file->Load(&size);
- if (size)
- file->Load(text, size);
- text[size] = '\0';
- insertMode = TRUE;
- if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
- delete file;
- }
- #endif
-
- #ifdef ZIL_STORE
- void UIW_STRING::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
- {
- UI_WINDOW_OBJECT::Store(name, file, storeFlags | S_SUB_LEVEL);
- file->Store(stFlags);
- file->Store(maxLength);
- file->Store(text);
- if (!FlagSet(storeFlags, S_SUB_LEVEL))
- file->ObjectSize(name, search);
- }
- #endif
-