Subversion Repositories Kolibri OS

Rev

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

  1. //11.03.12 - start!
  2. //TODO: select current value at startap
  3.  
  4. #define MEMSIZE 1024*70
  5. #include "../lib/mem.h"
  6. #include "../lib/strings.h"
  7. #include "../lib/io.h"
  8. #include "../lib/list_box.h"
  9. #include "../lib/gui.h"
  10.  
  11. #include "../lib/obj/box_lib.h"
  12. #include "../lib/obj/proc_lib.h"
  13. #include "../lib/obj/libini.h"
  14.  
  15. #include "../lib/patterns/select_list.h"
  16. #include "../lib/patterns/simple_open_dialog.h"
  17. #include "../lib/patterns/restart_process.h"
  18.  
  19. #include "ui_elements_preview.h"
  20. #include "const.h"
  21.  
  22. //===================================================//
  23. //                                                   //
  24. //                       DATA                        //
  25. //                                                   //
  26. //===================================================//
  27.  
  28. int active_skin, active_wallpaper, active_screensaver;
  29.  
  30. checkbox update_docky = { T_UPDATE_DOCK, false };
  31.  
  32. checkbox optionbox_stretch = { T_CHECKBOX_STRETCH, false };
  33. checkbox optionbox_tiled = { T_CHECKBOX_TILED, false };
  34. checkbox optionbox_auto = { T_CHECKBOX_AUTO, true };
  35.  
  36. char ss_available[200];
  37.  
  38. collection list;
  39.  
  40. //===================================================//
  41. //                                                   //
  42. //                       CODE                        //
  43. //                                                   //
  44. //===================================================//
  45.  
  46. void main()
  47. {  
  48.         int id, i;
  49.         load_dll(boxlib, #box_lib_init,0);
  50.         load_dll(libini, #lib_init,1);
  51.         load_dll(Proc_lib, #OpenDialog_init,0);
  52.         OpenDialog_init stdcall (#o_dialog);
  53.  
  54.         GetIniSettings();
  55.  
  56.         tabs.add(#t_skins, #EventTabSkinsClick);       
  57.         tabs.add(#t_wallpapers, #EventTabWallpappersClick);
  58.         tabs.add(#t_screensaver, #EventTabScreensaverClick);
  59.         tabs.draw_active_tab();
  60.  
  61.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  62.         loop() switch(WaitEvent())
  63.         {
  64.                 case evMouse:
  65.                         SelectList_ProcessMouse();
  66.                         if (tabs.active_tab == TAB_SCREENSAVERS) {
  67.                                 scrollbar_h_mouse stdcall (#ss_timeout);
  68.                                 if (ss_timeout.redraw) {
  69.                                         draw_timeout();
  70.                                         ss_timeout.redraw = false; //reset flag
  71.                                 }
  72.                         }
  73.                         break;
  74.  
  75.                 case evButton:
  76.                         id=GetButtonID();
  77.                         if (id==1) EventExit();
  78.                         tabs.click(id);
  79.                         if (tabs.active_tab == TAB_SKINS) {
  80.                                 checkbox1.click(id);
  81.                                 spinbox1.click(id);
  82.                                 if (update_docky.click(id)) EventUpdateDocky();
  83.                         }
  84.                         if (tabs.active_tab == TAB_WALLPAPERS) {
  85.                                 if (id==BTN_SELECT_WALLP_FOLDER) EventSelectWallpFolder();
  86.                                 if (optionbox_stretch.click(id)) EventSetWallpMode(1,0,0);
  87.                                 if (optionbox_tiled.click(id)) EventSetWallpMode(0,1,0);
  88.                                 if (optionbox_auto.click(id)) EventSetWallpMode(0,0,1);
  89.                         }
  90.                         if (tabs.active_tab == TAB_SCREENSAVERS) {
  91.                                 if (id==BTN_TEST_SCREENSAVER) EventOpenFile();
  92.                                 if (id==BTN_SET_SCREENSAVER) EventSetSs();
  93.                         }
  94.                         break;
  95.          
  96.                 case evKey:
  97.                         GetKeys();
  98.                         if (select_list.ProcessKey(key_scancode)) { EventApply(); break; }
  99.                         if (key_scancode==SCAN_CODE_ENTER) { EventOpenFile(); break; }
  100.                         if (key_scancode==SCAN_CODE_DEL) { EventDeleteFile(); break; }
  101.                         if (key_scancode==SCAN_CODE_TAB) {
  102.                                 id = tabs.active_tab+1;
  103.                                 if(id==3)id=0;
  104.                                 tabs.click(id + tabs.base_id);
  105.                                 break;
  106.                         }
  107.                         for (i=select_list.cur_y+1; i<select_list.count; i++)
  108.                         {
  109.                                 id = list.get(i) + strrchr(list.get(i), '/');
  110.                                 if (ESBYTE[id]==key_ascii) || (ESBYTE[id]==key_ascii-32)
  111.                                 {
  112.                                         select_list.cur_y = i - 1;
  113.                                         select_list.KeyDown();
  114.                                         EventApply();
  115.                                         break;
  116.                                 }
  117.                         }
  118.                         break;
  119.                  
  120.                  case evReDraw:        
  121.                         draw_window();
  122.    }
  123. }
  124.  
  125. void draw_window()
  126. {
  127.         sc.get();
  128.         DefineAndDrawWindow(screen.width-WIN_W-9/2,80,WIN_W+9,WIN_H+4+skin_height,0x34,sc.work,WINDOW_HEADER,0);
  129.  
  130.         tabs.draw();
  131.         draw_icon_16w(tabs.x + TAB_P, LP+5, 17);
  132.         draw_icon_16w(sizeof(t_skins)-1*8 + TAB_P + TAB_P + tabs.x, LP+5, 6);
  133.         draw_icon_16w(sizeof(t_wallpapers)+sizeof(t_skins)-2*8 + TAB_P + TAB_P + TAB_P + tabs.x, LP+5, 61);
  134.  
  135.         $push select_list.cur_y
  136.         SelectList_Init(
  137.                 LP,
  138.                 PANEL_H,
  139.                 LIST_W,
  140.                 WIN_H - LP - PANEL_H
  141.                 );
  142.         $pop select_list.cur_y
  143.  
  144.         DrawBar(RIGHTx, PANEL_H, RIGHTw, WIN_H-PANEL_H-LP, sc.work);
  145.  
  146.         SelectList_Draw();
  147.         SelectList_DrawBorder();
  148.  
  149.         if (tabs.active_tab == TAB_SKINS)
  150.         {
  151.                 DrawFrame(RIGHTx, PANEL_H+5, RIGHTw, RIGHTh, T_UI_PREVIEW);
  152.                 DrawUiElementsPreview(RIGHTx+20, PANEL_H+5, RIGHTh);
  153.                 if (CheckProcessExists("@DOCKY")) update_docky.draw(RIGHTx, PANEL_H+250);
  154.         }
  155.         if (tabs.active_tab == TAB_WALLPAPERS)
  156.         {
  157.                 DrawFrame(RIGHTx, PANEL_H+5, 180, 105, T_PICTURE_MODE);
  158.                 optionbox_stretch.draw(RIGHTx+14, PANEL_H+25);
  159.                 optionbox_tiled.draw(RIGHTx+14, PANEL_H+52);
  160.                 optionbox_auto.draw(RIGHTx+14, PANEL_H+79);
  161.                 DrawStandartCaptButton(RIGHTx, PANEL_H+130, BTN_SELECT_WALLP_FOLDER, T_SELECT_FOLDER);
  162.         }
  163.         if (tabs.active_tab == TAB_SCREENSAVERS)
  164.         {
  165.                 draw_timeout();
  166.                 ss_timeout.line_col = sc.work_graph;
  167.                 ss_timeout.frnt_col = sc.work;
  168.                 scrollbar_h_draw stdcall (#ss_timeout);
  169.                 DrawRectangle(RIGHTx, RIGHTy+25, RIGHTw-20, 15, sc.work_graph);
  170.                 ESI = DrawStandartCaptButton(RIGHTx, PANEL_H + 65, BTN_TEST_SCREENSAVER, T_SS_PREVIEW);
  171.                 DrawStandartCaptButton(RIGHTx+ESI, PANEL_H + 65, BTN_SET_SCREENSAVER, T_SS_SET);
  172.         }
  173. }
  174.  
  175. void draw_timeout()
  176. {
  177.         miniprintf(#param, T_SS_TIMEOUT, ss_timeout.position+1);
  178.         WriteTextWithBg(RIGHTx, PANEL_H, 0xD0, sc.work_text, #param, sc.work); 
  179. }
  180.  
  181. bool strreqi(dword _left, _right)
  182. {
  183.         return strcmp(_left+strrchr(_left,'.'), _right);
  184. }
  185.  
  186. dword files_mas[400];
  187. void Open_Dir()
  188. {
  189.         int j;
  190.         char fname[4096];
  191.         select_list.ClearList();
  192.         if(io.dir.buffer)free(io.dir.buffer);
  193.         io.dir.load(#folder_path,DIR_ONLYREAL);
  194.  
  195.         for (j=0; j<io.dir.count; j++)
  196.         {
  197.                 strcpy(#fname, io.dir.position(j));
  198.                 strlwr(#fname);
  199.                 if (tabs.active_tab==TAB_SKINS) {
  200.                         if (strreqi(#fname,"skn")!=0) continue;
  201.                 }
  202.                 if (tabs.active_tab==TAB_WALLPAPERS) {
  203.                         if (strreqi(#fname,"png")!=0)
  204.                         && (strreqi(#fname,"jpg")!=0)
  205.                         && (strreqi(#fname,"jpeg")!=0)
  206.                         && (strreqi(#fname,"bmp")!=0)
  207.                         && (strreqi(#fname,"gif")!=0) continue;
  208.                 }
  209.                 ESDWORD[select_list.count*4 + #files_mas] = j;
  210.                 select_list.count++;
  211.         }
  212.         Sort_by_Name(0, select_list.count-1);
  213.  
  214.         list.drop();
  215.         //save current item for tab change
  216.         //add default item
  217.         switch(tabs.active_tab) {
  218.                 CASE TAB_SKINS:
  219.                                 select_list.cur_y = active_skin;
  220.                                 select_list.count++;
  221.                                 list.add(#default_skin);
  222.                                 BREAK;
  223.                 CASE TAB_WALLPAPERS:
  224.                                 select_list.cur_y = active_wallpaper;
  225.                                 select_list.count++;
  226.                                 list.add(#default_wallp);
  227.                                 BREAK;
  228.                 CASE TAB_SCREENSAVERS:
  229.                                 select_list.cur_y = active_screensaver;
  230.         }
  231.  
  232.         for (j=0; j<select_list.count; j++) {
  233.                 miniprintf(#param,"%s/",#folder_path);
  234.                 strcat(#param, io.dir.position(files_mas[j]));
  235.                 list.add(#param);
  236.         }
  237.  
  238.         if (!select_list.count) notify(T_NO_FILES);
  239.         if (select_list.cur_y>select_list.visible) select_list.first=select_list.cur_y;
  240.         select_list.CheckDoesValuesOkey();     
  241.         if (LIST_W) draw_window();
  242. }
  243.  
  244. void Sort_by_Name(int a, b) // for the first call: a = 0, b = sizeof(mas) - 1
  245. {                                        
  246.         int j;
  247.         int isn = a;
  248.         if (a >= b) return;
  249.         for (j = a; j <= b; j++) {
  250.                 if (strcmpi(io.dir.position(files_mas[j]), io.dir.position(files_mas[b]))<=0) {
  251.                         files_mas[isn] >< files_mas[j];
  252.                         isn++;
  253.                 }
  254.         }
  255.         Sort_by_Name(a, isn-2);
  256.         Sort_by_Name(isn, b);
  257. }
  258.  
  259. void SelectList_DrawLine(dword i)
  260. {
  261.         int draw_y = i*SELECT_LIST_ITEMH+PANEL_H;
  262.         int i_abs = select_list.first + i;
  263.         dword text_color = 0xFFFfff;
  264.         dword bg_color = 0x000000;
  265.         char filename_buf[4096];
  266.         char* filename = #filename_buf;
  267.  
  268.         strcpy(filename, list.get(i_abs));
  269.         if (EAX = strrchr(filename,'/')) filename += EAX;
  270.         if (ESBYTE[filename]=='T') && (ESBYTE[filename+1]=='_') filename+=2;
  271.         EAX = math.min(strrchr(filename,'.')-1, LIST_W - 24 / 8);
  272.         if(EAX) ESBYTE[filename+EAX] = '\0';
  273.  
  274.         //save current item for tab change
  275.         switch(tabs.active_tab) {
  276.                 CASE TAB_SKINS:
  277.                                 active_skin = select_list.cur_y;
  278.                                 if (!i_abs) filename = T_DEFAULT;
  279.                                 BREAK;
  280.                 CASE TAB_WALLPAPERS:
  281.                                 active_wallpaper = select_list.cur_y;
  282.                                 if (!i_abs) filename = T_DEFAULT;
  283.                                 BREAK;
  284.                 CASE TAB_SCREENSAVERS:
  285.                                 active_screensaver = select_list.cur_y;
  286.                                 if (!i_abs) filename = T_NO_SS;
  287.         }
  288.        
  289.         if (select_list.cur_y == i_abs) {
  290.                 text_color = sc.button;
  291.                 bg_color = sc.button_text;
  292.         }
  293.         DrawBar(select_list.x, draw_y, LIST_W, SELECT_LIST_ITEMH, text_color);
  294.         WriteText(select_list.x+12,draw_y+select_list.text_y,select_list.font_type,bg_color, filename);
  295. }
  296.  
  297. void SelectList_LineChanged()
  298. {
  299.         EventApply();
  300. }
  301.  
  302. dword GetRealKolibriosPath()
  303. {
  304.         char real_kolibrios_path[4096];
  305.         if (!dir_exists("/kolibrios")) return 0;
  306.         SetCurDir("/kolibrios");
  307.         GetCurDir(#real_kolibrios_path, sizeof(real_kolibrios_path));
  308.         return #real_kolibrios_path;
  309. }
  310.  
  311. void GetIniSettings()
  312. {
  313.         ini.section = "screensaver";
  314.         ss_timeout.position = ini.GetInt("timeout", 10) - 1;
  315.         ini.GetString("available", #ss_available, sizeof(ss_available), 0);
  316.         ini.section = "style";
  317.         ini.GetString("default_skin", #default_skin, PATHLEN, 0);
  318.         ini.GetString("default_wallp", #default_wallp, PATHLEN, 0);
  319. }
  320.  
  321. //===================================================//
  322. //                                                   //
  323. //                     EVENTS                        //
  324. //                                                   //
  325. //===================================================//
  326.  
  327. void EventTabSkinsClick()
  328. {
  329.         miniprintf(#folder_path, "%s/res/skins", GetRealKolibriosPath());
  330.         Open_Dir();
  331. }
  332.  
  333. void EventTabWallpappersClick()
  334. {
  335.         if (opendir_path) {
  336.                 strcpy(#folder_path, #opendir_path);
  337.         } else {
  338.                 miniprintf(#folder_path, "%s/res/wallpapers", GetRealKolibriosPath());
  339.         }
  340.         Open_Dir();
  341. }
  342.  
  343. void EventTabScreensaverClick()
  344. {
  345.         dword j;
  346.         char ssmas[sizeof(ss_available)];
  347.         list.drop();
  348.         select_list.ClearList();
  349.  
  350.         select_list.count++;
  351.         list.add("");
  352.  
  353.         strcpy(#ssmas, #ss_available);
  354.         do {
  355.                 j = strrchr(#ssmas, '|');
  356.                 miniprintf(#param, "/sys/%s", #ssmas + j);
  357.                 list.add(#param);
  358.                 ESBYTE[#ssmas + j - 1] = '\0';
  359.                 select_list.count++;
  360.         } while (j);
  361.  
  362.         if (LIST_W) draw_window();
  363. }
  364.  
  365. void EventDeleteFile()
  366. {
  367.         if (select_list.cur_y) DeleteFile(#cur_file_path); //no not delete default
  368.         Open_Dir();
  369.         EventApply();
  370. }
  371.  
  372. void EventSelectWallpFolder()
  373. {
  374.         o_dialog.type = 2; //select folder
  375.         OpenDialog_start stdcall (#o_dialog);
  376.         if (o_dialog.status) EventTabWallpappersClick();
  377. }
  378.  
  379. void EventSetWallpMode(dword _stretch, _titled, _auto)
  380. {
  381.         optionbox_stretch.checked = _stretch;
  382.         optionbox_tiled.checked = _titled;
  383.         optionbox_auto.checked = _auto;
  384.         optionbox_tiled.redraw();
  385.         optionbox_stretch.redraw();
  386.         optionbox_auto.redraw();
  387.         EventApply();
  388. }
  389.  
  390. void EventApply()
  391. {
  392.         char kivparam[4096+10];
  393.         dword file_name = list.get(select_list.cur_y);
  394.         strcpy(#cur_file_path, list.get(select_list.cur_y));
  395.         if (tabs.active_tab==TAB_SKINS)
  396.         {
  397.                 SetSystemSkin(#cur_file_path);
  398.                 SelectList_Draw();
  399.                 strcpy(#cur_skin_path, #cur_file_path);
  400.                 EventUpdateDocky();
  401.         }
  402.         if (tabs.active_tab==TAB_WALLPAPERS)
  403.         {
  404.                 SelectList_Draw();
  405.                 miniprintf(#kivparam, "\\S__%s", #cur_file_path);
  406.                 if (optionbox_tiled.checked) || (!select_list.cur_y) kivparam[1]='T';
  407.                 if (optionbox_auto.checked) {
  408.                         file_name += strrchr(file_name, '/');
  409.                         if (ESBYTE[file_name] == 'T') && (ESBYTE[file_name+1] == '_') {
  410.                                 kivparam[1]='T';
  411.                         }
  412.                 }
  413.                 RunProgram("/sys/media/kiv", #kivparam);
  414.         }
  415.         if (tabs.active_tab==TAB_SCREENSAVERS)
  416.         {
  417.                 SelectList_Draw();
  418.         }
  419. }
  420.  
  421. void EventUpdateDocky()
  422. {
  423.         if (update_docky.checked) {
  424.                 RestartProcessByName("/sys/@docky", MULTIPLE);
  425.                 pause(50);
  426.                 ActivateWindow_Self();         
  427.         }
  428. }
  429.  
  430. void EventOpenFile()
  431. {
  432.         switch (tabs.active_tab) {
  433.                 case TAB_SKINS: RunProgram("/sys/skincfg", #cur_file_path); break;
  434.                 case TAB_WALLPAPERS: RunProgram("/sys/media/kiv", #cur_file_path); break;
  435.                 case TAB_SCREENSAVERS: if(select_list.cur_y) RunProgram(list.get(select_list.cur_y), "@ss");
  436.         }
  437. }
  438.  
  439. void EventExit()
  440. {
  441.         if (GetRealKolibriosPath()) {
  442.                 ini.section = "style";
  443.                 ini.SetString("skin", #cur_skin_path, strlen(#cur_skin_path));
  444.         }
  445.         ExitProcess();
  446. }
  447.  
  448. void EventSetSs()
  449. {
  450.         dword cur_ss = list.get(select_list.cur_y);
  451.         ini.section = "screensaver";
  452.         ini.SetString("program", cur_ss, strlen(cur_ss));
  453.         ini.SetInt("timeout", ss_timeout.position+1);
  454.         RestartProcessByName("/sys/@ss", MULTIPLE);
  455. }
  456.  
  457. stop:
  458.  
  459. char folder_path[PATHLEN];
  460. char cur_file_path[PATHLEN];
  461. char cur_skin_path[PATHLEN];
  462. char default_skin[PATHLEN];
  463. char default_wallp[PATHLEN];