SelItemList.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <precomp.h>
  2. #include "SelItemList.h"
  3. #include "listwnd.h"
  4. #define SELITEMEXPAND 2040
  5. SelItemList::SelItemList(ListWnd *parent) :
  6. listwnd(parent), list(SELITEMEXPAND), num_selected(0) { }
  7. void SelItemList::setSelected(int pos, int selected, int cb)
  8. {
  9. int s = list.getSize();
  10. if (pos >= s)
  11. {
  12. if (!selected)
  13. {
  14. // turning off something that's out of range is a no-brainer
  15. return ;
  16. }
  17. s = ((s / SELITEMEXPAND) + 1) * SELITEMEXPAND;
  18. list.setSize(s);
  19. }
  20. char *l = list.getMemory();
  21. if (selected)
  22. {
  23. // quick and dirty way to prevent more than one item from
  24. // ever being selected?
  25. if (listwnd->getPreventMultipleSelection())
  26. {
  27. listwnd->deselectAll(cb);
  28. }
  29. if (!isSelected(pos))
  30. {
  31. l[pos] = 1;
  32. num_selected++;
  33. if (cb) listwnd->itemSelection(pos, TRUE);
  34. listwnd->invalidateItem(pos);
  35. }
  36. }
  37. else
  38. {
  39. if (isSelected(pos))
  40. {
  41. l[pos] = 0;
  42. num_selected--;
  43. if (cb) listwnd->itemSelection(pos, FALSE);
  44. listwnd->invalidateItem(pos);
  45. }
  46. }
  47. }
  48. int SelItemList::isSelected(int pos)
  49. {
  50. if (pos >= list.getSize()) return 0;
  51. return list.getMemory()[pos];
  52. }
  53. int SelItemList::getNumSelected() { return num_selected; }
  54. void SelItemList::deleteByPos(int pos)
  55. {
  56. ASSERT(pos >= 0);
  57. int s = list.getSize();
  58. if (pos >= s) return ;
  59. num_selected -= isSelected(pos);
  60. char *m = list.getMemory() + pos;
  61. MEMCPY(m, m + 1, s - (pos + 1));
  62. }
  63. void SelItemList::deselectAll()
  64. { // for internal use, doesn't send callbacks
  65. list.zeroMemory();
  66. num_selected = 0;
  67. }