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

  1. // ------------- scrolbar.cpp
  2.  
  3. #include "desktop.h"
  4. #include "textbox.h"
  5. #include "scrolbar.h"
  6.  
  7. ScrollBar::ScrollBar(BarPlane Plane, TextBox *par)
  8.                         : DFWindow(1, 1, par)
  9. {
  10.     windowtype = ScrollbarWindow;
  11.     if (par == 0)
  12.         return;
  13.     scrollbox = 1;
  14.     sliding = False;
  15.     SetClearChar(SCROLLBARCHAR);
  16.     plane = Plane;
  17.     if (plane == HORIZONTAL)    {
  18.         rect.Left() = par->Left()+1;
  19.         rect.Right() = par->Right()-1;
  20.         rect.Top() = rect.Bottom() = par->Bottom();
  21.     }
  22.     else    {
  23.         rect.Left() = rect.Right() = par->Right();
  24.         rect.Top() = par->Top()+1;
  25.         rect.Bottom() = par->Bottom()-1;
  26.     }
  27.     colors.fg = par ? par->FrameFG() : 0;
  28.     colors.bg = par ? par->FrameBG() : 0;
  29. }
  30.  
  31. Bool ScrollBar::SetFocus()
  32. {
  33.     if (Parent() != 0)
  34.         Parent()->SetFocus();
  35.     return True;
  36. }
  37.  
  38. void ScrollBar::Paint()
  39. {
  40.     if (visible)    {
  41.         int fg = colors.fg;
  42.         int bg = colors.bg;
  43.         DFWindow::Paint();
  44.         if (plane == HORIZONTAL)    {
  45.             WriteWindowChar(LEFTSCROLLBOX, 0, 0, fg, bg);
  46.             WriteWindowChar(RIGHTSCROLLBOX, Width()-1, 0, fg, bg);
  47.             WriteWindowChar(SCROLLBOXCHAR, scrollbox, 0, fg, bg);
  48.         }
  49.         else    {
  50.             WriteWindowChar(UPSCROLLBOX, 0, 0, fg, bg);
  51.             WriteWindowChar(DOWNSCROLLBOX, 0, Height()-1, fg, bg);
  52.             WriteWindowChar(SCROLLBOXCHAR, 0, scrollbox, fg, bg);
  53.         }
  54.     }
  55. }
  56.  
  57. void ScrollBar::LeftButton(int mx, int my)
  58. {
  59.     if (Parent() != 0 && !sliding)    {
  60.         TextBox &Par = *(TextBox *)Parent();
  61.         if (plane == VERTICAL)    {
  62.             // -- test for hitting the vertical scroll buttons
  63.             if (my == rect.Top())
  64.                 Par.ScrollDown();
  65.             else if (my == rect.Bottom())
  66.                 Par.ScrollUp();
  67.             // ------- test for hitting the vertical scroll box
  68.             else if (my-rect.Top() == scrollbox)    {
  69.                 sliding = True;
  70.                 desktop.mouse().SetTravel(rect.Left(), rect.Right(),
  71.                                 rect.Top()+1, rect.Bottom()-1);
  72.             }
  73.             else    {
  74.                 // ----- hit in the scroll bar
  75.                 if (my-rect.Top() < scrollbox)
  76.                     Par.PageUp();
  77.                 else
  78.                     Par.PageDown();
  79.             }
  80.         }
  81.         else    {
  82.             // -- test for hitting the horizontal scroll buttons
  83.             if (mx == rect.Left())
  84.                 Par.ScrollRight();
  85.             else if (mx == rect.Right())
  86.                 Par.ScrollLeft();
  87.             // ------- test for hitting the horizontal scroll box
  88.             else if (mx-rect.Left() == scrollbox)    {
  89.                 sliding = True;
  90.                 desktop.mouse().SetTravel(rect.Left()+1, rect.Right()-1,
  91.                                 rect.Top(), rect.Bottom());
  92.             }
  93.             else    {
  94.                 // ----- hit in the scroll bar
  95.                 if (mx-rect.Left() < scrollbox)
  96.                     Par.PageLeft();
  97.                 else
  98.                     Par.PageRight();
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104. void ScrollBar::MouseMoved(int mx, int my)
  105. {
  106.     if (sliding)    {
  107.         if (plane == VERTICAL)
  108.             MoveScrollBox(my - rect.Top());
  109.         else
  110.             MoveScrollBox(mx - rect.Left());
  111.     }
  112. }
  113.  
  114. void ScrollBar::ButtonReleased(int, int)
  115. {
  116.     if (sliding && Parent() != 0)    {
  117.         TextBox &Par = *(TextBox *)Parent();
  118.         desktop.mouse().SetTravel(0, desktop.screen().Width()-1, 0, desktop.screen().Height()-1);
  119.         int pct = (scrollbox-1)*100;
  120.         if (plane == VERTICAL)    {
  121.             pct /= (rect.Height()-2);
  122.             Par.VerticalPagePosition(pct);
  123.         }
  124.         else    {
  125.             pct /= (rect.Width()-2);
  126.             Par.HorizontalPagePosition(pct);
  127.         }
  128.     }
  129.     sliding = False;
  130. }
  131.  
  132. void ScrollBar::ParentSized(int xdif, int ydif)
  133. {
  134.     if (plane == HORIZONTAL)    {
  135.         Size(Right()+xdif, Top());
  136.         Move(Left(), Bottom()+ydif);
  137.     }
  138.     else    {
  139.         Size(Left(), Bottom()+ydif);
  140.         Move(Right()+xdif, Top());
  141.     }
  142. }
  143.  
  144. void ScrollBar::TextPosition(int TxPct)
  145. {
  146.     int len = plane == HORIZONTAL ? Width()-2 : Height()-2;
  147.     int sb = 1 + ((len * TxPct) / 100);
  148.     MoveScrollBox(sb);
  149. }
  150.  
  151. void ScrollBar::MoveScrollBox(int sb)
  152. {
  153.     if (sb != scrollbox)    {
  154.         int fg = colors.fg;
  155.         int bg = colors.bg;
  156.         int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  157.         if (plane == HORIZONTAL)
  158.             x1 = sb, x2 = scrollbox;
  159.         else
  160.             y1 = sb, y2 = scrollbox;
  161.         WriteWindowChar(SCROLLBOXCHAR, x1, y1, fg, bg);
  162.         WriteWindowChar(SCROLLBARCHAR, x2, y2, fg, bg);
  163.         scrollbox = sb;
  164.     }
  165. }
  166.  
  167.