home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_2.ZIP / RADIO.LIF / RADIO.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-13  |  6.4 KB  |  238 lines

  1. //    RADIO.CPP - Source code for radio button tutorial.
  2. //    COPYRIGHT (C) 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include <ui_win.hpp>
  6. #include <graphics.h>
  7. #include <string.h>
  8. #include "radio.hpp"
  9.  
  10. void Exit(void *data, UI_EVENT &event)
  11. {
  12.     // Exit the application by sending a message.
  13.     UIW_BUTTON *item = (UIW_BUTTON *)data;
  14.     event.type = L_EXIT;
  15.     item->eventManager->Put(event);
  16. }
  17.  
  18. RADIO_BUTTON::RADIO_BUTTON(int left, int top, int width, char *aText,
  19.     int aValue, USHORT aWoStatus) :
  20.     UI_WINDOW_OBJECT(left, top, width, 1, WOF_JUSTIFY_RIGHT, WOAF_NO_FLAGS),
  21.     value(aValue)
  22. {
  23.     text = ui_strdup(aText);
  24.     woStatus |= aWoStatus;
  25.     if (FlagSet(woStatus, WOS_SELECTED))
  26.         woStatus |= WOS_CURRENT;
  27. }
  28.  
  29. int RADIO_BUTTON::Event(const UI_EVENT &event)
  30. {
  31.     // Special radio button palettes
  32.     static UI_PALETTE fillButton =
  33.     {
  34.         ' ', attrib(BLACK, BLACK), attrib(MONO_BLACK, MONO_BLACK),
  35.         SOLID_FILL, BLACK, BLACK, BW_BLACK, BW_BLACK, GS_BLACK, GS_BLACK
  36.     };
  37.     static UI_PALETTE nonCurrentButton =
  38.     {
  39.         ' ', attrib(BLACK, LIGHTGRAY), attrib(MONO_NORMAL, MONO_BLACK),
  40.         SOLID_FILL, BLACK, WHITE, BW_BLACK, BW_WHITE, GS_BLACK, GS_WHITE
  41.     };
  42.     static UI_PALETTE currentButton =
  43.     {
  44.         ' ', attrib(LIGHTGRAY, DARKGRAY), attrib(MONO_BLACK, MONO_NORMAL),
  45.         SOLID_FILL, BLACK, LIGHTGRAY, BW_WHITE, BW_BLACK, GS_BLACK, GS_GRAY
  46.     };
  47.  
  48.     // Switch on the event type.
  49.     int ccode = event.type;
  50.     switch (ccode)
  51.     {
  52.     case S_CREATE:
  53.         if (display->isText)
  54.         {
  55.             relative.left--;
  56.             relative.top--;
  57.             relative.right--;
  58.             relative.bottom--;
  59.         }
  60.         ccode = UI_WINDOW_OBJECT::Event(event);
  61.         break;
  62.  
  63.     case S_CURRENT:
  64.     case S_NON_CURRENT:
  65.         if (ccode == S_CURRENT)
  66.             woStatus |= WOS_SELECTED;
  67.         else
  68.             woStatus &= ~WOS_SELECTED;
  69.         // Continue to S_DISPLAY_ACTIVE and S_DISPLAY_INACTIVE.
  70.  
  71.     case S_DISPLAY_ACTIVE:
  72.     case S_DISPLAY_INACTIVE:
  73.         UI_PALETTE *palette = (ccode == S_CURRENT) ? ¤tButton : &nonCurrentButton;
  74.         if (display->isText)
  75.         {
  76.             if (UI_WINDOW_OBJECT::NeedsUpdate(event, ccode) ||
  77.                 ccode == S_CURRENT)
  78.                 UI_WINDOW_OBJECT::Text(text, 0, ccode, palette);
  79.             char *radioText = FlagSet(woStatus, WOS_SELECTED) ? "()" : "( )";
  80.             display->Text(screenID, true.left, true.top, radioText, palette);
  81.         }
  82.         else
  83.         {
  84.             int radius = (relative.bottom - relative.top + 1) / 2;
  85.             int fillDiameter = (relative.bottom - relative.top + 1) / 4;
  86.             if (UI_WINDOW_OBJECT::NeedsUpdate(event, ccode) ||
  87.                 ccode == S_CURRENT)
  88.             {
  89.                 UI_WINDOW_OBJECT::Text(text, 0, ccode, palette);
  90.                 display->Ellipse(screenID, true.left + radius, true.top + radius,
  91.                     0, 360, radius, radius, palette, FALSE);
  92.             }
  93.             if (FlagSet(woStatus, WOS_SELECTED))
  94.                 display->Ellipse(screenID, true.left + radius, true.top + radius,
  95.                     0, 360, fillDiameter, fillDiameter, &fillButton, TRUE);
  96.         }
  97.         break;
  98.  
  99.     default:
  100.         ccode = UI_WINDOW_OBJECT::Event(event);
  101.         break;
  102.     }
  103.  
  104.     // Return the control code.
  105.     return (ccode);
  106. }
  107.  
  108. RADIO_CONTROL::RADIO_CONTROL(int left, int top, int width, int height,
  109.     char *aTitle, USHORT woFlags) :
  110.     UIW_WINDOW(left, top, width, height, woFlags)
  111. {
  112.     title = ui_strdup(aTitle);
  113. }
  114.  
  115. int RADIO_CONTROL::Event(const UI_EVENT &event)
  116. {
  117.     // Switch on the event type.
  118.     UI_EVENT tEvent = event;
  119.     int ccode = UI_WINDOW_OBJECT::LogicalEvent(event, ID_WINDOW);
  120.     switch (ccode)
  121.     {
  122.     case S_NON_CURRENT:
  123.         if (current)
  124.         {
  125.             tEvent.type = S_DISPLAY_ACTIVE;
  126.             tEvent.region = true;
  127.             UI_WINDOW_OBJECT *object = (UI_WINDOW_OBJECT *)current;
  128.             object->Event(tEvent);
  129.         }
  130.         return (ccode);
  131.  
  132.     case S_CURRENT:
  133.     case S_DISPLAY_ACTIVE:
  134.     case S_DISPLAY_INACTIVE:
  135.         UIW_WINDOW::Event(tEvent);
  136.         display->Text(screenID, true.left + display->cellWidth, true.top, title, lastPalette, -1);
  137.         return (ccode);
  138.  
  139.     case L_NEXT:
  140.     case L_PREVIOUS:
  141.         return (S_UNKNOWN);
  142.         break;
  143.  
  144.     case L_UP:
  145.     case L_LEFT:
  146.         tEvent.type = L_PREVIOUS;
  147.         break;
  148.  
  149.     case L_DOWN:
  150.     case L_RIGHT:
  151.         tEvent.type = L_NEXT;
  152.         break;
  153.     }
  154.  
  155.     // Return the control code.
  156.     return (UIW_WINDOW::Event(tEvent));
  157. }
  158.  
  159. main()
  160. {
  161.     // Initialize the display, trying for graphics first.
  162.     UI_DISPLAY *display = new UI_DOS_BGI_DISPLAY;
  163.     if (!display->installed)
  164.     {
  165.         delete display;
  166.         display = new UI_DOS_TEXT_DISPLAY;
  167.     }
  168.  
  169.     // Initialize the event manager and add three devices to it.
  170.     UI_EVENT_MANAGER eventManager(100, display);
  171.     eventManager
  172.         + new UI_BIOS_KEYBOARD
  173.         + new UI_MS_MOUSE
  174.         + new UI_CURSOR;
  175.     
  176.     // Initialize the window manager.
  177.     UI_WINDOW_MANAGER windowManager(display, &eventManager);
  178.  
  179.     // Set up the radio controls.
  180.     RADIO_CONTROL *port = new RADIO_CONTROL(2, 1, 14, 7, " PORT ", WOF_BORDER);
  181.     *port
  182.         + new RADIO_BUTTON(2, 1, 10, " LPT 1 ", PORT_LPT1, WOS_SELECTED)
  183.         + new RADIO_BUTTON(2, 2, 10, " LPT 2 ", PORT_LPT2)
  184.         + new RADIO_BUTTON(2, 3, 10, " COM 1 ", PORT_COM1)
  185.         + new RADIO_BUTTON(2, 4, 10, " COM 2 ", PORT_COM2)
  186.         + new RADIO_BUTTON(2, 5, 10, " OTHER ", PORT_OTHER);
  187.  
  188.     RADIO_CONTROL *baudRate = new RADIO_CONTROL(19, 1, 13, 5, " BAUD ", WOF_BORDER);
  189.     *baudRate
  190.         + new RADIO_BUTTON(2, 1, 9, " 1200 ", BAUD_1200)
  191.         + new RADIO_BUTTON(2, 2, 9, " 2400 ", BAUD_2400, WOS_SELECTED)
  192.         + new RADIO_BUTTON(2, 3, 9, " 9600 ", BAUD_9600);
  193.  
  194.     RADIO_CONTROL *line = new RADIO_CONTROL(35, 1, 17, 4, " LINE ", WOF_BORDER);
  195.     *line
  196.         + new RADIO_BUTTON(2, 1, 13, " Enabled  ", LINE_ENABLED, WOS_SELECTED)
  197.         + new RADIO_BUTTON(2, 2, 13, " Disabled ", LINE_DISABLED);
  198.  
  199.     // Create the communication window.
  200.     UIW_WINDOW *window = new UIW_WINDOW(5, 5, 56, 14);
  201.  
  202.     *window
  203.         + new UIW_BORDER
  204.         + new UIW_MAXIMIZE_BUTTON
  205.         + new UIW_MINIMIZE_BUTTON
  206.         + new UIW_SYSTEM_BUTTON
  207.         + new UIW_TITLE("Communication Setup")
  208.         + port
  209.         + baudRate
  210.         + line
  211.         + new UIW_BUTTON(14, 10, 10, "~OK", BTF_NO_TOGGLE | BTF_AUTO_SIZE,
  212.             WOF_BORDER | WOF_JUSTIFY_CENTER, Exit, L_EXIT)
  213.         + new UIW_BUTTON(28, 10, 10, "~Cancel", BTF_NO_TOGGLE | BTF_AUTO_SIZE,
  214.             WOF_BORDER | WOF_JUSTIFY_CENTER, NULL, S_CONTINUE);
  215.  
  216.     // Add the window to the window manager.
  217.     windowManager + window;
  218.  
  219.     // Wait for user response.
  220.     int ccode;
  221.     do
  222.     {
  223.         // Get input from the user.
  224.         UI_EVENT event;
  225.         eventManager.Get(event, Q_NORMAL);
  226.  
  227.         // Send event information to the window manager.
  228.         ccode = windowManager.Event(event);
  229.     } while (ccode != L_EXIT && ccode != S_NO_OBJECT);
  230.  
  231.     // Clean up.
  232.     windowManager.UI_WINDOW_MANAGER::~UI_WINDOW_MANAGER();
  233.     eventManager.UI_EVENT_MANAGER::~UI_EVENT_MANAGER();
  234.     delete display;
  235.     return (0);
  236. }
  237.  
  238.