home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / PBUTTON.CPP < prev    next >
C/C++ Source or Header  |  1993-09-23  |  3KB  |  162 lines

  1. // ------------- pbutton.cpp
  2.  
  3. #include <ctype.h>
  4. #include "pbutton.h"
  5. #include "desktop.h"
  6.  
  7. static Color col = {
  8.     BLACK,                // fg
  9.     CYAN,                // bg
  10.     WHITE,                // selected fg
  11.     CYAN,                // selected bg
  12.     BLACK,                // frame fg
  13.     CYAN,                // frame bg
  14.     DARKGRAY,            // highlighted fg
  15.     CYAN                // highlighted bg
  16. };
  17.  
  18. PushButton::PushButton(const char *lbl, int lf, int tp, DFWindow *par)
  19.                 : TextBox(lf, tp, 1, strlen(lbl)+1, par)
  20. {
  21.     windowtype = PushButtonWindow;
  22.     SetAttribute(SHADOW);
  23.     pressed = False;
  24.     cmdfunction = 0;
  25.     String lb(" ");
  26.     lb += lbl;
  27.     lb += " ";
  28.     SetText(lb);
  29.     char *ac = strchr(lbl, SHORTCUTCHAR);
  30.     if (ac != 0)
  31.         shortcut = tolower(*(ac+1));
  32.     colors = col;
  33.     shortcutfg = RED;
  34. }
  35.  
  36.  
  37. void PushButton::Paint()
  38. {
  39.     if (visible)    {
  40.         COLORS fg;
  41.         COLORS bg;
  42.         if (!pressed)    {
  43.             if (isEnabled())    {
  44.                 if (this == desktop.InFocus())
  45.                     fg = colors.sfg;
  46.                 else
  47.                     fg = colors.fg;
  48.                 WriteShortcutLine(0, fg, colors.bg);
  49.             }
  50.             else 
  51.                 WriteTextLine(0, colors.hfg, colors.hbg);
  52.         }
  53.         else     {
  54.             // ---- display a pressed button
  55.             fg = ClientBG();
  56.             bg = Parent()->ClientBG();
  57.             int wd = Width();
  58.             WriteWindowChar(' ', 0, 0, fg, bg);
  59.             for (int x = 0; x < wd; x++)    {
  60.                 WriteWindowChar(220, x+1, 0, fg, bg);
  61.                 WriteWindowChar(223, x+1, 1, fg, bg);
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. void PushButton::Shadow()
  68. {
  69.     if (visible && !pressed && (attrib & SHADOW))    {
  70.         COLORS bg = Parent()->ClientBG();
  71.         int wd = Width();
  72.         WriteWindowChar(220, wd, 0, BLACK, bg);
  73.         for (int x = 1; x <= wd; x++)
  74.             WriteWindowChar(223, x, 1, BLACK, bg);
  75.     }
  76. }
  77.  
  78. Bool PushButton::SetFocus()
  79. {
  80.     TextBox::SetFocus();
  81.     Paint();
  82.     return True;
  83. }
  84.  
  85. void PushButton::ResetFocus()
  86. {
  87.     TextBox::ResetFocus();
  88.     Show();
  89. }
  90.  
  91. void PushButton::Keyboard(int key)
  92. {
  93.     if (key == '\r')
  94.         PressButton();
  95.     else 
  96.         TextBox::Keyboard(key);
  97. }
  98.  
  99. void PushButton::LeftButton(int mx, int my)
  100. {
  101.     if (ClientRect().Inside(mx,my))    {
  102.         PressButton();
  103.         CaptureFocus();
  104.     }
  105. }
  106.  
  107. void PushButton::MouseMoved(int mx, int my)
  108. {
  109.     if (desktop.FocusCapture() == this)
  110.         if (ClientRect().Inside(mx,my))
  111.             PressButton();
  112.         else
  113.             ReleaseButton();
  114. }
  115.  
  116. void PushButton::ButtonReleased(int, int)
  117. {
  118.     ReleaseFocus();
  119.     ButtonCommand();
  120. }
  121.  
  122. void PushButton::KeyReleased()
  123. {
  124.     ButtonCommand();
  125. }
  126.  
  127. void PushButton::ButtonCommand()
  128. {
  129.     if (pressed)    {
  130.         ReleaseButton();
  131.         if (cmdfunction != 0 && owner != 0)
  132.             (owner->*cmdfunction)();
  133.     }
  134. }
  135.  
  136. void PushButton::PressButton()
  137. {
  138.     if (!pressed)    {
  139.         pressed = True;
  140.         Show();
  141.     }
  142. }
  143.  
  144. void PushButton::ReleaseButton()
  145. {
  146.     if (pressed)    {
  147.         pressed = False;
  148.         Show();
  149.     }
  150. }
  151.  
  152. void PushButton::ShortcutSelect()
  153. {
  154.     Control::ShortcutSelect();
  155.     PressButton();
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.