CListCtrl.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * CListCtrl.h
  3. * -----------
  4. * Purpose: A class that extends MFC's CListCtrl with some more functionality and to handle unicode strings in ANSI builds.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "MPTrackUtil.h"
  12. OPENMPT_NAMESPACE_BEGIN
  13. class CListCtrlEx : public CListCtrl
  14. {
  15. public:
  16. struct Header
  17. {
  18. const TCHAR *text = nullptr;
  19. int width = 0;
  20. UINT mask = 0;
  21. };
  22. void SetHeaders(const mpt::span<const Header> &header)
  23. {
  24. for(int i = 0; i < static_cast<int>(header.size()); i++)
  25. {
  26. int width = header[i].width;
  27. InsertColumn(i, header[i].text, header[i].mask, width >= 0 ? Util::ScalePixels(width, m_hWnd) : 16);
  28. if(width < 0)
  29. SetColumnWidth(i, width);
  30. }
  31. }
  32. void SetItemDataPtr(int item, void *value)
  33. {
  34. SetItemData(item, reinterpret_cast<DWORD_PTR>(value));
  35. }
  36. void *GetItemDataPtr(int item)
  37. {
  38. return reinterpret_cast<void *>(GetItemData(item));
  39. }
  40. // Unicode strings in ANSI builds
  41. #ifndef UNICODE
  42. BOOL SetItemText(int nItem, int nSubItem, const WCHAR *lpszText)
  43. {
  44. ASSERT(::IsWindow(m_hWnd));
  45. ASSERT((GetStyle() & LVS_OWNERDATA)==0);
  46. LVITEMW lvi;
  47. lvi.iSubItem = nSubItem;
  48. lvi.pszText = (LPWSTR) lpszText;
  49. return (BOOL) ::SendMessage(m_hWnd, LVM_SETITEMTEXTW, nItem, (LPARAM)&lvi);
  50. }
  51. using CListCtrl::SetItemText;
  52. #endif
  53. };
  54. #ifdef MPT_MFC_FULL
  55. class CMFCListCtrlEx : public CMFCListCtrl
  56. {
  57. public:
  58. struct Header
  59. {
  60. const TCHAR *text = nullptr;
  61. int width = 0;
  62. UINT mask = 0;
  63. };
  64. void SetHeaders(const mpt::span<const Header> &header)
  65. {
  66. for(int i = 0; i < static_cast<int>(header.size()); i++)
  67. {
  68. InsertColumn(i, header[i].text, header[i].mask, Util::ScalePixels(header[i].width, m_hWnd));
  69. }
  70. }
  71. // Unicode strings in ANSI builds
  72. #ifndef UNICODE
  73. BOOL SetItemText(int nItem, int nSubItem, const WCHAR *lpszText)
  74. {
  75. ASSERT(::IsWindow(m_hWnd));
  76. ASSERT((GetStyle() & LVS_OWNERDATA)==0);
  77. LVITEMW lvi;
  78. lvi.iSubItem = nSubItem;
  79. lvi.pszText = (LPWSTR) lpszText;
  80. return (BOOL) ::SendMessage(m_hWnd, LVM_SETITEMTEXTW, nItem, (LPARAM)&lvi);
  81. }
  82. using CListCtrl::SetItemText;
  83. #endif
  84. };
  85. #endif // MPT_MFC_FULL
  86. OPENMPT_NAMESPACE_END