home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - ICON.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #define USE_RAW_KEYS
- #include "ui_win.hpp"
- #include <string.h>
-
- static UI_PALETTE _colorMap[16] =
- {
- { HS_NONE, BLACK, BLACK },
- { HS_NONE, BLUE, BLUE },
- { HS_NONE, GREEN, GREEN },
- { HS_NONE, CYAN, CYAN },
- { HS_NONE, RED, RED },
- { HS_NONE, MAGENTA, MAGENTA },
- { HS_NONE, BROWN, BROWN },
- { HS_NONE, LIGHTGRAY, LIGHTGRAY },
- { HS_NONE, DARKGRAY, DARKGRAY },
- { HS_NONE, LIGHTBLUE, LIGHTBLUE },
- { HS_NONE, LIGHTGREEN, LIGHTGREEN },
- { HS_NONE, LIGHTCYAN, LIGHTCYAN },
- { HS_NONE, LIGHTRED, LIGHTRED },
- { HS_NONE, LIGHTMAGENTA, LIGHTMAGENTA },
- { HS_NONE, YELLOW, YELLOW },
- { HS_NONE, WHITE, WHITE }
- };
-
- UIW_ICON::UIW_ICON(int left, int top, int _bitmapWidth, int _bitmapHeight,
- UCHAR *_bitmapArray, char *_string, USHORT _icFlags, USHORT _woFlags,
- void (*_userFunction)(void *object, UI_EVENT &event), int _ratioWidth,
- int _ratioHeight) :
- UI_WINDOW_OBJECT(left, top, 1, 1, _woFlags, WOAF_NO_SIZE | WOAF_MULTIPLE_REGIONS),
- icFlags(_icFlags), bitmapWidth(_bitmapWidth), bitmapHeight(_bitmapHeight),
- bitmapArray(_bitmapArray), userFunction(_userFunction), lastTime(0),
- window(0), ratioWidth(_ratioWidth), ratioHeight(_ratioHeight)
- {
- // Initialize the basic icon information.
- windowID[0] = ID_ICON;
- search.type = ID_ICON;
-
- if (FlagSet(_icFlags, ICF_READ | ICF_WRITE | ICF_AUTO_SELECT | ICF_AUTO_ESCAPE | ICF_END))
- woAdvancedFlags |= WOAF_NON_CURRENT;
- string = ui_strdup(_string);
- if (FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- bitmapArray = _bitmapArray;
- else
- {
- bitmapArray = new UCHAR[_bitmapWidth * _bitmapHeight];
- if (!_bitmapArray)
- memset(bitmapArray, LIGHTGRAY, _bitmapWidth * _bitmapHeight);
- else
- memcpy(bitmapArray, _bitmapArray, _bitmapWidth * _bitmapHeight);
- }
- }
-
- void UIW_ICON::DataSet(int _bitmapWidth, int _bitmapHeight, UCHAR *_bitmapArray)
- {
- if (_bitmapArray)
- {
- bitmapWidth = _bitmapWidth;
- bitmapHeight = _bitmapHeight;
- delete bitmapArray;
- bitmapArray = _bitmapArray;
- }
- UI_WINDOW_OBJECT::Redisplay(FALSE);
- }
-
- UIW_ICON::Event(const UI_EVENT &event)
- {
- // Switch on the event type.
- int redisplay = FALSE;
- int border = FlagSet(woFlags, WOF_BORDER) ? 1 : 0;
- int ccode = UI_WINDOW_OBJECT::LogicalEvent(event, ID_ICON);
- switch (ccode)
- {
- case S_DEFINE_REGION:
- if (!parent)
- {
- if (!display->isText)
- display->RegionDefine(screenID, iconRegion);
- if (string)
- display->RegionDefine(screenID, stringRegion);
- }
- break;
-
- case S_CREATE:
- case S_SIZE:
- if (display->isText)
- {
- relative.right = relative.left + display->TextWidth(string) + 1;
- relative.bottom = relative.top + 2;
- }
- else
- {
- display->RegionConvert(relative, &woStatus, WOS_GRAPHICS);
- relative.right = relative.left + bitmapWidth * ratioWidth - 1;
- relative.bottom = relative.top + bitmapHeight * ratioHeight - 1;
- if (FlagSet(woFlags, WOF_BORDER))
- {
- relative.right += bitmapWidth + 1;
- relative.bottom += bitmapHeight + 1;
- }
- if (FlagSet(icFlags, ICF_AUTO_SELECT))
- memset(bitmapArray, leftColor, bitmapWidth * bitmapHeight);
- else if (FlagSet(icFlags, ICF_AUTO_ESCAPE))
- memset(bitmapArray, rightColor, bitmapWidth * bitmapHeight);
- if (string)
- {
- if (relative.right - relative.left < display->TextWidth(string))
- relative.right = relative.left + display->TextWidth(string) - 1;
- relative.bottom += display->TextHeight(string) + 5;
- }
- }
- // Continue to S_MOVE.
-
- case S_MOVE:
- if (display->isText)
- {
- ccode = UI_WINDOW_OBJECT::Event(event);
- iconRegion = stringRegion = true;
- break;
- }
- int delta = FlagSet(woFlags, WOF_BORDER) ? bitmapWidth : 0;
- delta = display->isText ? 0 : (relative.right - relative.left - bitmapWidth * ratioWidth - delta) / 2;
- ccode = UI_WINDOW_OBJECT::Event(event);
-
- iconRegion = true;
- stringRegion = true;
- if (!display->isText)
- {
- iconRegion.left = true.left + delta;
- iconRegion.right = iconRegion.left + bitmapWidth * ratioWidth - 1;
- iconRegion.bottom = iconRegion.top + bitmapHeight * ratioHeight - 1;
- if (FlagSet(woFlags, WOF_BORDER))
- {
- iconRegion.right += bitmapWidth + 1;
- iconRegion.bottom += bitmapHeight + 1;
- }
- if (iconRegion.right > true.right)
- iconRegion.right = true.right;
- if (iconRegion.bottom > true.bottom)
- iconRegion.bottom = true.bottom;
- if (string)
- {
- if (iconRegion.bottom < true.bottom)
- stringRegion.top = iconRegion.bottom + 4;
- int length = true.right - true.left - display->TextWidth(string);
- if (length > 0)
- {
- stringRegion.left = true.left + length / 2;
- stringRegion.right = stringRegion.left + length - 1;
- }
- }
- }
- break;
-
- case L_FLOOD_COLOR:
- redisplay = TRUE;
- if (FlagSet(event.rawCode, M_LEFT_CHANGE) && FlagSet(icFlags, ICF_AUTO_SELECT | ICF_READ))
- memset(bitmapArray, leftColor, bitmapWidth * bitmapHeight);
- else if (FlagSet(event.rawCode, M_RIGHT_CHANGE) && FlagSet(icFlags, ICF_AUTO_ESCAPE | ICF_READ))
- memset(bitmapArray, rightColor, bitmapWidth * bitmapHeight);
- else
- redisplay = FALSE;
- if (next)
- {
- UI_WINDOW_OBJECT *object = Next();
- if (object)
- object->Event(event);
- }
- break;
-
- case L_CHECK_COLOR:
- redisplay = TRUE;
- if (FlagSet(event.rawCode, M_LEFT_CHANGE) && FlagSet(icFlags, ICF_AUTO_SELECT))
- bitmapArray[0] = leftColor;
- else if (FlagSet(event.rawCode, M_RIGHT_CHANGE) && FlagSet(icFlags, ICF_AUTO_ESCAPE))
- bitmapArray[0] = rightColor;
- else
- {
- UI_WINDOW_OBJECT *object = Next();
- if (object)
- object->Event(event);
- redisplay = FALSE;
- }
- break;
-
- case L_SELECT:
- if (userFunction)
- {
- UI_EVENT tEvent = event;
- (*userFunction)(this, tEvent);
- lastTime = 0;
- }
- if (window)
- {
- UI_WINDOW_MANAGER *tWindowManager = windowManager;
- *tWindowManager - this;
- *tWindowManager + window;
- }
- break;
-
- case L_BEGIN_SELECT:
- case L_CONTINUE_SELECT:
- case L_END_SELECT:
- int column = (event.position.column - true.left) / (ratioWidth + border);
- int line = (event.position.line - true.top) / (ratioHeight + border);
- int currentColor = FlagSet(event.rawCode, M_LEFT | M_LEFT_CHANGE) ? leftColor : rightColor;
- if ((icFlags == ICF_NO_FLAGS || icFlags == ICF_SINGLE_CLICK) &&
- UI_WINDOW_OBJECT::Overlap(event.position))
- {
- UI_REGION region = true;
- true = iconRegion;
- int move = UI_WINDOW_OBJECT::Overlap(event.position);
- true = stringRegion;
- if (!move)
- move = UI_WINDOW_OBJECT::Overlap(event.position);
- true = region;
- if (!move)
- break;
- if (icFlags == ICF_SINGLE_CLICK || ui_time() - lastTime < 12)
- {
- if (userFunction)
- {
- UI_EVENT tEvent = event;
- (*userFunction)(this, tEvent);
- lastTime = 0;
- }
- if (window)
- {
- UI_WINDOW_MANAGER *tWindowManager = windowManager;
- *tWindowManager - this;
- *tWindowManager + window;
- }
- break;
- }
- else
- lastTime = ui_time();
- UI_EVENT tEvent = event;
- tEvent.rawCode = DM_MOVE;
- eventManager->Event(tEvent);
- if (ccode == L_BEGIN_SELECT)
- {
- tEvent.type = S_MOVE;
- eventManager->Put(tEvent, Q_BEGIN);
- }
- break;
- }
- else if (column < 0 || column >= bitmapWidth || line < 0 || line >= bitmapHeight)
- break;
- else if (FlagSet(icFlags, ICF_WRITE) &&
- bitmapArray[line * bitmapWidth + column] == currentColor)
- {
- if (ccode == L_END_SELECT && ui_time() - lastTime < repeatRate && next)
- {
- UI_EVENT tEvent = event;
- tEvent.type = L_FLOOD_COLOR;
- UI_WINDOW_OBJECT *object = Next();
- object->Event(tEvent);
- lastTime = 0;
- }
- else if (ccode == L_END_SELECT)
- lastTime = ui_time();
- }
- else if (FlagSet(icFlags, ICF_READ) &&
- bitmapArray[line * bitmapWidth + column] != currentColor)
- {
- UI_REGION region;
- region.left = iconRegion.left + (ratioWidth + border) * column + border;
- region.right = region.left + ratioWidth - 1;
- region.top = iconRegion.top + (ratioHeight + border) * line + border;
- region.bottom = region.top + ratioHeight - 1;
- display->Rectangle(screenID, region, &_colorMap[currentColor], 0, TRUE);
- bitmapArray[line * bitmapWidth + column] = currentColor;
- if (next && !FlagSet(icFlags, ICF_END))
- {
- UI_WINDOW_OBJECT *object = Next();
- UI_EVENT tEvent = event;
- tEvent.position.column = object->true.left + column;
- tEvent.position.line = object->true.top + line;
- object->Event(tEvent);
- }
- }
- else if (FlagSet(icFlags, ICF_WRITE) &&
- bitmapArray[line * bitmapWidth + column] != currentColor &&
- ccode == L_END_SELECT)
- {
- if (FlagSet(event.rawCode, M_LEFT_CHANGE))
- leftColor = bitmapArray[line * bitmapWidth + column];
- else
- rightColor = bitmapArray[line * bitmapWidth + column];
- if (next && !FlagSet(icFlags, ICF_END))
- {
- UI_WINDOW_OBJECT *object = Next();
- UI_EVENT tEvent = event;
- tEvent.type = L_CHECK_COLOR;
- object->Event(tEvent);
- }
- lastTime = ui_time();
- }
- break;
-
- case S_CURRENT:
- case S_DISPLAY_ACTIVE:
- case S_DISPLAY_INACTIVE:
- case S_NON_CURRENT:
- if (string && UI_WINDOW_OBJECT::NeedsUpdate(event, ccode))
- {
- UI_REGION region = true;
- true = stringRegion;
- woFlags |= WOF_JUSTIFY_CENTER;
- Text(string, 0, ccode, lastPalette);
- true = region;
- }
- if (ccode != S_NON_CURRENT &&
- !FlagSet(woAdvancedStatus, WOAS_INVALID_REGION) &&
- UI_WINDOW_OBJECT::Overlap(event.region))
- redisplay = TRUE;
- break;
-
- default:
- ccode = UI_WINDOW_OBJECT::Event(event);
- break;
- }
-
- // Redisplay the object information.
- if (redisplay && ratioWidth == 1 && ratioWidth == 1 && !display->isText)
- display->Bitmap(screenID, iconRegion.left, iconRegion.top,
- bitmapWidth, bitmapHeight, bitmapArray, NULL, &iconRegion);
- else if (redisplay && !display->isText)
- {
- int k = 0;
- UI_REGION region;
- region.top = iconRegion.top + border;
- for (int i = 0; i < bitmapHeight; i++)
- {
- region.bottom = region.top + ratioHeight - 1;
- region.left = iconRegion.left + border;
- for (int j = 0; j < bitmapWidth; j++, k++)
- {
- region.right = region.left + ratioWidth - 1;
- display->Rectangle(screenID, region, &_colorMap[bitmapArray[k]], 0, TRUE);
- if (i == 0 && border)
- display->Line(screenID, region.right + 1, iconRegion.top,
- region.right + 1, iconRegion.bottom, &_colorMap[DARKGRAY]);
- if (j == 0 && border)
- display->Line(screenID, iconRegion.left, region.bottom + 1,
- iconRegion.right, region.bottom + 1, &_colorMap[DARKGRAY]);
- region.left += ratioWidth + border;
- }
- region.top += ratioHeight + border;
- }
- if (border)
- display->Rectangle(screenID, iconRegion, &_colorMap[DARKGRAY]);
- }
-
- // Return the control code.
- return (ccode);
- }
-
- #ifdef ZIL_LOAD
- UIW_ICON::UIW_ICON(const char *name, UI_STORAGE *file, USHORT loadFlags) :
- UI_WINDOW_OBJECT(name, file, loadFlags | L_SUB_LEVEL),
- userFunction(NULL), lastTime(0), ratioWidth(1), ratioHeight(1)
- {
- windowID[0] = ID_ICON;
-
- lastTime = 0;
- if (!file)
- file = _storage;
- file->Load(&icFlags);
- file->Load(&bitmapWidth);
- file->Load(&bitmapHeight);
- short size = bitmapWidth * bitmapHeight;
- bitmapArray = new UCHAR[size];
- file->Load(bitmapArray, size);
- file->Load(&string);
- if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
- delete file;
- }
- #endif
-
- #ifdef ZIL_STORE
- void UIW_ICON::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
- {
- UI_WINDOW_OBJECT::Store(name, file, storeFlags | S_SUB_LEVEL);
-
- file->Store(icFlags);
- file->Store(bitmapWidth);
- file->Store(bitmapHeight);
- short size = bitmapWidth * bitmapHeight;
- file->Store(bitmapArray, size);
- file->Store(string);
- if (!FlagSet(storeFlags, S_SUB_LEVEL))
- file->ObjectSize(name, search);
- }
- #endif
-