Subversion Repositories Kolibri OS

Rev

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