Subversion Repositories Kolibri OS

Rev

Rev 8822 | Rev 8954 | 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 1024*2000
  8.  
  9. #include "../lib/gui.h"
  10. #include "../lib/random.h"
  11. #include "../lib/mem.h"
  12. #include "../lib/cursor.h"
  13. #include "../lib/list_box.h"
  14. #include "../lib/events.h"
  15.  
  16. #include "../lib/obj/libimg.h"
  17. #include "../lib/obj/box_lib.h"
  18.  
  19. #include "../lib/patterns/rgb.h"
  20. #include "../lib/patterns/toolbar_button.h"
  21.  
  22. #include "colors_mas.h"
  23.  
  24. #ifndef AUTOBUILD
  25. #include "lang.h--"
  26. #endif
  27.  
  28. //===================================================//
  29. //                                                   //
  30. //                       DATA                        //
  31. //                                                   //
  32. //===================================================//
  33.  
  34. #ifdef LANG_RUS
  35. char edit_menu_items[] =
  36. "‚ë१ âì|Ctrl+X
  37. Š®¯¨à®¢ âì|Ctrl+C
  38. ‚áâ ¢¨âì|Ctrl+V";
  39. char image_menu_items[] =
  40. "®áç¨â âì ª®«¨ç¥á⢮ ã­¨ª «ì­ëå 梥⮢
  41. ‡ ¬¥­¨âì ¢á¥ æ¢¥â  1 ­  2
  42. à®¢¥à¨âì ¨ª®­ªã";
  43. ?define T_MENU_IMAGE "ˆª®­ª "
  44. ?define T_TEST_ICON "à®¢¥àª  ¨ª®­ª¨"
  45. ?define T_TITLE "¥¤ ªâ®à ¨ª®­®ª 0.70a Beta"
  46. ?define T_UNIC_COLORS_COUNT "'“­¨ª «ì­ëå 梥⮢: %i.' -I"
  47. ?define T_TOO_BIG_IMAGE_FOR_PREVIEW "'IconEdit
  48. ˆ§®¡à ¦¥­¨¥ ᫨誮¬ ¡®«ì讥 ¤«ï ¯à¥¤¯à®á¬®âà !' -tE"
  49. ?define T_ERROR_CROP_TOOL "'„«ï ®¡à¥§ª¨ ¨§®¡à ¦¥­¨ï ¢­ ç «¥ ­ã¦­® ¢ë¤¥«¨âì ®¡« áâì.' -W"
  50. ?define T_ERROR_IMA_ICONEDIT "'â® ¯à®á⮠। ªâ®à ¨ª®­®ª, ¢ë¡à ­®¥
  51. ¨§®¡à ¦¥­¨¥ ᫨誮¬ ¢¥«¨ª® ¤«ï ­¥£®!' -E"
  52. #else
  53. char edit_menu_items[] =
  54. "Cut|Ctrl+X
  55. Copy|Ctrl+C
  56. Paste|Ctrl+V";
  57. char image_menu_items[] =
  58. "Count unic colors used
  59. Replace all colors equal to 1 by 2
  60. Test icon";
  61. ?define T_MENU_IMAGE "Icon"
  62. ?define T_TEST_ICON "Test Icon"
  63. ?define T_TITLE "Icon Editor 0.70 Beta"
  64. ?define T_UNIC_COLORS_COUNT "'Image has %i unique colors.' -I"
  65. ?define T_TOO_BIG_IMAGE_FOR_PREVIEW "'IconEdit
  66. Image is too big for preview!' -tE"
  67. ?define T_ERROR_CROP_TOOL "'You need to select something before using crop tool.' -W"
  68. ?define T_ERROR_IMA_ICONEDIT "'Hey, this is just an icon editor,
  69. selected image is too big to open!' -E"
  70. #endif
  71.  
  72.  
  73.  
  74. #define PALLETE_SIZE 116
  75.  
  76. #define TOPBAR_H    24+8
  77. int leftbar_w;
  78.  
  79. #define PAL_ITEMS_X_COUNT 13
  80. #define COLSIZE 18
  81. #define RIGHT_BAR_W PAL_ITEMS_X_COUNT*COLSIZE
  82.  
  83. #define TO_CANVAS_X(xval) xval - canvas.x/zoom.value
  84. #define TO_CANVAS_Y(yval) yval - canvas.y/zoom.value
  85.  
  86. block canvas = { NULL, NULL, NULL, NULL };
  87. block wrapper = { 0, TOPBAR_H, NULL, NULL };
  88. block right_bar = { NULL, 10+TOPBAR_H, RIGHT_BAR_W+10, NULL };
  89. block image_menu_btn = { NULL, 4, NULL, 22 };
  90.  
  91. dword linear_gradient[RIGHT_BAR_W];
  92. block b_color_gradient = {NULL, 40+TOPBAR_H, RIGHT_BAR_W, 25};
  93. //block b_opacity_gradient = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, 15};
  94. block b_last_colors = {NULL, 75+TOPBAR_H, RIGHT_BAR_W, COLSIZE};
  95. block b_default_palette = {NULL, COLSIZE+10+75+TOPBAR_H, RIGHT_BAR_W, COLSIZE*9};
  96.  
  97. dword transparent = 0xBFCAD2;
  98. dword color1 = 0x000000;
  99. dword color2 = 0xBFCAD2;
  100. dword tool_color;
  101.  
  102. signed hoverX;
  103. signed hoverY;
  104. signed priorHoverX;
  105. signed priorHoverY;
  106. bool canvasMouseMoved = false;
  107.  
  108. EVENTS button;
  109. EVENTS key;
  110.  
  111. enum {
  112.         BTNS_PALETTE_COLOR_MAS = 100,
  113.         BTNS_LAST_USED_COLORS = 400
  114. };
  115.  
  116. proc_info Form;
  117. dword semi_white;
  118. bool bg_dark=false;
  119.  
  120. more_less_box zoom = { 11, 1, 40, "Zoom" };
  121.  
  122. dword default_palette[] = {
  123. 0x330000,0x331900,0x333300,0x193300,0x003300,0x003319,0x003333,0x001933,0x000033,0x190033,
  124. 0x330033,0x330019,0x000000,0x660000,0x663300,0x666600,0x336600,0x006600,0x006633,0x006666,
  125. 0x003366,0x000066,0x330066,0x660066,0x660033,0x202020,0x990000,0x994C00,0x999900,0x4C9900,
  126. 0x009900,0x00994C,0x009999,0x004C99,0x000099,0x4C0099,0x990099,0x99004C,0x404040,0xCC0000,
  127. 0xCC6600,0xCCCC00,0x66CC00,0x00CC00,0x00CC66,0x00CCCC,0x0066CC,0x0000CC,0x6600CC,0xCC00CC,
  128. 0xCC0066,0x606060,0xFF0000,0xFF8000,0xFFFF00,0x80FF00,0x00FF00,0x00FF80,0x00FFFF,0x0080FF,
  129. 0x0000FF,0x7F00FF,0xFF00FF,0xFF007F,0x808080,0xFF3333,0xFF9933,0xFFFF33,0x99FF33,0x33FF33,
  130. 0x33FF99,0x33FFFF,0x3399FF,0x3333FF,0x9933FF,0xFF33FF,0xFF3399,0xA0A0A0,0xFF6666,0xFFB266,
  131. 0xFFFF66,0xB2FF66,0x66FF66,0x66FFB2,0x66FFFF,0x66B2FF,0x6666FF,0xB266FF,0xFF66FF,0xFF66B2,
  132. 0xC0C0C0,0xFF9999,0xFFCC99,0xFFFF99,0xCCFF99,0x99FF99,0x99FFCC,0x99FFFF,0x99CCFF,0x9999FF,
  133. 0xCC99FF,0xFF99FF,0xFF99CC,0xE0E0E0,0xFFCCCC,0xFFE5CC,0xFFFFCC,0xE5FFCC,0xCCFFCC,0xCCFFE5,
  134. 0xCCFFFF,0xCCE5FF,0xCCCCFF,0xE5CCFF,0xFFCCFF,0xFFCCE5,0xFFFFFF 
  135. };
  136.  
  137. #define LAST_USED_MAX 13
  138. dword last_used_colors[LAST_USED_MAX] = {
  139. 0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,0xFFFFFF,
  140. 0xFFFFFF,0xFFFFFF,0xFFFFFF
  141. };
  142.  
  143. CustomCursor Cursor;
  144. dword CursorBar = FROM "cursors/bar.cur";
  145. dword CursorFill = FROM "cursors/fill.cur";
  146. dword CursorLine = FROM "cursors/line.cur";
  147. dword CursorPencil = FROM "cursors/pencil.cur";
  148. dword CursorPipette = FROM "cursors/pipette.cur";
  149. dword CursorRectangle = FROM "cursors/rectangle.cur";
  150. dword CursorSelect = FROM "cursors/select.cur";
  151.  
  152. _image image;
  153.  
  154. #include "actions_history.h"
  155.  
  156. _ActionsHistory actionsHistory;
  157.  
  158. #include "tools.h"
  159. #include "canvas_resize.h"
  160.  
  161. //===================================================//
  162. //                                                   //
  163. //                       CODE                        //
  164. //                                                   //
  165. //===================================================//
  166.  
  167. libimg_image top_icons;
  168. libimg_image left_icons;
  169.  
  170. void main()
  171. {
  172.         word btn;
  173.         libimg_image open_image;
  174.  
  175.         load_dll(libimg, #libimg_init, 1);
  176.         load_dll(boxlib, #box_lib_init,0);
  177.  
  178.         top_icons.load("/sys/icons16.png");
  179.         left_icons.load("/sys/icons16.png");
  180.         leftbar_w = left_icons.w + 16;
  181.  
  182.         sc.get();
  183.         bg_dark = skin_is_dark();
  184.  
  185.         semi_white = MixColors(sc.work, 0xFFFfff, bg_dark*90 + 96);
  186.         top_icons.replace_color(0xffFFFfff, semi_white);
  187.         top_icons.replace_color(0xffCACBD6, MixColors(semi_white, 0, 220));
  188.  
  189.         left_icons.replace_color(0xffFFFfff, sc.work);
  190.         left_icons.replace_color(0xffCACBD6, MixColors(sc.work, 0, 200));
  191.  
  192.         //fix line and rectandle color for dark skins
  193.         if (bg_dark) left_icons.replace_color(0xff545454, 0xffD3D3D4);
  194.  
  195.         EventSetActiveColor(1, color1);
  196.  
  197.         if (!param[0]) {
  198.                 image.create(32, 32);
  199.         }
  200.         else
  201.         {
  202.                 open_image.load(#param);
  203.                 open_image.convert_into(IMAGE_BPP24);
  204.  
  205.                 if (open_image.w*open_image.h>MAX_CELL_SIZE*MAX_CELL_SIZE) {
  206.                         notify(T_ERROR_IMA_ICONEDIT);
  207.                         ExitProcess();
  208.                 }
  209.                 else {
  210.                         image.create(open_image.h, open_image.w);
  211.                         image.set_image(open_image.imgsrc);
  212.                 }
  213.         }
  214.  
  215.         actionsHistory.init();
  216.  
  217.         initTools();
  218.         setCurrentTool(TOOL_PENCIL);
  219.        
  220.         SetEventMask(EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER);
  221.  
  222.         loop() switch(WaitEvent())
  223.         {
  224.                 case evMouse:
  225.                         if (Window_CanvasReSize.thread_exists()) break;
  226.                         mouse.get();
  227.                        
  228.                         if (mouse.lkm) tool_color = color1;
  229.                         if (mouse.pkm) tool_color = color2;
  230.                         if (mouse.mkm) break;
  231.  
  232.                         hoverX = mouse.x - canvas.x / zoom.value;
  233.                         hoverY = mouse.y - canvas.y / zoom.value;
  234.                         if (hoverX<0) hoverX = 0;
  235.                         if (hoverY<0) hoverY = 0;
  236.                         if (hoverX>image.columns-1) hoverX = image.columns-1;
  237.                         if (hoverY>image.rows-1) hoverY = image.rows-1;
  238.                         canvasMouseMoved = false;
  239.                         if (priorHoverX != hoverX) canvasMouseMoved = true;
  240.                         if (priorHoverY != hoverY) canvasMouseMoved = true;
  241.                         priorHoverX = hoverX;
  242.                         priorHoverY = hoverY;
  243.                         //DrawBar(Form.cwidth-100, 3, 80, 12, 0xFFFfff);
  244.                         //WriteText(Form.cwidth-100, 3, 0x80, 0x000000,
  245.                         //      sprintf(#param, "%i %i", hoverX, hoverY));
  246.  
  247.                         if (currentTool != TOOL_NONE)
  248.                                 tools[currentTool].onMouseEvent(mouse.x, mouse.y, mouse.lkm, mouse.pkm);
  249.  
  250.                         if (mouse.vert) {
  251.                                 if (mouse.vert==65535) zoom.inc();
  252.                                 if (mouse.vert==1) zoom.dec();
  253.                                 DrawEditArea();
  254.                         }
  255.  
  256.                         if (wrapper.hovered()) SetCursor();
  257.                         else Cursor.Restore();
  258.  
  259.                         if (mouse.down) {
  260.                                 if (b_color_gradient.hovered())
  261.                                 || (b_last_colors.hovered())
  262.                                 || (b_default_palette.hovered()) {
  263.                                         if (mouse.key&MOUSE_LEFT) EventSetActiveColor(1, GetPixelUnderMouse());
  264.                                         if (mouse.key&MOUSE_RIGHT) EventSetActiveColor(2, GetPixelUnderMouse());
  265.                                 }      
  266.                         }
  267.  
  268.                         break;
  269.  
  270.                 case evButton:
  271.                         if (Window_CanvasReSize.thread_exists()) break;
  272.                         btn = GetButtonID();
  273.  
  274.                         if (zoom.click(btn)) DrawEditArea();
  275.  
  276.                         button.press(btn);
  277.          
  278.                 case evKey:
  279.                         GetKeys();
  280.  
  281.                         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) key.press(ECTRL + key_scancode);
  282.  
  283.                         if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
  284.                                 if (key_scancode == SCAN_CODE_DEL) EventCleanCanvas();
  285.                         }
  286.  
  287.                         if (currentTool != TOOL_NONE) && (tools[currentTool].onKeyEvent != 0)
  288.                                 tools[currentTool].onKeyEvent(key_scancode);
  289.  
  290.                         key.press(key_scancode);
  291.  
  292.                         if (key_scancode == SCAN_CODE_KEY_Z) actionsHistory.undoLastAction();
  293.                         if (key_scancode == SCAN_CODE_KEY_Y) actionsHistory.redoLastAction();
  294.  
  295.                         if (key_scancode == SCAN_CODE_MINUS) {zoom.dec(); DrawEditArea();}
  296.                         if (key_scancode == SCAN_CODE_PLUS)  {zoom.inc(); DrawEditArea();}
  297.  
  298.                         break;
  299.                  
  300.                 case evReDraw:
  301.                         Window_CanvasReSize.thread_exists();
  302.                         if (CheckActiveProcess(Form.ID)) EventCheckMenuItemSelected();
  303.                         DrawWindow();
  304.                         break;
  305.         }
  306. }
  307.  
  308. void DrawTopPanelButton1(dword _event, _hotkey, _x, _icon_n)
  309. {
  310.         DrawWideRectangle(_x, 4, 22, 22, 3, semi_white);
  311.         PutPixel(_x,4,sc.work);
  312.         PutPixel(_x,4+21,sc.work);
  313.         PutPixel(_x+21,4,sc.work);
  314.         PutPixel(_x+21,4+21,sc.work);
  315.         DefineHiddenButton(_x, 4, 21, 21, button.add(_event));
  316.         img_draw stdcall(top_icons.image, -top_icons.w+16/2+_x+3, -top_icons.w+16/2+7, left_icons.w,
  317.                 left_icons.w, 0, _icon_n*left_icons.w);
  318.         //DrawTopPanelButton(button.add(_event), _x, 5, _icon_n, false);
  319.         if (_hotkey) key.add_n(_hotkey, _event);
  320. }
  321.  
  322.  
  323. int DrawFlatPanelButton(dword _id, _x, _y, _text)
  324. {
  325.         #define P 10
  326.         int w = strlen(_text)*6 + P + P;
  327.         DrawBar(_x, _y, w, 22, semi_white);
  328.         PutPixel(_x,_y,sc.work);
  329.         PutPixel(_x,_y+21,sc.work);
  330.         PutPixel(_x+w-1,_y,sc.work);
  331.         PutPixel(_x+w-1,_y+21,sc.work);
  332.         DefineHiddenButton(_x, _y, w, 21, _id);
  333.         WriteText(_x+P, _y+7, 0x80, sc.work_text, _text);
  334.         return w;
  335. }
  336.  
  337. void DrawLeftPanelButton(dword _event, _hotkey, _y, _icon_n)
  338. {
  339.         int x = 5;
  340.         DrawRectangle(x, _y, left_icons.w + 5, left_icons.w + 5, sc.work);
  341.         DefineHiddenButton(x, _y, left_icons.w + 5, left_icons.w + 5, button.add(_event));
  342.         img_draw stdcall(left_icons.image, x+3, _y+3, left_icons.w,
  343.                 left_icons.w, 0, _icon_n*left_icons.w);
  344.         key.add_n(_hotkey, _event);
  345. }
  346. void DrawStatusBar()
  347. {
  348.         zoom.draw(wrapper.x, wrapper.y + wrapper.h + 6);
  349.  
  350.         sprintf(#param,"%i x %i", image.columns, image.rows);
  351.         DrawCaptButton(
  352.                 wrapper.x+wrapper.w-calc(strlen(#param)*8) -6 - 1,
  353.                 zoom.y,
  354.                 calc(strlen(#param)*8)+6,
  355.                 18,
  356.                 button.add(#EventCanvasResize),
  357.                 sc.button,
  358.                 sc.button_text,
  359.                 #param
  360.                 );
  361. }
  362.  
  363.  
  364. void DrawWindow()
  365. {
  366.         #define GAPH 27
  367.         #define GAPV 28
  368.         #define GAP_S 26+5
  369.         #define GAP_B 26+18
  370.         #define BLOCK_SPACE 10
  371.         incn tx;
  372.         incn ty;
  373.         sc.get();
  374.         DefineAndDrawWindow(115+random(100), 50+random(100), 700, 540, 0x73, NULL, T_TITLE, 0);
  375.         GetProcessInfo(#Form, SelfInfo);
  376.         if (Form.status_window&ROLLED_UP) return;
  377.         if (Form.width  < 560) { MoveSize(OLD,OLD,560,OLD); return; }
  378.         if (Form.height < 430) { MoveSize(OLD,OLD,OLD,430); return; }
  379.         button.init(40);
  380.         key.init(40);
  381.  
  382.         right_bar.x = Form.cwidth - right_bar.w;
  383.         b_color_gradient.x = b_last_colors.x = b_default_palette.x = right_bar.x;
  384.         DrawBar(0, 0, Form.cwidth, TOPBAR_H-1, sc.work);
  385.         DrawBar(0, TOPBAR_H-1, Form.cwidth, 1, sc.work_graph);
  386.  
  387.         DrawTopPanelButton1(#EventCreateNewIcon,  ECTRL + SCAN_CODE_KEY_N, tx.set(5),    2);
  388.         DrawTopPanelButton1(#EventOpenIcon,       ECTRL + SCAN_CODE_KEY_O, tx.inc(GAP_S), 0);
  389.         DrawTopPanelButton1(#EventSaveIconToFile, ECTRL + SCAN_CODE_KEY_S, tx.inc(GAP_S), 5);
  390.         DrawTopPanelButton1(#EventMoveLeft,       ECTRL + SCAN_CODE_LEFT,  tx.inc(GAP_B), 30);
  391.         DrawTopPanelButton1(#EventMoveRight,      ECTRL + SCAN_CODE_RIGHT, tx.inc(GAP_S), 31);
  392.         DrawTopPanelButton1(#EventMoveUp,         ECTRL + SCAN_CODE_UP,    tx.inc(GAP_S), 32);
  393.         DrawTopPanelButton1(#EventMoveDown,       ECTRL + SCAN_CODE_DOWN,  tx.inc(GAP_S), 33);
  394.         DrawTopPanelButton1(#EventFlipHor,        0, tx.inc(GAP_B), 34);
  395.         DrawTopPanelButton1(#EventFlipVer,        0, tx.inc(GAP_S), 35);
  396.         DrawTopPanelButton1(#EventRotateLeft,     ECTRL + SCAN_CODE_KEY_L, tx.inc(GAP_S), 37);
  397.         DrawTopPanelButton1(#EventRotateRight,    ECTRL + SCAN_CODE_KEY_R, tx.inc(GAP_S), 36);
  398.         DrawTopPanelButton1(#EventCrop,           0, tx.inc(GAP_B), 46);
  399.  
  400.         image_menu_btn.x = tx.inc(GAP_B);
  401.         image_menu_btn.w = DrawFlatPanelButton(button.add(#EventShowImageMenu), image_menu_btn.x, image_menu_btn.y, T_MENU_IMAGE);
  402.         //tx.inc(image_menu_btn.w + BLOCK_SPACE);
  403.        
  404.         DrawEditArea();
  405.  
  406.         DrawBar(0, TOPBAR_H, leftbar_w-1, Form.cheight - TOPBAR_H, sc.work);
  407.  
  408.         ty.n = right_bar.y - GAPV - 2;
  409.  
  410.         DrawLeftPanelButton(#EventSelectToolPencil, SCAN_CODE_KEY_P, ty.inc(GAPV), 38);
  411.         DrawLeftPanelButton(#EventSelectToolPick,   SCAN_CODE_KEY_I, ty.inc(GAPV), 39);
  412.         DrawLeftPanelButton(#EventSelectToolFill,   SCAN_CODE_KEY_F, ty.inc(GAPV), 40);
  413.         DrawLeftPanelButton(#EventSelectToolLine,   SCAN_CODE_KEY_L, ty.inc(GAPV), 41);
  414.         DrawLeftPanelButton(#EventSelectToolRect,   SCAN_CODE_KEY_R, ty.inc(GAPV), 42);
  415.         DrawLeftPanelButton(#EventSelectToolBar,    SCAN_CODE_KEY_B, ty.inc(GAPV), 43);
  416.         DrawLeftPanelButton(#EventSelectToolSelect, SCAN_CODE_KEY_S, ty.inc(GAPV), 44);
  417.         DrawLeftPanelButton(#EventSelectToolScrCopy,SCAN_CODE_KEY_Q, ty.inc(GAPV), 45);
  418.         DrawLeftPanelSelection();
  419.  
  420.         button.add_n(1, #EventExitIconEdit);
  421.         key.add_n(ECTRL + SCAN_CODE_KEY_T, #EventTestIcon);
  422.  
  423.         DrawBar(wrapper.x+wrapper.w, TOPBAR_H, Form.cwidth-wrapper.x-wrapper.w,
  424.                 Form.cheight - TOPBAR_H, sc.work);
  425.         DrawActiveColor(right_bar.y);
  426.         DrawColorPallets();
  427.         DrawPreview();
  428.  
  429.         DrawBar(leftbar_w-1, wrapper.y + wrapper.h, wrapper.w+1,
  430.                 Form.cheight - wrapper.y - wrapper.h, sc.work);
  431.         DrawStatusBar();
  432. }
  433.  
  434. void DrawLeftPanelSelection()
  435. {
  436.         if (previousTool!=-1) DrawRectangle3D(5, previousTool*GAPV+right_bar.y-2, left_icons.w+5, left_icons.w+5, sc.work, sc.work);
  437.         DrawRectangle3D(5, currentTool*GAPV+right_bar.y-2, left_icons.w+5, left_icons.w+5, 0x333333, 0x777777);
  438. }
  439.  
  440. void DrawEditArea()
  441. {
  442.         dword color1=0xC0C0C0;
  443.         int top_side;
  444.         int left_side;
  445.  
  446.         wrapper.x = left_icons.w + 16;
  447.         wrapper.w = Form.cwidth - right_bar.w - 10 - wrapper.x;
  448.         wrapper.h = Form.cheight - TOPBAR_H - 35;
  449.  
  450.         //canvas{
  451.         canvas.w = image.columns * zoom.value;
  452.         canvas.h = image.rows * zoom.value;
  453.         if (canvas.w+2 > wrapper.w) || (canvas.h+2 > wrapper.h) {
  454.                 zoom.value--;
  455.                 if (zoom.x) zoom.redraw();
  456.                 DrawEditArea();
  457.                 return;
  458.         }
  459.         canvas.x = -zoom.value*image.columns+wrapper.w/2 + wrapper.x;
  460.         canvas.y = -zoom.value*image.rows+wrapper.h/2 + wrapper.y;
  461.         DrawCanvas();
  462.         //}
  463.  
  464.         left_side = canvas.x-wrapper.x-1;
  465.         top_side = canvas.y-wrapper.y-1;
  466.  
  467.         DrawRectangle(wrapper.x-1, wrapper.y-1, wrapper.w, wrapper.h, sc.work_graph);
  468.  
  469.         if (left_side>0)
  470.         {
  471.                 DrawBar(wrapper.x, wrapper.y, wrapper.w-1, top_side, color1); //top
  472.                 DrawBar(wrapper.x, wrapper.y+wrapper.h-top_side-1, wrapper.w-1, top_side, color1); //bottom
  473.         }
  474.         if (top_side>0)
  475.         {
  476.                 //left
  477.                 DrawBar(wrapper.x, wrapper.y+top_side, left_side,
  478.                         wrapper.h-top_side-top_side, color1);
  479.                 //right
  480.                 DrawBar(wrapper.x+wrapper.w-left_side-1, wrapper.y+top_side, left_side,
  481.                         wrapper.h-top_side-top_side, color1);
  482.         }
  483.         DrawRectangle(canvas.x-1, canvas.y-1, canvas.w+1, canvas.h+1, 0x808080);
  484. }
  485.  
  486. void DrawActiveColor(dword iny)
  487. {
  488.         #define CELL 20
  489.         static dword outy;
  490.         if (iny != NULL) outy = iny;
  491.         DrawFrame(right_bar.x, outy, CELL, CELL, NULL);
  492.         DrawBar(right_bar.x+2, outy+2, CELL-4, CELL-4, color1);
  493.  
  494.         DrawFrame(right_bar.x+CELL+5, outy, CELL, CELL, NULL);
  495.         DrawBar(right_bar.x+CELL+5+2, outy+2, CELL-4, CELL-4, color2);
  496.  
  497.         //sprintf(#param, "%A", color1);
  498.         //WriteTextWithBg(right_bar.x+30, outy+3, 0xD0, sc.work_text, #param+4, sc.work);
  499.         DrawCurrentColorGradient();
  500. }
  501.  
  502. int lmax;
  503. void GenerateCurrentColorGradient()
  504. {
  505.         int i, avg, rmax;
  506.  
  507.         rgb.DwordToRgb(color1);
  508.         avg = 255 - calc(rgb.r + rgb.g + rgb.b / 3);
  509.  
  510.         lmax = b_color_gradient.w *avg/255 | 1;
  511.         rmax = b_color_gradient.w - lmax | 1;
  512.         if (lmax == 0) lmax=1;
  513.         if (rmax == 0) rmax=1;
  514.        
  515.         for (i=0; i<lmax; i++) {
  516.                 linear_gradient[i] = MixColors(color1,0xFFFfff,255*i/lmax);
  517.         }
  518.  
  519.         for (i=0 ; i<=rmax; i++) {
  520.                 linear_gradient[lmax+rmax - i] = MixColors(color1,0x000000,255*i/rmax);
  521.         }
  522. }
  523.  
  524. int DrawGradientMarker(dword marker_x, marker_color)
  525. {
  526.         if (marker_x > b_color_gradient.w - 1) marker_x = b_color_gradient.w - 1;
  527.         DrawBar(b_color_gradient.x + marker_x-2, b_color_gradient.y-3, 5, 1, marker_color);
  528.         DrawBar(b_color_gradient.x + marker_x-1, b_color_gradient.y-2, 3, 1, marker_color);
  529.         PutPixel(b_color_gradient.x + marker_x, b_color_gradient.y-1, marker_color);
  530.         return marker_x;
  531. }
  532.  
  533. int old_marker_pos;
  534. void DrawCurrentColorGradient()
  535. {
  536.         int i;
  537.         for (i=0 ; i<b_color_gradient.w; i++) {
  538.                 DrawBar(b_color_gradient.x+i, b_color_gradient.y, 1, b_color_gradient.h, linear_gradient[i]);          
  539.         }
  540.         DrawGradientMarker(old_marker_pos, sc.work);
  541.         old_marker_pos = DrawGradientMarker(lmax, 0xFFFfff * bg_dark);
  542. }
  543.  
  544. void DrawColorPallets()
  545. {
  546.         int r, c, i=0;
  547.         //Last used colors
  548.         for (r = 0; r < LAST_USED_MAX/PAL_ITEMS_X_COUNT; r++)
  549.         {
  550.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  551.                 {
  552.                         DrawBar(c*COLSIZE + b_last_colors.x, r*COLSIZE + b_last_colors.y,
  553.                                 COLSIZE, COLSIZE, last_used_colors[i]);
  554.                 }
  555.         }
  556.         i=0;
  557.         //Default colors
  558.         for (r = 0; r < 9; r++)
  559.         {
  560.                 for (c = 0; c < PAL_ITEMS_X_COUNT; c++, i++)
  561.                 {
  562.                         DrawBar(c*COLSIZE + b_default_palette.x, r*COLSIZE + b_default_palette.y,
  563.                                 COLSIZE, COLSIZE, default_palette[PALLETE_SIZE-i]);
  564.                 }
  565.         }
  566. }
  567.  
  568. void DrawCanvasPixel(dword _r,_c,_color)
  569. {
  570.         DrawBar(_c*zoom.value + canvas.x, _r*zoom.value + canvas.y,
  571.         zoom.value, zoom.value, _color);
  572. }
  573.  
  574. void DrawCanvas()
  575. {
  576.         int r, c;
  577.         dword color;
  578.  
  579.         if ((currentTool != TOOL_NONE) && (tools[currentTool].onCanvasDraw != 0))
  580.         {
  581.                 tools[currentTool].onCanvasDraw();
  582.         }
  583.  
  584.         for (r = 0; r < image.rows; r++)
  585.         {
  586.                 for (c = 0; c < image.columns; c++)
  587.                 {
  588.                         if (image.pixel_state.is_drawable(r,c))
  589.                                 DrawCanvasPixel(r, c, image.get_pixel(r,c));
  590.                 }
  591.         }
  592.         image.pixel_state.reset_and_set_all_drawable();
  593.  
  594.         DrawPreview();
  595. }
  596.  
  597. void DrawPreview()
  598. {
  599.         int x = right_bar.x;
  600.         int y = b_default_palette.y + b_default_palette.h + 6;
  601.         int preview_h = Form.cheight - y;
  602.  
  603.         if (image.columns > right_bar.w) return;
  604.         if (image.rows > preview_h) return;
  605.  
  606.         _PutImage(right_bar.w - image.columns / 2 + x - 3,
  607.                 preview_h - image.rows / 2 + y,
  608.                 image.columns, image.rows, image.get_image()
  609.                 );
  610. }
  611.  
  612. dword GetPixelUnderMouse()
  613. {
  614.         return GetPixelColorFromScreen(mouse.x + Form.left + 5, mouse.y + Form.top + skin_height);
  615. }
  616.  
  617. int preview_size = 128;
  618. void DrawImageWithBg(dword _x, _y, _col_to)
  619. {
  620.         _x *= preview_size;
  621.         _y *= preview_size;
  622.         DrawWideRectangle(_x,_y, preview_size, preview_size, preview_size-image.columns/2, _col_to);
  623.         _PutImage(preview_size - image.columns / 2 + _x, preview_size - image.rows / 2 + _y,
  624.                 image.columns, image.rows, image.get_image_with_replaced_color(color2, _col_to));
  625. }
  626.  
  627. void ShowWindow_TestIcon()
  628. {
  629.         if (image.rows>=preview_size) || (image.columns>=preview_size) {
  630.                 notify(T_TOO_BIG_IMAGE_FOR_PREVIEW);
  631.                 return;
  632.         }
  633.         loop() switch(WaitEvent())
  634.         {
  635.                 case evButton:
  636.                         if (GetButtonID()) ExitProcess();
  637.                         break;
  638.          
  639.                 case evKey:
  640.                         GetKeys();
  641.                         if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  642.                         break;
  643.                  
  644.                 case evReDraw:
  645.                         DefineAndDrawWindow(Form.left+100, Form.top+100, preview_size*2+9,
  646.                                 preview_size*2+skin_height+4, 0x74, NULL, T_TEST_ICON, 0);
  647.                         DrawImageWithBg(0, 0, 0x000000);
  648.                         DrawImageWithBg(1, 0, 0xFFFfff);
  649.                         DrawImageWithBg(0, 1, GetPixelColorFromScreen(0, 0));
  650.                         DrawImageWithBg(1, 1, sc.work);
  651.                         break;
  652.         }
  653. }
  654.  
  655. //===================================================//
  656. //                                                   //
  657. //                      EVENTS                       //
  658. //                                                   //
  659. //===================================================//
  660.  
  661. void EventCreateNewIcon()
  662. {
  663.         EventSaveIconToFile();
  664.         Window_CanvasReSize.create();
  665. }
  666.  
  667. void EventOpenIcon()
  668. {
  669.         RunProgram("/sys/lod", sprintf(#param, "*png* %s",#program_path));
  670. }
  671.  
  672. void EventSaveIconToFile()
  673. {
  674.         int i=0;
  675.         char save_file_name[4096];
  676.         char save_path_stable[4096];
  677.         strcpy(#save_path_stable, "/tmp0/1");
  678.         do {
  679.                 i++;
  680.                 sprintf(#save_file_name, "%s/saved_icon_%i.png", #save_path_stable, i);
  681.         } while (file_exists(#save_file_name));
  682.         save_image(image.get_image(), image.columns, image.rows, #save_file_name);
  683. }
  684.  
  685. void EventCleanCanvas()
  686. {
  687.         image.create(image.rows, image.columns);
  688.         actionsHistory.saveCurrentState();
  689.         DrawCanvas();
  690. }
  691.  
  692. void EventExitIconEdit()
  693. {
  694.         EventSaveIconToFile();
  695.         ExitProcess();
  696. }
  697.  
  698. void EventSetActiveColor(int _number, _color)
  699. {
  700.         int i;
  701.         if (last_used_colors[0] == _color) return;
  702.         for (i=LAST_USED_MAX-1; i>0; i--) {
  703.                 last_used_colors[i] = last_used_colors[i-1];
  704.         }
  705.         last_used_colors[0] = _color;
  706.  
  707.         if (_number == 1) color1 = _color;
  708.         if (_number == 2) color2 = _color;
  709.  
  710.         if (b_color_gradient.hovered()) {
  711.                 lmax = mouse.x - b_color_gradient.x;
  712.         }
  713.         else {
  714.                 GenerateCurrentColorGradient();
  715.         }
  716.         DrawActiveColor(NULL);
  717.         DrawColorPallets();
  718. }
  719.  
  720. void EventTestIcon()
  721. {
  722.         CreateThread(#ShowWindow_TestIcon, #test_icon_stak+4092);
  723. }
  724.  
  725. void EventMove(dword _action)
  726. {
  727.         if (selection.state) {
  728.                 selection.buf.move(_action);
  729.                 SelectTool_onCanvasDraw();
  730.         }
  731.         else {
  732.                 image.move(_action);
  733.                 DrawCanvas();
  734.         }
  735.         actionsHistory.saveCurrentState();
  736. }
  737.  
  738. void EventCrop()
  739. {
  740.         if (selection.state) {
  741.                 EventSaveIconToFile();
  742.                 image.create(selection.buf.rows, selection.buf.columns);
  743.                 selection.move_to_point(0,0);
  744.                 selection.apply_to_image();
  745.                 selection.reset();
  746.                 actionsHistory.init();
  747.                 DrawWindow();
  748.         }
  749.         else {
  750.                 notify(T_ERROR_CROP_TOOL);
  751.         }
  752. }
  753.  
  754. void EventShowImageMenu()
  755. {
  756.         open_lmenu(image_menu_btn.x, image_menu_btn.y + image_menu_btn.h,
  757.                 MENU_TOP_LEFT, NULL, #image_menu_items);
  758. }
  759.  
  760. void EventShowEditMenu()
  761. {
  762.         open_lmenu(image_menu_btn.x, image_menu_btn.y + image_menu_btn.h,
  763.                 MENU_TOP_LEFT, NULL, #edit_menu_items);
  764. }
  765.  
  766. void EventCheckMenuItemSelected()
  767. {
  768.         switch(get_menu_click()) {
  769.                 case 1:
  770.                         EventCountColorsUsed();
  771.                         break;
  772.                 case 2:
  773.                         EventReplaceImageColors(color1, color2);
  774.                         break;
  775.                 case 3:
  776.                         EventTestIcon();
  777.                         break;
  778.         }
  779. }
  780.  
  781. void EventCountColorsUsed()
  782. {
  783.         char res_str[64];
  784.         int cur, prev;
  785.         int max = image.rows*image.columns;
  786.         int resi=0;
  787.         bool unic;
  788.         for (cur=0; cur<max; cur++) {
  789.                 unic = true;
  790.                 for (prev=0; prev<cur; prev++) {
  791.                         if (image.mas[prev] == image.mas[cur]) {unic=false; break;}
  792.                 }
  793.                 if (unic) resi++;
  794.         }
  795.         notify( sprintf(#res_str, T_UNIC_COLORS_COUNT, resi) );
  796. }
  797.  
  798. void EventReplaceImageColors(dword c1, c2)
  799. {
  800.         int max = image.rows*image.columns;
  801.         int cur;
  802.         for (cur=0; cur<max; cur++) {
  803.                 if (image.mas[cur] == color1) image.mas[cur] = color2;
  804.         }
  805. }
  806.  
  807. void EventCanvasResize()
  808. {
  809.         notify("Sorry, not implemented yet.");
  810. }
  811.  
  812. void EventMoveLeft() { EventMove(MOVE_LEFT); }
  813. void EventMoveRight() { EventMove(MOVE_RIGHT); }
  814. void EventMoveUp() { EventMove(MOVE_UP); }
  815. void EventMoveDown() { EventMove(MOVE_DOWN); }
  816. void EventFlipHor() { EventMove(FLIP_HOR); }
  817. void EventFlipVer() { EventMove(FLIP_VER); }
  818. void EventRotateLeft() { EventMove(ROTATE_LEFT); }
  819. void EventRotateRight() { EventMove(ROTATE_RIGHT); }
  820.  
  821. void EventSelectToolPencil() { setCurrentTool(TOOL_PENCIL); }
  822. void EventSelectToolPick() { setCurrentTool(TOOL_PIPETTE); }
  823. void EventSelectToolFill() { setCurrentTool(TOOL_FILL); }
  824. void EventSelectToolLine() { setCurrentTool(TOOL_LINE); }
  825. void EventSelectToolRect() { setCurrentTool(TOOL_RECT); }
  826. void EventSelectToolBar() { setCurrentTool(TOOL_BAR); }
  827. void EventSelectToolSelect() { setCurrentTool(TOOL_SELECT); }
  828. void EventSelectToolScrCopy() { setCurrentTool(TOOL_SCREEN_COPY);  }
  829.  
  830. char test_icon_stak22[4096];
  831.  
  832. stop:
  833.  
  834. char test_icon_stak[4096];
  835.