Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2. SOFTWARE CENTER v2.86
  3. */
  4.  
  5. #define MEMSIZE 4096 * 15
  6. #include "..\lib\strings.h"
  7. #include "..\lib\mem.h"
  8. #include "..\lib\io.h"
  9. #include "..\lib\gui.h"
  10.  
  11. #include "..\lib\obj\libio.h"
  12. #include "..\lib\obj\libimg.h"
  13. #include "..\lib\obj\libini.h"
  14. #include "..\lib\kfont.h"
  15. #include "..\lib\list_box.h"
  16. #include "..\lib\collection.h"
  17.  
  18. proc_info Form;
  19. llist list;
  20. collection app_path_collection=0;
  21. bool kolibrios_mounted;
  22.  
  23. int window_width,
  24.         window_height;
  25.  
  26. int list_pos,
  27.         row,
  28.         col,
  29.         default_icon;
  30.  
  31. char window_title[128],
  32.          settings_ini_path[256];
  33.  
  34. bool small_screen = false;
  35.  
  36. struct SW_COLORS
  37.  {
  38.         dword list_bg;
  39.         dword text;
  40.         dword graph;
  41.         dword dark;
  42.         dword light;
  43.  } swc;
  44.  
  45. block selection[128];
  46.  
  47. void load_ini_config(dword _ini_path)
  48. {
  49.         _ini ini;
  50.         ini.path = _ini_path;
  51.         ini.section = "Config";
  52.         ini.GetString("title", #window_title, sizeof(window_title), "Software widget");
  53.         window_width = ini.GetInt("win_width", 690);
  54.         list.item_w  = ini.GetInt("cell_w", 73);
  55.         list.item_h  = ini.GetInt("cell_h", 71);
  56.         default_icon = ini.GetInt("default_icon", 2);
  57. }
  58.  
  59. void main()
  60. {  
  61.         dword id;
  62.         kfont.init(DEFAULT_FONT);
  63.         load_dll(libio,  #libio_init,1);
  64.         load_dll(libimg, #libimg_init,1);
  65.         load_dll(libini, #lib_init,1);
  66.  
  67.         kolibrios_mounted = dir_exists("/kolibrios");
  68.  
  69.         if (param) {
  70.                 strcpy(#settings_ini_path, #param);
  71.         } else {
  72.                 strcpy(#settings_ini_path, "/sys/settings/");
  73.                 strcat(#settings_ini_path, I_Path + strrchr(I_Path, '/'));
  74.                 strcat(#settings_ini_path, ".ini");            
  75.         }
  76.        
  77.         load_ini_config(#settings_ini_path);
  78.         list.cur_y = -1;
  79.         list.y = 32;
  80.  
  81.         DrawList();
  82.         window_height = row+1*list.item_h + list_pos + skin_height + 15;
  83.         if (window_height>screen.height) {
  84.                 window_width = screen.width;
  85.                 list.item_h -= 5;
  86.                 window_height = row+1*list.item_h + list_pos + skin_height + 15;
  87.                 small_screen = true;
  88.         }
  89.  
  90.         loop() switch(@WaitEvent())
  91.         {
  92.                 case evKey:
  93.                         key_scancode = @GetKeyScancode();
  94.                         if (SCAN_CODE_LEFT == key_scancode) key_scancode = SCAN_CODE_UP;
  95.                         if (SCAN_CODE_RIGHT == key_scancode) key_scancode = SCAN_CODE_DOWN;
  96.                         if (list.ProcessKey(key_scancode)) DrawSelection();
  97.                         if (SCAN_CODE_ENTER == key_scancode) EventIconClick(list.cur_y);
  98.                         break;
  99.  
  100.                 case evButton:
  101.                         id = @GetButtonID();              
  102.                         if (id==1) ExitProcess();
  103.                         if (id>=100) EventIconClick(id-100);
  104.                         break;
  105.  
  106.                 case evReDraw:
  107.                         SetAppColors();
  108.                         DefineAndDrawWindow(screen.width-window_width/2,screen.height-window_height/2,window_width,window_height,0x74,sc.work,"",0);
  109.                         GetProcessInfo(#Form, SelfInfo);
  110.                         if (Form.status_window>2) {
  111.                                 DrawTitle(#window_title);
  112.                                 break;
  113.                         }
  114.                         if (small_screen) {
  115.                                 DrawTitle(#window_title);
  116.                                 list.y = 0;    
  117.                         } else {
  118.                                 DrawTitle(NULL);
  119.                                 DrawTopBar();
  120.                         }
  121.                         DrawList();
  122.                         DrawBar(0, row +1 * list.item_h + list_pos, Form.cwidth, -row - 1 * list.item_h - list_pos + Form.cheight, swc.list_bg);
  123.                         DrawSelection();
  124.         }
  125. }
  126.  
  127. void SetAppColors()
  128. {
  129.         dword bg_col, old_list_bg_color;
  130.         sc.get();
  131.         old_list_bg_color = swc.list_bg;
  132.         bg_col = sc.work;
  133.         if (skin_is_dark())
  134.         {
  135.                 //dark colors
  136.                 swc.list_bg = sc.work;
  137.                 swc.text = sc.work_text;
  138.                 swc.dark = sc.work_dark;
  139.                 swc.light = sc.work_light;
  140.         } else {
  141.                 //light colors
  142.                 swc.list_bg = 0xF3F3F3;
  143.                 swc.text = 0x000000;
  144.                 swc.dark = 0xDCDCDC;
  145.                 swc.light = 0xFCFCFC;
  146.         }
  147. }
  148.  
  149. void DrawList() {
  150.         list.count = 0;
  151.         row = -1;
  152.         app_path_collection.drop();
  153.         list_pos = list.y;
  154.         list.column_max = window_width - 10 / list.item_w;
  155.         ini_enum_sections stdcall (#settings_ini_path, #process_sections);
  156.         list.visible = list.count;
  157. }
  158.  
  159. byte draw_icons_from_section(dword key_value, key_name, sec_name, f_name)
  160. {
  161.         int icon_id = default_icon,
  162.                 icon_char_pos;
  163.         int space_pos;
  164.  
  165.         dword icon_x, icon_y, text_x, text_y;
  166.  
  167.         //do not show items located in /kolibrios/ if this directory not mounted
  168.         if (!strncmp(key_value, "/kolibrios/", 11)) || (!strncmp(key_value, "/k/", 3))
  169.                 if (!kolibrios_mounted) return true;
  170.  
  171.         if (col==list.column_max) {
  172.                 row++;
  173.                 col=0;
  174.         }
  175.  
  176.         if (col==0) DrawBar(0, row * list.item_h + list_pos, Form.cwidth, list.item_h, swc.list_bg);
  177.         DefineButton(col*list.item_w+6, row*list.item_h + list_pos,list.item_w,list.item_h-3,list.count + 100 + BT_HIDE,0);
  178.  
  179.         icon_char_pos = strchr(key_value, ',');
  180.         icon_x = col*list.item_w+calc(list.item_w/2)-10;
  181.         icon_y = row*list.item_h+5 + list_pos;
  182.         selection[list.count].x = icon_x-2;
  183.         selection[list.count].y = icon_y-2;
  184.         if (icon_char_pos) ESBYTE[icon_char_pos] = '\0'; //delete icon from string
  185.         app_path_collection.add(key_value);
  186.  
  187.         text_x = col*list.item_w+5;
  188.         text_y = list.item_h - 40 / 2;
  189.         if (!strchr(key_name, ' ')) {//|| (kfont.getsize(key_name)+30<list.item_w) <== too slow
  190.                 kfont.WriteIntoWindowCenter(text_x, row*list.item_h+46 + list_pos, list.item_w,0, swc.list_bg, swc.text, 12, key_name);
  191.         } else {
  192.                 space_pos = strrchr(key_name, ' ');
  193.                 ESBYTE[key_name+space_pos-1] = '\0';
  194.                 kfont.WriteIntoWindowCenter(text_x, row*list.item_h+46 + list_pos - 2, list.item_w,0, swc.list_bg, swc.text, 12, key_name);
  195.                 kfont.WriteIntoWindowCenter(text_x, row*list.item_h+46 + list_pos + 13, list.item_w,0, swc.list_bg, swc.text, 12, key_name+space_pos);
  196.         }
  197.         if (icon_char_pos) icon_id = atoi(icon_char_pos+1);
  198.         if (Form.cwidth) DrawIcon32(icon_x, icon_y, swc.list_bg, icon_id);
  199.         list.count++;
  200.         col++;
  201.         return true;
  202. }
  203.  
  204.  
  205. byte process_sections(dword sec_name, f_name)
  206. {
  207.         static int old_row; //to detect empty sections
  208.         int text_len;
  209.         if (!strcmp(sec_name, "Config")) return true;
  210.  
  211.         if ((col==0) && (row==old_row)) {
  212.                 list_pos -= 28;
  213.         } else {
  214.                 row++;
  215.         }
  216.         col = 0;
  217.         old_row = row;
  218.  
  219.         if (!small_screen) {
  220.                 DrawBar(0, row * list.item_h + list_pos, Form.cwidth , 29, swc.list_bg);
  221.                 text_len = kfont.WriteIntoWindow(10, row * list.item_h + 10 + list_pos, swc.list_bg, swc.text, 15, sec_name);
  222.                 DrawBar(text_len+20, row * list.item_h + list_pos + 20, Form.cwidth-text_len-20, 1, swc.dark);
  223.                 DrawBar(text_len+20, row * list.item_h + list_pos + 21, Form.cwidth-text_len-20, 1, swc.light);
  224.                 list_pos += 29;        
  225.         }
  226.         ini_enum_keys stdcall (f_name, sec_name, #draw_icons_from_section);
  227.         return true;
  228. }
  229.  
  230. void DrawTopBar()
  231. {
  232.         DrawBar(0,0,Form.cwidth, list.y-2, sc.work);
  233.         DrawBar(0,list.y-2, Form.cwidth, 1, MixColors(sc.work, sc.work_graph, 180));
  234.         DrawBar(0,list.y-1, Form.cwidth, 1, sc.work_graph);
  235.         kfont.WriteIntoWindowCenter(0,5, Form.cwidth, list.y, sc.work, sc.work_text, 16, #window_title);
  236. }
  237.  
  238. void EventIconClick(dword appid)
  239. {
  240.         char run_app_path[4096];
  241.         dword app_path = app_path_collection.get(appid);
  242.         dword param_pos = strchr(app_path, '|');
  243.         if (param_pos) {
  244.                 ESBYTE[param_pos] = NULL;
  245.                 param_pos++;
  246.         }
  247.  
  248.         // the next block is created to save some space in ramdisk{
  249.         //
  250.         // convert relative path to absolute      "calc"     => "/sys/calc"
  251.         // convert short kolibrios path to full   "/k/calc"  => "/kolibrios/calc"
  252.         // convert short kolibrios path to full   "/kg/2048" => "/kolibrios/games/2048"
  253.         // other copy => as is
  254.         if (ESBYTE[app_path]!='/') {
  255.                 strcpy(#run_app_path, "/sys/");
  256.         }
  257.         else if (!strncmp(app_path, "/k/",3)) {
  258.                 strcpy(#run_app_path, "/kolibrios/");
  259.                 app_path+=3;
  260.         }
  261.         else if (!strncmp(app_path, "/kg/",3)) {
  262.                 strcpy(#run_app_path, "/kolibrios/games/");
  263.                 app_path+=4;
  264.         }
  265.         strcat(#run_app_path, app_path);
  266.         // }end
  267.  
  268.         if (file_exists(#run_app_path)) {
  269.                 io.run(#run_app_path, param_pos); //0 or offset
  270.                 if (param_pos) ESBYTE[param_pos - 1] = '|';
  271.         } else {
  272.                 notify("'Application not found' -E");
  273.         }
  274. }
  275.  
  276. void DrawSelection()
  277. {
  278.         int i;
  279.         dword col;
  280.         for (i=0; i<list.count; i++) {
  281.                 if (i==list.cur_y) col=0x0080FF; else col=swc.list_bg;
  282.                 DrawWideRectangle(selection[i].x, selection[i].y, 36, 36, 2, col);
  283.         }
  284. }
  285.  
  286.  
  287. stop:
  288.