Subversion Repositories Kolibri OS

Rev

Rev 6039 | Rev 7160 | 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.  
  5. #ifndef INCLUDE_KOLIBRI_H
  6. #include "../lib/kolibri.h"
  7. #endif
  8.  
  9. struct llist
  10. {
  11.         int x, y, w, h, item_h, item_w;
  12.         int count, visible, first, column_max; //visible = row_max
  13.         int cur_x, cur_y;
  14.         int text_y;
  15.         byte font_w, font_h, font_type;
  16.         byte wheel_size;
  17.         byte active;
  18.         byte no_selection;
  19.         byte horisontal_selelection;
  20.         void ClearList();
  21.         void SetSizes(int xx, yy, ww, hh, item_hh);
  22.         void SetFont(dword font_ww, font_hh, font_tt);
  23.         int ProcessKey(dword key);
  24.         int ProcessMouse(int xx, yy);
  25.         int MouseOver(int xx, yy);
  26.         int MouseScroll(dword scroll_state);
  27.         int KeyDown();
  28.         int KeyUp();
  29.         int KeyHome();
  30.         int KeyEnd();
  31.         int KeyPgDown();
  32.         int KeyPgUp();
  33.         int KeyLeft();
  34.         int KeyRight();
  35.         void CheckDoesValuesOkey();
  36.         void debug();
  37. };
  38.  
  39. void llist::debug()
  40. {
  41.         char yi[128];
  42.         sprintf(#yi, "%s %d %s %d %s %d %s %d %s %d %s %d", "first:", first, "visible:", visible, "count:", count, "col_max:", column_max, "cur_y:", cur_y, "cur_x:", cur_x);
  43.         debugln(#yi);
  44. }
  45.  
  46.  
  47. void llist::ClearList()
  48. {
  49.         count = visible = first = cur_y = cur_x = 0;
  50. }
  51.  
  52.  
  53. void llist::SetSizes(int xx, yy, ww, hh, item_hh)
  54. {
  55.         x = xx;
  56.         y = yy;
  57.         w = ww;
  58.         h = hh;
  59.         item_h = item_hh;
  60.         text_y = item_h - font_h / 2;
  61.         visible = h / item_h;
  62.         wheel_size = 3;
  63.         CheckDoesValuesOkey();
  64. }
  65.  
  66. void llist::SetFont(dword font_ww, font_hh, font_tt)
  67. {
  68.         font_w = font_ww;
  69.         font_h = font_hh;
  70.         font_type = font_tt;
  71. }
  72.  
  73.  
  74. int llist::MouseScroll(dword scroll_state)
  75. {
  76.         if (count<=visible) return 0;
  77.         if (scroll_state == 65535)
  78.         {
  79.                 if (first == 0) return 0;
  80.                 if (first > wheel_size+1) first -= wheel_size; else first=0;
  81.                 return 1;
  82.         }
  83.         if (scroll_state == 1)
  84.         {
  85.                 if (visible + first == count) return 0;
  86.                 if (visible+first+wheel_size+1 > count) first = count - visible; else first+=wheel_size;
  87.                 return 1;
  88.         }
  89.         return 0;
  90. }
  91.  
  92.  
  93. int llist::MouseOver(int xx, yy)
  94. {
  95.         if (xx>x) && (xx<x+w) && (yy>y) && (yy<y+h) return 1;
  96.         return 0;
  97. }
  98.  
  99. int llist::ProcessMouse(int xx, yy)
  100. {
  101.         int cur_y_temp, cur_x_temp, ret=0;
  102.         if (MouseOver(xx, yy))
  103.         {
  104.                 cur_y_temp = yy - y / item_h + first;
  105.                 if (cur_y_temp != cur_y) && (cur_y_temp<count)
  106.                 {
  107.                         cur_y = cur_y_temp;
  108.                         ret = 1;
  109.                 }
  110.                 if (horisontal_selelection)
  111.                 {              
  112.                         cur_x_temp = xx - x / item_w;
  113.                         if (cur_x_temp != cur_x) && (cur_x_temp<column_max)
  114.                         {
  115.                                 cur_x = cur_x_temp;
  116.                                 ret = 1;
  117.                         }
  118.                 }
  119.         }
  120.         return ret;
  121. }
  122.  
  123. int llist::ProcessKey(dword key)
  124. {
  125.         switch(key)
  126.         {
  127.                 case SCAN_CODE_DOWN: return KeyDown();
  128.                 case SCAN_CODE_UP:   return KeyUp();
  129.                 case SCAN_CODE_HOME: return KeyHome();
  130.                 case SCAN_CODE_END:  return KeyEnd();
  131.                 case SCAN_CODE_PGUP: return KeyPgUp();
  132.                 case SCAN_CODE_PGDN: return KeyPgDown();
  133.         }
  134.         if (horisontal_selelection) switch(key)
  135.         {
  136.                 case SCAN_CODE_LEFT:  return KeyLeft();
  137.                 case SCAN_CODE_RIGHT: return KeyRight();
  138.         }
  139.         return 0;
  140. }
  141.  
  142. int llist::KeyDown()
  143. {
  144.         if (no_selection)
  145.         {
  146.                 if (visible + first >= count) return 0;
  147.                 first++;
  148.                 return 1;              
  149.         }
  150.  
  151.         if (cur_y-first+1<visible)
  152.         {
  153.                 if (cur_y + 1 >= count) return 0;
  154.                 cur_y++;
  155.         }
  156.         else
  157.         {
  158.                 if (visible + first >= count) return 0;
  159.                 first++;
  160.                 cur_y++;
  161.         }
  162.         if (cur_y < first) || (cur_y > first + visible)
  163.         {
  164.                 first = cur_y;
  165.                 CheckDoesValuesOkey();
  166.         }
  167.         return 1;
  168. }
  169.  
  170. int llist::KeyUp()
  171. {
  172.         if (no_selection)
  173.         {
  174.                 if (first == 0) return 0;
  175.                 first--;
  176.                 return 1;
  177.         }
  178.  
  179.         if (cur_y > first)
  180.         {
  181.                 cur_y--;
  182.         }
  183.         else
  184.         {
  185.                 if (first == 0) return 0;
  186.                 first--;
  187.                 cur_y--;
  188.         }
  189.         if (cur_y < first) || (cur_y > first + visible)
  190.         {
  191.                 first = cur_y;
  192.                 CheckDoesValuesOkey();
  193.         }
  194.         return 1;
  195. }
  196.  
  197. int llist::KeyHome()
  198. {
  199.         if (cur_y==0) && (first==0) return 0;
  200.         cur_y = first = 0;
  201.         return 1;
  202. }
  203.  
  204. int llist::KeyEnd()
  205. {
  206.         if (cur_y==count-1) && (first==count-visible) return 0;
  207.         cur_y = count-1;
  208.         first = count - visible;
  209.         return 1;
  210. }
  211.  
  212. int llist::KeyPgUp()
  213. {
  214.         if (count <= visible) return KeyHome();
  215.         if (first == 0) return 0;
  216.         first -= visible;
  217.         cur_y = first;
  218.         CheckDoesValuesOkey();
  219.         return 1;
  220. }
  221.  
  222. int llist::KeyPgDown()
  223. {
  224.         if (count <= visible) return KeyEnd();
  225.         if (first == count - visible) return 0;
  226.         first += visible;
  227.         cur_y = first + visible - 1;
  228.         CheckDoesValuesOkey();
  229.         return 1;
  230. }
  231.  
  232. void llist::CheckDoesValuesOkey()
  233. {
  234.         if (visible + first > count) first = count - visible;
  235.         if (first < 0) first = 0;
  236.         if (cur_y >= count) cur_y = count - 1;
  237.         if (cur_y < 0) cur_y = 0;
  238.         if (cur_x < 0) cur_x = 0;
  239. }
  240.  
  241. int llist::KeyRight()
  242. {
  243.         if (cur_x < column_max)
  244.         {
  245.                 cur_x++;
  246.         }
  247.         else
  248.         {
  249.                 if (!KeyDown()) return 0;
  250.                 cur_x = 0;
  251.         }
  252.         return 1;
  253. }
  254.  
  255. int llist::KeyLeft()
  256. {
  257.         if (cur_x > 0)
  258.         {
  259.                 cur_x--;
  260.         }
  261.         else
  262.         {
  263.                 if (!KeyUp()) return 0;
  264.                 cur_x = column_max;
  265.         }
  266.         return 1;
  267. }
  268.  
  269.  
  270. void llist_copy(dword dest, src)
  271. {
  272.         EDI = dest;
  273.         ESI = src;
  274.         EDI.llist.x = ESI.llist.x;
  275.         EDI.llist.y = ESI.llist.y;
  276.         EDI.llist.w = ESI.llist.w;
  277.         EDI.llist.h = ESI.llist.h;
  278.         EDI.llist.item_h = ESI.llist.item_h;
  279.         EDI.llist.text_y = ESI.llist.text_y;
  280.         EDI.llist.font_w = ESI.llist.font_w;
  281.         EDI.llist.font_h = ESI.llist.font_h;
  282.         EDI.llist.font_type = ESI.llist.font_type;
  283.         EDI.llist.count = ESI.llist.count;
  284.         EDI.llist.visible = ESI.llist.visible;
  285.         EDI.llist.first = ESI.llist.first;
  286.         EDI.llist.cur_y = ESI.llist.cur_y;
  287.         EDI.llist.column_max = ESI.llist.column_max;
  288.         EDI.llist.active = ESI.llist.active;
  289. }
  290.  
  291. #endif