Subversion Repositories Kolibri OS

Rev

Rev 5784 | Rev 5829 | 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_values();
  38. };
  39.  
  40. void llist::debug_values()
  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.                 cur_x_temp = xx - x / item_w;
  112.                 if (cur_x_temp != cur_x) && (cur_x_temp<column_max)
  113.                 {
  114.                         cur_x = cur_x_temp;
  115.                         ret = 1;
  116.                 }
  117.         }
  118.         return ret;
  119. }
  120.  
  121. int llist::ProcessKey(dword key)
  122. {
  123.         switch(key)
  124.         {
  125.                 case SCAN_CODE_DOWN: return KeyDown();
  126.                 case SCAN_CODE_UP:   return KeyUp();
  127.                 case SCAN_CODE_HOME: return KeyHome();
  128.                 case SCAN_CODE_END:  return KeyEnd();
  129.                 case SCAN_CODE_PGUP: return KeyPgUp();
  130.                 case SCAN_CODE_PGDN: return KeyPgDown();
  131.         }
  132.         if (horisontal_selelection) switch(key)
  133.         {
  134.                 case SCAN_CODE_LEFT:  return KeyLeft();
  135.                 case SCAN_CODE_RIGHT: return KeyRight();
  136.         }
  137.         return 0;
  138. }
  139.  
  140. int llist::KeyDown()
  141. {
  142.         if (no_selection)
  143.         {
  144.                 if (visible + first >= count) return 0;
  145.                 first++;
  146.                 return 1;              
  147.         }
  148.  
  149.         if (cur_y-first+1<visible)
  150.         {
  151.                 if (cur_y + 1 >= count) return 0;
  152.                 cur_y++;
  153.         }
  154.         else
  155.         {
  156.                 if (visible + first >= count) return 0;
  157.                 first++;
  158.                 cur_y++;
  159.         }
  160.         if (cur_y < first) || (cur_y > first + visible)
  161.         {
  162.                 first = cur_y;
  163.                 CheckDoesValuesOkey();
  164.         }
  165.         return 1;
  166. }
  167.  
  168. int llist::KeyUp()
  169. {
  170.         if (no_selection)
  171.         {
  172.                 if (first == 0) return 0;
  173.                 first--;
  174.                 return 1;
  175.         }
  176.  
  177.         if (cur_y > first)
  178.         {
  179.                 cur_y--;
  180.         }
  181.         else
  182.         {
  183.                 if (first == 0) return 0;
  184.                 first--;
  185.                 cur_y--;
  186.         }
  187.         if (cur_y < first) || (cur_y > first + visible)
  188.         {
  189.                 first = cur_y;
  190.                 CheckDoesValuesOkey();
  191.         }
  192.         return 1;
  193. }
  194.  
  195. int llist::KeyHome()
  196. {
  197.         if (cur_y==0) && (first==0) return 0;
  198.         cur_y = first = 0;
  199.         return 1;
  200. }
  201.  
  202. int llist::KeyEnd()
  203. {
  204.         if (cur_y==count-1) && (first==count-visible) return 0;
  205.         cur_y = count-1;
  206.         first = count - visible;
  207.         return 1;
  208. }
  209.  
  210. int llist::KeyPgUp()
  211. {
  212.         if (count <= visible) return KeyHome();
  213.         if (first == 0) return 0;
  214.         first -= visible;
  215.         CheckDoesValuesOkey();
  216.         return 1;
  217. }
  218.  
  219. int llist::KeyPgDown()
  220. {
  221.         if (count <= visible) return KeyEnd();
  222.         if (first == count - visible) return 0;
  223.         first += visible;
  224.         CheckDoesValuesOkey();
  225.         return 1;
  226. }
  227.  
  228. void llist::CheckDoesValuesOkey()
  229. {
  230.         if (visible + first > count) first = count - visible;
  231.         if (first < 0) first = 0;
  232.         if (cur_y >= count) cur_y = count - 1;
  233.         if (cur_y < 0) cur_y = 0;
  234.         if (cur_x < 0) cur_x = 0;
  235. }
  236.  
  237. int llist::KeyRight()
  238. {
  239.         if (cur_x < column_max)
  240.         {
  241.                 cur_x++;
  242.         }
  243.         else
  244.         {
  245.                 if (!KeyDown()) return 0;
  246.                 cur_x = 0;
  247.         }
  248.         return 1;
  249. }
  250.  
  251. int llist::KeyLeft()
  252. {
  253.         if (cur_x > 0)
  254.         {
  255.                 cur_x--;
  256.         }
  257.         else
  258.         {
  259.                 if (!KeyUp()) return 0;
  260.                 cur_x = column_max;
  261.         }
  262.         return 1;
  263. }
  264.  
  265.  
  266. void llist_copy(dword dest, src)
  267. {
  268.         EDI = dest;
  269.         ESI = src;
  270.         EDI.llist.x = ESI.llist.x;
  271.         EDI.llist.y = ESI.llist.y;
  272.         EDI.llist.w = ESI.llist.w;
  273.         EDI.llist.h = ESI.llist.h;
  274.         EDI.llist.item_h = ESI.llist.item_h;
  275.         EDI.llist.text_y = ESI.llist.text_y;
  276.         EDI.llist.font_w = ESI.llist.font_w;
  277.         EDI.llist.font_h = ESI.llist.font_h;
  278.         EDI.llist.font_type = ESI.llist.font_type;
  279.         EDI.llist.count = ESI.llist.count;
  280.         EDI.llist.visible = ESI.llist.visible;
  281.         EDI.llist.first = ESI.llist.first;
  282.         EDI.llist.cur_y = ESI.llist.cur_y;
  283.         EDI.llist.column_max = ESI.llist.column_max;
  284.         EDI.llist.active = ESI.llist.active;
  285. }
  286.  
  287. #endif