Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Icon Editor for KolibriOS
  3.  * Authors: Leency, Nicolas
  4.  * Licence: GPL v2
  5. */
  6.  
  7. #define MEMSIZE 4096*500
  8.  
  9. #include "../lib/gui.h"
  10. #include "../lib/random.h"
  11. #include "../lib/mem.h"
  12. #include "../lib/cursor.h"
  13.  
  14. #include "../lib/obj/libimg.h"
  15. #include "../lib/obj/box_lib.h"
  16.  
  17. #include "../lib/patterns/rgb.h"
  18. #include "../lib/patterns/libimg_load_skin.h"
  19.  
  20. #include "colors_mas.h"
  21.  
  22. #include "lang.h--"
  23.  
  24. //===================================================//
  25. //                                                   //
  26. //                       DATA                        //
  27. //                                                   //
  28. //===================================================//
  29.  
  30. #ifdef LANG_RUS
  31. #define T_NOTIFY_OPEN "'IconEdit
  32. ‚ ¤ ­­ë© ¬®¬¥­â IconEdit ¬®¦¥â ®âªà뢠âì ⮫쪮 ¨ª®­ª¨, ᮧ¤ ­­ë¥ ¢ á ¬®¬ । ªâ®à¥.
  33. …᫨ ­ã¦­® ®âªàëâì ¤à㣮¥ ¨§®¡à ¦¥­¨¥, ¢®á¯®«ì§ã©â¥áì ¨­áâà㬥­â®¬ <”®â® ¯¯ à â>
  34. ¤«ï § å¢ â  ª à⨭ª¨ á íªà ­ .' -Wt"
  35. #else
  36. #define T_NOTIFY_OPEN "'IconEdit
  37. You can open only files created in IconEdit for now!
  38. In other case please use <Photo> tool to get an image from screen.' -Wt"
  39. #endif
  40.  
  41.  
  42. #define T_TITLE "Icon Editor 0.56 Alpha"
  43.  
  44. #define TOPBAR_H    24+8
  45. #define LEFTBAR_W 16+5+5+3+3
  46. #define PALLETE_SIZE 116
  47.  
  48. #define PAL_ITEMS_X_COUNT 13
  49. #define COLSIZE 18
  50. #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE
  51.  
  52. #define TO_CANVAS_X(xval) xval - canvas.x/zoom.value
  53. #define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value
  54.  
  55. block canvas = { NULL, NULL, NULL, NULL };
  56. block wrapper = { LEFTBAR_W, TOPBAR_H, NULL, NULL };
  57. block right_bar = { NULL, 10+TOPBAR_H, RIGHT_BAR_W+10, NULL };
  58.  
  59. block b_color_gradient = {NULL, 40+TOPBAR_H, RIGHT_BAR_W, 30};
  60. block b_last_colors = {NULL, 80+TOPBAR_H, RIGHT_BAR_W, COLSIZE*2};
  61. block b_default_palette = {NULL, COLSIZE*2+10+80+TOPBAR_H, RIGHT_BAR_W, COLSIZE*9};
  62.  
  63. dword color1 = 0x000000;
  64. dword color2 = 0xBFCAD2;
  65. dword tool_color;
  66.  
  67. signed hoverX;
  68. signed hoverY;
  69. signed priorHoverX;
  70. signed priorHoverY;
  71. bool canvasMouseMoved = false;
  72.  
  73. enum {
  74.         BTN_NEW = 40,
  75.         BTN_OPEN,
  76.         BTN_SAVE,
  77.         BTN_MOVE_LEFT,
  78.         BTN_MOVE_RIGHT,
  79.         BTN_MOVE_UP,
  80.         BTN_MOVE_DOWN,
  81.         BTN_FLIP_HOR,
  82.         BTN_FLIP_VER,
  83.         BTN_ROTATE_LEFT,
  84.         BTN_ROTATE_RIGHT,
  85.         BTN_TEST_ICON,
  86.         BTN_PENCIL,
  87.         BTN_PICK,
  88.         BTN_FILL,
  89.         BTN_LINE,
  90.         BTN_RECT,
  91.         BTN_BAR,
  92.         BTN_SELECT,
  93.         BTN_SCREEN_COPY,
  94.         BTN_ZOOM_IN,
  95.         BTN_ZOOM_OUT,
  96.         BTN_CANVAS_RESIZE,
  97.         BTN_CROP,
  98.         BTNS_PALETTE_COLOR_MAS = 100,
  99.         BTNS_LAST_USED_COLORS = 400
  100. };
  101.  
  102. proc_info Form;
  103. dword semi_white;
  104.  
  105. more_less_box zoom = { 11, 1, 40, "Zoom" };
  106.  
  107. dword default_palette[] = {
  108. 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
  109. 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
  110. 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
  111. 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
  112. 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
  113. 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
  114. 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
  115. 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
  116. 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
  117. 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
  118. 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
  119. 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF 
  120. };
  121. dword last_used_colors[13*2] = {
  122. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  123. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  124. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF
  125. };
  126.  
  127. CustomCursor Cursor;
  128. dword CursorBar = FROM "cursors/bar.cur";
  129. dword CursorFill = FROM "cursors/fill.cur";
  130. dword CursorLine = FROM "cursors/line.cur";
  131. dword CursorPencil = FROM "cursors/pencil.cur";
  132. dword CursorPipette = FROM "cursors/pipette.cur";
  133. dword CursorRectangle = FROM "cursors/rectangle.cur";
  134. dword CursorSelect = FROM "cursors/select.cur";
  135.  
  136. _image image;
  137.  
  138. #include "actions_history.h"
  139.  
  140. _ActionsHistory actionsHistory;
  141.  
  142. #include "tools.h"
  143. #include "canvas_resize.h"
  144.  
  145. //===================================================//
  146. //                                                   //
  147. //                       CODE                        //
  148. //                                                   //
  149. //===================================================//
  150.  
  151. libimg_image top_icons;
  152. libimg_image left_icons;
  153.  
  154. void main()
  155. {
  156.         word btn;
  157.         libimg_image open_image;
  158.  
  159.         load_dll(libio,  #libio_init,  1);
  160.         load_dll(libimg, #libimg_init, 1);
  161.         load_dll(boxlib, #box_lib_init,0);
  162.  
  163.         Libimg_LoadImage(#top_icons, "/sys/icons16.png");
  164.         Libimg_LoadImage(#left_icons, "/sys/icons16.png");
  165.  
  166.         system.color.get();
  167.         semi_white = MixColors(system.color.work, 0xFFFfff, 96);
  168.         Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffFFFfff, semi_white);
  169.         Libimg_ReplaceColor(top_icons.image, top_icons.w, top_icons.h, 0xffCACBD6, MixColors(semi_white, 0, 220));
  170.  
  171.         Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffFFFfff, system.color.work);
  172.         Libimg_ReplaceColor(left_icons.image, left_icons.w, left_icons.h, 0xffCACBD6, MixColors(system.color.work, 0, 200));
  173.  
  174.         if (!param[0]) {
  175.                 image.create(32, 32);
  176.         }
  177.         else
  178.         {
  179.                 Libimg_LoadImage(#open_image, #param);
  180.  
  181.                 if (open_image.w*open_image.h>MAX_CELL_SIZE*MAX_CELL_SIZE) {
  182.                         notify("'Hey, this is just an icon editor,\nselected image is too big to open!' -E");
  183.                         ExitProcess();
  184.                 }
  185.                 else {
  186.                         image.create(open_image.w, open_image.h);
  187.                         image.set_image(open_image.imgsrc);
  188.                 }
  189.         }
  190.  
  191.         actionsHistory.init();
  192.  
  193.         initTools();
  194.         setCurrentTool(TOOL_PENCIL);
  195.        
  196.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  197.  
  198.         loop() switch(WaitEvent())
  199.         {
  200.                 case evMouse:
  201.                         if (Window_CanvasReSize.thread_exists()) break;
  202.                         mouse.get();
  203.                        
  204.                         if (mouse.lkm) tool_color = color1;
  205.                         if (mouse.pkm) tool_color = color2;
  206.                         if (mouse.mkm) break;
  207.  
  208.                         hoverX = mouse.x - canvas.x / zoom.value;
  209.                         hoverY = mouse.y - canvas.y / zoom.value;
  210.                         if (hoverX<0) hoverX = 0;
  211.                         if (hoverY<0) hoverY = 0;
  212.                         if (hoverX>image.columns-1) hoverX = image.columns-1;
  213.                         if (hoverY>image.rows-1) hoverY = image.rows-1;
  214.                         canvasMouseMoved = false;
  215.                         if (priorHoverX != hoverX) canvasMouseMoved = true;
  216.                         if (priorHoverY != hoverY) canvasMouseMoved = true;
  217.                         priorHoverX = hoverX;
  218.                         priorHoverY = hoverY;
  219.                         //DrawBar(Form.cwidth-100, 3, 80, 12, 0xFFFfff);
  220.                         //WriteText(Form.cwidth-100, 3, 0x80, 0x000000,
  221.                         //      sprintf(#param, "%i %i", hoverX, hoverY));
  222.  
  223.                         if (currentTool != TOOL_NONE)
  224.                                 tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
  225.  
  226.                         if (mouse.vert) {
  227.                                 if (mouse.vert==65535) zoom.inc();
  228.                                 if (mouse.vert==1) zoom.dec();
  229.                                 DrawEditArea();
  230.                         }
  231.  
  232.                         if (wrapper.hovered()) SetCursor();
  233.                         else Cursor.Restore();
  234.  
  235.                         if (mouse.down) {
  236.                                 if (b_color_gradient.hovered())
  237.                                 || (b_last_colors.hovered())
  238.                                 || (b_default_palette.hovered()) {
  239.                                         if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
  240.                                         if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
  241.                                 }      
  242.                         }
  243.  
  244.                         break;
  245.  
  246.                 case evButton:
  247.                         if (Window_CanvasReSize.thread_exists()) break;
  248.                         btn = GetButtonID();
  249.  
  250.                         if (zoom.click(btn)) DrawEditArea();
  251.  
  252.                         switch(btn)
  253.                         {
  254.                                 case BTN_NEW:
  255.                                         EventCreateNewIcon();
  256.                                         break;
  257.                                 case BTN_OPEN:
  258.                                         notify(T_NOTIFY_OPEN);
  259.                                         RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
  260.                                         break;
  261.                                 case BTN_SAVE:
  262.                                         EventSaveIconToFile();
  263.                                         break;
  264.                                 case BTN_MOVE_LEFT:
  265.                                         EventMove(MOVE_LEFT);
  266.                                         break;
  267.                                 case BTN_MOVE_RIGHT:
  268.                                         EventMove(MOVE_RIGHT);
  269.                                         break;
  270.                                 case BTN_MOVE_UP:
  271.                                         EventMove(MOVE_UP);
  272.                                         break;
  273.                                 case BTN_MOVE_DOWN:
  274.                                         EventMove(MOVE_DOWN);
  275.                                         break;
  276.                                 case BTN_FLIP_VER:
  277.                                         EventMove(FLIP_VER);
  278.                                         break;
  279.                                 case BTN_FLIP_HOR:
  280.                                         EventMove(FLIP_HOR);
  281.                                         break;
  282.                                 case BTN_TEST_ICON:
  283.                                         EventTestIcon();
  284.                                         break;
  285.                                 case BTN_PENCIL:
  286.                                         setCurrentTool(TOOL_PENCIL);
  287.                                         break;
  288.                                 case BTN_PICK:
  289.                                         setCurrentTool(TOOL_PIPETTE);
  290.                                         break;
  291.                                 case BTN_FILL:
  292.                                         setCurrentTool(TOOL_FILL);
  293.                                         break;
  294.                                 case BTN_LINE:
  295.                                         setCurrentTool(TOOL_LINE);
  296.                                         break;
  297.                                 case BTN_RECT:
  298.                                         setCurrentTool(TOOL_RECT);
  299.                                         break;
  300.                                 case BTN_BAR:
  301.                                         setCurrentTool(TOOL_BAR);
  302.                                         break;
  303.                                 case BTN_SELECT:
  304.                                         setCurrentTool(TOOL_SELECT);
  305.                                         break;
  306.                                 case BTN_SCREEN_COPY:
  307.                                         setCurrentTool(TOOL_SCREEN_COPY);
  308.                                         break;
  309.                                 case BTN_CANVAS_RESIZE:
  310.                                         notify("Sorry, not implemented yet.");
  311.                                         break;
  312.                                 case BTN_CROP:
  313.                                         EventCrop();
  314.                                         break;
  315.                                 case CLOSE_BTN:
  316.                                         EventExitIconEdit();
  317.                                         break;
  318.                         }
  319.                         break;
  320.          
  321.                 case evKey:
  322.                         GetKeys();
  323.  
  324.                         if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0)
  325.                                 tools[currentTool].onKeyEvent(key_scancode);
  326.  
  327.                         if (key_scancode == SCAN_CODE_DEL) EventCleanCanvas();
  328.  
  329.                         if (key_scancode == SCAN_CODE_KEY_P) setCurrentTool(TOOL_PENCIL);
  330.                         if (key_scancode == SCAN_CODE_KEY_I) setCurrentTool(TOOL_PIPETTE);
  331.                         if (key_scancode == SCAN_CODE_KEY_F) setCurrentTool(TOOL_FILL);
  332.                         if (key_scancode == SCAN_CODE_KEY_L) setCurrentTool(TOOL_LINE);
  333.                         if (key_scancode == SCAN_CODE_KEY_R) setCurrentTool(TOOL_RECT);
  334.                         if (key_scancode == SCAN_CODE_KEY_B) setCurrentTool(TOOL_BAR);
  335.                         if (key_scancode == SCAN_CODE_KEY_S) setCurrentTool(TOOL_SELECT);
  336.  
  337.                         if (key_scancode == SCAN_CODE_KEY_T) EventTestIcon();
  338.  
  339.                         if (key_scancode == SCAN_CODE_KEY_Z) actionsHistory.undoLastAction();
  340.                         if (key_scancode == SCAN_CODE_KEY_Y) actionsHistory.redoLastAction();
  341.  
  342.                         if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();}
  343.                         if (key_scancode == SCAN_CODE_PLUS)  {zoom.inc(); DrawEditArea();}
  344.  
  345.                         break;
  346.                  
  347.                 case evReDraw:
  348.                         Window_CanvasReSize.thread_exists();
  349.                         DrawWindow();
  350.                         break;
  351.         }
  352. }
  353.  
  354. void DrawTopPanelButton(dword _id, _x, _icon_n)
  355. {
  356.         DrawWideRectangle(_x, 4, 22, 22, 3, semi_white);
  357.         PutPixel(_x,4,system.color.work);
  358.         PutPixel(_x,4+21,system.color.work);
  359.         PutPixel(_x+21,4,system.color.work);
  360.         PutPixel(_x+21,4+21,system.color.work);
  361.         DefineHiddenButton(_x, 4, 21, 21, _id);
  362.         img_draw stdcall(top_icons.image, _x+3, 7, 16, 16, 0, _icon_n*16);
  363. }
  364.  
  365. void DrawLeftPanelButton(dword _id, _y, _icon_n)
  366. {
  367.         int x = 5;
  368.         DrawRectangle(x, _y, 22-1, 22-1, system.color.work);
  369.         DefineHiddenButton(x, _y, 21, 21, _id);
  370.         img_draw stdcall(left_icons.image, x+3, _y+3, 16, 16, 0, _icon_n*16);
  371. }
  372.  
  373. void DrawStatusBar()
  374. {
  375.         zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
  376.  
  377.         sprintf(#param,"%i x %i", image.columns, image.rows);
  378.         DrawCaptButton(
  379.                 wrapper.x+wrapper.w-calc(strlen(#param)*8) -6 - 1,
  380.                 zoom.y,
  381.                 calc(strlen(#param)*8)+6,
  382.                 18,
  383.                 BTN_CANVAS_RESIZE,
  384.                 system.color.work_button,
  385.                 system.color.work_button_text,
  386.                 #param
  387.                 );
  388. }
  389.  
  390. void DrawWindow()
  391. {
  392.         #define GAP 27
  393.         #define BLOCK_SPACE 10
  394.         incn tx;
  395.         system.color.get();
  396.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x73, NULL, T_TITLE, 0);
  397.         GetProcessInfo(#Form, SelfInfo);
  398.         if (Form.status_window>2) return;
  399.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  400.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  401.  
  402.         right_bar.x = Form.cwidth - right_bar.w;
  403.         b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
  404.         DrawBar(0, 0, Form.cwidth, TOPBAR_H-1, system.color.work);
  405.         DrawBar(0, TOPBAR_H-1, Form.cwidth, 1, system.color.work_graph);
  406.  
  407.         tx.n = 5-GAP;
  408.         DrawTopPanelButton(BTN_NEW,    tx.inc(GAP), 2); //not implemented
  409.         DrawTopPanelButton(BTN_OPEN,   tx.inc(GAP), 0); //not implemented
  410.         DrawTopPanelButton(BTN_SAVE,   tx.inc(GAP), 5);
  411.         DrawTopPanelButton(BTN_MOVE_LEFT,  tx.inc(GAP+BLOCK_SPACE), 30);
  412.         DrawTopPanelButton(BTN_MOVE_RIGHT, tx.inc(GAP),   31);
  413.         DrawTopPanelButton(BTN_MOVE_UP,    tx.inc(GAP),   32);
  414.         DrawTopPanelButton(BTN_MOVE_DOWN,  tx.inc(GAP),   33);
  415.        
  416.         DrawTopPanelButton(BTN_FLIP_HOR,   tx.inc(GAP+BLOCK_SPACE), 34);
  417.         DrawTopPanelButton(BTN_FLIP_VER,   tx.inc(GAP),   35);
  418.  
  419.         DrawTopPanelButton(BTN_TEST_ICON,  tx.inc(GAP+BLOCK_SPACE), 12);
  420.  
  421.         DrawTopPanelButton(BTN_CROP,  tx.inc(GAP+BLOCK_SPACE), 46);
  422.         // DrawTopPanelButton(BTN_ROTATE_LEFT,   tx.inc(GAP), 36); //not implemented
  423.         // DrawTopPanelButton(BTN_ROTATE_RIGHT,  tx.inc(GAP), 37); //not implemented
  424.        
  425.         DrawEditArea();
  426.  
  427.         DrawBar(0, TOPBAR_H, LEFTBAR_W-1, Form.cheight - TOPBAR_H, system.color.work);
  428.         DrawLeftPanel();
  429.  
  430.         DrawBar(wrapper.x+wrapper.w, TOPBAR_H, Form.cwidth-wrapper.x-wrapper.w,
  431.                 Form.cheight - TOPBAR_H, system.color.work);
  432.         DrawActiveColor(right_bar.y);
  433.         DrawColorPallets();
  434.         DrawPreview();
  435.  
  436.         DrawBar(LEFTBAR_W-1, wrapper.y + wrapper.h, wrapper.w+1,
  437.                 Form.cheight - wrapper.y - wrapper.h, system.color.work);
  438.         DrawStatusBar();
  439. }
  440.  
  441. void DrawLeftPanel()
  442. {
  443.         #define GAP 28
  444.         incn ty;
  445.         ty.n = right_bar.y - GAP - 2;
  446.         DrawLeftPanelButton(BTN_PENCIL, ty.inc(GAP), 38);
  447.         DrawLeftPanelButton(BTN_PICK,   ty.inc(GAP), 39);
  448.         DrawLeftPanelButton(BTN_FILL,   ty.inc(GAP), 40);
  449.         DrawLeftPanelButton(BTN_LINE,   ty.inc(GAP), 41);
  450.         DrawLeftPanelButton(BTN_RECT,   ty.inc(GAP), 42);
  451.         DrawLeftPanelButton(BTN_BAR,    ty.inc(GAP), 43);
  452.         DrawLeftPanelButton(BTN_SELECT, ty.inc(GAP), 44);
  453.         DrawLeftPanelButton(BTN_SCREEN_COPY, ty.inc(GAP), 45);
  454.         DrawRectangle3D(5, currentTool*GAP+right_bar.y-2, 16+3+2, 16+3+2, 0x333333, 0x777777);
  455. }
  456.  
  457. void DrawEditArea()
  458. {
  459.         dword color1=0xC0C0C0;
  460.         int top_side;
  461.         int left_side;
  462.  
  463.         wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
  464.         wrapper.h = Form.cheight - TOPBAR_H - 35;
  465.  
  466.         //canvas{
  467.         canvas.w = image.columns * zoom.value;
  468.         canvas.h = image.rows * zoom.value;
  469.         if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
  470.                 zoom.value--;
  471.                 DrawEditArea();
  472.                 return;
  473.         }
  474.         canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
  475.         canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
  476.         DrawCanvas();
  477.         //}
  478.  
  479.         left_side = canvas.x-wrapper.x-1;
  480.         top_side = canvas.y-wrapper.y-1;
  481.  
  482.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, system.color.work_graph);
  483.  
  484.         if (left_side>0)
  485.         {
  486.                 DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
  487.                 DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
  488.         }
  489.         if (top_side>0)
  490.         {
  491.                 //left
  492.                 DrawBar(wrapper.x, wrapper.y+top_side, left_side,
  493.                         wrapper.h-top_side-top_side, color1);
  494.                 //right
  495.                 DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
  496.                         wrapper.h-top_side-top_side, color1);
  497.         }
  498.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  499. }
  500.  
  501. void DrawActiveColor(dword iny)
  502. {
  503.         #define CELL 20
  504.         static dword outy;
  505.         if (iny != NULL) outy = iny;
  506.         DrawFrame(right_bar.x, outy, CELL, CELL, NULL);
  507.         DrawBar(right_bar.x+2, outy+2, CELL-4, CELL-4, color1);
  508.         sprintf(#param, "%A", color1);
  509.         EDI = system.color.work;
  510.         WriteText(right_bar.x + 30, outy + 3, 0xD0, system.color.work_text, #param+4);
  511.  
  512.         DrawFrame(right_bar.x+110, outy, CELL, CELL, NULL);
  513.         DrawBar(right_bar.x+110+2, outy+2, CELL-4, CELL-4, color2);
  514.         sprintf(#param, "%A", color2);
  515.         EDI = system.color.work;
  516.         WriteText(right_bar.x+110 + 30, outy + 3, 0xD0, system.color.work_text, #param+4);     
  517.         DrawCurrentColorGradientByLightness();
  518. }
  519.  
  520. void DrawCurrentColorGradientByLightness()
  521. {
  522.         int i;
  523.         int w = right_bar.w-10/2;
  524.         for (i=0; i<w; i++)
  525.                 DrawBar(b_color_gradient.x+i, b_color_gradient.y,
  526.                         1, b_color_gradient.h, MixColors(color1,0xFFFfff,255*i/w));
  527.  
  528.         //current color marker 
  529.         DrawBar(b_color_gradient.x+i-1, b_color_gradient.y-2, 3,2, 0x000000);
  530.  
  531.         for (i=0 ; i<=w; i++)
  532.                 DrawBar(b_color_gradient.x+w+w-i, b_color_gradient.y,
  533.                         1, b_color_gradient.h, MixColors(color1,0x000000,255*i/w));
  534. }
  535.  
  536. void DrawColorPallets()
  537. {
  538.         int r, c, i=0;
  539.         //Last used colors
  540.         for (r = 0; r < 2; r++)
  541.         {
  542.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  543.                 {
  544.                         DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
  545.                                 COLSIZE, COLSIZE, last_used_colors[i]);
  546.                 }
  547.         }
  548.         i=0;
  549.         //Default colors
  550.         for (r = 0; r < 9; r++)
  551.         {
  552.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  553.                 {
  554.                         DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
  555.                                 COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
  556.                 }
  557.         }
  558. }
  559.  
  560. void DrawCanvasPixel(dword _r,_c,_color)
  561. {
  562.         DrawBar(_c*zoom.value + canvas.x, _r*zoom.value + canvas.y,
  563.         zoom.value, zoom.value, _color);
  564. }
  565.  
  566. void DrawCanvas()
  567. {
  568.         int r, c;
  569.         dword color;
  570.  
  571.         if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
  572.         {
  573.                 tools[currentTool].onCanvasDraw();
  574.         }
  575.  
  576.         for (r = 0; r < image.rows; r++)
  577.         {
  578.                 for (c = 0; c < image.columns; c++)
  579.                 {
  580.                         if (image.pixel_state.is_drawable(r,c))
  581.                                 DrawCanvasPixel(r, c, image.get_pixel(r,c));
  582.                 }
  583.         }
  584.         image.pixel_state.reset_and_set_all_drawable();
  585.  
  586.         DrawPreview();
  587. }
  588.  
  589. void DrawPreview()
  590. {
  591.         int x = right_bar.x;
  592.         int y = wrapper.y + wrapper.h - image.rows-2;
  593.         DrawRectangle(x, y, image.columns+1, image.rows+1, system.color.work_graph);
  594.         _PutImage(x+1,y+1, image.columns, image.rows, image.get_image());
  595. }
  596.  
  597. dword GetPixelUnderMouse()
  598. {
  599.         return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  600. }
  601.  
  602. int preview_size = 128;
  603. void DrawImageWithBg(dword _x, _y, _col_to)
  604. {
  605.         _x *= preview_size;
  606.         _y *= preview_size;
  607.         DrawWideRectangle(_x,_y, preview_size, preview_size, preview_size-image.columns/2, _col_to);
  608.         _PutImage(preview_size - image.columns / 2 + _x, preview_size - image.rows / 2 + _y,
  609.                 image.columns, image.rows, image.get_image_with_replaced_color(color2, _col_to));
  610. }
  611.  
  612. void ShowWindow_TestIcon()
  613. {
  614.         loop() switch(WaitEvent())
  615.         {
  616.                 case evButton:
  617.                         if (GetButtonID()) ExitProcess();
  618.                         break;
  619.          
  620.                 case evKey:
  621.                         GetKeys();
  622.                         if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  623.                         break;
  624.                  
  625.                 case evReDraw:
  626.                         DefineAndDrawWindow(Form.left+100, Form.top+100, preview_size*2+9,
  627.                                 preview_size*2+skin_height+4, 0x74, NULL, "Test Icon", 0);
  628.                         DrawImageWithBg(0, 0, 0x000000);
  629.                         DrawImageWithBg(1, 0, 0xFFFfff);
  630.                         DrawImageWithBg(0, 1, GetPixelColorFromScreen(0, 0));
  631.                         DrawImageWithBg(1, 1, system.color.work);
  632.                         break;
  633.         }
  634. }
  635.  
  636. //===================================================//
  637. //                                                   //
  638. //                      EVENTS                       //
  639. //                                                   //
  640. //===================================================//
  641.  
  642. void EventCreateNewIcon()
  643. {
  644.         EventSaveIconToFile();
  645.         Window_CanvasReSize.create();
  646. }
  647.  
  648. void EventSaveIconToFile()
  649. {
  650.         int i=0;
  651.         char save_file_name[4096];
  652.         char save_path_stable[4096];
  653.         strcpy(#save_path_stable, "/tmp0/1");
  654.         do {
  655.                 i++;
  656.                 sprintf(#save_file_name, "%s/saved_icon_%i.png", #save_path_stable, i);
  657.         } while (file_exists(#save_file_name));
  658.         save_image(image.get_image(), image.columns, image.rows, #save_file_name);
  659. }
  660.  
  661. void EventCleanCanvas()
  662. {
  663.         image.create(image.rows, image.columns);
  664.         actionsHistory.saveCurrentState();
  665.         DrawCanvas();
  666. }
  667.  
  668. void EventExitIconEdit()
  669. {
  670.         EventSaveIconToFile();
  671.         ExitProcess();
  672. }
  673.  
  674. void EventSetActiveColor(int _number, _color)
  675. {
  676.         int i;
  677.         for (i=13*2-1; i>0; i--) {
  678.                 last_used_colors[i] = last_used_colors[i-1];
  679.         }
  680.         last_used_colors[0] = _color;
  681.  
  682.         if (_number == 1) color1 = _color;
  683.         if (_number == 2) color2 = _color;
  684.  
  685.         DrawActiveColor(NULL);
  686.         DrawColorPallets();
  687. }
  688.  
  689. void EventTestIcon()
  690. {
  691.         CreateThread(#ShowWindow_TestIcon, #test_icon_stak+4092);
  692. }
  693.  
  694. void EventMove(dword _action)
  695. {
  696.         if (selection.state) {
  697.                 selection.buf.move(_action);
  698.                 SelectTool_onCanvasDraw();
  699.         }
  700.         else {
  701.                 image.move(_action);
  702.                 DrawCanvas();
  703.         }
  704.         actionsHistory.saveCurrentState();
  705. }
  706.  
  707. void EventCrop()
  708. {
  709.         if (selection.state) {
  710.                 EventSaveIconToFile();
  711.                 image.create(selection.buf.rows, selection.buf.columns);
  712.                 selection.move_to_point(0,0);
  713.                 selection.apply_to_image();
  714.                 selection.reset();
  715.                 actionsHistory.init();
  716.                 DrawWindow();
  717.         }
  718.         else {
  719.                 notify("'You need to select something before usnig crop tool.' -W");
  720.         }
  721. }
  722.  
  723. stop:
  724.  
  725. char test_icon_stak[4096];
  726.