home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 20.ddi / DOCVIEW.PAK / ODLISTBX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  5.0 KB  |  161 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   Implements class TODListBox
  4. //----------------------------------------------------------------------------
  5. #include <owl\owlpch.h>
  6. #include "odlistbx.h"
  7.  
  8. TODListBox::TODListBox(int id)
  9.           : TControl(0, id, 0, 0,0, 100,100), BufTemp(0), BufLen(0), MaxWidth(0)
  10. {
  11.   Attr.Style = (Attr.Style & ~(LBS_SORT | LBS_HASSTRINGS | LBS_MULTIPLESEL))
  12.   | (LBS_NOTIFY | WS_VSCROLL | WS_HSCROLL | WS_BORDER | LBS_OWNERDRAWVARIABLE);
  13. }
  14.  
  15. char far *
  16. TODListBox::GetClassName()
  17. {
  18.   return "LISTBOX";
  19. }
  20.  
  21. void TODListBox::ItemRedraw(int index)   // force item to be redrawn
  22. {
  23.   TRect rect;
  24.   if (GetItemRect(index, rect) <= 0) return;   // return 1 if OK, -1 if error
  25.   InvalidateRect (rect);
  26. }
  27.  
  28. void TODListBox::DrawItem(DRAWITEMSTRUCT far & dis)
  29. {
  30.   ODItemInfo item;
  31.   UINT action = dis.itemAction;
  32.   item.Hdc = dis.hDC;
  33.   item.State = dis.itemState;
  34.   item.Data  = (void far*) dis.itemData;
  35.   item.Index = dis.itemID;
  36.   item.Bound.Set(dis.rcItem.left, dis.rcItem.top,
  37.                  dis.rcItem.right,dis.rcItem.bottom);
  38.  
  39.   if (item.Index < 0)
  40.     return; // ignore if empty listbox, no focus rect?
  41.   GetItemInfo(item);       // must fill in: Extent,Text,TextLen,Offset
  42.   if (action & ODA_DRAWENTIRE){
  43. //   // shift the item rect to account for horizontal scrolling
  44. //if (item.Bound.right > clientrect.right)
  45. //      item.Bound.left = -(item.Bound.right - clientrect.right);
  46.   if (item.Extent.cx > MaxWidth) {
  47.     SetHorizontalExtent(MaxWidth = item.Extent.cx);
  48.   }
  49.   DrawItemData(item);      
  50.   action = 0;  // redraw destoys previous focus and selection painting
  51.   if (item.State & ODS_SELECTED) action |= ODA_SELECT;
  52.   if (item.State & ODS_FOCUS)    action |= ODA_FOCUS;
  53.   }
  54.   if (action & ODA_SELECT) {
  55.     ChangeHilight(item);
  56.   }
  57.   if (action & ODA_FOCUS){
  58.    item.Bound.right = item.Extent.cx;  // focus drawn around entire item
  59.    ChangeFocus(item);
  60.   }
  61. }
  62.  
  63. void TODListBox::MeasureItem (MEASUREITEMSTRUCT far& mis)
  64. {
  65.   ODItemInfo item;
  66.   item.Hdc = ::GetDC(*this);
  67.   item.Data  = (void far*) mis.itemData;
  68.   item.Index = mis.itemID;
  69.   item.State = 0;
  70.   GetItemInfo(item);
  71.   mis.itemHeight = item.Extent.cy;
  72.   mis.itemWidth  = item.Extent.cx;
  73.   if (item.Extent.cx > MaxWidth) {
  74.      SetHorizontalExtent(MaxWidth = item.Extent.cx);
  75.   }
  76.   ::ReleaseDC(*this, item.Hdc);
  77. }
  78.  
  79. // The following methods are defaults for use with text string items only.
  80. // These must be overridden if for data items that are not standard text.
  81. // If the style LBS_HASSTRINGS is set, strings are stored within the list box.
  82. // Otherwise only the pointers are stored; data must be managed by the caller.
  83.  
  84. BOOL TODListBox::GetItemInfo(ODItemInfo& item) {
  85.   LPSTR str;
  86.   int len;
  87.   if (Attr.Style & LBS_HASSTRINGS) {   // strings stored in list box
  88.     len = GetStringLen(item.Index);
  89.     if (len == 0) {
  90.       str = 0;
  91.     } else {
  92.       if (len >= BufLen){  // check temporary buffer size
  93.         if (BufTemp) delete BufTemp;
  94.         BufTemp = new char[BufLen = len+1];
  95.       }
  96.       GetString(str = BufTemp, item.Index);
  97.     }
  98.   }else{ // assume text pointer is allocated data stored in item data     
  99.     str = (char far*)item.Data;
  100.     len = str ? lstrlen(str) : 0;
  101.   }
  102.   item.TextLen = len;
  103.   item.Text = str;
  104.   if (!len) {
  105.     str = " ";     // must have text to measure
  106.     len++;
  107.   }
  108.   GetTextExtentPoint(item.Hdc, str, len, &item.Extent);
  109.   item.Extent.cx += 2; // room for focus rectangle
  110.   item.Extent.cy += 2; // room for focus rectangle
  111.   item.Offset.x = 1;   // room for focus rectangle, no indentation
  112.   item.Offset.y = 1;   // room for focus rectangle
  113.   return TRUE;         // OK to draw
  114. }
  115.  
  116. void TODListBox::ChangeHilight(ODItemInfo& item)
  117. {
  118.   InvertRect(item.Hdc, &item.Bound);  // need to fix to do nicer!!
  119. }
  120.  
  121. void TODListBox::ChangeFocus(ODItemInfo& item)
  122. {
  123.   int brushtype = item.State & ODS_FOCUS ? LTGRAY_BRUSH
  124.                 :(item.State & ODS_SELECTED ? BLACK_BRUSH : WHITE_BRUSH);
  125.   FrameRect(item.Hdc, &item.Bound, (HBRUSH)GetStockObject(brushtype));
  126. }
  127.  
  128. void TODListBox::DrawItemData(ODItemInfo& item)
  129. {
  130. //HFONT oldfont = SelectObject(item.Hdc, StdFont);  // need member HFONT StdFont
  131.   ExtTextOut(item.Hdc, 
  132.          item.Bound.left + item.Offset.x,
  133.          item.Bound.top  + item.Offset.y,
  134.          ETO_CLIPPED | ETO_OPAQUE, 
  135.          &item.Bound,
  136.          item.Text, item.TextLen,
  137.          (LPINT) NULL); /* default char spacing */ 
  138. //SelectObject(item.Hdc, oldfont);
  139. }
  140.  
  141. IMPLEMENT_STREAMABLE1(TODListBox, TControl);
  142.  
  143. void*
  144. TODListBox::Streamer::Read(ipstream& is, uint32 /*version*/) const
  145. {
  146.   ReadBaseObject((TControl*)GetObject(), is);
  147.   is >> GetObject()->MaxWidth;
  148.   GetObject()->BufTemp = 0;
  149.   GetObject()->BufLen  = 0;
  150.   return GetObject();
  151. }
  152.  
  153. void
  154. TODListBox::Streamer::Write(opstream& os) const
  155. {
  156.   WriteBaseObject((TControl*)GetObject(), os);
  157.   os << GetObject()->MaxWidth;
  158. }
  159.  
  160.  
  161.