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 / STRING.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  7.1 KB  |  285 lines

  1. //    Zinc Interface Library - STRING.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 <ctype.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. UIW_STRING::UIW_STRING(int left, int top, int width, char *_text,
  11.     short _maxLength, USHORT _stFlags, USHORT woFlags,
  12.     int (*_validateFunction)(void *object, int ccode)) :
  13.     UI_WINDOW_OBJECT(left, top, width, 1, woFlags, WOAF_NO_FLAGS),
  14.     maxLength(_maxLength)
  15. {
  16.     windowID[0] = ID_STRING;
  17.     search.type = ID_STRING;
  18.     stFlags = _stFlags;
  19.  
  20.     // Match with the appropriate Windows 3.0 flags.
  21.     MSWindowsStyle |= ES_AUTOHSCROLL;
  22.  
  23.     if (FlagSet(woFlags, WOF_BORDER) && !FlagSet(woFlags, WOF_NON_FIELD_REGION))
  24.         MSWindowsStyle |= WS_BORDER;
  25.     if (FlagSet(woFlags, WOF_JUSTIFY_CENTER))
  26.         MSWindowsStyle |= ES_CENTER;
  27.     if (FlagSet(woFlags, WOF_JUSTIFY_RIGHT))
  28.         MSWindowsStyle |= ES_RIGHT;
  29.  
  30.     if (FlagSet(stFlags, STF_LOWER_CASE))
  31.         MSWindowsStyle |= ES_LOWERCASE;
  32.     if (FlagSet(stFlags, STF_UPPER_CASE))
  33.         MSWindowsStyle |= ES_UPPERCASE;
  34.     if (FlagSet(stFlags, STF_PASSWORD))
  35.         MSWindowsStyle |= ES_PASSWORD;
  36.  
  37.     Validate = _validateFunction;
  38.         if (maxLength < 0)
  39.             maxLength = ui_strlen(_text) + 1;
  40.     if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  41.         text = _text;
  42.     else
  43.     {
  44.         text = new char[maxLength];
  45.         if (_text)
  46.         {
  47.             int srcLen = ui_strlen(_text) + 1;
  48.             memcpy(text, _text, Min(srcLen, maxLength));
  49.         }
  50.         else
  51.             text[0] = '\0';
  52.     }
  53.     insertMode = TRUE;
  54.     fieldWidth = _maxLength;
  55. }
  56.  
  57. UIW_STRING::~UIW_STRING(void)
  58. {
  59.     if (!FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  60.         delete text;
  61. }
  62.  
  63. // Member functions ---------------------------------------------------------
  64.  
  65. int UIW_STRING::Event(const UI_EVENT &event)
  66. {
  67.     int ccode = UI_WINDOW_OBJECT::LogicalEvent(event, ID_STRING);
  68.     int needValidate = NeedsValidation();
  69.  
  70.     // Switch on the event type.
  71.     switch (ccode)
  72.     {
  73.     case S_CREATE:
  74.     case S_SIZE:
  75.         ccode = UI_WINDOW_OBJECT::Event(event);
  76.         true.bottom += 1;
  77.         true.right += 1;
  78.         if (FlagSet(woFlags, WOF_BORDER))
  79.             true.bottom -= 2 * HIWORD(GetDialogBaseUnits()) / 8;
  80.  
  81.         UI_WINDOW_OBJECT *root = parent;
  82.         while (root->parent && !root->hWnd)
  83.             root = root->parent;
  84.         if (!root->hWnd)
  85.             break;
  86.         POINT client = { 0, 0 };
  87.         ClientToScreen(root->hWnd, &client);
  88.  
  89.         if (!hWnd)
  90.         {
  91.             hWnd = CreateWindow("Edit", text,
  92.                 WS_CHILD | WS_VISIBLE | MSWindowsStyle,
  93.                 true.left - client.x, true.top - client.y,
  94.                 true.right - true.left, true.bottom - true.top,
  95.                 root->hWnd, 1, ((UI_MSWINDOWS_DISPLAY *)display)->hInstance, NULL);
  96.             screenID = hWnd;
  97.  
  98.             SendMessage(hWnd, EM_LIMITTEXT, maxLength, 0L);
  99.         }
  100.         else
  101.         {
  102.             RECT windowRect;
  103.             GetWindowRect(hWnd, &windowRect);
  104.             if (true.left != windowRect.left || true.top != windowRect.top ||
  105.                 true.right != windowRect.right || true.bottom != windowRect.bottom)
  106.             {
  107.                 MoveWindow(hWnd, true.left - client.x, true.top - client.y,
  108.                     true.right - true.left, true.bottom - true.top, TRUE);
  109.                 SendMessage(hWnd, WM_NCPAINT, 0, 0x0L);
  110.             }
  111.         }
  112.  
  113.         if (FlagSet(woAdvancedStatus, WOAS_TOO_SMALL))
  114.         {
  115.             SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
  116.             SendMessage(hWnd, WM_ENABLE, 0, 0);
  117.         }
  118.         else
  119.             SendMessage(hWnd, WM_ENABLE, 1, 0);
  120.         break;
  121.  
  122.     case S_ERROR_RESPONSE:
  123.         woStatus |= event.rawCode;
  124.         if (FlagSet(woStatus, WOS_UNANSWERED))
  125.         {
  126.             SetWindowText(hWnd, "");
  127.             GetWindowText(hWnd, text, maxLength);
  128.         }
  129.         break;
  130.  
  131.     case S_CURRENT:
  132.         if (needValidate)
  133.             (*Validate)(this, ccode);
  134.         if (!FlagSet(woFlags, WOF_VIEW_ONLY))
  135.         {
  136.             SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
  137.             SendMessage(hWnd, WM_SETFOCUS, parent->hWnd, 0L);
  138.             if (FlagSet(woFlags, WOF_AUTO_CLEAR))
  139.                 SendMessage(hWnd, EM_SETSEL, 0, 0x7FFF0000L);
  140.         }
  141.  
  142.         // Clear the WOS_UNANSWERED and WOS_NO_AUTO_CLEAR bits.
  143.         woStatus &= ~(WOS_UNANSWERED | WOS_NO_AUTO_CLEAR);
  144.         break;
  145.  
  146.     case S_DISPLAY_ACTIVE:
  147.         if (FlagSet(woStatus, WOS_CURRENT) && !FlagSet(woFlags, WOF_VIEW_ONLY))
  148.         {
  149.             SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
  150.             SendMessage(hWnd, WM_SETFOCUS, parent->hWnd, 0L);
  151.         }
  152.         break;
  153.  
  154.     case S_NON_CURRENT:
  155.         if (needValidate && (*Validate)(this, ccode) != 0 )
  156.         {
  157.             ccode = S_ERROR;
  158.             woStatus |= WOS_INVALID;
  159.         }
  160.         else
  161.             woStatus &= ~WOS_INVALID;
  162.         // Continue on to S_DISPLAY_INACTIVE.
  163.  
  164.     case S_DISPLAY_INACTIVE:
  165.         SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
  166.         break;
  167.  
  168.     case L_MARK:
  169.         if (SendMessage(hWnd, EM_GETSEL, 0, 0L))
  170.             SendMessage(hWnd, EM_SETSEL, 0, 0L);
  171.         else
  172.             SendMessage(hWnd, EM_SETSEL, 0, 0x7FFF0000L);
  173.         break;
  174.  
  175.     case L_BEGIN_MARK:
  176.         SendMessage(hWnd, WM_LBUTTONDOWN, 0, event.position.column -
  177.             true.left + ((DWORD)(event.position.line - true.top) << 16));
  178.         break;
  179.  
  180.     case L_CONTINUE_MARK:
  181.         SendMessage(hWnd, WM_MOUSEMOVE, 0, event.position.column -
  182.             true.left + ((DWORD)(event.position.line - true.top) << 16));
  183.         break;
  184.  
  185.     case L_END_MARK:
  186.         SendMessage(hWnd, WM_RBUTTONDOWN, 0, event.position.column -
  187.             true.left + ((DWORD)(event.position.line - true.top) << 16));
  188.         break;
  189.  
  190.     case L_INSERT_TOGGLE:
  191.         insertMode = !insertMode;
  192.         // Continue on to E_KEY.
  193.  
  194.     case E_KEY:
  195.     case L_DELETE:
  196.     case L_DELETE_EOL:
  197.     case L_DELETE_WORD:
  198.         if (FlagSet(woFlags, WOF_VIEW_ONLY))
  199.             break;
  200.  
  201.     case L_LEFT:
  202.     case L_RIGHT:
  203.     case L_BOL:
  204.     case L_EOL:
  205.     case L_WORD_LEFT:
  206.     case L_WORD_RIGHT:
  207.         if (event.key.value != VK_TAB && event.key.value != VK_RETURN)
  208.         {
  209.             if (event.rawCode)
  210.                 SendMessage(hWnd, WM_KEYDOWN, event.key.value, 0L);
  211.             else
  212.                 SendMessage(hWnd, WM_CHAR, event.key.value, 0L);
  213.             GetWindowText(hWnd, text, maxLength);
  214.         }
  215.         break;
  216.  
  217.     default:
  218.         ccode = UI_WINDOW_OBJECT::Event(event);
  219.         break;
  220.     }
  221.     return (ccode);
  222. }
  223.  
  224. #pragma argsused
  225. void UIW_STRING::DataSet(char *newText, short _maxLength)
  226. {
  227.     if (newText)
  228.     {
  229.         if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  230.             text = newText;
  231.         else
  232.         {
  233.             if (_maxLength != -1 && _maxLength > maxLength)
  234.             {
  235.                 delete text;
  236.                 text = new char[_maxLength];
  237.             }
  238.             int srcLen = ui_strlen(newText) + 1;
  239.             maxLength = (_maxLength == -1) ? maxLength : _maxLength;
  240.             memcpy(text, newText, Min(srcLen, maxLength));
  241.         }
  242.  
  243.         if (hWnd)
  244.         {
  245.             SetWindowText(hWnd, text);
  246.             SendMessage(hWnd, EM_LIMITTEXT, maxLength, 0L);
  247.         }
  248.     }
  249.     UI_WINDOW_OBJECT::Redisplay(FALSE);
  250. }
  251.  
  252. #ifdef ZIL_LOAD
  253. UIW_STRING::UIW_STRING(const char *name, UI_STORAGE *file, USHORT loadFlags) :
  254.     UI_WINDOW_OBJECT(name, file, loadFlags | L_SUB_LEVEL)
  255. {
  256.     windowID[0] = ID_STRING;
  257.  
  258.     if (!file)
  259.         file = _storage;
  260.     file->Load(&stFlags);
  261.     file->Load(&maxLength);
  262.     text = new char[maxLength];
  263.     short size;
  264.     file->Load(&size);
  265.     if (size)
  266.         file->Load(text, size);
  267.     text[size] = '\0';
  268.     insertMode = TRUE;
  269.     if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
  270.         delete file;
  271. }
  272. #endif
  273.  
  274. #ifdef ZIL_STORE
  275. void UIW_STRING::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
  276. {
  277.     UI_WINDOW_OBJECT::Store(name, file, storeFlags | S_SUB_LEVEL);
  278.     file->Store(stFlags);
  279.     file->Store(maxLength);
  280.     file->Store(text);
  281.     if (!FlagSet(storeFlags, S_SUB_LEVEL))
  282.         file->ObjectSize(name, search);
  283. }
  284. #endif
  285.