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

  1. // ---------- toolbutt.cpp
  2.  
  3. #include "toolbar.h"
  4. #include "desktop.h"
  5.  
  6. // ------- various color patterns
  7. static Color EnabledColor = {
  8.     LIGHTGRAY,            // fg
  9.     ToolBarBG,            // bg
  10.     WHITE,                // selected fg
  11.     ToolBarBG,            // selected bg
  12.     CYAN,                 // frame fg
  13.     ToolBarBG,            // frame bg
  14.     WHITE,                // highlighted fg
  15.     ToolBarBG             // highlighted bg
  16. };
  17.  
  18. static Color PressedColor = {
  19.     BLACK,                // fg
  20.     ToolBarBG,            // bg
  21.     ToolBarBG,            // selected fg
  22.     ToolBarBG,            // selected bg
  23.     CYAN,                 // frame fg
  24.     ToolBarBG,            // frame bg
  25.     ToolBarBG,            // highlighted fg
  26.     ToolBarBG             // highlighted bg
  27. };
  28.  
  29. static Color DisabledColor = {
  30.     BLACK,                // fg
  31.     ToolBarBG,            // bg
  32.     BLACK,                // selected fg
  33.     ToolBarBG,            // selected bg
  34.     BLACK,                // frame fg
  35.     ToolBarBG,            // frame bg
  36.     BLACK,                // highlighted fg
  37.     ToolBarBG             // highlighted bg
  38. };
  39.  
  40. // ---- pressed and unpressed frame corner characters
  41. const int PRESSED_NE   = 184;
  42. const int PRESSED_SW   = 211;
  43. const int UNPRESSED_NE = 183;
  44. const int UNPRESSED_SW = 212;
  45.  
  46. // ----- button frame when pressed
  47. static BoxLines PressedBorder = {
  48.     FOCUS_NW,
  49.     FOCUS_LINE,
  50.     PRESSED_NE,
  51.     SIDE,
  52.     SE,
  53.     LINE,
  54.     PRESSED_SW,
  55.     FOCUS_SIDE,
  56. };
  57.  
  58. // ----- button frame when not pressed
  59. static BoxLines UnPressedBorder = {
  60.     NW,
  61.     LINE,
  62.     UNPRESSED_NE,
  63.     FOCUS_SIDE,
  64.     FOCUS_SE,
  65.     FOCUS_LINE,
  66.     UNPRESSED_SW,
  67.     SIDE
  68. };
  69.  
  70. // --- common constructor code
  71. void ToolButton::InitWindow(const char *lbl)
  72. {
  73.     oldFocus = 0;
  74.     windowtype = ToolButtonWindow;
  75.     Size(Left()+5, Top()+2);
  76.     ClearAttribute(SHADOW);
  77.     SetAttribute(BORDER);
  78.     SetText(lbl);
  79.     SetColors();
  80. }
  81.  
  82. // --------- construct a Toolbar button specifying position
  83. ToolButton::ToolButton(const char *lbl, int lf, int tp, DFWindow *par)
  84.                 : PushButton(lbl, lf, tp, par)
  85. {
  86.     InitWindow(lbl);
  87. }
  88.  
  89. // ------ construct a Toolbar button, self-positioning
  90. ToolButton::ToolButton(const char *lbl, DFWindow *par)
  91.                 : PushButton(lbl, 0, 0, par)
  92. {
  93.     InitWindow(lbl);
  94.     if (par != 0 && par->WindowType() == ToolbarWindow)    {
  95.         int btcount = 0;
  96.         DFWindow *Wnd = par->First();
  97.         while (Wnd != 0 && Wnd != this)    {
  98.             if (Wnd->WindowType() == ToolButtonWindow)
  99.                 btcount++;
  100.             Wnd = Wnd->Next();
  101.         }
  102.         Move(par->Left()+btcount*6, par->Top());
  103.     }
  104. }
  105.  
  106. // ----- draw the button's frame
  107. void ToolButton::Border()
  108. {
  109.     if (visible)    {
  110.         if (pressed)    {
  111.             colors = PressedColor;
  112.             DrawBorder(PressedBorder);
  113.         }
  114.         else    {
  115.             SetColors();
  116.             DrawBorder(UnPressedBorder);
  117.         }
  118.     }
  119. }
  120.  
  121. // -------- set the button's colors
  122. void ToolButton::SetColors()
  123. {
  124.     if (isEnabled())
  125.         colors = EnabledColor;
  126.     else
  127.         colors = DisabledColor;
  128. }
  129.  
  130. // ------- paint the button
  131. void ToolButton::Paint()
  132. {
  133.     SetColors();
  134.     TextBox::Paint();
  135. }
  136.  
  137. // -------- the button was pressed
  138. void ToolButton::ButtonCommand()
  139. {
  140.     PushButton::ButtonCommand();
  141.     if (oldFocus != 0)
  142.         oldFocus->SetFocus();
  143.     oldFocus = 0;
  144. }
  145.  
  146. // ---- remember who had the focus before the button got it
  147. Bool ToolButton::SetFocus()
  148. {
  149.     if (oldFocus == 0)
  150.         oldFocus = desktop.InFocus();
  151.     return PushButton::SetFocus();
  152. }
  153.  
  154.