home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - BUTTON.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_win.hpp"
- #include <ctype.h>
- #include <string.h>
-
- UIW_BUTTON::UIW_BUTTON(int left, int top, int width, char *_string,
- USHORT _btFlags, USHORT _woFlags, void (*_userFunction)(void *object, UI_EVENT &event),
- USHORT _value) :
- UI_WINDOW_OBJECT(left, top, width, 1, _woFlags, WOAF_NO_FLAGS),
- btFlags(_btFlags), btStatus(BTS_NO_STATUS), userFunction(_userFunction),
- string(NULL), value(_value), getString(NULL), time(0)
- {
- // Initialize the button information.
- windowID[0] = ID_BUTTON;
- search.type = ID_BUTTON;
- depth = FlagSet(_btFlags, BTF_NO_3D) ? 0 : 1;
-
- // Match with the appropriate Windows 3.0 flags.
- if (FlagSet(woFlags, WOF_BORDER))
- MSWindowsStyle |= WS_BORDER;
-
- if (!FlagSet(btFlags, BTF_NO_3D))
- MSWindowsStyle |= BS_PUSHBUTTON;
-
- if (!_string)
- return;
- string = FlagSet(_woFlags, WOF_NO_ALLOCATE_DATA) ? _string : ui_strdup(_string);
- }
-
- UIW_BUTTON::~UIW_BUTTON()
- {
- // Destroy the string information.
- if (string && !FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- delete string;
- if (getString)
- delete getString;
- }
-
- void UIW_BUTTON::DataSet(char *_string)
- {
- // Reset the button's string information.
- if (_string)
- {
- if (string && !FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
- delete string;
- string = FlagSet(woFlags, WOF_NO_ALLOCATE_DATA) ? _string : ui_strdup(_string);
-
- if (hWnd)
- SetWindowText(hWnd, string);
- }
- UI_WINDOW_OBJECT::Redisplay(FALSE);
- }
-
- const char *UIW_BUTTON::DataGet(int stripString)
- {
- if (!string || !stripString)
- return (string);
- if (getString)
- delete getString;
- getString = new char[strlen(string)+1];
- int offset = 0;
- while (string[offset] == ' ' || string[offset] == '√')
- offset++;
- strcpy(getString, &string[offset]);
- offset = strlen(getString) - 1;
- while (offset >= 0 && getString[offset] == ' ')
- offset--;
- getString[offset + 1] = '\0';
- return (getString);
- }
-
- int UIW_BUTTON::Event(const UI_EVENT &event)
- {
- // Switch on the event type.
- UI_PALETTE *palette;
- int ccode = UI_WINDOW_OBJECT::LogicalEvent(event, ID_BUTTON);
- int t_depth = (FlagSet(woFlags, WOF_NON_SELECTABLE)) ? 0 : depth;
- if (FlagSet(btFlags, BTF_CHECK_MARK))
- string[0] = (FlagSet(woStatus, WOS_SELECTED)) ? '√' : ' ';
- switch (ccode)
- {
- case S_INITIALIZE:
- {
- // Add item - event.rawCode ID number.
- char *tHotKey = NULL;
- if (string)
- tHotKey = strchr(string, '~');
- if (tHotKey)
- hotKey = toupper(tHotKey[1]);
- if (!FlagSet(btFlags, BTF_NO_3D))
- ui_strrepc(string, '~', '&');
- }
- break;
-
- case S_CREATE:
- depth = FlagSet(btFlags, BTF_NO_3D) ? 0 : 1;
- // Continue to S_SIZE.
-
- case S_SIZE:
- display->RegionConvert(relative, &woStatus, WOS_GRAPHICS);
- if (!display->isText && FlagSet(btFlags, BTF_AUTO_SIZE))
- relative.top = relative.bottom - (display->cellHeight * 3) / 2;
- UI_WINDOW_OBJECT::RegionMax(TRUE);
-
- 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 && !FlagSet(btFlags, BTF_NO_3D))
- {
- hWnd = CreateWindow("Button", string,
- 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;
- }
- else if (hWnd)
- {
- 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 (hWnd && FlagSet(woAdvancedStatus, WOAS_TOO_SMALL))
- {
- EnableWindow(hWnd, 0);
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- }
- else if (hWnd && !IsWindowEnabled(hWnd))
- EnableWindow(hWnd, 1);
- break;
-
- case S_CURRENT:
- case S_DISPLAY_ACTIVE:
- if (hWnd)
- {
- if (FlagSet(woStatus, WOS_CURRENT) && !FlagSet(woFlags, WOF_VIEW_ONLY))
- {
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- SendMessage(hWnd, WM_SETFOCUS, parent->hWnd, 0L);
- }
- }
- else if (ccode == S_CURRENT || UI_WINDOW_OBJECT::NeedsUpdate(event, ccode))
- {
- lastPalette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
- UI_WINDOW_OBJECT::Text(string, t_depth, ccode, lastPalette);
- }
- break;
-
- case S_NON_CURRENT:
- woStatus &= ~WOS_CURRENT;
- btStatus &= ~BTS_DEPRESSED;
- if (hWnd)
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- // Continue on to S_DISPLAY_INACTIVE.
-
- case S_DISPLAY_INACTIVE:
- if (!hWnd && UI_WINDOW_OBJECT::NeedsUpdate(event, ccode))
- {
- lastPalette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
- UI_WINDOW_OBJECT::Text(string, t_depth, ccode, lastPalette);
- }
- break;
-
- case L_VIEW:
- // See if it is an event the button knows how to handle.
- if (event.rawCode == 0)
- {
- UI_WINDOW_OBJECT::Event(event);
- break;
- }
- // Continue to L_BEGIN_SELECT.
-
- case L_BEGIN_SELECT:
- case L_CONTINUE_SELECT:
- case L_END_SELECT:
- // Make sure the button can be selected.
- if (FlagSet(woFlags, WOF_NON_SELECTABLE))
- break;
-
- if (hWnd && ccode == L_BEGIN_SELECT)
- SendMessage(hWnd, WM_LBUTTONDOWN, 0, 0L);
-
- if (ccode != L_END_SELECT && !FlagSet(btStatus, BTS_DEPRESSED) &&
- UI_WINDOW_OBJECT::Overlap(event.position))
- {
- btStatus |= BTS_DEPRESSED;
- SendMessage(hWnd, BM_SETSTATE, 1, 0L);
- }
- else if (FlagSet(btStatus, BTS_DEPRESSED) &&
- !UI_WINDOW_OBJECT::Overlap(event.position))
- {
- btStatus &= ~BTS_DEPRESSED;
- SendMessage(hWnd, BM_SETSTATE, 0, 0L);
- }
-
- if (ccode == L_BEGIN_SELECT && FlagSet(btFlags, BTF_DOWN_CLICK))
- ccode = L_END_SELECT;
- else if (ccode != L_BEGIN_SELECT && FlagSet(btFlags, BTF_DOWN_CLICK))
- break;
-
- // See if the button is to be selected.
- if (!UI_WINDOW_OBJECT::Overlap(event.position) || ccode != L_END_SELECT ||
- (FlagSet(btFlags, BTF_DOUBLE_CLICK) && ui_time() - time > repeatRate))
- {
- time = ui_time();
- break;
- }
- // Continue to L_SELECT.
-
- case L_SELECT:
- if (!FlagSet(woStatus, WOS_SELECTED) && !FlagSet(btFlags, BTF_NO_TOGGLE))
- woStatus |= WOS_SELECTED;
- else if (!FlagSet(btFlags, BTF_NO_TOGGLE))
- woStatus &= ~WOS_SELECTED;
- btStatus &= ~BTS_DEPRESSED;
- UI_EVENT t_event = event;
- if (hWnd)
- {
- if (ccode == L_SELECT)
- {
- SendMessage(hWnd, BM_SETSTATE, 1, 0L);
- WaitMessage();
- SendMessage(hWnd, BM_SETSTATE, 0, 0L);
- }
- SendMessage(hWnd, WM_KILLFOCUS, parent->hWnd, 0L);
- SendMessage(hWnd, WM_SETFOCUS, parent->hWnd, 0L);
- }
- else
- {
- palette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
- UI_WINDOW_OBJECT::Text(string, t_depth, ccode, palette);
- }
- if (userFunction)
- (*userFunction)(this, t_event);
- woStatus &= ~WOS_UNANSWERED;
- time = ui_time();
- if (FlagSet(btFlags, BTF_CHECK_MARK))
- {
- string[0] = (FlagSet(woStatus, WOS_SELECTED)) ? '√' : ' ';
- if (hWnd)
- SetWindowText(hWnd, string);
- }
- break;
-
- default:
- ccode = UI_WINDOW_OBJECT::Event(event);
- break;
- }
-
- // Return the control code.
- return (ccode);
- }
-
- #ifdef ZIL_LOAD
- UIW_BUTTON::UIW_BUTTON(const char *name, UI_STORAGE *file, USHORT loadFlags) :
- UI_WINDOW_OBJECT(name, file, loadFlags | L_SUB_LEVEL),
- btStatus(BTS_NO_STATUS), userFunction(NULL), getString(NULL), value(0)
- {
- windowID[0] = ID_BUTTON;
-
- if (!file)
- file = _storage;
- file->Load(&btFlags);
- file->Load(&value);
- file->Load(&string);
- if (!string)
- string = ui_strdup("");
- depth = (FlagSet(BTF_NO_3D, btFlags)) ? 0 : 1;
- if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
- delete file;
- }
- #endif
-
- #ifdef ZIL_STORE
- void UIW_BUTTON::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
- {
- UI_WINDOW_OBJECT::Store(name, file, storeFlags | S_SUB_LEVEL);
- file->Store(btFlags);
- file->Store(value);
- file->Store(string);
- if (!FlagSet(storeFlags, S_SUB_LEVEL))
- file->ObjectSize(name, search);
- }
- #endif
-