Subversion Repositories Kolibri OS

Rev

Rev 8383 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #define MEMSIZE 4096*40
  2.  
  3. #include "../lib/gui.h"
  4. #include "../lib/collection.h"
  5. #include "../lib/list_box.h"
  6. #include "../lib/fs.h"
  7.  
  8. #define ITEM_H 19
  9. #define SEP_H 4
  10.  
  11. llist menu1;
  12. collection names=0;
  13. collection hotkeys=0;
  14.  
  15. int selected, win_x, win_y;
  16.  
  17. int max_name_len;
  18. int max_hotkey_len;
  19. int menu_w, menu_h;
  20.  
  21. void GetWindowPosition()
  22. {
  23.         int position;
  24.         shared_mem = memopen(#shared_name, 16, SHM_OPEN + SHM_WRITE);
  25.         selected = ESDWORD[shared_mem     ];
  26.         win_x    = ESDWORD[shared_mem +  4];
  27.         win_y    = ESDWORD[shared_mem +  8];
  28.         position = ESDWORD[shared_mem + 12];
  29.         if (position == MENU_TOP_RIGHT) win_x -= menu1.w;
  30.         if (position == MENU_BOT_LEFT) win_y -= menu1.h;
  31.         if (position == MENU_BOT_RIGHT) {
  32.                 win_x -= menu1.w;
  33.                 win_y -= menu1.h;
  34.         }
  35. }
  36.  
  37. void GetMenuWidths()
  38. {
  39.         int i;
  40.         for (i=0; i<names.count; i++) {
  41.                 max_name_len = math.max(max_name_len, strlen(names.get(i)));
  42.         }
  43.         for (i=0; i<hotkeys.count; i++) {
  44.                 max_hotkey_len = math.max(max_hotkey_len, strlen(hotkeys.get(i)));
  45.         }
  46.         max_name_len = max_name_len * 6;
  47.         max_hotkey_len *= 6;
  48.         if (max_hotkey_len) max_name_len += 12;
  49. }
  50.  
  51. void GetMenuItems(dword current_name)
  52. {
  53.         dword next_name = strchr(current_name, '\n');
  54.         dword hotkey = strchr(current_name, '|');
  55.  
  56.         ESBYTE[next_name] = '\0';
  57.  
  58.         if (hotkey) && (hotkey < next_name) {
  59.                 ESBYTE[hotkey] = '\0';
  60.         } else {
  61.                 if (hotkey) && (!next_name) {
  62.                         ESBYTE[hotkey] = '\0';
  63.                 } else {
  64.                         hotkey = " ";
  65.                 }
  66.         }
  67.  
  68.         hotkeys.add(hotkey+1);
  69.         names.add(current_name);
  70.  
  71.         if (next_name) GetMenuItems(next_name+2);
  72. }
  73.  
  74. int GetSeparatorsCount()
  75. {
  76.         int i, count=0;
  77.         for (i=0; i<names.count; i++) {
  78.                 if (streq(names.get(i), "-")) count++;
  79.         }
  80.         return count;
  81. }
  82.  
  83. int MoveMouseToHandleSeparators(int _mouse_y)
  84. {
  85.         int i, item_y=menu1.y;
  86.         int item_i=0;
  87.         for (i=0; i<menu1.count; i++;)
  88.         {
  89.                 if (streq(names.get(i), "-")) {
  90.                         item_y += SEP_H;
  91.                 } else {
  92.                         item_y += ITEM_H;
  93.                         item_i++;
  94.                 }
  95.                 if (_mouse_y >= item_y) && (_mouse_y < item_y + ITEM_H) {
  96.                         return item_i * ITEM_H + menu1.y;
  97.                 }
  98.         }
  99.         return _mouse_y;
  100. }
  101.  
  102. void main()
  103. {
  104.         proc_info Form;
  105.  
  106.         if (!param) RunProgram("/sys/network/WebView", "http://board.kolibrios.org/viewtopic.php?f=24&t=4233#p74599");
  107.  
  108.         GetMenuItems(#param);
  109.         GetMenuWidths();
  110.  
  111.         menu_w = max_name_len + max_hotkey_len + 23;
  112.         menu_h = GetSeparatorsCount() * SEP_H
  113.                 + calc(names.count - GetSeparatorsCount() * ITEM_H);
  114.  
  115.         menu1.count = names.count;
  116.         menu1.SetFont(6, 9, 0x80);
  117.         menu1.SetSizes(2,2, menu_w, menu_h, ITEM_H);
  118.         menu1.cur_y = -1;
  119.  
  120.         GetWindowPosition();
  121.  
  122.         @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE);
  123.         loop() switch(WaitEvent())
  124.         {
  125.                 case evMouse:                  
  126.                         GetProcessInfo(#Form, SelfInfo);
  127.                         if (!CheckActiveProcess(Form.ID)) exit();
  128.                         mouse.get();
  129.                         if (menu1.MouseOver(mouse.x, mouse.y)) {
  130.                                 mouse.y = MoveMouseToHandleSeparators(mouse.y);
  131.                                 if (menu1.ProcessMouse(mouse.x, mouse.y)) draw_list();
  132.                                 if (mouse.lkm)&&(mouse.up) click();    
  133.                         }
  134.                         break;
  135.  
  136.                 case evKey:
  137.                         ProcessKeys();
  138.                         break;
  139.  
  140.                 case evReDraw:
  141.                         DefineAndDrawWindow(win_x, win_y, menu1.w+4, menu1.h+4, 0x01, 0, 0, 0x01fffFFF);
  142.                         sc.get();
  143.                         Draw3DPopup(0,0,menu1.w+2,menu1.h+2);
  144.                         draw_list();
  145.         }
  146. }
  147.  
  148. void CorrectLastItem()
  149. {
  150.         if (menu1.cur_y > menu1.count - GetSeparatorsCount() - 1) {
  151.                 menu1.cur_y = menu1.count - GetSeparatorsCount() - 1;
  152.         }
  153. }
  154.  
  155. inline ProcessKeys()
  156. {
  157.         @GetKeyScancode();
  158.         switch(AL)
  159.         {
  160.                 case SCAN_CODE_ESC:
  161.                         exit();
  162.  
  163.                 case SCAN_CODE_ENTER:
  164.                         click();
  165.  
  166.                 case SCAN_CODE_DOWN:
  167.                         if (!menu1.KeyDown())
  168.                         || (menu1.count - menu1.cur_y - GetSeparatorsCount() -1 < 0) menu1.KeyHome();
  169.                         draw_list();
  170.                         break;
  171.  
  172.                 case SCAN_CODE_UP:
  173.                         if (!menu1.KeyUp()) {
  174.                                 menu1.KeyEnd();
  175.                                 CorrectLastItem();
  176.                         }
  177.                         draw_list();
  178.                         break;
  179.  
  180.                 case SCAN_CODE_END:
  181.                         menu1.KeyEnd();
  182.                         CorrectLastItem();
  183.                         draw_list();
  184.                         break;                 
  185.  
  186.                 default:
  187.                         if (menu1.ProcessKey(AL)) draw_list();
  188.         }
  189. }
  190.  
  191. void draw_list()
  192. {
  193.         int i, item_y=menu1.y, item_i=0;
  194.         dword name_color;
  195.         dword hotkey_color;
  196.  
  197.         static dword inactive_background_color;
  198.         static dword active_background_color;
  199.         static dword active_top_border_color;
  200.         static dword inactive_text_shadow_color;
  201.         static bool skin_dark;
  202.  
  203.         static bool colors_set;
  204.         if (!colors_set) {
  205.                 colors_set = true;
  206.                 inactive_background_color = MixColors(sc.work, 0xFFFfff,230);
  207.                 active_background_color = MixColors(sc.button, sc.work,230);
  208.                 active_top_border_color = MixColors(sc.work_graph, sc.button,240);
  209.                 inactive_text_shadow_color = MixColors(sc.work,0xFFFfff,120);
  210.                 skin_dark = skin_is_dark();
  211.         }
  212.  
  213.         for (i=0; i<menu1.count; i++;)
  214.         {
  215.                 if (streq(names.get(i), "-")) {
  216.                         DrawBar(menu1.x, item_y+0, menu1.w, 1, inactive_background_color);
  217.                         DrawBar(menu1.x-1, item_y+1, menu1.w+1, 1, sc.work_dark);
  218.                         DrawBar(menu1.x, item_y+2, menu1.w, 1, sc.work_light);
  219.                         DrawBar(menu1.x, item_y+3, menu1.w, 1, inactive_background_color);
  220.                         //DrawBar(menu1.x, item_y+0, menu1.w, 4, inactive_background_color);
  221.                         //DrawBar(13, item_y+1, menu1.w-24, 1, sc.work_dark);
  222.                         //DrawBar(13, item_y+2, menu1.w-24, 1, sc.work_light);
  223.                         item_y += SEP_H;
  224.                 } else {
  225.                         if (item_i==menu1.cur_y) {
  226.                                 hotkey_color = name_color = sc.button_text;
  227.                                 DrawBar(menu1.x, item_y+1,        menu1.w, ITEM_H-2, active_background_color);
  228.                                 DrawBar(menu1.x, item_y,          menu1.w, 1, active_top_border_color);
  229.                                 DrawBar(menu1.x, item_y+ITEM_H-1, menu1.w, 1, sc.work_light);
  230.                         } else {
  231.                                 name_color = sc.work_text;
  232.                                 hotkey_color = sc.work_graph;
  233.                                 DrawBar(menu1.x, item_y, menu1.w, ITEM_H, inactive_background_color);
  234.                                 if (!skin_dark) WriteText(13+1, item_y + menu1.text_y +1, 0x80,
  235.                                         inactive_text_shadow_color, names.get(i));
  236.                         }
  237.                         WriteText(-strlen(hotkeys.get(i))*6 + 13 + max_name_len + max_hotkey_len,
  238.                                 item_y + menu1.text_y, 0x80, hotkey_color, hotkeys.get(i));
  239.                         WriteText(13, item_y + menu1.text_y, 0x80, name_color, names.get(i));
  240.                         item_y += ITEM_H;
  241.                         item_i++;              
  242.                 }
  243.         }
  244.         if (selected) WriteText(5, selected-1*ITEM_H + menu1.y + menu1.text_y, 0x80, 0xEE0000, "\x10");
  245. }
  246.  
  247. void click()
  248. {
  249.         ESDWORD[shared_mem] = menu1.cur_y + 1;
  250.         ExitProcess();
  251. }
  252.  
  253. void exit()
  254. {
  255.         ESDWORD[shared_mem] = 0;
  256.         ExitProcess();
  257. }
  258.