home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / LISTBOX.CPP < prev    next >
C/C++ Source or Header  |  1993-09-24  |  3KB  |  163 lines

  1. // ------------ listbox.cpp
  2.  
  3. #include "listbox.h"
  4. #include "keyboard.h"
  5.  
  6. static Color col = {
  7.     BLACK,            // fg
  8.     LIGHTGRAY,        // bg
  9.     LIGHTGRAY,        // selected fg
  10.     BLACK,            // selected bg
  11.     BLACK,            // frame fg
  12.     LIGHTGRAY,        // frame bg
  13.     LIGHTGRAY,        // inactive fg
  14.     BLACK            // inactive bg
  15. };
  16.  
  17. // ----------- common constructor code
  18. void ListBox::OpenWindow()
  19. {
  20.     windowtype = ListboxWindow;
  21.     selection = -1;
  22.     addmode = False;
  23.     anchorpoint = -1;
  24.     selectcount = 0;
  25.     colors = col;
  26. }
  27.  
  28. void ListBox::ClearSelection()
  29. {
  30.     if (selection != -1)
  31.         WriteTextLine(selection, colors.fg, colors.bg);
  32. }
  33.  
  34. void ListBox::ClearText()
  35. {
  36.     TextBox::ClearText();
  37.     OpenWindow();
  38. }
  39.  
  40. void ListBox::SetSelection(int sel)
  41. {
  42.     ClearSelection();
  43.     if (sel >= 0 && sel < wlines)    {
  44.         selection = sel;
  45.         WriteTextLine(sel, colors.sfg, colors.sbg);
  46.         Select();
  47.     }
  48. }
  49.  
  50. void ListBox::Keyboard(int key)
  51. {
  52.     int sel = selection; // (ClearSelection changes selection)
  53.     switch (key)    {
  54.         case UP:
  55.             if (sel > 0)    {
  56.                 ClearSelection();
  57.                 if (sel == wtop)
  58.                     ScrollDown();
  59.                 SetSelection(sel-1);
  60.             }
  61.             return;
  62.         case DN:
  63.             if (sel < wlines-1)    {
  64.                 ClearSelection();
  65.                 if (sel == wtop+ClientHeight()-1)
  66.                     ScrollUp();
  67.                 SetSelection(sel+1);
  68.             }
  69.             return;
  70.         case '\r':
  71.             Choose();
  72.             return;
  73.         default:
  74.             break;
  75.     }
  76.     TextBox::Keyboard(key);
  77. }
  78.  
  79. // ---------- paint the listbox
  80. void ListBox::Paint()
  81. {
  82.     TextBox::Paint();
  83.     if (text != 0)
  84.         WriteTextLine(selection, colors.sfg, colors.sbg);
  85. }
  86.  
  87. void ListBox::ScrollUp()
  88. {
  89.     TextBox::ScrollUp();
  90.     WriteTextLine(selection, colors.sfg, colors.sbg);
  91. }
  92.  
  93. void ListBox::ScrollDown()
  94. {
  95.     TextBox::ScrollDown();
  96.     WriteTextLine(selection, colors.sfg, colors.sbg);
  97. }
  98.  
  99. void ListBox::ScrollRight()
  100. {
  101.     TextBox::ScrollRight();
  102.     WriteTextLine(selection, colors.sfg, colors.sbg);
  103. }
  104.  
  105. void ListBox::ScrollLeft()
  106. {
  107.     TextBox::ScrollLeft();
  108.     WriteTextLine(selection, colors.sfg, colors.sbg);
  109. }
  110.  
  111. void ListBox::PageUp()
  112. {
  113.     TextBox::PageUp();
  114.     WriteTextLine(selection, colors.sfg, colors.sbg);
  115. }
  116.  
  117. void ListBox::PageDown()
  118. {
  119.     TextBox::PageDown();
  120.     WriteTextLine(selection, colors.sfg, colors.sbg);
  121. }
  122.  
  123. void ListBox::PageRight()
  124. {
  125.     TextBox::PageRight();
  126.     WriteTextLine(selection, colors.sfg, colors.sbg);
  127. }
  128.  
  129. void ListBox::PageLeft()
  130. {
  131.     TextBox::PageLeft();
  132.     WriteTextLine(selection, colors.sfg, colors.sbg);
  133. }
  134.  
  135. // ---------- Left mouse button was clicked
  136. void ListBox::LeftButton(int mx, int my)
  137. {
  138.     if (my != prevmouseline)    {
  139.         if (ClientRect().Inside(mx, my))    {
  140.             int y = my - ClientTop();
  141.             if (wlines && y < wlines-wtop)
  142.                 SetSelection(wtop+y);
  143.         }
  144.     }
  145.     DFWindow::LeftButton(mx, my);
  146. }
  147.  
  148. void ListBox::DoubleClick(int mx, int my)
  149. {
  150.     if (ClientRect().Inside(mx, my))    {
  151.         my -= ClientTop();
  152.         if (wlines && my < wlines-wtop)
  153.             Choose();
  154.     }
  155.     DFWindow::DoubleClick(mx, my);
  156. }
  157.  
  158. void ListBox::ButtonReleased(int, int)
  159. {
  160.     prevmouseline = -1;
  161. }
  162.  
  163.