home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / uidemo / bigstrvw / bigstrv.cxx < prev    next >
C/C++ Source or Header  |  1995-02-26  |  5KB  |  162 lines

  1.  
  2.  
  3. #include "bigstrv.h"
  4.  
  5. #include "ui/dsplsurf.h"
  6. #include "ui/font.h"
  7. #include "ui/cntroler.h"
  8.  
  9. typedef CL_Binding<BigStringView> Bind;
  10.  
  11. #if defined(__GNUC__)
  12. template class CL_Binding<BigStringView>;
  13. #endif
  14.  
  15. const short scrollBarWidth = 20;
  16. const UI_ViewID ID_BAR = 101;
  17.  
  18. BigStringView::BigStringView
  19.     (UI_VObjCollection* parent, const UI_Rectangle& shape, 
  20.      CL_StringSequence& model, UI_ViewID id)
  21. : UI_VObjCollection (parent, shape, id), _modelSeq (model)
  22. {
  23.     Bind bind (this, &BigStringView::_ModelChanged);
  24.     _modelSeq.AddDependent (bind, 1);
  25.  
  26.     UI_Rectangle scrollBarRect (shape.Width () - scrollBarWidth, 0,
  27.                                 scrollBarWidth, shape.Height ());
  28.     _scrollBar = new UI_VScrollBar (this, scrollBarRect, ID_BAR);
  29.     Bind scrollBind (this, &BigStringView::_Scroll);
  30.     _scrollBar->ClientSet().Add (scrollBind, 1);
  31.  
  32.     _selection = -1;
  33. }
  34.  
  35. void BigStringView::Initialize ()
  36. {
  37.     _visibleCount    = 0;
  38. }
  39.  
  40. void BigStringView::_SetupScrollBar ()
  41. {
  42.     long size = _modelSeq.Size();
  43.     _scrollBar->SetVisibility (_visibleCount < size ? TRUE : FALSE);
  44.     _scrollBar->PageAmount() = _visibleCount-1;
  45.     _scrollBar->Range () = CL_Interval (0, maxl (_visibleCount,
  46.                                                  size - _visibleCount));
  47.     CL_Interval& scrollInterval = (CL_Interval&) _scrollBar->Model();
  48.     long low = minl (scrollInterval.Low(), size-1);
  49.     scrollInterval = CL_Interval (low, low + _visibleCount - 1);
  50. }
  51.  
  52. #include <iostream.h>
  53. bool BigStringView::_DrawClientArea ()
  54. {
  55.     UI_DisplaySurface& sfc = CreateDisplaySurface ();
  56.     short charHeight       = Font().Height();
  57.     if (charHeight <= 1) { // Something wrong
  58.         DestroyDisplaySurface ();
  59.         return FALSE;
  60.     }
  61.  
  62.     // Determine the box in which the strings will be drawn
  63.     short boxHeight = _shape.Height() - 2;
  64.     UI_Rectangle box = UI_Rectangle (0, 0, _shape.Width(), boxHeight);
  65.     long modelSize  = _modelSeq.Size();
  66.     if (!_visibleCount) {
  67.         // Very first paint event
  68.         _visibleCount   = boxHeight/charHeight;
  69.         _SetupScrollBar ();
  70.     }
  71.     if (_visibleCount < modelSize)
  72.         box.AddToWidth (-scrollBarWidth);
  73.     
  74.     sfc.Brush().Color   (UIColor_White);
  75.     sfc.Brush().Pattern (UIBrush_Solid);
  76.     sfc.DrawRectangle  (box, UID_Outline | UID_Fill);
  77.  
  78.     // Draw the strings
  79.     long topElement = _visibleCount >= modelSize ? 0
  80.         : ((CL_Interval&) _scrollBar->Model ()).Low ();
  81.     short yOffset = 0;
  82.     short count = minl (topElement + _visibleCount, modelSize);
  83.     short textWidth = box.Width()-2; // text rectangle is narrower than box
  84.     for (long i = topElement; i < count
  85.          && yOffset + charHeight <= boxHeight; i++, yOffset += charHeight) {
  86.         UI_Rectangle textRect (2, yOffset, textWidth, charHeight);
  87.         sfc.WriteString (_modelSeq[i], textRect);
  88.     }
  89.  
  90.     // Invert the selected string, if any
  91.     if (_selection >= 0) {
  92.         short index = _selection - topElement;
  93.         if (index >= 0 && index < _visibleCount) {
  94.             UI_Rectangle selectRect (2, index * charHeight,
  95.                                      textWidth, charHeight);
  96.             sfc.InvertRectangle (selectRect);
  97.         }
  98.     }
  99.  
  100.     // All done
  101.     DestroyDisplaySurface ();
  102.     return TRUE;
  103. }
  104.  
  105. bool BigStringView::_ModelChanged (CL_Object&, long)
  106. {
  107.     _SetupScrollBar ();
  108.     _DrawClientArea ();
  109.     return TRUE;
  110. }
  111.  
  112. bool BigStringView::_Scroll (CL_Object&, long)
  113. {
  114.     _DrawClientArea ();
  115.     return TRUE;
  116. }
  117.  
  118. bool BigStringView::Paint ()
  119. {
  120.     return _DrawClientArea ();
  121. }
  122.  
  123. bool BigStringView::ButtonDown (const UI_Point& position, UI_MouseButton btn,
  124.                                 bool, bool)
  125. {
  126.     if (btn != UIM_Left)
  127.         return FALSE;
  128.     short charHeight = Font().Height();
  129.     if (charHeight <= 1)
  130.         return FALSE;  // Safety check
  131.     short index = position.YCoord() / charHeight;
  132.     if (index >= _visibleCount)
  133.         return FALSE;
  134.     long topElement = _visibleCount >= _modelSeq.Size() ? 0
  135.         : ((CL_Interval&) _scrollBar->Model()).Low();
  136.     long newSelection = topElement + index;
  137.     if (newSelection >= _modelSeq.Size())
  138.         return FALSE;
  139.     if (newSelection != _selection) {
  140.         UI_DisplaySurface& sfc = CreateDisplaySurface ();
  141.         short charHeight       = Font().Height();
  142.         short width = _shape.Width()-2;
  143.         if (_scrollBar->IsVisible())
  144.             width -= scrollBarWidth;
  145.         if (_selection >= topElement &&
  146.             _selection < topElement + _visibleCount) {
  147.             // Old selection exists and is visible, so de-highlight it
  148.             UI_Rectangle oldSelRect (2, (_selection - topElement) * charHeight,
  149.                                      width, charHeight);
  150.             sfc.InvertRectangle (oldSelRect);
  151.         }                                     
  152.         _selection = newSelection;
  153.         UI_Rectangle selRect (2, (newSelection - topElement) * charHeight,
  154.                               width, charHeight);
  155.         sfc.InvertRectangle (selRect);
  156.     }
  157.  
  158.     _Controller->AddEvent (new UI_Event (Event_Select, this, this));
  159.     return TRUE;
  160. }
  161.  
  162.