Subversion Repositories Kolibri OS

Rev

Rev 7783 | Rev 7796 | 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.  
  11. llist menu1;
  12. collection names;
  13. collection hotkeys;
  14.  
  15. int selected, win_x, win_y;
  16.  
  17. int max_name_len;
  18. int max_hotkey_len;
  19.  
  20. void GetWindowPosition()
  21. {
  22.         int position;
  23.         shared_mem = memopen(#shared_name, 16, SHM_OPEN + SHM_WRITE);
  24.         selected = ESDWORD[shared_mem     ];
  25.         win_x    = ESDWORD[shared_mem +  4];
  26.         win_y    = ESDWORD[shared_mem +  8];
  27.         position = ESDWORD[shared_mem + 12];
  28.         if (position == MENU_ALIGN_TOP_RIGHT) win_x -= menu1.w;
  29.         if (position == MENU_ALIGN_BOT_LEFT) win_y -= menu1.h;
  30.         if (position == MENU_ALIGN_BOT_RIGHT) {
  31.                 win_x -= menu1.w;
  32.                 win_y -= menu1.h;
  33.         }
  34. }
  35.  
  36. void GetMenuWidths()
  37. {
  38.         int i;
  39.         for (i=0; i<names.count; i++) {
  40.                 max_name_len = math.max(max_name_len, strlen(names.get(i)));
  41.         }
  42.         for (i=0; i<hotkeys.count; i++) {
  43.                 max_hotkey_len = math.max(max_hotkey_len, strlen(hotkeys.get(i)));
  44.         }
  45.         max_name_len = max_name_len * 6;
  46.         max_hotkey_len *= 6;
  47.         if (max_hotkey_len) max_name_len += 12;
  48. }
  49.  
  50. void GetMenuItems(dword current_name)
  51. {
  52.         dword next_name = strchr(current_name, '\n');
  53.         dword hotkey = strchr(current_name, '|');
  54.  
  55.         ESBYTE[next_name] = '\0';
  56.  
  57.         if (hotkey) && (hotkey < next_name) {
  58.                 ESBYTE[hotkey] = '\0';
  59.         } else {
  60.                 if (hotkey) && (!next_name) {
  61.                         ESBYTE[hotkey] = '\0';
  62.                 } else {
  63.                         hotkey = " ";
  64.                 }
  65.         }
  66.  
  67.         hotkeys.add(hotkey+1);
  68.         names.add(current_name);
  69.  
  70.         if (next_name) GetMenuItems(next_name+2);
  71. }
  72.  
  73. void main()
  74. {
  75.         proc_info Form;
  76.  
  77.         if (!param) die("'Menu component is for developers only' -I");
  78.  
  79.         GetMenuItems(#param);
  80.         GetMenuWidths();
  81.  
  82.         menu1.count = names.count;
  83.         menu1.SetFont(6, 9, 0x80);
  84.         menu1.SetSizes(2,2, max_name_len + max_hotkey_len + 23, menu1.count*ITEM_H, ITEM_H);
  85.         menu1.cur_y = -1;
  86.  
  87.         GetWindowPosition();
  88.  
  89.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE);
  90.         loop() switch(WaitEvent())
  91.         {
  92.                 case evMouse:                  
  93.                         GetProcessInfo(#Form, SelfInfo);
  94.                         if (!CheckActiveProcess(Form.ID)) exit();
  95.                         mouse.get();
  96.                         if (menu1.ProcessMouse(mouse.x, mouse.y)) draw_list();
  97.                         if (mouse.lkm)&&(mouse.up) click();
  98.                         break;
  99.  
  100.                 case evKey:
  101.                         GetKeys();
  102.                         ProcessKeys();
  103.                         break;
  104.  
  105.                 case evReDraw:
  106.                         DefineAndDrawWindow(win_x, win_y, menu1.w+4, menu1.h+4, 0x01, 0, 0, 0x01fffFFF);
  107.                         system.color.get();
  108.                         Draw3DPopup(0,0,menu1.w+2,menu1.h+2);
  109.                         draw_list();
  110.         }
  111. }
  112.  
  113. void ProcessKeys()
  114. {
  115.         switch(key_scancode)
  116.         {
  117.                 case SCAN_CODE_ESC:
  118.                         exit();
  119.  
  120.                 case SCAN_CODE_ENTER:
  121.                         click();
  122.  
  123.                 case SCAN_CODE_DOWN:
  124.                         if (!menu1.KeyDown()) menu1.KeyHome();
  125.                         draw_list();
  126.                         break;
  127.  
  128.                 case SCAN_CODE_UP:
  129.                         if (!menu1.KeyUp()) menu1.KeyEnd();
  130.                         draw_list();
  131.                         break;
  132.  
  133.                 default:
  134.                         if (menu1.ProcessKey(key_scancode)) draw_list();
  135.         }
  136. }
  137.  
  138. void draw_list()
  139. {
  140.         int i, item_y;
  141.         dword name_color;
  142.         dword hotkey_color;
  143.  
  144.         static dword inactive_background_color;
  145.         static dword active_background_color;
  146.         static dword active_top_border_color;
  147.         static dword inactive_text_shadow_color;
  148.         static bool skin_dark;
  149.  
  150.         static bool colors_set;
  151.         if (!colors_set) {
  152.                 colors_set = true;
  153.                 inactive_background_color = MixColors(system.color.work, 0xFFFfff,230);
  154.                 active_background_color = MixColors(system.color.work_button, system.color.work,230);
  155.                 active_top_border_color = MixColors(system.color.work_graph, system.color.work_button,240);
  156.                 inactive_text_shadow_color = MixColors(system.color.work,0xFFFfff,120);
  157.                 skin_dark = skin_is_dark();
  158.         }
  159.  
  160.         for (i=0; i<menu1.count; i++;)
  161.         {
  162.                 item_y = i*ITEM_H+menu1.y;
  163.                 if (i==menu1.cur_y) {
  164.                         hotkey_color = name_color = system.color.work_button_text;
  165.                         DrawBar(menu1.x, item_y+1,        menu1.w, ITEM_H-2, active_background_color);
  166.                         DrawBar(menu1.x, item_y,          menu1.w, 1, active_top_border_color);
  167.                         DrawBar(menu1.x, item_y+ITEM_H-1, menu1.w, 1, system.color.work_light);
  168.                 } else {
  169.                         name_color = system.color.work_text;
  170.                         hotkey_color = system.color.work_graph;
  171.                         DrawBar(menu1.x, item_y, menu1.w, ITEM_H, inactive_background_color);
  172.                         if (!skin_dark) WriteText(13+1, item_y + menu1.text_y +1, 0x80,
  173.                                 inactive_text_shadow_color, names.get(i));
  174.                 }
  175.                 WriteText(-strlen(hotkeys.get(i))*6 + 13 + max_name_len + max_hotkey_len,
  176.                         item_y + menu1.text_y, 0x80, hotkey_color, hotkeys.get(i));
  177.                 WriteText(13, item_y + menu1.text_y, 0x80, name_color, names.get(i));
  178.         }
  179.         if (selected) WriteText(5, selected-1*ITEM_H + menu1.y + menu1.text_y, 0x80, 0xEE0000, "\x10");
  180. }
  181.  
  182. void click()
  183. {
  184.         ESDWORD[shared_mem] = menu1.cur_y + 1;
  185.         ExitProcess();
  186. }
  187.  
  188. void exit()
  189. {
  190.         ESDWORD[shared_mem] = 0;
  191.         ExitProcess();
  192. }
  193.