Subversion Repositories Kolibri OS

Rev

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