home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_6.ZIP / DOSSRC.ZIP / BUTTON.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  6.2 KB  |  224 lines

  1. //    Zinc Interface Library - BUTTON.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 <string.h>
  8.  
  9. UIW_BUTTON::UIW_BUTTON(int left, int top, int width, char *_string,
  10.     USHORT _btFlags, USHORT _woFlags, void (*_userFunction)(void *object, UI_EVENT &event),
  11.     USHORT _value) :
  12.     UI_WINDOW_OBJECT(left, top, width, 1, _woFlags, WOAF_NO_FLAGS),
  13.     btFlags(_btFlags), btStatus(BTS_NO_STATUS), userFunction(_userFunction),
  14.     string(NULL), value(_value), getString(NULL), time(0)
  15. {
  16.     // Initialize the button information.
  17.     windowID[0] = ID_BUTTON;
  18.     search.type = ID_BUTTON;
  19.     depth = FlagSet(_btFlags, BTF_NO_3D) ? 0 : 1;
  20.  
  21.     if (!_string)
  22.         return;
  23.     string = FlagSet(_woFlags, WOF_NO_ALLOCATE_DATA) ? _string : ui_strdup(_string);
  24. }
  25.  
  26. UIW_BUTTON::~UIW_BUTTON()
  27. {
  28.     // Destroy the string information.
  29.     if (string && !FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  30.         delete string;
  31.     if (getString)
  32.         delete getString;
  33. }
  34.  
  35. void UIW_BUTTON::DataSet(char *_string)
  36. {
  37.     // Reset the button's string information.
  38.     if (_string)
  39.     {
  40.         if (string && !FlagSet(woFlags, WOF_NO_ALLOCATE_DATA))
  41.             delete string;
  42.         string = FlagSet(woFlags, WOF_NO_ALLOCATE_DATA) ? _string : ui_strdup(_string);
  43.     }
  44.     UI_WINDOW_OBJECT::Redisplay(FALSE);
  45. }
  46.  
  47. const char *UIW_BUTTON::DataGet(int stripString)
  48. {
  49.     if (!string || !stripString)
  50.         return (string);
  51.     if (getString)
  52.         delete getString;
  53.     getString = new char[strlen(string)+1];
  54.     int offset = 0;
  55.     while (string[offset] == ' ' || string[offset] == '√')
  56.         offset++;
  57.     strcpy(getString, &string[offset]);
  58.     offset = strlen(getString) - 1;
  59.     while (offset >= 0 && getString[offset] == ' ')
  60.         offset--;
  61.     getString[offset + 1] = '\0';
  62.     return (getString);
  63. }
  64.  
  65. int UIW_BUTTON::Event(const UI_EVENT &event)
  66. {
  67.     // Switch on the event type.
  68.     UI_REGION region = true;
  69.     UI_PALETTE *palette;
  70.     int ccode = UI_WINDOW_OBJECT::LogicalEvent(event, ID_BUTTON);
  71.     int t_depth = (FlagSet(woFlags, WOF_NON_SELECTABLE)) ? 0 : depth;
  72.     if (FlagSet(btFlags, BTF_CHECK_MARK))
  73.         string[0] = (FlagSet(woStatus, WOS_SELECTED)) ? '√' : ' ';
  74.     switch (ccode)
  75.     {
  76.     case S_CREATE:
  77.         {
  78.         depth = FlagSet(btFlags, BTF_NO_3D) ? 0 : 1;
  79.         if (depth)
  80.             woFlags |= WOF_BORDER;
  81.         char *tHotKey = strchr(string, '~');
  82.         if (tHotKey)
  83.             hotKey = toupper(tHotKey[1]);
  84.         }
  85.         // Continue to S_SIZE.
  86.  
  87.     case S_SIZE:
  88.         display->RegionConvert(relative, &woStatus, WOS_GRAPHICS);
  89.         if (!display->isText && FlagSet(btFlags, BTF_AUTO_SIZE))
  90.             relative.top = relative.bottom - (display->cellHeight * 3) / 2;
  91.         UI_WINDOW_OBJECT::RegionMax(TRUE);
  92.         break;
  93.  
  94.     case S_NON_CURRENT:
  95.         woStatus &= ~WOS_CURRENT;
  96.         btStatus &= ~BTS_DEPRESSED;
  97.         palette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
  98.         UI_WINDOW_OBJECT::Text(string, t_depth, ccode, palette);
  99.         break;
  100.  
  101.     case S_CURRENT:
  102.     case S_DISPLAY_INACTIVE:
  103.     case S_DISPLAY_ACTIVE:
  104.         // Display the button information.
  105.         if (ccode == S_CURRENT || UI_WINDOW_OBJECT::NeedsUpdate(event, ccode))
  106.         {
  107.             lastPalette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
  108.             UI_WINDOW_OBJECT::Text(string, t_depth, ccode, lastPalette);
  109.         }
  110.         break;
  111.  
  112.     case L_VIEW:
  113.         // See if it is an event the button knows how to handle.
  114.         if (event.rawCode == 0)
  115.         {
  116.             UI_WINDOW_OBJECT::Event(event);
  117.             break;
  118.         }
  119.         // Continue to L_BEGIN_SELECT.
  120.  
  121.     case L_BEGIN_SELECT:
  122.     case L_CONTINUE_SELECT:
  123.     case L_END_SELECT:
  124.         // Make sure the button can be selected.
  125.         if (FlagSet(woFlags, WOF_NON_SELECTABLE))
  126.             break;
  127.  
  128.         /* Make sure the button needs to be depressed */
  129.         if (!FlagSet(btStatus, BTS_DEPRESSED) && 
  130.             UI_WINDOW_OBJECT::Overlap(event.position))
  131.         {
  132.             if (display->isText)
  133.             {
  134.                 palette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
  135.                 UI_WINDOW_OBJECT::Text(string, 0, ccode, palette);
  136.             }
  137.             else
  138.                 UI_WINDOW_OBJECT::Shadow(region, -t_depth);
  139.             btStatus |= BTS_DEPRESSED;
  140.         }
  141.         else if (FlagSet(btStatus, BTS_DEPRESSED) &&
  142.             !UI_WINDOW_OBJECT::Overlap(event.position))
  143.         {
  144.             if (display->isText)
  145.             {
  146.                 palette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
  147.                 UI_WINDOW_OBJECT::Text(string, 0, ccode, palette);
  148.             }
  149.             else
  150.                 UI_WINDOW_OBJECT::Shadow(region, t_depth);
  151.             btStatus &= ~BTS_DEPRESSED;
  152.         }
  153.         if (ccode == L_BEGIN_SELECT && FlagSet(btFlags, BTF_DOWN_CLICK))
  154.             ccode = L_END_SELECT;
  155.         else if (ccode != L_BEGIN_SELECT && FlagSet(btFlags, BTF_DOWN_CLICK))
  156.             break;
  157.  
  158.         /* See if the button is to be selected */
  159.         if (!UI_WINDOW_OBJECT::Overlap(event.position) || ccode != L_END_SELECT ||
  160.             (FlagSet(btFlags, BTF_DOUBLE_CLICK) && ui_time() - time > repeatRate))
  161.         {
  162.             time = ui_time();
  163.             break;
  164.         }
  165.         // Continue to L_SELECT.
  166.  
  167.     case L_SELECT:
  168.         if (!FlagSet(woStatus, WOS_SELECTED) && !FlagSet(btFlags, BTF_NO_TOGGLE))
  169.             woStatus |= WOS_SELECTED;
  170.         else if (!FlagSet(btFlags, BTF_NO_TOGGLE))
  171.             woStatus &= ~WOS_SELECTED;
  172.         btStatus &= ~BTS_DEPRESSED;
  173.         UI_EVENT t_event = event;
  174.         if (userFunction)
  175.             (*userFunction)(this, t_event);
  176.         woStatus &= ~WOS_UNANSWERED;
  177.         time = ui_time();
  178.         if (FlagSet(btFlags, BTF_CHECK_MARK))
  179.             string[0] = (FlagSet(woStatus, WOS_SELECTED)) ? '√' : ' ';
  180.         palette = UI_WINDOW_OBJECT::LogicalPalette(ccode);
  181.         UI_WINDOW_OBJECT::Text(string, t_depth, ccode, palette);
  182.         break;
  183.  
  184.     default:
  185.         ccode = UI_WINDOW_OBJECT::Event(event);
  186.         break;
  187.     }
  188.  
  189.     // Return the control code.
  190.     return (ccode);
  191. }
  192.  
  193. #ifdef ZIL_LOAD
  194. UIW_BUTTON::UIW_BUTTON(const char *name, UI_STORAGE *file, USHORT loadFlags) :
  195.     UI_WINDOW_OBJECT(name, file, loadFlags | L_SUB_LEVEL),
  196.     btStatus(BTS_NO_STATUS), userFunction(NULL), getString(NULL), value(0)
  197. {
  198.     windowID[0] = ID_BUTTON;
  199.  
  200.     if (!file)
  201.         file = _storage;
  202.     file->Load(&btFlags);
  203.     file->Load(&value);
  204.     file->Load(&string);
  205.     if (!string)
  206.         string = ui_strdup("");
  207.     depth = (FlagSet(BTF_NO_3D, btFlags)) ? 0 : 1;
  208.     if (!FlagSet(loadFlags, L_SUB_LEVEL) && FlagSet(file->stStatus, STS_TEMPORARY))
  209.         delete file;
  210. }
  211. #endif
  212.  
  213. #ifdef ZIL_STORE
  214. void UIW_BUTTON::Store(const char *name, UI_STORAGE *file, USHORT storeFlags)
  215. {
  216.     UI_WINDOW_OBJECT::Store(name, file, storeFlags | S_SUB_LEVEL);
  217.     file->Store(btFlags);
  218.     file->Store(value);
  219.     file->Store(string);
  220.     if (!FlagSet(storeFlags, S_SUB_LEVEL))
  221.         file->ObjectSize(name, search);
  222. }
  223. #endif
  224.