Subversion Repositories Kolibri OS

Rev

Rev 5694 | Rev 5709 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //list_box
  2. #ifndef INCLUDE_LIST_BOX_H
  3. #define INCLUDE_LIST_BOX_H
  4. #print "[include <list_box.h>]\n"
  5.  
  6. #ifndef INCLUDE_KOLIBRI_H
  7. #include "../lib/kolibri.h"
  8. #endif
  9.  
  10. struct llist
  11. {
  12.         int x, y, w, h, min_h, line_h, text_y;
  13.         int column_max;
  14.         int count, visible, first, current; //visible = row_max
  15.         int active;
  16.         void ClearList();
  17.         int MouseOver(int xx, yy);
  18.         int ProcessMouse(int xx, yy);
  19.         int ProcessKey(dword key);
  20.         int KeyDown();
  21.         int KeyUp();
  22.         int KeyHome();
  23.         int KeyEnd();
  24.         int KeyPgDown();
  25.         int KeyPgUp();
  26.         void CheckDoesValuesOkey();
  27.         void SetSizes(int xx, yy, ww, hh, min_hh, line_hh);
  28.         int MouseScroll(dword scroll_state);
  29.         int MouseScrollNoSelection(dword scroll_state);
  30.         void debug_values();
  31. };
  32.  
  33.  
  34. void llist::debug_values()
  35. {
  36.         char yi[128];
  37.         sprintf(#yi, "%s %d %s %d %s %d %s %d", "current:", current, "first:", first,
  38.         "visible:", visible, "count:", count);
  39.         debugln(#yi);
  40. }
  41.  
  42.  
  43.  
  44. void llist::ClearList()
  45. {
  46.         count = visible = first = current = 0;
  47. }
  48.  
  49.  
  50. void llist::SetSizes(int xx, yy, ww, hh, min_hh, line_hh)
  51. {
  52.         x = xx;
  53.         y = yy;
  54.         w = ww;
  55.         h = hh;
  56.         min_h = min_hh;
  57.         line_h = line_hh;
  58.         text_y = line_h / 2 - 4;
  59.         visible = h / line_h;
  60.         column_max = w / 6;
  61.         //if (visible > count) visible=count;
  62. }
  63.  
  64.  
  65. int llist::MouseScroll(dword scroll_state)
  66. {
  67.         if (count<=visible) return 0;
  68.         if (scroll_state == 65535)
  69.         {
  70.                 if (first == 0) return 0;
  71.                 if (first > 3) first -= 2; else first=0;
  72.                 return 1;
  73.         }
  74.         if (scroll_state == 1)
  75.         {
  76.                 if (visible + first == count) return 0;
  77.                 if (visible+first+3 > count) first = count - visible; else first+=2;
  78.                 return 1;
  79.         }
  80.         return 0;
  81. }
  82.  
  83. int llist::MouseScrollNoSelection(dword scroll_state)
  84. {
  85.         if (count<=visible) return 0;
  86.         if (scroll_state == 65535)
  87.         {
  88.                 if (current == 0) return 0;
  89.                 if (current > 3) current -= 2; else current=0;
  90.                 return 1;
  91.         }
  92.         if (scroll_state == 1)
  93.         {
  94.                 if (visible + current == count) return 0;
  95.                 if (visible+current+3 > count) current = count - visible; else current+=2;
  96.                 return 1;
  97.         }
  98.         return 0;
  99. }
  100.  
  101. int llist::MouseOver(int xx, yy)
  102. {
  103.         if (xx>x) && (xx<x+w) && (yy>y) && (yy<y+h) return 1;
  104.         return 0;
  105. }
  106.  
  107. int llist::ProcessMouse(int xx, yy)
  108. {
  109.         int current_temp;
  110.         if (MouseOver(xx, yy))
  111.         {
  112.                 current_temp = yy - y / line_h + first;
  113.                 if (current_temp != current) && (current_temp<count)
  114.                 {
  115.                         current = current_temp;
  116.                         return 1;
  117.                 }
  118.         }
  119.         return 0;
  120. }
  121.  
  122. int llist::ProcessKey(dword key)
  123. {
  124.         switch(key)
  125.         {
  126.                 case SCAN_CODE_DOWN: return KeyDown();
  127.                 case SCAN_CODE_UP:   return KeyUp();
  128.                 case SCAN_CODE_HOME: return KeyHome();
  129.                 case SCAN_CODE_END:  return KeyEnd();
  130.                 case SCAN_CODE_PGUP: return KeyPgUp();
  131.                 case SCAN_CODE_PGDN: return KeyPgDown();
  132.         }
  133.         return 0;
  134. }
  135.  
  136. int llist::KeyDown()
  137. {
  138.         if (current-first+1<visible)
  139.         {
  140.                 if (current + 1 >= count) return 0;
  141.                 current++;
  142.         }
  143.         else
  144.         {
  145.                 if (visible + first >= count) return 0;
  146.                 first++;
  147.                 current++;
  148.         }
  149.         if (current < first) || (current > first + visible)
  150.         {
  151.                 first = current;
  152.                 CheckDoesValuesOkey();
  153.         }
  154.         return 1;
  155. }
  156.  
  157. int llist::KeyUp()
  158. {
  159.         if (current > first)
  160.         {
  161.                 current--;
  162.         }
  163.         else
  164.         {
  165.                 if (first == 0) return 0;
  166.                 first--;
  167.                 current--;
  168.         }
  169.         if (current < first) || (current > first + visible)
  170.         {
  171.                 first = current;
  172.                 CheckDoesValuesOkey();
  173.         }
  174.         return 1;
  175. }
  176.  
  177. int llist::KeyHome()
  178. {
  179.         if (current==0) && (first==0) return 0;
  180.         current = first = 0;
  181.         return 1;
  182. }
  183.  
  184. int llist::KeyEnd()
  185. {
  186.         if (current==count-1) && (first==count-visible) return 0;
  187.         current = count-1;
  188.         first = count - visible;
  189.         return 1;
  190. }
  191.  
  192. int llist::KeyPgUp()
  193. {
  194.         if (count <= visible) return KeyHome();
  195.         if (first == 0) return 0;
  196.         first -= visible;
  197.         CheckDoesValuesOkey();
  198.         return 1;
  199. }
  200.  
  201. int llist::KeyPgDown()
  202. {
  203.         if (count <= visible) return KeyEnd();
  204.         if (first == count - visible) return 0;
  205.         first += visible;
  206.         CheckDoesValuesOkey();
  207.         return 1;
  208. }
  209.  
  210. void llist::CheckDoesValuesOkey()
  211. {
  212.         if (first < 0) first = 0;
  213.         if (visible + first > count) first = count - visible;
  214.         if (current >= count) current = count - 1;
  215.         if (current < 0) current = 0;
  216. }
  217.  
  218. #endif