home *** CD-ROM | disk | FTP | other *** search
/ Beginning Direct3D Game Programming / Direct3D.iso / directx / dxf / samples / multimedia / directinput / diconfig / flexlistbox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-22  |  13.1 KB  |  576 lines

  1. //-----------------------------------------------------------------------------
  2. // File: flexlistbox.cpp
  3. //
  4. // Desc: Implements a list box control that can display a list of text strings,
  5. //       each can be selected by mouse.  The class CFlexListBox is derived from
  6. //       CFlexWnd.  It is used by the class CFlexComboBox when it needs to
  7. //       expand to show the list of choices.
  8. //
  9. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  10. //-----------------------------------------------------------------------------
  11.  
  12. #include "common.hpp"
  13.  
  14.  
  15. CFlexListBox::CFlexListBox() :
  16.     m_nTextHeight(-1),
  17.     m_hWndNotify(NULL),
  18.     m_rgbText(RGB(255,255,255)),
  19.     m_rgbBk(RGB(0,0,0)),
  20.     m_rgbSelText(RGB(0,0,255)),
  21.     m_rgbSelBk(RGB(0,0,0)),
  22.     m_rgbFill(RGB(0,0,255)),
  23.     m_rgbLine(RGB(0,255,255)),
  24.     m_dwFlags(0),
  25.     m_nTopIndex(0),
  26.     m_nSBWidth(11),
  27.     m_hFont(NULL),
  28.     m_bOpenClick(FALSE),
  29.     m_bDragging(FALSE),
  30.     m_bCapture(FALSE),
  31.     m_nSelItem(-1),
  32.     m_bVertSB(FALSE)
  33. {
  34. }
  35.  
  36. CFlexListBox::~CFlexListBox()
  37. {
  38. }
  39.  
  40. CFlexListBox *CreateFlexListBox(FLEXLISTBOXCREATESTRUCT *pcs)
  41. {
  42.     CFlexListBox *psb = new CFlexListBox;
  43.  
  44.     if (psb && psb->Create(pcs))
  45.         return psb;
  46.     
  47.     delete psb;
  48.     return NULL;
  49. }
  50.  
  51. BOOL CFlexListBox::CreateForSingleSel(FLEXLISTBOXCREATESTRUCT *pcs)
  52. {
  53.     if (!Create(pcs))
  54.         return FALSE;
  55.  
  56.     StartSel();
  57.  
  58.     return TRUE;
  59. }
  60.  
  61. BOOL CFlexListBox::Create(FLEXLISTBOXCREATESTRUCT *pcs)
  62. {
  63.     if (this == NULL)
  64.         return FALSE;
  65.  
  66.     if (m_hWnd)
  67.         Destroy();
  68.  
  69.     if (pcs == NULL)
  70.         return FALSE;
  71.  
  72.     if (pcs->dwSize != sizeof(FLEXLISTBOXCREATESTRUCT))
  73.         return FALSE;
  74.  
  75.     m_hWndNotify = pcs->hWndNotify ? pcs->hWndNotify : pcs->hWndParent;
  76.  
  77.     m_dwFlags = pcs->dwFlags;
  78.  
  79.     SetFont(pcs->hFont);
  80.     SetColors(pcs->rgbText, pcs->rgbBk, pcs->rgbSelText, pcs->rgbSelBk, pcs->rgbFill, pcs->rgbLine);
  81.     m_VertSB.SetColors(pcs->rgbBk, pcs->rgbFill, pcs->rgbLine);
  82.     m_nSBWidth = pcs->nSBWidth;
  83.  
  84.     if (!CFlexWnd::Create(pcs->hWndParent, pcs->rect, FALSE))
  85.         return FALSE;
  86.  
  87.     FLEXSCROLLBARCREATESTRUCT sbcs;
  88.     sbcs.dwSize = sizeof(FLEXSCROLLBARCREATESTRUCT);
  89.     sbcs.dwFlags = FSBF_VERT;
  90.     sbcs.min = 0;
  91.     sbcs.max = 3;
  92.     sbcs.page = 1;
  93.     sbcs.pos = 1;
  94.     sbcs.hWndParent = m_hWnd;
  95.     sbcs.hWndNotify = m_hWnd;
  96.     RECT rect = {0, 0, 1, 1};
  97.     sbcs.rect = rect;
  98.     sbcs.bVisible = FALSE;
  99.     m_VertSB.Create(&sbcs);
  100.  
  101.     Calc();
  102.  
  103.     // show if we want it shown
  104.     if (pcs->bVisible)
  105.         ShowWindow(m_hWnd, SW_SHOW);
  106.     if (m_bVertSB)
  107.         SetVertSB();
  108.  
  109.     // TODO:  make sure that creation sends no notifications.
  110.     // all initial notifications should be sent here.
  111.  
  112.     return TRUE;
  113. }
  114.  
  115. void CFlexListBox::Calc()
  116. {
  117.     // handle getting text height
  118.     if (m_nTextHeight == -1)
  119.     {
  120.         m_nTextHeight = GetTextHeight(m_hFont);
  121.         Invalidate();
  122.         assert(m_nTextHeight != -1);
  123.     }
  124.  
  125.     // don't do the rest unless we've been created
  126.     if (m_hWnd == NULL)
  127.         return;
  128.  
  129.     // handle integral height
  130.     int iUsedHeight = m_ItemArray.GetSize() * m_nTextHeight;
  131.     // If more than max height, use the max height
  132.     if (iUsedHeight > g_UserNamesRect.bottom - g_UserNamesRect.top)
  133.         iUsedHeight = g_UserNamesRect.bottom - g_UserNamesRect.top;
  134.  
  135.     SIZE client = GetClientSize();
  136.     int fit = iUsedHeight / m_nTextHeight;
  137.     if (fit < 1)
  138.         fit = 1;
  139.     int setheight = (m_dwFlags & FLBF_INTEGRALHEIGHT) ? fit * m_nTextHeight : iUsedHeight;
  140.     if (setheight != client.cy)
  141.         SetWindowPos(m_hWnd, NULL, 0, 0, client.cx, setheight,
  142.             SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
  143.  
  144.     // handle scroll bar
  145.     SetVertSB(m_ItemArray.GetSize() > fit);
  146. }
  147.  
  148. void CFlexListBox::SetFont(HFONT hFont)
  149. {
  150.     m_hFont = hFont;
  151.     m_nTextHeight = -1;
  152.     Calc();
  153. }
  154.  
  155. void CFlexListBox::OnPaint(HDC hDC)
  156. {
  157.     HDC hBDC = NULL, hODC = NULL;
  158.     CBitmap *pbm = NULL;
  159.  
  160.     if (!InRenderMode())
  161.     {
  162.         hODC = hDC;
  163.         pbm = CBitmap::Create(GetClientSize(), RGB(0,0,0), hDC);
  164.         if (pbm != NULL)
  165.         {
  166.             hBDC = pbm->BeginPaintInto();
  167.             if (hBDC != NULL)
  168.             {
  169.                 hDC = hBDC;
  170.             }
  171.         }
  172.     }
  173.  
  174.     InternalPaint(hDC);
  175.  
  176.     if (!InRenderMode())
  177.     {
  178.         if (pbm != NULL)
  179.         {
  180.             if (hBDC != NULL)
  181.             {
  182.                 pbm->EndPaintInto(hBDC);
  183.                 pbm->Draw(hODC);
  184.             }
  185.             delete pbm;
  186.         }
  187.     }
  188. }
  189.  
  190. void CFlexListBox::InternalPaint(HDC hDC)
  191. {
  192.     if (m_nTextHeight == -1)
  193.         return;
  194.  
  195.     SIZE client = GetClientSize();
  196.     RECT rc = {0,0,0,0};
  197.     GetClientRect(&rc);
  198.  
  199.     HGDIOBJ hPen, hOldPen, hBrush, hOldBrush;
  200.     hPen= (HGDIOBJ)CreatePen(PS_SOLID, 1, m_rgbBk);
  201.     if (hPen != NULL)
  202.     {
  203.         hOldPen = SelectObject(hDC, hPen);
  204.  
  205.         hBrush = CreateSolidBrush(m_rgbBk);
  206.         if (hBrush != NULL)
  207.         {
  208.             hOldBrush = SelectObject(hDC, hBrush);
  209.  
  210.             Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);  // Paint entire window black first.
  211.  
  212.             if (!m_bVertSB)
  213.                 m_nTopIndex = 0;
  214.  
  215.             int iLastY;
  216.             for (int at = 0, i = m_nTopIndex; at < client.cy; i++, at += m_nTextHeight)
  217.             {
  218.                 RECT rect = {0, at, client.cx, at + m_nTextHeight};
  219.  
  220.                 if (i < m_ItemArray.GetSize())
  221.                 {
  222.                     BOOL bSel = m_ItemArray[i].bSelected;
  223.                     SetTextColor(hDC, bSel ? m_rgbSelText : m_rgbText);
  224.                     SetBkColor(hDC, bSel ? m_rgbSelBk : m_rgbBk);
  225.                     DrawText(hDC, m_ItemArray[i].GetText(), -1, &rect, DT_NOPREFIX);
  226.                     iLastY = at + m_nTextHeight;
  227.                 }
  228.             }
  229.  
  230.             SelectObject(hDC, hOldBrush);
  231.             DeleteObject(hBrush);
  232.         }
  233.  
  234.         SelectObject(hDC, hOldPen);
  235.         DeleteObject(hPen);
  236.     }
  237.  
  238.     // Draw an outline around the box
  239.     hPen = (HGDIOBJ)CreatePen(PS_SOLID, 1, m_rgbLine);
  240.     if (hPen != NULL)
  241.     {
  242.         hOldPen = SelectObject(hDC, hPen);
  243.         hOldBrush = SelectObject(hDC, GetStockObject(NULL_BRUSH));
  244.         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
  245.  
  246.         SelectObject(hDC, hOldPen);
  247.         DeleteObject(hPen);
  248.     }
  249. }
  250.  
  251. int CFlexListBox::AddString(LPCTSTR str)
  252. {
  253.     int newIndex = m_ItemArray.GetSize();
  254.     m_ItemArray.SetSize(newIndex + 1);
  255.     FLEXLISTBOXITEM &i = m_ItemArray[newIndex];
  256.     i.SetText(str);
  257.     i.bSelected = FALSE;
  258.  
  259.     SetSBValues();
  260.     Calc();
  261.     Invalidate();
  262.  
  263.     return newIndex;
  264. }
  265.  
  266. void CFlexListBox::StartSel()
  267. {
  268.     if (m_bDragging)
  269.         return;
  270.     SetTimer(m_hWnd, 5, 200, NULL);
  271.     m_bOpenClick = TRUE;  // Initial click on the combobox
  272.     m_bDragging = TRUE;
  273.     m_bCapture = TRUE;
  274.     SetCapture();
  275. }
  276.  
  277. LRESULT CFlexListBox::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  278. {
  279.     // first handle scroll bar messages
  280.     switch (msg)
  281.     {
  282.         case WM_FLEXVSCROLL:
  283.             int code = (int)wParam;
  284.             CFlexScrollBar *pSB = (CFlexScrollBar *)lParam;
  285.             if (!pSB)
  286.                 return 0;
  287.  
  288.             int nLine = 1;
  289.             int nPage = MulDiv(pSB->GetPage(), 9, 10);
  290.  
  291.             switch (code)
  292.             {
  293.                 case SB_LINEUP: pSB->AdjustPos(-nLine); break;
  294.                 case SB_LINEDOWN: pSB->AdjustPos(nLine); break;
  295.                 case SB_PAGEUP: pSB->AdjustPos(-nPage); break;
  296.                 case SB_PAGEDOWN: pSB->AdjustPos(nPage); break;
  297.                 case SB_THUMBTRACK: pSB->SetPos(pSB->GetThumbPos()); break;
  298.                 case SB_ENDSCROLL:
  299.                     SetCapture();     // Recapture after the scroll bar releases the capture.
  300.                     break;
  301.             }
  302.  
  303.             switch (msg)
  304.             {
  305.                 case WM_FLEXVSCROLL:
  306.                     m_nTopIndex = pSB->GetPos();
  307.                     if (m_nTopIndex < 0)
  308.                         m_nTopIndex = 0;
  309.                     break;
  310.             }
  311.  
  312.             Invalidate();
  313.             return 0;
  314.     }
  315.  
  316.     // now non-scrolly input
  317.     switch (msg)
  318.     {
  319.         case WM_SIZE:
  320.             SetSBValues();
  321.             Calc();
  322.             Invalidate();
  323.             return 0;
  324.  
  325.         // make sure flexwnd doesn't do ANYTHING with our mouse messages
  326.         case WM_MOUSEMOVE:
  327.         case WM_LBUTTONUP:
  328.         case WM_LBUTTONDOWN:
  329.         case WM_RBUTTONUP:
  330.         case WM_RBUTTONDOWN:
  331.         case WM_LBUTTONDBLCLK:
  332.         case WM_RBUTTONDBLCLK:
  333.         {
  334.             POINT point = {int(signed short(LOWORD(lParam))), int(signed short(HIWORD(lParam)))};
  335.             m_point = point;
  336.         }
  337.         case WM_TIMER:
  338.         case WM_CAPTURECHANGED:
  339.             break;
  340.         default:
  341.             return CFlexWnd::WndProc(hWnd, msg, wParam, lParam);
  342.     }
  343.  
  344.     switch (msg)
  345.     {
  346.         case WM_LBUTTONDOWN:
  347.             // Check if we clicked the scroll bar area.  If so, send the click to the scroll bar.
  348.             RECT rc;
  349.             m_VertSB.GetClientRect(&rc);
  350.             ClientToScreen(m_VertSB.m_hWnd, &rc);
  351.             ScreenToClient(m_hWnd, &rc);
  352.             if (PtInRect(&rc, m_point))
  353.             {
  354.                 POINT point = {int(signed short(LOWORD(lParam))), int(signed short(HIWORD(lParam)))};
  355.                 ClientToScreen(m_hWnd, &point);
  356.                 ScreenToClient(m_VertSB.m_hWnd, &point);
  357.                 PostMessage(m_VertSB.m_hWnd, WM_LBUTTONDOWN, wParam, point.x + (point.y << 16));  // This will make it lose capture.
  358.             } else
  359.                 StartSel();
  360.             break;
  361.  
  362.         case WM_MOUSEMOVE:
  363.         case WM_LBUTTONUP:
  364.             if (!m_bDragging)
  365.                 break;
  366.             if (m_nTextHeight == -1)
  367.                 break;
  368.         case WM_TIMER:
  369.             if (m_bDragging || msg != WM_TIMER)
  370.             {
  371.                 int adj = m_point.y < 0 ? -1 : 0;
  372.                 SelectAndShowSingleItem(adj + m_point.y / m_nTextHeight + m_nTopIndex, msg != WM_MOUSEMOVE);
  373.                 Notify(FLBN_SEL);
  374.             }
  375.             // Check if the mouse cursor is within the listbox rectangle.  If not, don't show the tooltip.
  376.             if (msg == WM_MOUSEMOVE)
  377.             {
  378.                 RECT rect;
  379.                 GetClientRect(&rect);
  380.                 POINT pt;
  381.                 GetCursorPos(&pt);
  382.                 ScreenToClient(m_hWnd, &pt);
  383.                 if (!PtInRect(&rect, pt))
  384.                 {
  385.                     CFlexWnd::s_ToolTip.SetToolTipParent(NULL);
  386.                     CFlexWnd::s_ToolTip.SetEnable(FALSE);
  387.                 }
  388.             }
  389.             break;
  390.     }
  391.  
  392.     switch (msg)
  393.     {
  394.         case WM_CAPTURECHANGED:
  395.             if ((HWND)lParam == m_VertSB.m_hWnd)  // If the scroll bar is getting the capture, we do not clean up.
  396.                 break;
  397.         case WM_LBUTTONUP:
  398.             if (m_bOpenClick)
  399.             {
  400.                 m_bOpenClick = FALSE;  // If this is the result of clicking the combobox window, don't release capture.
  401.                 break;
  402.             }
  403.             if (m_bCapture)
  404.             {
  405.                 m_bCapture = FALSE;
  406.                 KillTimer(m_hWnd, 5);
  407.                 ReleaseCapture();
  408.                 m_bDragging = FALSE;
  409.                 BOOL bCancel = TRUE;
  410.                 if (msg == WM_LBUTTONUP)
  411.                 {
  412.                     RECT wrect;
  413.                     GetClientRect(&wrect);
  414.                     if (PtInRect(&wrect, m_point))
  415.                         bCancel = FALSE;
  416.                 }
  417.                 Notify(bCancel ? FLBN_CANCEL : FLBN_FINALSEL);
  418.             }
  419.             break;
  420.     }
  421.  
  422.     return 0;
  423. }
  424.  
  425. void CFlexListBox::SelectAndShowSingleItem(int i, BOOL bScroll)
  426. {
  427.     int nItems = m_ItemArray.GetSize();
  428.  
  429.     if (nItems < 1)
  430.     {
  431.         m_nSelItem = i;  // We have to update m_nSelItem even if there is no items because the username combobox
  432.                          // is not initialized when there is only 1 user.  Selection of user 0 is assumed.
  433.         return;
  434.     }
  435.  
  436.     if (i < 0)
  437.         i = 0;
  438.     if (i >= nItems)
  439.         i = nItems - 1;
  440.  
  441.     if (m_nSelItem >= 0 && m_nSelItem < nItems)
  442.         m_ItemArray[m_nSelItem].bSelected = FALSE;
  443.  
  444.     m_nSelItem = i;
  445.     m_ItemArray[m_nSelItem].bSelected = TRUE;
  446.  
  447.     if (bScroll)
  448.     {
  449.         SIZE client = GetClientSize();
  450.         int nBottomIndex = m_nTopIndex + client.cy / m_nTextHeight - 1;
  451.  
  452.         if (m_nSelItem < m_nTopIndex)
  453.             m_nTopIndex = m_nSelItem;
  454.  
  455.         assert(m_nTopIndex >= 0);
  456.  
  457.         if (m_nSelItem > nBottomIndex)
  458.         {
  459.             m_nTopIndex += m_nSelItem - nBottomIndex + 1;
  460.             nBottomIndex = m_nSelItem + 1;
  461.         }
  462.  
  463.         if (nBottomIndex > nItems - 1)
  464.             m_nTopIndex -= nBottomIndex - nItems + 1;
  465.  
  466.         if (m_nTopIndex < 0)
  467.             m_nTopIndex = 0;
  468.  
  469.         if (m_nTopIndex >= nItems)
  470.             m_nTopIndex = nItems - 1;
  471.  
  472.         assert(m_nTopIndex >= 0 && m_nTopIndex < nItems);
  473.     }
  474.  
  475.     if (m_bVertSB)
  476.         SetSBValues();
  477.  
  478.     SIZE client = GetClientSize();
  479.     int nBottomIndex = m_nTopIndex + client.cy / m_nTextHeight - 1;
  480.     int iToolTipIndex = m_nSelItem;
  481.     // Make sure that we don't display tooltip for items outside the listbox window
  482.     if (iToolTipIndex > nBottomIndex)
  483.         iToolTipIndex = nBottomIndex;
  484.     if (iToolTipIndex < m_nTopIndex)
  485.         iToolTipIndex = m_nTopIndex;
  486.     // Create and initialize a tooltip if the text is too long to fit.
  487.     RECT rect = {0, 0, client.cx, m_nTextHeight};
  488.     RECT ResultRect = rect;
  489.     HDC hDC = CreateCompatibleDC(NULL);
  490.     if (hDC)
  491.     {
  492.         DrawText(hDC, m_ItemArray[iToolTipIndex].GetText(), -1, &ResultRect, DT_NOPREFIX|DT_CALCRECT);
  493.         DeleteDC(hDC);
  494.     }
  495.     if (ResultRect.right > rect.right)
  496.     {
  497.         TOOLTIPINITPARAM ttip;
  498.         ttip.hWndParent = GetParent(m_hWnd);
  499.         ttip.iSBWidth = m_nSBWidth;
  500.         ttip.dwID = iToolTipIndex;
  501.         ttip.hWndNotify = m_hWnd;
  502.         ttip.tszCaption = GetSelText();
  503.         CFlexToolTip::UpdateToolTipParam(ttip);
  504.     }
  505.  
  506.     Invalidate();
  507. }
  508.  
  509. void CFlexListBox::Notify(int code)
  510. {
  511.     if (!m_hWndNotify)
  512.         return;
  513.  
  514.     SendMessage(m_hWndNotify, WM_FLEXLISTBOX,
  515.         (WPARAM)code, (LPARAM)(LPVOID)this);
  516. }
  517.  
  518. void CFlexListBox::SetColors(COLORREF text, COLORREF bk, COLORREF seltext, COLORREF selbk, COLORREF fill, COLORREF line)
  519. {
  520.     m_rgbText = text;
  521.     m_rgbBk = bk;
  522.     m_rgbSelText = seltext;
  523.     m_rgbSelBk = selbk;
  524.     m_rgbFill = fill;
  525.     m_rgbLine = line;
  526.     Invalidate();
  527. }
  528.  
  529. void CFlexListBox::SetVertSB(BOOL bSet)
  530. {
  531.     if (bEq(bSet, m_bVertSB))
  532.         return;
  533.  
  534.     m_bVertSB = bSet;
  535.  
  536.     if (m_hWnd == NULL)
  537.         return;
  538.  
  539.     SetVertSB();
  540. }
  541.  
  542. void CFlexListBox::SetVertSB()
  543. {
  544.     if (m_bVertSB)
  545.     {
  546.         SetSBValues();
  547.         SIZE client = GetClientSize();
  548.         SetWindowPos(m_VertSB.m_hWnd, NULL, client.cx - m_nSBWidth - 1, 0, m_nSBWidth, client.cy - 1, SWP_NOZORDER);
  549.     }
  550.  
  551.     ShowWindow(m_VertSB.m_hWnd, m_bVertSB ? SW_SHOW : SW_HIDE);
  552. }
  553.  
  554. void CFlexListBox::SetSBValues()
  555. {
  556.     if (m_hWnd == NULL)
  557.         return;
  558.  
  559.     SIZE client = GetClientSize();
  560.     int fit = client.cy / m_nTextHeight;
  561.     m_VertSB.SetValues(0, m_ItemArray.GetSize(), fit, m_nTopIndex);
  562. }
  563.  
  564. LPCTSTR CFlexListBox::GetSelText()
  565. {
  566.     if (m_nSelItem < 0 || m_nSelItem >= m_ItemArray.GetSize())
  567.         return NULL;
  568.  
  569.     return m_ItemArray[m_nSelItem].GetText();
  570. }
  571.  
  572. int CFlexListBox::GetSel()
  573. {
  574.     return m_nSelItem;
  575. }
  576.