home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MDBS.ZIP / LISTBOX.CLS < prev    next >
Text File  |  1990-01-15  |  7KB  |  223 lines

  1. /*
  2. **  (C) Copyright 1989, 1990
  3. **  Micro Data Base Systems Inc.
  4. **  Lafayette, Indiana
  5. */
  6. /* Class ListBox is the class of all listbox
  7.    type control windows. */
  8. subclass(Control,
  9.          #ListBox,
  10.          nil,
  11.          true, true, false);$
  12.  
  13. /* Answer the window class name string. */
  14. method ListBox::windowClassNameOf(self)
  15. {
  16.         return WC_LISTBOX;
  17. }
  18.  
  19. /* Add a string to a listbox for display. Answer with the index to the
  20.    string in the list box, or LIT_ERROR for errors, or LIT_MEMERR for
  21.    insufficient memory. */
  22. method ListBox::add(self, itemText)
  23. {
  24.         return loWord(sendMessage(self, LM_INSERTITEM, LIT_END, itemText));
  25. }
  26.  
  27. /* Add a collection of strings to a list box. */
  28. method ListBox::addAll(self, collection)
  29. {
  30.         disableUpdate(self);
  31.         do(collection, ::( item ){
  32.                 add(self, item);
  33.         });
  34.         enableUpdate(self);
  35.         return collection;
  36. }
  37.  
  38. /* Remove the indexed item from the List Box. Answer the count of
  39.    remaining items in the List Box. */
  40. method ListBox::remove(self, itemIndex)
  41. {
  42.         return asInteger(sendMessage(self, LM_DELETEITEM, itemIndex, 0));
  43. }
  44.  
  45. /* Answer the count of items contained in the List Box. */
  46. method ListBox::countOf(self)
  47. {
  48.         return loWord(sendMessage(self, LM_QUERYITEMCOUNT, 0, 0));
  49. }
  50.  
  51. /* Answer the index of the currently selected item in the listbox. */
  52. method ListBox::currentSelectionOf(self)
  53. {
  54.         return loWord(sendMessage(self, LM_QUERYSELECTION, LIT_FIRST, 0));
  55. }
  56.  
  57. /* Answer 'true' if the indexed item is selected in the List Box. */
  58. method ListBox::isSelected(self, itemIndex)
  59. {
  60.         return currentSelectionOf(self) == itemIndex;
  61. }
  62.  
  63. /* Answer the text representing the indexed item in the List Box. */
  64. method ListBox::textOf(self, itemIndex)
  65. {
  66.         local  itemLength, itemText ;
  67.  
  68.         itemLength = textLengthOf(self, itemIndex);
  69.         itemText   = new(String, itemLength);
  70.         sendMessage(self, LM_QUERYITEMTEXT,
  71.                     makeLong(itemIndex, itemLength + 1), itemText);
  72.         return itemText;
  73. }
  74.  
  75. /* Answer the length of the indexed item in the List Box. */
  76. method ListBox::textLengthOf(self, itemIndex)
  77. {
  78.         return loWord(sendMessage(self, LM_QUERYITEMTEXTLENGTH, itemIndex, 0));
  79. }
  80.  
  81. /* Insert the string at the indexed position in the List Box. Answer the
  82.    index that the string was inserted. */
  83. method ListBox::insert(self, itemIndex, itemText)
  84. {
  85.         return asInteger(sendMessage(self, LM_INSERTITEM, itemIndex, itemText));
  86. }
  87.  
  88. /* Remove all the strings from the list box. */
  89. method ListBox::reset(self)
  90. {
  91.         return sendMessage(self, LM_DELETEALL, 0, 0) != 0;
  92. }
  93.  
  94. /* Change the selection to the first string in the List Box with the specified
  95.    prefix string. The search begins after the specified index. Answer the
  96.    index of the selected string. */
  97. method ListBox::select(self, itemIndex, itemText)
  98. {
  99.         return setSelection(self, search(self, itemIndex, itemText));
  100. }
  101. /* Search the list for a string that matches the given string
  102.    starting at the string following the given index. Answer
  103.    the index of the matching string, or LIT_NONE if no strings
  104.    match the given string. */
  105. method ListBox::search(self, itemIndex, itemText)
  106. {
  107.         return loWord(sendMessage(self,
  108.                                   LM_SEARCHSTRING,
  109.                                   makeLong(LSS_CASESENSITIVE,
  110.                                            itemIndex),
  111.                                   itemText));
  112. }
  113.  
  114. /* Select the indexed string in the List Box and scroll it into view if
  115.    necessary. Removes the highlight from any previously selected string. */
  116. method ListBox::setSelection(self, itemIndex)
  117. {
  118.         if (sendMessage(self, LM_SELECTITEM, itemIndex, true) != 0)
  119.                 return itemIndex;
  120.         return LIT_NONE;
  121. }
  122.  
  123. /* Answer an array of the items contained in the List Box. */
  124. method ListBox::itemsOf(self)
  125. {
  126.         local  count, index, vector ;
  127.  
  128.         count = countOf(self);
  129.         if (count > 0) {
  130.                 vector = new(Array, count);
  131.                 for (index = 0; index < count; index = index + 1) {
  132.                         vector[index] = textOf(self, index);
  133.                 }
  134.         }
  135.         return vector;
  136. }
  137.  
  138. /* Remove the currently selected item from the List Box. Answer the
  139.    count of remaining items in the List Box. */
  140. method ListBox::removeCurrentSelectionOf(self)
  141. {
  142.         return remove(self, currentSelectionOf(self));
  143. }
  144.  
  145. /* Enumerate all the items in the list box through the
  146.    one argument block. */
  147. method ListBox::do(self, block)
  148. {
  149.         return do(itemsOf(self), ::(item){
  150.         valueOf(block, textOf(self, item));
  151.         });
  152. }
  153.  
  154. /* Answer the index of the top item in the list box. */
  155. method ListBox::topIndexOf(self)
  156. {
  157.     return asInteger(sendMessage(self, LM_QUERYTOPINDEX, 0, 0));
  158. }
  159.  
  160. /* Move the indexed item to the top of he list box. */
  161. method ListBox::setTopIndex(self, itemIndex)
  162. {
  163.         return sendMessage(self, LM_SETTOPINDEX, itemIndex, 0) != 0;
  164. }
  165.  
  166. /* Change the text string for the indexed item. */
  167. method ListBox::setItemText(self, itemIndex, itemText)
  168. {
  169.         return sendMessage(self, LM_SETITEMTEXT, itemIndex, itemText) != 0;
  170. }
  171.  
  172. /* Search the list for a string that matches the given string
  173.    starting at the string following the given index. Answer
  174.    the index of the matching string, or LIT_NONE if no strings
  175.    match the given string. */
  176. method ListBox::searchSubString(self, itemIndex, itemText)
  177. {
  178.         return loWord(sendMessage(self,
  179.                                   LM_SEARCHSTRING,
  180.                                   makeLong(LSS_CASESENSITIVE | LSS_SUBSTRING,
  181.                                            itemIndex),
  182.                                   itemText));
  183. }
  184.  
  185.  
  186. /* Return the default style. */
  187. method ListBoxClass::defaultStyleOf(self)
  188. {
  189.     return WS_TABSTOP | WS_CLIPSIBLINGS;
  190. }
  191. /* Return the corrseponding paint class. */
  192. method ListBoxClass::paintClassOf(self)
  193. {
  194.     return PaintListBox;
  195. }
  196. /* Return the default shape. */
  197. method ListBoxClass::defaultShapeOf(self)
  198. {
  199.     return rect(0,0,100,100);
  200. }
  201. /* Return the default title. */
  202. method ListBoxClass::defaultTitleOf(self)
  203. {
  204.     return EmptyString;
  205. }
  206.  
  207.  
  208. /* Answer a collection of indexes of the currently selected items
  209.    in the listbox. */
  210. method ListBox::allCurrentSelectionsOf(self)
  211. {
  212.         local ans, t, lastIndex;
  213.  
  214.         t = new(OrderedCollection, 0);
  215.         lastIndex = LIT_FIRST;
  216.         while(LIT_NONE != (ans = sendMessage(self,
  217.                                              LM_QUERYSELECTION,
  218.                                              lastIndex, 0))) {
  219.                 lastIndex = ans;
  220.                 add(t, ans);
  221.         }
  222.         return t;
  223. }