Subversion Repositories Kolibri OS

Rev

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