Subversion Repositories Kolibri OS

Rev

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